|
7 | 7 | [](https://github.com/astral-sh/ruff) |
8 | 8 | [](https://doi.org/10.5281/zenodo.17297276) |
9 | 9 |
|
10 | | -foam2dolfinx is a tool for converting OpenFOAM output files to functions that can be used within [dolfinx](https://github.com/FEniCS/dolfinx). |
| 10 | +foam2dolfinx converts OpenFOAM output files into dolfinx meshes, meshtags, and functions for use in [DOLFINx](https://github.com/FEniCS/dolfinx)-based finite element workflows. |
11 | 11 |
|
12 | | -> [!NOTE] |
13 | | -> This small package was inspired by Stefano Riva's [ROSE-pyforce](https://github.com/ERMETE-Lab/ROSE-pyforce) repository. |
| 12 | +> [!NOTE] |
| 13 | +> This package was inspired by Stefano Riva's [ROSE-pyforce](https://github.com/ERMETE-Lab/ROSE-pyforce) repository. |
14 | 14 |
|
15 | 15 | ## Installation |
16 | 16 |
|
17 | 17 | ```bash |
18 | 18 | conda create -n foam2dolfinx-env |
19 | 19 | conda activate foam2dolfinx-env |
20 | | -conda install -c conda-forge fenics-dolfinx=0.9.0 mpich pyvista |
21 | | -``` |
22 | | -Once in the created in environment: |
23 | | -```bash |
24 | | -python -m pip install foam2dolfinx |
| 20 | +conda install -c conda-forge fenics-dolfinx mpich pyvista |
| 21 | +pip install foam2dolfinx |
25 | 22 | ``` |
26 | 23 |
|
27 | | -# Example usage |
| 24 | +> [!NOTE] |
| 25 | +> Only single cell-type meshes are supported. Supported VTK cell types: `10` (tetrahedron) and `12` (hexahedron). |
| 26 | +
|
| 27 | +## Usage |
28 | 28 |
|
29 | | -## Standard case |
| 29 | +### Reading field data |
| 30 | + |
| 31 | +Cell-centred data (e.g. from a finite volume solution) and point data are both supported: |
30 | 32 |
|
31 | 33 | ```python |
32 | 34 | from foam2dolfinx import OpenFOAMReader |
33 | | -from pyvista import examples |
34 | 35 |
|
35 | | -# use foam data from the examples in pyvista |
36 | | -foam_example = examples.download_cavity(load=False) |
| 36 | +reader = OpenFOAMReader(filename="my_case.foam", cell_type=12) |
37 | 37 |
|
38 | | -# instantiate reader: |
39 | | -my_reader = OpenFOAMReader(filename=foam_example, cell_type=10) |
| 38 | +# Cell data — maps to a DG-0 function space |
| 39 | +T_cells = reader.create_dolfinx_function_with_cell_data(t=1.0, name="T") |
40 | 40 |
|
41 | | -# read velocity field at t=2.5s |
42 | | -vel = my_of_reader.create_dolfinx_function(t=2.5, name="U") |
| 41 | +# Point data — maps to a CG-1 function space |
| 42 | +U_points = reader.create_dolfinx_function_with_point_data(t=1.0, name="U") |
43 | 43 | ``` |
44 | 44 |
|
45 | | -> [!NOTE] |
46 | | -> Currently only domains with a unique cell type across the domain are supported. Furthermore, only vtk type cells 10 - tetrahedron and 12 - hexhedron are supported. |
47 | | -
|
48 | | -## Multiple fields |
| 45 | +### Multi-domain cases |
49 | 46 |
|
50 | | -Consider a case where in the same file there is both a temperature and velocity field to read at |
| 47 | +For multi-region OpenFOAM cases, pass the subdomain name to read fields from a specific region: |
51 | 48 |
|
52 | 49 | ```python |
53 | | -from foam2dolfinx import OpenFOAMReader |
54 | | - |
55 | | -# instantiate reader: |
56 | | -my_reader = OpenFOAMReader(filename="my_local_file.foam") |
| 50 | +reader = OpenFOAMReader(filename="my_case.foam", cell_type=12) |
57 | 51 |
|
58 | | -# read velocity and temperature fields at t=1s |
59 | | -vel = my_of_reader.create_dolfinx_function(t=1.0, name="U") |
60 | | -T = my_of_reader.create_dolfinx_function(t=1.0, name="T") |
| 52 | +T_fluid = reader.create_dolfinx_function_with_cell_data(t=1.0, name="T", subdomain="fluid") |
| 53 | +T_solid = reader.create_dolfinx_function_with_cell_data(t=1.0, name="T", subdomain="solid") |
61 | 54 | ``` |
62 | 55 |
|
63 | | -## Multiple subdomains |
| 56 | +### Meshtags |
| 57 | + |
| 58 | +Boundary patches and cell zones can be extracted as `dolfinx.mesh.MeshTags` for use in variational forms and boundary condition assignment. |
| 59 | + |
| 60 | +For multi-domain cases, a single merged global mesh is built automatically. Interface facets between subdomains are detected and tagged with sequential IDs continuing after the boundary patch IDs. |
| 61 | + |
64 | 62 | ```python |
| 63 | +from mpi4py import MPI |
| 64 | +from dolfinx.io import XDMFFile |
65 | 65 | from foam2dolfinx import OpenFOAMReader |
66 | 66 |
|
67 | | -# instantiate reader: |
68 | | -my_reader = OpenFOAMReader(filename="my_local_file.foam") |
| 67 | +reader = OpenFOAMReader(filename="my_case.foam", cell_type=12) |
69 | 68 |
|
70 | | -# read velocity and temperature fields at t=1s |
71 | | -vel1 = my_of_reader.create_dolfinx_function(t=3.0, name="U", subdomain="sub1") |
72 | | -vel2 = my_of_reader.create_dolfinx_function(t=3.0, name="U", subdomain="sub2") |
| 69 | +facet_tags = reader.create_facet_meshtags() |
| 70 | +cell_tags = reader.create_cell_meshtags() |
| 71 | + |
| 72 | +mesh = reader.dolfinx_meshes_dict["_global"] # single-domain: use "default" |
| 73 | + |
| 74 | +with XDMFFile(MPI.COMM_WORLD, "facet_meshtags.xdmf", "w") as f: |
| 75 | + f.write_mesh(mesh) |
| 76 | + f.write_meshtags(facet_tags, mesh.geometry) |
| 77 | + |
| 78 | +with XDMFFile(MPI.COMM_WORLD, "cell_meshtags.xdmf", "w") as f: |
| 79 | + f.write_mesh(mesh) |
| 80 | + f.write_meshtags(cell_tags, mesh.geometry) |
73 | 81 | ``` |
74 | 82 |
|
75 | | -## Tips and tricks |
| 83 | +Both methods print a summary to stdout showing each patch/zone name, its assigned integer ID, and how many facets or cells carry that tag. |
76 | 84 |
|
77 | | -If you are unaware of the time values with data within the OpenFOAM data, you can check with the `time_values` function within the 'reader' attribute of the 'OpenFOAMReader' class: |
| 85 | +### Finding available time values |
78 | 86 |
|
79 | 87 | ```python |
80 | | -from foam2dolfinx import OpenFOAMReader |
| 88 | +reader = OpenFOAMReader(filename="my_case.foam", cell_type=12) |
| 89 | +print(reader.times) # e.g. [0.0, 0.5, 1.0, 2.0] |
| 90 | +``` |
81 | 91 |
|
82 | | -# instantiate reader: |
83 | | -my_reader = OpenFOAMReader(filename="my_local_file.foam") |
| 92 | +To find the closest available time to a target value: |
84 | 93 |
|
85 | | -# find the time values |
86 | | -print(my_reader.reader.time_values) |
87 | | -``` |
88 | | -This should return a list of floats with the time values in the file: |
89 | | -``` |
90 | | -[1.0, 2.0, 3.0] |
| 94 | +```python |
| 95 | +from foam2dolfinx import find_closest_value |
| 96 | + |
| 97 | +t = find_closest_value(reader.times, 1.3) # returns 1.0 or 2.0 |
91 | 98 | ``` |
0 commit comments