Hi everyone,
I was looking for a way to read OpenFOAM results into dolfinx, meaning being able to generate an equivalent Function in dolfinx from an OpenFOAM case.
I looked at this old topic: however, from my understanding this allows for reading the fields, starting from an xmf file generated from the case itself (I suppose).
In the past, I have implemented a routine, ImportOF, to do so using an external package, fluidfoam. The problem is that a new mesh has to be generated and, as far as I know, there is no easy to directly convert an OpenFOAM mesh, generated with blockMesh into gmsh and then dolfinx.
Lately, I saw that pyvista has capabilities of reading openfoam fields and mesh in here, is it possible to use these last functionalities to map everything into dolfinx?
Hello @Stefano_Riva. Look again, my code snippet can handle vector values. The ordering is pretty simple actually, something like x_1 y_1 x_2 y_2.
Hurts to see my main contribution to this forum described as an old post because it’s true. I hope you can figure it out and my ageing hacks still work.
The problem is not entirely related to vector values, actually i was looking for something able to take the openfoam spatial mesh and generate an equivalent one for dolfinx. In this way, we would have a compliant mesh and the interpolation process (using scipy maybe) would be easier, keeping the same features both on OpenFOAM and dolfinx.
I hope this clears it out a bit.
Sorry for calling it old , is your code able to generate the mesh also or should it be done outside using openfoam?
Thank you very much!
I have implemented this code to extract the topology and the points to create the dolfinx mesh from a blockMeshDict in OpenFOAM.
I’ll leave it here if anyone is interested (it works with dolfinx v0.6.0 sorry, but i guess it could be updated accordingly to the new versions).
import pyvista as pv
from pyvista import examples
from mpi4py import MPI
import dolfinx
import numpy as np
import ufl # Import UFL for mesh type definitions
from dolfinx.fem import FunctionSpace, Function
# Load the OpenFOAM file and extract the internal mesh
filename = examples.download_cavity(load=False)
reader = pv.POpenFOAMReader(filename)
reader.set_active_time_value(2.5)
reader.cell_to_point_creation = True
ugrid = reader.read()['internalMesh']
# Nodes of the mesh - They have to be (Nh, gdim)
nodes = ugrid.points
# # Connectivity of the mesh (topology) - The second dimension indicates the type of cell used
args_conn = np.argsort(ugrid.cells_dict[12], axis=1)
connectivity = np.array([ugrid.cells_dict[12][i, arg] for i, arg in enumerate(args_conn)])
# Define mesh element
shape = "hexahedron"
degree = 1
cell = ufl.Cell(shape, geometric_dimension=3)
mesh_ufl = ufl.Mesh(ufl.VectorElement("Lagrange", cell, degree))
# Create domain
cells = np.array(connectivity, dtype=np.int32)
domain = dolfinx.mesh.create_mesh(MPI.COMM_WORLD, cells, nodes, mesh_ufl)