In this guide, we will walk you through setting up and running OpenFOAM simulations using the Inductiva API.

We will cover:

  • Configuring OpenFOAM simulations with the appropriate input directories.

  • Example codes to help you get started with simulations.

  • An advanced example for running MB9 Micro-benchmark by ExaFOAM.

OpenFOAM#

OpenFOAM is a Finite Volume method for CFD simulations with a wide range of applications across several areas of engineering and science. It offers a broad set of features for everything from complex fluid flows (including chemical reactions, turbulence, and heat transfer) to solid dynamics and electromagnetics.

There are two main open-source distributions of OpenFOAM: one developed by the OpenFOAM foundation and another by the ESI Group. The Inductiva API supports both, and you can select your preferred distribution by setting the distribution parameter when initializing the simulator. By default, it uses the OpenFOAM Foundation version.

We are assuming the canonical file structure for OpenFOAM simulations, which includes the time, constant, and system directories.

  • time: Contains files for particular fields, like initial values and boundary conditions that you must specify. For example, initial conditions at t=0 are stored in the 0 directory.

  • constant: Contains files that describe the objects in the simulation and the physical properties being modeled.

  • system: Contains files that describe the simulation, including solvers, numerical parameters, and output files.

In order to run your simulation you can simply run your Allrun script and you are good to go.

First Example: Motorbike Tutorial#

In this example, we demonstrate how to run the motorbike tutorial tutorial using the OpenFOAM Foundation distribution.

"""OpenFOAM Foundation example."""
import inductiva

# Instantiate machine group
machine_group = inductiva.resources.MachineGroup("c2-standard-8")
machine_group.start()

# Set simulation input directory
input_dir = inductiva.utils.download_from_url(
    "https://storage.googleapis.com/inductiva-api-demo-files/"
    "openfoam-input-example.zip",
    unzip=True)

# Initialize the Simulator
openfoam = inductiva.simulators.OpenFOAM(distribution="foundation")

# Run simulation with config files in the input directory
task = openfoam.run(input_dir=input_dir,
                    shell_script="./Allrun",
                    on=machine_group)

task.wait()
task.download_outputs()

machine_group.terminate()

The current example is devided into four steps:

  1. Configure the Machine Type: In this step, we define the machine type and start it.

  2. Download Input Files: In this step, we retrieve the input files from the Inductiva bucket.

  3. Pick the Simulator: We select the simulator we want to use. in this case, the OpenFOAM Foundation distribution.

  4. Run the Simulation: In the final step, we run the simulation using the run method, specifying the shell_script responsible for running the process.

The last three lines handle post-simulation tasks: waiting for the simulation to finish, downloading the outputs, and terminating the machine, in that order.

Example Code - ESI Distribution#

To run the sample simulation above, download the openfoam-esi-input-example.zip file, select the appropriate distribution with inductiva.simulators.OpenFOAM(distribution="esi"), and run the corresponding Allrun script.

Advanced Tutorial: Running the MB9 Micro-benchmark from ExaFOAM#

This guide walks you through running a complex OpenFOAM simulation using the MB9 micro-benchmark from ExaFOAM. This benchmark simulates a high-lift aircraft configuration, ideal for studying near-wall turbulence using wall-modeled Large Eddy Simulation (WMLES).

Objective#

We’ll run this simulation on a single 360 vCPU machine.

Prerequisites#

  1. Download Input Files: Get the input files from the repository and place them in a folder named highLiftConfiguration.

    Directory Structure:

    ls -lasgo highLiftConfiguration
    total 104
    0 drwxrwxr-x@ 14     448 Sep 23 11:44 .
    0 drwx------@ 19     608 Sep 23 11:49 ..
    0 drwxrwxr-x@ 12     384 Sep 20 09:43 0.orig
    8 -rwxr-xr-x@  1     626 Jun 13 10:09 Allclean
    16 -rwxr-xr-x@  1    6998 Jun 13 10:09 Allrun
    8 -rw-rw-r--@  1     991 Jun 13 10:09 COPYING
    48 -rw-rw-r--@  1   21547 Jun 13 10:09 README.md
    0 -rw-rw-r--@  1       0 Jun 13 10:09 case.foam
    0 drwxrwxr-x@  6     192 Sep 20 09:43 constant
    0 drwxrwxr-x@ 13     416 Jun 13 10:09 figures
    0 drwxrwxr-x@ 28     896 Sep 20 09:43 system
    24 -rw-rw-r--@  1   11399 Jun 13 10:09 thumbnail.png
    

