12
A tensorial approach to computational continuum mechanics using object-oriented techniques H. G. Weller and G. Tabor a! Department of Mechanical Engineering, Imperial College, London SW7 2BX, United Kingdom H. Jasak Computational Dynamics Limited, London W10 6RA, United Kingdom C. Fureby Department of Weapons and Protection, National Defense Research Establishment (FOA), S-17290 Stockholm, Sweden (Received 1 June 1998; accepted 13 August 1998) In this article the principles of the field operation and manipulation ~FOAM! C11 class library for continuum mechanics are outlined. Our intention is to make it as easy as possible to develop reliable and efficient computational continuum-mechanics codes: this is achieved by making the top-level syntax of the code as close as possible to conventional mathematical notation for tensors and partial differential equations. Object-orientation techniques enable the creation of data types that closely mimic those of continuum mechanics, and the operator overloading possible in C11 allows normal mathematical symbols to be used for the basic operations. As an example, the implementation of various types of turbulence modeling in a FOAM computational-fluid-dynamics code is discussed, and calculations performed on a standard test case, that of flow around a square prism, are presented. To demonstrate the flexibility of the FOAM library, codes for solving structures and magnetohydrodynamics are also presented with appropriate test case results given. © 1998 American Institute of Physics. @S0894-1866~98!01906-3# INTRODUCTION Computational continuum mechanics ~CCM! is the simula- tion of continua using computers. Fluid dynamics is a sig- nificant branch of continuum mechanics and covers a vari- ety of cases, including compressible, incompressible, multiphase, and free-surface flows, as well as flows involv- ing further physics such as chemical reactions, species transport, phase changes, and electromagnetic effects. All these flows can be described by systems of linked partial differential equations of the form ]r Q ] t 1~ r U^ Q! 2r D Q5S p Q1S q , ~1! where U is the fluid velocity, r its density, and Q is any tensor-valued property of the flow, such as species concen- tration. These equations involve time derivatives ( ]r Q/ ] t ), convective terms @ ( r U^ Q) # , diffusive terms ( r D Q), and source terms ~S Q and S q !. A simple ex- ample is that of incompressible flow as described by the Navier–Stokes equations ( Q5$ 1,U% ): U50, ~2! ] U ] t 1~ U^ U! 22 n D52 1 r p , where D5 1 2 ~ U1U T ! . ~3! The effect of the nonlinearity embodied in these equations is significant; only in special cases can algebraic solutions be found. The vast majority of fluid-flow problems can only be properly studied by using computational methods in- volving discretization of the domain and the equations, fol- lowed by numerical solution of the resulting system of equations. The complexity of the problem is increased if effects such as turbulence, compressibility, multiphase, free surface, chemical reactions, and electromagnetism are in- cluded. The two predominant solution techniques are the finite-element method ~FEM!, 1 in which the functional form of the solution to these equations is expanded in terms of a predetermined basis set and its residual minimized, and the finite-volume method ~FVM!. 2,3 In the latter technique, which is used in this article, the computational domain is divided into a set of discrete volumes d V i which fill the computational domain D without overlap, i.e., l i d V i 5D and ø i d V i 5B. The fluid-flow equations are then volume- integrated over each individual finite volume d V i . Gauss’s theorem is used to convert the divergence terms in Eqs. ~2! into surface-integrated flux terms, reducing the problem of discretizing these terms to one of finding difference ap- proximations for the fluxes at the surface of the control volume based on the known cell-center values. Other spa- tial derivatives are dealt with in a similar manner. This a! Corresponding author; E-mail: [email protected] 620 COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998 © 1998 AMERICAN INSTITUTE OF PHYSICS 0894-1866/98/12~6!/620/12/$15.00

A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

  • Upload
    hadang

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

A tensorial approach to computational continuum mechanicsusing object-oriented techniquesH. G. Weller and G. Tabora!

Department of Mechanical Engineering, Imperial College, London SW7 2BX, United Kingdom

H. JasakComputational Dynamics Limited, London W10 6RA, United Kingdom

C. FurebyDepartment of Weapons and Protection, National Defense Research Establishment (FOA),S-17290 Stockholm, Sweden

(Received 1 June 1998; accepted 13 August 1998)

In this article the principles of the field operation and manipulation~FOAM! C11 class library forcontinuum mechanics are outlined. Our intention is to make it as easy as possible to developreliable and efficient computational continuum-mechanics codes: this is achieved by making thetop-level syntax of the code as close as possible to conventional mathematical notation for tensorsand partial differential equations. Object-orientation techniques enable the creation of data typesthat closely mimic those of continuum mechanics, and the operator overloading possible in C11allows normal mathematical symbols to be used for the basic operations. As an example, theimplementation of various types of turbulence modeling in a FOAM computational-fluid-dynamicscode is discussed, and calculations performed on a standard test case, that of flow around a squareprism, are presented. To demonstrate the flexibility of the FOAM library, codes for solvingstructures and magnetohydrodynamics are also presented with appropriate test case results given.© 1998 American Institute of Physics.@S0894-1866~98!01906-3#

ig-ari-le,lv-

cies. Artia

cenes

the

nsonslyin-ol-of

iffree

in-the

rmsand

is

-

.ofp-

rolpa-is

INTRODUCTION

Computational continuum mechanics~CCM! is the simula-tion of continua using computers. Fluid dynamics is a snificant branch of continuum mechanics and covers a vety of cases, including compressible, incompressibmultiphase, and free-surface flows, as well as flows invoing further physics such as chemical reactions, spetransport, phase changes, and electromagnetic effectsthese flows can be described by systems of linked padifferential equations of the form

]rQ

]t1¹–~rU^ Q!2¹–rD¹Q5SpQ1Sq , ~1!

whereU is the fluid velocity,r its density, andQ is anytensor-valued property of the flow, such as species contration. These equations involve time derivativ(]rQ/]t), convective terms@¹–(rU^ Q)#, diffusive terms(¹–rD¹Q), and source terms~SQ andSq!. A simple ex-ample is that of incompressible flow as described byNavier–Stokes equations (Q5$1,U%):

¹–U50,~2!

]U

]t1¹–~U^ U!2¹–2nD52

1

r¹p,

a!Corresponding author; E-mail: [email protected]

620 COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998

lll

-

where

D5 12~¹U1¹UT!. ~3!

The effect of the nonlinearity embodied in these equatiois significant; only in special cases can algebraic solutibe found. The vast majority of fluid-flow problems can onbe properly studied by using computational methodsvolving discretization of the domain and the equations, flowed by numerical solution of the resulting systemequations. The complexity of the problem is increasedeffects such as turbulence, compressibility, multiphase,surface, chemical reactions, and electromagnetism arecluded. The two predominant solution techniques arefinite-element method~FEM!,1 in which the functionalform of the solution to these equations is expanded in teof a predetermined basis set and its residual minimized,the finite-volume method~FVM!.2,3 In the latter technique,which is used in this article, the computational domaindivided into a set of discrete volumesdVi which fill thecomputational domainD without overlap, i.e.,ø idVi5Dandù idVi5B. The fluid-flow equations are then volumeintegrated over each individual finite volumedVi . Gauss’stheorem is used to convert the divergence terms in Eqs~2!into surface-integrated flux terms, reducing the problemdiscretizing these terms to one of finding difference aproximations for the fluxes at the surface of the contvolume based on the known cell-center values. Other stial derivatives are dealt with in a similar manner. Th

