UNIVERSITY OF SOUTHERN CALIFORNIA

CFD Modeling of fluid flow in a rectangular channel Via Adams-Bashforth Time Advancing Sina Golshany 12/10/2010

This report presents the results and explore the methods employed to model the flow in a low Re # rectangular channel.

AME 535a - Final Project Due December 9, 2010, by 4:00 p.m. Consider a steady, viscous entrance ﬂow into a two-dimensional plane channel as shown below. The channel walls are separated by a distance 2h in the y direction and at the entrance x = 0 the velocity is assumed to have a uniform value u0 and is directed in the x direction. The ﬂow is driven by a constant pressure gradient in the streamwise direction x. Between the inﬂow and the outﬂow at x = xmax the velocity changes from the uniform value to a form appropriate for a fully developed 2-D parabolic Poiseuille ﬂow for which ∂u = 0. ∂x u = 0, v = 0

∂ u =0 ∂x v=0

u = u0 v=0

u = 0, v = 0

Figure 1: Entrance ﬂow. 1. Using a half channel width h as a length scale, u0 as a velocity scale, and h/u0 as a time scale show that the nondimensional, 2-D Navier-Stokes equations are: ∂u ∂u ∂u ∂P 1 = −u −v − + ∂t ∂x ∂y ∂x Re ∂v ∂v ∂P 1 ∂v = −u −v − + ∂t ∂x ∂y ∂y Re ∂u ∂v + = 0, ∂x ∂y 1

(

(

)

∂ 2u ∂ 2u + , ∂x2 ∂y 2 )

∂ 2v ∂ 2v + , ∂x2 ∂y 2

where u = u(x, y) and v = v(x, y) are the velocity components, P (x, y) is the pressure, and Re = u0 h/ν is the Reynolds number (ν is the kinematic viscosity). 2. The pressure is decomposed into a mean pressure P¯ (x) driving the ﬂow and a perturbation p. Show that at the outﬂow the velocity ﬁeld is 1 u(y) = |C|Re(1 − y 2 ), 2 ¯

where |C| is the constant mean pressure gradient, i.e ddxP = −|C|. Note that the system of coordinates is chosen such that −1 < y < +1. 3. Use the conservation of mass to show that the constant |C| = 3/Re and that the u-momentum equation can be re-written as ∂u ∂u ∂u 3 ∂p 1 = −u −v + − + ∂t ∂x ∂y Re ∂x Re

(

)

∂ 2u ∂ 2u + . ∂x2 ∂y 2

4. Write a numerical code to determine the ﬂow in the channel assuming no-slip boundary conditions on the channel walls: u(x, −1) = 0, u(x, +1) = 0; a uniform streamwise velocity at the inﬂow: u(0, y) = 1, v(0, y) = 0; a Neumann boundary condition for the streamwise velocity at the outﬂow ∂u(x,y) |x=xmax = 0 and v(xmax , y) = 0. ∂x Method of solution: Use a pseudo-transient, fractional step method (time-splitting method) consisting of the following steps. 1. Nonlinear step. ∂u = Fu , ∂t ∂v = Fv , ∂t where (Fu , Fv ) are the nonlinear terms 2

Fu = −u

∂u ∂u 3 −v + , ∂x ∂y Re

∂v ∂v −v . ∂x ∂y Time advancement using Adams-Bashforth method, i.e. Fv = −u

u∗ − u n 3 1 = Fun − Fun−1 , ∆t 2 2 1 v∗ − vn 3 = Fvn − Fvn−1 . ∆t 2 2 2. Pressure step. ∂u ∂p =− , ∂t ∂x ∂v ∂p =− , ∂t ∂y ∂u ∂v + = 0. ∂x ∂y Time discretization u∗∗ − u∗ = −∇p. ∆t Taking divergence of the above equation and assuming that u∗∗ is incompressible we get a Poisson equation for p 1 ∂2p ∂ 2p + 2 = 2 ∂x ∂y ∆t

(

∂u∗ ∂v ∗ + ∂x ∂y

)

The above equation is ﬁrst solved with the Neumann boundary con∂p ∂p ditions on the walls, i.e. ∂x = 0 at x = 0, xmax and ∂y = 0 at y = +1, −1. After a solution for p is obtained, ∇p can be computed and the velocity u∗∗ is found from the previous equation. 3

3. Two viscous steps. The ﬁrst viscous step ∂u 1 ∂2u = ∂t Re ∂x2 ∂v 1 ∂ 2v . = ∂t Re ∂x2 Discretization using Crank-Nicolson scheme, i.e. for u u∗∗∗ − u∗∗ 1 = ∆t Re

(

)

1 ∂ 2 u∗∗∗ 1 ∂ 2 u∗∗ + , 2 ∂x2 2 ∂x2

and a similar equation for v ∗∗∗ . The second viscous step similarly solves equations ∂u 1 ∂2u = ∂t Re ∂y 2 ∂v 1 ∂ 2v = , ∂t Re ∂y 2 using the Crank-Nicolson method. For u we get un+1 − u∗∗∗ 1 = ∆t Re

(

and a similar equation for v n+1 .

4

)

1 ∂ 2 un+1 1 ∂ 2 u∗∗∗ + , 2 ∂y 2 2 ∂y 2

