Snek5000 and Fluidsimfoam:
new Python frameworks for
Nek5000 and OpenFOAM


Pierre Augier, Ashwin Vishnu Mohanan,
Arman Khoubani, Pooria Danaeifar


Journées thématiques de la Société Française de Thermique, 3-4 July 2023

Me and Fluidsim

  • Me: CNRS researcher

    • stratified turbulence
    • scientific programming with Python


  • Fluiddyn project
    open-source Python for fluid dynamics
lab experiments fluidlab image processing fluidimage simulations fluidsim performance transonic


  • Fluidsim: user centric CFD framework

    • internal solvers mostly based on pseudo-spectral methods

    • fluidsim-core: library to build solvers from any methods and codes

Snek5000: Fluidsim framework for Nek5000


For Arman Khoubani’s PhD thesis… 2 new Python packages:

Collaboration with Ashwin Vishnu Mohanan (Stockholm university)

Snek5000: Fluidsim framework for Nek5000

  • A new Python package

  • Improve user experience for all aspects of numerical studies using Nek5000

  • Good quality software:
    tested (coverage > 95%), documented, modular, user centric

  • A new workflow for Nek5000


Success for our study on instabilities in vertical convection!

  • Tens of simulations with complex relations

  • Advanced postprocessing with Python

In my lab (LEGI)

  • 0 Nek5000 user…

  • many OpenFOAM users…

So let’s build a Fluidsim framework for OpenFOAM! (Master 2 internship, Pooria Danaeifar)

Fluidsim + OpenFOAM -> Fluidsimfoam

Fluidsimfoam: Fluidsim framework for OpenFOAM

  • A (very) new Python package

  • Improve user experience for all aspects of numerical studies using OpenFOAM

  • Good quality software:
    tested (coverage > 95%), documented, modular, user centric

  • A new workflow for OpenFOAM

Improve user experience and productivity for CFD studies with Python/IPython

About Nek5000 and OpenFOAM

2 CFD open-source community driven frameworks

Nek5000

  • FORTRAN
  • spectral element methods
  • simple geometries
  • NS equations with some extensions
  • academic community

OpenFOAM

  • C++
  • finite volume
  • unstructured meshes
  • multi-physics
  • big community (academic and industry)


Great tools

  • performance and features
  • not very user friendly

Snek5000 and Fluidsimfoam

Principles

  • Avoid manual copy and edition of OpenFOAM/Nek5000 input files

  • Split the workflow in 2 steps

    1. Described sets of potential simulations

    2. Create the directory/files for 1 particular simulation and launch it

  • Integrated object oriented API (sim object)

  • Share and reuse more than just the core CFD codes

Workflow split in 2 steps

1st step: described a set of potential simulations

The set (and the associated parameters) are described in a small Python package called a “Fluidsim solver”

  • Python API to describe and create parametrized input files
  • Potentially Jinja templates

Warning for Openfoam users

“Fluidsim solver” and “OpenFOAM solvers” are very different things!

Snek5000 and Fluidsimfoam

Particularly suitable for

  • 1 script with arguments to launch different simulations

  • Automation of case generation, simulation launching and postprocessing (parametric studies, optimization, …)

  • Programmatic generation of complex and parametrized input files (for example blockMeshDict) and initial conditions

  • Programmatic control of simulations at runtime (example here)

  • Simple visualisations (figures and movies) with 1 line of Python

Demo

  1. Install

  2. Launch a simulation with an existing solver

  3. Reload a sim object for runtime control / post-processing / plots

  4. Create a new Fluidsimfoam solver from a case

Demo: installation

Require Python >=3.9


Snek5000

Clone NEK5000 code and pip install snek5000-cbox


Fluidsimfoam

Install OpenFOAM and pip install fluidsimfoam

Demo: launch a simulation with an existing solver

Run a script (or notebook) with

python doc/examples/fluidsimfoam-dam/doc/tuto_simple.py

Content of the script:

from fluidsimfoam_dam import Simul

params = Simul.create_default_params()

params.output.sub_directory = "tuto_fluidsimfoam/dam"
params.control_dict.end_time = 4.0

params.parallel.method = "simple"
params.parallel.nsubdoms = 2
params.parallel.nsubdoms_xyz = [2, 1, 1]

params.constant.transport.water.nu = 0.5e-6

params.block_mesh_dict.height_dam = 0.5
params.block_mesh_dict.width_dam = 0.2
params.block_mesh_dict.nx = 80
params.block_mesh_dict.ny = 80

# creation of the simulation directory
sim = Simul(params)
# run the simulation (i.e. all necessary OpenFOAM commands)
sim.make.exec("run")
# or for programmatic control of the simulation
# sim.make.exec_async("run")

Demo: reload a sim object

Applications: runtime control / post-processing / plots

In a terminal:

cd /path/to/directory
fluidsimfoam-ipy-load

which gives:

Python 3.9.2 (default, Feb 28 2021, 17:03:44)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.
Loading simulation
path_run: /home/users/me/Sim_data/tests_fluidsimfoam/dambreak/dambreak_run_2023-06-08_14-56-26
INFO     sim:                        <class 'fluidsimfoam_dambreak.Simul'>
         sim.output.log:             <class 'fluidsimfoam.output.log.Log'>
         sim.output.fields:          <class 'fluidsimfoam.output.fields.Fields'>
         input_files:
           - in 0:          U alpha_water p_rgh
           - in constant:   g transportProperties turbulenceProperties
           - in system:     blockMeshDict controlDict decomposeParDict fvSchemes fvSolution sampling setFieldsDict
         sim.output:                 <class 'fluidsimfoam_dambreak.output.OutputDambreak'>
         sim.oper:                   <class 'fluidsimfoam.operators.Operators'>
         sim.init_fields:            <class 'fluidsimfoam.init_fields.InitFields'>
         sim.make:                   <class 'fluidsimfoam.make.MakeInvoke'>

`sim`, `params`, `np`, `plt` and `pd` variables are available

In [1]:

Demo: reload a sim object

Then, in IPython shell:

sim.params

sim.stop_time_loop()

sim.output.log.plot_clock_times()
sim.output.log.time_last

# Change a parameter affecting just one file:
sim.params.control_dict.end_time = 2
sim.input_files.control_dict.generate_file()

# Get the cells coordinates
x, y, z = sim.oper.get_cells_coords()

# Read output fields:
field = sim.output.fields.read_field("U", time_approx="last")
vx, vy, vz = field.get_components()

# Plots
sim.output.plot_coutour("alpha.water", time=0.4)
sim.output.plot_profile()

How the fluidsimfoam-dam solver was created?


fluidsimfoam-initiate-solver dambreak \
  -c $FOAM_TUTORIALS/multiphase/interFoam/laminar/damBreak/damBreak


OpenFOAM users, try it for one of your case!

fluidsimfoam-initiate-solver great-short-name -c /path/to/your/case

Errors? Create an issue here: https://foss.heptapod.net/fluiddyn/fluidsimfoam/-/issues

Conclusions and perspectives

FluidDyn project: Python packages for fluid mechanics

fluiddyn, fluidimage, fluidlab, fluidsim
and two promissing kids!

2 new frameworks for Nek5000 and OpenFOAM

  • Strong underpinnings
  • A new workflow based on Python/IPython
  • Can greatly improve productivity of users with Python skills

Become community projects?

We need to build a community of users (creating issues and feature requests), contributors and core developers.

If you are interested, do not hesitate to (i) star the projects and (ii) open issues!