You are on page 1of 66

A brief Introduction to fvOptions References

OpenFOAM Technology Training

sourceflux

June 2016

OpenFOAM Technology Training 1


A brief Introduction to fvOptions References

Disclaimer

OpenFOAM® and OpenCFD® are registered trademarks of OpenCFD Limited,


the producer OpenFOAM software. All registered trademarks are property of
their respective owners. This offering is not approved or endorsed by OpenCFD
Limited, the producer of the OpenFOAM software and owner of the
OPENFOAM® and OpenCFD® trade marks. Jens Höpken, Tomislav Maric and
sourceflux are not associated to OpenCFD.

© Jens Höpken, Tomislav Maric and sourceflux 2016. Unauthorized use and/or
duplication of this material without express and written permission from this
blog’s author and/or owner is strictly prohibited. Excerpts and links may be used,
provided that full and clear credit is given to Jens Höpken, Tomislav Maric and
sourceflux with appropriate and specific direction to the original content.

OpenFOAM Technology Training 2


TL;DR

<sarcasm>

3
A brief Introduction to fvOptions References

Outline

Learning goals

Understanding fvOptions
Capabilities
Setup the fvOptions dictionary
Prototype new sources

OpenFOAM Technology Training A brief Introduction to fvOptions Outline 4


A brief Introduction to fvOptions References

Fundamentals

Fundamentals

General management of source terms.


Generic.
Somewhat similar to function objects and turbulence models.
(Same Design Pattern) → Repetitive Software Building Blocks.
Interchangable.
Easy to adobt.

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 5


A brief Introduction to fvOptions References

Fundamentals

Supported Solvers

Almost all

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 6


A brief Introduction to fvOptions References

Fundamentals

Supported Solvers

Equations affected:
Momentum
Energy
Scalar
Temperature Almost all

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 6


A brief Introduction to fvOptions References

Fundamentals

Supported Solvers

Equations affected:
Momentum
Energy
Scalar
Temperature Almost all

You have to take care for your own solvers.

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 6


A brief Introduction to fvOptions References

Fundamentals

Usage

Defined in system/fvOptions.
Spatially selected using sets or zones.
Any number sources can be used.
Sourceterms are:
calculated at runtime.
usually not written to the case.
composed of explicit and linearized-implicit components.

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 7


A brief Introduction to fvOptions References

Fundamentals

Mathematical Background

Source Term S (x )

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 8


A brief Introduction to fvOptions References

Fundamentals

Mathematical Background

Source Term S (x )

Explicit part Su

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 8


A brief Introduction to fvOptions References

Fundamentals

Mathematical Background

Source Term S (x )

Explicit part Su Linearized implicit part Sp · x

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 8


A brief Introduction to fvOptions References

Fundamentals

Mathematical Background

Source Term S (x )

Explicit part Su Linearized implicit part Sp · x

Source terms must be linearized before being discretized Patankar [1980], Jasak
[1996].

S (x ) = Su + Sp x

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 8


A brief Introduction to fvOptions References

Fundamentals

sources vs. constraints

Sources are:
applied before solving respective equations.
most commonly used.

Constraints are:
applied after respective transport equation is solved.
overriding specific values in the matrix explicitely.
used less frequently.
interfering with the solution after the equation is solved.

OpenFOAM Technology Training A brief Introduction to fvOptions Fundamentals 9


A brief Introduction to fvOptions References

General Source Terms

General Source Terms

Only 2 exist.
No specific application.
No specific equation.

1 semiImplicitSource → Su and Sp coefficient pairs.


2 codedSource → Constructs user defined source terms.

Brief example for 1.


Hands on for 2.

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 10


A brief Introduction to fvOptions References

General Source Terms

semiImplicitSource I - Background

template<class Type>
void Foam::fv::SemiImplicitSource<Type>::addSup
(
fvMatrix<Type>& eqn,
const label fieldI
)
{
//...

UIndirectList<Type>(Su, cells_) = injectionRate_[fieldI].first()/VDash_;

UIndirectList<scalar>(Sp, cells_) = injectionRate_[fieldI].second()/VDash_;

//...

eqn += Su + fvm::SuSp(Sp, psi);


}

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 11


A brief Introduction to fvOptions References

General Source Terms

semiImplicitSource I - Background

template<class Type>
void Foam::fv::SemiImplicitSource<Type>::addSup
(
fvMatrix<Type>& eqn,
const label fieldI
)
{
//...

UIndirectList<Type>(Su, cells_) = injectionRate_[fieldI].first()/VDash_;

UIndirectList<scalar>(Sp, cells_) = injectionRate_[fieldI].second()/VDash_;

//...

eqn += Su + fvm::SuSp(Sp, psi);


}

What do injectionRate_ and VDash_ stand for?

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 11


A brief Introduction to fvOptions References

General Source Terms

semiImplicitSource II - Background

//- Source field values for Su and Sp


List<Tuple2<Type, scalar> > injectionRate_;

//- Volume normalisation


scalar VDash_;

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 12


A brief Introduction to fvOptions References

General Source Terms

semiImplicitSource II - Background

//- Source field values for Su and Sp


List<Tuple2<Type, scalar> > injectionRate_;

//- Volume normalisation


scalar VDash_;

injectionRate_ is a List (length of existing fields) of a Tuple2 of Type


and scalar:

U ( (0 0 0) 0 );

Explicit part Su Linearized implicit part Sp · x

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 12


A brief Introduction to fvOptions References

General Source Terms

semiImplicitSource III - Usage

semiImplicit
{
type vectorSemiImplicitSource;
active true;
selectionMode cellZone;
cellZone semi;

vectorSemiImplicitSourceCoeffs
{
volumeMode absolute;
injectionRateSuSp
{
U ((1 1 1) 0);
}
}
}

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 13


A brief Introduction to fvOptions References

General Source Terms

semiImplicitSource III - Usage

selectionMode can also be pointList.


Is defined for any Type (read: scalar, vector, tensor, etc.).
Su and Sp can be defiend individually.
No specific application → fairly versatile.

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 14


A brief Introduction to fvOptions References

General Source Terms

codedSource I - Background

User coded.
Runtime compiled.
No need for an extra customized library to be compiled.
Somewhat similar to codedFunctionObject and
codedBoundaryCondition.
Deligates compilation to codedBase (very important).
Plugs into hooks, that are called at certain (predefined) points.

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 15


A brief Introduction to fvOptions References

General Source Terms

codedSource I - Background

User coded.
Runtime compiled.
No need for an extra customized library to be compiled.
Somewhat similar to codedFunctionObject and
codedBoundaryCondition.
Deligates compilation to codedBase (very important).
Plugs into hooks, that are called at certain (predefined) points.

Hooks:
codeInclude
codeCorrect
codeAddSup
codeSetValue
code

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 15


A brief Introduction to fvOptions References

General Source Terms

codedSource II - Hooks in simpleFoam


UEqn.H of simpleFoam

tmp<fvVectorMatrix> UEqn
(
fvm::div(phi, U)
+ turbulence->divDevReff(U)
==
fvOptions(U)
);

UEqn().relax();

fvOptions.constrain(UEqn());

solve(UEqn() == -fvc::grad(p));

fvOptions.correct(U);

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 16


A brief Introduction to fvOptions References

General Source Terms

codedSource II - Hooks in simpleFoam


UEqn.H of simpleFoam

tmp<fvVectorMatrix> UEqn fvOptions(U)


( Is a List<fvOption> and calls
fvm::div(phi, U) addSup<Type> for each of them.
+ turbulence->divDevReff(U)
==
fvOptions(U)
);

UEqn().relax();

fvOptions.constrain(UEqn());

solve(UEqn() == -fvc::grad(p));

fvOptions.correct(U);

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 16


A brief Introduction to fvOptions References

General Source Terms

codedSource II - Hooks in simpleFoam


UEqn.H of simpleFoam

tmp<fvVectorMatrix> UEqn fvOptions(U)


