OpenFOAM Tutorials:
Programming Session 2
Hrvoje Jasak and Henrik Rusche
[email protected], [email protected]
Wikki, United Kingdom and Germany
Advanced Training at the OpenFOAM Workshop
21.6.2010, Gothenborg, Sweden
OpenFOAM Tutorials:Programming Session 2 – p. 1
Troubleshooting this tutorial
• oscillatingCylinder.git is a Git repository which contains snapshots of the input data
and source code in it’s branches
• The slides have references to those branches
• Something like: In trouble? This is in branch initU
• With this information you can use
git checkout -f initU
to go to initU (throwing away your changes)
• or use
git checkout -f initU file1 file2 dir
to select individual files or directories
• or use
cd $HOME/oscillatingCylinder.git
rm -rf *
git checkout -f initU
to start at a given point. Note that you still have to compile and blockMesh!
OpenFOAM Tutorials:Programming Session 2 – p. 2
Unpacking the case (Git repository)
• Unpack Git repo
cd $HOME
tar xf /cdrom/OFW5/Advanced_Training/oscillatingCylinder.git.tgz
cd $HOME/oscillatingCylinder.git
• Make the mesh
blockMesh
• Inspect the mesh
• You want to go back here? This is in branch start
OpenFOAM Tutorials:Programming Session 2 – p. 3
Run icoFoam and change inlet BC
• You know how to do this!
cd $HOME/oscillatingCylinder.git
icoFoam
• Does it run? Look at the result!
• Change the BC on U to oscillatingFixedValue
inlet
{
type oscillatingFixedValue;
refValue uniform (1 0 0);
amplitude 0.2;
frequency 5;
value uniform (1 0 0);
}
• Does it run? Look at the result! Dis they change?
• In trouble? This is in branch oscInlet
OpenFOAM Tutorials:Programming Session 2 – p. 4
Copy oscillatingFixedValue BC for Mod
• Get it from the disto
cp -r $FOAM_SRC/finiteVolume/fields/fvPatchFields/derived/ \\
oscillatingFixedValue newOscillatingFixedValue
wclean
• Create a Make directory
• Create files and options
• Make/files:
newOscillatingFixedValueFvPatchFields.C
LIB = $(FOAM_USER_LIBBIN)/libuserBC
• Make/options:
EXE_INC = \
-I$(LIB_SRC)/finiteVolume/lnInclude
LIB_LIBS = \
-lfiniteVolume
OpenFOAM Tutorials:Programming Session 2 – p. 5
Rename oscillatingFixedValue BC for Mod
• Rename the source files
rename "s/oscillatingFixedValue/newOscillatingFixedValue/g" *H
rename "s/oscillatingFixedValue/newOscillatingFixedValue/g" *C
• Modify the source
sed -i "s/oscillatingFixedValue/newOscillatingFixedValue/g" *H
sed -i "s/oscillatingFixedValue/newOscillatingFixedValue/g" *C
• Add some output in updateCoeffs()
template<class Type>
void newOscillatingFixedValueFvPatchField<Type>::updateCoeffs()
{
if (this->updated())
{
return;
}
Info << "newOscillatingFixedValueFvPatchField<Type>"
<< "::updateCoeffs()" << endl;
• Does it compile? Does it run? In trouble? This is in branch copyBC
OpenFOAM Tutorials:Programming Session 2 – p. 6
Modify the H-file
• Add additional member variables
// Private data
//- Reference value
Field<Type> refValue_;
//- Amplitude
scalar amplitude_;
//- Amplitude
scalar amplitude2_;
//- Frequency
scalar frequency_;
//- Frequency
scalar frequency2_;
OpenFOAM Tutorials:Programming Session 2 – p. 7
Modify the C-file (1)
• Change currentScale()
template<class Type>
scalar newOscillatingFixedValueFvPatchField<Type>::currentScale
(
) const
{
return
1.0
+ amplitude_*sin
(
2*mathematicalConstant::pi*frequency_*this->db().time().value()
)
+ amplitude2_*sin
(
2*mathematicalConstant::pi*frequency2_*this->db().time().value()
);
}
• Change the five constructors (all of them!)
OpenFOAM Tutorials:Programming Session 2 – p. 8
Modify the C-file (2)
• Change write(Ostream& os)
template<class Type>
void newOscillatingFixedValueFvPatchField<Type>::write(Ostream& os) con
{
fvPatchField<Type>::write(os);
refValue_.writeEntry("refValue", os);
os.writeKeyword("amplitude")
<< amplitude_ << token::END_STATEMENT << nl;
os.writeKeyword("frequency")
<< frequency_ << token::END_STATEMENT << nl;
os.writeKeyword("amplitude2")
<< amplitude2_ << token::END_STATEMENT << nl;
os.writeKeyword("frequency2")
<< frequency2_ << token::END_STATEMENT << nl;
this->writeEntry("value", os);
}
• Does it compile?
• Does it run? Look at the result!
• In trouble: This is in branch modifyBC
OpenFOAM Tutorials:Programming Session 2 – p. 9