dolfinx icon indicating copy to clipboard operation
dolfinx copied to clipboard

[BUG]: Wrong dispatchment of field values in the mesh with VTK and VTX writers using a mixed space

Open Reclu opened this issue 1 year ago • 1 comments

Summarize the issue

When one scalar field and one vector field that belong to two subspaces of a mixed function space are exported using one single VTK file, the second field exported exhibits a DOF mixing when plotted in paraview (good values at wrong places). Note that the same goes when using a VTX writer.

I encounter this issue when solving a multiphysic problem because I want to gather all the solution fields in one single file.

How to reproduce the bug

Simply interpolate analytical vector and scalar expressions to vector and scalar functions belonging to a mixed space and export both fields in different orders. In each case, the visualization of the first exported field is correct while that of the second one is not.

Minimal Example (Python)

import ufl
import numpy as np


from dolfinx import mesh,fem,io
from petsc4py import PETSc
from mpi4py import MPI

# Define mesh
nx, ny = 20, 5
h,l= 1.0,4.0
domain = mesh.create_rectangle(MPI.COMM_WORLD, [np.array([0, 0]), np.array([l, h])],[nx, ny], mesh.CellType.quadrilateral)


### Parameters of a two-dimensional gaussian centered at point C
xC=2.0
yC=0.5
radius=0.2


vector_field = lambda x: np.array([x[0],x[1]])
scalar_field = lambda x: np.exp(-(x[0]-xC)**2/radius**2 -(x[1]-yC)**2/radius**2)

## Function space and fields
Uel = ufl.VectorElement("Lagrange",domain.ufl_cell(),1,dim=2)
Tel = ufl.FiniteElement("Lagrange",domain.ufl_cell(),1)
Mixed_elem = ufl.MixedElement([Uel,Tel])
Vmixed = fem.FunctionSpace(domain,Mixed_elem)

X=fem.Function(Vmixed)

vec = X.split()[0].collapse()
scal = X.split()[1].collapse()
vec.name = "Vector Field"
scal.name = "Scalar field"


fileName="output"
vec.interpolate(vector_field)
scal.interpolate(scalar_field)
vtk = io.VTKFile(domain.comm, fileName+"_vec_scal", "w")
vtk.write_function([vec,scal], 0.0)
vtk.close()

vtx = io.VTXWriter(domain.comm, fileName+"_vec_scal.bp", [vec,scal])
vtx.write(0.0)
vtx.close()

vtk = io.VTKFile(domain.comm, fileName+"_scal_vec", "w")
vtk.write_function([scal,vec], 0.0)
vtk.close()

vtx = io.VTXWriter(domain.comm, fileName+"_scal_vec.bp", [scal,vec])
vtx.write(0.0)
vtx.close()

## Export reference solutions to xdmf 
xdmf_vec= io.XDMFFile(domain.comm, 'vector_solution.xdmf', "w")
xdmf_scal= io.XDMFFile(domain.comm, 'scalar_solution.xdmf', "w")

xdmf_vec.write_mesh(domain)
xdmf_vec.write_function(vec, 0.0)
xdmf_scal.write_mesh(domain)
xdmf_scal.write_function(scal, 0.0)

xdmf_vec.close()
xdmf_scal.close()

Output (Python)

No response

Version

0.7.3

DOLFINx git commit

No response

Installation

Installation on ubuntu MATE 22.04 using "apt install fenicsx"

Additional information

No response

Reclu avatar Apr 26 '24 12:04 Reclu

I also have this. Perhaps a workaround is possible by doing a similar action as the old 'move_mesh'

marc-git avatar Jan 29 '25 19:01 marc-git