Overview#

Here’s the code you’ll be working on as we progress through the tutorial. Don’t worry if it doesn’t all make sense right now; everything will become clearer in the upcoming steps.

import inductiva

machine_group = inductiva.resources.MachineGroup(
            machine_type="c3d-highcpu-360",
            spot=True)
machine_group.start()

input_dir = "/path/to/highLiftConfiguration"

#Choose your simulator
openfoam = inductiva.simulators.OpenFOAM(distribution="esi")

task = openfoam.run(
            input_dir=input_dir,
            shell_script="./Allrun",
            n_vcpus=180,
            use_hwthread=True,
            on=machine_group)

task.wait()
task.download_outputs()
machine_group.terminate()

task.print_summary()

Step 1: Adjust Simulation Parameters#

For a faster simulation, modify the following parameters in the case definition file (system/include/caseDefinition):

  • Time Step (dt): Set to 0.00002.

  • Start Time (initTime): 0.10

  • End Time (finalTime): 0.30

Step 2: Running the Simulation#

a. Configure and Start Machine#

  1. Pick your machine:

    import inductiva
    machine_group = inductiva.resources.MachineGroup(
            machine_type="c3d-highcpu-360",
            spot=True)
    

    Note: spot machines are a lot cheaper but can be terminated by the provider if needed.

  2. Start your machine

    machine_group.start()
    

b. Simulation inputs#

  1. Specify Simulation Directory: Let’s start by defining a variable that points to the highLiftConfiguration folder where all your simulation files are located.

    input_dir = "/path/to/highLiftConfiguration"
    
  2. Bash script: To run this simulation, simply execute the Allrun file by pointing to it like so:

     shell_script = "./Allrun"
    

c. Run your simulation#

  1. Run the simulation: We now have all we need to run our simulation.

    #Choose your simulator
    openfoam = inductiva.simulators.OpenFOAM(distribution="esi")
    
    task = openfoam.run(
                input_dir=input_dir,
                shell_script="./Allrun",
                on=machine_group)
    
  2. Wait and Download Outputs: That is it. Our simulation is now running on the cloud. We can wait for the simulation to be over, or we can turn our computer off go for a coffe (☕️).

    task.wait()
    task.download_outputs()
    
  3. Terminate Machine: Once our simulation is over we can/should terminate our machine to save on costs. If you forget, dont worry we got your back. By default, a machine will be automaticly terminated if no simulation runs on it for 30 minutes.

    machine_group.terminate()
    
  4. Check your simulation summary: Now that our simulation has finished we can print a summary of said simulation. This includes information about the execution times, outputs generated and much more.

    task.print_summary()
    
    inductiva tasks info j416r4dv5u461ys7ovghp7so1
    
    Task status: Success
    
    Timeline:
       Waiting for Input         at 25/09, 06:38:51      15.627 s
       In Queue                  at 25/09, 06:39:06      18.72 s
       Preparing to Compute      at 25/09, 06:39:25      2.45 s
       In Progress               at 25/09, 06:39:28      145200.342 s
       Finalizing                at 26/09, 22:59:28      1037.572 s
       Success                   at 26/09, 23:16:46      
    
    Data:
       Size of zipped output:    24.76 GB
       Size of unzipped output:  30.83 GB
       Number of output files:   25585
    
    Estimated computation cost (US$): 162.19 US$
    
    Go to https://console.inductiva.ai/tasks/j416r4dv5u461ys7ovghp7so1 for more details.
    

Set Commands Manually#

For greater flexibility, you can manually set and run commands one by one, giving you complete control over your simulation.

If you decide to set your commands manually, here is an example:

commands_single_machine = [
    "runApplication surfaceFeatures",
    "runApplication blockMesh",
    "runApplication decomposePar -copyZero",
    "runParallel snappyHexMesh -overwrite",
    "runParallel potentialFoam",
    "runParallel simpleFoam",
    "runApplication reconstructParMesh -constant",
    "runApplication reconstructPar -latestTime"
]

task = openfoam.run(
            input_dir=input_dir,
            commands=commands_single_machine,
            on=machine_group)

For more details on commands and MPI configuration, refer to the Custom Docker Images documentation.