DEV Community

Cover image for CFD Simulations in MATLAB - Flow Around a Cylinder Tutorial
Precise Simulation
Precise Simulation

Posted on • Edited on • Originally published at featool.com

CFD Simulations in MATLAB - Flow Around a Cylinder Tutorial

This tutorial and model example shows how to easily set up and solve computational fluid dynamics (CFD) simulations in MATLAB with the FEATool Multiphysics and CFD toolbox.

MATLAB GUI for CFD simulations

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 SU2 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 CFD 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 )
Enter fullscreen mode Exit fullscreen mode

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.

CFD Solver Comparison: FEATool MATLAB Solver vs OpenFOAM vs FEniCS

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.


Please visit the homepage https://www.featool.com for toolbox download and more information about CFD simulations for MATLAB.

Top comments (0)