├── CNAME ├── index.markdown ├── .gitignore ├── assets ├── img │ ├── cpp_intellisense.png │ ├── multi_stage_docker.png │ ├── vscode_edit_setting.png │ ├── cavity_crop-1024x698.png │ ├── cylinder_post-1024x576.png │ ├── boxes_post_crop-1024x915.png │ ├── andre_weiner_reduced-edited.jpg │ └── pytorch_download_selection.png └── main.scss ├── 404.html ├── Gemfile ├── _config.yml ├── _posts ├── 2020-06-12-3-ways-to-run-tutorials-in-OpenFOAM.markdown ├── 2020-12-20-openfoam-docker-workflow.markdown ├── 2020-12-29-running-pytorch-models-in-openfoam.markdown └── 2020-12-29-minimizing-docker-images.markdown ├── Gemfile.lock └── about.markdown /CNAME: -------------------------------------------------------------------------------- 1 | ml-cfd.com -------------------------------------------------------------------------------- /index.markdown: -------------------------------------------------------------------------------- 1 | --- 2 | layout: home 3 | --- 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | _site 2 | .sass-cache 3 | .jekyll-cache 4 | .jekyll-metadata 5 | vendor 6 | -------------------------------------------------------------------------------- /assets/img/cpp_intellisense.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreWeiner/ml-cfd/HEAD/assets/img/cpp_intellisense.png -------------------------------------------------------------------------------- /assets/img/multi_stage_docker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreWeiner/ml-cfd/HEAD/assets/img/multi_stage_docker.png -------------------------------------------------------------------------------- /assets/img/vscode_edit_setting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreWeiner/ml-cfd/HEAD/assets/img/vscode_edit_setting.png -------------------------------------------------------------------------------- /assets/img/cavity_crop-1024x698.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreWeiner/ml-cfd/HEAD/assets/img/cavity_crop-1024x698.png -------------------------------------------------------------------------------- /assets/img/cylinder_post-1024x576.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreWeiner/ml-cfd/HEAD/assets/img/cylinder_post-1024x576.png -------------------------------------------------------------------------------- /assets/main.scss: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | 4 | @import "minima"; 5 | 6 | .page-content { padding: 30px 0; flex: 1; text-align: justify;} -------------------------------------------------------------------------------- /assets/img/boxes_post_crop-1024x915.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreWeiner/ml-cfd/HEAD/assets/img/boxes_post_crop-1024x915.png -------------------------------------------------------------------------------- /assets/img/andre_weiner_reduced-edited.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreWeiner/ml-cfd/HEAD/assets/img/andre_weiner_reduced-edited.jpg -------------------------------------------------------------------------------- /assets/img/pytorch_download_selection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreWeiner/ml-cfd/HEAD/assets/img/pytorch_download_selection.png -------------------------------------------------------------------------------- /404.html: -------------------------------------------------------------------------------- 1 | --- 2 | permalink: /404.html 3 | layout: default 4 | --- 5 | 6 | 19 | 20 |
Page not found :(
24 |The requested page could not be found.
25 |
|
10 | |:--:|
11 | |*That's me in 2017.*|
12 |
13 | ### Profile pages
14 |
15 | If you are interested in my academic curriculum, have a look at the following profile pages:
16 |
17 | - [Google Scholar](https://scholar.google.de/citations?user=wCmTgzgAAAAJ&hl=de) (journal articles)
18 | - [Technical University of Dresden](https://tu-dresden.de/ing/maschinenwesen/ism/psm/die-professur/beschaeftigte/weiner-andre) (since Sep 2023)
19 | - [Technical University of Braunschweig](https://www.tu-braunschweig.de/ism/forschung/modellierung-und-regelung-von-stroemungen/mitarbeiter/weiner) (May 2020 - Jul 2023)
20 | - [Technical University of Darmstadt](https://www.mma.tu-darmstadt.de/index/mitarbeiter_3/mitarbeiter_details_mma_45120.en.jsp) (Oct 2014 - Apr 2020)
21 |
22 | ### Scientific software packages
23 |
24 | - **flowTorch** - a Python library for analysis and reduced-order modeling of fluid flows, [Github](https://github.com/FlowModelingControl/flowtorch)
25 | - **drlFoam** - Deep Reinforcement Learning with OpenFOAM, [Github](https://github.com/OFDataCommittee/drlfoam)
26 |
27 | ### University courses
28 |
29 | - [Machine learning in computational fluid dynamics](https://github.com/AndreWeiner/ml-cfd-lecture), TU Braunschweig and TU Dresden, since winter term 2021/2022; I designed this course from scratch; lecture notes, slides, exercises, and datasets are freely available
30 | - [Modeling and simulation of turbulent flows](https://github.com/AndreWeiner/turbulence_modelling_lecture), TU Braunschweig, summer term 2023; I designed this course mostly from scratch (syllabus, lecture slides, exercises)
31 | - [Computational fluid dynamics](https://tu-dresden.de/ing/maschinenwesen/ism/psm/studium/lehrveranstaltungen/computational-fluid-dynamics), TU Dresden, since winter term 2024/2025
32 |
33 | ### Presentation and training slides
34 |
35 | - [Habilitationsvorhaben](https://andreweiner.github.io/slides/habil.html), faculty council mechanical engineering, **Oct 2025**, Dresden
36 | - [Bayesian parameter and design optimization](https://andreweiner.github.io/slides/ofw25-bo.html), 20th OpenFOAM workshop, **Jul 2025**, Vienna
37 | - [Analyse turbulenter Strömungen - Wie man Ordnung im Chaos findet](https://andreweiner.github.io/slides/lndw2025.html), Lange Nacht der Wissenschaften, **Jun 2025**, Dresden
38 | - [Model-based DRL for accelerated learning from flow simulations](https://andreweiner.github.io/slides/mbdrl-2025-2.html), seminar talk ILR TU Berlin, **Feb 2025**, Berlin
39 | - [Data-driven modeling, optimization, and control in CFD](https://andreweiner.github.io/slides/ml-cfd-2025.html), invited talk automotive industry, **Feb 2025**
40 | - [A brief introduction to Bayesian optimization](https://andreweiner.github.io/reveal.js/bayesopt_intro.html#/), PSM seminar, **Oct 2024**, Dresden
41 | - Best practice guidelines for the modal analysis of numerical and experimental data, DLRK 2024, **Sep 2024**, Hamburg
42 | - [An optimized dynamic mode decomposition for flow analysis and forecasting](https://andreweiner.github.io/reveal.js/ofw2024_optdmd.html#/), 19th OpenFOAM workshop, **June 2024**, Beijing
43 | - Machine learning in computational fluid dynamics - an overview, invited talk, automotive industry, **July 2024**
44 | - Combining machine learning with computational fluid dynamics using OpenFOAM and SmartSim, 19th OpenFOAM workshop, **June 2024**, Beijing
45 | - Analyse turbulenter Strömungen - wie man Ordnung im Chaos findet, Lange Nacht der Wissenschaft, **June 2024**, Dresden
46 | - Modal analysis of fluid flows - best practices, ISM seminar - invited talk, **May 2024**, Braunschweig
47 | - [Model-based reinforcement learning for active flow control](https://andreweiner.github.io/reveal.js/gamm_2024.html#/), 94th GAMM, **Mar 2024**, Magdeburg
48 | - Extracting coherent structures and reduced-order models from flows, Bosch - invited talk, **Jan 2024**, Stuttgart
49 | - DMD analysis of the XRF-1 wing undergoing transonic shock buffet, DLRK 2023, **Sep 2023**, Stuttgart
50 | - [Advances in the application of DRL to flow control](https://andreweiner.github.io/reveal.js/ofw_talk_2023.html#/), 18th OpenFOAM workshop, **Jul 2023**, Genoa
51 | - [Setting up a new flow control problem in drlFoam](https://andreweiner.github.io/reveal.js/ofw_training_2023.html#/), 18th OpenFOAM workshop, **Jul 2023**, Genoa
52 | - [Model-based deep reinforcement learning for flow control](https://andreweiner.github.io/reveal.js/cfc2023.html#/), 22nd Computer Fluids Conference, **Apr 2023**, Cannes
53 | - [Data-driven modeling and validation of reactive mass transfer at rising bubbles](https://andreweiner.github.io/reveal.js/ofmf_short_course_mar2023.html#/), OpenFoam multiphase short course - invited talk, **Mar 2023**, Darmstadt
54 | - [ProDiGI - Machine learning in computational fluid dynamics lecture](https://andreweiner.github.io/reveal.js/prodigi_final.html#/), [ProDiGI](https://www.tu-braunschweig.de/en/teaching-and-media-education/our-services/international-teaching/prodigi) event, **Feb 2023**, Braunschweig
55 | - Analysis of shock buffet PSP measurements by means of dynamic mode decomposition, Airbus XRF-1 workshop, **Dec 2022**, virtual
56 | - [Activities of the data-driven modeling special interest group](https://andreweiner.github.io/reveal.js/release_webinar_2206.html#/), OpenFOAM-v2206 release webinar, **Jul 2022**, virtual
57 | - [Modal analysis of transonic shock buffets on a NACA-0012 airfoil](https://andreweiner.github.io/reveal.js/ofw2022_naca_dmd.html#/), 17th OpenFOAM workshop, **Jul 2022**, Cambridge
58 | - [Analyzing coherent structures by means of dynamic mode decomposition](https://andreweiner.github.io/reveal.js/ofw2022_dmd_training.html#/), 17th OpenFOAM workshop, **Jul 2022**, Cambridge
59 | - [Active flow control via deep reinforcement learning implemented in OpenFOAM](https://andreweiner.github.io/reveal.js/combustion_seminar_may2022.html#/), OpenFOAM combustion seminar - invited talk, **May 2022**, Singapore, virtual
60 | - [Simulation and modal analysis of transonic shock buffets on a NACA-0012 airfoil](https://andreweiner.github.io/reveal.js/euromech2022_buffet.html#/), Euromech Colloquium 612, **Mar 2022**, Aachen, virtual
61 | - [Machine learning in computational fluid dynamics - an overview](https://andreweiner.github.io/reveal.js/mlcfd_overview_feb2022.html#/), keynote lecture chemical industry, **Feb 2022**
62 | - [Simulation and modal analysis of transonic shock buffets on a NACA-0012 airfoil](https://andreweiner.github.io/reveal.js/aiaa2022_buffet.html#/), AIAA SciTech Forum, **Jan 2022**, San Diego, virtual
63 | - [Active control of the flow past a cylinder using deep reinforcement learning](https://andreweiner.github.io/reveal.js/of_conf_2021.html#/), OpenFOAM conference, **Oct 2021**, virtual
64 | - [flowTorch - A platform for analysis and reduced-order modeling of high-speed stall flow phenomena](https://andreweiner.github.io/reveal.js/DLRK_2021.html#/), DLRK 2021, **Aug 2021**, Bremen, virtual
65 | - [Machine learning-aided CFD with OpenFOAM and PyTorch](https://andreweiner.github.io/reveal.js/ofw_training_2021.html#/), training at 16th OpenFOAM Workshop, **Jun 2021**, Dublin, virtual
66 | - [Machine learning-aided CFD with OpenFOAM and PyTorch](https://andreweiner.github.io/reveal.js/ssd_seminar_2021.html#/), SSD Seminar - invited talk, **Jun 2021**, RWTH Aachen, virtual
67 | - [Sparse Spatial Samling - S³](https://wp-ml-cfd.s3.eu-central-1.amazonaws.com/wp-content/uploads/2020/12/29173309/aiaa_2021_fernex_weiner_noack_semaan.pdf), AIAA SciTech Forum, **Jan 2021**, Nashville, virtual
68 | - [Creating data-driven workflows with OpenFOAM and PyTorch](https://andreweiner.github.io/reveal.js/of_conf_2020.html#/), 8th ESI OpenFOAM conference, **Oct 2020**, Berlin, virtual
69 | - [An introduction to supervised learning by example: path regime classification](https://andreweiner.github.io/reveal.js/path_regime_classification_2020.html#/), internal training, **Aug 2020**, Braunschweig, virtual
70 | - [A hybrid approach to compute convection-dominated mass transfer at rising bubbles](https://andreweiner.github.io/reveal.js/gofun2020.html#/), 4th GOFUN, **Apr 2020**, Braunschweig, virtual
71 | - [Modeling and simulation of convection-dominated species transfer at rising bubbles](https://andreweiner.github.io/reveal.js/phd_defence.html#/), Ph.D. defense, **Jan 2020**, Darmstadt
72 | - [A brief introduction to machine learning and its potential application to CFD](https://andreweiner.github.io/reveal.js/ofw2019_slides.html#/), 14th OpenFOAM workshop, **Jul 2019**, Duisburg
73 | - [Data-driven subgrid-scale modeling for convection-dominated concentration boundary layers](https://andreweiner.github.io/reveal.js/ofw2019_sgs_modeling.html#/), 14th OpenFOAM workshop, **Jul 2019**, Duisburg
74 |
75 | ### Supervised student projects
76 |
77 | - [Learning of optimized multigrid solver settings for CFD applications](https://doi.org/10.5281/zenodo.10361535), Master thesis, **Janis Geise**, 2023, [Github](https://github.com/JanisGeise/learning_of_optimized_multigrid_solver_settings_for_CFD_applications)
78 | - [A comparison of reduced-order models for wing buffet predictions](https://zenodo.org/records/10255520), course project, **Anton Schreiber**, 2023, [Github](https://github.com/AntonSchreiber/ROM-buffet-forecasting)
79 | - Prediction of ice formation on heat exchanger surfaces and defrost control using machine learning, master project, together with [Coldsense](https://www.cold-sense.com/en/), **Tom Krogmann**, 2023
80 | - [Robust model-based deep reinforcement learning for flow control](https://zenodo.org/record/7642927#.ZEqIt41Bxkg), course project, **Janis Geise**, 2023, [Github](https://github.com/JanisGeise/robust_MB_DRL_for_flow_control)
81 | - [Optimal sensor placement for active flow control with deep reinforcement learning](https://zenodo.org/record/7636959#.Y-qTLcbMJkg), course project, **Tom Krogmann**, 2022, [Github](https://github.com/TomKrogmann/Optimal_Sensor_Placement_for_Active_Flow_Control_in_Deep_Reinforcement_Learning)
82 | - [Design and implementation of a data-driven wall function for the velocity in RANS simulations](https://zenodo.org/record/6590747), master project, **Jihoo Kang**, 2022, [Github](https://github.com/JihooKang-KOR/Data_driven_wall_modeling)
83 | - POD-based parametric reduced-order models for time-dependent fluid flows, course project, **Jan Schlüter**, 2022
84 | - [Model-based reinforcement learning for accelerated learning from CFD simulations](https://doi.org/10.5281/zenodo.6375574), course project, **Jan Erik Schulze**, 2022, [Github](https://github.com/ErikSchulze1796/Active_flow_control_past_cylinder_using_DRL)
85 | - Reduced-order modeling based on cluster-based network modeling applied to the latent variables of an autoencoder, course project, **Niels Formella**, 2021, [Github](https://github.com/nformella/Cluster-based-network-modeling-using-auto-encoders)
86 | - [Active control of the flow past a cylinder under Reynolds number variation using deep reinforcement learning](https://zenodo.org/record/5634050#.YaumMrso9hE), Bachelor thesis, **Fabian Gabriel**, 2021, [Github](https://github.com/FabianGabriel/Active_flow_control_past_cylinder_using_DRL)
87 | - Numerical investigation of 2D transonic shock-buffet around a NACA 0012-34 airfoil using OpenFOAM and flowTorch, course project, **Tushar Anil Gholap**, 2021, [Github](https://github.com/Tushargh29/transonic_shock_buffet)
88 | - [Active flow control in simulations of fluid flows based on deep reinforcement learning](https://zenodo.org/record/4897961#.YL58_TqxVhE), course project, **Darshan Thummar**, 2021, [Github](https://github.com/darshan315/flow_past_cylinder_by_DRL)
89 | - [Simulation of Fluid Flows based on the Data-driven Evolution of Vortex Particles](https://publikationsserver.tu-braunschweig.de/receive/dbbs_mods_00069386), Master thesis, **Vemburaj Chockalingam Yadav**, 2021, [Github](https://github.com/VemburajYadav/DeepLearningLagrangainVortexDynamics)
90 | - [Datenbasierte Subgridskalen-Modellierung reaktiver Konzentrationsgrenzschichten an freiaufsteigenden Einzelblasen](https://tuprints.ulb.tu-darmstadt.de/11667/), Master thesis, **Alexander Kiefer**, 2020
91 | - [A comparative study of different mesh types for transport processes near gas bubbles regarding accuracy, stability, and run time](https://tuprints.ulb.tu-darmstadt.de/7321/), Bachelor thesis, **Jan-Alexander Kleikemper**, 2018
92 | - [Numerical simulation of single rising bubbles influenced by soluble surfactant in the spherical and ellipsoidal regime](https://tuprints.ulb.tu-darmstadt.de/8296/), Master thesis, **Matthias Steinhausen**, 2018
93 | - [Numerical simulation of reactive species transfer at a spherical gas bubble](https://tuprints.ulb.tu-darmstadt.de/6940/), Bachelor thesis, **Tim Jeremy Patrick Karpowski**, 2017
94 |
--------------------------------------------------------------------------------
/_posts/2020-12-20-openfoam-docker-workflow.markdown:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 | title: "A detailed look at the OpenFOAM docker workflow"
4 | date: 2020-12-20 12:00:00 +0100
5 | categories: OpenFOAM Docker
6 | ---
7 |
8 | **Update (Mar 5 2022)**: the workflow described in this article has been revised at the end of 2022. Have a look at the [updated workflow](https://develop.openfoam.com/Development/openfoam/-/wikis/precompiled/docker). Nonetheless, if you are new to working with Docker and OpenFOAM, you will still learn the essentials from this article.
9 |
10 | ---
11 |
12 | This article is all about
13 |
14 | - what the installOpenFOAM script does,
15 | - what the startOpenFOAM script does, and
16 | - ways to ship your OpenFOAM/solvers/utilities using Docker.
17 |
18 | The quickest way to get a running OpenFOAM installation on any Linux distribution (or even Mac and Windows) is probably via a Docker image. In case you have never heard of Docker, and you are wondering why you should bother to use it, Robin Knowles from [CFD Engine](https://www.cfdengine.com/) wrote a fantastic article entitled [The complete guide to Docker & OpenFOAM](https://www.cfdengine.com/blog/how-to-install-openfoam-anywhere-with-docker/), which I really recommend to read before continuing with this post. If you are not in the mood to read another article, here is why you should care about Docker in a nutshell:
19 |
20 | - Docker allows creating isolated, standardized software images.
21 | - An image contains the software itself and all its dependencies, which is why you can share and run your app (almost) without limit.
22 | - This flexibility makes your app executable even if you do not know the hardware or operating system (OS) it is going to run on. You can run your app in the cloud, or you can share it quickly with a co-worker who uses a different OS, or you can conserve a solver and it will be still runnable in five years from now and it will still give the exact same results.
23 |
24 | If you followed the [installation instructions](https://openfoam.com/download/install-binary-linux.php) provided on the ESI OpenFOAM website, you installed Docker and executed two scripts, namely installOpenFOAM, and startOpenFOAM. Afterward, you were magically presented with a running OpenFOAM installation without having had to worry about any dependencies besides Docker. And there is more: in the isolated container environment, you still have the same username as on the Linux host and use the same credentials to run sudo commands. There is also a fully-fledged version of paraFoam available. You may be wondering why this should be a big deal?! Well, Docker is great for creating standardized, isolated, and minimalistic environments. Isolation means that Docker only uses few core components of the host system’s Linux kernel (Cgroups, Namespaces, etc. - [learn more](https://docs.docker.com/engine/security/security/)), but it doesn’t depend on or interact with any other applications, libraries, or configuration files of the host. To work in a Docker container feels a bit like working on a remote server, with the difference being only that this remote server is an isolated fraction of your workstation. Still, with the OpenFOAM-plus workflow, the OpenFOAM container integrates seamlessly into your system, and you can run simulations just as well as with the native installation. But how and when does the mapping of username, password, or permissions happen? And how does it become possible to access your simulation data if created in an isolated environment? It all happens with the execution of two short scripts. Understanding these scripts will enable you to modify them according to your needs. If you are curious to learn more, read on.
25 |
26 | ### *installOpenFOAM*
27 |
28 | Let’s start with the first script that was executed: `installOpenFOAM`. The name suggests that this script installs OpenFOAM on your computer, but as you will learn in the next paragraphs, there is no classical installation process when working with images/containers. A more suitable name might be `initOpenFOAMContainer` or even better `runOpenFOAMContainer`, but I guess such naming could confuse users new to Docker and containerization. The script may be divided into two logical parts: first, some useful environment variables are defined, and then the Docker run command is executed.
29 |
30 | ```bash
31 | username="$USER"
32 | user="$(id -u)"
33 | home="${1:-$HOME}"
34 | imageName="openfoamplus/of_v1812_centos73"
35 | containerName="of_v1812"
36 | displayVar="$DISPLAY"
37 | ```
38 |
39 | Line 1 and 2 define variables for username and user id. The username will be your login name as in the command line prompt, e. g. **username**`@workstation:~$`. The id is an integer value associated with the user, most likely 1001. The next line is of great importance because it defines where (in which path) you will interact with the OpenFOAM container. The syntax of the right-hand-side works as follows: `${defined_path:-default_path}`. The default path is simply your home directory `HOME`. The default path is used if no other valid path was given as the first command-line argument to the `installOpenFOAM` script. To change the default path, one would execute `./installOpenFOAM /absolute/alternative/path`. The imageName is the name of the OpenFOAM image hosted on [Dockerhub](https://hub.docker.com/r/openfoamplus/of_v1812_centos73). The part of the image name before the front slash corresponds to the Dockerhub user, here **openfoamplus**. The second part gives more information about the image. For example, the present image is built on CentOS 7.3 and contains version 1812 of OpenFOAM-plus. Last but not least, there is the `DISPLAY` variable, which tells an application with a graphical user interface (GUI) where to display the interface. On my laptop, the value of `DISPLAY` is simply `:0` (zero), which is my primary (and only) screen. Remember, Docker containers are a bit like remote servers, so you have to provide some additional information to use GUI applications. With all the information gathered up to this point, we are ready to move on to the actual container creation.
40 |
41 | The syntax to create and execute a container is `docker run [options] IMAGE [command] [args]` ([read more](https://docs.docker.com/engine/reference/run/)). The image name is stored in the variable `imageName` and appears only in the twelfth line of the command in the code box below. Every item between docker run and ${imageName} starting with `-` or `--` is an option. The command to be executed after the container was created is /bin/bash, which is why you are presented with a command-line prompt when starting the OpenFOAM container. Bash is run with the `-rcfile` argument to execute additional commands from the file specified thereafter (line 13). The content of `setImage.sh` is shown in the last code box of this article for completeness.
42 |
43 | {% highlight bash linenos %}
44 | docker run -it -d --name ${containerName} --user=${user} \
45 | -e USER=${username} \
46 | -e QT_X11_NO_MITSHM=1 \
47 | -e DISPLAY=${displayVar} \
48 | -e QT_XKB_CONFIG_ROOT=/usr/share/X11/xkb \
49 | --workdir="${home}" \
50 | --volume="${home}:${home}" \
51 | --volume="/etc/group:/etc/group:ro" \
52 | --volume="/etc/passwd:/etc/passwd:ro" \
53 | --volume="/etc/shadow:/etc/shadow:ro" \
54 | --volume="/etc/sudoers.d:/etc/sudoers.d:ro" \
55 | -v=/tmp/.X11-unix:/tmp/.X11-unix ${imageName} \
56 | /bin/bash --rcfile /opt/OpenFOAM/setImage_v1812.sh
57 | {% endhighlight %}
58 |
59 | Let’s take a closer look at all the container options displayed above because this is where the magic happens.
60 |
61 | | option | description |
62 | |:--:|:--|
63 | | `-i` or `--interactive` | keeps the standard input open even if you detach from the container, e.g., if you close the terminal |
64 | | `-t` or `--tty` | allocates a virtual console to interact with the container |
65 | | `-d` or `--detach` | allows running a container in the background; the container will not stop after you exit from the container |
66 | | `--name` | sets a container name |
67 | | `-u` or `--user` | creates an additional user within the container; the default user is `root` |
68 | | `-e` or `--env` | sets environment variables in the container |
69 | | `-w` or `--workdir` | sets the working directory inside the container, e.g., the default directory after attaching (logging in) to the container |
70 | | `-v` or `--volume` | binds a source from the host to the container; such a source might be a file, a directory, or a Docker volume |
71 |
72 | The first interesting option is the `--user` flag, which is used to create a new user inside the container with the same id as the user creating the container. Additionally, in line 2, the `-e` option sets the corresponding username. More environment variables are set in lines 3-5 to enable a GUI (mainly ParaView) to be forwarded to the host system. In line 6 the working directory is set to `home`. The home directory will be the same as on the host system since we first mapped the user to the container (unless a different directory was specified). The syntax to [bind volumes](https://docs.docker.com/storage/bind-mounts/) to the container is `--volume path_on_host:path_in_container:options`. The last argument is optional, for example, to make a file or directory read-only with the `ro` option. The first and most important directory-mount happens in line 7, where the home directory of the container is bound to the host’s home. If you use the `FOAM_RUN` directory to run test cases, then all the solver/utility output will be accessible from the home directory of the host. Likewise, data can be made accessible to the container by moving it to the home directory. After mounting home, there are three more single files and two folders that are bound to the container:
73 |
74 | - the `/etc/group` file contains a list of groups and their members
75 | - `/etc/passwd` contains further user attributes like id or password
76 | - `/etc/shadow` is a file with the same content as `/etc/passwd` but only root has read access (this is some safety feature of modern Linux systems)
77 | - the `sudoers.d` folder sometimes contains sudoers (users with root privileges) information that has to stay unchanged whenever the system is upgraded
78 | - the `.X11-unix` folder contains an endpoint (a Unix socket) for the Xserver to communicate with clients (applications like ParaView)
79 |
80 | ### *startOpenFOAM*
81 |
82 | The `installOpenFOAM` will be only run once to create the OpenFOAM container. `startOpenFOAM` is the script to execute whenever you need to login to the container. The first line in the scripts grants the container access to the Xserver of the host (to draw GUIs). After that follow two common Docker commands. `docker start CONTAINER_NAME` will start the container in case it was stopped, for example, after rebooting the host system. It is important to note, however, that the container must exist. Finally, an interactive bash shell is executed in the running container using the Docker `exec` command: `docker exec [options] CONTAINER_NAME command [args]`.
83 |
84 | ```bash
85 | xhost +local:of_v1812
86 | docker start of_v1812
87 | docker exec -it of_v1812 /bin/bash -rcfile /opt/OpenFOAM/setImage_v1812.sh
88 | ```
89 |
90 | The content of the `rcfile` loaded when you execute bash in the container is included in the code box below. First, all the OpenFOAM-specific variables and commands are sourced (made available). After that, some third-party binaries and libraries are added to `PATH` and `LD_LIBRARY_PATH` to have them system-wide available for execution or for compiling new applications (in the container). The last exported variable is again a dependency of `paraFoam`, which is built with [Qt](https://en.wikipedia.org/wiki/Qt_(software)).
91 |
92 | ```bash
93 | source /opt/OpenFOAM/OpenFOAM-v1812/etc/bashrc
94 | export LD_LIBRARY_PATH=$WM_THIRD_PARTY_DIR/platforms/linux64Gcc/ParaView-5.6.0/lib/mesa:$LD_LIBRARY_PATH
95 | export LD_LIBRARY_PATH=$WM_THIRD_PARTY_DIR/platforms/linux64Gcc/ParaView-5.6.0/lib/paraview-5.6/plugins:$LD_LIBRARY_PATH
96 | export LD_LIBRARY_PATH=$WM_THIRD_PARTY_DIR/platforms/linux64Gcc/qt-5.9.0/lib:$LD_LIBRARY_PATH
97 | export LD_LIBRARY_PATH=$WM_THIRD_PARTY_DIR/platforms/linux64/zlib-1.2.11/lib:$LD_LIBRARY_PATH
98 | export PATH=$WM_THIRD_PARTY_DIR/platforms/linux64Gcc/qt-5.9.0/bin:$PATH
99 | export QT_PLUGIN_PATH=$WM_THIRD_PARTY_DIR/platforms/linux64Gcc/qt-5.9.0/plugins
100 | ```
101 |
102 | ### Shipping OpenFOAM using Docker
103 |
104 | If you have your own modified version of OpenFOAM or some solver/utility you have written, here are approaches to preserve and ship your work using Docker:
105 |
106 | - Large modifications to OpenFOAM itself require to build a new OpenFOAM Docker image. For that you can base your image on a Linux distribution close to your development environment, e.g. Ubuntu or CentOS, you install all dependencies needed to compile OpenFOAM from scratch, you copy your modified version to the image, and compile it.
107 | - A new solver/utility can be built on top of the official OpenFOAM Docker image. You will copy the new sources to the image, compile them, and voilà.
108 | - If you only need a runnable binary of your app, it is possible to build an image from scratch and to copy only the binary and the dynamically-linked libraries to the image. That’s the most minimalistic way to ship an app.
109 |
110 | I will describe each of these approaches in more detail in follow-up articles.
111 |
112 | Cheers, Andre
--------------------------------------------------------------------------------
/_posts/2020-12-29-running-pytorch-models-in-openfoam.markdown:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 | title: "Running PyTorch models in OpenFOAM - basic setup and examples"
4 | date: 2020-12-29 12:00:00 +0100
5 | categories: OpenFOAM PyTorch Docker
6 | ---
7 |
8 | 1. Why PyTorch
9 | 2. Docker image with OpenFOAM and PyTorch
10 | 3. Local installation of LibTorch
11 | 4. Setting up Visual Studio Code
12 | 5. Compiling examples using wmake and CMake
13 | 6. Additional links to resources
14 | 7. Summary
15 |
16 | Incorporating data-driven workflows in computational fluid dynamics (CFD) is currently a hot topic, and it will undoubtedly gain even more traction over the months and years to come. The main idea is to use available datasets to make simulation-based workflows faster or more accurate. In the field of machine learning (ML) applied to CFD, deep learning (DL) algorithms allow us to tackle high-dimensional problems more effectively and promise significant progress in fields like turbulence modeling, flow control, or shape optimization. If you found your way to this article, chances are high that you don’t need to be convinced of the potential of ML/DL + CFD. So let’s skip the prose and get started with the nitty-gritty of this article: **how to set up PyTorch to run DL models in OpenFOAM apps**.
17 |
18 | ### Why PyTorch
19 |
20 | Why should you consider using PyTorch instead of Tensorflow/Keras? The short answer is because **PyTorch is easy and fast**. Both PyTorch and Tensorflow provide C++ and Python frontend APIs. However, at the time of writing, my arguments in favor of PyTorch when it comes to incorporating DL models in OpenFOAM are:
21 |
22 | - it is easy to set up the C++ libraries because there are pre-compiled packages ([libtorch](https://pytorch.org/cppdocs/installing.html))
23 | - it is easy to move data and models between the Python and C++ interface thanks to [TorchScript](https://pytorch.org/docs/stable/jit.html)
24 | - the C++ API is closely aligned with its Python counterpart; once you know how to do something in one frontend, you have at least an idea what to look for in the other frontend
25 |
26 | Of course, these arguments only capture my current impression, and DL frameworks are improving at lightning speed. If you had a different experience with Tensorflow or PyTorch, let me know! I would love to see workflows that make it as easy as possible for users and developers to switch between both frameworks according to their needs.
27 |
28 | ### Docker image with OpenFOAM and PyTorch
29 |
30 | If you have read some of my previous blog posts, you know that I am a fan of software containers as a means to make workflows reproducible and shareable. If you’re not much of a Docker user, you should still read this section because it also explains some details needed for local installations. So here is how to create a Docker image based on:
31 |
32 | - Ubuntu 20.04 LTS
33 | - OpenFOAM-v2006; pre-compiled Debian/Ubuntu package by ESI-OpenCFD
34 | - PyTorch 1.6; pre-compiled C++ API package (LibTorch)
35 |
36 | The Dockerfile and instructions on how to build and use an image can be found in [this repository](https://github.com/AndreWeiner/of_pytorch_docker) (I try to keep it up to date with the current versions of PyTorch and OpenFOAM). Here, I only want to focus on some of the details.
37 |
38 | ```docker
39 | # some commands to install required packages
40 | # ...
41 | # install OpenFOAM via Debian package
42 | ARG FOAM_PATH=/usr/lib/openfoam/openfoam2006
43 | RUN apt-get update && apt-get install --no-install-recommends -y \
44 | openfoam2006-default && \
45 | echo ". ${FOAM_PATH}/etc/bashrc" >> /etc/bash.bashrc && \
46 | sed -i "s/-std=c++11/-std=c++14/g" ${FOAM_PATH}/wmake/rules/General/Gcc/c++ && \
47 | sed -i "s/-Wold-style-cast/-Wno-old-style-cast/g" ${FOAM_PATH}/wmake/rules/General/Gcc/c++
48 |
49 |
50 | ## download and extract the PyTorch C++ libraries (libtorch)
51 | RUN wget -q -O libtorch.zip https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.6.0%2Bcpu.zip && \
52 | unzip libtorch.zip -d opt/ && \
53 | rm *.zip
54 |
55 | ## set libtorch enironment variable
56 | ENV TORCH_LIBRARIES /opt/libtorch
57 | ```
58 |
59 | The Dockerfile contains two modifications to the OpenFOAM compiler flags. First, the C++ standard is set from C++11 to C++14. This change is necessary because otherwise, PyTorch C++ code will not compile. It would be proper to re-compile OpenFOAM after changing the standard, however, there are only minor differences between both standards, and so far I haven’t had any trouble without recompiling the sources. Still, you can also change the standard and re-compile OpenFOAM with C++14 without any trouble (at least the core library; I didn’t test any third-party packages).
60 |
61 | The second modification switches off old-style-cast warnings being displayed when compiling PyTorch code. This change is only for convenience and helps to spot truly important warning and error messages more easily.
62 |
63 | “Installing” LibTorch is as easy as downloading and extracting a zip file. The shared object and header files are located under `/opt/libtorch` on the image. Moreover, it makes your life easier if you define an environment variable pointing to the LibTorch directory (e.g. to switch between Docker and local installation, to switch between different library versions, or to set up your code editor).
64 |
65 | ### Local installation of LibTorch
66 |
67 | The local installation of LibTorch is very similar to the Docker recipe. First, go to the [PyTorch website](https://pytorch.org/) and select the C++ API package as indicated in the picture below. **Important:** use the download link containing **-abi-shared-with-deps-** (cxx11 ABI). Then extract the archive to a location of your choice.
68 |
69 | |  |
70 | |:--:|
71 | |*Selection to download libtorch without GPU support.*|
72 |
73 | As in the Dockerfile, I recommend to set up an environment variable pointing to the LibTorch installation, e.g., add `export TORCH_LIBRARIES=/path/to/libtorch` to your `~/.bashrc` file.
74 |
75 | ### Setting up Visual Studio Code
76 |
77 | Powerful code editors can make your life much easier when learning to use a large library with little documentation. Over the last couple of months, I have started using [Visual Studio Code](https://code.visualstudio.com/) (vscode) for more and more of my projects. The main reason for me is the easy setup for a variety of different programming languages and tools (e.g., support for CMake and Docker is available). There are extensions for almost everything, and **they are easy to install and manage**. With very little effort, you can configure linting, code-completion, automatic code-formatting, or quickly jump to the definition of functions and classes. Setting up vscode for your C++/libtorch project requires only a couple of steps.
78 |
79 | #### Installing vscode
80 |
81 | On the [download page](https://code.visualstudio.com/Download) of vscode, you find plenty of options to get vscode. There are `.deb` and `.rpm` packages for the most popular Linux distributions, but also installers for Windows and MacOS. For C++ projects, you also want to install the official C/C++ extension. After staring vscode (simply type `code .` in the command line), open the extension manager by pressing *Crtl+Shift+X*, search for C/C++, and click on install.
82 |
83 | |  |
84 | |:--:|
85 | |*C/C++ extension for vscode.*|
86 |
87 | #### Configuring Intellisense
88 |
89 | If you open up one of the PyTorch C++ [examples](https://github.com/AndreWeiner/of_pytorch_docker/tree/master/test) in the repository with vscode, you will notice that Intellisense (the vscode engine doing all the magic in the background) is not able to find the torch.h header file. To fix this issue, some of the Intellisense settings have to be changed. In vscode, open the command palette by pressing *Ctrl+Shift+P*, search for C/C++, and select *C/C++: Edit Configurations (JSON)* as in the image below.
90 |
91 | |  |
92 | |:--:|
93 | |*Opening C/C++ configurations in vscode.*|
94 |
95 | Assuming that you have defined an environment variable called `TORCH_LIBRARIES` as described above, the following settings allow Intellisense to find the LibTorch header files. Tip: you may also want to add the path to the OpenFOAM sources when programming with components from both libraries. If the OpenFOAM environment variables were available in the shell in which you opened vscode, add `"${FOAM_SRC}/**"` to the `includePath` section of the Intellisense configuration file. Otherwise, it is also possible to add the full path to the OpenFOAM source folder.
96 |
97 | ```js
98 | {
99 | "configurations": [
100 | {
101 | "name": "Linux",
102 | "includePath": [
103 | "${workspaceFolder}/**",
104 | "${TORCH_LIBRARIES}/**",
105 | "${FOAM_SRC}/**"
106 | ],
107 | "defines": [],
108 | "compilerPath": "/usr/bin/g++",
109 | "cStandard": "c11",
110 | "cppStandard": "c++14",
111 | "intelliSenseMode": "gcc-x64"
112 | }
113 | ],
114 | "version": 4
115 | }
116 | ```
117 |
118 | ### Compiling examples using wmake and CMake
119 |
120 | By default, LibTorch applications are compiled using CMake, and dependencies are defined in `CMakeLists.txt` files. In contrast, OpenFOAM applications are typically compiled using `wmake`. Therefore, you are confronted with the following dilemma: you can either try to figure out how to compile OpenFOAM apps with CMake or you learn how to build LibTorch programs with `wmake`. I decided some time ago for the latter approach and haven’t changed my workflow since then. This [repository](https://github.com/AndreWeiner/of_pytorch_docker) currently contains two examples and instructions on how to run them:
121 |
122 | - `tensorCreation`: basics of PyTorch tensors and Autograd; compiled using wmake
123 | - `simpleMLP`: implementation of a simple neural network (multilayer perceptron - MLP); compiled using CMake
124 |
125 | Instead of checking all the CMake files contained in LibTorch, I found it much easier to simply look at the final compile command created by CMake and then to add PyTorch-related options to the `wmake` options file. The simpleMLP example in the [repository](https://github.com/AndreWeiner/of_pytorch_docker) mentioned above contains the implementation of a simple neural network in LibTorch and a CMake configuration file that enables [verbose output](https://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands) during the compilation. The output of make should look similar to the content of the code box below.
126 |
127 | ```bash
128 | # step 1: using cmake to create a makefile
129 | cmake ..
130 | # step 2: compiling the application using make
131 | make
132 | # verbose output
133 | ...
134 | [ 50%] Building CXX object CMakeFiles/simpleMLP.dir/simpleMLP.C.o
135 | /usr/bin/c++ -DAT_PARALLEL_OPENMP=1 -isystem /opt/libtorch/include -isystem /opt/libtorch/include/torch/csrc/api/include -D_GLIBCXX_USE_CXX11_ABI=1 -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wno-write-strings -Wno-unknown-pragmas -Wno-missing-braces -fopenmp -std=gnu++14 -o CMakeFiles/simpleMLP.dir/simpleMLP.C.o -c /home/andre/pyTorchCmake/simpleMLP.C
136 | [100%] Linking CXX executable simpleMLP
137 | /usr/bin/cmake -E cmake_link_script CMakeFiles/simpleMLP.dir/link.txt --verbose=1
138 | /usr/bin/c++ -rdynamic CMakeFiles/simpleMLP.dir/simpleMLP.C.o -o pyTorchOnes -Wl,-rpath,/opt/libtorch/lib /opt/libtorch/lib/libtorch.so /opt/libtorch/lib/libc10.so -Wl,--no-as-needed,/opt/libtorch/lib/libtorch_cpu.so -Wl,--as-needed /opt/libtorch/lib/libc10.so -lpthread -Wl,--no-as-needed,/opt/libtorch/lib/libtorch.so -Wl,--as-needed
139 | make[2]: Leaving directory '/home/andre/simpleMLP/build'
140 | [100%] Built target simpleMLP
141 | ...
142 | ```
143 |
144 | Now you can add the paths to header and shared object files to the `wmake` options and simplify the paths using the `TORCH_LIBRARIES` environment variable. The following box shows the options file of the [tensorCreation](https://github.com/AndreWeiner/of_pytorch_docker#tensorcreation) example, compiled with `wmake`. Note that the last three lines are optional.
145 |
146 | ```bash
147 | EXE_INC = \
148 | -I$(TORCH_LIBRARIES)/include \
149 | -I$(TORCH_LIBRARIES)/include/torch/csrc/api/include
150 |
151 | EXE_LIBS = \
152 | -Wl,-rpath,$(TORCH_LIBRARIES)/lib $(TORCH_LIBRARIES)/lib/libtorch.so $(TORCH_LIBRARIES)/lib/libc10.so \
153 | -Wl,--no-as-needed,$(TORCH_LIBRARIES)/lib/libtorch_cpu.so \
154 | -Wl,--as-needed $(TORCH_LIBRARIES)/lib/libc10.so \
155 | -Wl,--no-as-needed,$(TORCH_LIBRARIES)/lib/libtorch.so
156 | ```
157 |
158 | ### Additional links to resources
159 |
160 | - PyTorch [Python API](https://pytorch.org/docs/stable/torch.html) and [C++ API](https://pytorch.org/cppdocs/) documentation
161 | - [Autograd](https://pytorch.org/tutorials/advanced/cpp_autograd.html) in the PyTorch C++ frontend
162 | - plethora of [PyTorch C++ examples](https://github.com/prabhuomkar/pytorch-cpp) by Omkar Prabhu
163 | - creating a [Docker image](https://github.com/AndreWeiner/of_pytorch_docker) with OpenFOAM and PyTorch
164 | - examples for [ML applied to CFD](https://github.com/AndreWeiner/machine-learning-applied-to-cfd) problems
165 |
166 | ### Summary
167 |
168 | Getting started in a new, huge field like ML and DL can be hard for CFD people, but I strongly believe it is worth the trouble. I hope that this article saves you some time and maybe motivates you to give ML+CFD a try in case you’re undecided. Should you have follow-up questions or suggestions for future articles related to this topic, let me know! Finally, I would like to thank **Chiara Pesci** for her early feedback on this blog post and [Tomislav Maric](https://tmaric.gitlab.io/posts/) for our ongoing discussions about OpenFOAM and PyTorch, which have significantly influenced and improved the content of this post.
169 |
170 | Cheers, Andre
--------------------------------------------------------------------------------
/_posts/2020-12-29-minimizing-docker-images.markdown:
--------------------------------------------------------------------------------
1 | ---
2 | layout: post
3 | title: "Minimizing the size of Docker images using multi-stage builds"
4 | date: 2020-12-29 12:00:00 +0100
5 | categories: OpenFOAM Docker
6 | ---
7 |
8 | **Update (Mar 6 2022)**: there are now different official [OpenFOAM images](https://develop.openfoam.com/Development/openfoam/-/wikis/precompiled/docker) available, which are tailored to development or application. The images are also build in multiple stages and have increasing features/size with each stage.
9 |
10 | ---
11 |
12 | 1. A multi-stage build version of hello world
13 | 2. Single-stage build of dummyFoam
14 | 3. Analyzing library dependencies
15 | 4. Multi-stage build of dummyFoam
16 | 5. Summary
17 |
18 | When compiling a new high-level application or utility that will be shipped as a container, ideally, you want to start from a base image that comes as close as possible to the build-environment you need to compile your application. For example, if you’re going to create and build a custom OpenFOAM® solver, it makes perfect sense to start with the [OpenFOAM Docker image](https://develop.openfoam.com/Development/openfoam/-/wikis/precompiled/docker) as a base. You may install additional third-party libraries, copy your sources to the image, and compile. This workflow is really convenient since it allows, for example, to compile your application against a variety of OpenFOAM versions or flavors at lighting speed while keeping all dependencies nicely separated. One disadvantage, however, is the quickly increasing storage demand since every newly created image contains both the build-environment and the application. Moreover, sending the image over the network will take longer and cause significantly more traffic. Of course, there is an established way to overcome these disadvantages called **multi-stage builds**, and this article tells you how to apply it to OpenFOAM applications.
19 |
20 | The idea behind multi-stage builds is simple: first, you prepare one or more build-environments with all dependencies (typically called builder(s)), then you build your application, and finally, you extract only what is really needed from the builder and discard the rest. The simplest multi-stage build is a two-stage build as depicted in the image below. However, you could also merge applications from multiple different builders into one final image, or you could combine different builders to create a new builder. The first scenario may be relevant to merge several binaries needed to run a simulation into a single image (e. g. third-party meshing tool + custom OpenFOAM solver + third-party post-processing tool). The latter scenario may occur if OpenFOAM is combined with another custom library to build the final app. In the early days of Docker, users would define individual Dockerfiles for each step in the build process, and write wrapper scripts to execute them in order. Since the introduction of [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/), Docker handles the execution and copy processes for the user, and the entire build can be defined in a single Dockerfile.
21 |
22 | |  |
23 | |:--:|
24 | |*Multi-stage build with two stages: in the first stage, the environment is set up and the application is compiled; in the second stage, the compiled application and its dependencies are isolated.*|
25 |
26 | ### A multi-stage build version of *hello world*
27 |
28 | Let’s start with the probably simplest app we can build in a multi-stage process as outlined before: `hello_world.cpp`. If you want to follow along:
29 |
30 | 1. `mkdir hello_world`
31 | 2. `cd hello_world`
32 | 3. `touch hello_world.cpp`
33 | 4. copy & paste the source code from the box below into `hello_world.cpp`
34 |
35 | ```cpp
36 | #include