© 1998 AMERICAN INSTITUTE OF PHYSICS 0894-1866/98/12~6!/620/12/$15.00

Page 2: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

tialis-ceons

to

edthe

suf-p--chssir-n isua

acllynossdle-f a

cs, in-

wererla-e-the

tndns

dens

conua-u-ed

ng. Itt isral

te-

tterat

idethel ob

onstheypes

ati-fe is, ashisiouseen

ingthehislassati-

of assisnt

tog aticalin

nsi-dareor,ingols,r,tialardn

entear-P,iple

theleorkthe

al-entat-

hinore,n-ande

el-m-

eg

converts the equations into a set of ordinary differenequations including temporal derivatives, which can be dcretized in a straightforward manner using finite-differenapproximations. This results in a set of difference equatithat, when linearized by fixing the fluxrU, can be de-scribed in matrix form

Mc5y, ~4!

whereM is a sparse block matrix: this can be invertedsolve the equation. The nonlinear term in Eqs.~2! requiresan iterative solution technique, one in which the linearizsystem specified above is solved several times, withfluxes being updated each time, until it has convergedficiently. Coupling between equations is treated in field oeration and manipulation~FOAM! using a segregated approach, in which equations are formulated for eadependent variable and solved sequentially, with the pobility of iteration over the system of equations until convegence is achieved. So, for example, any vector equatiosolved by solving each component in turn as a scalar eqtion, within the solution of the overall vector equation~i.e.,the user sees a vector equation, but when the solutiontually occurs, the components are solved for sequentia!.Provided that the coupling between the components isstrong~as it would be if the coupling were due to a croproduct of some form!, this is quite acceptable. If the neearises, the design of FOAM does not preclude the impmentation of a block solver to solve the components ovector ~or tensor! equation simultaneously.

In general, CCM and computational-fluid-dynami~CFD! codes have been written in procedural languagesparticular in Fortran.2 This reliance on procedural programming techniques has led to a concentration on the lolevels of the coding: implementation of models, howevcomplicated, is often discussed in terms of the manipution of individual floating-point values. The approach prsented in this article is different and has culminated increation of the FOAM C11 class library for CCM. Theintention is to develop a C11 class library that makes ipossible to implement complicated mathematical aphysical models as high-level mathematical expressioThis is facilitated by making the high levels of the coresemble as closely as possible standard vector and tenotation. In this approach the tensorial fieldsQ5r, U, etc.,which represent the state of the system of interest, aresidered as the solution of a set of partial differential eqtions ~PDEs! ~2!, rather than viewing the problem as a nmerical one in which arrays of floating values are obtainby inverting matrices. An object-oriented programmi~OOP! methodology has been adopted for this approachis generally recognized that OOP produces code thaeasier to write, validate, and maintain than procedutechniques.4 Our intention in this article is to demonstrathe utility of OOP techniques for solving continuummechanics~and especially fluid-dynamics! problems.

The exact definition of an OOP language is a mathat is discussed elsewhere,5,6 but Stroustrup suggests than OOP approach is one that involvesabstraction, inherit-ance, andpolymorphism.7 Abstraction is the ability to rep-resent conceptual constructs in the program and to hdetails behind an interface. This is achieved by allowingprogrammer to create classes to represent conceptua

-

-

-

t

.

or

-

-

jects in the code, classes thatencapsulate, i.e., contain andprotect, the data that make up the object. Member functiare provided that permit limited, well-defined access toencapsulated data. Thus it is possible to create data tthat represent tensor fields~see Sec. I A! and typical termsin the equations constructed to behave like their mathemcal counterparts~Sec. I B!, hiding the numerical details othe implementation by encapsulation. The class interfacdesigned to resemble standard mathematical notationseen below, while the implementation is not relevant at tlevel. Inheritance enables relationships between the varclasses to be expressed, representing commonality betwdifferent classes of objects by class extension. By dothis existing classes can be given new behavior withoutnecessity of modifying the existing class. For example, tcan be used to construct a complicated mathematical cby extending base classes that express simpler mathemcal objects. A class representing the algebraic structurering could thus be derived from a pre-existing group claby adding the concept of multiplication. Examples of thwithin FOAM include the use of inheritance to represeconceptual links between turbulence models~see Sec. II!.Polymorphism is the ability to provide the same interfaceobjects with different implementations, thus representinconceptual equivalence between classes that in practerms have to be coded differently. Examples of thisFOAM include the implementation of boundary conditio~Sec. I C!. C11 is a good programming language for scentific work; although it is less rigorously object-orientethan other languages such as Smalltalk or Eiffel, therepractical considerations to take into account. Its authStroustrup, intended it to support a range of programmstyles, and so he incorporated a number of conceptual tonot all of which are strictly object-based. In particulaC11 implements operator overloading, which is essenin order to construct an interface that resembles standmathematical notation. Moreover it is widely available oall platforms and, being based on C, is fast. Recstudies8,9 indicate no significant difference in performancbetween Fortran and the C group of languages. In thisticle C11 terminology is adopted when discussing OOand so reference is made to member functions and multinheritance, rather than to methods and interfaces.

While a great deal of attention has been paid todevelopment of new and efficient algorithms for CFD, litthas been published about overall code design. Some whas been done on the use of object-oriented methods inFEM10,11 and in the spectral-element method.12 The ap-proach has tended to be from the perspective of lineargebra in which the highest-level data structures represvectors and matrices, although there have been sometempts to develop symbolic-manipulation techniques witOOP to derive and automatically code matrix forms fstructural calculations.13,14 The approach proposed herwhich is from the viewpoint of the tensor calculus, is cosidered appropriate for the FVM and is easier to understfrom the point of view of the continuum mechanics. Thresult is a C11 class library in which it is possible toimplement a wide variety of continuum-mechanics moding techniques, including those of incompressible and copressible fluid flow,15 multiphase flow, and free surfacflow,16 together with various turbulence modelin

COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998 621

Page 3: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

Esbe

tedoudif-nc

w:ionultsowe

vinthelid.

theandhav

beon

facead-us

blet-

romsi-n-

tedrs,nd

s,hell aesof

-istic

tortorhat

trick-sesed,

pt

d. Ito

ce

-arya-

lge-en-to

M.calk-

re

atand-nce

nc-f aa

earon

thes,

ag-es

pu-AMoneheialthetaro-ein

in

ing

onel ofd toingheg

theanhe

ing

techniques.17 In fact any system of time-dependent PDincluding convection, diffusion, and source terms canhandled. In this article an outline of the library is presenand discussed, including some of the issues involved inapproach to implementing classes for tensor fields andferential equations. These issues are illustrated by refereto the two main modeling techniques for turbulent floReynolds-averaged simulation and large-eddy simulatwhich are implemented as virtual class hierarchies. Resare presented for the simulations of incompressible flaround a square prism. To illustrate the flexibility of thcode, two other examples are also presented, one involmagnetohydrodynamical flow in a duct, and the othercalculation of the stress and strain fields in an elastic so

I. IMPLEMENTATION

A. Implementation of tensor fields

