Setting Up A CFD Problem

Setting up a computational fluid dynamic (CFD) problem can be daunting with all the different parameters and solvers available. These settings can differ greatly between CFD programs and may differ in the units used to define the problems. Currently this blog has investigated Navier-Stokes problems with Understanding The Navier-Stokes Solvers and Phase-Field problems with Microfluidic Droplet Formation using BERNAISE, Binary ElectRohydrodyNAmIc SolvEr, which runs on FEniCS, Finite Element Computational Software.

Here we are going to use a CFD template and add the values of the mesh and parameters of the CFD problem using BERNAISE. This process is without a graphical user interface unlike other popular CFD simulation programs. However, the layout in BERNAISE is straight forward with the definitions that novice users can implement problems.

Creating a Microfluidic Computer-Aided Design (CAD)

We need a model to simulate our CFD where computer-aided design (CAD) allows for particular control of structures and features in the flow path. For this tutorial, we are going to use a curved channel for simplicity, created in a CAD program as well as labelling the boundaries later. The CAD program I use is AutoDesk Fusion as it is free to use for personal and hobby use. Check it out here!

To create a curved microfluidic channel…

  1. We need to ensure we set the units for the document up correctly so that when we draw our model, it is represented correctly. I personally set the document settings to milli-metres.
  2. Once the document is set up correctly, we need to create a sketch. For simplicity, let’s create a 2D sketch. Select the circle tool and draw a circle with a radius of 5 mm and a smaller circle with a 4.5 mm radius, 0.5 mm smaller. Then draw straight lines on the axis and crop one side of the circle. That’s all that is needed. The straight line, channel width, should be 500 μm assuming both circles are centred.
  3. Now we need to extrude the 2D sketch into 3D. Exit the 2D sketch and select the extrude tool. Select your sketch and extrude, for example 0.2 mm which is 200 μm.
  4. Export the CAD as a “.STEP” file

Creating a Mesh

Now we have our CAD, we need to turn it into a mesh we can use in BERNAISE. To create a mesh, we need meshing software such as GMSH. I personally use GMSH as we can create 2D and 3D meshes as well as having control over the element arrangement. To make a mesh in GMSH…

  1. Open GMSH and load the “.STEP” file we created in AutoDesk Fusion.
  2. Within GMSH, open the Tools > Option and select “Mesh” from the left hand side. Select the “General” tab and min/max element size can be set. The element size is relative to the mesh size. For example, if the mesh is in millimetres, 0.01 will be 0.01 millimetres.
  3. Next we must label each “Surface” and the “Volume” using the “Physical Groups”. If we do not label the surfaces and volume, we cannot set inlet, outlet and wall boundary conditions later.
  4. The mesh can be created by selecting 3D from the left hand menu, for example Modules > Mesh > 3D. If we wanted 2D, we could select 2D from the left hand menu. Once generated, the mesh can be exported by File > Export menu and choosing “Mesh – Gmsh MSH – (*.msh)” format from the file type drop down menu and saved with an appropriate name. The MSH file format should be “Version 4 ASCII” but “Version 2 ASCII” should work due to the wide support within Meshio.

Meshio is software which converts the GMSH “.msh” file to “.XDMF” which has better file size in comparison to other mesh formats as well as working well within BERNAISE.

Converting to a FEniCS Friendly Mesh

We need to convert the GMSH MSH file to an XDMF, the preferred format for FEniCS. The XDMF format avoids some incompatibility issues, often more memory efficient as well as splitting the mesh into the tetrahedral mesh and physical boundary mesh.

Fortunately, the conversion is very simple using Meshio. I have written a basic script, available here, which converts the GMSH mesh file to 2 XDMF files, tetrahedral and physical boundary mesh. The script must be edited with the correct GMSH file path and the corresponding export mesh names with XDMF filetype.