AME 535a Final Project - Hints • For spatial derivatives use the second order central diﬀerence formulas. • Velocity equations are solved on the internal points j = 2, 3, ..., N X − 1 and k = 2, 3, ..., N Y − 1 and the outﬂow boundary j = N X. • Maximum time step ∆t is estimated from the CFL condition using the inﬂow velocity. • For pressure formulate and solve discretized equation on all mesh points j = 1, 2, ..., N X and k = 1, 2, ..., N Y assuming that the r.h.s. of the equation is zero at all boundaries. To get spatial derivatives at the boundary points, when needed, ﬁctitious points (usually denoted with a subscript ‘0’ in the class notes) outside the computational domain must be introduced. • In the pressure step the discretized Poisson equation is not diagonally dominant and the convergence of iterative procedures (e.g., used in programs FIVOL and DUCT) may be slow. The equation is most safely solved using direct solvers for systems of linear equations (e.g., FACT/SOLVE subroutines). In formulating the matrix equation to be solved, using one-sided formulas for divergence inside the domain often leads to better results (

Dj,k =

∂u∗ ∂v ∗ + ∂x ∂y

)

= j,k

∗ ∗ u∗j,k − u∗j−1,k vj,k − vj,k−1 + . ∆x ∆y

• Values of pressure at corner points may be approximated in terms of values at the neighboring points, e.g. 1 p1,1 = (p1,2 + p2,1 ) 2 • It is preferable to construct the matrix in the direct solver for the pressure equation such that the solution is obtained as a sequence of values in columns along y, rather than in rows along x, i.e. (p1,1 , p1,2 , p1,3 , ..., pj,k , pj,k+1 , pj,k+2 , ..., pnx,ny−1 , pnx,ny ) 5

• The viscous step solution is similar to that used in program TWDIF. • Design program as a sequence of calls to three subroutines: CONVEC (for advancing equations in the nonlinear step), PRESS (for the pressure step), VISC (for the viscous step). You may use parts of the programs from the book, if convenient, or write the program from scratch in a computer language of your choice. • Test each subroutine separately. • In order to test subroutines a good approach is to consider artiﬁcial exact solutions. For instance, for the pressure equation the exact solution for a square domain 0 < x < 1, 0 < y < 1 can be constructed as follows. The equation is ∂ 2p ∂ 2p + = f (x, y). ∂x2 ∂y 2 ∂p ∂p and the boundary conditions are ∂x = 0 and ∂y = 0 at vertical and horizontal boundaries (x=0,1; y=0,1), respectively. Consider any function that would satisfy such boundary conditions, e.g.

pex (x, y) = cos(πx) cos(πy). For this function the Laplacian can be computed ∂ 2 pex ∂ 2 pex + = −2π 2 cos(πx) cos(πy) ∂x2 ∂y 2 Therefore the rhs can be used as the function f (x, y) in the equation for p(x, y), the equation solved using a given numerical method, and the numerical solution compared with pex above to determine how accurate the numerics are. • Consider again the above pressure equation for a square domain ∂ 2p ∂ 2p + = f (x, y) ∂x2 ∂y 2

6

∂p ∂p with the boundary conditions ∂x = 0 and ∂y = 0 at vertical and horizontal boundaries (x=0,1; y=0,1), respectively. If one adds any linear function of x and y to the solution p(x, y)

p′ (x, y) = p(x, y) + αx + βy + γ, the function p′ (x, y) is also a solution. To enforce the boundary conditions we must have α = 0 and β = 0 but γ is an arbitrary constant. If subroutines FACT and SOLVE are used to solve the discretized pressure equation, usually the solution is dominated by a very large con∂p ∂p stant γ. However, the derivatives ∂x and ∂y needed to advance in time the velocity are ﬁnite and well behaved (though not very accurate). To get the ﬁnite values of pressure the solver can be modiﬁed by forcing the pressure to a constant value at one or few selected points. For instance, for the example of the exact solution, pex (1, 1) = 1. The matrix equation can be considered for all points except point (NX,NY), corresponding for a square domain to x = 1, y = 1. In all remaining equations wherever pN X,N Y appears, it is replaced by its known value of 1, and moved to the r.h.s. The result is a system of (N X × N Y − 1) equations for pressure values pj,k at all points except the corner point (N X, N Y ). Solving this system by subroutines FACT/SOLVE produces values of pressure of the order unity. However, the pressure gradients in both methods are approximately the same, leading to comparable results for the velocities u and v. The approach has been tested using FACT/SOLVE and Matlab direct inverse INV, producing results with the following accuracy: for FACT()/SOLVE() mesh: 11X11 21X21 31X31 rms : 9.4150E-03 2.3240E-03 1.02890E-03 ============================================================== for Matlab (direct solver: inv() ) mesh rms :

11X11 9.1395E-02

21X21 4.7675E-02

7

31X31 3.2274E-02