The majority of fluid dynamics can be described usingtensor calculus of up to rank 2, i.e., scalars, vectors,second-rank tensors. Therefore three basic classesbeen created:scalarField, vectorField, and tensorField. De-tails of the implementation of these field classes willdescribed in a future paper; in this article we concentratethe code-design issues relating to the tensor-field interand the differential operators thereof. One of the greatvantages of OOP is that this division into interface versimplementation is possible: in theory, it should be possito rewrite totally the tensor implementation without affecing the rest of the library.18

These tensor field classes are somewhat different fa mathematical tensor field in that they contain no potional information; they are essentially ordered lists of tesors, and so only pointwise operations~i.e., tensor algebra!can be performed at this level. The operators implemeninclude addition and subtraction, multiplication by scalaformation of various inner products, and the vector aouter products of vectors~resulting in vectors and tensorrespectively!. In addition, operations such as taking ttrace and determinant of a tensor are included as wefunctions to obtain the eigenvalues and eigenvectors; thare not necessary for solution of fluid systems but areimportance for postprocessing the data~see comments below!. Since C11 implements operator overloading, itpossible to make the tensor algebra resemble mathemanotation by overloading1, 2, !, etc. The one probleminherent here is that the precedence of the various operais preset, which makes it quite difficult to find an operafor the dot product with the correct precedence and tlooks correct.

The next level of tensors are referred to as ‘‘geometensor fields’’ and contain the positional information lacing in the previous classes. Again, there are clasfor the three ranks of tensors currently implementvolScalarField, volVectorField, andvolTensorField. At first,the relationship between, for example,scalarFieldand volScalarField should be ‘‘isA,’’ i.e., derivation.However, this would allow the compiler to accescalarField1volScalarField as an operation, which wouldnot be appropriate, and so encapsulation is used insteaaddition to the additional metrical information necessaryperform differentiation, which is contributed by a referen

622 COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998

r

e

,

g

e

se

al

s

n

to a ‘‘mesh class’’fvMesh ~see below!, these classes contain boundary information, previous time steps necessfor the temporal discretization, and dimension set informtion. All seven base SI dimensions are stored, and all abraic expressions implemented above this level are dimsionally checked at execution. It is therefore impossibleexecute a dimensionally incorrect expression in FOAThis has no significant runtime penalty whatsoever: typifields have 104– 105 tensors in them, and dimension checing is done once per field operation.

Currently two types of tensor-derivative classes aimplemented in FOAM:finiteVolumeCalculus or fvc, whichperforms an explicit evaluation from predetermined dand returns a geometric tensor field, afiniteVolumeMethod or fvm, which returns a matrix representation of the operation, which can be solved to advathe dependent variable~s! by a time step.fvm will be de-scribed in more detail in Sec. I B. Thefvc class has noprivate data and merely implements static member futions that map from one tensor field to another. Use ostatic class in this manner mimics the concept ofnamespace which has recently been introduced into C11,and by implementing the operations in this manner, a cldistinction is drawn between the data and the operationsthe data. The member functions of this class implementfinite-volume equivalent of various differential operatorfor example, the expression

vorticity 5 0.5* fvc::curl(U);

calculates the vorticity of a vector fieldU as 12¹3U.1 ~For

reasons of space, not all the variables in the program frments used to illustrate points will be defined. The namare, however, usually quite descriptive.! This also illus-trates the ease with which FOAM can be used to manilate tensorial data as a postprocessing exercise. Any FOcode can be thought of as an exercise in mapping fromtensor field to another, and it matters little whether tmapping procedure involves the solution of a differentequation or not. Hence, writing a short code to calculatevorticity of a vector field is a matter of reading in the da~for which other functions, not described here, are pvided!, performing this manipulation, and writing out thresults. Very complicated expressions can be built upthis way with considerable ease.

All possible tensorial derivatives are implementedFOAM: ]/]t, ¹–, ¹, and ¹3. In addition, the Laplacianoperator is implemented independently rather than relyon the use of¹ followed by ¹–. This enables improveddiscretization practices to be used for this operator. Thenumerical issue that has to be dealt with at the top levethe code is the choice of differencing scheme to be usecalculate the derivative. Again, because of the data hidin OOP, the numerics can be effectively divorced from thigh-level issues of modeling: improved differencinschemes can be implemented and tested separately fromcodes that they will eventually be used in. The choice cbe made at the modeling level by using a switch in toperator. Hence, the temporal derivative]/]t can be in-voked as

volVectorField dUdt 5 fvc::ddt(U, EI)

where the second entry specifies which differenc

Page 4: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

or-thiare

ialldss,ofis-rted

ero-

ientgric

ut,ac-th

temingices

beua

esese. Th

en-s

hereer

arees

cellx,thisctioslls tnt.

Aialc-

ix

byh

p-msser-

y,

s-or

tion.the

to-hyents

ric

es

e it

-velistare

pes

.s,for

art

on,

xednda

-veis

hefor

ific

i-

scheme to use~in this case Euler implicit!. Several tempo-ral differencing schemes are available, with a default cresponding to the scheme that gets the most use, incase, backward differencing. Other selection methodspossible, but this one is the simplest.

B. Implementation of partial-differential-equation classes

The fvc methods correspond directly to tensor differentoperators, since they map tensor fields to tensor fieCCM requires the solution of partial differential equationwhich is accomplished by converting them into systemsdifference equations by linearizing them and applying dcretization procedures. The resulting matrices are inveusing a suitable matrix solver.

The differential operators¹–, ¹, and ¹3 lead tosparse matrices, which for unstructured meshes havcomplex structure requiring indirect addressing and apppriate solvers. FOAM currently uses the conjugate-gradmethod,19 with incomplete Cholensky preconditionin~ICCG!,20 to solve symmetric matrices. For asymmetmatrices the Bi-CGSTAB method21 is used. The matrix in-version is implemented using face addressing througho15

a method in which elements of the matrix are indexedcording to which cell face they are associated with. Botransient and steady-state solutions of the equation sysare obtained by time-marching, with the time step beselected to guarantee diagonal dominance of the matras required by the solvers.

In order that standard mathematical notation canused to create matrix representations of a differential eqtion, classes of equation object calledfvMatrixScalar,fvMatrixVector, etc., are defined to handle addressing issustorage allocation, solver choice, and the solution. Thclasses store the matrices that represent the equationsstandard mathematical operators1 and 2 are overloadedto add and subtract matrix objects. In addition, all the tsorial derivatives]/]t, ¹–, ¹3, etc., are implemented amember functions of a classfiniteVolumeMethod ~abbrevi-ated tofvm!, which construct appropriate matrices using tfinite-volume discretization. Numerical considerations arelevant in deciding the exact form of many of the membfunctions. For instance, in the FVM, divergence termsrepresented by surface integrals over the control volumdVi . Thus the divergence function call isdiv(phi,Q), wherephi is the flux, a field whose values are recorded on thefaces, andQ is the quantity being transported by the fluand is a field whose values are on the cell centers. Forreason, this operation cannot be represented as a funcall of the formdiv(phi*Q). Again, the Laplacian operator iimplemented as a single separate call rather than as cadiv andgrad, since its numerical representation is differeVarious forms of source term are also implemented.source term can be explicit, in which case it is a speckind of equation object with entries only in the source vetor y, or it can be made implicit, with entries in the matrM . In Eq. ~1! these are the termsSq andSpQ, respectively.Construction of an explicit source term is provided forfurther overloading1 ~and 2! to provide operations sucas fvm1volScalarField. Construction of an implicit sourceis arranged by providing a functionSp(a,Q), thus specify-ing the dependent variableQ to be solved for.

