From 62f30c0ffa07ca990245931c24bb6b4689830758 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Franck=20P=C3=A9rignon?= <franck.perignon@imag.fr>
Date: Fri, 24 May 2013 14:37:15 +0000
Subject: [PATCH] Add methods to serialize the problem and restart a simulation
 from a file

---
 HySoP/hysop/domain/box.py                |  4 ++--
 HySoP/hysop/fields/discrete.py           |  3 +++
 HySoP/hysop/operator/monitors/printer.py | 23 ++++++++++++----------
 HySoP/hysop/operator/poisson.py          |  4 ++--
 HySoP/hysop/problem/problem.py           | 25 +++++++++++++++---------
 HySoP/hysop/problem/simulation.py        | 10 ++++++----
 HySoP/hysop/problem/transport.py         |  5 +++--
 7 files changed, 45 insertions(+), 29 deletions(-)

diff --git a/HySoP/hysop/domain/box.py b/HySoP/hysop/domain/box.py
index 650df43c4..92fcd9755 100644
--- a/HySoP/hysop/domain/box.py
+++ b/HySoP/hysop/domain/box.py
@@ -28,13 +28,13 @@ class Box(Domain):
         @param dimension : Box dimension. Default: 3
         @param length : Box length. Default [1.0, 1.0, 1.0]
         @param origin : Box minimum position. Default [0., 0., 0.]
-
+        \code
         >>> import parmepy as pp
         >>> import numpy as np
         >>> b = pp.Box()
         >>> (b.max == np.asarray([1.0, 1.0, 1.0])).all()
         True
-
+        \endcode
         """
         if not (dimension == len(length) and dimension == len(origin)):
             raise ValueError("Box parameters inconsistents dimensions")
diff --git a/HySoP/hysop/fields/discrete.py b/HySoP/hysop/fields/discrete.py
index da71fd982..8502f487b 100644
--- a/HySoP/hysop/fields/discrete.py
+++ b/HySoP/hysop/fields/discrete.py
@@ -145,8 +145,11 @@ class DiscreteField(object):
         filename += '_rk_'
         filename += str(main_rank)
         db = NumPyDB_cPickle(filename, mode='load')
+        print "lllll", self.name
         if self.isVector:
             for dim in xrange(self.dimension):
+                print dim, len(self.data)
+                print db.load(self.name)
                 self.data[dim] = db.load(self.name)[0][dim]
         else:
             self.data = db.load(self.name)[0]
diff --git a/HySoP/hysop/operator/monitors/printer.py b/HySoP/hysop/operator/monitors/printer.py
index f433e94db..2e0f1c32d 100644
--- a/HySoP/hysop/operator/monitors/printer.py
+++ b/HySoP/hysop/operator/monitors/printer.py
@@ -3,7 +3,6 @@
 
 Classes for handling ouputs.
 """
-from parmepy import __VERBOSE__
 from parmepy.constants import np, S_DIR, PARMES_REAL, debug
 from parmepy.operator.monitors.monitoring import Monitoring
 import evtk.hl as evtk
@@ -48,6 +47,14 @@ class Printer(Monitoring):
             self.step = self._printStep
         else:
             self.step = self._passStep
+        ## Internal counter
+        self._call_number = 0
+
+    def setUp(self):
+        """
+        Print initial state
+        """
+        self.step(0)
 
     @debug
     @timed_function
@@ -86,7 +93,7 @@ class Printer(Monitoring):
             raise ValueError("Cannot set non callable method to get data " +
                              "to print. Given method : " + str(method))
 
-    def _passStep(self, ite):
+    def _passStep(self, ite=None):
         """A code method that do nothing."""
         pass
 
