Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
78 views

Matlab Codes

This document contains tutorials for solving fluid flow problems using MATLAB codes. The first tutorial shows code for solving laminar flow past a cylinder using the Navier-Stokes equations. The code includes importing a mesh, initializing solutions, assembling matrices, applying boundary conditions, and iterating to convergence. The second tutorial discusses implementing the SIMPLE algorithm for pressure-velocity coupling.

Uploaded by

wisdom ukueje
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Matlab Codes

This document contains tutorials for solving fluid flow problems using MATLAB codes. The first tutorial shows code for solving laminar flow past a cylinder using the Navier-Stokes equations. The code includes importing a mesh, initializing solutions, assembling matrices, applying boundary conditions, and iterating to convergence. The second tutorial discusses implementing the SIMPLE algorithm for pressure-velocity coupling.

Uploaded by

wisdom ukueje
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MATLAB CODES

https://quickersim.com/category/tutorials-en/

ONLINE EDTOTOR – GRAMMER CHECKER

Tutorial 1 – Laminar Flow Past Cylinder


Posted on July 13, 2017 - Tutorials

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

  

Alternatively you can solve a Stokes problem defined by


v

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:

Geometry & Mesh Generation


Lite version of our toolbox imports meshes prepared with the open source Gmsh mesh generator. Go to [Gmsh download] and [geometry
creation and meshing tutorial] .After geometry and mesh have been created, you should end up with the following geometry (note the ids
of physical lines) and the mesh shown below. You can also refer to files cylinderGeometry.geo and cylinder.msh which are attached to
this tutorial.
Code
Clear MATLAB Command Window and Workspace

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.

8 nu = 0.01; % For our geometry and velocity this implies Re = 100

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.

9 [u, convergence] = initSolution(p,t,[1 0],0);

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

Displaying and Exporting Solution Fields


Generate pressure data in all mesh nodes (of the second order mesh – not only in nodes of the first order mesh, where it is actually
solved).
47 pressure = generatePressureData(u,p,t);
48  
49 % Display x-velocity to figure(3)
50 figure(3)
displaySolution2D(p,t,u(indices.indu),'x-velocity field [m/s]')
51

51 % Display pressure field to figure(4)


figure(4)
52
displaySolution2D(p,t,u(indices.indp),'pressure divided by density')
53

Tutorial 2 – Numerics SIMPLE Scheme


Posted on July 13, 2017 - Tutorials

This tutorial is intended for the full version of the toolbox.

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,

You might also like