s

.

a

s

,

-

,

e

n

o

Thus it is possible to build up the matrix system apropriate to any equation by summing the individual terin the equation. As an example, consider the mass convation equation]r/]t1¹–(f)50, wheref5rU. The ma-trix system can be assembled by writing

fvMatrixScalar rhoEq(

fvm::ddt(rho) 1 fvc::div(phi));

where the velocity fluxphi has been evaluated previousland solved by the call

rhoEq.solve( );

to advance the value ofr by one timestep. Where necesary, the solution tolerance can be explicitly specified. Fcompleteness, the operation55 is defined to represenmathematical equality between two sides of an equatThis operator is here entirely for stylistic reasons, sincecode automatically rearranges the equation~all implicitterms go into the matrix, and all explicit terms contributethe source vector!. In order for this to be possible, the operator chosen must have the lowest priority, which is w55 was used; this also emphasizes that this represequality of the equation, not assignment.

C. Mesh topology and boundary conditions

Geometric information is contributed to the geometfields by the classfvMesh, which consists of a list of verti-ces, a list of internal cells, and a list of boundary patch~which in turn are lists of cell faces!. The vertices specifythe mesh geometry, whereas the topology of any cell—bone dimension~1D! ~a line!, two dimensions~2D! ~a face!,or three dimensions~3D! ~a cell!—is specified as an ordered list of the indices together with a shape primitidescribing the relationship between the ordering in theand the vertices in the shape. These primitive shapesdefined at run time, and so the range of primitive shacan be extended with ease, although the 3D settetrahedron~four vertices!, pyramid ~five vertices!, prism ~six vertices!,and hexahedron ~eight vertices! cover most eventualitiesIn addition, eachn-dimensional primitive shape knowabout its decomposition into (n21)-dimensional shapeswhich are used in the creation of addressing lists as,example, cell-to-cell connectivity.

Boundary conditions are regarded as an integral pof the field rather than as an added extra.fvMesh incorpo-rates a set of patches that define the exterior boundary]Dof the domain. Every patch carries a boundary conditiwhich is dealt with by everyfvm operator in an appropriatemanner. Different classes of patch treat calculated, fivalue, fixed gradient, zero gradient, symmetry, cyclic, aother boundary conditions, all of which are derived frombase classpatchField. All boundary conditions have to provide the same types of information, that is, that they hathe same interface but different implementations. Thistherefore a good example of polymorphism within tcode. From these basic elements, boundaries suitableinlets, outlets, walls, etc., can be devised for each specsituation. An additionalpatchField, processor is also avail-able. Parallelization of FOAM is via domain decompos

COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998 623

Page 5: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

hf thsois

i-

o-

d,theer-es

to-e-

forien

e,es

and

isre-ld.ith

ceurem

or-

reISOs isnd

u-n atheis-

y belvingre-

eheod-

art,uiva-

ngde-

theng

ress

nsch

s tothat

bede-

ar

tion: the domain is split intoN subdomains, one for eacprocessor available at run time, and a separate copy ocode is run on each domain. However, separate procesneed to exchange information within the solver, and thisthe function of theprocessor patch class. On decompostion, internal boundaries within the~complete! mesh aregiven processor patches, which know about the interprcessor topology and hide the interprocessor calls.~At thebottom level, PVM, MPI, or SHMEM calls can be usedepending on the computer, without affecting the rest ofcode.! This has the additional benefit that, since the intprocessor communication is at the level of the field classany geometric field, and hence any FOAM code, will aumatically parallelize. Other than this, the parallelization bhaves in the expected manner, showing linear speedupsmall number of processors and becoming less efficwhen the number of processors involved is increased~thatis, when message passing becomes a critical factor!.

D. An example: icoFoam

As an example, the following is the main part of a codicoFoam, which solves the incompressible Navier–StokEqs. ~2!. The predictor–corrector PISO method,22,23 inwhich the pressure and velocity fields are decoupledsolved iteratively, is as follows.

~1! An initial guess for the pressure field is made~in fact,the pressure solution from the previous time stepused! and the momentum equation is solved to a pdefined tolerance to give an approximate velocity fie

~2! The pressure Poisson equation is then formulated wthe divergence of the partial velocity flux as a sourterm and solved to give a new estimate of the pressfield. A new set of conservative fluxes is obtained frothe pressure equation.

~3! The corrected pressure field is used in an explicit crection to the velocity field.

Steps ~2! and ~3! can be iterated as many times as anecessary to reach a converged solution. Usually two Pcorrectors are sufficient for each time step, although thidependent on the time step selected, which in turn depeon the temporal accuracy required.

for(runTime11; !runTime.end( ); runTime11)$

Info ! ‘‘Time 5 ’’ ! runTime.curTime( ) nl ! endl;fvMatrixVector Ueqn(

fvm::ddt(U)1fvm::div(phi, U)2fvm::laplacian(nu, U)

);solve(Ueqn 55 2 fvc::grad(p));// --- PISO loopfor (int corr 5 0; corr,nCorr; corr11)$

phi5 Interpolate(Ueqn.H( )/Ueqn.A( )) & mesh.areas( );fvMatrixScalar peqn(

fvm::laplacian (1.0/Ueqn.A( ), p) 55 fvc::div(phi)

624 COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998

ers

,

at

s

);peqn.setReference(0, 0.0);peqn.solve( );phi 2 5 peqn.flux( );U 5 (Ueqn.H( ) 2 fvc::grad(p))/Ueqn.A( );U.correctBoundaryConditions( );

%%

II. TURBULENCE MODELING

A major issue in fluid dynamics is the existence of turblence. In a turbulent flow, there are coherent structures ovariety of spatial scales from the largest, determined bysize of the geometry, down to very small scales where vcous effects dominate. The range of scales involved maover several decades. There are two approaches to sofor turbulence: either the mesh used is fine enough tosolve~and thus simulate! all of the flow scales, or the rangof scales explicitly simulated must be reduced, with teffect of the unresolved scales being accounted for by meling. In Reynolds-averaged simulations~RAS!, the flow isdecomposed into an average part and a fluctuating pwhere the averaging can be across an ensemble of eqlent flows, or can be a time average over a timedt, short incomparison to any long-term changes to the flow but locompared to the turbulent fluctuations. In all cases thecomposition of the velocity field may be writtenU5U1U8, where the overbar indicates the average andprime the fluctuating component. Applying this averagito the momentum equation~2! yields

]U

]t1¹–~U^ U!1¹–R2¹–2nD52

1

r¹ p ~5!

whereR5U8^U8 is the Reynolds stress tensor.R is com-monly modeled as a turbulent viscosityn t times“U, andn t

