Skip to main content

A Python interface for interacting with OpenFOAM

Project description

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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

foamlib-0.6.15.tar.gz (35.0 kB view details)

Uploaded Source

Built Distribution

foamlib-0.6.15-py3-none-any.whl (39.3 kB view details)

Uploaded Python 3

File details

Details for the file foamlib-0.6.15.tar.gz.

File metadata

  • Download URL: foamlib-0.6.15.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.1

File hashes

Hashes for foamlib-0.6.15.tar.gz
Algorithm Hash digest
SHA256 e34aa31d94c493819a523636c733882aa39c6f88c0ed24a48c5ca5cfe2e60d78
MD5 e561bf9aaacdd727e6b019ee4239459e
BLAKE2b-256 d271d29235b10d583088b9d9f8761ccd70993942d525f0e15502e3798e050f88

See more details on using hashes here.

File details

Details for the file foamlib-0.6.15-py3-none-any.whl.

File metadata

File hashes

Hashes for foamlib-0.6.15-py3-none-any.whl
Algorithm Hash digest
SHA256 ddbf5261970a4994aee687723b6be40359f338af0cab1c1f9457b16d2ff435a7
MD5 e9bad73b821a30024a60a626ae1b52fc
BLAKE2b-256 5fe8dfe73f5f4cf0c3f549641b0758526b999f45b58d6376c5d9db8f1a1fef48

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page