Format of the Final Report General Requirements: • Provide all equations for the algorithms used in the program, including discretized equations that are solved in each step of the method. • Provide listing of the program implementing the discretized equations, with comments and explanations what diﬀerent parts of the program do. • Provide a full computer program package that contains all needed subroutines/functions. Email the program package and instructions how to run it to both the instructor and TA. Speciﬁcally, the ﬁnal report should provide the following information: 1. Solutions for three diﬀerent Reynolds numbers Re = 1, Re = 10, and Re = 100. Experiment with diﬀerent mesh sizes and values of xmax until solution at the outﬂow converges to the form predicted analytically. 2. For a uniform comparison between diﬀerent programs set the length of the domain in the x-direction to xmax = 3, the mesh size to ∆x = ∆y = 0.1, i.e. number of mesh points is nx = 31 and ny = 21, and the time step to ∆t = 0.01. The domain size is a bit too small for the case Re = 100 so consider only two values of Re, 1 and 10 for this part of the project. 3. Provide printout and plots for the following quantities: (a) vertical proﬁles of the velocity components u and v at j = 2, (nx+ 1)/2, nx − 1, nx, i.e., (u(2, k), k = 1, ny), etc.; plot proﬁles for u in one ﬁgure together with the exact solution at the outﬂow. (b) velocity u and v along the centerline k = (ny+1)/2, i.e, (u(j, (ny+ 1)/2), j = 1, nx), etc.; plot u as a function of x and mark in the ﬁgure the asymptotic value of 1.5 at the outﬂow. 8

(c) the ﬂow rate Q(x) in the channel at each j; plot this quantity together with a line corresponding to the constant ﬂow rate at the inﬂow. (d) the rms error for the u velocity prediction at the outﬂow 

∆urms = 

ny−1 ∑ k=2

1/2

(uexa (k) − u(nx, k))2  ny − 2

(e) the rms error for the ﬂow rate prediction along the channel 

∆Qrms = 

nx ∑ (Qexa (j) − Q(j))2 j=1

nx

1/2 

4. After completing the above tasks explore other choices of the mesh size, domain size, time step, etc. until your program, in your opinion, gives the best possible agreement with the theoretical u velocity proﬁle at the outﬂow. Print and plot your best results and compare with the results obtained for previously prescribed resolution.

9

Reynols # =1 :

Figure 1) u-velocity contour, N_x=50, N_y=34, x_max=2

Figure 2) v-velocity contour, N_x=50, N_y=34, x_max=2 2

Figure 3) u-velocity profile at exit, N_x=50, N_y=34, x_max=2 Reynols # =1 0:

Figure 4) u-velocity contour, N_x=45, N_y=21, x_max=4 3

Figure 5) u-velocity contour, N_x=45, N_y=21, x_max=4

Figure 6) u-velocity exit profile, N_x=45, N_y=21, x_max=4, ~ 5% Error in u_ma 4

Reynols # =1 00:

Figure 7) u-velocity contour, N_x=45, N_y=21, x_max=4

Figure 8) v-velocity contour, N_x=45, N_y=21, x_max=4 5

Figure 8) u-velocity exit profile, N_x=45, N_y=21, x_max=4, ~ 5% Error in u_ma

6

2-Solutions are plotted in form of colored contour plots and exit flow profiles:

Fig. 9) Re=1, U Contour, 31x21 Mesh Grid, RMS=10E-10

Fig. 10) Re=1, V Contour, 31x21 Mesh Grid, RMS=10E-10 7

Fig. 11) Re=1, U Velocity profile at exit, 31x21 Mesh Grid, RMS=10E-10

Fig. 12) Re=10, U Velocity contour, 31x21 Mesh Grid, RMS=10E-10 8

Fig. 13) Re=10, V Velocity contour, 31x21 Mesh Grid, RMS=10E-10

Fig. 14) Re=10, U Velocity profile at exit, 31x21 Mesh Grid, RMS=10E-10 9

3-

(a) Vertical profiles of v & u in various x-stations

Fig. 15) Re=1, U Velocity profile at different x-stations

Fig. 16) Re=10, U Velocity profile at different x-stations 10

Fig. 17) Re=100, U Velocity profile at different x-stations

Fig. 18) Re=1, V Velocity profile at different x-stations

11

Fig. 18) Re=10, V Velocity profile at different x-stations

Fig. 19) Re=100, V Velocity profile at different x-stations

12

(b) U and V components of fluid velocity along the centerline of the channel in the x direction. Note that the v-component is effectively zero on the centerline. Due to inadequate size of the domain for Reynolds # of 100, results are physically meaningless.

Fig. 20) Re#=1 U-component of velocity along x-axis. V component is zero.

Fig. 21) Re#=10 U-component of velocity along x-axis. V component is zero.

13

Fig. 22) Re#=100 U-component of velocity along x-axis. V component is zero.

Fig. 23) Re#=100 V-component of velocity along x-axis. Fluctuations of the vcomponent on the center line can be observed

14

(c) Flow Rate at the channel vs. x-station (i.e. j)

Fig. 24) Re=1, Flow rate vs. x-station in the channel. Red line indicates the constant inflow rate.

Fig. 25) Re=10, Flow rate vs. x-station in the channel. Red line indicates the constant inflow rate.

15

Fig. 26) Re=100, Flow rate vs. x-station in the channel. Red line indicates the constant inflow rate. Parts d & e-RMS Errors Reynolds # 1 10 100

Flow Rate RMS 0.0184 0.0171 N/A

U-Component RMS 0.0058 0.0042 N/A

16

Part 4)

Fig. 26) U-velocity contour for N_x=N_y=50 and Reynolds #=1.

Fig. 27) V-velocity contour for N_x=N_y=50 and Reynolds #=1. 17

