Python has emerged as a powerful tool for Computational Fluid Dynamics (CFD) simulations due to its open-source nature, extensive libraries, and ease of use. This blog post will guide you through the essential steps to set up your Python environment for CFD, including the installation of key libraries and the configuration of necessary tools.
Python has rapidly gained traction as a preferred language for Computational Fluid Dynamists. Its versatility, coupled with a rich ecosystem of open-source libraries, makes it an ideal choice for researchers and engineers alike. This guide will walk you through the essential steps to establish a robust Python environment tailored for CFD. Specifically, I will list out the various python modules and libraries essential for CFD data analysis and post-processing.
Let’s begin!!
Python distribution is a bundle that includes the Python interpreter and a collection of pre-installed libraries and tools. Choosing the right Python distribution is essential for efficiently setting up a Python environment, particularly in specialized fields like Computational Fluid Dynamics (CFD), due to its ability to ensure compatibility with essential libraries, avoid dependency conflicts, and offer pre-built, optimized packages that save time and enhance performance. Distributions like Anaconda provide robust environment management tools, allowing for isolated, reproducible environments tailored to specific projects. Additionally, they facilitate cross-platform compatibility, simplify installation and maintenance, and integrate smoothly with other tools in the CFD workflow, all while benefiting from strong community support and extensive documentation.
Here are the Recommended Distributions for python:
Recommendations:
In short, Anaconda and Miniconda are preferred choices for most users due to their ease of use and pre-installed packages, while CPython is suitable for those who want a minimalistic setup or prefer to have full control over their environment. As a researcher, I usually work with CPython mainly due to the benifits of manual installation of all necessary libraries. This way, one can ensure total control over the python environment.
PS: Any of these python distributions should work on Windows or Mac!!
Go to python.org. On the homepage, you’ll see a download button that automatically selects the latest version of Python for your operating system (Windows, macOS, or Linux). While the latest version is recommended, you can also select older versions if needed by clicking on “View the full list of downloads.” Once you have selected a specific version of python, click the download button to start downloading the installer file.
Once the download is complete, find the installer file in your Downloads folder and simply run it!!! Don’t forget to select the Install launcher for all users checkbox, which enables all users of the computer to access the Python launcher application, and Select the Add python.exe to PATH checkbox, which enables users to launch Python from the command line.
If you’re just getting started with Python and you want to install it with default features as described in the dialog, then simply click Install Now.
After the installation is complete, a Setup was successful message displays. I recommend selecting the Disable path length limit option and thereby removing the path length limit. It will potentially save you time in future on debugging an avoidable issue.
Reboot your system. This ensures that the Path environments are set for your system.
(Optional, ONLY FOR WINDOWS) Adding Python to Environment Variables If you want to access Python through the command line but you didn’t add Python to your environment variables during installation, then you can still do it manually. Skip this step if you selected Add Python to environment variables during installation.
First, locate the Python installation directory on your system. Most often, python is installed in C:\Program Files\Python310 for example. Note that the folder name will be different if you installed a different version, but will still start with Python.
Go to Start menu and enter advanced system settings in the search bar. Click View advanced system settings. In the System Properties dialog, click the Advanced tab and then click Environment Variables. Select Path from the list of System Variables and click Edit. Click New and enter the Python directory path, then click OK until all the dialogs are closed.
To verigy the python installation, simply open the PowerShell in windows or your terminal on a mac and type in python -V. As an example, the output should be Python 3.11.6
First of all, upgrade the pip. Pip is the python package manager, mainly used to install packages from the Python Package Index (PyPI). To upgrade pip, simply type in python -m pip install --upgrade pip.
Next, lets install some some essential packages like NumPy, SciPy, Matplotlib, pandas, and Jupyter. For this, I utilize a file named requirements.txt. This is a file that contains a list of packages or libraries needed to work on a project (if you are working with multiple python enviroments) that can all be installed with the file. It provides a consistent environment and makes collaboration easier.
You can use your terminal or command prompt to create your requirements file. Simply generate an empty text file named requirements.txt. Then open it up and type in the names of the necessary packages as such:
matplotlib
numpy
pandas
scipy
jupyter
fluidfoam
imageio
pyvista
Save this file on your working directory and simply Install the dependencies noted in this file using pip install -r requirements.txt or python -m pip install -r requirements.txt. Since these are lare packages, wait for the installation to finish!!
Your Python environment is set!!!
fft and welch.Here are some more articles you might like to read next: