Matlab Codes
Matlab Codes
https://quickersim.com/category/tutorials-en/
Introduction
In this tutorial we will show the code of a complete Navier-Stokes solver, which we always advise to use as a starting point when doing any flow
simulation. You can later modify this code according to your needs. This QuickerSim CFD Toolbox for MATLAB® solver is incompressible,
laminar, fluid flow governed by the Navier-Stokes equations, i.e.:
subject to
supposed that you will use assembleStokeMatrix2D function in place of assembleNavierStokesMatrix2D function.You have to apply
proper boundary conditions to above systems that will guarantee a unique solution to the stationary Stokes or Navier-Stokes
problem.You can use an ‘inlet’ boundary condition for which you need to specify two velocity components (not necessarily perpendicular
to the inlet), ‘wall’boundary condition, which sets both velocity components to zero or ‘slipAlongY’ boundary conditions which imply zero
shear stress at the given boundary. If you do not apply explicitly any boundary condition, the toolbox will assume this boundary to be
open boundary (i.e. outflow) where the following boundary condition will be applied:
1 clc;
2 clear;
Import mesh generated by Gmsh; p, e, t arrays store the computational mesh. Type: help importMeshGmsh in Command Window to get
more details.
3 [p,e,t] = importMeshGmsh('cylinder.msh');
4
5 % You can now display mesh to check, if everything is alright.
6 displayMesh2D(p,t);
For a fluid flow simulation we always need to convert mesh to second order. nVnodes stands for number of velocity nodes in the mesh,
nPnodes number of pressure nodes and indices is a Matlab structure which will help us to refer to solution fields (x-velocity, y-velocity
and pressure), since all of them will be stored in one solution vector u. Indices will enable us easy access.
7 [p,e,t,nVnodes,nPnodes,indices] = convertMeshToSecondOrder(p,e,t);
We define kinematic viscosity of the fluid. We never specify dynamic viscosity. The Toolbox solves only incompressible flows, assuming
constant density, which is nowhere introduced to equations. In other words density always equals 1. If you simulate e.g. water flow with
real density of 1000 kg/m3, your computed pressure field will be actually real pressure field divided by fluid density. So, if you want to
obtain real static pressure in Pascals, you would need to multiply computed values of pressure by 1000.
Now, define some initial approximation to the solution and define convergence criteria, i.e. maximum number of iterations to solve for the
nonlinearity in convection term and maximum residual value. These are absolute residuals. We set initial velocity field to [1 0] everywhere
and initial pressure field to 0 everywhere. convergence is an additional array which wonderfully helps plotting convergence plots and
checking for breaking the iteration loop.
We require absolute (not a scaled) residuals to drop to 1e-3, but in any case we don’t allow for more than 25 iterations.
10 maxres = 1e-3;
maxiter = 25;
11
% Start iterations
12
13 for iter = 1:maxiter
% Assemble Navier-Stokes matrix and right-hand side vector
14
% u(indices.indu) accesses x-velocities in the solution vector
15 % u(indices.indv) accesses y-velocities in the solution vector
16 % 'nosupg' flag tells that we do not use any kind of SUPG stabilization
17 [NS, F] = assembleNavierStokesMatrix2D(p,e,t,nu,u(indices.indu),u(indices.indv),'nosupg');
18
19 % Apply boundary conditions
% Inlet at wall 10 with velocity set to [1 0]
20 [NS, F] = imposeCfdBoundaryCondition2D(p,e,t,NS,F,10,'inlet', [1 0]);
21
22 % Free slip along x axis on channel walls (wall id = 12)
23 [NS, F] = imposeCfdBoundaryCondition2D(p,e,t,NS,F,12,'slipAlongX');
24
25 % No-slip wall boundary condition at cylinder walls (id = 13)
26 [NS, F] = imposeCfdBoundaryCondition2D(p,e,t,NS,F,13,'wall');
27
% Do nothig for outflow at boundary with id = 11
28
29 % Compute and plot actual residuals
30 [stop, convergence] = computeResiduals(NS,F,u,size(p),convergence,maxres);
31
32 % Plot residuals to figure(2)
33 plotResiduals(convergence,2);
34
% Break if solution converged
35 if(stop)
36 break;
37 end
38
39 % Solve equations
40 u = NS\F;
41
end
42
43
44
45
46
Problem definition
In this tutorial we show how to implement the SIMPLE algorithm for pressure-velocity coupling problem, which occurs in the solution of
incompressible flows. The original SIMPLE algorithm (Semi Implicit Method for Pressure Linked Equations) proposed in [1] has been mainly
intended for finite volume discretization technique. However,