Fig. 28) Exit U-velocity profile. A very good agreement was observed as the Number of nodes were increased and the CFL number was lowered. Increasing the number of grid points seem to have a significant influence on the agreement of the numeric results with the theoretical prediction. Also it seems that the code performs substantially better if the aspect ratio of grid elements matches the aspect ratio of the analysis domain. Better agreement was observed for the square domain, for which the result can be seen here. N_x was set equal to N_y equal to 50, with a CFL number of 0.2. Compared to the rectangular domain with lower number of nodes, a significant improvement in agreement with theoretical projections is observed.

18

-Main File: % This code provides a solution to the 2-D Navier Stokes Equation, applied to % the flow inside a rectangular chanel of hight 2h, and a lenght x_max, % with a prescribed entery flow velocity of u_0 in the x_direction, and % assuming that the flow is a fully developed parabolic Poiseuille flow at the % outlet. %File Name: MainFile.m MATLAB 7.1.0.124 (R14) SP 3 %Programmer: Sina Golshany % I/O: % Input: % epsilon: Convergence criteria for u and v Default set to 10e9. May be decreased as much as 10e-5 to allow faster convergence. % CFL: CFL number % N_x: Number of mesh points in the x-direction % N_y: Number of mesh points in the y-direction % x_max: Maximum x_value for the analysis domain % h: Half-width of the channel (total width=2h) % Reynolds: Prescribed Reynolds # by the user % t_initial: Start time of the analysis % N_t:Number of time steps defined by the user % t_final: End time of the analysis computed based on t_initial and N_t requested by the user % % Output: % final values of u & v % %Record of Revission: % Date: Programmer: Description: %====================================================================== ==== % 11/20/2010 S. Golshany Main Code Started % 11/26/2010 S. Golshany nonlinearstep added,main file modified % 11/27/2010 S. Golshany Pressure Step Added % 11/27/2010 S. Golshany Pressure Step Modified % 11/28/2010 S. Golshany Am.m Generating Correct 'A' Matrix for pressure solver % 11/29/2010 S. Golshany Pressure Solver debugging % 11/30/2010 S. Golshany Pressure Solver debugging => Running AOK % 11/01/2010 S. Golshany Viscouss Step programmed, working AOK % 11/02/2010 S. Golshany Basic Testing & Benchmarking. Small Modifications added to the main code. % 11/03/2010 S. Golshany Report Preperation Started % 11/10/2010 S. Golshany To be delivered close all clc clear epsilon=1e-10 %Convergence criteria for u and v: CFL=0.5; % CFL Number. Lower values produce more realistic flow rate estimations. u_0=1; % Normalized enterance flow velocity. global N_x %Globalizing the number of mesh elements in the x-Direction N_x=31;

19

global N_y N_y=21; x_max=3; h=1; del_x=x_max/h/(N_x-1); %computing the physical size of the x-direction elements del_y=2/(N_y-1); %computing the physical size of the y-direction elements %nu=0.0001; Reynolds=1; t_initial=0; del_t=0.01; N_t=5000; t_final=del_t*N_t; %defining x & y: x=0:del_x*h:x_max; y=h:-del_y*h:-h; %defining Mesh grids for contour plotting [X,Y] = meshgrid(x,y); %Creating the Matrix of coefficients for pressure solver [A]=Am(N_x,N_y,del_x,del_y); % Initializing u_n u_n=zeros(N_y,N_x); % Initializing v_n v_n=zeros(N_y,N_x); % Initializing u_n interiors to one. u_n(2:N_y-1,1:N_x-1)=1; % Initializing u_n1 (u_n-1) in the same manner as u_n u_n1=u_n; % Initializing u_n1 (u_n-1) in the same manner as v_n v_n1=v_n; % Time Loop for the solvers for t=t_initial:del_t:t_final-del_t %Re-computing del_t based on CFL stability criteria del_t=CFL*(del_x)^2/(max(max(u_n))); % Running the non-linear step in the time loop [u_star,v_star]=convec(del_x,u_n1,v_n1,u_n,v_n,del_y,del_t,Reynolds,u_0 ); %Updating del_t based on CFL stability criteria and new velocity values del_t=CFL*(del_x)^2/(max(max(u_star))); % Running the pressure step in the time loop [u_dstar,v_dstar]=press(u_star,v_star,A,del_t,del_x,del_y); del_t=CFL*(del_x)^2/(max(max(u_dstar))); % Running the pressure step in the time loop [u_np1,v_np1,u_ts,v_ts]=visc(u_dstar,v_dstar,del_t,Reynolds,del_x,del_y ); % Updating the u_n minus one by u_n u_n1=u_n; % Updating the v_n minus one by v_n v_n1=v_n; u_n=u_np1; v_n=v_np1; %Computing and displaying the rms errors for u & v: ErrorU=(sum(sum((u_n-u_n1).^2))).^0.5

20

ErrorV=(sum(sum((v_n-v_n1).^2))).^0.5 %Terminating if exceeding the prescribed epsillon value. if ErrorU <epsilon break end

end %Computing the flow rate values along x for j=1:N_x Q(j)=mean(u_n(:,j)); end %Computing the rms for the flow rate values along x ErrorQ=(sum(sum(1-Q))/N_x)^0.5

