From 6e475ec2bf01aae44bc4b722cc99449e3d4f330d Mon Sep 17 00:00:00 2001
From: Daniel Wheeler <daniel.wheeler2@gmail.com>
Date: Fri, 16 Nov 2012 10:54:18 -0500
Subject: [PATCH] Fixing ticket:482 by passing `*args` and `**kwargs` for grid
 meshes

The `_GridRepresentation` class was assuming that grid meshes take a
`_RepresentationClass`. However, a number of derived grid classes had
not had their `__init__`s modified to take a `_GridRepresentation`
class. They now take and pass `*args` and `**kwargs` to their parents.

 * Pickling tests were added in `gridRepresentation.py`.

 * Jon says that there is a better way to pickle and unpickle not
   involving calling `__init__`, but will be a pervasive change
   throughout FiPy (not sure which ticket this is).
---
 fipy/meshes/periodicGrid1D.py                     |    4 +-
 fipy/meshes/periodicGrid2D.py                     |    4 +-
 fipy/meshes/representations/gridRepresentation.py |   45 ++++++++++++++++++++-
 fipy/meshes/skewedGrid2D.py                       |    5 +--
 fipy/meshes/test.py                               |    3 +-
 5 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/fipy/meshes/periodicGrid1D.py b/fipy/meshes/periodicGrid1D.py
index b48350f..ab0c107 100755
--- a/fipy/meshes/periodicGrid1D.py
+++ b/fipy/meshes/periodicGrid1D.py
@@ -80,11 +80,11 @@ class PeriodicGrid1D(Grid1D):
         ...                        [0, 1, 0]]) # doctest: +PROCESSOR_0
         True
     """
-    def __init__(self, dx = 1., nx = None, overlap=2):
+    def __init__(self, dx = 1., nx = None, overlap=2, *args, **kwargs):
 
         Grid1D.__init__(self, dx = dx, nx = nx, overlap=overlap,
                         _BuilderClass=_PeriodicGrid1DBuilder,
-                        _TopologyClass=_PeriodicGrid1DTopology)
+                        _TopologyClass=_PeriodicGrid1DTopology, *args, **kwargs)
         self._nonPeriodicCellFaceIDs = numerix.array(super(Grid1D, self).cellFaceIDs)
         self._makePeriodic()
 
diff --git a/fipy/meshes/periodicGrid2D.py b/fipy/meshes/periodicGrid2D.py
index bcdbea0..95f8945 100755
--- a/fipy/meshes/periodicGrid2D.py
+++ b/fipy/meshes/periodicGrid2D.py
@@ -44,8 +44,8 @@ from fipy.meshes.grid2D import Grid2D
 __all__ = ["PeriodicGrid2D", "PeriodicGrid2DLeftRight", "PeriodicGrid2DTopBottom"]
 
 class _BasePeriodicGrid2D(Grid2D):
-    def __init__(self, dx = 1., dy = 1., nx = None, ny = None, overlap=2, communicator=parallel):
-        super(_BasePeriodicGrid2D, self).__init__(dx = dx, dy = dy, nx = nx, ny = ny, overlap=overlap, communicator=communicator)
+    def __init__(self, dx = 1., dy = 1., nx = None, ny = None, overlap=2, communicator=parallel, *args, **kwargs):
+        super(_BasePeriodicGrid2D, self).__init__(dx = dx, dy = dy, nx = nx, ny = ny, overlap=overlap, communicator=communicator, *args, **kwargs)
         self._nonPeriodicCellVertexIDs = super(_BasePeriodicGrid2D, self)._cellVertexIDs
         self._orderedCellVertexIDs_data = super(_BasePeriodicGrid2D, self)._orderedCellVertexIDs        
         self._nonPeriodicCellFaceIDs = numerix.array(super(_BasePeriodicGrid2D, self).cellFaceIDs)
diff --git a/fipy/meshes/representations/gridRepresentation.py b/fipy/meshes/representations/gridRepresentation.py
index 4447bef..8e55ccb 100644
--- a/fipy/meshes/representations/gridRepresentation.py
+++ b/fipy/meshes/representations/gridRepresentation.py
@@ -62,7 +62,44 @@ class _GridRepresentation(_AbstractRepresentation):
                 dnstr.append(n + "=" + str(self.mesh.args[n]))
 
         return "%s(%s)" % (self.mesh.__class__.__name__, ", ".join(dnstr))
-                               
+
+    def _test(self):
+        """
+
+        Check that the following grid classes can be pickled and unpickled.
+
+        >>> import fipy as fp
+
+        >>> m = fp.PeriodicGrid2DLeftRight(nx=10, ny=10)
+        >>> v = fp.CellVariable(mesh=m, value=m.x)
+        >>> fp.dump.write(v, filename='dump.gz')
+        >>> v0 = fp.dump.read(filename='dump.gz')
+        >>> print (v == v0.mesh.x).all()
+        True
+        
+        >>> m = fp.PeriodicGrid1D(nx=10)
+        >>> v = fp.CellVariable(mesh=m, value=m.x)
+        >>> fp.dump.write(v, filename='dump.gz')
+        >>> v0 = fp.dump.read(filename='dump.gz')
+        >>> print (v == v0.mesh.x).all()
+        True
+        
+        >>> m = fp.Tri2D(nx=10, ny=10)
+        >>> v = fp.CellVariable(mesh=m, value=m.x)
+        >>> fp.dump.write(v, filename='dump.gz')
+        >>> v0 = fp.dump.read(filename='dump.gz')
+        >>> print (v == v0.mesh.x).all()
+        True
+        
+        >>> m = fp.SkewedGrid2D(nx=10, ny=10)
+        >>> v = fp.CellVariable(mesh=m, value=m.x)
+        >>> fp.dump.write(v, filename='dump.gz')
+        >>> v0 = fp.dump.read(filename='dump.gz')
+        >>> print (v == v0.mesh.x).all()
+        True
+        
+        """
+        
 class _Grid1DRepresentation(_GridRepresentation):
 
     def repr(self):
@@ -78,3 +115,9 @@ class _Grid3DRepresentation(_GridRepresentation):
     def repr(self):
         return self._repr(dns=[("dx", "nx"), ("dy", "ny"), ("dz", "nz")])
  
+def _test():
+    import fipy.tests.doctestPlus
+    return fipy.tests.doctestPlus.testmod()
+
+if __name__ == "__main__":
+    _test()
diff --git a/fipy/meshes/skewedGrid2D.py b/fipy/meshes/skewedGrid2D.py
index 5fb7152..5b45d54 100644
--- a/fipy/meshes/skewedGrid2D.py
+++ b/fipy/meshes/skewedGrid2D.py
@@ -49,7 +49,7 @@ class SkewedGrid2D(Mesh2D):
     vertical faces.  The points are skewed by a random amount (between `rand`
     and `-rand`) in the X and Y directions.
     """
