Adios4dolfinx and VectorElement with prescribed size

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

I cannot reproduce this with DOLFINx nightly:

docker pull  ghcr.io/fenics/dolfinx/dolfinx:nightly
docker run -ti -v $(pwd):/root/shared -w /root/shared --rm ghcr.io/fenics/dolfinx/dolfinx:nightly

after installing the main branch of adios4dolfinx I run this code:

import numpy as np
from mpi4py import MPI
import dolfinx
from dolfinx.fem import (Function, functionspace)
import adios4dolfinx
import basix.ufl
# Write
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10, cell_type=dolfinx.mesh.CellType.triangle)
adios4dolfinx.write_mesh(mesh, "./mesh/square.bp")


el = basix.ufl.element("Lagrange", mesh.topology.cell_name(), 2, shape=(1,))
V = functionspace(mesh, el)
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)
el = basix.ufl.element("Lagrange", mesh_new.topology.cell_name(), 2, shape=(1,))

W = functionspace(mesh_new, el)
w = Function(W)
adios4dolfinx.read_function(w, "function.bp")

# Check
assert (np.all(u.x.array == w.x.array))

Could you please specify what version you are using @LucasPa ?

Edit: I also cannot reproduce it with dolfinx v0.7.2 and adios4dolfinx v0.7.1

Thanks for your help.

Actually I found out that by first removing all the .bp directories the run is successful. This dependency to existing files was already observed in this reply (but the error was not the same).
From this experience, I try now to always remove existing .bp directory before using adios4dolfinx to write meshes or functions.