27
Pergamo. Computers & Geosciences Vol. 20, No. 3, pp. 265-291, 1994 Copyright © 1994 Elsevier Science Ltd 0098-3004(93)E0011-4 Printed in Great Britain. All rights reserved 0098-3004/94 $7.00 + 0.00 FDFLOW: A FORTRAN-77 SOLVER FOR 2-D INCOMPRESSIBLE FLUID FLOW C. M. LEMOS Department of Coastal and Estuarine Dynamics, Instituto Hidrografico, R. das Trinas 49, 1296 Lisbon-Codex, Portugal (Received 13 April 1993; accepted 25 October 1993) Abstract--ln this work, a FORTRAN-77 program for solving two-dimensional (2-D) incompressible flows is presented. The algorithm is based on finite difference approximations of the momentum and continuity equations, and consists of two stages. In the first stage, a temporary field is obtained using conservative approximations of the momentum equations, either implicit or explicit, with the old pressure gradient. In the second stage, the advanced-time velocity and pressure are obtained using an iterative method, in such a way that the incompressibility constraint is satisfied. The program was written in a modular form, and can be used to obtain accurate solutions to some difficult fluid-flow problems with small PCs. This capability was tested by its application to lid-dtiven cavity flow and flow past a backward-facing step, with Reynolds number up to 10,000. The model described herein might be useful for both educational and professional purposes. Because of its simple design, the program can be taken as a starting point to test other numerical schemes, or to treat more advanced problems than those considered in this work, such as thermally coupled flow, free-surface flow, and 3-D flow. Key Words: Navier-Stokes equations, Finite differences, Computer program, Personal computers. NOMENCLATURE Dij = discrete divergence in cell i,j f = (fx, f~.) = vector of the external force field i = index for computational cells in the x- direction /_ = identity operator I = number of interior cells in the x-direction IMAX = total number of cells in the x-direction j = index for computational cells in the y- direction = number of interior cells in the y-direction JMAX = total number of cells in the y-direction L~ = first difference in the x-direction Lx. , = second difference in the x-direction L r = first difference in the y-direction L~. r = second difference in the y-direction n = index for time level n = (ny, n,) = outward unit normal vector p = kinematic pressure (pressure divided by con- stant density) Q~ = generic difference scheme for the convection- diffusion part of the momentum equations, involving y-derivatives only Q = genetic difference scheme for the convection- diffusion part of the momentum equations t = time u = (u, v) = velocity vector u b = (u b, vb) = boundary velocity u0 = (u0, v0) = initial velocity u* = (u*, v*) = temporary velocity, such that ~ x u* = Vxu,~-u*~O fi=(:4, f) = auxiliary velocity, used in the implicit momentum calculations x = (x, y) = position vector F o = subset of c~fZ on which Ditichlet-type con- ditions are imposed FN = subset of c~fZon which Neumann-type con- ditions are imposed At = time step Ax = mesh spacing in the x-direction Ay = mesh spacing in the y-direction v = kinematic viscosity fZ = domain for the fluid-flow problem = closure of f~ ~f~ = boundary of fL = FD + FN INTRODUCTION In this work, a FORTRAN-77 program is presented and documented that implements a finite difference solution algorithm for the incompressible Navier- Stokes equations in two dimensions. This program was developed primarily as an educational tool for teaching computational fluid dynamics on small PCs. The solution algorithm consists of two stages. In the first stage, a temporary velocity field u* is calculated from the momentum equation, with the pressure gradient evaluated at the old time level (nAt). Two finite difference schemes were implemented for the computation of this temporary solution, one explicit and another implicit (fractional-step), which are both conservative. In the second stage, the advanced-time velocity u'+l and pressure p'+l are calculated from the temporary solution u* using a pressure-velocity iteration scheme, so that u" +1 satisfies the continuity equation. 265

Fdflow a Fortran 77 Solver for 2d Incomp Flow

Embed Size (px)

DESCRIPTION

contoh fortran

Citation preview

Page 1: Fdflow a Fortran 77 Solver for 2d Incomp Flow

Pergamo. Computers & Geosciences Vol. 20, No. 3, pp. 265-291, 1994

Copyright © 1994 Elsevier Science Ltd 0098-3004(93)E0011-4 Printed in Great Britain. All rights reserved

0098-3004/94 $7.00 + 0.00

FDFLOW: A F O R T R A N - 7 7 SOLVER FOR 2-D INCOMPRESSIBLE F L U I D FLOW

C. M. LEMOS

Depar tment of Coastal and Estuarine Dynamics, Instituto Hidrografico, R. das Trinas 49, 1296 Lisbon-Codex, Portugal

(Received 13 April 1993; accepted 25 October 1993)

Abst rac t - - ln this work, a FORTRAN-77 program for solving two-dimensional (2-D) incompressible flows is presented. The algorithm is based on finite difference approximations of the momen tum and continuity equations, and consists of two stages. In the first stage, a temporary field is obtained using conservative approximations of the momen t um equations, either implicit or explicit, with the old pressure gradient. In the second stage, the advanced-time velocity and pressure are obtained using an iterative method, in such a way that the incompressibility constraint is satisfied. The program was written in a modular form, and can be used to obtain accurate solutions to some difficult fluid-flow problems with small PCs. This capability was tested by its application to lid-dtiven cavity flow and flow past a backward-facing step, with Reynolds number up to 10,000.

The model described herein might be useful for both educational and professional purposes. Because of its simple design, the program can be taken as a starting point to test other numerical schemes, or to treat more advanced problems than those considered in this work, such as thermally coupled flow, free-surface flow, and 3-D flow.

Key Words: Navier-Stokes equations, Finite differences, Computer program, Personal computers.

N O M E N C L A T U R E

Dij = discrete divergence in cell i,j f = (fx, f~.) = vector of the external force field

i = index for computat ional cells in the x- direction

/_ = identity operator I = number of interior cells in the x-direction

IMAX = total number of cells in the x-direction j = index for computat ional cells in the y-

direction = number of interior cells in the y-direction

JMAX = total number of cells in the y-direction L~ = first difference in the x-direction

Lx. , = second difference in the x-direction L r = first difference in the y-direction

L~. r = second difference in the y-direction n = index for time level

n = (ny, n,) = outward unit normal vector p = kinematic pressure (pressure divided by con-

stant density) Q~ = generic difference scheme for the convection-

diffusion part of the momen t um equations, involving y-derivatives only

Q = genetic difference scheme for the convection- diffusion part of the momen t um equations

t = time u = (u, v) = velocity vector

u b = (u b, v b) = boundary velocity u0 = (u0, v0) = initial velocity

u* = (u*, v*) = temporary velocity, such that ~ x u* = Vxu,~-u*~O

fi=(:4, f) = auxiliary velocity, used in the implicit momentum calculations

x = (x, y) = position vector F o = subset of c~fZ on which Ditichlet-type con-

ditions are imposed

FN = subset of c~fZ on which Neumann-type con- ditions are imposed

At = time step Ax = mesh spacing in the x-direction Ay = mesh spacing in the y-direction

v = kinematic viscosity fZ = domain for the fluid-flow problem

= closure of f~ ~f~ = boundary of fL = FD + FN

I N T R O D U C T I O N

In th is work , a F O R T R A N - 7 7 p r o g r a m is p r e s e n t e d

a n d d o c u m e n t e d t ha t i m p l e m e n t s a finite d i f ference

s o l u t i o n a l g o r i t h m for the i n c o m p r e s s i b l e N a v i e r -

S tokes e q u a t i o n s in two d i m e n s i o n s . T h i s p r o g r a m

was deve loped p r ima r i l y as a n e d u c a t i o n a l tool for

t e a c h i n g c o m p u t a t i o n a l f luid d y n a m i c s on sma l l PCs .

T h e so lu t i on a l g o r i t h m cons i s t s o f two s tages . In the

first s tage, a t e m p o r a r y veloci ty field u* is ca l cu l a t ed

f r o m the m o m e n t u m e q u a t i o n , w i th the p r e s su re

g r a d i e n t e v a l u a t e d a t the old t ime level (nAt). T w o

finite d i f ference s c h e m e s were i m p l e m e n t e d for the

c o m p u t a t i o n o f th is t e m p o r a r y so lu t ion , one explici t a n d a n o t h e r impl ic i t ( f rac t iona l - s tep) , w h i c h are b o t h

conse rva t ive . In the s e c o n d s tage, the a d v a n c e d - t i m e

ve loc i ty u ' + l a n d p r e s s u r e p ' + l a re ca l cu la t ed f r o m the t e m p o r a r y s o l u t i o n u* u s i n g a p r e s s u r e - v e l o c i t y

i t e ra t ion s cheme , so t ha t u" +1 sat isf ies the c o n t i n u i t y e q u a t i o n .

265

Page 2: Fdflow a Fortran 77 Solver for 2d Incomp Flow

266 C.M. LEMOS

After the introduction of the governing equations, the essential aspects of incompressible fluid dynamics are reviewed in the second section of this paper. The basic solution algorithm is presented in the third section, which describes the explicit and implicit methods for the finite difference represen- tation of the momentum equations, and the press- ure-velocity iteration method for satisfying the continuity equation. The fourth and fifth sections are concerned with the discretized initial and boundary conditions and the stability constraints for the ex- plicit finite difference approximations, respectively. Finally, the computer program and some sample calculations of lid-driven cavity flow, and flow past a backward-facing step are described. Appendices 1-4 contain listings of the programs referred to in the text.

correspond to either velocity or stress specified on the boundary, respectively. If df~ = FD(Fs =g) , then ub must be compatible with Equation (2), that is

: u b ' n d S = O , t > : O . (5) n

Also, if FN = ff there are no boundary conditions for the pressure. The initial condition u0(x ) = u(x,0) must satisfy the incompressibility constraint

V ' u0 = 0, (6)

and its normal component must be continuous at the boundary for t = 0,

n . u 0 ( x ) = n . u b V X • F D . (7)

GOVERNING EQUATIONS

The 2-D motion of an incompressible fluid of uniform properties in a domain ~ with smooth boundary ~3fL can be stated as an initial- boundary value problem as follows: determine u = ( u ( x , t ) , v ( x , t ) ) and p ( x , t ) V x = ( x , y ) • • = f~ + c ~ for t > 0, such that

Ou -- + Vp + V. (u®u - vVu) = f, (I) c2t

v . u = 0, (2)

where t is the time, u is the velocity, p is the pressure divided by the constant density (or energy per unit mass resulting from the pressure field), v is the kinematic viscosity, and f = ( f , , f y ) is the vector of the external force field, V is the nabla operator, and ® denotes the tensor product. The momentum Equation (1) is written in the conserva- tive or divergence form, which is appropriate for the derivation of numerical schemes that ensure conservation of the physical quantities in a discrete sense (Roache, 1972).

To define a well-posed problem, the governing equations must be supplemented by appropriate initial and boundary conditions. The boundary conditions may be of Dirichlet or Neumann type, that is

u = ub(x,t) Vx • FD, (3)

and

-? + v ~-~" = F . (x , t )

~/gr V 7 n = F A x : ) Vx • FN, (4)

where a f l = F o + F N , and n and z denote the normal and tangential directions. These conditions

The tangential component of the velocity may not be continuous at the boundary, and in general vortex sheets may be present on F D for t = 0.

The main difficulty associated with the solution of Equations (1)-(2) is the absence of an evolution equation for p. The pressure gradient is a con- servative field which keeps the flow always and everywhere nondivergent, that is maintains conserva- tion of mass. Thus, if the momentum equation without the pressure gradient was advanced in time using a consistent scheme ~ for the convection- diffusion part, the (auxiliary) advanced-time field u* =[1 + A t ~ ] ( u " ) would contain the correct vorticity, but V. u* #-0. Subtracting the correct pressure gradient then projects u* onto its div- free part (Chorin, 1968). Writing (u " * ~ - u*)/At = --Vp "+L, applying the divergence operator, and making use of the condition V • u" + ~ = 0, it is seen that the pressure may be determined (up to an arbitrary constant) by solving the following Poisson- Neumann problem (Chorin, 1968; Peyret and Taylor, 1983):