evaluated from a hierarchy of equations derived fromnthorder moments of the Navier–Stokes equation~NSE!.These are also commonly referred to as algebraic stmodels. As an alternative, dynamic equations forR can beformulated; these contain terms including triple correlatioof the fluctuating velocity, which must be modeled. Sumodels, known as Reynolds Stress~RS! models, involvemuch more effort in their solution, since, althoughR is asymmetric tensor, it still has six independent componentbe solved for, and the equation set is even stiffer thanfor the standardk2e model. A completely different ap-proach is that of large-eddy simulation~LES!, in which thedivision is by scale via a filtering operation that canconveniently represented as a convolution between thependent field variable and a filter function with particulproperties. The filtered form of the NS equations is

¹–U50,~6!

]U

]t1¹–~U^U!1¹–B2¹–2nD52

1

r¹ p,

whereB is the subgrid scale~SGS! stress tensor

B5U^U2U^ U. ~7!

Page 6: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

nd

,-

n bE.

forntith-arthebeacer-

odn ad tothe

ell.ged

-er

u-

w

ilarhe

enn isef.ctlyhymed-

arethethe

es

Turbulence modeling consists of finding convenient aphysically correct representations forR andB.

A. RAS modeling

In all there are 11 RAS models implemented in FOAMincluding standard,24 nonlinear, and low-Reynoldsnumber25 variants of thek2e model,q2z, and nonlinearShih models. The appropriate terms and equations caincorporated directly into the code as additions to the NSHowever, since FOAM is intended as a research toolinvestigating fluid flows as well as modeling, it is importato be able to select the turbulence model at run time wout recompiling the code. Once again, OOP techniquesof value here. Since all of the models generate a term ofform “–R to add to the momentum equation, they canimplemented as a set of classes with a common interfThis implies that polymorphism via a virtual class hierachy is appropriate. A virtual base classturbulenceModel isdeclared, and classes that implement the various RAS mels are derived from it. The model selection is stored iparsed dictionary file, and a pointer mechanism is useinstantiate the correct RAS model as an instance ofvirtual base classturbulenceModel at run time, with thevalues of the coefficients being taken from this file as wThe additional terms can be included into the averaNSE as follows:

fvMatrixVector Ueqn(

fvm::ddt(U)1 fvm::div(phi, U)2 fvm::laplacian(turbulence.nuEff( ), U)1 turbulence.momentumSource( )

);solve (Ueqn 55 2 fvc::grad(p));

where turbulence is the RAS-model object. The contribution of the turbulence model to the flow is via the membfunction nuEff(), which returnsn1n t , wheren is the lami-nar viscosity. As an example, the standardk2e model24

requires the solution of the following equations for turblent kinetic energyk and dissipation ratee:

Pk52n tu¹Uu2,

]k

]t1¹–~Uk!2¹–

n t

sk¹k5Pk2e, ~8!

]e

]t1¹–~Ue!2¹–

n t

se¹e5C1e

e

kPk2C2e

e2

k,

wherePk is the production rate ofk andsk , sk , C1e , andC2e are model coefficients. Below is an example of hothis can be implemented in FOAM.

// Turbulent kinetic energy productionvolScalarField Pk 5 2*nut*magSqr(fvc::grad(U));// Turbulent kinetic energy equationsolve(

fvm::ddt(k)1 fvm::div(phi, k)2 fvm::laplacian(nu/sigmaK, k)

55

e

e

.

-Pk

2 fvm::Sp(epsilon/k, k));// Dissipation equationsolve(

fvm::ddt(epsilon)1 fvm::div(phi, epsilon)2 fvm::laplacian(nu/sigmalEps, epsilon)

55C1*Pk*epsilon/k

2 fvm::Sp(C2*epsilon/k, Epsilon));

Reynolds stress models can be implemented in a simmanner. Here, for example, is the implementation of tLaunder–Reece–Rodi~LRR! model equation26 for theReynolds stress tensorR:

volTensorField P 52 (R & fvc::grad(U)) 2 (fvc::grad(U) & R);solve(

fvm::ddt(R)1 fvm::div(phi, R)2 fvm::laplacian(Cs* (k/epsilon)*R, R)

55P

2 fvm::Sp(Cr1*epsilon/k, R)2 (2.0/3.0)* (1.0 2 Cr1)* I*epsilon2 Cr2* (P 2 (1.0/3.0)* I* tr(P))

);

B. LES modeling

The range of possible LES models is, if anything, evlarger than the range of RAS models, and a comparisomade between a large number of different models in R17. Once again, the models can be incorporated direinto the momentum equation, or a virtual class hierarcmay be constructed in order to make the models’ run tiselectable. Unlike the flat hierarchy used for the RAS moels, in this case a three-level system is used~Fig. 1!. Thevarious LES models can be grouped into sets that shcommon characteristics, and it is natural to constructclass hierarchy to match, using derivation to represent

Figure 1. Virtual class hierarchy for LES SGS models. The solid linrepresent the inheritance hierarchy.

COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998 625

Page 7: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

th-thesity

ys

alr,oria

,u-an

or-ity

one i

rethe

dis

r-ingor-exment,nsentuchartlsoingsm,ple,ichrecy.o-

e-onalu-

a

foc-

e

the

edts ofh

relationships between the different models~although thisdoes make the coding somewhat more involved!.

A wide selection of models use the Bousinesq hypoesis, in which the effect of the unresolved turbulence onlarge-scale flow is modeled as an increase in the viscoThis is equivalent to modelingB as

B5 23kI22n tDD , ~9!

with DD5D2 13tr „D…I , and the models differ in the wa

that the turbulent viscosityn t is evaluated. Examples of thimodeling approach include the Smagorinsky27 and one-equation eddy-viscosity models.28,29 A second set of mod-els provides a full solution of the balance equations forB:for example, the model of Deardorff30 has the form

]B

]t1¹–~B^U!2¹–

n t

sB¹B5P2C1

e

kB2

2

3~12C1!Ie

2C2@P2 13 I tr ~P!#. ~10!

Most conventional CFD codes require the six individuequations in Eq.~10! to be written out separately; howevein FOAM these can be expressed as the single tensequation

solve(

fvm::ddt(B)1 fvm::div(phi, B)2 fvm::laplacian(nut/sigmaB, B)

55P

2 fvm::Sp(C1*epsilon/k, B)2 (2.0/3.0)* (1.0 2 C1)* I*epsilon2 C2* (P 2 (1.0/3.0)* I* tr(P))

);

A third set of LES models is the scale-similarity models31

which introduce further interaction between different turblent scales by introducing a second level of filtering and cbe written

B5U% ^ U% 2U^ U. ~11!

These models do not include the effects of dissipation crectly and are usually combined with an eddy-viscostype model to give a mixed model.32

The FOAM LES models class hierarchy is basedthe model relationships described; see Fig. 1. At the basa virtual base classisoLESmodel. Derived from this areintermediate classesisoGenEddyVisc, isoGenSGSStress,and isoGenScaleSimilarity, which implement Eqs.~9!, ~10!,and ~11!, respectively. Finally details of the models aimplemented in the highest-level classes. For example,classes derived fromisoGenEddyVisc calculate the valueof nk used in Eq. ~9!, while those derived fromisoGenSGSStress implement the modeling of Eq.~10!.isoMixedSmagorinsky is a mixture of a scale-similarity anan eddy-viscosity model, and so multiple inheritanceused to represent this relationship.

626 COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998

.

l

s

