Skip to content

Commit 85ee749

Browse files
committed
update text and codeblocks
1 parent 069369c commit 85ee749

1 file changed

Lines changed: 53 additions & 46 deletions

File tree

README.md

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,85 +7,92 @@
77
[![Code style: Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
88
[![DOI](https://zenodo.org/badge/944519483.svg)](https://doi.org/10.5281/zenodo.17297276)
99

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.
1111

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.
1414
1515
## Installation
1616

1717
```bash
1818
conda create -n foam2dolfinx-env
1919
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
2522
```
2623

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
2828

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:
3032

3133
```python
3234
from foam2dolfinx import OpenFOAMReader
33-
from pyvista import examples
3435

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

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")
4040

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")
4343
```
4444

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
4946

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:
5148

5249
```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)
5751

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")
6154
```
6255

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+
6462
```python
63+
from mpi4py import MPI
64+
from dolfinx.io import XDMFFile
6565
from foam2dolfinx import OpenFOAMReader
6666

67-
# instantiate reader:
68-
my_reader = OpenFOAMReader(filename="my_local_file.foam")
67+
reader = OpenFOAMReader(filename="my_case.foam", cell_type=12)
6968

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)
7381
```
7482

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.
7684

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
7886

7987
```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+
```
8191

82-
# instantiate reader:
83-
my_reader = OpenFOAMReader(filename="my_local_file.foam")
92+
To find the closest available time to a target value:
8493

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

0 commit comments

Comments
 (0)