f ' Wp ~ + ~ = ~-~ V" u*, (8a)

@"+' (u "~ -u*) - - = - n . (8b)

On At

After Vp "+1 is determined, u * - A t V p "+1 will have the correct vorticity and divergence, and hence be determined uniquely by virtue of Helmholtz's decomposition theorem (e.g. Chorin and Marsden, 1979; Gustafson, 1987). Likewise, if the auxiliary velocity field was calculated using the old pressure gradient Vp ~, the pressure correction 6p = p "+ ~ - p" would be obtained as the solution of the same Poisson-Neumann problem with p n + ~ replaced by 6p, and u "+ ~= u* -AtV(6p) . This strategy for split- ting the velocity and pressure calculations is typical of many algorithms (e.g. Chorin, 1968; Peyret and Taylor, 1983) for incompressible fluid dynamics, including the one presented in this work. A recent

Page 3: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow

survey of the theory of splitting (projection) methods is given in Quartapelle (1992).

v * = vT,j + At n n

P i , j + l - - P i , j

k x

267

T H E B A S I C A L G O R I T H M

The governing equations were discretized by a staggered mesh consisting of I × J interior rectangular cells of uniform width Ax and height Ay (see Fig. 1). The fluid region is surrounded by a layer of fictitious (exterior) cells, which are used to impose boundary conditions. The fluid velocities and pressure are defined at the cell positions shown in Figure 2 (left). This staggered grid arrangement [known as the MAC mesh, after its introduction by Hariow and Welch (1965) in conjunc- tion with the Marker-And-Cell method] ensures a tight coupling between the u, v, and p solutions at adjacent grid points, avoiding even-odd decou- pling in the pressure solution. In computer codes fractional indices cannot be used. The correspon- dence between u;+j,,2o, Ui-l/2,j, Ui, j+ I/2, Ui, j - 1 /2 , and the indices used in the code, is shown in Figure 2. For the sake of clarity, all difference equations will be written as they occur in the FORTRAN program.

The solution algorithm consists of two stages. In the first stage, a temporary velocity field (u*,v*) is computed using finite difference approximations of the momentum equation with the pressure gradient evaluated at the old time level. Two different schemes were implemented, one explicit and another implicit. In the second stage, the continuity equation is satisfied using an implicit pressure-velocity relax- ation scheme (Chorin, 1968; Viecelli, 1971; Bulgarelli, Casulli, and Greenspan, 1984) which is equivalent to the solution of Equations (8a)-(8b), followed by u "+ J = u* - AtVp "+~.

When the explicit method is used, the temporary field u * = (u*,v*) is computed using the Forward- Time-Centered-Space (FTCS) scheme:

* __ ,, ~ P T + l , j - - p i n j u i / - u i ' i + A t ~ Ax

1

4Ax - - - - [ ( U n+ I j -~- u in, j )2 - - (U ~j dr- u ni l,j ]~2]]

1

4Ay

1

4Ax

