Hi,
From this post, I created the following script that successfully writes and read a function (and a mesh):
import numpy as np
from mpi4py import MPI
import dolfinx
from dolfinx.fem import (Function, FunctionSpace, Expression)
from dolfinx.io import XDMFFile
import ufl
from petsc4py import PETSc
import adios4dolfinx
# Write
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10, cell_type=dolfinx.mesh.CellType.triangle)
adios4dolfinx.write_mesh(mesh, "square.bp")
V = FunctionSpace(mesh, ufl.VectorElement("Lagrange", mesh.ufl_cell(), 2))
u = Function(V)
x = ufl.SpatialCoordinate(mesh)
dfnx_expr = Expression(x, V.element.interpolation_points())
u.interpolate(dfnx_expr)
adios4dolfinx.write_function(u, "function.bp")
# Read
mesh_new = adios4dolfinx.read_mesh(MPI.COMM_WORLD, "./mesh/square.bp", "BP4", dolfinx.mesh.GhostMode.none)
W = FunctionSpace(mesh_new, ufl.VectorElement("Lagrange", mesh_new.ufl_cell(), 2))
w = Function(W)
adios4dolfinx.read_function(w, "function.bp")
# Check
assert(np.all(u.x.array==w.x.array))
For my cases of interest, my solution function has more than one component and therefore I need to prescribe a size when defining VectorElement
. Therefore I adapted the above script such that a size (=1) is prescribed:
import numpy as np
from mpi4py import MPI
import dolfinx
from dolfinx.fem import (Function, FunctionSpace, Expression)
from dolfinx.io import XDMFFile
import ufl
from petsc4py import PETSc
import adios4dolfinx
# Write
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10, cell_type=dolfinx.mesh.CellType.triangle)
adios4dolfinx.write_mesh(mesh, "square.bp")
V = FunctionSpace(mesh, ufl.VectorElement("Lagrange", mesh.ufl_cell(), 2, 1))
u = Function(V)
u.interpolate(lambda x: (x[0]))
adios4dolfinx.write_function(u, "function.bp")
# Read
mesh_new = adios4dolfinx.read_mesh(MPI.COMM_WORLD, "./mesh/square.bp", "BP4", dolfinx.mesh.GhostMode.none)
W = FunctionSpace(mesh_new, ufl.VectorElement("Lagrange", mesh_new.ufl_cell(), 2, 1))
w = Function(W)
adios4dolfinx.read_function(w, "function.bp")
# Check
assert(np.all(u.x.array==w.x.array))
Unfortunately this script fails with the following error:
Traceback (most recent call last):
File "my_script.py", line 23, in <module>
adios4dolfinx.read_function(w, "function.bp")
File "dolfinx/Dist/real/lib/python3.9/site-packages/adios4dolfinx/checkpointing.py", line 198, in read_function
dof_owner = index_owner(comm, input_dofmap.array, num_dofs_global)
File "dolfinx/Dist/real/lib/python3.9/site-packages/adios4dolfinx/utils.py", line 54, in index_owner
assert (indices < N).all()
AssertionError
Is there a way to save and read such a function of a FunctionSpace
for which the family is a VectorElement
with a prescribed size?
Best,
Lucas