Skip to content
Snippets Groups Projects
Commit 62f30c0f authored by Franck Pérignon's avatar Franck Pérignon
Browse files

Add methods to serialize the problem and restart a simulation from a file

parent 27a862a9
No related branches found
No related tags found
No related merge requests found
...@@ -28,13 +28,13 @@ class Box(Domain): ...@@ -28,13 +28,13 @@ class Box(Domain):
@param dimension : Box dimension. Default: 3 @param dimension : Box dimension. Default: 3
@param length : Box length. Default [1.0, 1.0, 1.0] @param length : Box length. Default [1.0, 1.0, 1.0]
@param origin : Box minimum position. Default [0., 0., 0.] @param origin : Box minimum position. Default [0., 0., 0.]
\code
>>> import parmepy as pp >>> import parmepy as pp
>>> import numpy as np >>> import numpy as np
>>> b = pp.Box() >>> b = pp.Box()
>>> (b.max == np.asarray([1.0, 1.0, 1.0])).all() >>> (b.max == np.asarray([1.0, 1.0, 1.0])).all()
True True
\endcode
""" """
if not (dimension == len(length) and dimension == len(origin)): if not (dimension == len(length) and dimension == len(origin)):
raise ValueError("Box parameters inconsistents dimensions") raise ValueError("Box parameters inconsistents dimensions")
......
...@@ -145,8 +145,11 @@ class DiscreteField(object): ...@@ -145,8 +145,11 @@ class DiscreteField(object):
filename += '_rk_' filename += '_rk_'
filename += str(main_rank) filename += str(main_rank)
db = NumPyDB_cPickle(filename, mode='load') db = NumPyDB_cPickle(filename, mode='load')
print "lllll", self.name
if self.isVector: if self.isVector:
for dim in xrange(self.dimension): 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] self.data[dim] = db.load(self.name)[0][dim]
else: else:
self.data = db.load(self.name)[0] self.data = db.load(self.name)[0]
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
Classes for handling ouputs. Classes for handling ouputs.
""" """
from parmepy import __VERBOSE__
from parmepy.constants import np, S_DIR, PARMES_REAL, debug from parmepy.constants import np, S_DIR, PARMES_REAL, debug
from parmepy.operator.monitors.monitoring import Monitoring from parmepy.operator.monitors.monitoring import Monitoring
import evtk.hl as evtk import evtk.hl as evtk
...@@ -48,6 +47,14 @@ class Printer(Monitoring): ...@@ -48,6 +47,14 @@ class Printer(Monitoring):
self.step = self._printStep self.step = self._printStep
else: else:
self.step = self._passStep self.step = self._passStep
## Internal counter
self._call_number = 0
def setUp(self):
"""
Print initial state
"""
self.step(0)
@debug @debug
@timed_function @timed_function
...@@ -86,7 +93,7 @@ class Printer(Monitoring): ...@@ -86,7 +93,7 @@ class Printer(Monitoring):
raise ValueError("Cannot set non callable method to get data " + raise ValueError("Cannot set non callable method to get data " +
"to print. Given method : " + str(method)) "to print. Given method : " + str(method))
def _passStep(self, ite): def _passStep(self, ite=None):
"""A code method that do nothing.""" """A code method that do nothing."""
pass pass
...@@ -97,8 +104,7 @@ class Printer(Monitoring): ...@@ -97,8 +104,7 @@ class Printer(Monitoring):
@param iter : current iteration number @param iter : current iteration number
""" """
if (ite % self.freq) == 0: if (ite % self.freq) == 0:
if __VERBOSE__: self._call_number += 1
print "== IO"
# Transfer from GPU to CPU if required # Transfer from GPU to CPU if required
for f in self.variables: for f in self.variables:
for df in f.discreteFields.values(): for df in f.discreteFields.values():
...@@ -109,8 +115,6 @@ class Printer(Monitoring): ...@@ -109,8 +115,6 @@ class Printer(Monitoring):
# Set output file name # Set output file name
filename = self.prefix + '_it_' + str(ite) + self.ext filename = self.prefix + '_it_' + str(ite) + self.ext
print "Print to file " + filename print "Print to file " + filename
if __VERBOSE__:
print "Print file : " + filename
## VTK output \todo: Need fix in 2D, getting an IOError. ## VTK output \todo: Need fix in 2D, getting an IOError.
try: try:
evtk.imageToVTK(filename, pointData=self._build_vtk_dict()) evtk.imageToVTK(filename, pointData=self._build_vtk_dict())
...@@ -159,10 +163,9 @@ class Printer(Monitoring): ...@@ -159,10 +163,9 @@ class Printer(Monitoring):
df[i, j, k])) df[i, j, k]))
f.write("\n") f.write("\n")
f.close() f.close()
if __VERBOSE__: print "==\n"
print "=="
if __name__ == "__main__": if __name__ == "__main__":
print __doc__ print __doc__
print "- Provided class : " + providedClass print "- Provided class : " + Printer
print eval(providedClass).__doc__ print Printer.__doc__
...@@ -42,8 +42,6 @@ class Poisson(Operator): ...@@ -42,8 +42,6 @@ class Poisson(Operator):
## the same resolution. ## the same resolution.
self.resolutions = resolutions self.resolutions = resolutions
self.input = [self.vorticity]
self.output = [self.velocity]
Operator.__init__(self, [velocity, vorticity], method, name="Poisson") Operator.__init__(self, [velocity, vorticity], method, name="Poisson")
self.config = other_config self.config = other_config
if method is None: if method is None:
...@@ -53,6 +51,8 @@ class Poisson(Operator): ...@@ -53,6 +51,8 @@ class Poisson(Operator):
assert self.resolution == self.resolutions[self.vorticity],\ assert self.resolution == self.resolutions[self.vorticity],\
'Poisson error : for fftw, all variables must have\ 'Poisson error : for fftw, all variables must have\
the same global resolution.' the same global resolution.'
self.input = [self.vorticity]
self.output = [self.velocity]
@debug @debug
def setUp(self): def setUp(self):
......
...@@ -79,7 +79,7 @@ class Problem(object): ...@@ -79,7 +79,7 @@ class Problem(object):
else: else:
self.name = name self.name = name
## Default file name prefix for dump. ## Default file name prefix for dump.
self.filename = str(name) self.filename = str(self.name)
self._filedump = self.filename + '_rk_' + str(main_rank) self._filedump = self.filename + '_rk_' + str(main_rank)
@debug @debug
...@@ -92,11 +92,6 @@ class Problem(object): ...@@ -92,11 +92,6 @@ class Problem(object):
if not isinstance(op, Monitoring): if not isinstance(op, Monitoring):
if not isinstance(op, Redistribute): if not isinstance(op, Redistribute):
op.setUp() op.setUp()
for op in self.operators:
if isinstance(op, Monitoring):
op.setUp()
if isinstance(op, Redistribute):
op.setUp()
if __VERBOSE__: if __VERBOSE__:
print "==== Variables initialization ====" print "==== Variables initialization ===="
...@@ -116,6 +111,12 @@ class Problem(object): ...@@ -116,6 +111,12 @@ class Problem(object):
for v in self.input: for v in self.input:
v.initialize() v.initialize()
for op in self.operators:
if isinstance(op, Monitoring):
op.setUp()
if isinstance(op, Redistribute):
op.setUp()
if __VERBOSE__: if __VERBOSE__:
print "====" print "===="
for op in self.operators: for op in self.operators:
...@@ -138,9 +139,6 @@ class Problem(object): ...@@ -138,9 +139,6 @@ class Problem(object):
Displays timings at simulation end. Displays timings at simulation end.
""" """
print "\n\n Start solving ..." print "\n\n Start solving ..."
for op in self.operators:
if isinstance(op, Monitoring):
op.apply(self.simulation)
try: try:
## Pass the main loop to the OpenGL viewer ## Pass the main loop to the OpenGL viewer
if not self.glRenderer is None: if not self.glRenderer is None:
...@@ -203,6 +201,8 @@ class Problem(object): ...@@ -203,6 +201,8 @@ class Problem(object):
db = NumPyDB_cPickle(self._filedump, mode='store') db = NumPyDB_cPickle(self._filedump, mode='store')
db.dump(self.simulation, 'simulation') db.dump(self.simulation, 'simulation')
for v in self.input: for v in self.input:
print 'dump v ...'
print v
v.dump(self.filename, mode='append') v.dump(self.filename, mode='append')
def restart(self, filename=None): def restart(self, filename=None):
...@@ -221,8 +221,15 @@ class Problem(object): ...@@ -221,8 +221,15 @@ class Problem(object):
self.simulation = db.load('simulation')[0] self.simulation = db.load('simulation')[0]
self.simulation.reset() self.simulation.reset()
for v in self.input: for v in self.input:
print "load ...", self.filename
v.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): def setDumpFreq(self, freq):
""" """
set rate of problem.dump call (every 'rate' iteration) set rate of problem.dump call (every 'rate' iteration)
......
...@@ -28,7 +28,7 @@ class Simulation(object): ...@@ -28,7 +28,7 @@ class Simulation(object):
## Is simulation is terminated ## Is simulation is terminated
self.isOver = False self.isOver = False
## Iteration counter ## Iteration counter
self.currentIteration = 0 self.currentIteration = 1
## Simulation time step ## Simulation time step
self.timeStep = timeStep self.timeStep = timeStep
## Maximum number of iterations ## Maximum number of iterations
...@@ -38,18 +38,20 @@ class Simulation(object): ...@@ -38,18 +38,20 @@ class Simulation(object):
""" """
Proceed to next time Proceed to next time
""" """
if self.currentIteration < self.iterMax and self.time < self.end: if self.currentIteration < self.iterMax:
self.currentIteration += 1 self.currentIteration += 1
self.time += self.timeStep self.time += self.timeStep
else: else:
self.isOver = True self.isOver = True
if self.time >= self.end:
self.isOver = True
def printState(self): def printState(self):
""" """
print current state print current state
""" """
if main_rank == 0: 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) self.currentIteration, self.time)
def reset(self): def reset(self):
...@@ -59,7 +61,7 @@ class Simulation(object): ...@@ -59,7 +61,7 @@ class Simulation(object):
""" """
self.start = self.time self.start = self.time
self.isOver = False self.isOver = False
self.currentIteration = 0 self.currentIteration = 1
def __str__(self): def __str__(self):
s = "Simulation parameters : " s = "Simulation parameters : "
......
...@@ -10,9 +10,10 @@ class TransportProblem(Problem): ...@@ -10,9 +10,10 @@ class TransportProblem(Problem):
""" """
Transport problem description. 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, Problem.__init__(self, operators, simulation, monitors,
name="Transport Problem") dumpFreq, name)
self.advection, self.velocity = None, None self.advection, self.velocity = None, None
for op in self.operators: for op in self.operators:
if isinstance(op, Advection): if isinstance(op, Advection):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment