You are on page 1of 72

OpenFOAM

clpjoy@njut.edu.cn

OpenFOAM

OpenFOAM

OpenFOAM
Open Source Field Operation and Manipulation
C++
library

www.openfoam.com

1. OpenFOAM
OpenFOAM
application

solver

utility

2. OpenFOAM
(1)

(2)

(3)

2)
3

udf user subroutine


OpenFOAM

CFD

Qri
Qcdi
Qcvi
m+1

Qcvi m

finiteVolume OpenFOAM

OpenFOAM
NVD TVD 40

CFD

OpenFOAM

solver

Case

blockMeshDict

FoamFile
{
version 2.0;
format ascii;
class
dictionary;
object blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * //
convertToMeters 0.1;

vertices
(
(0 0 0)
(1 0 0)
(1 1 0)
(0 1 0)
(0 0 0.1)
(1 0 0.1)
(1 1 0.1)
(0 1 0.1)
);
blocks
(
hex (0 1 2 3 4 5 6 7) (20 20 1) simpleGrading (1 1 1)
);
edges
(
);

patches
(
wall movingWall
(
(3 7 6 2)
)
wall fixedWalls
(
(0 4 7 3)
(2 6 5 1)
(1 5 4 0)
)
empty frontAndBack
(
(0 3 2 1)
(4 5 6 7)
)
);


blockMesh


paraFoam

system/controlDict, controlDict
FoamFile
{
version 2.0;
format ascii;
class
dictionary;
object controlDict;
}
// * * * * * * * * * * * * * * * * * * //
application icoFoam;
startFrom
startTime;
start Time
0;
stopAt
endTime;
endTime
0.5;
deltaT
0.005;

writeControl timeStep;
writeInterval 20;
purgeWrite 0;
writeFormat ascii;
writePrecision 6;
uncompressed/compressed;
writeCompression uncompressed;
timeFormat general;
timePrecision 6; //
runTimeModifiable yes; //

system/fvSolution

solvers //
{
p PCG //
{
preconditioner DIC;
tolerance
1e-06; //
relTol
0; //
};
U PBiCG //
{
preconditioner DILU; // LU
tolerance
1e-05;//
relTol
0; //
};
}

PISO //piso
{
nCorrectors 2;//
nNonOrthogonalCorrectors 0;//
pRefCell
0;// cell index
pRefValue
0;//
}


system/fvSchemes

ddtSchemes
{
default
Euler; //
}
gradSchemes //
{
default
Gauss linear;//
grad(p)
Gauss linear; //
}

divSchemes //
{
default
none;
//
div(phi,U) Gauss linear;//

}
laplacianSchemes //
{
default
none; //
laplacian(nu,U) Gauss linear corrected; //

laplacian((1|A(U)),p) Gauss linear corrected;//

interpolationSchemes //
{
default
linear; //
interpolate(HbyA) linear;//
}
snGradSchemes//
{
default
corrected;//
}
fluxRequired //
{
default
no; //
p;//

0
FoamFile
{
version

2.0;

format

ascii;

class

volScalarField;

object

p;

}
// * * * * * * * * * * ** * * * * * * * * * * * //
dimensions

[0 2 -2 0 0 0 0];

internalField uniform 0;

boundaryField
{
movingWall
{type

zeroGradient; }

fixedWalls
{type

zeroGradient; }

frontAndBack
{ type

empty; }

}
// *********************************** //


dimensions [0 1 -1 0 0 0 0];
internalField uniform (0 0 0);
boundaryField
{
movingWall
{type
fixedValue;
value
uniform (1 0 0);
}
fixedWalls
{type
fixedValue;
value
uniform (0 0 0);
}
frontAndBack
{type
empty;
}
}

transportProperties
FoamFile
{
version 2.0;
format ascii;
class
dictionary;
location "constant";
object transportProperties;
}
// * * * * * * * * * * * * * * * * * * * * * //
nu
nu [ 0 2 -1 0 0 0 0 ] 0.01;

icoFoam

createFields.H
Info<< Reading transportProperties\n << endl;
//
IOdictionary transportProperties
(
IOobject
(
transportProperties,
runTime.constant(), //
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);

dimensionedScalar nu
(
transportProperties.lookup("nu")
);
//
Info<< "Reading field p\n" << endl;
volScalarField p
(
IOobject
(
"p",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);

//
Info<< "Reading field U\n" << endl;
volVectorField U
(
IOobject
(
"U",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);

//
# include "createPhi.H"
// cell index
label pRefCell = 0;
scalar pRefValue = 0.0;
// system/fvSolution PISO
cell
setRefCell(p, mesh.solutionDict().subDict("PISO"),
pRefCell, pRefValue);

icoFoam.C
int main(int argc, char *argv[])
{
#
#
#
#
#

include "setRootCase.H"
include "createTime.H"
include "createMesh.H"
include "createFields.H"
include "initContinuityErrs.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

Info<< \nStarting time loop\n << endl;


// runTime
for (runTime++; !runTime.end(); runTime++)
{
//
Info<< Time = << runTime.timeName() <<
nl << endl;
# include readPISOControls.H // piso

runTime

# include "CourantNo.H

//
fvVectorMatrix UEqn
(
fvm::ddt(U)
+ fvm::div(phi, U)
- fvm::laplacian(nu, U)
);
solve(UEqn == -fvc::grad(p));

u
i

u
ju
i
xj

u
i

p
=
t

xj
xj
xi

// --- PISO loop----


for (int corr=0; corr<nCorr; corr++)
{
//
volScalarField rUA = 1.0/UEqn.A();
U = rUA*UEqn.H(); //
//
phi = (fvc::interpolate(U) & mesh.Sf())
+ fvc::ddtPhiCorr(rUA, U, phi);
//
adjustPhi(phi, U, p);

//
for (int nonOrth=0; nonOrth<=nNonOrthCorr; nonOrth++)
{
fvScalarMatrix pEqn //
(
fvm::laplacian(rUA, p) == fvc::div(phi)
);
pEqn.setReference(pRefCell, pRefValue);
pEqn.solve();
//
if (nonOrth == nNonOrthCorr)
{
phi -= pEqn.flux();
}
}

//
# include "continuityErrs.H
U -= rUA*fvc::grad(p); //
// (
U.correctBoundaryConditions();
}// PISO

//
runTime.write();// CPU
Info<< ExecutionTime = <<
runTime.elapsedCpuTime() << s
<< ClockTime = << runTime.elapsedClockTime() <<
s
<< nl << endl;
} //
Info<< End\n << endl;
return(0);
)//

Make/files
icoFoam.C

EXE = $(FOAM_APPBIN)/icoFoam

Make/options
EXE_INC =
-I$(LIB_SRC)/finiteVolume/lnInclude
EXE_LIBS =
-lfiniteVolume

wmake

icoFoam
jC
C u

=
c
+SC

xj
xj
xj

a icoFoam
b) c
c) C c
d) T
e) T


1) cicoFoam
2)
creatFields.H
cicoFoam.C
3 cicoFoam