% Creates figure(1), final value of u: figure(1) contourf(X,Y,u_n) xlabel('X') ylabel('Y') title('U Velocity Contour') colorbar % Creates figure(2), final value of u: figure(2) contourf(X,Y,v_n) xlabel('X') ylabel('Y') title('V Velocity Contour') colorbar % Create figure(3), the outlet velocity profile both theoritical & % numerical figure(3) plot(u_n(:,N_x),y,'--') hold on u_theory=zeros(1,N_x) u_theory=(3/2)*(1-y.^2) Q_exact=1 for k=2:N_y-1 Del_U_RMS=(sum((u_theory(k)-u_n(k,N_x)).^2)/(N_y-2)).^0.5; %Computing RMS Error for U end for j=1:N_x Del_Q_RMS=(sum((Q_exact-Q(j)).^2/N_x))^0.5; %Computing RMS Error for Q end clc Del_U_RMS Del_Q_RMS plot(u_theory,y) xlabel('U-Nondimentional in U_0') ylabel('Y Nondimentional in h') title('Velocity profile at exit') figure(3) plot(v_n(:,N_x),y,'--') xlabel('V-Nondimentional in U_0') ylabel('Y Nondimentional in h') title('Velocity profile at exit') hold off % Clearing the globalized values of N_x, and N_y

21

clear global N_x clear global N_y

Am.m:

% This code computes the matrix of coefficients for the pressure solver in % sub-routin file press.m. %File Name: Am.m MATLAB 7.1.0.124 (R14) SP 3 %Programmer: Sina Golshany % I/O: % Input: % epsilon: Convergence criteria for u and v Default set to 10e9. May be decreased as much as 10e-5 to allow faster convergence. % CFL: CFL number % N_x: Number of mesh points in the x-direction % N_y: Number of mesh points in the y-direction % x_max: Maximum x_value for the analysis domain % h: Half-width of the channel (total width=2h) % Reynolds: Prescribed Reynolds # by the user % t_initial: Start time of the analysis % N_t:Number of time steps defined by the user % t_final: End time of the analysis computed based on t_initial and N_t requested by the user % % Output: % A=Matrix of coefficients for the pressure solver % %Record of Revission: % Date: Programmer: Description: %====================================================================== % 11/28/2010 S. Golshany Am.m Generating Correct 'A' Matrix for pressure solver function [A]=Am(N_x,N_y,del_x,del_y) A=zeros(N_y-2,N_x-2); %Internal Nodes of the domain of analysis for k=2:N_y-1 for j=2:N_x-1 A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); A(stack(k,j,N_y),stack(k+1,j,N_y))=1/((del_y)^2); A(stack(k,j,N_y),stack(k-1,j,N_y))=1/((del_y)^2); A(stack(k,j,N_y),stack(k,j+1,N_y))=1/((del_x)^2); A(stack(k,j,N_y),stack(k,j-1,N_y))=1/((del_x)^2); end end %Upper boundary of the domain of analysis for j=2:N_x-1 for k=N_y A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); A(stack(k,j,N_y),stack(k,j+1,N_y))=1/((del_x)^2); A(stack(k,j,N_y),stack(k,j-1,N_y))=1/((del_x)^2); A(stack(k,j,N_y),stack(k-1,j,N_y))=2/((del_y)^2); end end %Lower boundary of the domain of analysis for j=2:N_x-1 for k=1

22

end

A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); A(stack(k,j,N_y),stack(k,j+1,N_y))=1/((del_x)^2); A(stack(k,j,N_y),stack(k,j-1,N_y))=1/((del_x)^2); A(stack(k,j,N_y),stack(k+1,j,N_y))=2/((del_y)^2); end

%Left side boundary of the domain of analysis for j=1 for k=2:N_y-1 A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); A(stack(k,j,N_y),stack(k,j+1,N_y))=2/((del_x)^2); A(stack(k,j,N_y),stack(k+1,j,N_y))=1/((del_y)^2); A(stack(k,j,N_y),stack(k-1,j,N_y))=1/((del_y)^2); end end %Right side boundary of the domain of analysis for j=N_x for k=2:N_y-1 A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); A(stack(k,j,N_y),stack(k-1,j,N_y))=1/((del_y)^2); A(stack(k,j,N_y),stack(k+1,j,N_y))=1/((del_y)^2); A(stack(k,j,N_y),stack(k,j-1,N_y))=2/((del_x)^2); end end %Corner values: % Upper Left Corner: k=N_y; j=1; A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); A(stack(k,j,N_y),stack(k,j+1,N_y))=2/((del_x)^2); A(stack(k,j,N_y),stack(k-1,j,N_y))=2/((del_y)^2); % Upper Right Corner: k=N_y; j=N_x; %A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); %A(stack(k,j,N_y),stack(k-1,j,N_y))=2/((del_y)^2); %A(stack(k,j,N_y),stack(k,j-1,N_y))=2/((del_x)^2); A(stack(k,j,N_y),stack(k,j,N_y))=1; % Lower Right Corner: k=1; j=N_x; %A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); %A(stack(k,j,N_y),stack(k+1,j,N_y))=2/((del_y)^2); %A(stack(k,j,N_y),stack(k,j-1,N_y))=2/((del_x)^2); A(stack(k,j,N_y),stack(k,j,N_y))=1; % Lower Left Corner: k=1; j=1; A(stack(k,j,N_y),stack(k,j,N_y))=-2*((1/(del_x^2))+(1/(del_y^2))); A(stack(k,j,N_y),stack(k+1,j,N_y))=2/((del_y)^2); A(stack(k,j,N_y),stack(k,j+1,N_y))=2/((del_x)^2); end % Stack function, adopted from AME-404 course material, Fall 2008 Dr. % Dravinski

23

