# Hydrodynamic modeling (CFD) on terrain with MantaFlow and visualization of results in ParaView

Discipline Computational fluid dynamics (CFD) or, in Russian, Computational fluid dynamics studies the behavior of various flows, including vortex ones. This is the simulation of a tsunami, and lava flows, and stones ejected from the mouth of a volcano along with lava and gases, and much more. Let's see how you can use MantaFlow and ParaView , implemented in the embedded MantaFlow language Python required data conversion functions. As usual, see my GitHub repository for the source code: MantaFlow-ParaView .

Visualization of a volcanic eruption plume. We have already seen this picture in a series of articles on visualization in ParaView How to render and animate (geophysical) models , discussed creating geological models For more information, see the article Computer vision methods for solving the inverse problem of geophysics , and now let's talk about modeling smoke.

## Introduction

Simplified methods that neglect friction are often used for visualization modeling, while for physical modeling it is necessary to simulate the Navier-Stokes equations, taking into account friction. Of course, the results of physical modeling can be used simply for visualization, although obtaining them is more resource-intensive. At the same time, many physical processes cannot be modeled without friction, for example, pyroclastic flow . Fortunately, over the past few years, physical modeling of flows has become more accessible, including due to machine learning methods - it has become possible to further refine an accurate low-detail model using appropriate methods (in my opinion, due to the self-similarity of processes at different scales, this is generally one of the most successful applications of machine learning techniques). Now, the open source 3D graphics program Blender uses the physics modeling based on the open framework MantaFlow .

As with satellite interferometry, see the previous article 21st Century Geology as Earth Data Science , flow modeling methods allow you to learn a lot about the processes that took place - to learn, not to assume. Indeed, modeling lava flows on the paleorelief (built, for example, by solving the inverse problem of geophysics), we can compare the resulting model with real [frozen] lava flows and fields, since they are strong enough and can be well preserved for many, many millions of years. The constructed model allows us to study the occurrence of various layers where we do not have geological information - often, the same volcano erupted many times from different vents, while lava flows of different eruptions [and of different composition] may overlap. In addition, the same model will show discrepancies between the used model of paleorelief and existing lava occurrences - and will allow for refinements. That is, instead of a complexly formalized geological intuition, we can work with a model, including changing parameters and assessing how different geological assumptions are generally reasonable. Since I am not a geologist myself, but a physicist, for me the modeling path is of quite understandable interest.As a result, a geologist can more accurately assess the possible areas of occurrence of minerals and their potential - of course, in a geological study the task is not at all to do without a geologist, but, as elsewhere, the value of the results is consistent with the well-known principle of informatics: "Garbage at the entrance - garbage at exit ", therefore every opportunity to clarify, certify and supplement the available data is literally worth its weight in gold (or oil, or water,.).

## MantaFlow

As mentioned above, MantaFlow is a well-known and supported software package, and there are also some interesting derivative projects based on it: with a lot of emphasis on using machine learning PhiFlow , with the ability to calculate process parameters from a set of snapshots reconstructScalarFlows and others.

MantaFlow allows you to simulate laminar and turbulent flows, including flames and puffs of smoke and water flows, and much more. For example, I built several models to evaluate the quality of the simulation. For example, this model of water flow looks great and even the formation and rupture of air bubbles in the flow is visible:

And this is a model of filling a given relief with a flow of water:

In the repository you will find another script for the pyroclastic flow model, with the density of the ejection of volcanic matter visualized in pseudo colors (the pressure and particle velocities are also usually needed, they are also calculated in the example, but, for simplicity, they are not saved in the script - this can be added in just a couple of lines of code, similar to storing density data).

## Add relief to MantaFlow

For me, the most convenient option is to use ParaView with my extension N-Cube ParaView plugin for 3D/4D GIS Data Visualization to build a 3D elevation model in ParaView based on NetCDF or GeoTIFF data and save the desired area in OBJ format for use in MantaFlow. Since MantaFlow is able to load and work with this format in dimensionless coordinates, we only need to specify the required dimension in dimensionless coordinates (say, 100% in horizontal coordinates and 25% in vertical - so that there is enough space to simulate a column of smoke) and save the transformation parameters to export results in physical coordinates. Here is a repository script that implements the corresponding function: mesh2manta.py

## Save the simulation results in MantaFlow for ParaView

Since we set the initial modeling space in physical coordinates (the OBJ file and its scaling factors), we have everything we need to save the results in physical coordinates. By default, MantaFlow saves dimensionless results in compressed Numpy array format, so we will add storage in VTK format, see the repository script npz2vtk.py . I will add that the script creates an array xarray: ND labeled arrays and datasets in Python , from which one command can save data in NetCDF format and some others.

## Rendering in ParaView

As we have already discussed in previous articles (with examples), ParaView supports working with data series, so we can work with 4D data - for example, in the form of 3D animation. Here is an example of a volcanic smoke animation from a series of VTK files exported from MantaFlow:

## High Detail Models

Increasing the detail of models requires more resources to build them. If models on a 64x64x64 grid are computed in a few minutes on a laptop, then doubling the resolution for each coordinate increases the time 8 times (the third power of two).

Below is a much more detailed model of a turbulent tornado from the MantaFlow project website:

This model seemed interesting to me and I tried to find out how it is built. I did not find it and turned to the authors, but they could not answer the question, they only reported that the calculations require the power of a computer cluster. Model building can probably be greatly accelerated by using the TensorFlow machine learning library, which support is built into MantaFlow (there are examples in the official repository).

## Conclusion

Although I wanted to write about building models earlier, I had to start with a series of articles about data visualization and then gradually move on directly to modeling. In fact, if you do not tell in advance how to at least see the results, it will not turn out interesting. Although it is possible to use MantaFlow in Blender, I don’t work with the latter myself, and I’m not sure how many of the readers are familiar with it. So let it be as it is - familiarity with ParaView earlier and a story about MantaFlow and ParaView, and those readers who want it - can try MantaFlow and Blender.

Thank you all for your attention, I think this is all with geophysical modeling for now, since then it would be necessary to move on to more complex things (for example, raster routing, which I already mentioned when processing satellite interferometry data - and this is also a probabilistic approach to modeling flows and much more), and at best, a quarter of the reader will remain with me - the head of some sleep-lover on the keyboard. If you're interested, check out my repositories on GitHub and posts on LinkedIn for tips on what you think is worth sharing.

...Source