Applying ai to OpenFOAM
2026-04-03
AI Agent for OpenFOAM Setup
Setting up an OpenFOAM case is a chalenging task. It requires answering a series of technical questions,
- What is the fluid type - gas, liquid or multiphase?
- What is the expected velocity range?
- Is the flow laminar or turbulent?
- Is heat transfer involved?
- Are gravitional effects significant?
- …
Each of these decisions directly determines the parameters and the values of multiple input files. For example, when modeling water flow through a pipe, the flow region depends on the Reynolds number: it is laminar at low values and turbulent at high values. This distinction affects near-wall meshing and turbulent modelling. The choices propagate across several OpenFOAM files including:
- physicalProperties and momentumTransport in folder constant
- fvSchemes and fvSolution in folder system
- Initial value and boundary condition files in folder 0 (or 0.orig)
Incorrect settings can prevent a simulation from running, cause computation divergence, or produce unphysics results.
In practice, many engineers using OpenFOAM may not be familiar with the underlying numerical methods. As a result, defining appropriate discretization schemes and solver settings becomes a significant barrier, often leading to frustration and underutilization of the software.
Ideally, a trained CFD expert would handle these tasks. However, enabling engineers and scientists to run reliable simulations independently would significantly make OpenFOAM used to promote designs and innovations.
Using an LLM as an OpenFOAM Assistant
A pre-trained large language model (LLM) can help bridge this gap by translating natural language descriptions into valid OpenFOAM configuration files.
Below is a simple example using a generative AI model:
from google import genai
client = genai.Client()
# base prompt
prompt = """
Write only OpenFOAM input file fvScheme,
OpenFOAM version v1421,
solver is simpleFOAM,
flow is turbulent flow,
turbulent is modelled with k-epsilon model,
Do not include markdown fences.
"""
response = client.models.generate_content(
model="gemini-3-flash-preview",
contents=prompt,
)
code = response.text
with open("fvScheme", "w", encoding="utf-8") as f:
f.write(code)
The generated fvScheme file:
FoamFile
{
version 2.0;
format ascii;
class dictionary;
location "system";
object fvSchemes;
}
ddtSchemes
{
default steadyState;
}
gradSchemes
{
default Gauss linear;
grad(U) Gauss linear;
}
divSchemes
{
default none;
div(phi,U) bounded Gauss linearUpwind grad(U);
div(phi,k) bounded Gauss upwind;
div(phi,epsilon) bounded Gauss upwind;
div((nuEff*dev2(T(grad(U))))) Gauss linear;
}
laplacianSchemes
{
default Gauss linear corrected;
}
interpolationSchemes
{
default linear;
}
snGradSchemes
{
default corrected;
}
wallDist
{
method meshWave;
}
This example demonstrates that a pre-trained model can translate high-level user intent into a structured OpenFOAM configuration file. This is a promising step toward automating case setup.
Challenges and Next Steps
OpenFOAM has evloved over many versions - roughly one per year for the past two decades. As a result, configuration formats and available options can vary between versions. A general-purpose LLM may inadvertently mix syntax or settings from different versions, leading to incrrect or incompatible input files.
To address this, the next work is to develop prompting strategy and to fine-tune or constrain the model using a curated dataset of validated OpenFOAM cases. This would improve reliability.
Summary
LLM-based assistants have the potential to significantly lower the barrier to entry for OpenFOAM by automating complex and error-prone setup tasks. While challenges remain, the approach shows strong promise for enabling faster and more accessible CFD workflows.