function [ivec]=stack(k,j,N_y) ivec=(j-1)*N_y+k; end Convec.m % This code performs the computation in the non-linear step of the main.m % CFD solver for a channel flow. %File Name: MainFile.m MATLAB 7.1.0.124 (R14) SP 3 %Programmer: Sina Golshany % I/O: % Input: % epsilon: Convergence criteria for u and v Default set to 10e9. May be decreased as much as 10e-5 to allow faster convergence. % CFL: CFL number % N_x: Number of mesh points in the x-direction % N_y: Number of mesh points in the y-direction % x_max: Maximum x_value for the analysis domain % h: Half-width of the channel (total width=2h) % Reynolds: Prescribed Reynolds # by the user % t_initial: Start time of the analysis % N_t:Number of time steps defined by the user % t_final: End time of the analysis computed based on t_initial and N_t requested by the user % % Output: % values of u* and v* %Record of Revission: % Date: Programmer: Description: %====================================================================== % 11/26/2010 S. Golshany nonlinearstep added,main file modified function[u_star,v_star]=convec(del_x,u_n1,v_n1,u_n,v_n,del_y,del_t,Reyn olds,u_0) % Calling global values of N_x, and N_y. global N_x; global N_y; u_star=u_n; v_star=v_n; for k=2:N_y-1 for j=2:N_x-1 %Computing u*: star1=-u_n(k,j)*((u_n(k,j+1)-u_n(k,j-1))/(2*del_x))v_n(k,j)*(u_n(k+1,j)-u_n(k-1,j))/(2*del_y)+(3/Reynolds); star2=-u_n1(k,j)*((u_n1(k,j+1)-u_n1(k,j-1))/(2*del_x))v_n1(k,j)*(u_n1(k+1,j)-u_n1(k-1,j))/(2*del_y)+(3/Reynolds); u_star(k,j)=del_t*((3/2)*star1-0.5*star2)+u_n(k,j); %Computing v*: star3=(-u_n(k,j)*((v_n(k,j+1)-v_n(k,j-1))/(2*del_x)))v_n(k,j)*(v_n(k+1,j)-v_n(k-1,j))/(2*del_y); star4=(-u_n1(k,j)*((v_n1(k,j+1)-v_n1(k,j-1))/(2*del_x)))v_n1(k,j)*(v_n1(k+1,j)-v_n1(k-1,j))/(2*del_y); v_star(k,j)=del_t*((3/2)*star3-0.5*star4)+v_n(k,j); end end % Computing the u* values on right hand exit boundary (Neumann's) excluding % the corner points.

24

for j=N_x for k=2:N_y-1 star1=-v_n(k,j)*(u_n(k+1,j)-u_n(k-1,j))/(2*del_y)+(3/Reynolds); star2=-v_n1(k,j)*(u_n1(k+1,j)-u_n1(k1,j))/(2*del_y)+(3/Reynolds); u_star(k,j)=del_t*((3/2)*star1-0.5*star2)+u_n(k,j); end end

Press.m:

% This code provides a solution to the 2-D Navier Stokes Equation, applied to % the flow inside a rectangular chanel of hight 2h, and a lenght x_max, % with a prescribed entery flow velocity of u_0 in the x_direction, and % assuming that the flow is a fully developed parabolic Poiseuille flow at the % outlet. %File Name: MainFile.m MATLAB 7.1.0.124 (R14) SP 3 %Programmer: Sina Golshany % I/O: % Input: % epsilon: Convergence criteria for u and v Default set to 10e9. May be decreased as much as 10e-5 to allow faster convergence. % CFL: CFL number % N_x: Number of mesh points in the x-direction % N_y: Number of mesh points in the y-direction % x_max: Maximum x_value for the analysis domain % h: Half-width of the channel (total width=2h) % Reynolds: Prescribed Reynolds # by the user % t_initial: Start time of the analysis % N_t:Number of time steps defined by the user % t_final: End time of the analysis computed based on t_initial and N_t requested by the user % % Output: % Final matrixes of u** and v** (i.e. u_dstar, and v_dstar) % %Record of Revission: % Date: Programmer: Description: %====================================================================== % 11/27/2010 S. Golshany Pressure Step Added % 11/27/2010 S. Golshany Pressure Step Modified % 11/28/2010 S. Golshany Am.m Generating Correct 'A' Matrix for pressure solver % 11/29/2010 S. Golshany Pressure Solver debugging % 11/30/2010 S. Golshany Pressure Solver debugging => Running AOK function [u_dstar,v_dstar,forcing]=press(u_star,v_star,A,del_t,del_x,del_y) % Calling global values for N_x, and N_y global N_x global N_y % Initializing the forcing term in pressure forcing=zeros(1,N_x*N_y); % Computing the forcing term for the interior nodes. Using the stack

25

