Skip to content

gerlero/foamlib

Repository files navigation

foamlib

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

πŸ‘‹ Basics

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

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

πŸ‘ 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 in parallel

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

base = AsyncFoamCase(Path(os.environ["FOAM_TUTORIALS"]) / "incompressible/simpleFoam/pitzDaily")
# Replace with `AsyncSlurmFoamCase` if on a cluster and you want cases to be run as Slurm jobs

async def cost(x):
    async with base.clone() as clone:
        clone[0]["U"].boundary_field["inlet"].value = [x[0], 0, 0]
        await clone.run()
        return abs(clone[-1]["U"].internal_field[0][0])

result = differential_evolution(cost, bounds=[(-1, 1)], workers=AsyncFoamCase.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 more information, check out the documentation.