PythonFOAM: In-situ data analyses with OpenFOAM and Python

Romit Maulik, Dimitrios Fytanidis, Bethany Lusch, Venkatram Vishwanath, Saumil Patel

TL;DR

PythonFOAM is a new framework that embeds a Python interpreter directly into the OpenFOAM C++ environment, allowing researchers to perform complex data analysis—like deep learning and singular value decomposition—in-situ (in-memory) during a simulation. By bypassing the massive I/O bottlenecks of saving terabytes of data to disk, it enables real-time, distributed data science for exascale fluid dynamics.

What Problem Does This Paper Address?

In the world of Computational Fluid Dynamics (CFD), we are victims of our own success. Modern supercomputers allow us to simulate turbulent flows with incredible resolution, but these simulations generate mountains of data. Traditionally, researchers save "snapshots" of the simulation to a hard drive and analyze them later.

This workflow hits two major walls:

  1. The I/O Bottleneck: Writing and reading terabytes of data is significantly slower than the actual computation.
  2. Storage Limits: We often can't afford to save every timestep, meaning we lose high-frequency temporal information.

The authors address this by creating a flexible, general-purpose bridge between OpenFOAM (the industry standard for C++ CFD) and the Python ecosystem (the industry standard for Data Science). This allows for "in-situ" analysis—processing data while it still resides in the system's RAM.

Methods

The core of PythonFOAM is the use of the Python/C API to embed an interpreter within the OpenFOAM solver.

Key technical innovations include:

  • NumPy Casting: The framework uses the NumPy C-API to cast OpenFOAM’s double-precision data structures directly into NumPy arrays. This allows Python libraries like SciPy or TensorFlow to "see" the simulation data with minimal overhead.
  • MPI Synchronization: For large-scale simulations running across hundreds of CPU cores, PythonFOAM ensures that the C++ solver and the Python mpi4py library share the same Message Passing Interface (MPI) communicator. This allows Python functions to perform distributed calculations (like a global SVD) across different computing ranks in parallel with the solver.
  • Blocking Workflow: The current implementation uses a "blocking" paradigm where the solver pauses, hands data to Python for analysis, and resumes once the analysis is complete.

Datasets

The authors validated PythonFOAM using two canonical CFD benchmarks rather than static datasets:

  1. LES of a Backward-Facing Step: A classic test for flow separation and recirculation. This was used to test "Online SVD" for extracting coherent structures.
  2. Turbulent Channel Flow ($Re_\tau = 395$): A simulation with 3.84 million degrees of freedom used to test the scaling and performance of the distributed SVD across up to 128 nodes.

Key Insights and Ideas

  • The "In-Situ" Advantage: By using an Online Singular Value Decomposition (SVD), the authors showed they could extract the most important physical structures of a flow (the "modes") without ever saving a full snapshot to disk. This effectively compresses the physics in real-time.
  • Neural Network Integration: The paper demonstrates a "Deep Autoencoder" implemented in TensorFlow that runs inside the simulation loop. It learns a non-linear, low-dimensional representation of the flow, compressing the complex field into just a few "latent" variables.
  • The "Python Tax" is Low: A common fear in HPC is that Python is too slow. The authors found that the computational cost of the PDE solver (the physics) so heavily dominates the total time that the overhead of casting data to NumPy and running Python analysis is often negligible.

Key Results

  • Strong Scaling: The distributed SVD (using the APMOS algorithm) showed excellent scaling on the Intel Broadwell architecture, maintaining efficiency as the simulation was spread across more nodes.
  • Architecture Comparison: Interestingly, the implementation performed significantly faster on Intel Broadwell nodes than on the many-core Knights Landing (KNL) architecture, highlighting the need for architecture-specific optimizations in hybrid C++/Python code.
  • Data Compression: The deep learning autoencoder successfully compressed the flow field to a bottleneck of just 4 neurons while still being able to reconstruct the primary features of the streamwise velocity.

Limitations and Future Work

  • Asynchronous Coupling: Currently, the solver must wait for Python to finish (blocking). The authors identify "loose coupling"—where the analysis runs on separate cores or accelerators (like GPUs) while the solver continues—as a critical next step.
  • Memory Management: While casting to NumPy is efficient, the "in-situ" approach still requires keeping snapshots in memory, which can be a constraint for extremely large problems.
  • Optimization: The performance gap on KNL suggests that more work is needed to leverage high-bandwidth memory (MCDRAM) and advanced vectorization when calling Python from C++.

Summary updated

BibTeX
@article{maulik2022pythonfoam,
  author = {Romit Maulik and Dimitrios Fytanidis and Bethany Lusch and Venkatram Vishwanath and Saumil Patel},
  title = {{PythonFOAM: In-situ data analyses with OpenFOAM and Python}},
  journal = {J. Comput. Sci.},
  year = {2022},
  doi = {10.1016/j.jocs.2022.101750},
}

Loading related content...

Figure