@@ -97,8 +104,7 @@ class Printer(Monitoring):
         @param iter : current iteration number
         """
         if (ite % self.freq) == 0:
-            if __VERBOSE__:
-                print "== IO"
+            self._call_number += 1
             # Transfer from GPU to CPU if required
             for f in self.variables:
                 for df in f.discreteFields.values():
@@ -109,8 +115,6 @@ class Printer(Monitoring):
             # Set output file name
             filename = self.prefix + '_it_' + str(ite) + self.ext
             print "Print to file " + filename
-            if __VERBOSE__:
-                print "Print file : " + filename
             ## VTK output \todo: Need fix in 2D, getting an IOError.
             try:
                 evtk.imageToVTK(filename, pointData=self._build_vtk_dict())
@@ -159,10 +163,9 @@ class Printer(Monitoring):
                                                     df[i, j, k]))
                                     f.write("\n")
                     f.close()
-            if __VERBOSE__:
-                print "=="
+            print "==\n"
 
 if __name__ == "__main__":
     print __doc__
-    print "- Provided class : " + providedClass
-    print eval(providedClass).__doc__
+    print "- Provided class : " + Printer
+    print Printer.__doc__
diff --git a/HySoP/hysop/operator/poisson.py b/HySoP/hysop/operator/poisson.py
index 43967904a..0782abf01 100644
--- a/HySoP/hysop/operator/poisson.py
+++ b/HySoP/hysop/operator/poisson.py
@@ -42,8 +42,6 @@ class Poisson(Operator):
         ## the same resolution.
         self.resolutions = resolutions
 
-        self.input = [self.vorticity]
-        self.output = [self.velocity]
         Operator.__init__(self, [velocity, vorticity], method, name="Poisson")
         self.config = other_config
         if method is None:
@@ -53,6 +51,8 @@ class Poisson(Operator):
             assert self.resolution == self.resolutions[self.vorticity],\
                 'Poisson error : for fftw, all variables must have\
                 the same global resolution.'
+        self.input = [self.vorticity]
+        self.output = [self.velocity]
 
     @debug
     def setUp(self):
diff --git a/HySoP/hysop/problem/problem.py b/HySoP/hysop/problem/problem.py
index 3b87aa76f..a1c30c329 100644
--- a/HySoP/hysop/problem/problem.py
+++ b/HySoP/hysop/problem/problem.py
@@ -79,7 +79,7 @@ class Problem(object):
         else:
             self.name = name
         ## Default file name prefix for dump.
-        self.filename = str(name)
+        self.filename = str(self.name)
         self._filedump = self.filename + '_rk_' + str(main_rank)
 
     @debug
@@ -92,11 +92,6 @@ class Problem(object):
             if not isinstance(op, Monitoring):
                 if not isinstance(op, Redistribute):
                     op.setUp()
-        for op in self.operators:
-            if isinstance(op, Monitoring):
-                op.setUp()
-            if isinstance(op, Redistribute):
-                op.setUp()
 
         if  __VERBOSE__:
             print "==== Variables initialization ===="
@@ -116,6 +111,12 @@ class Problem(object):
         for v in self.input:
             v.initialize()
 
+        for op in self.operators:
+            if isinstance(op, Monitoring):
+                op.setUp()
+            if isinstance(op, Redistribute):
+                op.setUp()
+
         if __VERBOSE__:
             print "===="
         for op in self.operators:
@@ -138,9 +139,6 @@ class Problem(object):
         Displays timings at simulation end.
         """
         print "\n\n Start solving ..."
-        for op in self.operators:
-            if isinstance(op, Monitoring):
-                op.apply(self.simulation)
         try:
         ## Pass the main loop to the OpenGL viewer
             if not self.glRenderer is None:
@@ -203,6 +201,8 @@ class Problem(object):
         db = NumPyDB_cPickle(self._filedump, mode='store')
         db.dump(self.simulation, 'simulation')
         for v in self.input:
+            print 'dump v ...'
+            print v
             v.dump(self.filename, mode='append')
 
     def restart(self, filename=None):
@@ -221,8 +221,15 @@ class Problem(object):
         self.simulation = db.load('simulation')[0]
         self.simulation.reset()
         for v in self.input:
+            print "load ...", self.filename
             v.load(self.filename)
 
+        for op in self.operators:
+            if isinstance(op, Monitoring):
+                op.setUp()
+            if isinstance(op, Redistribute):
+                op.setUp()
+
     def setDumpFreq(self, freq):
         """
         set rate of problem.dump call (every 'rate' iteration)
diff --git a/HySoP/hysop/problem/simulation.py b/HySoP/hysop/problem/simulation.py
index ae8590bd6..2d484fd91 100644
--- a/HySoP/hysop/problem/simulation.py
+++ b/HySoP/hysop/problem/simulation.py
@@ -28,7 +28,7 @@ class Simulation(object):
         ## Is simulation is terminated
         self.isOver = False
         ## Iteration counter
-        self.currentIteration = 0
+        self.currentIteration = 1
         ## Simulation time step
         self.timeStep = timeStep
         ## Maximum number of iterations
@@ -38,18 +38,20 @@ class Simulation(object):
         """
         Proceed to next time
         """
-        if self.currentIteration < self.iterMax and self.time < self.end:
+        if self.currentIteration < self.iterMax:
             self.currentIteration += 1
             self.time += self.timeStep
         else:
             self.isOver = True
+        if self.time >= self.end:
+            self.isOver = True
 
     def printState(self):
         """
         print current state
         """
         if main_rank == 0:
-            print "\n==== Iteration : {0:3d}   t={1:6.3f} ====".format(
+            print "==== Iteration : {0:3d}, start from t ={1:6.3f} ====".format(
                 self.currentIteration, self.time)
 
     def reset(self):
@@ -59,7 +61,7 @@ class Simulation(object):
         """
         self.start = self.time
         self.isOver = False
-        self.currentIteration = 0
+        self.currentIteration = 1
 
     def __str__(self):
         s = "Simulation parameters : "
diff --git a/HySoP/hysop/problem/transport.py b/HySoP/hysop/problem/transport.py
index 39ddd844f..2360ee801 100644
--- a/HySoP/hysop/problem/transport.py
+++ b/HySoP/hysop/problem/transport.py
@@ -10,9 +10,10 @@ class TransportProblem(Problem):
     """
     Transport problem description.
     """
-    def __init__(self, operators, simulation, monitors=[]):
+    def __init__(self, operators, simulation, monitors=[],
+                 dumpFreq=100, name=None):
         Problem.__init__(self, operators, simulation, monitors,
-                         name="Transport Problem")
+                         dumpFreq, name)
         self.advection, self.velocity = None, None
         for op in self.operators:
             if isinstance(op, Advection):
-- 
GitLab