III. EXAMPLES: FLOW AROUND A SQUARE PRISM

The flow around bluff bodies usually produces strong votices in the wake, which in turn generate intense fluctuatforces on the body. The prediction of these forces is imptant in many applications. Such flows may involve complphenomena such as unsteady separation and reattachvortex shedding and bimodal behavior, laminar subregioand transition to turbulence, high turbulence, and coherstructures, as well as curved shear layers. Recently sflows have received increasing attention, motivated in pby the engineering demand for accurate predictions but aby the desire to increase understanding of the underlyphysical processes governing these flows. A square primounted in a rectilinear channel, is chosen as a simrepresentative bluff body, the separation points of whare fixed and known, unlike the case of a cylinder, whethe separation points oscillate at the shedding frequenThe flow around and behind the prism is not only temprally but also spatially complex, with direct interaction btween the separated shear layers and regions of irrotatiflow entrained into the wake, requiring high local resoltion.

The flow configuration is presented in Fig. 2 and hascross section of 10h314h, and a length of 20h where theprism has cross sectionh3h. TheRe number based onhand the inlet velocityv` is 21,400. Experimental profiles othe first- and second-order statistical moments of the velity are available from Lynet al.33 (Re521,400) and fromDuraoet al.,34 (Re514,800) atx1 /h51 and 2 and on thecenter line~the positions shown in Fig. 2!. Since all quan-tities are nondimensionalized withh andv` , experimental

profiles of U and v rms from these experiments should b

able to be compared directly. At the inlet,U5vx,` and¹ pex50, whereex is the unit normal vector in thexx di-

rection; at the outlet,p5p` and¹Uex50. For RAS, addi-tional boundary conditions have to be prescribed fork ande. The RAS calculations are two-dimensional, whereas

Figure 2. Geometry and inlet conditions for flow past a square-basprism. The coordinate system is indicated, and the distances are in unithe square height h54 cm. The dotted lines indicate the lines along whicthe velocity profiles are measured.

Page 8: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

ee-

ong a

e

ss

fhethere

ime

-forta

at

enithd-of

clene-ap-e

ofd

th-aterels

elESlex

ndan-tyof

wiseta-onom

en

5)],delcityandx/hbot

ed

LES are three-dimensional, and thus a semiartificial frslip condition, Uex50 and (¹Uek)ek50, k5y, z, is ap-plied on the planesxI e350 and 4h. For the top and bottomboundaries,x/h567h, slip conditions are applied whileno-slip conditions are used for the boundary conditionthe prism. The computational domain is discretized usin250,000-cell grid.

Results are shown in Fig. 3 from LES using thSmagorinsky27 and one-equation eddy-viscosity29 models,from RS using the Launder–Gibson Reynolds stremodel,35 and the standard and RNG36 k2e models. Thefunction of the comparison is to illustrate the flexibility oFOAM for model implementation rather than to analyze tmerits of the various models; a detailed analysis ofmodels will be described elsewhere. Hence, only a repsentative set of results is presented, these being the taveraged velocitiesU ~streamwise, that is, along thex axis!andV ~in the y direction!. There is a significant and unexplained discrepancy between the Lyn and Durao datathe U component on the center line, with the Lyn da

Figure 3. Comparison between velocity components for the experim(symbols) and for calculations (profiles) using standard k2e, RNG k2e, Reynolds stress [using a model of Launder and Gibson (Ref. 3and LES using the Smagorinsky and one-equation eddy-viscosity moThe top diagram shows the U (streamwise) component of the veloalong the center of the domain. The next two figures show the U (left)V (transverse, right) velocity components on a transverse plane at51, that is, one obstacle height downstream from the obstacle. Thetom two figures show the same profiles at x/h52.

--

failing to show the expected return to the inlet velocitylarge distances from the obstacle. The simplek2e modelperforms poorly in this case, a result that has also benoted elsewhere. The other models all perform well, wthe one-equation eddy-viscosity model and the LRR moels predicting the near obstacle flow well. The accuracyall models in capturing the recirculation behind the obstacan also be enhanced by increasing the degree of refiment in this area. Further downstream all three modelsproach the data of Duraoet al. The other results shown arthe U and V components in they direction atx/h51,2.Only the top half of the data is shown. The inadequacythek2e model is clear. All the other models provide goopredictions for theU component, with the main differencebetween the LES and RS models being the lack of smooness in the curves, which is probably due to inadequtemporal averaging. TheV component shows much largediscrepancies between the models, with the LES modoutperforming the RS model atx/h51. Further down-stream no model performs well. Overall, the LRR modproduces results that are very similar to those of the Lmodels; however, this is not the case for more compgeometries.

Figure 4 shows a perspective view of the flow arouand behind the prism at one instant in time selected rdomly from a LES with the one-equation eddy-viscosimodel. The top part of Fig. 4 shows an isosurfacesubgrid-scale turbulent kinetic energyk, and the bottom an

isosurface of grid-scale enstrophy@ 12(¹3U)2#. The grid-

scale enstrophy traces the presence of the primary spanvortices, which are generated by Kelvin–Helmholtz insbilities in the shear layers originating from the separatipoints at the upstream corners of the prism and shed fr

t

s.

-

Figure 4. Visualization of SGS kinetic energy (top) and enstrophy12~¹

3U!2, (bottom) behind a square prism. The calculation was performusing the one-equation eddy viscosity LES model.

COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998 627

Page 9: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

ar.the

the

usidlaras

a

othbe

pre-pat-Theher-is

ing

t

alternate sides of the prism. It also traces the secondstreamwise vortices that envelop the primary vorticesktraces the subgrid-scale turbulence that arises frombreakdown of these vortices and correlates well withvortex distribution.

IV. OTHER EXAMPLES

A. Stress analysis

The traditional approach to solid-body stress analysis ising the FEM. However, the governing equations for fluflow and for solid-body stress analysis are of a simiform, indicating that the FVM is also applicable as wdemonstrated by Demirdzˇic and Muzaferija.37,38This is notjust a tour de force: problems involving solidification offluid ~welding or casting, for example! are most easily

e overlcu-n.es

al-ac-

hertwoThtheap

onthethe

heera

628 COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998

y

-

solved if the same solution technique can be utilized in bphases. To demonstrate the ease with which FOAM canused to develop a structures code, such a code will besented, together with the simple example of the stresstern generated by a circular hole in a rectangular plate.plate material is assumed to be linear, elastic, and isotmal, and the assumption of infinitesimal deformationmade. Under these circumstances the displacementD canbe solved for as a dependent quantity without introducthe complexity of how this affects the mesh.

The time-dependent equation for the displacemenDis

]2D

]t2 5¹–@m~¹D1¹DT!1lI tr~¹D!#, ~12!

the FOAM representation of which is

for(runTime11; !runTime.end( ); runTime11)$

for(int iCorr 5 0; iCorr,nCorr; iCorr11)$

volTensorField gradU 5 fvc::grad(U);solve(

fvm::d2dt2(U)55

fvm::laplacian(2*mu 1 lambda, U)1 fvc::div

(mu*gradU.T( ) 2 (mu 1 lambda)*gradU 1 lambda* I* tr(gradU)

));

%%

s aa

n.ble.lyna-

redd-gle

th

n-the