msh ="CurvedChannel.msh") # Read in GMSH file
meshio.write("mesh_curvedMicrochannel.xdmf", ... # Write tetrahedral mesh
meshio.write("mf_curvedMicrochannel.xdmf", ... # Write physical boundary mesh

Setting The Fluid Parameters

A simulations comparability to the real world physics is dependent on the correct parameters being used. If poor parameters are used, the model will be unrealistic. The parameters are set within the BERNAISE problem file. Template files for the previous microfluidic models are available on GitHub problems folder here. Remember to download the corresponding mesh for the problem too!

In this example we are looking at the

First, we need to confirm the “.XDMF” mesh and corresponding boundary domain file, also “.XDMF”, look correct and extract the boundary labels. This should correspond to the GMSH values but they may differ. Once we have the boundary labels, we can fill out the boundary_Facet. Make sure to set import_mesh as true as well as set the appropriate scale_factor and mesh/subdomain files.

boundaries_Facet = {'inlet': 2,'outletL': 7, 'wall': [3,4,5,6]},import_mesh = True, # If importing XDMF mesh files
scale_factor = scaling_factor,  # Change mesh dimension (Use if mesh not in metres)
mesh_file = "meshes/mesh_curvedMicrochannel.xdmf", # Mesh filepath
subdomains_file = "meshes/mf_curvedMicrochannel.xdmf", # Subdomains filepath

Now we have We have to set enable_NS = True and make sure we use the correct Navier-Stokes solver and set the results folder to store our output.

enable_NS = True,
solver="basic_IPCS_Adj", # Type of problem sovler
folder="results_curvedChannel", # Save folder

One of the most important parts of the simulation is the time-step. I have mentioned before in a previous blog about the importance of a time-step and how to automate it somewhat using Courant-Friedrichs-Lewy (CFL) condition, read more here! In this implementation, we set up parameters to use CFL condition using a max CFL 0.5 parameter. Here it doesn’t matter what the dt is initially set as, as we will replace it using the dt = get_dt_CFL(mesh1, parameters["inlet_velocity"]) command.

dt=4e-6, # s Time steps
t_0=0., # s Start time
T=300, # s Total time or number of steps if CFL

Regarding the flow parameters, we need to set the initial flow conditions which in this example are 0.1 m/s as well as the direction of flow which is determined by the inlet orientation. In this example, the flow is entering the inlet in the Y direction, where it should be initially 0 m/s in the XZ direction. The reason we need the direction of flow is to apply a parabolic inflow profile of the Y values to the XZ plane co-ordinates in this example. The parabolic inflow profile is defined as Velocity at PosX = 4 * U * x * (D – x) / (D2) where D is the diameter of the channel, U is the inlet velocity and x is the co-ordinate position. As this is a 3D model, the expression for parabolic inflow has been adapted to include the Z profile too. Finally, XYZ is set dependent on the inflow velocity direction. In this example, XYZ is set to 1 which is the XZ plane meaning inlet velocity is in the Y direction, see images below.

inlet_velocity = 0.1, # m/s (Negative due to -x inflow direction)
dim = 3, # Dimensions
XYZ = 1, # If XY(2), XZ(1) or YZ(0) direction of flow

This is typically all that is required for the basic curved microchannel simulation.

Running the CFD problem

To run the CFD problem, we must have FEniCS and BERNAISE installed on the PC. These can be installed from the FEniCS installation guide and BERNAISE installation guide. This can be done on Windows, Linux and Mac but for simplicity (and I like a little variety and challenge), I run the simulations within Linux.

Running in Windows is relatively simple now using the Ubuntu App available within the Microsoft Store. The following installation and commands should be similar between Windows, Linux and Mac with minor variations.

After we have installed FEniCS & BERNAISE, we can execute code using this command here:

python problem=flow_curvedChannel

Running it with the command above can be slow as it is using a single process. Using MPI (Message Passing Interface) within Python, we can run the command across multiple processes which are often more efficient and faster but your mileage may vary:

mpirun -np 4 python problem=flow_spiral2D

For more commands, refer to the BERNAISE wiki here!

Reviewing the Result

To review the result, we need an application which can open the “.XDMF” file formats. This can be done open source using ParaView, a powerful and open source visualisation tool.

Paraview is available on Windows, Mac and Linux where results can be easily compared by creating macros to load and apply filters to data. This saves time and effort in finding the same positions or settings to visualise results. This is especially important when comparing multiple CFD results of differing inflow velocities.

Here we are looking at a simple curved microchannel and we can see how the flow still follows a somewhat parabolic profile but is shifted to the outer edge of the channel. Additionally, we can setup a macro to select the cross-section in the middle of the channel to visualise Dean drag forces like we did in part 1 of Inertia Microfluidics Separation here!


This basic tutorial using BERNAISE should provide the basics to get started implementing a computational fluid dynamic (CFD) problem. Here we have covered creating a computer aided design (CAD), converting the CAD to a FEniCS mesh, setting up and performing a simple CFD problem as well as visualising the CFD result.

Enjoyed this blog post? Like, Subscribe and leave a comment on YouTube if you want to see anything more!

Funded by EPRAT – Keep your face toward the PRAT, shadows will wall behind you
YouTube Subscribe Button

Leave Your Reply

Your email address will not be published. Required fields are marked *