Storing terms in fipy as arrays instead of fipy objects

476 Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

But is there a way to store fipy objects in human-readable (or python-readable) form, other than suggested in the question above?

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,

import fipy
mesh = fipy.Grid2D(nx=3, ny=3)
var = fipy.CellVariable(mesh=mesh)
var[:] = mesh.x * mesh.y
fipy.dump.write(var, 'dump.gz')

You can then read this back in another Python session with

var = fipy.dump.read('dump.gz')

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,

np.save('dump.npy', var)

and then read in with

var_array = np.load('dump.npy')
var = fipy.CellVariable(mesh=mesh, value=var_array)

If I want to do some more fancy/customized plotting than what is in the default fipy viewer, how can I do it? If I want to do some more fancy/customized plotting than what is in the default fipy viewer, how can I do it?

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

import pandas
df = pandas.DataFrame({'x' : mesh.x, 'y': mesh.y, 'value': var})
df.to_csv('dump.csv')

But could I, for example, store the mesh as an array?

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.

Or could I store the value of the DiffusionTerm instead of the CellVariable in each step?

The DiffusionTerm doesn't really store anything other than its coefficient. The equation stores its matrix and b vector.