A Python interface for interacting with OpenFOAM
Project description
foamlib provides a simple, modern, ergonomic and fast Python interface for interacting with OpenFOAM.
Parsing a volVectorField with 200k cells.
👋 Basics
foamlib offers the following Python classes:
FoamFile
(andFoamFieldFile
): read-write access to OpenFOAM configuration and field files as if they were Pythondict
s, usingfoamlib
'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 ofFoamCase
with asynchronous methods for running multiple cases at once.AsyncSlurmFoamCase
: subclass ofAsyncFoamCase
used for running cases on a Slurm cluster.
☑️ Get started
📦 Install
🐑 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 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.7.3.tar.gz
(35.4 kB
view details)
Built Distribution
foamlib-0.7.3-py3-none-any.whl
(39.5 kB
view details)
File details
Details for the file foamlib-0.7.3.tar.gz
.
File metadata
- Download URL: foamlib-0.7.3.tar.gz
- Upload date:
- Size: 35.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac1282248c2363fa130d693252621074c102201d70bd125ec6ee69e13389e626 |
|
MD5 | 564d8fe8def78e68f03b92762c243a9f |
|
BLAKE2b-256 | 07c20ebf2ffa9b04b4133738f5e3374f6cf938ff14f1ac0c3d8d84b58db12cf7 |
File details
Details for the file foamlib-0.7.3-py3-none-any.whl
.
File metadata
- Download URL: foamlib-0.7.3-py3-none-any.whl
- Upload date:
- Size: 39.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bf4a506e8f4f99a18e4526f5a3108bb28f4946261c445ab44cd9b9f750e3674 |
|
MD5 | 703870a7daa6ee5ae78059fdfdcb6708 |
|
BLAKE2b-256 | 8061b8bff2733d07e1f7109ac317676e111a9b85ac227da76c21d80e0bdd1561 |