- - - [(v ,"j+, + v " ) 2 _ (v 7. + v ~, ,)21

- - - - [(U in j+ 1 + U in j ) (U 7+ I,y -~- U in/)

- (u7 ,.j+~ +u7 ~.j)(v,"/+vL,.,)]

vT+ l , j - 2v~: + vT,_ 1,/ +v

A X 2

/) n - - n u n j

+ v i,j+l Ax 22vi4+ , o - l + f , ~ . (10)

The explicit FTCS approximation of the momentum equations can be made more accurate and stable by introducing upstream curvature correction terms in the interpolations for the convective fluxes (e.g. the QUICK scheme of Leonard, 1979), with moderate additional programming and computational effort. However, Leonard's scheme also is explicit, and thus subject to stability constraints.

When the implicit method is selected, the tem- porary velocity field (u*,v*) is calculated using the following fractional-step method:

1 At(tJ - u " ) = --Lx(t?t2)+vL~x(lJ), (11)

1 At (3 - v") = - Lx([th3]) + vL~,(t3), (12)

1 At (u* - ~) = -Ly([UV]*) + vL,.~.(u*)

n n + 12 - -Lx( p ) + f y , (13)

1 At (v* - g) = - Ly ([v 2],) + vL,.,. (v *)

- Ly(p" .. + 12 ) + J , (14)

where (fi,f) is a new auxiliary velocity, and the operators L~, L,., L,~ and L,, are defined as follows:

1 4Ay [(u ,"j+, + u 7,/) (v 7+ ~./+ v 7,)

--(ldin, j'~-uin, j l ) ( I d n + l , j - I - ~ t d T ` j I ) 1

+ v u n + I , j - - 2U~,j "~-un l , j

Ax 2

1 &(4 , ) = Ax (~'+ ,.2.; - ¢ , ~ _~.,),

1 L,,(4) = ~ (O,,, ~, ~ - 4 , , , , : ) ,

1 L~(~a)= ~x2(~gi+ l,.;-- 249,,i + 49i ,./),

+ v uT./+,- 2u~' u" L} ,,j + , , j - I Ax 2 t- (9)

1 L, , . (c~)=~y2(~bi j+l- 2C~,.i+qS,. l),

Page 4: Fdflow a Fortran 77 Solver for 2d Incomp Flow

268 C. M. LEMOS

dNI~X

l l 1

I I I I 1 2 ~ + 1 II'I/~X

Figure 1. Finite difference mesh.

in which qb is a generic variable• The new auxiliary velocities ~ and t3 do not require extra storage space.

To obtain an efficient solution method, the fol- lowing linearizations are introduced:

[~]2 = [u ,,]2 + 2u "(t~ -- u ") + ~(At ~),

[a~] = [uv]" + u °(c) - v ") + ¢ (~ . t : ) ,

[uv]* = [fig] + f (u* -- fi) + d)(At 2),

[v ~]* = [t)] 2 + 2~ (v* - ~ ) + C(At 2).

Ut.l, dl m

Vi,j l m

Pi,j

1 1

Oi,&l

q i, j+l/2 l 1 l

Ilu ,j

Pi,j 0 I I~l Ui+ 1/2, d

1 qi,&lt2 Figure 2. Location of variables in typical mesh cell: fractional indices (left), and integer indices used in code

(right).

Substituting these expressions into Equations (11)-(14), four implicit linear equations for fi, ~, u*, and v* are obtained. These linear equations then are solved in two half-steps, taken in alternating direc- tions. In the first half-step, fi and t~ are obtained from the finite difference equations:

fi~4 + AtL*([2u "f]i.J) - v AtLxx(f~4)

=u~y + AtLx(Iu2]~y), (15)

vi, j + AtLx([u "v]~4) - v ~tL~d6, j) = v " . . . . . j. (16)

These equations define tridiagonal systems of the form

I dbi a j 0 d 2 a 2

b3 d3 • . . " . .

, 07)

in which b~, dl, ai are the coefficients behind, on, and ahead of the main diagonal; qS~ are the unknowns; and RHS~ are the coefficients on the right-hand side.

For Equation (15) we have

~bi = fi,,i, (18a)

At bi = - ut ~ - s,, (18b)

Al 4 = 1 + % - u~) ~ x + 2s~ (18c)

Al a, = ur ~ - s~, (18d)

At R H S i = u , , . , + - - ( U r - U ? ) , A x (18e)

" + u" ~ " " wi th ur= 1/2(Ui+l,j i.j,, u)= 1/2(uij+ui_l,j), s~ = vAt/Ax 2, for I = I . . . . . IMAX-1.

Page 5: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow 269

For Equation (16), we have

~ i = Ui, j , (19a)

1 At b, = - ~ ul ~ - s~, (19b)

1 At d , = 1 + ~ ( U r - - Ut)~X +2S~, (19c)

1 At a i = ~ u, -~x - sx, (19d)

RHSi = v ~j, (19e)

in which u~ and ul are now u~= l /2(uT, j + l + U i ~ j ) , uj= 1/2(uT_l,j+t +uT_l,:), for I = 1 . . . . . IMAX-I. The tridiagonal systems defined by Equations (18) and (19) are solved using the Thomas algorithm [see Roache (1972) or Peyret and Taylor (1983) for a detailed description], sweeping in the x-direction for successive values of J, with J = 2 . . . . , JMAX-I.

In the second half-step, u* and v* are obtained from the finite difference approximations

U *j + A t Z y ( [ V u * ] i , j ) - - vA tLyy (U *j)

= a~ o - A t L x ( p 7o) + f ~ + l/2At, (20)

v *j + AtL,.([2fv*kj) - vAtLyy(V ,.*)

= g, j + A/Ly([~]~j ) - AtLy(p,~,j) + f y ÷ ~/2At. (21)

These equations lead to tridiagonal systems for u* and v*, which may be solved using the Thomas algorithm, sweeping in the y-direction for successive values of I, with I = 2 , . . . , IMAX-1. The boundary values for u*, v*, ~, and :3 at the mesh boundaries are incorporated in the tridiagonal systems, by over- writing the coefficients b,, d~, a~, and RHS~, as will be shown later.

The temporary field (u*,v*) obtained in the first stage, in general, will not satisfy the continuity equation, because the correct advanced-time pressure gradient is not known yet. To satisfy the continuity equation, pressures and velocities are adjusted itera- tively for each computational cell, in such a way that the discrete divergence left by the first stage is annihilated. The finite difference approximation of the continuity equation is

1 D'/(u"+') - Axx (" ~''+ ~ - u ~'_+:j)

1 ( v n + l n + l

+A-YY" ' j - v / . j _ , ) = 0 . (22)

The iterative solution of Equation (22) is done as follows. Let I") and ~n+J~ denote iteration levels. The pressure change required to drive the discrete diver- gence in cell I,J to zero is

Di'j(ll(n)) (23) Ap,,j= 2At(i/Ax2 + i/ky2).

The pressures and the four velocities at the faces of the cell I,J then are adjusted to reflect this change:

p~! --, n !"! ~- Ape, j, (24a) I,J 1" 10 - -

! . ! - - * ~.) A t u ,o u ~,j + ~ Ap~ o, (24b)

u ! n ) l , j _ . , u ! , ) ___At APio, (24c) - ' - ' - ~'J A x

!n! -4- A t V !"! ~ V Api . j , (24d) t,J 1,) - -

v~") ---* v !"! A t " J - ' " ' - ' - X-}y api , j , (24e)

The iteration defined by Equations (23) and (24) is performed until the maximum residual divergence in the mesh falls below a certain tolerance parameter E, whose typical value is of d9(10-3). If E is too small, a large number of iterations must be performed for each time step, decreasing the computational efficiency. If E is too large, the flow becomes "numerically compressible", and the residual diver- gences may introduce instabilities in the momentum equations. To accelerate the convergence of the iterative procedure, the pressure correction usually is multiplied by an over-relaxation factor co with values between 1 and 2. A value of co = 1.7 may be optimum.

INITIAL AND BOUNDARY CONDITIONS

The initial condition for the velocity must satisfy Equations (6) and (7) in a discrete sense. This may be a difficult task for Equation (6) when a nontrivial starting condition is imposed, and there are internal obstacles. Nevertheless, if Equations (5) and (7) are satisfied, it is possible to project (Uo,Vo) onto its d i v - f r e e part, and hence obtain a valid initial con- dition. Another possibility is the combination of a cold-start with the boundary condition Ub = (0,0) for t = 0, followed by a gradual change of the velocity on the boundary to the desired value.

The boundary conditions are imposed in the first stage of the calculation and during the pressure-velocity iteration, using the fictitious cells surrounding the mesh. The way the boundary con- ditions are treated depends on whether the velocity is calculated explicitly or implicitly. This process now will be illustrated for the left boundary (see Fig. 3), and for both types of velocity calculation.

If the velocity is calculated explicitly, as occurs for Equations (9), (10), and (24), and the boundary condition to be imposed is of the form ub = (ub,vb) on c3f~, the velocities in the fictitious cell 1,J are set as follows:

Ul, j ~---- Ub ,

Vl,j = 2vb - v2.j.

For the particular situation of a no-slip wall, these conditions become uLj= 0 and v L j = - v = j . If the

Page 6: Fdflow a Fortran 77 Solver for 2d Incomp Flow

270 C. M. LEMOS

U 1"i÷1 I U2'j+l

i

Ul' j I u2'j

I

Figure 3. Schematization of boundary conditions for left boundary.

boundary is to be a rigid free-slip wall, the normal velocity and the gradient of the tangential velocity should be zero. In this situation, the appropriate boundary conditions are

Ul, j = 0,

~l,j ~ U2,j"

Free-slip conditions may be used to simulate inviscid- fluid behavior when the discrete mesh is too coarse to capture the details of the boundary layer.

When implicit methods are used, it is necessary to prescribe boundary values for the temporary fields. If the time-accuracy of the interior scheme is to be preserved, these boundary values must be prescribed in such a way that the implicit operators can be inverted. Therefore, the boundary conditions for (f , f) and (u*,v*) are

tJ b = ( I - A t Q y ) u g + l + A t L x ( p n + i )

_ A t f ~ + 112 + ¢(At 2), Vb = ( I - - A t Q y ) v ~ + 1 + AtL~. ( p "+ 1 )

_ Atf~+ 1/2 + C(At 2),

u * = u ~ +1 + A t L x ( p " + ~ - - p " ) + O(At 2),

v * = v ~,+ ~ + A t L y ( p "+ ~ - p " ) + C(At 2),

where Qy is the approximation for the convec- tion~liffusion part of the momentum equations, involving only y-derivatives. Because p"+l is not known during the first stage, the approximation p" +~ ~-p" may be satisfactory, at the cost of some loss of formal accuracy. For first-order accuracy, it is enough to set fib=U~,+l/2, 38=Vg+~/2, U* = U~ +1 , and v* = v[', +1 . In the present work, this simple but satisfactory solution was adopted. More complicated boundary conditions can be imposed using the technique described next, with additional programming effort. Consider the situation when the velocity is to be prescribed, in the first half-step. The discretized boundary conditions take the form

/41,j = (/db)~ ,+ 1 2

½~,,j + ½~:,j = (~O7 + '/:

These boundary values for the auxiliary fields f and ~ can be included in the tridiagonal systems (l 8) and (19) if

d I = 1, a I = 0, RHSj = (Ub)7 + C2

for Equation (18) and

dl = ½, al -- ½, RHS1 = (t'b)~ '+ 12

for Equation (19). If the derivative O u / O x = gb is to be prescribed at the left boundary, then

- - f l , j -{- UZ,j = A x (gb)7 + I /2

which fits into the tridiagonal scheme if dl = - 1 , a l = 1, and R H S a = A x ( g b ) y +1/2. In the computer code, the coefficients for the tridiagonal equations corresponding to the left and right boundaries are calculated in a separate subroutine. The Thomas elimination algorithm is performed a f t e r the bound- ary equations have been formed.

STABILITY CONSIDERATIONS

The purpose of stability analyses is to determine the conditions for which, upon repeated application of the numerical scheme on the initial conditions, the error remains bounded once the time step and mesh spacing have been fixed. In incompressible flow com- putations, instabilities may be generated either in the discretized momentum equation or in the pressure solution. In the algorithm described here, pressure- related instabilities are not important because there is no need for (derived) pressure boundary conditions, and also because the residual divergence is used as the convergence criterion. The greatest problems concerning stability are posed by the explicit approxi- mation for the momentum equation. Because this equation is nonlinear, a complete stability analysis is not available. Nevertheless, approximate stability conditions may be obtained by "freezing the nonlin- earity". A Fourier-von Neumann analysis of the 2-D FTCS scheme gives the following constraints for the maximum permissible time step (e.g. Hirt, Nichols, and Romero, 1975):

Ax 2Ay 2 At ~< 2v (Ax 2 + Ay 2), (25)

2v At ~< max{u2 + t~2}' (26)

At~<min lul' ]~i" (27)

Condition (25) is the most restrictive for highly viscous flows, whereas condition (26) is limiting for convection-dominated flow. Finally, the third inequality is the Courant-Friedrichs-Levy (CFL) condition, which states that material cannot move more than one cell in one time step.

A Fourier-von Neumann analysis of the linearized implicit fractional-step scheme described here, with the pressure gradient and body force terms neglected,

Page 7: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow 271

shows that it is unconditionally stable (see e.g. Mitchell and Griffiths, 1980; Smith, 1985). This is a considerable advantage, especially for the numerical computation of flows with high Reynolds number. Nevertheless, nonlinear instabilities may develop in long-term integrations of high-Re flows or geo- physical flows with small dissipation, when the aliasing phenomenon associated with wavelengths shorter than twice the mesh spacing can lead to significant contamination of the long wavelength solution (e.g. Haltiner and Williams, 1980).

PROGRAM DESCRIPTION

In this section, the computer implementation of the algorithm is described. The graphical representations of the results generated by the calculations are produced by a separate post-processing program, which calls only a few basic graphics primitives. This simplifies the structure of the main solver and gives the user a greater flexibility of operation. The FORTRAN calls to the graphics primitives are compatible with the PLOT88 library (Young and Van Woert, 1990). A FORTRAN interface also is provided, which constructs "*.PLT" files in the format used by the VIEW and PLOT programs of the GRAPHER/SURFER packages by Golden Software.

The computer code was written in four separate files: FDFLO W.INC, FDFLO W.FOR, FPLOT.FOR, and GRFPRIM.FOR. The source code contained in these files is listed in Appendices 1-4. A brief description of the code now will be presented.

FDFLO W.INC

This is an INCLUDE file containing the definitions for the program constants (parameters) and the global variables, and a list with a short description of each variable. The global definitions contained in FDFLOW.INC are used by both the main solver (FDFLOW) and the post processor (FPLOT). The list of global variables is useful as a reference for preparing the input data for a new problem, and for reading and modifying the code.

FDFLO W.FOR

This file contains the main program (FDFLOW) that implements the solution algorithm (see Fig. 4). The program was written in subroutine form, each subroutine performing a single task. At the start of each subprogram unit, there is a short header describ- ing its function. The code contained in this file consists of the following program units:

FDFLO W (main program)

The main program starts with an initialization section, and then prints the initial conditions for the field variables to a disk file. After this initial stage, the program provides cyclic control over the calculation, by calling subroutines ADVANCE, STEP1, STEP2, and PRINTF. The calculation is stopped when the time-to-finish (TWFIN) has been reached.

ADVANCE

This subroutine stores the advanced-time field variables into the corresponding old-time arrays, and increments the calculational time and the cycle counter.

SETUP: Read input data Compute model constants Initialize field uariables

L PRINTF:

Print initial conditions

MAIN CYCLE

nDVANCE: Advance time Update field ~ control variables

t STEPI: Compute temporary

velocity (u.,u.) T

STEP2: Pressure-uelocitg iteration Compute (u n+l ,u n*l ,pn*l )

v PRINTF: Print cgcle information

Print uariables to disk n ?

, (r >: T FI, ]

Figure 4. Structure chart of FDFLOW program.

Page 8: Fdflow a Fortran 77 Solver for 2d Incomp Flow

272 C.M. LEMOS

BCEXP

This subroutine sets the boundary conditions on all mesh boundaries when the velocity is evaluated explicitly, by overwriting the values stored in the layer of fictitious cells surrounding the mesh. This subroutine must be called after the calculation of u * i,j

and v *j with Equations (9) and (10), and after each pressure-velocity iteration. The explicit boundary conditions must be coded by hand for each specific problem. They also must be compatible with the implicit boundary conditions specified in subroutine BC1MP, if the implicit method is selected for the first stage. If FN =if, the explicit boundary conditions must verify Equation (5).

B C I M P

This subroutine sets the boundary conditions for all mesh boundaries when the velocity is calcu- lated implicitly (METHOD =2), using the tech- nique described in the section concerned with the boundary conditions. The implicit boundary con- ditions must be coded by hand for each specific problem, and also must be compatible with the explicit boundary conditions specified in subroutine BCEXP. The arguments for this subroutine are

INDEX: the index which is constant in the tridi- agonal system of equations; this index is J in the first half-step and I in the second half-step, and is required only when the boundary conditions depend on the position along the boundary.

ISTEP: = 1 in the first half-step; =2 in the second half-step.

IVAR: = 1 for u-velocity; = 2 for v-velocity.

P R I N T F

This subroutine outputs the calculational time, the current cycle, and the number of iterations for convergence in the pressure-velocity iteration to terminal or system console. It then tests if the time for the next output has been reached (T >i TWPRT). When this condition is true, the subroutine creates a FLOWnnn.OUT file, and writes the current time, cycle, number of iterations for convergence in the pressure-velocity iteration, and formatted tables of the u,v and p arrays to the disk file. nnn is obtained from the current value of the PRCOUNT integer variable. After the results are written on the disk file, the time for the next printout and the counter for the output files are incremented. The FLOWnnn.OUT output files are used by the post processor to produce graphical representations of the numerical solution.

SETUP

This subroutine opens the input file FDFLOW.DAT, and reads the input parameters needed for each specific calculation. The preparation of the FDFLOW.DAT file for a sample calculation is described in the next section. After the input

parameters are read, the index bounds for the field arrays and some constants necessary to the calcu- lation are initialized. The initial velocity and pressure arrays are initialized with U(I ,J )= UI, V(I ,J)= VI, and P(I,J) = 0 on every interior cell. BCEXP then is called to set the boundary conditions for the first step.

S T E P I

This subroutine computes the temporary solution (u*, v*) from u n v", and p n using either the explicit or the implicit methods as described. Subroutines BCEXP or BCIMP are called to impose the boundary conditions. The temporary solution obtained in STEP1 is advanced to the (n ÷ 1) At time level in the STEP2 subroutine.

STEP2

This subroutine computes the advanced-time velocity and pressure fields u ~ +~, v n +1, and p "+ from the temporary velocity (u*,v*), using Equations (23) and (24).

TRIDA G

This subroutine solves tridiagonal systems of linear equations, using the Thomas algorithm.

FPLOT.FOR

This file contains the post-processing program, FPLOT.FOR, which constructs graphical represen- tations of the computed velocity field. The user needs to specify the name of the FLOWnnn.OUT file to be represented, and a reference velocity VELMX. This latter is used internally to scale the velocity vector. Typically, VELMX should be equal to the maximum velocity expected in the calculation.

The velocity vectors are plotted using two auxiliary routines, CONVERT and DRWVEC. Subroutine CONVERT converts "world" coordinates in the window [XMIN,XMAX] × [YMIN,YMAX] to plot- ting coordinates in the viewpoint [2.83,10.83] x [0.33,8.33], with XMIN = 0, YMIN = 0, XMAX = i , A x , Y M A X = ~ * A y . Subroutine DRWVEC draws a segment between two points, and optionally appends a small tip to the endpoint of the segment (see Appendix 3 for further details). Only five graphi- cal primitives are called. For a detailed description of these primitives, see Young and Woert (1990) or the description of GRFPRIM.FOR presented next. The post processor can be run on a PC by linking with the PLOT88 library, or using the PLOT and VIEW programs by Golden Software using GRFPRIM.FO R.

GRFPRIM.FOR

This file contains six subroutines that construct "*.PLT" files: PLOTS, which opens the drawing file; PLOT, which moves the pen, draws line segments, and closes the plotting file; SYMBOL, which draws a character string; NUMBER, which converts a real

Page 9: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow 273

number to a character string and draws it; COLOR, which changes the current pen color; and F A C T O R , which applies a scale factor to the plot. For the details of operation of each of these subroutines, the reader is referred to the headers following each subprogram definition (Appendix 4). When compiled and linked with FPLOT.FOR, these subroutines will generate a " , . P L T " file that can be viewed on a PC screen, or plotted, using the V I E W / P L O T programs.

Table 1. Input data for test problem

10 IBAR

10 JBAR

1.E-01 DELX

S A M P L E T E S T P R O B L E M

In this section, the details of the operation of the F D F L O W program are described. To help debug the program in new installations, listings of the computed results for a simple test problem will be presented. The test problem considered is the calculation of the flow in a square cavity of unit dimensions driven by the uniform translation of the top lid (see Fig. 5). The remaining boundaries are no-slip.

Table 1 shows the contents of the F D F L O W . D A T file for the test problem. The labels on the right column are included for readability, and are discarded by the SETUP subroutine. A crude mesh of 10 × l0 cells ( IBAR = 10, J B A R = 10) was used. The mesh increments were D E L X = 0.1 and D E L Y = 0.1. The time step was D E L T = 0.01. The kinematic viscosity was N U = 0 . 1 . The over- relaxation factor for the pressure-velocity iteration was O M G = 1.5 and the tolerance for the maximum cell divergence was EPSI = 10 3. The time-to-finish was T W F I N = 3.0 sec and the solution was printed with an interval of P R T D T = 1.0 sec. The boundary conditions were programmed by hand in subroutines BCEXP and BCIMP. These conditions correspond to the cavity flow calculation reported in Greenspan and Casulli (1988). Before starting a new calculation, the user must check if the dimensions of the arrays specified in the P A R A M E T E R statement in the FDFLOW.INC file are appropriate for the desired number of mesh cells. If this is not the situation, this statement must be changed and the code recompiled. The maximum permissible time step also must be checked, if the explicit method is to be used.

After about 1 sec, the flow became steady. Tables 2, 3, and 4 and show listings of the computed u,v,

Y u : ( t ,0 )

P

u = (o,o) u = (0,o)

! x

u = (0,o)

Figure 5. Definition sketch for cavity flow pattern.

1 .E-01 DELY

1 . 0 - 0 2 DELT

O.OE+O0 UI

O.OE+O0 VI

3 .00 T~FIN

1 .0 PRTDT

1.5 OMG

1.OE-01 NU

O.OE+O0 GX

O.OE+O0 GY

1 .0E-03 EPSI

1 METHOD

and p fields. Figure 6 shows a graphical represen- tation of the final steady solution. The results are virtually identical to those reported by Greenspan and Casulli (1988), the differences being the result of their use of nonconservative differencing for the convection terms. This sample test problem took 13sec (including disk I/O) on a 486DX/33MHz- based PC, using the Microsoft F O R T R A N compiler (version 5.0).

APPLICATIONS

In order to test the accuracy and efficiency of the computer program, two numerical studies were performed: the calculation of lid-driven cavity flow at high Reynolds number, and the calculation of the flow past a backward-facing step for three different Reynolds numbers. This last problem will serve to illustrate how to define internal obstacles and outflow boundary conditions by simple changes in the code.

Cavity )qow at high ReynoMs number

The computation of lid-driven cavity flows at high Reynolds number has become a " 'standard" for testing new algorithms for incompressible flows. From the physical viewpoint the problem is of considerable importance, for it provides a typical example of separated flow which displays several ranges of behavior of the Navier Stokes equations.

Page 10: Fdflow a Fortran 77 Solver for 2d Incomp Flow

274 C. M. LEMOS

Table 2. Calculated horizontal velocity for test problem, T = 2.0 sec

I= U ( I , J )

1 2 3 4 5 6 7 8 9 10

J= 12 J= ii J= I0 J= 9 J= 8 J= 7 J= 6 J= 5 J= 4 J= 3 J= 2 J= I

2 .000 1 .736 1.521 1.392 1.324 1.298 1 .309 1 .363 1 .485 1.711 0 .000 0 .264 0 .479 0 .608 0 .676 0 .702 0.691 0 .637 0 .515 0 .289 0 .000 - 0 . 0 2 7 0 .053 0.151 0 .222 0 .252 0 .238 0 .177 0 .077 - 0 . 0 1 6 0 .000 - 0 . 0 6 0 - 0 , 0 7 8 - 0 . 0 5 8 - 0 . 0 3 2 - 0 . 0 2 0 - 0 . 0 3 0 - 0 . 0 5 8 - 0 . 0 8 4 - 0 . 0 6 9 0 .000 - 0 . 0 5 2 - 0 . 1 0 6 - 0 . 1 3 5 -0 .147 -0 .152 - 0 . 1 5 4 - 0 . 1 5 0 - 0 . 1 2 4 - 0 . 0 6 5 0 .000 -0 .041 -0 .101 - 0 . 1 5 0 -0 .181 - 0 . 1 9 4 - 0 . 1 9 0 - 0 . 1 6 6 - 0 . 1 1 8 - 0 . 0 5 0 0 .000 - 0 . 0 3 2 - 0 . 0 8 6 -0 .137 -0 .173 -0 .188 - 0 . 1 8 0 - 0 . 1 4 9 - 0 . 0 9 7 - 0 . 0 3 6 0 .000 - 0 . 0 2 4 - 0 . 0 6 8 - 0 . 1 1 4 -0 .146 - 0 . 1 6 0 -0 .151 - 0 . 1 2 0 - 0 . 0 7 4 - 0 . 0 2 5 0 .000 - 0 . 0 1 7 - 0 . 0 5 0 - 0 . 0 8 6 - 0 . 1 1 3 - 0 . 1 2 4 - 0 . 1 1 5 - 0 . 0 8 9 - 0 . 0 5 2 - 0 . 0 1 7 0 .000 - 0 . 0 1 0 - 0 . 0 3 2 - 0 . 0 5 6 - 0 . 0 7 6 - 0 . 0 8 3 - 0 . 0 7 7 - 0 . 0 5 8 - 0 . 0 3 2 - 0 . 0 1 0 0 .000 - 0 . 0 0 2 -0 .011 - 0 . 0 2 2 -0 .031 - 0 . 0 3 5 - 0 . 0 3 2 - 0 . 0 2 3 -0 .011 - 0 . 0 0 2 0 .000 0 .002 0.011 0 .022 0.031 0 .035 0 .032 0 .023 0 .011 0 .002

From the viewpoint of the numerical computation, this is a good test problem because of its simple boundary conditions. The previous numerical studies have shown that a large primary vortex is formed near the center of the cavity, together with secondary and tertiary vortices near the corners. The complexity of the flow topology and of the transient dynamics increases with the Reynolds number.

To test the accuracy and efficiency of the present algorithm, a numerical simulation of cavity flow with Re = 10,000 was performed. The viscosity was set to v = 10 4m2sec- ' , and a mesh with 100 x 100 cells with Ax = A y = 1 0 - : m was selected. This spatial resolution was adequate to capture the details of the boundary layers. The good stability properties of the implicit method allowed the use of a time step At = 10 -2 (corresponding to a Courant number of 1 near the top lid). Because the flow evolves slowly, the time-to-finish was set to 70 sec. No changes were needed in the coded boundary conditions.

Figure 7 shows several "snapshots" of the flow, which illustrate the complexity of the transient behavior. The qualitative development of the flow was emphasized by dividing the velocities by their magnitudes. It is observed that a large primary vortex

was formed near the center of the cavity. Most of the kinetic energy of the flow was concentrated in this primary vortex. Three secondary vortices were formed near the bot tom corners and the upper- left corner, which are associated physically with the separation of the decelerating boundary layers (Lighthill, 1963). Near the bot tom corners, the velocities were small, the flow evolving close to the Stokes regime. The computed solution also showed tiny tertiary vortices adjacent to the bot tom corners. The appearance of this sequence of eddies of decreas- ing strength was reported in the numerical work of Gresho and others (1984).

In the sample test problem discussed in the pre- vious section, the number of pressure iterations was large during the first few time steps, and then decayed rapidly in a monotonic way. Because for Re = 10 the flow is diffusion-dominated, the solution evolved quickly to the steady state. The behavior of the solution for Re = 10,000 was qualitatively different. The number of iterations needed to satisfy the conti- nuity equation decayed more slowly, and usually increased for a few cycles. Such behavior corresponds to the slow formation and development of the vortex structures, followed by the nonlinear interaction between them. This calculation took 3 h and 5 min

Table 3. Calculated vertical velocity ~ r test problem, T = 2.0 sec

V(I,J) I= 1 2 3 4 5 6 7 8 9 I0

J= 12 J= 11 J= 10 J= 9 J= 8 J= 7 J= 6 J= 5 J= 4 J= 3 J= 2 J= 1

0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000

- 0 . 2 6 4 0 .264 0 .215 0 .129 0 .068 0 .026 -0 .011 - 0 . 0 5 4 - 0 . 1 2 2 - 0 . 2 2 6 - 0 . 2 3 8 0 .238 0 .295 0 .226 0 .139 0 .056 - 0 . 0 2 5 - 0 . 1 1 5 - 0 . 2 2 1 - 0 . 3 1 8 - 0 . 1 7 7 0 .177 0 .277 0 .246 0 .165 0 .068 - 0 . 0 3 5 - 0 . 1 4 4 - 0 . 2 4 7 - 0 . 3 0 3 - 0 . 1 2 5 0 .125 0 .224 0 .217 0 .154 0 .064 - 0 . 0 3 8 - 0 . 1 4 0 - 0 . 2 2 2 - 0 . 2 4 4 - 0 . 0 8 4 0 .084 0 .163 0 .168 0 .123 0 .050 - 0 . 0 3 4 - 0 . 1 1 6 - 0 . 1 7 3 - 0 . 1 7 6 - 0 . 0 5 2 0 .052 0 .109 0 .117 0 .088 0 .035 - 0 . 0 2 7 - 0 . 0 8 4 - 0 . 1 2 0 - 0 . 1 1 5 - 0 . 0 2 8 0 .028 0 .064 0 .072 0 .055 0 .022 - 0 . 0 1 8 - 0 . 0 5 4 - 0 . 0 7 4 - 0 . 0 6 7 - 0 . 0 1 2 0 .012 0.031 0 .036 0 .028 0.011 - 0 . 0 1 0 - 0 . 0 2 8 - 0 . 0 3 7 - 0 . 0 3 2 - 0 . 0 0 2 0 .002 0 .009 0.011 0 .009 0 .004 - 0 . 0 0 3 - 0 . 0 0 9 - 0 . 0 1 2 - 0 . 0 0 9

0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000 0 .000

Page 11: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow

Table 4. Calculated pressure for test problem, T = 2.0 sec

275

P ( I , J ) I= 1 2 3 4 5 6 7 8 9 10

J= 12 J= 11 J= 10 J= 9 J= 8 J= 7 J= 6 J= 5 J= 4 J= 3 J= 2 J= 1

0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 - 1 . 9 1 5 - 0 . 8 7 6 - 0 . 4 5 8 - 0 . 2 7 2 - 0 . 1 6 2 - 0 . 0 5 7 0 . 0 9 3 0 . 3 6 5 0 . 9 1 0 0 . 0 0 0 - 1 . 0 6 4 - 0 . 7 4 0 - 0 . 4 8 8 - 0 . 3 1 9 - 0 . 1 9 2 - 0 . 0 6 5 0 . 0 9 8 0 . 3 3 8 0 . 6 9 0 0 . 0 0 0 - 0 . 6 0 4 - 0 . 5 2 5 - 0 . 4 0 4 - 0 . 2 9 1 - 0 . 1 8 5 - 0 . 0 7 0 0 . 0 6 9 0 . 2 4 1 0 . 4 3 0 0 . 0 0 0 - 0 . 3 5 0 - 0 . 3 4 9 - 0 . 2 9 9 - 0 . 2 3 1 - 0 . 1 5 3 - 0 . 0 6 3 0 . 0 4 0 0 . 1 5 0 0 . 2 4 0 0 . 0 0 0 - 0 . 2 0 6 - 0 . 2 2 7 - 0 . 2 1 0 - 0 . 1 7 0 - 0 . 1 1 6 - 0 . 0 5 2 0 . 0 1 8 0 . 0 8 2 0 . 1 2 0 0 . 0 0 0 - 0 . 1 2 5 - 0 . 1 5 0 - 0 . 1 4 6 - 0 . 1 2 3 - 0 . 0 8 7 - 0 . 0 4 3 0 . 0 0 2 0 . 0 3 7 0 . 0 4 9 0 . 0 0 0 - 0 . 0 8 4 - 0 . 1 0 7 - 0 . 1 0 8 - 0 . 0 9 4 - 0 . 0 6 9 - 0 . 0 3 9 - 0 . 0 1 0 0 . 0 1 0 0 . 0 1 1 0 . 0 0 0 - 0 . 0 7 0 - 0 . 0 8 7 - 0 . 0 8 9 - 0 . 0 7 9 - 0 . 0 6 0 - 0 . 0 3 7 - 0 . 0 1 6 - 0 . 0 0 3 - 0 . 0 0 5 0 . 0 0 0 - 0 . 0 7 2 - 0 . 0 8 4 - 0 . 0 8 6 - 0 . 0 7 6 - 0 . 0 5 7 - 0 . 0 3 5 - 0 . 0 1 6 - 0 . 0 0 5 - 0 . 0 0 6 0 . 0 0 0 - 0 . 0 8 3 - 0 . 0 9 3 - 0 . 0 9 4 - 0 . 0 8 2 - 0 . 0 6 0 - 0 . 0 3 2 - 0 . 0 0 8 0 . 0 0 4 0 . 0 0 3 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0

to complete on a PC-compat ib le 486 machine run- ning at 33 MHz.

Flow past a backward-facing step, at Re = 100, 1000, and 10,000

The third problem considered here is the compu- ta t ion of the flow past a backward-facing step (Fig. 8). For a given step geometry the propert ies of the flow, part icularly the poin t of rea t tachment , are a funct ion of the Reynolds number . Such propert ies are impor t an t in the numerical model ing of the behavior of heat exchangers, reactors, combus t ion machines, and many practical problems concerning internal flows. A similar problem encountered in river

and coastal engineering is the scouring caused by artificial structures.

This problem will serve to il lustrate how the pro- gram can be adapted to treat new problems by simple changes in the code. In this application, it was required to (i) impose the boundary condi t ion u = (1,0) at the inlet; (ii) block certain internal cells in the mesh, in order to define the geometry of the step; and (iii) formulate an outflow (radiative) condi t ion on the r ight boundary . To study the influence of the Reynolds number on the flow, three different simu- lat ions were performed, with v = 10 2, v = 10 -3. and v = 10 -4 (v = i /Re). The computa t iona l domain was discretized using a mesh of 120 x 20 ceils for the first

T = 2 . O0

CYCLE=2OO

Ro = I O

VELMX=I.@O

7

t

1

," ¢

,¢- ,¢"

1

1

i

Figure 6. Steady-state velocity field obtained in test problem.

Page 12: Fdflow a Fortran 77 Solver for 2d Incomp Flow

8

0

• 3os OL pue '0g 'gE '01 = 1 aoj '000'01 = o~I ql!A~ molqoad ,~o U /~I!A~,O aoj aOlO0A ~(I!OOI0A poz!lv, tuao N L ~ang!::I

, , , , , , , , , , , , . . - ~ . . . . . . , , , , , , , , , , , , , , , . . . . . . . . . . ~ , , , , . . . . . . . . . . . . , , , , , , , , , , ,

l~lllllllllh,, ..... "-=~:"';'~IIi@llillil ~'=~° ""' ...... "==" "'""""'"

l [ tF t~v . . .~ . . '~ : : : : : ; , ~ :: - - - : ~ . . . . %~,,,'vvvHz~l

~._ . . . . : : : : : .. '. : : : : : : : : : : : s s s ; . - . - ; y , ~.~Z~2 , . ~ ~t t~.- -" . . . . ~ " . . . . . . . . ~ s s ~ . ; ; ; , , , , , ; . ~ , , , ~ ~c_,] IkE , . . . . . . : " : . . . . . .~,{'{SS~l ,,. . ............ ,, -- LO'OL = 1 , ~ T T - : : ! : ! . . . . . . . , "z: ~ i . ~

h~--~.~ '~YT,; ;s . . . . . . . : : : : . . . . . ~ . . . . - . . . . ~1 ~ z ' = x " ] ~ ^ 1 > . ~ : : _ ~ - ' = - - ' - ~ - - - I,...--:

tl

~x~xx~xxxx~.......:...~_.:~ . . % / / H H Z I U I I ~ l / Oosg=3qg'kg/ ~ x x x x ~ . . . . . . ~ . ' / / / l / l l l / ~ l

"~',',",~,",'-.".\',. '.".~.~HQ~.~.~ /1/111,11~11 ' i~X,<~. . .~ . . .~7__~.~z ,¢/ l / / l l l l iJ / l l

[ . ~______________ i

S L ' = X W q 3 A

0000L = ~

LOOS=37~D

LO "0S = ±

0000L =

9Z'=XWq3A

oa!

000L=37DID

cq 00"0L = I

Page 13: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow

= (0 ,0 }

i

L s

= 10,0} I H s

L d

: ( 0 , 0 )

H d

L d = 12 H d = 2 L s = 1.4 H s = 1

Figure 8. Definition sketch for problem of flow past backward-facing step.

q

C)

',--, s -I1

I

277

two simulations, and a mesh of 240 x 40 cells for the simulation with Re = 10,000. The time step was At = 10 -2, and the calculations were done using the implicit scheme.

The step was created by blocking those cells for which I ~< IBAR/5 + 1 and J ~< JBAR/2 + 1. To gen- erate the outflow boundary, the conditions Ou/Sx = 0

and Ov/6y = 0 were imposed during the implicit momentum calculation, but not during the pressure iteration (following the method of Hirt, Nichols, and Romero, 1975). The definition of the internal obstacle and special boundary conditions was done as follows. In subroutine BCEXP, the statements for prescribing U ( I M A X - l , J ) and V(IMAX,J) were eliminated, because during the pressure iteration these boundary conditions were not applied. Also in BCEXP, another loop was added to impose U(I,J) = 0 and V(I,J) = 0

for I ~< IBAR/5 + ! and J ~< JBAR/2 + 1. In sub- routine BCIMP, a statement was added in the block ISTEP = I / IVAR = 1 to put R H S X ( I ) = 1.0 for I N D E X > J B A R / 2 + 1, fixing the boundary condition at the inlet. Also in BCIMP, the homo- geneous Neumann conditions for u and v were im- posed on the outlet boundary, using the method as described. In subroutine STEP1, it was necessary to add the statements D D X ( I ) = 1 . E + 3 0 and DDY(J) = 1.E + 30 whenever I ~< IBAR/5 + 1 and J ~< JBAR/2 + 1. This simple procedure effectively sets U(I,J) = 0 and V(I,J) = 0 for all cells defining the step. In subroutine STEP2, a statement was included in the main loop, to skip all obstacle cells.

Figure 9 shows the computed velocity distri- butions, each one corresponding to a different Reynolds number. It is observed that as the Reynolds

Be : ] 0 0

Fie ~]OOO • .

Be ~10@OO

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . : : : . . . . . . ; i l i . . . . . . . . . . : i i i ] i ] ] ] i Z 2 : 2 i i 2 ] : . . ? i ~ ? ~ i ! i i ] i ! i ] i } ] : : ! ! Z i i : : i ! ]

Figure 9. Flow past backward-facing step, for Re = 100 (top), Re = 1000 (middle), and Re = 10.000 (bottom).

Page 14: Fdflow a Fortran 77 Solver for 2d Incomp Flow

278 C.M. LEMOS

number increased from 100 to 1000, the point of reattachment moved away from the step. For R e = 100, the flow adapted itself quickly to the expansion. The velocity profile was nearly parabolic at the end of the step, and near the open boundary. For Re = 1000, the pattern of the eddying motion downstream of the step became more complicated, and the flow separated from the upper boundary. For R e = 10,000 the flow pattern, became even more complicated. The main stream was dispersed by meandering around a series of strong vortices. The interaction between the main stream and the vortex system created an intricate transient. In fact, the flow represented in Figure 9 was highly unsteady.

SUMMARY

A F O R T R A N - 7 7 program for the computat ion of 2-D incompressible flows was presented. The system was designed to be operated on PCs using low-cost software, for both educational and pro- fessional purposes. Educational applications en- compass testing different numerical schemes for the Navier-Stokes equations, illustrating the influence of the Reynolds number on the flow pattern, in- stability and separation, and programming issues. Professional applications include the study of steady flows in channels and ducts, and the investigation of scouring resulting from currents near coastal pro- tection structures. To help new users debug the code, the numerical results for a simple test case were described in detail.

The quality and flexibility of the program were illustrated by the results obtained in two classical test problems, lid-driven cavity flow and flow past a sudden expansion of a duct, with Reynolds numbers up to 10,000. The fine details of the solutions, as well as the complexity of the transient behavior at high Reynolds number, were captured well. All simu- lations reported in this work were calculated using an 80486-based PC. The calculations also illustrated how the program can be changed to meet practical needs (e.g. definition of new geometries or new types of boundary conditions).

Because of its modular structure, the code described herein can be used as a foundation for the development of more sophisticated versions. For example, new physical phenomena, such as thermal coupling, surface and internal gravity waves, etc. can be introduced by adding new global variables, and new subroutines to solve the transport equations for the new fields. At the time of writing, work is under way to develop a version of the present algor- ithm for Boussinesq fluids with an arbitrary equation of state and distributed heat sources, incorporating several types of boundary conditions. This type of model can be useful for studying the evolution of the

near and intermediate field of plumes, either in the atmosphere or in the ocean. The present model also can be extended to treat 3-D flows in a straight- forward way.

Acknowledgments--The author wants to express his grati- tude and appreciation to Prof. Daniel A. Rodrigues for his incentive and encouragement, and also for his helpful and stimulating discussions.

REFERENCES

Bulgarelli, U., Casulli V., and Greenspan, D., 1984, Pressure methods for the numerical solution of free surface fluid flows: Pineridge Press, Swansea, 323 p.

Chorin, A. J., 1968, Numerical solution of the Navier Stokes equations: Mathematics of Computation, v. 22, p. 742-762.

Chorin A. J., and Marsden, J., 1979, A mathematical introduction to fluid mechanics: Springer Verlag, New York, 168 p.

Greenspan, D., and Casulli, V., 1988, Numerical analysis for applied mathematics, science and engineering: Addiso~Wesley Publ. Co., New York, 341 p.

Gresho, P. M., Chan, S. T., Lee, R. L., and Upson, C. D., 1984, A modified finite element method for solving the time-dependent, incompressible Navie~ Stokes equations: Int~. a. Jour. Num. Meth. in Fluids, v. 4, no. 7, p. 619~i40.

Gustafson, K. E., 1987, Introduction to partial differential equations and Hilbert space methods (2nd ed.): John Wiley & Sons, New York, 409 p.

Haltiner, G. J., and Williams, R. T., 1980, Numerical prediction and dynamic meteorology. (2nd ed.): John Wiley & Sons, New York, 477 p.

Harlow, F. H., and Welsh, J. E., 1965, Numerical calcu- lation of time-dependent viscous incompressible flow: Physics of Fluids, v. 8, p. 2182-2189.

Hirt, C. W., Nichols, B. D., and Romero, N. C., 1975, SOLA: a numerical solution algorithm for transient fluid flows: Los Alamos Scientific Lab. Rept. LA-5852, 50 p.

Leonard, B. P., 1979, A stable and accurate convective modelling procedure based on quadratic upstream interpolation: Comp. Meth. Appl. Mech. Eng., v. 19, no. 1, p. 59-98.

Lighthill, M. J., 1963, in Rosenhead, L., Ed. Laminar boundary layers: Oxford Univ. Press, London, p. 687.

Mitchell, A. R., and Griffiths, D. F., 1980, The finite difference method in partial differential equations: John Wiley & Sons, London, 267 p.

Peyret, R., and Taylor, T. D., 1983, Computational methods for fluid flow: Springer Verlag, New York, 358 p.

Quartapelle, L., 1992, Solution of the time-dependent incompressible Navier-Stokes equations by finite elements: Lecture Series 1992-04, von-K~irmfin Institute for Fluid Dynamics, Rhode Saint Gen6se-Belgium, 154p.

Roache, P., 1972, Computational fluid dynamics: Hermosa Publ., Albuquerque, New Mexico, 446 p.

Smith, G. D., 1985, Numerical solution of partial differen- tial equations: finite difference methods: Oxford Univ. Press, Oxford, 337 p.

Viecelli, J. A., 1971, A computing method for incompress- ible flows bounded by moving walls: Jour. Comp. Physics, v. 4, no. 1, p. 119-143.

Young, T. L., and Van Woert, M. L., 1990, Plot88 Software Library Reference Manual (3rd. ed.): Plot Works Inc., Ramona, California, 342 p.

Page 15: Fdflow a Fortran 77 Solver for 2d Incomp Flow

C C C C C C C C C C C C C C C C C C C C C C C C C C

C C C C C C C C C C C C C C C

FORTRAN solver for 2-D incompressible fluid flow

A P P E N D I X 1

FDFLO W.INC Listing

: : : : : : : : : : : : : : : : : : : : : : : : : : FILE "FDFLO~.INC" : ========================= C INCLUDE f i l e c o n t a i n i n g g l o b a l v a r i a b l e s f o r FDFLOW.FOR and FPLOT.FOR

PARAMETER (IBAR2=IO2,JBAR2:102) PARAMETER (I0=9) REAL NU REAL.8 T INTEGER.4 CYCLE,PRCOUNT COMMON UN(IBAR2,JBAR2),VN(IBAR2,JBAR2),U(IBAR2,JBAR2),

& V(IBAR2,JBAR2),P(IBAR2,JBAR2),XFLUX(IBAR2,JBAR2), YFLUX(IBAR2,JBAR2),IBAR,JBAR,IMAX,JMAX, IM1,JM1,DELX,DELY,RDX,RDY,DELT,UI,VI,T~FIN,TWPRT,

& PRTDT,OMG,BETA,T,ITER,CYCLE,NU,GX,GY,EPSI,METHOD,PRCOUNT, & BBX(IBAR2),DDX(IBAR2),AAX(IBAR2),RHSX(IBAR2), & BBY(JBAR2),DDY(JBAR2),AAY(JBAR2),RHSY(JBAR2),SX,SY, & DTDX,DTDY

C INPUT PARAMETERS (READ FROM FILE "FDFLO~.DAT") :

IBAR : JBAR : DELX : DELY : DELT : UI VI TNFIN : PRTDT : OMG NU GX GY EPSI : METHOD :

NUMBER OF CELLS IN THE X-DIRECTION NUMBER OF CELLS IN THE Y-DIRECTION MESH SPACING ALONG THE X-DIRECTION MESH SPACING ALONG THE Y-DIRECTION TIME STEP X-COMPONENT OF VELOCITY USED FOR INITIALIZING MESH Y-COMPONENT OF VELOCITY USED FOR INITIALIZING MESH PROBLEM TIME TO END CALCULATION TIME INCREMENT BETWEEN SUCCESSIVE OUTPUTS OVER-RELAXATION FACTOR USED IN THE PRESSURE ITERATION KINEMATIC VISCOSITY BODY ACCELERATION ALONG POSITIVE X-DIRECTION BODY ACCELERATION ALONG POSITIVE Y-DIRECTION PRESSURE ITERATION CONVERGENCE CRITERION =1 FOR EXPLICIT MOMENTUM CALCULATION =2 FOR IMPLICT MOMENTUM CALCULATION

OTHER GLOBAL VARIABLES :

U

V P UN VN XFLUX : YFLUX : IMAX : JMAX : IM1 JM1 RDX RDY BETA : T ITER : CYCLE : PRCOUNT: BBX DDX AAX

VELOCITY COMPONENT IN THE X-DIRECTION, TIME LEVEL N+I VELOCITY COMPONENT IN THE Y-DIRECTION, TIME LEVEL N+I PRESSURE VELOCITY COMPONENT IN THE X-DIRECTION, TIME LEVEL N VELOCITY COMPONENT IN THE Y-DIRECTION, TIME LEVEL N AUXILIARY ARRAY, USED TO STORE TRANSPORT FLUXES AUXILIARY ARRAY, USED TO STORE TRANSPORT FLUXES TOTAL NUMBER OF CELLS IN THE X-DIRECTION TOTAL NUMBER OF CELLS IN THE Y-DIRECTION = IMAX-1 = JMAX-1 = 1.0/DELX = 1.0/DELY PRESSURE ITERATION RELAXATION FACTOR PROBLEM TIME PRESSURE ITERATION COUNTER COMPUTATIONAL TIME CYCLE OUTPUT FILE COUNTER, USED TO NAME OUTPUT FILES AUXILIARY VECTOR FOR TRIDIAGONAL ELIMINATION AUXILIARY VECTOR FOR TRIDIAGONAL ELIMINATION AUXILIARY VECTOR FOR TRIDIAGONAL ELIMINATION

279

CAGEO 20/3--B

Page 16: Fdflow a Fortran 77 Solver for 2d Incomp Flow

280 C.M. LEMOS

RBSX BBY DDY AAY RHSY DTDX DTDY SX SY

: AUXILIARY VECTOR FOR TRIDIAGONAL ELIMINATION : AUXILIARY VECTOR FOR TRIDIAGONAL ELIMINATION : AUXILIARY VECTOR FOR TRIDIAGONAL ELIMINATION : AUXILIARY VECTOR FOR TRIDIAGONAL ELIMINATION : AUXILIARY VECTOR FOR TRIDIAGONAL ELIMINATION : = DELT/DELX : = DELT/DELY : = NU*DELT/(DELX*DELX) : = NU*DELT/(DELY*DELY)

PROGRAM CONSTANTS (PARAMETERS) :

IBAR2 JBAR2 IO

: MAXIMUM NUMBER OF CELLS IN THE X-DIRECTION : MAXIMUM NUMBER OF CELLS IN THE Y-DIRECTION : LOGICAL UNIT FOR INPUT/0UTPUT

: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

A P P E N D I X 2

F D F L O W . F O R L i s t i n g

C . . . . . . . . . . . . . . . . . . . . . == PROGRAM "FDFLOW.FOR" : . . . . . . . . . . . . . . . . . . . . . . . .

C=:=== A FORTRAN p r o g r a m f o r c o m p u t i n g i n c o m p r e s s i b l e f l u i d f l o w s ======

C C C

10 C C C

PROGRAM FDFLOW INCLUDE ~FDFLOW.INC ~

** I N I T I A L I Z E VARIABLES AND PRINT INITIAL DATA

CALL SETUP CALL PRINTF

** START MAIN CYCLE

CONTINUE

** ADVANCE TIME

CALL ADVANCE

** COMPUTE TEMPORARY VELOCITIES

CALL STEP1

** COMPUTE ADVANCED-TIME VELOCITIES AND PRESSURE

CALL STEP2

** PRINT CYCLE INFORMATION k FIELD VARIABLES

CALL PRINTF I F (T.GT.TWFIN) STOP ~End o f P r o g r a m ~ GOTO 10 END

SUBROUTINE ADVANCE INCLUDE ~FDFLOW.INC ~

** ADVANCE TIME

DO 10 I= I , IMAX DO 10 J=I ,JMAX

Page 17: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow 281

10

U N ( I , J ) = U ( I , J ) V N ( I , J ) = V ( I , J ) CONTINUE T=T+DELT CYCLE=CYCLE+I RETURN END

200 C

500

SUBROUTINE BCEXP INCLUDE ~FDFLOW.INC ~

** SET BOUNDARY CONDITIONS WHEN VELOCITY IS CALCULATED EXPLICITLY ** BC'S MUST BE CODED BY HAND FOR EACH SPECIFIC PROBLEM

DO 200 J=I,JMAX U ( 1 , J ) : O . O V ( 1 , J ) = - V ( 2 , J ) U( IM1, J )=O.O V ( I M A X , J ) = - V ( I M 1 , J ) CONTINUE

DO 500 I=I,IMAX V( I , JM1)=O.O U ( I , J M A X ) = 2 . 0 - U ( I , J M 1 ) V ( I , 1 ) = O . O U ( I , 1 ) = - U ( I , 2 ) CONTINUE RETURN END

SUBROUTINE BCIMP(INDEX,ISTEP,IVAR) INCLUDE ~FDFLOW.INC ~ INTEGER INDEX,ISTEP,IVAR

** SET BOUNDARY CONDITIONS WHEN VELOCITY IS CALCULATED IMPLICITLY ** BC~S MUST BE CODED BY HAND FOR EACH SPECIFIC PROBLEM

INDEX IS J IN THE FIRST HALF-STEP AND I IN THE SECOND HALF-STEP ISTEP IS 1 IN THE FIRST HALF-STEP AND 2 IN THE SECOND HALF-STEP IVAR IS 1 FOR U-VELOCITY, AND 2 FOR V-VELOCITY

IF ( I ST E P .E Q .1 ) THEN IF ( IVAR.Eq .1 ) THEN

DDX(1)=I.O AAX(1)=O.O RHSX(1)=O.O DDX(IMI)=I.O BBX(IM1)=O.O RHSX(IM1)=O.O

ELSEIF (IVAR.EQ.2) THEN DDX(1) :O.5 AAX(1)=0 .5 RHSX(1):O.O DDX(IMAX)=O.5 BBX(IMAX)=0.5 RHSX(IMAX):O.O

ENDIF ENDIF IF ( I S T E P . E Q . 2 ) THEN

IF ( IVAR.EQ.1) THEN DDY(1)=0.5 AAY(1)=0.5 RHSY(~)=O.O DDY(JMAX)=0.5 BBY(JMAX)=0.5 RHSY(JMAX)=I.0

Page 18: Fdflow a Fortran 77 Solver for 2d Incomp Flow

282 C.M. LEMOS

ELSEIF ( IVAR.Eq .2 ) THEN DDY(1)=I.O AAY(1)=O.O RHSY(1)=O.O DDY(JM1)=I.O BBY(JM1)=O.O RBSY(JM1)=O.O

ENDIF ENDIF RETURN END

20

40 41 42 46 47 48 50

SUBROUTINE PRINTF INCLUDE 'FDFLOW.INC' CHARACTER*II FILENAME

** PRINT CYCLE INFORMATION AND FIELD VARIABLES

WRITE ( * , 5 0 ) ITER,T,CYCLE IF (CYCLE.LE.O.OR.T+I.E-O6.GE.TWPRT) THEN FILENAME=' ' FILENAME(I:4)='FLOW' ~RITE(FILENAME(5:7),46) PRCOUNT+IO0 FILENAME(8:II)='.OUT ~ OPEN(IO,FILE=FILENAME,STATUS=~UNKNOWN ' ) WRITE ( 1 0 , 4 0 ) ITER WRITE (I0,41) T WRITE (I0,42) CYCLE WRITE(IO,47) DO 20 I=I,IMAX DO 20 J=I,JMAX WRITE(IO,48) I , J ,U( I , J ) ,V( I , J ) ,P ( I , J ) CONTINUE CLOSE(IO) TWPRT=TWPRT+PRTDT PRCOUNT=PRCOUNT+I ENDIF RETURN FORMAT(IS,' ITER') FORMAT(F8.3, ~ T ~ ) FORMAT(IS,' CYCLE') FBRMAT(I3) F O R M A T ( 3 X , ' I ' , 3 X , ' J ' , 7 X , ~ U ' , 1 2 X , ' V ~ , 1 3 X , ' P ' ) FORMAT(2(IX,13),3(IX,IPE12.5)) FORMAT(IOX,'ITER=',I5,1OX,'TIME=',IPEI2.5,1OX,'CYCLE=',I6) END

SUBROUTINE SETUP INCLUDE 'FDFLOW.INC'

** READ INPUT PARAMETERS

OPEN(IO,FILE:'FDFLOW.DAT',STATUS=~OLD ' ) READ ( I 0 , . ) IBAR READ ( I 0 , . ) JBAR READ ( I O , . ) DELX READ ( I O , . ) DELY READ ( I O , . ) DELT READ ( I O , . ) UI READ ( I O , . ) VI READ ( I 0 , . ) TWFIN READ ( I O , . ) PRTDT READ ( I O , . ) OMG READ ( I 0 , . ) NU READ ( I O , . ) GX READ ( I 0 , . ) GY

Page 19: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow 283

10

1000 C C C

10

20

READ ( I O , . ) EPSI READ ( I O , . ) METHOD CLOSE(IO)

** COMPUTE MODEL PARAMETERS AND INITIALIZE CONTROL VARIABLES

IMAX=IBAR+2 JMAX=JBAR+2 IMI=IMAX-1 JMI=JMAX-1 RDX=I.O/DELX RDY=I.0/DELY T=O.O ITER=O CYCLE=O T~PRT:O.O BETA=OMG/(2.0,DELT,(RDX,RDX+RDY,RDY)) PRCOUNT=O SX=NU*DELT*RDX**2 SY=NU*DELT*RDY**2 DTDX=DELT*RDX DTDY=DELT*RDY

** SET INITIAL U, V, AND P FIELDS

DO 10 I=2,IM1 DO 10 J=2,JM1 U( I , J )=UI V ( I , J ) : V I P( I , J )=O.O CONTINUE CALL BCEXP RETURN END

SUBROUTINE STEP1 INCLUDE 'FDFLOW.INC ~

** COMPUTES TEMPORARY VELOCITIES USING THE OLD PRESSURE GRADIENT

GOTO (1000,2000) METHOD CONTINUE

** EXPLICIT METHOD (METHOD = 1)

DO 10 J=I,JM1 DO 10 I=I,IM1 XFLUX(I+I,J)= P ( I+ l , J )+O.25* (U N ( I , J )+U N ( I+ l , J ) )**2

& -NU.RDX.(UN(I+I ,J) -UN(I ,J) ) YFLUX(I,J+I)= 0 . 2 5 . ( V N ( I , J ) + V N ( I + I , J ) ) . ( U N ( I , J ) + U N ( I , J + I ) )

-NU*RDY*(UN(I,J+I)-UN(I,J)) CONTINUE DO 20 J=2,JM1 DO 20 I=2,IMI-1 U(I,J):UN(I,J)+DELT*(-RDX*(XFLUX(I+I,J)-XFLUX(I,J))

-RDY*(YFLUX(I,J+I)-YFLUX(I,J))+GX) CONTINUE DO 30 J=l,JM1 DO 30 I=I,IM1 XFLUX(I+I,J)= 0 .25* (VN( I , J )+V N ( I+ I , J ) )* (U N ( I , J )+U N ( I , J+ I ) )

-NU.RDX.(VN(I+I ,J)-VN(I ,J)) YFLUX(I,J+I)= P( I , J+1)+O.25 . (VN(I , J )+VN(I , J+1) )**2

& -NU.RDY.(VN(I ,J+I)-VN(I ,J)) 30 CONTINUE

DO 40 J=2,JMI-1 DO 40 I=2,IM1 V(I,J)=VN(I,J)+DELT.(-RDX.(XFLUX(I+I,J)-XFLUX(I,J))

-RDY*(YFLUX(I,J+I)-YFLUX(I,J))+GY)

Page 20: Fdflow a Fortran 77 Solver for 2d Incomp Flow

284 C.M. LEMOS

40

C 2000

C C C C C

110

120 100

C C C

210

220 200

C C C

230 C C C

310

320 300

C C C

CONTINUE CALL BCEXP RETURN

CONTINUE

** IMPLICIT METHOD (METHOD = 2 )

** STEP 1, EQ. 1

DO 100 J=2,JM1 DO 110 I=2,1M1-1 UR=O.5*(UN(I+I,J)+UN(I,J)) UL=O.b.(UN(I,J)+UN(I-1,J)) BBX(I)=-DTDX*UL-SX DDX(1)= I.O+DTDX*(UR-UL)+2.0*SX AAX(I)= DTDX*UR-SX RHSX(I)=UN(I,J)+DTDX*(UR**2-UL**2) CONTINUE CALL B C I M P ( J , I , 1 ) CALL TRIDAG(BBX,DDX,AAX,RHSX,IM1) DO 120 I = I , I M1 U ( I , J ) = R H S X ( I ) CONTINUE

** STEP 1, EQ. 2

DO 200 J=2,JMI-I DO 210 I=2,IMI UR=O.5*(UN(I,J+I)+UN(I,J)) UL=O.5.(UN(I-1,J+I)+UN(I-1,J)) BBX(1)=-O.5*DTDX*UL-SX DDX(I)= I.O+0.5.DTDX.(UR-UL)+2.0.SX AAX(1)= 0.5.DTDX.UR-SX RHSX(I)=VN(I,J) CONTINUE CALL BCIMP(J,1,2) CALL TRIDAG(BBX,DDX,AAX,RHSX,IMAX) DO 220 I=I,IMAX V ( I , J ) = R H S X ( I ) CONTINUE

** PREPARE FOR STEP 2; COPY TEMPORARY SOLUTION TO UN AND VN ARRAYS

DO 230 J=2,JM1 DO 230 I=I,IMAX VN(I,J)=V(I,J) U N ( I , J ) = U ( I , J )

** STEP 2, EO. 1

DO 300 I=2,1MI-I DO 310 J=2,JMI VT=O.5*(VN(I,J)+VN(I+I,J)) VB=O.5*(VN(I,J-I)+VN(I+I,J-1)) BBY(J)=-O.5*VB*DTDY-SY DDY(J)= I.O+0.5*(VT-VB)*DTDY+2.0*SY AAY(J)= 0.5*VT*DTDY-SY RHSY(J)=UN(I,J)-DTDX*(P(I+I,J)-P(I,J))+GX*DELT CONTINUE CALL BCIMP(I,2,1) CALL TRIDAG(BBY,DDY,AAY,RHSY,JMAX) DO 320 J=I,JMAX U ( I , J ) : R H S Y ( J ) CONTINUE

** STEP 2, EQ. 2

Page 21: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow 285

410

420 400

DO 400 I=2,IMI DO 410 J=2,JMI-I VT=O.5*(VN(I,J)+VN(I,J+I)) VB=O.5*(VN(I,J)+VN(I,J-1)) BBY(J)=-DTDY*VB-SY BDY(J)= I.O+(VT-VB)*DTDY+2.0*SY AAY(J)= DTDY.VT-SY RHSY(J)=VN(I , J )+DTDY.(VT**2-VB**2-P(I , J+I )+P(I , J ) )+GY.DELT CONTINUE CALL BCIMP(I ,2 ,2 ) CALL TRIDAG(BBY,BDY,AAY,RHSY,JM1) DO 420 J : I , J M 1 V( I , J )=RHSY(J ) CONTINUE RETURN END

10 C C C

20 C C C

30

SUBROUTINE STEP2 INCLUDE ~FDFLO~.INC ~

** ITERATIVELY ADJUSTS PRESSURE AND VELOCITIES

ITER=O FLAG:I .0 IF(FLAG.EO.O.O) GOTO 30 ITER=ITER+I IF( ITER.GF.5000) STOP ~ P r e s s u r e f a i l e d to c o n v e r g e !~ FLAG=O.O

** COMPUTE UPDATED CELL PRESSURE AND VELOCITIES

DO 20 J=2,JMI DO 20 I=2,1M1 D=RDX*(U(I,J)-U(I-I,J))+RDY.(V(I,J)-V(I,J-I)) IF(ABS(D).GE.EPSI) FLAG=I.0 DELP=-BETA.D P(I,J)=P(I,J)+DELP U(I,J)=U(I,J)+DELT*RDX*DELP U(I-I,J)=U(I-1,J)-DELT.RDX.DELP V(I,J)=V(I,J)+DELT.RDY.DELP V(I,J-I)=V(I,J-1)-DELT*RDY.DELP CONTINUE

** SET BOUNDARY CONDITIONS

CALL BCEXP GOTO 5 RETURN END

10

SUBROUTINE TRIDAG(BB,DD,AA,CC,NN) DIMENSION A A ( . ) , B B ( . ) , C C ( . ) , D D ( . )

** THIS SUBROUTINE PERFORMS TRIDIAGBNAL ELIMINATION

BB=COEFFICIENT BEHIND DIAGONAL DD=COEFFICIENT ON DIAGONAL AA=COEFFICIENT AHEAD OF DIAGONAL CC:R.H.S. VECTOR; CONTAINS THE SOLUTION ON RETURN NN=NUMBER OF EQUATIONS

NOTE: DD AND CC ARE OVERWRITTEN BY THE SUBROUTINE

DO 10 I=2,NN D D ( I ) = D D ( I ) - A A ( I - 1 ) . ( B B ( I ) / D D ( I - 1 ) ) CC(I):CC(I)-CC(I-1)*(BB(I)/nn(I-1))

Page 22: Fdflow a Fortran 77 Solver for 2d Incomp Flow

286 C.M. LEMOS

CC(NN)=CC(NN)/DD(NN) DO 20 I=2,NN J=NN-I+I

20 CC(J)=(CC(J)-AA(J)*CC(J+I))/DD(J) RETURN END

: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

A P P E N D I X 3

FPLOT.FOR Listing

C ........................ PROGRAM "FPLOT.FOR" : ..... == ..............

C=== Post-processing program for visualization of the velocity field

10

PROGRAM FPLflT INCLUDE 'FDFLOW.INC' COMMON /WINDOW/ XMIN,YMIN,XMAX,YMAX CHARACTER*11FILENAME

OPEN (IO,FILE=~FDFLOW.DAT~,STATUS='OLD,) READ (IO,.) IBAR READ (IO,.) JBAR READ (IO,.) DELX READ (IO,.) DELY READ ( I 0 , . ) DELT READ ( I O , . ) UI READ ( I O , . ) VI READ ( I 0 , . ) TWFIN READ (IO,.) PRTDT READ (IO,.) 0MG READ (I0,.) NU READ (I0,.) GX READ (IO,.) GY READ (I0,*) EPSI CLOSE(IO)

XMAX=DELX*FLOAT(IBAR) YMAX=DELY*FLOAT(JBAR) XMIN=O.O YMIN=O.O IMAX=IBAR+2 JMAX=JBAR+2 IMI=IMAX-I JMI=JMAX-I

PRINT *,~Filename for plot ?~ READ (*,'(A)') FILENAME OPEN (IO,FILE=FILENAME,STATUS='OLD') READ (IO,*) ITER READ (IO,.) T READ ( I O , . ) CYCLE READ ( I O , ' ( A ) ' ) DO 10 I=I,IMAX DO I0 J=I,JMAX READ (IO,*) I I ,JJ ,U(I ,J) ,V(I ,J) ,P(I ,J) PRINT *,~Maximum velocity expected in problem ?~ READ (*,*) VELMX VELMXI=AMINI(DELX,DELY)/VELMX

CALL PLOTS(O,91,91) CALL FACTOR(0.89)

C C ** DRAW A FRAME AROUND THE PLOT

Page 23: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow 287

20

CALL DRWVEC(XMIN,YMIN,XMIN,YMAX,O) CALL DRWVEC(XMIN,YMAX,XMAX,YMAX,O) CALL DRWVEC(XMAX,YMAX,XMAX,YMIN,O) CALL DRWVEC(XMAX,YMIN,XMIN,YMIN,O)

** DRAW PLOT BORDER AND LEGEND

CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL CALL

PLOT(O.OI,O.01,3) PLOT(O.01,8.5,2) P L O T ( l l . O , 8 . 5 , 2 ) PLOT(11.0,O.01,2) PLOT(O.01,0.01,2) SYMBOL(O.15,8.13,0.175, 'T = ' , 0 . 0 , 6 ) NUMBER(999.0,999.0,O.175,SNGL(T),O.O,2) SYMBOL(O.15,5.53,0.175,'CYCLE=',O.O,6) NUMBER(999.0,999.0,O.175,FLOAT(CYCLE),O.O,-1) SYMBOL(O.15,2.93,0.175, 'Re = ' , 0 . 0 , 6 ) NUMBER(999.0,999.0,O.175,SNGL(1.OD+O/DBLE(NU))+l.E-3,0.O,-1) SYMBOL(O.15,0.33,0.175, 'VELMK:',O.O,6) NUMBER(999.,999.0,O.175,VELMK,O.O,2)

** DRAW THE VELOCITY VECTORS

DO 20 I=2,IM1 DO 20 J=2,JM1 XC=(FLOAT(I)-I.5).DELX YC=(FLOAT(J)-I.5).DELY XEND=XC+O.5.(U(I,J)+U(I-1,J)).VELMX1 YEND=YC+O.5.(V(I,J)+V(I,J-1)).VELMX1 CALL DRWVEC(XC,YC,XEND,YEND,1) CALL PLOT(O.O,O.O,999)

STOP 'End o f FPLOT program' END

SUBROUTINE DRWVEC(XINI,YINI,XEND,YEND,TIP) COMMON /WINDOW/ XMIN,YMIN,XMAX,YMAX REAL XINI,YINI,XEND,YEND,XMIN,YMIN,XMAX,YMAX REAL XP1,YP1,XP2,YP2,XT1,YT1,XT2,YT2 INTEGER TIP

CALL CBNVERT(XINI,YINI,XP1,YP1) CALL CONVERT(XEND,YEND,XP2,YP2) CALL PLOT(XPI,YP1,3) CALL PLOT(XP2,YP2,2) IF (TIP.Eq.O) RETURN

** DRAW T I P OF ARROW I F T I P : I

UE=XP2-XP1 VE=YP2-YP1 XTl=XP2+O.125.(-2.0.UE-VE) XT2=KP2+O.125*(-2.0.UE+VE) YTl=YP2+O.125*(-2.0*VE+UE) YT2=YP2+O.125*(-2.0*VE-UE) CALL PLOT(XT1,YT1,2) CALL PLOT(XT2,YT2,2) CALL PLOT(XP2,YP2,2) RETURN

END

SUBROUTINE CONVERT(X,Y,XPLOT,YPLOT) COMMON /WINDOW/ XMIN,YMIN,XMAX,YMAX REAL X,Y,XMIN,YMIN,XMAX,YMAX,XPLOT,YPLOT

CAGEO 2 0 i 3 ~ ;

Page 24: Fdflow a Fortran 77 Solver for 2d Incomp Flow

288 C.M. LEMOS

DX=XMAX-XMIN DY=YMAX-YMIN D=AMAXI(DX,DY) SF=I .O/D XSHFT=O.5.(1 .0-DX.SF) YSHFT=O.5.(1.0-DY.SF) XPLOT=((X-XMIN).SF+XSHFT).8.0+2.83 YPLOT=((Y-YMIN).SF+YSHFT).8.0+0.33 RETURN END

: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

A P P E N D I X 4

GRFPR1M.FOR Listing

C . . . . . GRAPHICS PRIMITIVES FOR GENERATING "*.PLT" FILES . . . . . . C . . . . . IN THE FORMAT OF THE VIEW/PLOT PROGRAMS BY Golden S o f t w a r e . . . . . .

SUBROUTINE PLOTS(IDUMMY,IOPORT,MODEL) ************************************************************************ C . C INITIALIZES PLOT FILE . C ARGUMENTS IDUMMY,IOPORT AND MODEL INCLUDED FOR COMPATIBILITY WITH * C PLOT88 (Young h Van Woer t , 1990) . C . ************************************************************************

CHARACTER*12 PLTFILE IDI=IDUMMY ID2=IOPORT ID3=MODEL PRINT * , ~ F i l e n a m e f o r p l o t f i l e >' READ ( * , 1 0 ) PLTFILE OPEN(UNIT=90,FILE=PLTFILE) CALL COLOR(7,IERR)

10 FORMAT (A) 20 FORMAT ( A , 2 F l O . 5 ) 30 FORMAT ( A , F I O . 5 )

RETURN END

SUBROUTINE PLOT(X,Y,IPEN) C*********************************************************************** C . C DRAWS A LINE FROM CURRENT PEN POSITION TO POINT (X,Y) , WITH: * C . C IPEN=2 PEN UP . C IPEN=3 PEN DOWN . C IPEN=999 CLOSE PLOT FILE (END DRAWING) . C . C NOTE: THIS SUBROUTINE IS NOT ENTIRELY COMPATIBLE WITH THE * C . . . . PLOT88 LIBRARY . C . ************************************************************************

IF ( IPEN.EO.999) THEN CLOSE (90)

ENBIF IF ( IPEN.EQ,2) THEN

WRITE ( 9 0 , 1 0 ) ~PA ' , X , Y ENDIF IF ( IPEN.EQ.3) THEN

Page 25: Fdflow a Fortran 77 Solver for 2d Incomp Flow

10

F O R T R A N solver for 2-D incompressible fluid flow

WRITE ( 9 0 , 1 0 ) 'MA ' , X , Y ENDIF FORMAT ( A , 2 F l O . 5 ) RETURN END

289

SUBROUTINE SYMBOL(X,Y,HEIGHT,CTEXT,ANGLE,NC) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

C * C DRAWS A CHARACTER STRING . C . C X , Y : LOWER LEFT CORNER OF FIRST CHARACTER TO BE DRAWN; *

C IF X = 9 9 9 . 0 AND/OR Y = 9 9 9 . 0 , APPEND CTEXT TO THE LAST * C TEXT STRING . C HEIGHT = HEIGHT OF EACH CHARACTER . C IF X = 9 9 9 . 0 AND/OR Y = 9 9 9 . 0 , THE HEIGHT OF THE LAST * C TEXT STRING IS USED • C CTEXT = TEXT TO BE DRAWN . C A N G L E = ANGLE ABOUT THE Z-AXIS , IN DEGREES * C IF X = 9 9 9 . 0 AND/OR Y = 9 9 9 . 0 , THE ANGLE OF THE LAST * C TEXT STRING IS USED . C NC = NUMBER OF CHARACTERS TO BE DRAWN *

C . C . C NOTE: THIS SUBROUTINE IS NOT ENTIRELY COMPATIBLE WITH THE * C . . . . PLOT88 LIBRARY .

C .

CHARACTER CTEXT*(*) CHARACTER*80 STPLflT,STSAVE SAVE

10

XPLOT=X YPLOT=Y ANGPLOT=ANGLE HPLOT=HEIGHT STPLOT:CTEXT(I :NC) IF ( X . E Q . 9 9 9 . 0 . O R . Y . E Q . 9 9 9 . 0 ) THEN

XPLOT=XSAVE YPLOT=YSAVE ANGPLOT=ANGSAVE HPLflT=HSAVE STPLf lT=STSAVE(I :LSTRING(STSAVE)) / /CTEXT(I :NC)

ENDIF WRITE ( 9 0 , 1 0 ) ~PS~,XPLOT,YPLOT,HPLOT,ANGPLOT, ~ "~

& S T P L O T ( I : L S T R I N G ( S T P L O T ) ) , ' " ' XSAVE=XPLOT YSAVE=YPLOT HSAVE:HPLOT

ANGSAVE=ANGPLOT STSAVE=STPLOT FORMAT ( A , 4 F l O . 5 , 3 A ) RETURN END

Continued overleaf

Page 26: Fdflow a Fortran 77 Solver for 2d Incomp Flow

290 C . M . LEMOS

SUBROUTINE NUMBER(X,Y,HEIGHT,FPN,ANGLE,NDEC) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

C * C CONVERTS A REAL NUMBER TO A CHARACTER STRING, AND DRAWS THE LATTER • C * C X,Y = LOWER LEFT CORNER OF FIRST CHARACTER TO BE DRAWN; • C IF X = 9 9 9 . 0 AND/OR Y = 9 9 9 . 0 , APPEND FPN TO THE LAST • C TEXT STRING • C HEIGHT = HEIGHT OF EACH CHARACTER • C IF X : 9 9 9 . 0 AND/OR Y = 9 9 9 . 0 , THE HEIGHT OF THE LAST • C TEXT STRING IS USED • C FPN = REAL NUMBER TO BE CONVERTED • C A N G L E = ANGLE ABOUT THE Z-AXIS , IN DEGREES; • C IF X = 9 9 9 . 0 AND/OR Y = 9 9 9 . 0 , THE ANGLE OF THE LAST • C TEXT STRING IS USED • C N D E C = FORMAT OPTION : • C * C 0 , WRITE INTEGER PART OF FPN PLUS A DECIMAL POINT * C > 0 , WRITE FPN IN F-FORMAT WITH NDEC DIGITS * C - 1 , WRITE INTEGER PART OF FPN *

C * C NOTE: THIS SUBROUTINE IS NOT ENTIRELY COMPATIBLE WITH THE * C . . . . PLOT88 LIBRARY *

C * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

100 110

120 130

C 10 20

140

CHARACTER RNUMBER*20,FM*20 RNUMBER=~ FM=~ ,

I F ( N D E C . E Q . - 1 ) THEN IRNUM = INT(FPN) WRITE(RNUMBER,IO) IRNUM

ENDIF IF (NDEC.GE.O) THEN

WRITE(FM,20) NDEC WRITE(RNUMBER,FM) FPN

ENDIF DO 100 I = 1 , 2 0

I F ( R N U M B E R ( I : I ) . N E . ' ' ) GOTO 110 CONTINUE CONTINUE DO 120 J = I , 2 0

I F ( R N U M B E R ( J : J ) . E q . ' ' ) GOTO 130 CONTINUE CONTINUE NCHAR=J-I DO 140 NC=I,NCHAR

RNUMBER(NC:NC)=RNUMBER(NC+I-I:NC+I-1) CONTINUE RNUMBER(NCHAR+I:20)=~ , CALL SYMBOL (X,Y,HEIGHT,RNUMBER,ANGLE,NCHAR)

FORMAT(IIO) F O R M A T ( ' ( F 1 5 . ' , I 1 , ' ) ' ) RETURN END

SUBROUTINE FACTOR(FACT) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

C . C APPLIES A SCALE FACTOR . C . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

W R I T E ( 9 0 , 1 0 ) 'SC ' ,FACT,FACT 10 FORMAT ( A , 2 F 1 0 . 5 )

RETURN END

Page 27: Fdflow a Fortran 77 Solver for 2d Incomp Flow

FORTRAN solver for 2-D incompressible fluid flow

FUNCTION LSTRING(STRING) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

C * C FINDS THE NUMBER OF NON-BLANK CHARACTERS IN A CHARACTER STRING * C * C NOTE: T h i s f u n c t i o n was e x t r a c t e d from "Programming w i t h FORTRAN 77"* C . . . . by J . A s h c r o f t , R. H. E ] d r i d g e , H. W. Pau l s o n and G. A. Wi l son* C S h e r i d a n House, I n c . , N.Y. , 1981 . C * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

CHARACTER STRING.(*) DO 10 I=LEN(STRING),I , -1 I F ( S T R I N G ( I : I ) . N E . ' ' ) THEN

LSTRING=I RETURN

ENDIF 10 CONTINUE

LSTRING=O RETURN END

291

SUBROUTINE COLOR(IC,IERR) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

C * C SETS CURRENT PEN COLOR * C * C IC = COLOR INDEX, 1 =< IC =< 15 (FOR VGA SCREEN) * C IERR = RETURN ERROR CODE (=0 FOR NO ERROR, =1 IF AN ERROR * C HAS BEEN DETECTED) * C *

IERR=O IF(IC.LT.I.OR.IC.GT.15) THEN

IERR=I RETURN

ENDIF WRITE ( 9 0 , 1 0 ) 'SP ' , IC

10 FORMAT(A,I2) RETURN END

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::