I am new to fipy, so I apologise if this is a stupid question (and this doesn't seem to help me). But is there a way to store fipy objects in human-readable (or python-readable) form, other than suggested in the question above? This is only applicable to the cell variable. If I want to do some more fancy/customized plotting than what is in the default fipy viewer, how can I do it?
Take for example a simple 1D diffusion:
from fipy import *
# USER-DEFINED PARAMETERS
nx = 100
dx = 0.1
D = 1.0
bound1 = 30
bound2 = 70
# PREPARED FOR SOLUTION
mesh = Grid1D(nx=nx, dx=dx)
print "mesh", mesh
# define some parameters specific to this solution
T0 = bound2
Tinf = bound1
hour = 3600
day = hour*24
ndays = 1
duration = ndays*day
T = CellVariable(name="Temperature", mesh=mesh, value=bound1)
# Constant temperature boundary condition
T.constrain(T0, mesh.facesLeft)
T.constrain(Tinf, mesh.facesRight)
# SOLUTION
eq = (TransientTerm() == DiffusionTerm(coeff=D))
timeStepDuration = 0.5*hour
steps = int(duration/timeStepDuration)
for step in range(steps):
eqCirc.solve(var=T,dt=timeStepDuration)
But could I, for example, store the mesh as an array? Or could I store the value of the DiffusionTerm
instead of the CellVariable
in each step?
In my case, I would like to plot the thermal gradient (so extract it from the diffusion term) with distance for each time step. Can I do it? How?
There are a number of options. Any FiPy object can be pickled using
fipy.dump
, which will gather data when running in parallel. For example,You can then read this back in another Python session with
However, Pickle isn't great for long term storage as it depends on using the same version of the code to read the data back. An alternative is to save a Numpy array using,
and then read in with
To save the data in a human readable form with the location and value data for plotting in another package, you might try using pandas
You can of course Pickle any Python object, but using knowledge of the actual object is better for long term storage. For a grid mesh, only
dx
,dy
,nx
,ny
are required to reinstantiate. Mesh objects have a__getstate__
method that gives the requirements for pickling the object. All that needs to be stored is what this method returns.The
DiffusionTerm
doesn't really store anything other than its coefficient. The equation stores its matrix and b vector.