( Is a List<fvOption> and calls
fvm::div(phi, U) addSup<Type> for each of them.
+ turbulence->divDevReff(U)
==
fvOptions(U)
); fvOptions.constrain(UEqn.H)
Manipulates the matrix, after assembly,
UEqn().relax(); but before solving it.

fvOptions.constrain(UEqn());

solve(UEqn() == -fvc::grad(p));

fvOptions.correct(U);

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 16


A brief Introduction to fvOptions References

General Source Terms

codedSource II - Hooks in simpleFoam


UEqn.H of simpleFoam

tmp<fvVectorMatrix> UEqn fvOptions(U)


( Is a List<fvOption> and calls
fvm::div(phi, U) addSup<Type> for each of them.
+ turbulence->divDevReff(U)
==
fvOptions(U)
); fvOptions.constrain(UEqn.H)
Manipulates the matrix, after assembly,
UEqn().relax(); but before solving it.

fvOptions.constrain(UEqn());

solve(UEqn() == -fvc::grad(p));
fvOptions.correct(U)
Corrects the field values, solved for.
fvOptions.correct(U);

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 16


A brief Introduction to fvOptions References

General Source Terms

codedSource III - Usage

energySource
{
type scalarCodedSource;

active true;
selectionMode all;

scalarCodedSourceCoeffs
{
}

sourceTimeCoeffs
{
// Dummy entry
}
}

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 17


A brief Introduction to fvOptions References

General Source Terms

codedSource IV - Usage

fieldNames (U);
redirectType sourceTime;

codeInclude
#{
#};
codeCorrect
#{
#};
codeAddSup
#{
#};
codeSetValue
#{
#};
code
#{
$codeInclude
$codeCorrect
$codeAddSup
$codeSetValue
#};

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 18


A brief Introduction to fvOptions References

General Source Terms

codedSource V - Usage

codeInclude
#{
// All required additional includes.
//
// An example for this would be:
#include "scalar.H"
#};

Add header files here.

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 19


A brief Introduction to fvOptions References

General Source Terms

codedSource VI - Usage

codeCorrect
#{
// Code that goes into void correct.
//
// An example for this would be:
Pout<< "I'm correcting now" << endl;
#};

Pure C++, OpenFOAM-style code.


Hooks into overloaded abstract void member function:
virtual void correct(volScalarField& fld);

Exists for all volume fields.

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 20


A brief Introduction to fvOptions References

General Source Terms

codedSource VII - Usage

codeAddSup
#{
// Code that goes into void addSup.
// This is where the source terms are added to the RHS.
// Using this creates a "source".
//
// An example for this would be:
scalarField& fooSource = eqn.source();
fooSource -= time.value();
#};

Pure C++, OpenFOAM-style code.


Hooks into overloaded abstract void member function (exists for all tensor
ranks):
virtual void addSup
(
fvMatrix<vector>& eqn,
const label fieldI
);

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 21


A brief Introduction to fvOptions References

General Source Terms

codedSource VIII - Usage

codeSetValue
#{
// Code that goes into void setValue.
// This is where values can be overridden, after solving the
// respective equations. Using this creates a "constraint".
//
// An example for this would be:
Pout<< "I'm setting values now" << endl;

#};

Pure C++, OpenFOAM-style code.


Hooks into overloaded abstract void member function (exists for all tensor
ranks):
virtual void setValue
(
fvMatrix<vector>& eqn,
const label fieldI
);

OpenFOAM Technology Training A brief Introduction to fvOptions General Source Terms 22


A brief Introduction to fvOptions References

Derived Source Terms

Derived Source Terms

