Skip to content

gerlero/foamlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

foamlib

Documentation CI Codecov Checked with mypy Ruff uv Publish PyPI Conda Version PyPI - Python Version OpenFOAM Docker Docker image DOI

foamlib provides a simple, modern, ergonomic and fast Python interface for interacting with OpenFOAM.

benchmark

Parsing a volVectorField with 200k cells.1

๐Ÿš€ Introduction

foamlib is a Python package designed to simplify the manipulation of OpenFOAM cases and files. Its standalone parser makes it easy to work with OpenFOAMโ€™s input/output files from Python, while its case-handling capabilities facilitate various execution workflowsโ€”reducing boilerplate code and enabling efficient Python-based pre- and post-processing, as well as simulation management.

Compared to PyFoam and other similar tools like fluidfoam, fluidsimfoam, and Ofpp, foamlib offers advantages such as modern Python compatibility, support for binary-formatted fields, a fully type-hinted API, and asynchronous operations; making OpenFOAM workflows more accessible and streamlined.

๐Ÿ‘‹ Basics

foamlib offers the following Python classes:

  • FoamFile (and FoamFieldFile): read-write access to OpenFOAM configuration and field files as if they were Python dicts, using foamlib's own parser and in-place editor. Supports ASCII and binary field formats (with or without compression).
  • FoamCase: a class for configuring, running, and accessing the results of OpenFOAM cases.
  • AsyncFoamCase: variant of FoamCase with asynchronous methods for running multiple cases at once.
  • AsyncSlurmFoamCase: subclass of AsyncFoamCase used for running cases on a Slurm cluster.

โ˜‘๏ธ Get started

๐Ÿ“ฆ Install

  • With pip:

    pip install foamlib
  • With conda:

    conda install -c conda-forge foamlib
  • With Homebrew:

    brew install gerlero/openfoam/foamlib

๐Ÿ‘ Clone a case

import os
from pathlib import Path
from foamlib import FoamCase

pitz_tutorial = FoamCase(Path(os.environ["FOAM_TUTORIALS"]) / "incompressible/simpleFoam/pitzDaily")

my_pitz = pitz_tutorial.clone("myPitz")

๐Ÿƒ Run the case

my_pitz.run()

๐Ÿ”Ž Access the results

latest_time = my_pitz[-1]

p = latest_time["p"]
U = latest_time["U"]

print(p.internal_field)
print(U.internal_field)

๐Ÿงน Clean the case

my_pitz.clean()

โš™๏ธ Edit the controlDict file

my_pitz.control_dict["writeInterval"] = 10

๐Ÿ“ Make multiple file reads and writes in a single go

with my_pitz.fv_schemes as f:
    f["gradSchemes"]["default"] = f["divSchemes"]["default"]
    f["snGradSchemes"]["default"] = "uncorrected"

โณ Run a case asynchronously

import asyncio
from foamlib import AsyncFoamCase

async def run_case():
    my_pitz_async = AsyncFoamCase(my_pitz)
    await my_pitz_async.run()

asyncio.run(run_case())

๐Ÿ”ข Parse a field using the FoamFieldFile class directly

from foamlib import FoamFieldFile

U = FoamFieldFile(Path(my_pitz) / "0/U")

print(U.internal_field)

๐Ÿ” Run an optimization loop on a Slurm-based cluster

import os
from pathlib import Path
from foamlib import AsyncSlurmFoamCase
from scipy.optimize import differential_evolution

base = 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

#!/usr/bin/env python3
from pathlib import Path
from foamlib import FoamCase

case = FoamCase(Path(__file__).parent)
# Any additional configuration here
case.run()

๐Ÿ“˜ Documentation

For details on how to use foamlib, check out the documentation.

๐Ÿ™‹ Support

If you have any questions or need help, feel free to open a discussion.

If you believe you have found a bug in foamlib, please open an issue.

๐Ÿง‘โ€๐Ÿ’ป Contributing

You're welcome to contribute to foamlib! Check out the contributing guidelines for more information.

๐Ÿ–‹๏ธCitation

If you find foamlib useful for your work, don't forget to cite it!

Citations help us a lot. You may find the following snippets useful:

BibTeX
@article{foamlib,
    author = {Gerlero, Gabriel S. and Kler, Pablo A.},
    doi = {10.21105/joss.07633},
    journal = {Journal of Open Source Software},
    month = may,
    number = {109},
    pages = {7633},
    title = {{foamlib: A modern Python package for working with OpenFOAM}},
    url = {https://joss.theoj.org/papers/10.21105/joss.07633},
    volume = {10},
    year = {2025}
}
APA

Gerlero, G. S., & Kler, P. A. (2025). foamlib: A modern Python package for working with OpenFOAM. Journal of Open Source Software, 10(109), 7633. https://doi.org/10.21105/joss.07633

๐Ÿ‘Ÿ Footnotes

[1] foamlib 0.8.1 vs PyFoam 2023.7 on a MacBook Air (2020, M1) with 8 GB of RAM. Benchmark script.