FEniCS / FEniCS/dolfinx

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

Open
#3,178 2 comments 0 reactions 1 assignee Claimed by @jorgensd View on GitHub
io
Dominant language
C++
Stars
1.2k
Forks
261
Avg merge
1d 15h
Merged PRs (30d)
65

Description

### 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)

```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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.