Hi there, I had a question on saving the normal vector solution nh in another format. In the code which was provided, the solution has been written to a .bp format. I would prefer to write it into the format of a .xdmf/.h5 file, but there is a constant discrepancy between both files. Probably this is due to the fact that a “Continuous Lagrange” family was chosen to write the .xdmf file, in contrast to the .bp file, were a “Discontinuous Lagrange” family was chosen. In the images down below is shown the differences for both formats. This was the original code;
# Create a DG1 space for the facet vectors to be approximated.
DG1 = ufl.VectorElement(family='Discontinuous Lagrange', cell=mesh.ufl_cell(), degree=1)
space = dfx.fem.FunctionSpace(mesh=mesh, element=DG1)
# Compute the facet vector approximation.
nh = facet_vector_approximation(V=space, mt=facet_tags, mt_id=ft_id, interior=interior, tangent=tangent_flag)
# Write the results to file
filename = 'tangents.bp' if tangent_flag else 'normals.bp'
with dfx.io.VTXWriter(mesh.comm, filename, [nh], engine='BP4') as vtx:
vtx.write(0)
To write it to a .xdmf file, the following was added;
xdmf_filename = "normals.xdmf"
# Create a continuous vector space for visualization
CG1 = ufl.VectorElement("Lagrange", mesh.ufl_cell(), degree=1)
V_vis = dfx.fem.FunctionSpace(mesh, CG1)
# Interpolate discontinuous data to the continuous space
nh_vis = dfx.fem.Function(V_vis)
nh_vis.interpolate(nh)
# Now write to XDMF
with dolfinx.io.XDMFFile(mesh.comm, xdmf_filename, "w") as xdmf_file:
xdmf_file.write_mesh(mesh)
xdmf_file.write_function(nh_vis)
So probably the difference in Lagrange family type is causing this to happen, but if “Discontinuous Lagrange” was chosen for the last as well, this error occurs;
Traceback (most recent call last):
File "/home/username/normals_calc.py", line 240, in <module>
xdmf_file.write_function(nh_vis)
File "/home/username/miniconda3/envs/env_name/lib/python3.10/site-packages/dolfinx/io/utils.py", line 235, in write_function
super().write_function(getattr(u, "_cpp_object", u), t, mesh_xpath)
RuntimeError: Function and Mesh dof layouts do not match. Maybe the Function needs to be interpolated?
Does anyone know how to resolve this issue? Thanks for any help already.