In this blog, quick reference notes about OpenFOAM are posted in a form of a summary to address a specific topic per post.
Set (Initial) Internal Fields Using #codeStream
Posted April 23, 2012 at 14:55 by Hisham
Tags codestream, field, initial, internal, openfoam
To set up (initial) internal fields of variables according to an expression, one can use the #codeStream feature as:
Reference: the example from the Doxygen manual
Code:
internalField #codeStream { code #{ const IOdictionary& d = static_cast<const IOdictionary&>(dict); const fvMesh& mesh = refCast<const fvMesh>(d.db()); scalarField fld(mesh.nCells(), 12.34);// uniform value of the field as 12.34 or just fld(mesh.nCells()) // to set a position-dependent value forAll(mesh.C(), i) { fld[i] = 4. * mesh.C()[i].x();// 4 * cell_X_Coordinate } fld.writeEntry("", os); // now return the value of fld as the internal field value #}; codeInclude #{ #include "fvCFD.H" #}; codeOptions #{ -I$(LIB_SRC)/finiteVolume/lnInclude #}; };
Reference: the example from the Doxygen manual
Total Comments 2
Comments
-
set initial vector field
Hi Hisham,
I was interested to read this post of yours.
Here is how you do the same for a vector field: put the following in a file called "initialCondition" and then #include it in the file 0/U and set internalField as $velocity. NB, it sets a Poiseuille profile for a parallel plate channel with the walls at +/- h/2.
=====
velocity #codeStream
{
code
#{
double h = 1;
double Umax = 0.5;
const IOdictionary& d = dynamicCast<const IOdictionary>(dict);
const objectRegistry& db = d.db();
volVectorField& fld =
const_cast<volVectorField&>(db.lookupObject<volVec torField>("U"));
const fvMesh& mesh = fld.mesh();
// to set a position-dependent value
forAll(mesh.C(), i)
{
double yloc = mesh.C()[i].y();
fld[i] = vector(Umax * (1.0 - pow(2.0 * yloc / h, 2)), 0.0, 0.0);
}
os << fld.internalField();
#};
codeInclude
#{
#include "fvCFD.H"
#};
codeOptions
#{
-I$(LIB_SRC)/finiteVolume/lnInclude
#};
codeLibs
#{
-lfiniteVolume
#};
};
#inputMode merge
=====
Cheers,
SamPosted February 5, 2015 at 02:21 by SamMallinson
Updated February 24, 2015 at 19:25 by SamMallinson (Figured out how to do what I asked) -
For OpenFOAM-7, the following works
===
velocity #codeStream
{
code
#{
double h = 5.0;
double Umax = 2.0;
const IOdictionary& d = static_cast<const IOdictionary&>(dict);
const fvMesh& mesh = refCast<const fvMesh>(d.db());
vectorField fld(mesh.nCells(), vector(0.0, 0.0, 0.0));
// to set a position-dependent value
forAll(mesh.C(), i)
{
double yloc = mesh.C()[i].y();
fld[i] = vector(Umax * (1.0 - pow(2.0 * yloc / h, 2)), 0.0, 0.0);
}
writeEntry(os,"",fld);
#};
codeInclude
#{
#include "fvCFD.H"
#};
codeOptions
#{
-I$(LIB_SRC)/finiteVolume/lnInclude \
-I$(LIB_SRC)/meshTools/lnInclude
#};
codeLibs
#{
-lfiniteVolume \
-lmeshTools
#};
};
#inputMode mergePosted October 31, 2019 at 22:04 by SamMallinson