Note the split into implicitfvm and explicitfvc parts. Thisparticular rearrangement guarantees diagonal dominancthe matrix and good convergence behavior. Iteration othe equation is required every time step for transient calation in order to converge over the explicit contributioFor steady-state calculation, the inner iteration is unnecsary.

Figure 5 shows the computational domain for the cculation of the stress field in a 2-D square plate withcircular hole in it. The plate is being stretched in one diretion by uniform traction of 104 Pa. This is a relativelysmall load, and so the problem can be considered isotmal, linear, elastic, and steady-state. Since the plate isdimensional, the assumption of plane stress is made.hole is central to the plate, and so only one quarter ofdomain need be simulated, symmetry boundaries beingplied on the left and bottom. The mesh for this calculaticontained 15,000 cells. The top boundary, together withhole, are given zero-traction boundary conditions, whileright boundary has fixed traction of 104 Pa. The results areshown in Fig. 6 in the form of contours ofsxx , sxy , andsyy . Apart from a small region in the plate’s center, tagreement is close. The error is in the region where sev

f

-

--e

-

l

blocks of cells making up the mesh meet; this producesuboptimal arrangement of cells. The error is probablyresult of the nonorthogonality of the mesh in this regioThe stress concentration around the hole is clearly visiThis comparison shows that the solution is qualitativecorrect; to demonstrate its quantitative accuracy, the alytical solution on the edge of the hole can be compawith the values from the FOAM calculation on this bounary. The results are shown in Fig. 7 as a function of anaround the hole~Q50° being thex direction, that of theimposed strain!. The accuracy achieved is very good, withe maximum error being less than 2%.

B. Magnetohydrodynamics calculation

The flow of an incompressible conducting fluid with costant properties in a magnetic field is governed byNavier–Stokes equations

¹–U50~13!]U

]t1¹–~U^U!2¹–n¹U52

1

r¹p1

1

rJ3B,

together with Maxwell’s equations,

Page 10: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

grmis

nintd in

d

ids-

ent,on

us, in

sst:

rero-

.

¹3H5J1]D

]t, ¹3E52

]B

]t,

~14!¹–B50, ¹–J50,

and Ohm’s lawJ5s(E1U3B). To conform to acceptedpractice in electrodynamics,B is the magnetic field (B5mH) and D the electric displacement vector. Assuminthat ]D/]t is negligible, then

J3B5~¹3H!3B52¹B2

2m1

1

mB¹B, ~15!

and so the momentum equation takes the form

]U

]t1¹–~U^U!2¹–n¹U52

1

r¹S p1

B2

2m D1

1

rm¹–~B^B!. ~16!

Also,

¹3H5J5s~E1U3B!, ~17!

and taking the curl results in

Figure 5. Geometry of the domain for stress calculation. The varioboundary types are indicated. In theory the domain should be infinitepractice a domain 16 m on each side seemed to be adequate.

Figure 6. Comparison of calculated (top) and analytical (bottom) strecomponents for a square plate with a circular hole. From left to righsxx , sxy , andsyy .

]B

]t1¹–~U^B2B^U!2¹–

1

sm¹B50. ~18!

These two transport equations for the system are in a fothat can be solved using FOAM. The pressure equationconstructed from the total pressure~static pressurep plusthe magnetic pressureB2/2m!. A fictitious magnetic-fluxpressurepH is introduced into the magnetic-field equatioto facilitate the obeyance of the divergence-free constraon B in the same manner as the pressure equation is usePISO. The resulting fieldpH has no physical meaning, anat convergence represents the discretization error.

A simple magnetohydrodynamics~MHD! test case isthe flow of an electrically conducting incompressible flubetween parallel insulating plates with an applied tranverse magnetic field. Figure 8 shows such an arrangemknown as the Hartmann problem. The analytical solutifor the velocity and magnetic field profiles are

Figure 7. Variation of stress with angle around the hole. Curves acalculated from the analytical solution, while the symbols represent pfiles extracted from the boundary field of the FOAM computation.Q590° is the x direction (the direction of applied strain).

Figure 8. Geometry and symbol definition for the Hartmann problem

COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998 629

Page 11: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

bers Mmagnetic

Figure 9. Comparison between computed profiles (symbols) and analytical profiles (solid lines) for MHD flow in a channel at Hartmann num51, 5, and 20. Shown on the left is the normalized velocity as a function of distance across the duct, and on the right is the nondimensionalizedfield component parallel to the flow.

s-s

isdebee oasehe

helat-ionnsofandonioung,writallybery

s.

ta-rip-e

theby

ionP

ev-nd

led

blesn-

omealofwto

nsorth-ti-

Mnalinberreorecale of

ar-in

lev-ew

U

v05

cosh~y0 /d!2cosh~y/d!

cosh~y0 /d!21,

B

v0mAsnr52S ~y/y0!sinh~y0 /d!2sinh~y/d!

cosh~y0 /d!21 D ,

~19!

d51

B0Arn

s,

whereM5y0 /d is the Hartmann number. Figure 9 showthe analytical profiles ofU andB compared with a calculation on a 4000-cell mesh. ForM51 the computed resultare exact, which is not too surprising, since the profilequadratic and FOAM uses second-order numerics byfault. As the Hartmann number increases, the profilescome steeper, with greater curvature towards the edgthe duct. The error increases somewhat, although incremesh resolution~for example, a refined mesh towards twall! will reduce this error.

V. CONCLUSIONS

As a discipline, CCM involves a wide range of issues: tphysics of continua, the mathematical issues of manipuing the governing equations into a form capable of solut~for example, the derivation of the LES and RAS equatiofor the mean flow!, the mathematical and physical issuesmodeling terms in these equations, and the numericalalgorithmic issues of constructing efficient matrix inversiroutines and accurate differencing schemes for the varderivatives. In a traditional, procedural approach to codithe modeled equations have to be discretized and thenten out in terms of components before coding can rebegin. Since a typical general-purpose CFD code will;100,000 lines of code, the implementation of this is vechallenging.

The aim of this work was to produce a C11 classlibrary ~FOAM! with which it is easy to develop CCMcodes to investigate modeling and simulation of fluid flow

630 COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998

--fd

s

-

In essence, FOAM is a high-level language, a CCM melanguage, that closely parallels the mathematical desction of continuum mechanics. As well as simplifying thimplementation of new models, this makes checkingmodeling more straightforward. This aspect is enhancedthe inclusion of features such as automatic dimenschecking of operations. In addition to this, use of OOmethodology has enabled the dissociation of different lels of the code, thus minimizing unwanted interaction, apermitting research into the numerics~differencingschemes and matrix-inversion techniques! to be largely di-vorced from that of modeling.

A number of key properties of OOP that have enabthe development of FOAM are the following.

~1! The use of data encapsulation and data hiding enalower-level details of the code, such as the implemetation of the basic tensorial classes, to be hidden frview at the higher levels. This enables a user to donly with aspects of the code relevant to the levelcoding that they are involved in; for example, a neSGS model can be implemented without the needaccess~or even know about! the details of how thecomponents of a tensor are being stored. Since a teis not simply a group of components but is a maematical object in its own right, this is also aesthecally pleasing.

