https://github.com/gerlero/foamlib
A modern Python interface for interacting with OpenFOAM
https://github.com/gerlero/foamlib
asyncio openfoam parsing python
Last synced: 3 months ago
JSON representation
A modern Python interface for interacting with OpenFOAM
- Host: GitHub
- URL: https://github.com/gerlero/foamlib
- Owner: gerlero
- License: gpl-3.0
- Created: 2024-03-18T15:53:08.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-16T01:03:31.000Z (9 months ago)
- Last Synced: 2024-09-16T09:31:45.904Z (9 months ago)
- Topics: asyncio, openfoam, parsing, python
- Language: Python
- Homepage: https://foamlib.readthedocs.io
- Size: 370 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[
](https://github.com/gerlero/foamlib)
[](https://foamlib.readthedocs.io/)
[](https://github.com/gerlero/foamlib/actions/workflows/ci.yml)
[](https://codecov.io/gh/gerlero/foamlib)
[](http://mypy-lang.org/)
[](https://github.com/astral-sh/ruff)
[](https://github.com/astral-sh/uv)
[](https://github.com/gerlero/foamlib/actions/workflows/pypi-publish.yml)
[](https://pypi.org/project/foamlib/)
[](https://anaconda.org/conda-forge/foamlib)
[](https://pypi.org/project/foamlib/)

[](https://github.com/gerlero/foamlib/actions/workflows/docker.yml)
[](https://hub.docker.com/r/microfluidica/foamlib/)**foamlib** provides a simple, modern, ergonomic and fast Python interface for interacting with [OpenFOAM](https://www.openfoam.com).
![]()
Parsing a volVectorField with 200k cells.## ๐ Basics
**foamlib** offers the following Python classes:
* [`FoamFile`](https://foamlib.readthedocs.io/en/stable/files.html#foamlib.FoamFile) (and [`FoamFieldFile`](https://foamlib.readthedocs.io/en/stable/files.html#foamlib.FoamFieldFile)): read-write access to OpenFOAM configuration and field files as if they were Python `dict`s, using `foamlib`'s own parser and in-place editor. Supports ASCII and binary field formats (with or without compression).
* [`FoamCase`](https://foamlib.readthedocs.io/en/stable/cases.html#foamlib.FoamCase): a class for configuring, running, and accessing the results of OpenFOAM cases.
* [`AsyncFoamCase`](https://foamlib.readthedocs.io/en/stable/cases.html#foamlib.AsyncFoamCase): variant of `FoamCase` with asynchronous methods for running multiple cases at once.
* [`AsyncSlurmFoamCase`](https://foamlib.readthedocs.io/en/stable/cases.html#foamlib.AsyncSlurmFoamCase): subclass of `AsyncFoamCase` used for running cases on a Slurm cluster.## โ๏ธ Get started
### ๐ฆ Install
* With [pip](https://pypi.org/project/pip/):
```bash
pip install foamlib
```* With [conda](https://docs.conda.io/en/latest/):
```bash
conda install -c conda-forge foamlib
```### ๐ Clone a case
```python
import os
from pathlib import Path
from foamlib import FoamCasepitz_tutorial = FoamCase(Path(os.environ["FOAM_TUTORIALS"]) / "incompressible/simpleFoam/pitzDaily")
my_pitz = pitz_tutorial.clone("myPitz")
```### ๐ Run the case
```python
my_pitz.run()
```### ๐ Access the results
```python
latest_time = my_pitz[-1]p = latest_time["p"]
U = latest_time["U"]print(p.internal_field)
print(U.internal_field)
```### ๐งน Clean the case
```python
my_pitz.clean()
```### โ๏ธ Edit the `controlDict` file
```python
my_pitz.control_dict["writeInterval"] = 10
```### ๐ Make multiple file reads and writes in a single go
```python
with my_pitz.fv_schemes as f:
f["gradSchemes"]["default"] = f["divSchemes"]["default"]
f["snGradSchemes"]["default"] = "uncorrected"
```### โณ Run a case asynchronously
```python
import asyncio
from foamlib import AsyncFoamCaseasync def run_case():
my_pitz_async = AsyncFoamCase(my_pitz)
await my_pitz_async.run()asyncio.run(run_case())
```### ๐ข Parse a field using the [`FoamFieldFile`](https://foamlib.readthedocs.io/en/stable/#foamlib.FoamFieldFile) class directly
```python
from foamlib import FoamFieldFileU = FoamFieldFile(Path(my_pitz) / "0/U")
print(U.internal_field)
```### ๐ Run an optimization loop on a Slurm-based cluster
```python
import os
from pathlib import Path
from foamlib import AsyncSlurmFoamCase
from scipy.optimize import differential_evolutionbase = AsyncSlurmFoamCase(Path(os.environ["FOAM_TUTORIALS"]) / "incompressible/simpleFoam/pitzDaily")
async def cost(x):
async with base.clone() as clone:
clone[0]["U"].boundary_field["inlet"].value = [x[0], 0, 0]
await clone.run(fallback=True) # Run locally if Slurm is not available
return abs(clone[-1]["U"].internal_field[0][0])result = differential_evolution(cost, bounds=[(-1, 1)], workers=AsyncSlurmFoamCase.map, polish=False)
```### ๐ Use it to create a `run` (or `clean`) script
```python
#!/usr/bin/env python3
from pathlib import Path
from foamlib import FoamCasecase = FoamCase(Path(__file__).parent)
# Any additional configuration here
case.run()
```## ๐ Documentation
For more information, check out the [documentation](https://foamlib.readthedocs.io/).