Basic run: local cavity¶
Run an unchanged OpenFOAM case through Longship; closures, transforms and observations are omitted when unused.
Before running¶
Install FoamNordic, build its integration and check the OpenFOAM environment. Start inside a FoamNordic checkout containing the tutorial cases. Runs are copied below the notebook output directory.
This example omits the scheduler to run locally with OpenFOAM.app (of_cmd="openfoam", shell="zsh"). On Linux, select the local OpenFOAM activation and shell in the case declaration.
Read or download the original notebook. Run the original cells below in order.
The code shows the main workload choices and leaves advanced execution options at their defaults; see the execution reference. Figure styling uses Hedgehogs helpers on Matplotlib axes.
Preamble¶
# Standard Library
from pathlib import Path
import foamnordic as fno
Directories¶
# Case Type and Name
CASE_TYPE = "laminar"
CASE_NAME = "lidDrivenCavity"
CATEGORY = "incompressible"
# FoamNordic Project Directory
PROJECT_DIR = Path.cwd().resolve()
BASE_DIR = next((path for path in (PROJECT_DIR, *PROJECT_DIR.parents) if (path / "tutorials" / "openfoam_tutorials").is_dir()), None)
if BASE_DIR is None:
raise FileNotFoundError("Run this notebook from inside the FoamNordic source tree")
MAIN_DIR = BASE_DIR / "tutorials" / "foamnordic_tutorials" / CATEGORY
OF_SCRIPT_DIR = BASE_DIR / "tutorials" / "openfoam_tutorials" / CATEGORY / CASE_TYPE / CASE_NAME
# Output Directory
OUTPUT_DIR = MAIN_DIR / "output"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
Configuration¶
# Local configuration
N_TASKS = 1
Example - OpenFOAM on FoamNordic (Baseline)¶
Case Definition¶
# Initialize the OpenFOAM case
case = fno.OpenFOAM.Case(
name=CASE_NAME,
case_dir=OF_SCRIPT_DIR,
run_dir=OUTPUT_DIR,
of_cmd="openfoam",
shell="zsh",
application="pimpleFoam",
ranks=N_TASKS,
)
case.initialize(ranks=N_TASKS, mesh="blockMesh", validate_mesh=True);
Submit Job¶
# Connect the OpenFOAM case and execution resources
longship = fno.Longship(
case=case,
verbose=True,
)
# Launch the workload
run = longship.launch(verbose=None)
# Wait for the job to complete (polling the job status)
result = run.stop(force=False, timeout=3600, progress=True)
# Summary of the job result
result.summary(style="compact", display=True);
Post-processing¶
# Postprocessing
post = result.postprocess
velocity = post.field(name="U", time_idx=-1, physical_time=None)
pressure = post.field(name="p", time_idx=-1, physical_time=None)
print("U shape:", velocity.shape)
print("p shape:", pressure.shape)
statistics = post.statistics(
fields=["U", "p"],
time_idx=-1,
physical_time=None,
verbose=True,
)
Check the result¶
A successful run reports success and exposes U and p through postprocessing. Field sizes and values depend on the case.