This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to save a cell-function

0 votes

Hi,

There exist a way to save a cellfunction defined over a mesh in a file? I want to do this because i have a large domain where a large set of subdomains are defined. So, i would like to know if i can save the cell function in a first run of the code, and then eventually just load the function from a file to avoid the long time that the computer takes to create it.

Thanks!

asked Dec 25, 2015 by felipe_galarce FEniCS User (1,190 points)

1 Answer

+1 vote
 
Best answer

Consider this:

from dolfin import *

# Sub domain
class Omega1(SubDomain):
    def inside(self, x, on_boundary):
        return x[0] > 0.5

# Square mesh
mesh = UnitSquareMesh(20, 20, "crossed")

# Create a cell function and mark subdomains
cf1 = CellFunction("size_t", mesh, 0)
Omega1 = Omega1()
Omega1.mark(cf1, 1)

# write/export cellfunction
hdf = HDF5File(mesh.mpi_comm(), "file.h5", "w")
hdf.write(cf1, "/cf1")  
hdf.close()

# read/import cell function
hdf = HDF5File(mesh.mpi_comm(), "file.h5", "r")
cf2 = CellFunction("size_t", mesh)
hdf.read(cf2, "/cf1") 
hdf.close()
answered Dec 25, 2015 by hernan_mella FEniCS Expert (19,460 points)
selected Dec 25, 2015 by felipe_galarce

you can also export and import a CellFunction using the File class in this way (this does not run parallel):

# Export cell function
File = File("cellfunction.xml")
File << cf1

# Import cell function
cf3 = MeshFunction("size_t", mesh, "cellfunction.xml")

totally works!

...