% function to place at the corect point in the forcing matrix. for k=2:N_y-1 for j=2:N_x-1 forcing(stack(k,j))=(1/del_t)*(((u_star(k,j+1)-u_star(k,j1))/(2*del_x))+((v_star(k+1,j)-v_star(k-1,j))/(2*del_y))); end end % Computing the forcing term for the Left side: for k=2:N_y-1 for j=1 forcing(stack(k,j))=(1/del_t)*((u_star(k,j+1)u_star(k,j))/(del_x)+((v_star(k+1,j)-v_star(k-1,j))/(2*del_y))); end end % Computing the forcing term for the Right Side: for k=2:N_y-1 for j=N_x forcing(stack(k,j))=(1/del_t)*((v_star(k+1,j)-v_star(k1,j))/(2*del_y)); end end % Computing the forcing term for the Lower Side: for k=1 for j=2:N_x-1 forcing(stack(k,j))=(1/del_t)*((u_star(k,j+1)-u_star(k,j1))/(2*del_x)+((v_star(k+1,j)-v_star(k,j))/(del_y))); end end % Computing the forcing term for the Upper Side: for k=N_y for j=2:N_x-1 forcing(stack(k,j))=(1/del_t)*((u_star(k,j)-u_star(k,j1))/(del_x)+((v_star(k,j)-v_star(k-1,j))/(del_y))); end end % Computing the forcing term for the top left corner: k=N_y; j=1; forcing(stack(k,j))=(1/del_t)*((u_star(k,j+1)u_star(k,j))/(del_x)+((v_star(k,j)-v_star(k-1,j))/(del_y))); % Computing the forcing term for the lower left corner: k=1; j=1; forcing(stack(k,j))=(1/del_t)*((u_star(k,j+1)u_star(k,j))/(del_x)+((v_star(k+1,j)-v_star(k,j))/(del_y))); % Computing the forcing term for the lower right boundary: forcing(stack(1,N_x))=1; % Computing the forcing term for the right upper boundary: forcing(stack(N_y,N_x))=1; % Solving the pressure system using the internal MATLAB feature '\'. p=A\forcing'; % Reshaping the vector of perturbation pressure in the coresponding matrix. p_mat=reshape(p,N_y,N_x); % Initializing the u** and v** matrixes using the values for u* and v* u_dstar=u_star; v_dstar=v_star;

26

% Computing u** and v** values using newly computed perturbation pressure % values. for k=2:N_y-1 for j=2:N_x-1 u_dstar(k,j)=u_star(k,j)-(del_t/(2*del_x))*(p_mat(k,j+1)p_mat(k,j-1)); v_dstar(k,j)=v_star(k,j)-(del_t/(2*del_y))*(p_mat(k+1,j)p_mat(k-1,j)); end end end % Stack function, adopted from AME-404 course material, Fall 2008 Dr. % Dravinski function ivec=stack(k,j) global N_y ivec=(j-1)*N_y+k; end

Visc.m: % This code performs the computation to obtain the u and v matrixes for the % viscous step in the CFD code Main.m. This code works in 2 steps. %File Name: visc.m MATLAB 7.1.0.124 (R14) SP 3 %Programmer: Sina Golshany % I/O: % Input: % epsilon: Convergence criteria for u and v Default set to 10e9. May be decreased as much as 10e-5 to allow faster convergence. % CFL: CFL number % N_x: Number of mesh points in the x-direction % N_y: Number of mesh points in the y-direction % x_max: Maximum x_value for the analysis domain % h: Half-width of the channel (total width=2h) % Reynolds: Prescribed Reynolds # by the user % t_initial: Start time of the analysis % N_t:Number of time steps defined by the user % t_final: End time of the analysis computed based on t_initial and N_t requested by the user % % Output: % final values of u & v % %Record of Revission: % Date: Programmer: Description: %====================================================================== % 11/01/2010 S. Golshany Viscouss Step programmed, working AOK function [u_n1,v_n1,u_ts,v_ts]=visc(u_ds,v_ds,del_t,Reynolds,del_x,del_y); global N_x global N_y %==== First Viscous Step====

27

u_ts=u_ds;% Enforcing the BCs which are already applied to u_ds v_ts=v_ds;% Enforcing the BCs which are already applied to u_ds %Solving for u_ts: %forming the a,b,c,d for Mytridiag applied to the first step, solving for u*** au_xx=-0.5*(del_t/(Reynolds*del_x^2))*ones(1,N_x-1); au_xx(N_x-1)=2*(au_xx(N_x-1)); au_xx(1)=0; % As required by the Mytridiag solver. bu_xx=zeros(1,N_x-1); %Initialized, to be computed cu_xx=-0.5*(del_t/(Reynolds*del_x^2))*ones(1,N_x-1); cu_xx(N_x-1)=0; % As required by the Mytridiag solver. du_xx=ones(1,N_x-1)*(1+(del_t/(Reynolds*del_x^2))); for k=2:N_y-1 %completing bu_xx: for j=2:N_x-1 bu_xx(j-1)=(del_t/(2*Reynolds*del_x^2))*u_ds(k,j-1)+(1del_t/(Reynolds*del_x^2))*u_ds(k,j)+(del_t/(2*Reynolds*del_x^2))*u_ds(k ,j+1); end j=N_x; bu_xx(j-1)=(del_t/(2*Reynolds*del_x^2))*u_ds(k,j-1)+(1del_t/(Reynolds*del_x^2))*u_ds(k,j)+(del_t/(2*Reynolds*del_x^2))*u_ds(k ,j-1); bu_xx(1)=bu_xx(1)+(del_t/(2*Reynolds*(del_x^2))); %Solving for u_ts (u***) using Mytridiag solver: u_ts(k,2:N_x)=Mytridiag(au_xx,bu_xx,cu_xx,du_xx); end %Solving for v_ts: %forming the a,b,c,d for Mytridiag applied to the first step, solving for v*** av_xx=-0.5*(del_t/(Reynolds*del_x^2))*ones(1,N_x-2); bv_xx=zeros(1,N_x-2); cv_xx=-0.5*(del_t/(Reynolds*del_x^2))*ones(1,N_x-2); dv_xx=ones(1,N_x-2)*(1+(del_t/(Reynolds*del_x^2))); av_xx(1)=0;% As required by the Mytridiag solver. cv_xx(N_x-2)=0;% As required by the Mytridiag solver. for k=2:N_y-1 %completing bv_xx: for j=2:N_x-1 bv_xx(j-1)=(del_t/(2*Reynolds*del_x^2))*v_ds(k,j-1)+(1del_t/(Reynolds*del_x^2))*v_ds(k,j)+(del_t/(2*Reynolds*del_x^2))*v_ds(k ,j-1); end %Solving for u_ts (u***) using Mytridiag solver: v_ts(k,2:N_x-1)=Mytridiag(av_xx,bv_xx,cv_xx,dv_xx); end %==== Second Viscous Step==== u_n1=u_ts;% Enforcing the BCs which are already applied to u_ds v_n1=v_ts;% Enforcing the BCs which are already applied to u_ds %Solving for u_n+1: %forming the a,b,c,d for Mytridiag applied to the first step, solving for u^n+1 au_yyy=-0.5*(del_t/(Reynolds*del_y^2))*ones(1,N_y-2); au_yyy(1)=0; % As required by the Mytridiag solver.

