I would like to know if, in the current version of fenicsx, it is possible to load a variable field (velocity, pressure) from an external simulation and then use it. I know that within fenicsx you have to go through adios4dolfinx and the .bp format. But I would like to use a field created by an external solver in fenicsx. What would be the best format to use?
What kind of external solver, and data format are we talking about?
I assumed the data is tied to your mesh in some way? Is it vertex based data, or is it cell based, or something completely different?
It is a finite volume CFD solver. By default, values are averaged for each cell, but it is possible, if necessary, to extract a value at each node. The standard format is cgns, but it is possible to convert to many other formats.
If you have the cell index associated with the values that have been averaged for each cell, you can read this into DOLFINx in quite a standard way (as long as you can get these arrays into Python).
Then, what I would suggest you do, is to create a DG 0 field on the mesh:
from mpi4py import MPI
import dolfinx.mesh
import numpy as np
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
num_cells_global = mesh.topology.index_map(mesh.topology.dim).size_global
my_data = np.arange(num_cells_global, dtype=np.float64)
original_cell_indices = mesh.topology.original_cell_index
V = dolfinx.fem.functionspace(mesh, ("DG", 0))
u = dolfinx.fem.Function(V)
u.x.array[:] = my_data[original_cell_indices]
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "floating_data.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)
xdmf.write_function(u)
Note that this gives you the same DG-0 function every time, irregardless of the number of processes used to distribute your mesh, as you access the data from your data (here called my_data with the correct local index).
Thank you very much for your reply, it’s much clearer now.
The real goal is actually to retrieve the field calculated by an external solver (speed, pressure, etc.), then solve an adjoint problem based on it to create an optimisation loop. I would therefore need to load the field onto a DG0 space in FEniCSx and then interpolate it onto a mixed space (v,p for example) of mini-Taylor Hood elements in order to perform my calculations.