Write mixed element function to files

Hello,

I’m working with a mixed element function space. I use a simple Taylor Hood element with second order interpolation for velocity and first order for pressure. Issues arise when I try to write a function from the velocity space to a file. The code below reproduces the error that I have. Writing the pressure works just fine, but writing velocity crashes the kernel.

from dolfinx import mesh, fem, io
from ufl import *
from mpi4py import MPI

domain = mesh.create_rectangle(MPI.COMM_WORLD, [(0.0, 0.0), (1.0, 1.0)],
                          (3,3), mesh.CellType.quadrilateral)

# create a Taylor Hood function space
velocity_element = VectorElement("Lagrange", domain.ufl_cell(), degree=2)
pressure_element = FiniteElement("Lagrange", domain.ufl_cell(), degree=1)
combinedSpace = fem.functionspace(domain, velocity_element*pressure_element)

f = fem.Function(combinedSpace)

# this works fine
file1 = io.VTXWriter(domain.comm, "output1.vtx", f.sub(1))
file1.write(0.0)
file1.close()

# this crashes the kernel
file2 = io.VTXWriter(domain.comm, "output2.vtx", f.sub(0))
file2.write(0.0)
file2.close()

In addition to that, what would be a clean way to write multiple time steps?
Thank you in advance.

Try collapsing the function, e.g., f.sub(1).collapse().

1 Like

The collapse did fix the crashing issue. Is it correct that the the outputs are folders? I can’t find a way to open them in paraview.

VTX creates .bp folders, that can be opened with paraview from the GUI (not terminal).

1 Like

I didn’t realize that you load an entire folder. Unfortunately paraview crashes when I try to load the folder. I tried both the ADIOS2CoreImageReader and ADIOS2VTXReader.

Which version of Paraview are you using? I can run your code and visualise with Paraview with no problem with:

$ paraview --version
paraview version 5.11.1
1 Like

I installed now version 5.11.1, before I had 5.12.0-RC1. I still can’t read the bp file, but at least I get an error message instead of just a crash

ERROR: In vtkADIOS2CoreImageReader.cxx, line 492
vtkADIOS2CoreImageReader (0000018326419270): No inquire variable is specified. Abort reading now

Is it possible that there is an issue, if I create the file in my WSL and open the file in windows?

Unfortunately I have no experience with Windows/WSL.

Did you manage to solve it? I use WSL, and I need to install it within the Ubuntu 22.04 subsystem, because I didn’t want to manually use folders to be able to open it in Paraview installed on Windows. Maybe it will work for you.
Another point, you must provide the correct path, where all the files are located, even with different extensions, in my case, saved in XDMF, but I need to provide the .H5 file in the same folder for it to open correctly. I hope this clarifies something for you.

I am not quite sure what I changed, so I add a new code snippet. Apparently passing "BP4" as an engine to the VTXWriter helps to open the file in paraview.

from dolfinx import mesh, fem, io
from ufl import *
from mpi4py import MPI

domain = mesh.create_rectangle(MPI.COMM_WORLD, [(0.0, 0.0), (1.0, 1.0)],
                          (3,3), mesh.CellType.quadrilateral)

# create a Taylor Hood function space
velocity_element = VectorElement("Lagrange", domain.ufl_cell(), degree=2)
pressure_element = FiniteElement("Lagrange", domain.ufl_cell(), degree=1)
combinedSpace = fem.functionspace(domain, velocity_element*pressure_element)

f = fem.Function(combinedSpace)

file1 = io.VTXWriter(domain.comm, "output1.bp", f.sub(1).collapse(), "BP4")
file1.write(0.0)
file1.close()

file2 = io.VTXWriter(domain.comm, "output2.bp", f.sub(0).collapse(), engine="BP4")
file2.write(0.0)
file2.close()

When I open the bp Folder in paraview, it seems like I need to tell it how to read my file? What I basically get is the following error:

ERROR: In vtkADIOS2CoreImageReader.cxx, line 648
vtkADIOS2CoreImageReader (00000131155E33C0): Can not use the dimension of array NumberOfEntities to set the dimension of image data. Its size is neither 2 nor 3

I also get in the Properties tab of my file the option to set an image dimension and a time step array.

The options paraview suggest are the same as shown in the Arrays window. Do I have to set here something manually? I have never used bp files before and I couldn’t find a video tutorial where somebody loads one of these files into paraview.

ADIOS2 has started to use bp5 as the default bp format. This is not supported by Paraview yet, see: Paraview opens ADIOS2-VTX BP5 files as BP4 (#22108) · Issues · ParaView / ParaView · GitLab

This is not the correct adios2 reader, as it should point to the correct image reader:

and for
For FidesWriter one should use

2 Likes

Thank you so much for that answer. That solved the problem for the pressure field instantly. The velocity field needed apparently a name to work properly. So my code looks now like this

from dolfinx import mesh, fem, io
from ufl import *
from mpi4py import MPI

domain = mesh.create_rectangle(MPI.COMM_WORLD, [(0.0, 0.0), (1.0, 1.0)],
                          (3,3), mesh.CellType.quadrilateral)

# create a Taylor Hood function space
velocity_element = VectorElement("Lagrange", domain.ufl_cell(), degree=2)
pressure_element = FiniteElement("Lagrange", domain.ufl_cell(), degree=1)
combinedSpace = fem.functionspace(domain, velocity_element*pressure_element)

f = fem.Function(combinedSpace)

f1 = f.sub(1).collapse()
f1.name = "pressure"
file1 = io.VTXWriter(domain.comm, "output1.bp", f1, "BP4")
file1.write(0.0)
file1.close()

f2 = f.sub(0).collapse()
f2.name = "velocity"
file2 = io.VTXWriter(domain.comm, "output2.bp", f2, engine="BP4")
file2.write(0.0)
file2.close()

One last question I have. Is it possible to give subspaces of the combined space a name directly?
Something like f.sub(1).name = "pressure"?

There is no generator to create Function::name inside Function itself.

In the python layer Function.name is just an accessor for the C++ public member. dolfinx/python/dolfinx/fem/function.py at main · FEniCS/dolfinx · GitHub.

Function::sub returns a new Function. See dolfinx/cpp/dolfinx/fem/Function.h at main · FEniCS/dolfinx · GitHub.

Furthermore, in the python layer, the sub function’s name is created at instantiation dolfinx/python/dolfinx/fem/function.py at main · FEniCS/dolfinx · GitHub.

You could create a sub function with a name in one line, e.g. the following

f1 = fem.Function(f._V.sub(1), f.x, name="pressure").collapse()
(f1 := f.sub(1).collapse()).name = "pressure"
f1 = f.sub(1).collapse(); f1.name = "pressure"

But I don’t find these particularly readable compared with what you already have.

3 Likes

Hi Jegor,
for me your MWE is not working anymore in Paraview 5.11.2. What about for you?

I get the error:

ERROR: In vtkADIOS2CoreImageReader.cxx, line 651
vtkADIOS2CoreImageReader (0x55d86eb66540): Can not use the dimension of array NumberOfEntities to set the dimension of image data. Its size is neither 2 nor 3

You are using the wrong reader.

You should use the ADIOS2VTXReader not ADIOS2CoreImageReader.

Yes indeed thanks!! You have to press OK already when you have selected the folder without changing the filter “All files…”