28

bu_yyy=zeros(1,N_y-2); %Initialized, to be computed cu_yyy=-0.5*(del_t/(Reynolds*del_y^2))*ones(1,N_y-2); cu_yyy(N_y-2)=0; % As required by the Mytridiag solver. du_yyy=ones(1,N_y-2)*(1+(del_t/(Reynolds*del_y^2))); for j=2:N_x %completing bu_yyy: for k=2:N_y-1 bu_yyy(k-1)=(del_t/(2*Reynolds*del_y^2))*u_ts(k-1,j)+(1del_t/(Reynolds*del_y^2))*u_ts(k,j)+(del_t/(2*Reynolds*del_y^2))*u_ts(k +1,j); end %Solving for u_n1 (u^n+1) using Mytridiag solver: u_n1(2:N_y-1,j)=Mytridiag(au_yyy,bu_yyy,cu_yyy,du_yyy); end %Solving for v_n+1: %forming the a,b,c,d for Mytridiag applied to the first step, solving for u^n+1 av_yyy=-0.5*(del_t/(Reynolds*del_y^2))*ones(1,N_y-2); av_yyy(1)=0; % As required by the Mytridiag solver. bv_yyy=zeros(1,N_y-2); %Initialized, to be computed cv_yyy=-0.5*(del_t/(Reynolds*del_y^2))*ones(1,N_y-2); cv_yyy(N_y-2)=0; % As required by the Mytridiag solver. dv_yyy=ones(1,N_y-2)*(1+(del_t/(Reynolds*del_y^2))); for j=2:N_x-1 %completing bu_yyy: for k=2:N_y-1 bv_yyy(k-1)=(del_t/(2*Reynolds*del_y^2))*v_ts(k-1,j)+(1del_t/(Reynolds*del_y^2))*v_ts(k,j)+(del_t/(2*Reynolds*del_y^2))*v_ts(k +1,j); end %Solving for u_n1 (u^n+1) using Mytridiag solver: v_n1(2:N_y-1,j)=Mytridiag(av_yyy,bv_yyy,cv_yyy,dv_yyy); end

Mytridiag.m:

function x=Mytridiag(a,b,c,d) % This code provides a solution to a tri-diagonal system of equations %File Name: MyLTriang.m MATLAB 7.1.0.124 (R14) SP 3 %Programmer: Sina Golshany % I/O: % Input: % a: Lower Sub-diagonal vector (inside the coefficient matrix) % b: Constants for the Tri-diagonal system % c: Upper Sub-diagonal vector (inside the coefficient matrix) % d: Super diagonal vector of the coefficient matrix for the % tri-diagonal system % x : Solution to the provided linear system for the desired % unknowns (x_1,x_2,...,x_n) %Record of Revission: % Date: Programmer: Description:

29

%====================================================================== ==== % 9/13/2008 S. Golshany Main Code msg=nargchk(4,4,nargin); error=('msg'); N_a=length(a); N_b=length(b); N_c=length(c); N_d=length(d); N=[N_a,N_b,N_c,N_d]; for ii=1:4 for jj=1:4 if N(ii)~=N(jj) error('Incompatible Matrices') else end end end n=length(a); if a(1)~=0 error('Provided vectors do not satisfy the requirement a(1)=0') else end if c(n)~=0 error('Provided vectors do not satisfy the requirement c(1)=0') else end for zz=1:n % Corect the flow chart if d(zz)~=0 else error('Provided vectors do not satisfy the requirement d(n)=0') end end for kk=2:n fa=(a(kk)/d(kk-1)); d(kk)=d(kk)-fa*c(kk-1); b(kk)=b(kk)-fa*b(kk-1); end x(n)=b(n)/d(n); for kk=n-1:-1:1 x(kk)=(b(kk)-c(kk)*x(kk+1))/d(kk); end

30

CFD Modeling Of Fluid Flow In A Rectangular Channel aia Adams-Bashforth Time Advancing
CFD Modeling Of Fluid Flow In A Rectangular Channel aia Adams-Bashforth Time Advancing