~2! Operator overloading allows the syntax of the CCmetalanguage to closely resemble that of conventiomathematics. This enhances legibility at high levelsthe code. Although this approach increases the numof copying operations somewhat and involves motemporary storage than would be the case for a mtraditional code, this has not been found to be a criticost, and is more than compensated for by the easuse of the resulting code.

~3! Function overriding and the use of virtual class hierchies enable new low-level algorithms to be includeda manner that can be entirely transparent at higherels. For example, constructing and implementing n

Page 12: A tensorial approach to computational continuum mechanics ...powerlab.fsb.hr/ped/kturbo/OpenFOAM/docs/Foam.pdf · A tensorial approach to computational continuum mechanics using object-oriented

nceail--

gh-

one, abendhiscannguselsll.

ionitde

msl-deionall

hlya

es,

toin-ac-anTheenthas

up-910

sed.

ival,

SLA

, and

l.

l.

-

pl.

pl.

bo-

ch.

lop-

No.

, J.

he

pe-

g.

differencing schemes are complicated tasks; oimplemented, the differencing scheme should be avable to all high-level codes. By implementing a common interface for all differencing schemes throufunction callsdiv, grad, curl, ddt, etc., this can be accomplished.

~4! Polymorphism can also be used to give a commstructure to higher-level elements of the code. Henccommon framework for all LES SGS models canconstructed in the form of a virtual base class, aspecific SGS models can be derived from this. Tprovides a mechanism whereby parts of the codebe switched at run time; it also assists in partitionithe code into independent sections. By appropriateof derivation, the implementation of the SGS modecan be made to reflect their internal structure as we

~5! The distinction between interface and implementatand the rigorous hiding of the implementation makepossible to replace whole sections of the low-level coif this should become necessary, without problehigher up.~In particular, the implementation of paralelization via domain decomposition can be matransparent to the top level through our implementatof interprocessor boundaries. Because of this,FOAM codes parallelize immediately.!

~6! Code reuse, which is a major feature of OOP, is higachievable in FOAM, since all CFD codes sharenumber of common features: differencing schemsolvers, tensor algebra, etc.

In summary, the application of OOP techniquesCCM has resulted in a powerful and flexible code forvestigating numerics, modeling, and flow physics. Intertion between these levels has been kept to a minimum,this makes the code easy to extend and maintain.FOAM project demonstrates that it is possible to implema mathematically oriented CCM metalanguage anddemonstrated its benefits.

ACKNOWLEDGMENTS

The LES work used as an example in this article was sported by the EPSRC under Grants No. 43902 and K20and by European Gas Turbines.

REFERENCES

1. C. A. J. Fletcher,Computational Techniques for Fluid Dynamic,Springer Series in Computational Physics Vols. I and II, 2nd~Springer, Berlin, 1991!.

2. J. H. Ferziger and M. Peric´, Computational Methods for Fluid Dy-namics~Springer, Berlin, 1996!.

3. H. K. Versteeg and W. Malalasekera,An Introduction to Computa-

d

tional Fluid Dynamics: The Finite Volume Method~Longman Scien-tific and Technical, 1995!.

4. B. W. R. Forde, R. O. Foschi, and S. F. Stiemer, Comput. Struct.34,355 ~1990!.

5. B. Stroustrup, Proceedings of the 1st European Software Fest1991.

6. B. Stroustrup,The C11 Programming Language, 3rd ed.~Addison–Wesley, Reading, MA, 1997!.

7. B. Stroustrup, OOPS Messenger, 1995, an addendum to the OOP’95 Proceedings.

8. J. R. Cary, S. G. Shasharina, J. C. Cummings, J. V. W. ReyndersP. J. Hinker, Comput. Phys. Commun.~submitted!; available at http://jove.colorado.edu/ cary/CompCPP–F90SciOOP.html.

9. Y. Dubois-Pe`lerin and Th. Zimmermann, Comput. Methods AppMech. Eng.108, 165 ~1993!.

10. J-L. Liu, I.-J. Lin, M.-Z. Shih, R.-C. Chen, and M.-C. Hseih, AppNumer. Math.21, 439 ~1996!.

11. Th. Zimmermann, Y. Dubois-Pe`lerin, and P. Bomme, Comput. Methods Appl. Mech. Eng.98, 291 ~1992!.

12. L. Machiels and M. O. Deville, ACM Trans. Math. Softw.23, 32~1997!.

13. Th. Zimmermann and D. Eyheramendy, Comput. Methods ApMech. Eng.132, 259 ~1996!.

14. D. Eyheramendy and Th. Zimmermann, Comput. Methods ApMech. Eng.132, 277 ~1996!.

15. H. Jasak, Ph.D. thesis, Imperial College, 1996.16. O. Ubbink, Ph.D. thesis, Imperial College, 1997.17. C. Fureby, G. Tabor, H. Weller, and A. D. Gosman, Phys. Fluids9,

1416 ~1997!.18. S. Meyer,Effective C11 ~Addison–Wesley, Reading, MA, 1992!.19. M. R. Hestens and E. L. Steifel, J. Res.29, 409 ~1952!.20. D. A. H. Jacobs, Technical report, Central Electricity Research La

ratories, 1980.21. H. A. van der Vorst, SIAM J. Comput.13, 631 ~1992!.22. R. I. Issa, J. Comput. Phys.62, 40 ~1986!.23. R. I. Issa, A. D. Gosman, and A. P. Watkins, J. Comput. Phys.62, 66

~1986!.24. B. E. Launder and D. B. Spalding, Comput. Methods Appl. Me

Eng.3, 269 ~1974!.25. V. C. Patel, W. Rodi, and G. Scheuerer, AIAA J.23, 1308~1985!.26. B. E. Launder, G. J. Reece, and W. Rodi, ‘‘Progress in the deve

ment of a Reynolds-stress Turbulence Closure,’’ J. Fluid Mech.68,537 ~1975!.

27. J. Smagorinsky, Mon. Weather Rev.91, 99 ~1963!.28. U. Schumann, J. Comput. Phys.18, 376 ~1975!.29. A. Yoshizawa, Phys. Fluids A29, 2152~1986!.30. J. W. Deardorff, Trans. ASME, Ser. I: J. Fluids Eng.156, 55 ~1973!.31. J. Bardina, J. H. Ferziger, and W. C. Reynolds, Technical Report

TF-19, Stanford University, 1983.32. G. Erlebacher, M. Y. Hussaini, C. G. Speziale, and T. A. Zang

Fluid Mech.238, 155 ~1992!.33. D. Lyn, S. Einav, W. Rodi, and J. Park, Technical report, Karlsru

University, 1994.34. D. F. G. Durao, M. V. Heitor, and J. C. F. Pereira, Exp. Fluids6, 298

~1988!.35. M. M. Gibson and B. E. Launder, J. Fluid Mech.86, 491 ~1978!.36. V. Yakhot, S. A. Orszag, S. Thangam, T. B. Gatski, and C. G. S

ziale, Phys. Fluids A4, 1510~1992!.37. I. Demirdzic and S. Muzaferija, Int. J. Numer. Methods Eng.37, 3751

~1994!.38. I. Demirdzic and S. Muzaferija, Comput. Methods Appl. Mech. En

125, 235 ~1995!.

COMPUTERS IN PHYSICS, VOL. 12, NO. 6, NOV/DEC 1998 631