-    def __init__(self, dx = 1., dy = 1., nx = None, ny = 1, rand = 0):
+    def __init__(self, dx = 1., dy = 1., nx = None, ny = 1, rand = 0, *args, **kwargs):
         self.args = {
             'dx': dx, 
             'dy': dy, 
@@ -92,8 +92,7 @@ class SkewedGrid2D(Mesh2D):
         
         cells = self.grid.cellFaceIDs
 
-        Mesh2D.__init__(self, changedVertices, faces, cells,
-                        _RepresentationClass=_Grid2DRepresentation, _TopologyClass=_Grid2DTopology)
+        Mesh2D.__init__(self, changedVertices, faces, cells, *args, **kwargs)
         
         self.scale = scale
         
diff --git a/fipy/meshes/test.py b/fipy/meshes/test.py
index 02e1e8a..beea117 100755
--- a/fipy/meshes/test.py
+++ b/fipy/meshes/test.py
@@ -58,7 +58,8 @@ def _suite():
         'fipy.meshes.cylindricalGrid1D',
         'fipy.meshes.cylindricalGrid2D',
         'fipy.meshes.factoryMeshes',
-        'fipy.meshes.abstractMesh'))
+        'fipy.meshes.abstractMesh',
+        'fipy.meshes.representations.gridRepresentation'))
     
 if __name__ == '__main__':
     fipy.tests.testProgram.main(defaultTest='_suite')
-- 
1.7.10.4