8 exist.
Usually model specific.
MRF got was removed after being encapsulated m(.

1 actuationDiskSource → Propeller model, distributed uniformly.


2 effectivenessHeatExchangerSource → Heat exchanger.
3 explicitPorositySource → Expicit Porosity.
4 meanVelocityForce → Applies a force to maintain a specified mean velocity.
5 radialActuationDiskSource → Propeller model, distributed radially.
6 rotorDiskSource → BEM code for wind turbines.
7 solidificationMeltingSource → Model solidification and melting.
8 tabulatedAccelerationSource → Tablulated 6-DoF motion (prescribed).

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 23


A brief Introduction to fvOptions References

Derived Source Terms

What’s the deal with MRFSource?


MRF.correctBoundaryVelocity(U);

tmp<fvVectorMatrix> UEqn
(
fvm::div(phi, U)
+ MRF.DDt(U)
+ turbulence->divDevReff(U)
fvOptions(U)
);

UEqn().relax();

fvOptions.constrain(UEqn());

solve(UEqn() == -fvc::grad(p));

fvOptions.correct(U);

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 24


A brief Introduction to fvOptions References

Derived Source Terms

actuationDiskSource I - Background

Propeller / actuation disk model.


Distributes thrust T uniformly.
Source term is calcualted by:

Vi
 
S= TE · Uupstream (1)
Vtotal

Calculate thrust with:

T = 2ρA |Uupstream | · a (1 − a) (2)

Coefficient a:

CP
a =1− (3)
CT

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 25


A brief Introduction to fvOptions References

Derived Source Terms

actuationDiskSource II - Usage
disk
{
type actuationDiskSource;
active on;

actuationDiskSourceCoeffs
{
cellSet disk;
selectionMode cellSet;
fieldNames (U);
diskDir (1 0 0);
Cp 0.4;
Ct 0.6;
diskArea 1.2;
upstreamPoint (1 1 1);
}
}

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 26


A brief Introduction to fvOptions References

Derived Source Terms

actuationDiskSource III - Example

Get the repository


?> run
?> git clone https://bitbucket.org/sourceflux/sourceflux-training-code
?> cp -r sourceflux-training-code/tutorials/unitCube .
?> cd unitCube

Create the mesh and scale it by the factor of 10:


?> blockMesh
?> scaleMesh -scale 10

Get an example topoSetDict and edit it


?> cp `find $FOAM_TUTORIALS -name topoSetDict | head -1` system/
?> $EDITOR system/topoSetDict

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 27


A brief Introduction to fvOptions References

Derived Source Terms

actuationDiskSource IV - Example

Change the content to the following, save and close:


actions
(
{
name disk;
type cellSet;
action new;
source cylinderToCell;
sourceInfo
{
p1 (4 5 5);
p2 (6 5 5);
radius 2;
}
}
);

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 28


A brief Introduction to fvOptions References

Derived Source Terms

actuationDiskSource V - Example

Execute topoSet
?> topoSet

Get an example fvOptions dictionary and edit it


?> cp `find $FOAM_TUTORIALS -name fvOptions | head -1` system/
?> $EDITOR system/fvOptions

You can usually find an example dicionary in the header of the respective fvOption
you are using. Or not.

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 29


A brief Introduction to fvOptions References

Derived Source Terms

actuationDiskSource VI - Example
disk
{
type actuationDiskSource;
active on;
selectionMode cellZone;
cellZone disk;

actuationDiskSourceCoeffs
{
fieldNames (U);
diskDir (1 0 0);
Cp 0.4;
Ct 0.6;
diskArea 1.2;
upstreamPoint (5 5 5);
}
}

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 30


A brief Introduction to fvOptions References

Derived Source Terms

actuationDiskSource VII - Example

Execute the solver


?> simpleFoam

Convert the set to VTK


?> foamToVTK -latestTime -poly -cellSet disk

View the result in paraview


?> touch foo.foam
?> paraview foo.foam &

Load the VTK set manually into paraview

OpenFOAM Technology Training A brief Introduction to fvOptions Derived Source Terms 31


A brief Introduction to fvOptions References

Prototyping new sources

The Workflow
Use codedSource - it makes your life easier!

Used for first prototyping.


Class hierarchy generated automatically.
“Just” some renaming required.

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 32


A brief Introduction to fvOptions References

Prototyping new sources

The Workflow
Use codedSource - it makes your life easier!

Used for first prototyping.


Class hierarchy generated automatically.
“Just” some renaming required.

reads calls
system/fvOptions Solver Application codedBase

is linked to
creates sources
Library and compiles to

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 32


A brief Introduction to fvOptions References

Prototyping new sources

The Workflow
Use codedSource - it makes your life easier!

Used for first prototyping.


Class hierarchy generated automatically.
“Just” some renaming required.

reads calls
system/fvOptions Solver Application codedBase

is linked to
creates sources
Library and compiles to

Perfectly suited for prototyping.


Just as codedFunctionObject and codedFvPatchField.

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 32


A brief Introduction to fvOptions References

Prototyping new sources

How would you start programming a custom fvOption source?

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 33


A brief Introduction to fvOptions References

Prototyping new sources

How would you start programming a custom fvOption source?

1 Copy an existing one.


2 Rename the files.
3 Rename the class(es).
4 Refactor the code: get rid of. . .
1 Obsolete member functions.
2 Obsolete attributes.
3 Obsolete includes.
4 Obsolete inheritances.
5 Hope it compiles after you’ve done that.
6 Start implementing your desired functionality.
7 Hope it can be done, the way you desired.
1 If not, just wasted 3 hours of typing.
2 Need a new keyboard .

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 33


A brief Introduction to fvOptions References

Prototyping new sources

And how to do that with codedSource?

1 Implement functionality using codedSource.


2 Copy the generated source files to a different directory.
3 Clean them up:
1 Rename class names.
2 Rename library to compile to.

sed will be your best friend!


4 Compile them.
5 Perform further development there.

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 34


A brief Introduction to fvOptions References

Prototyping new sources

And how to do that with codedSource?

1 Implement functionality using codedSource.


2 Copy the generated source files to a different directory.
3 Clean them up:
1 Rename class names.
2 Rename library to compile to.

sed will be your best friend!


4 Compile them.
5 Perform further development there.

No need to resolve any obsolete elements!

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 34


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term I

The source term is defined as explicit:

Su = Ut (4)

1 Copy an existing case:


?> git clone https://bitbucket.org/sourceflux/sourceflux-training-code
?> cp -r sourceflux-training-code/tutorials/unitCube codedSource
?> cd codedSource
?> cp `find $FOAM_TUTORIALS -name fvOptions | head -1` system

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 35


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term II


Open system/fvOptions with an editor of you choice.

code
{
type vectorCodedSource;
active true;
Templated on Type.
vectorCodedSourceCoeffs Type basically bassed via type entry.
{ Add actual type as prefix.
selectionMode all;
}

sourceTimeCoeffs
{
// Don't add anything
}
}

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 36


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term II


Open system/fvOptions with an editor of you choice.

code
{
type vectorCodedSource;
active true;
Templated on Type.
vectorCodedSourceCoeffs Type basically bassed via type entry.
{ Add actual type as prefix.
selectionMode all;
}
Only incremental changes shown in the
sourceTimeCoeffs following.
{
// Don't add anything
}
}

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 36


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term III

vectorCodedSourceCoeffs
{
fieldNames (U);
redirectType sourceTime;

codeInclude
#{
// We don't need any, at this point.
// If we would, these could be added like the following:
// #include "scalar.H"
#};
}

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 37


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term IV

vectorCodedSourceCoeffs
{
// [..]
codeCorrect
#{
// Nothing to add here either, for this applicaition
// If we would, these could be added like the following:
// fld = fld*10;
#};
}
Remember, this hooks into:
virtual void correct(volScalarField& fld);
It provides non-constant access to the respective field.

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 38


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term V


Now we start implementing the desired functionality:
vectorCodedSourceCoeffs
{
// [..]
codeAddSup
#{
vectorField& vectorSource = eqn.source();
vectorSource -= mesh_.time().value()*vectorSource;
#};
}

Utilize constant access to the fvMesh.

Remember, this hooks into:


virtual void addSup
(
fvMatrix<vector>& eqn,
const label fieldI
);

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 39


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term VI

vectorCodedSourceCoeffs
{
// [..]
codeSetValue
#{
// Not needed for this example
#};
}

Remember, this hooks into:


virtual void setValue
(
fvMatrix<vector>& eqn,
const label fieldI
);

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 40


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term VII

Assemble the obligatory code entry, that get’s passed to codedBase:


vectorCodedSourceCoeffs
{
// [..]
code
#{
$codeInclude
$codeCorrect
$codeAddSup
$codeSetValue
#};
}

Similar to codedFunctionObject.

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 41


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term VIII

Start building up the case, but save system/fvOptions first!


?> blockMesh
?> sed -i -e "s/endTime.*/endTime 1;/g" \
-e "s/deltaT.*/deltaT 1;/g" system/controlDict
Once these steps are finished, execute the solver:
?> simpleFoam
This compiles the codedSource, links it to the solver and starts it.

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 42


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term VIII

Start building up the case, but save system/fvOptions first!


?> blockMesh
?> sed -i -e "s/endTime.*/endTime 1;/g" \
-e "s/deltaT.*/deltaT 1;/g" system/controlDict
Once these steps are finished, execute the solver:
?> simpleFoam
This compiles the codedSource, links it to the solver and starts it.

And now?

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 42


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term IX

A new directory is created: dynamicCode


?> ls dynamicCode
platforms sourceTime

?> ls dynamicCode/sourceTime
codedFvOptionTemplate.C codedFvOptionTemplate.dep
codedFvOptionTemplate.H lnInclude/
Make/

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 43


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term IX

A new directory is created: dynamicCode


?> ls dynamicCode
platforms sourceTime

?> ls dynamicCode/sourceTime
codedFvOptionTemplate.C codedFvOptionTemplate.dep
codedFvOptionTemplate.H lnInclude/
Make/

Does this look familiar?

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 43


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term X

Let’s have a closer look:


?> cat dynamicCode/sourceTime/codedFvOptionTemplate.C | \
head -164 | tail -17
void sourceTimeFvOptionvectorSource::addSup
(
fvMatrix<vector>& eqn,
const label fieldI
)
{
if (false)
{
Info<< "sourceTimeFvOptionvectorSource::addSup()\n";
}
//{{{ begin code
#line 39 "/home/.../codedSource/system/fvOption.code.vectorCodedSourceCoeffs"
vectorField& vectorSource(eqn.source());
vectorSource -= mesh_.time().value()*vectorSource;
//}}} end code
}

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 44


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term XI

Cleaning source-code up:


1 Rename the source files:
?> cd dynamicCode/sourceTime
?> ls codedFvOption*.[C,H]
./codedFvOptionTemplate.C
./codedFvOptionTemplate.H
?> mv codedFvOptionTemplate.C sledgeHammer.C
?> mv codedFvOptionTemplate.H sledgeHammer.H

2 Rename sourceTimeFvOptionvectorSource to
sledgeHammerFvOptionvectorSource:
?> sed -i "s/sourceTimeFvOptionvectorSource/sledgeHammerFvOptionvectorSource/g" \
*.[C,H]

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 45


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term XII

3 Remove preprocessor macros, we don’t need them.


4 Change the target library LIB in Make/files:

sledgeHammerFvOptionvectorSource.C

LIB = $(FOAM_USER_LIBBIN)/sledgeHammer

5 Check if it compiles:
?> wmake libso

6 If it does, copy the new library to some safe place.

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 46


A brief Introduction to fvOptions References

Prototyping new sources

A Time-Dependent Source Term XII

3 Remove preprocessor macros, we don’t need them.


4 Change the target library LIB in Make/files:

sledgeHammerFvOptionvectorSource.C

LIB = $(FOAM_USER_LIBBIN)/sledgeHammer

5 Check if it compiles:
?> wmake libso

6 If it does, copy the new library to some safe place.

Your library is now ready to be used and/or developed further!

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 46


A brief Introduction to fvOptions References

Prototyping new sources

Hrvoje Jasak. Error analysis and estimation for the finite volume method with
applications to fluid flows. PhD thesis, Imperial College London, 1996.
S. Patankar. Numerical Heat Transfer and Fluid Flow. Series in computational
methods in mechanics and thermal sciences. Taylor & Francis, 1980. ISBN
9780891165224.

OpenFOAM Technology Training A brief Introduction to fvOptions Prototyping new sources 47

You might also like