cicoFoam
1. icoFoam
cicoFoam .
cd cicoFoam
2.
icoFoam.C cicoFoam.C
icoFoam.dep
3. files options
Make files
icoFoam.C cicoFoam.C
EXE = $(FOAM_APPBIN)/icoFoam
EXE = $(FOAM_USER_APPBIN)/cicoFoam

options

4. obj
5. wmake
cicoFoam

2)
creatFields.H
cicoFoam.C

CreateFields.H

CreateFields.H

dimensionedScalar c
//
(
transportProperties.lookup(" c ")
);

CreateFields.H

Info<< "Reading field C" << endl;


volScalarField C
(
IOobject
(
C",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
CreateFields.H
mesh

);

cicoFoam.C
cicoFoam.C PISO
fvScalarMatrix Ceqn

(
fvm::ddt(C)
+ fvm::div(phi, C)

- fvm::laplacian(c , C)
);
Ceqn.solve();

cicoFoam.C

3 cicoFoam
wmake

cicoFoam

1.
2.
3.

1.

(1) cavity ccavity


(2) transportProperties c
cd constant
transportProperties c =0.002m2/s
c

c [0 2 -1 0 0 0 0] 0.002;

(3) C
0 p C
C

2
A system
fvSchemes
divSchemes
{
default
none;
div(phi,U) Gauss upwind;
div(phi,C) Gauss upwind;
}
laplacianSchemes
{
default
none;
laplacian(nu,U) Gauss linear corrected;
laplacian(c ,C) Gauss linear corrected;
laplacian((1|A(U)),p) Gauss linear corrected;
}

fvSolution

C PBiCG
{
preconditioner DILU;
tolerance
1e-06;
relTol
0;
};

3
cicoFoam . ccavity

dC
= f(C, t)
dn

newgrad

1. newgrad
2.

.H

. C

3.
4.

1. newgrad
(1)
OpenFOAM-1.X/src/finiteVolume/fields/
fvpatchFields/derived totalTemperature
2
totalTemperature newgrad,
totalTemperature
newgrad.

2. newgradFvPatchScalarField.H
(1) newgradFvPatchScalarField.H

class
totalTemperatureFvPatchScalarField
:
public
fixedValueFvPatchScalarField

class
newgradFvPatchScalarField
:
public
fixedGradientFvPatchScalarField

2
// Private data
word UName_;

// Private data

word phiName_;

word CC_;

word psiName_;

scalarField gradient_;

scalar gamma_;
scalarField T0_;

3
//- Runtime type information
TypeName("totalTemperature");

//- Runtime type information


TypeName("newgrad");

const scalarField& T0() const


{
return T0_;
}
scalarField& T0()
{
return T0_;
}
const scalarField& gradient() const
{
return gradient_;
}
scalarField& gradient()
{
return gradient_;
}

2. newgradFvPatchScalarField.C

updateCoeffs()

nst volScalarField&
C = db().lookupObject<volScalarField>(C");

adient_ = f(C);

erator==
(
this->patchInternalField()
+ gradient_/this->patch().deltaCoeffs()
);

3. OpenFOAM-1.x\src\finiteVolume\Make files

$(derivedFvPatchFields)/newgrad/
newgradFvPatchField.C

4. 4
OpenFOAM-1.x\src\finiteVolume
4
wmake libso

newgrad

OpenFOAM

Thank you for listening!

You might also like