MATLAB CFD simulations have for a long time not been easy to do. However, with the FEATool Multiphysics toolbox it is now possible to perform advanced CFD simulations from the convenience of an easy-to-use MATLAB GUI, or alternatively as m-file model scripts with the MATLAB programming API.
In contrast to other simulation software, FEATool supports multiple CFD and FEA solvers, such as OpenFOAM, SU2, and FEniCS. This uniquely allows users to only set up and define models once, while allowing to run parametric validation and verification (V&V) studies for the same test cases for multiple solvers. This automation can save significant amount of time, as users do not have to learn intricacies of different solvers, and can script automatic studies and generate simulation reports efficiently.
MATLAB together with FEATool Multiphysics now provides a very flexible CFD software platform for performing fluid dynamics studies. Its user-friendly interface, powerful scripting capabilities, and extensive toolboxes make it a valuable tool for researchers, engineers, and scientists working in various fields involving fluid dynamics, heat, and mass transfer.
Easy-to-use MATLAB CFD Toolbox.
Quick Example: MATLAB CFD Simulation
The following short tutorial and model example shows how quick and easy it can be to set up and solve computational fluid dynamics (CFD) simulations in MATLAB with FEATool Multiphysics.
Test Case Definition
The example used in this case involves stationary and laminar flow around a cylindrical obstacle in a channel with Reynolds number (Re) of 20. The computed pressure drop, drag, and lift coefficients are compared with established benchmark references to ensure accuracy of the simulation results [2,3].
The test configuration considers a solid cylinder centered at (0.2, 0.2) with diameter d = 0.1 in a l = 2.2 by h = 0.41 rectangular channel. The fluid is assumed to have a constant density equal to 1 and viscosity 0.001. A fully developed parabolic velocity profile is prescribed at the inlet, u(0,y) = (4umaxy(h-y)/h2, 0), with a maximum velocity of umax = 0.3. This results in the mean velocity umean = 2 / 3umax = 0.2 and laminar flow with Reynolds number Re = ρumeand/µ = 20. As the fluid is considered incompressible the problem is governed by the Navier-Stokes equations.
MATLAB GUI for CFD Simulations
CFD models can be quickly set up and solved in the FEATool Multiphysics graphical user interface (GUI), with either the built-in solver, or the “one-click” integrations for the SU2 Code and OpenFOAM CFD solver GUI.
Step-by-step instructions are available how to set up and run this test case in the tutorial and examples section of the toolbox User’s Guide [1]. Alternatively, a YouTube video tutorial for this model is linked here below (note that you can also run the GUI model tutorial directly in the toolbox interface).
CFD Simulations with MATLAB programming API
In addition to the MATLAB GUI, the toolbox also includes a MATLAB m-file programming API for FEA and CFD simulations. Models created in the GUI can easily and conveniently be exported as m-file MATLAB simulation scripts.
The benchmark test case discussed here for example will look like the following compact MATLAB code
% MATLAB Script for a CFD simulation of flow around a cylinder
% Geometry definition
fea.sdim = { 'x', 'y' };
fea.geom.objects = { gobj_rectangle( 0, 2.2, 0, 0.41, 'R1' ), ...
gobj_circle( [0.2, 0.2], 0.05, 'C1') };
fea.geom = geom_apply_formula( fea.geom, 'R1-C1' );
% Mesh generation
fea.grid = gridgen( fea, 'hmax', 0.02 );
% Constants and expressions
fea.expr = { 'h', 0.41;
'diam', 0.1;
'rho', 1;
'miu', 0.001;
'umax', 0.3;
'umean', '2/3*umax';
'fx', 'nx*p+miu*(-2*nx*ux-ny*(uy+vx))';
'cd', '2*fx/(rho*umean^2*diam)' };
% Equation settings and material parameters
fea = addphys( fea, @navierstokes );
fea.phys.ns.eqn.coef{1,end} = { 'rho' };
fea.phys.ns.eqn.coef{2,end} = { 'miu' };
% Boundary conditions
fea.phys.ns.bdr.sel = [ 1, 4, 1, 2, 1, 1, 1, 1 ];
fea.phys.ns.bdr.coef{2,7}{1,4} = '4*umax*y*(h-y)/h^2';
% Call to MATLAB CFD solver
fea = parsephys( fea );
fea = parseprob( fea );
fea.sol.u = solvestat( fea );
% Postprocessing & Visualization
postplot( fea, 'surfexpr', 'sqrt(u^2+v^2)', 'isoexpr', 'p', 'arrowexpr', { 'u', 'v' } );
% Boundary integration of drag coefficient on cylinder
c_d = intbdr( 'cd', fea, 5:8 )
The MATLAB scripting API is useful to set up and run advanced simulations, such as parametric CFD studies, and naturally integrates with other MATLAB toolboxes such as for example Simulink and optimization toolboxes.
CFD Solver Comparison
A small benchmark study has also been performed to which compare the OpenFOAM, FEniCS and FEATool solvers for this specific flow test case, and illustrates how one can perform CFD validation & verification studies (V&V) in MATLAB.
References
[1] FEATool Multiphysics User’s Guide: Flow Around a Cylinder CFD Benchmark Tutorial, Precise Simulation Ltd, 2013-2024.
[2] John V, Matthies G. Higher-order finite element discretizations in a benchmark problem for incompressible flows. International Journal for Numerical Methods in Fluids 2001.
[3] Nabh G. On higher order methods for the stationary incompressible Navier-Stokes equations. PhD Thesis, Universitaet Heidelberg, 1998.