Skip to content

Commit 4baca52

Browse files
authored
Merge pull request #115 from NREL/pythoninterfacedoc
Document the python reading of open foam files
2 parents 0b5b7be + 483b731 commit 4baca52

6 files changed

Lines changed: 152 additions & 6 deletions

File tree

bird/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Bio reactor design version"""
22

3-
__version__ = "0.0.24"
3+
__version__ = "0.0.25"

docs/source/contribute.rst

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Contributing
22
=====
33

4-
We welcome pull requests from anybody!
4+
We welcome pull requests from anyone!
55

66
Formatting
77
------------
@@ -16,8 +16,33 @@ You can automatically enforce the formatting guidelines with
1616
bash fixFormat.sh
1717
1818
19-
Test
19+
Tests
2020
------------
21-
Please ensure your contribution passes the tests in ``tests``
21+
Please ensure your contribution passes the tests in the CI (``.github/worklows/ci.yml``)
22+
To run the unit tests
2223

24+
.. code-block:: console
25+
26+
conda activate bird
27+
pip install pytest
28+
BIRD_HOME=`python -c "import bird; print(bird.BIRD_DIR)"`
29+
cd ${BIRD_HOME}/../
30+
pytest .
31+
32+
To run the regression tests
33+
34+
.. code-block:: console
35+
36+
source <OpenFOAM-9 installation directory>/etc/<your-shell>rc
37+
conda activate bird
38+
pip install pytest
39+
BIRD_HOME=`python -c "import bird; print(bird.BIRD_DIR)"`
40+
cd ${BIRD_HOME}/../tutorial_cases
41+
bash run_all.sh
2342
43+
44+
Demonstrating and documenting your contribution
45+
------------
46+
We prefer the use of docstrings and type hinting. A good example to follow are functions in ``bird/utilities/ofio.py``.
47+
48+
If you add a new capability, please make sure to add relevant unit tests in the ``tests/`` folder. A good example to follow are tests ``tests/io``

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ We provide a solver ``birdmultiphaseEulerFoam`` that contains custom models adde
2727
meshing
2828
preprocess
2929
postprocess
30+
python_interface
3031
tutorials
3132
contribute
3233
references

docs/source/python_interface.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Python interface to OpenFOAM
2+
=====
3+
4+
We provide a simple python interface for reading OpenFOAM case results and dictionaries.
5+
A more comprehensive interface is available through ``pyFoam`` although we found it difficult to use and without recent support, which motivated the implementation of a new python interface.
6+
7+
8+
Read fields
9+
------------
10+
11+
Internal scalar and vector fields
12+
~~~~~~~~~~~~~~~~~~~~
13+
14+
Currently internal scalar and vector fields can be read using the python interface. In particular, note that
15+
#. 1. We do not support reading tensor fields for now.
16+
#. 2. We do not support reading boundary fields for now.
17+
#. 3. We only read reconstructed files
18+
19+
We are open to implementing 1. and 2. if need be. Implementing 3. is possible but will be more involved.
20+
21+
The main function interface to read openFOAM fields is ``readOF`` in ``bird.utilities.ofio``
22+
23+
The function only reads fields written in ASCII format and decided based on the header whether the field is a scalar or a vector. In the case of scalar, ``readOF`` returns a (N,) numpy array, where N is the number of computational cells. In the case of a vector, ``readOF`` returns a (N,3) numpy array.
24+
25+
If a uniform field is read, the number of cells may not be available from the field file and the function returns a float (equal to the uniform internal field value). If a numpy array is needed, the user can specify the number of cells in the field as an input.
26+
27+
Reuse instead of re-read
28+
~~~~~~~~~~~~~~~~~~~~
29+
30+
The ``read_field`` function uses ``readOF`` and takes a dictionary ``field_dict`` as input which is used to avoid reading multiple times the same field. For example, if one wants to compute the reactor-averaged concentration of a species, and then the reactor-averaged mass fraction of a species, the same mass fraction field will be used in both cases. As fields are read, ``field_dict`` will store the mass fraction field and recognize that the same field is needed.
31+
32+
It is up to the user to reinitialize ``field_dict``. For example, if the reactor-averaged mass fraction needs to be computed at time ``1`` and then at time ``2``, the user needs to pass an empty dictionary (or nothing) to ``read_field`` before reading the fields at time ``2``. Otherwise, ``read_field`` will assume that the mass fraction field is the same between time ``1`` and time ``2``.
33+
34+
35+
Read mesh-related fields
36+
~~~~~~~~~~~~~~~~~~~~
37+
38+
We rely on OpenFOAM utilities to provide mesh-based fields. The results of the OpenFOAM utilities can still be processed using ``bird`` functions.
39+
40+
41+
Reading cell volumes
42+
^^^^^^^^^^^^^^^
43+
44+
A cell volume field can be generated using the following OpenFOAM command ``postProcess -func writeCellVolumes -time {time_folder} -case {case_folder}``
45+
It will generate a file ``{time_folder}/V`` which can be read with the ``readOF`` function of ``bird``.
46+
This workflow is used in ``bird.postprocess.post_quantitities``, for example in the ``compute_gas_holdup`` function.
47+
48+
49+
Reading cell centers
50+
^^^^^^^^^^^^^^^
51+
52+
A mesh object file can be generated with the OpenFOAM command ``writeMeshObj -case {case_folder}``
53+
The file can then be read with the function ``readMesh`` from ``bird.utilities.ofio``.
54+
Again, this is used in ``bird.postprocess.post_quantities`` in the ``compute_superficial_velocity`` function.
55+
56+
57+
58+
59+
Read dictionaries
60+
------------
61+
62+
We provide a function ``parse_openfoam_dict`` in ``bird.utilities.ofio`` that can parse OpenFOAM dictionaries. The function requires a lot of special characters handling but works for processing basic dictionaries needed to manage OpenFOAM cases (``controlDict``, ``setFieldsDict``, ``phaseProperties``, ``thermophysicalProperties``, ``momentumTransport``, ...)
63+
64+
65+
Generate cases
66+
------------
67+
68+
(to be added based on the reactor optimization work)

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
"License :: OSI Approved :: BSD License",
3030
"Natural Language :: English",
3131
"Programming Language :: Python :: 3.10",
32-
"Programming Language :: Python :: 3.11",
33-
"Programming Language :: Python :: 3.12",
32+
"Programming Language :: Python :: 3.13",
3433
],
3534
package_data={
3635
"": [

tutorial_cases/runall.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#Compile solver
2+
3+
conda activate bird
4+
BIRD_HOME=`python -c "import bird; print(bird.BIRD_DIR)"`
5+
cd ${BIRD_HOME}/../OFsolvers/birdmultiphaseEulerFoam
6+
export WM_COMPILE_OPTION=Debug
7+
./Allwmake
8+
cd ../../
9+
10+
# Run all tests
11+
12+
## Run deckwer17 PBE
13+
cd experimental_cases/deckwer17
14+
bash run.sh
15+
cd ../../
16+
## Run deckwer17 constantD
17+
cd experimental_cases/deckwer17
18+
cp constant/phaseProperties_constantd constant/phaseProperties
19+
bash run.sh
20+
cd ../../
21+
## Run deckwer19 PBE
22+
cd experimental_cases/deckwer19
23+
bash run.sh
24+
cd ../../
25+
## Run side sparger tutorial
26+
cd tutorial_cases/side_sparger
27+
bash run.sh
28+
cd ../../
29+
## Run bubble column tutorial
30+
cd tutorial_cases/bubble_column_20L
31+
bash run.sh
32+
cd ../../
33+
## Run stirred-tank tutorial
34+
cd tutorial_cases/stirred_tank
35+
bash run.sh
36+
cd ../../
37+
## Run reactive loop reactor tutorial
38+
cd tutorial_cases/loop_reactor_reacting
39+
bash run.sh
40+
cd ../../
41+
## Run mixing loop reactor tutorial
42+
cd tutorial_cases/loop_reactor_mixing
43+
bash run.sh
44+
cd ../../
45+
## Run airlift reactor tutorial
46+
cd tutorial_cases/airlift_40m
47+
bash run.sh
48+
cd ../../
49+
## Run flat panel reactor tutorial
50+
cd tutorial_cases/FlatPanel_250L_ASU
51+
bash run.sh
52+
cd ../../
53+

0 commit comments

Comments
 (0)