CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums

Set (Initial) Internal Fields Using #codeStream

Register Blogs Members List Search Today's Posts Mark Forums Read

In this blog, quick reference notes about OpenFOAM are posted in a form of a summary to address a specific topic per post, which I think could be helpful as a reminder for my Alzheimer condition
Rate this Entry

Set (Initial) Internal Fields Using #codeStream

Posted April 23, 2012 at 13:55 by Hisham

To set up (initial) internal fields of variables according to an expression, one can use the #codeStream feature as:
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
Posted in Uncategorized
Views 1251 Comments 1 Edit Tags Email Blog Entry
« Prev     Main     Next »
Total Comments 1

Comments

  1. Old Comment

    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,

    Sam
    permalink
    Posted February 5, 2015 at 02:21 by SamMallinson SamMallinson is offline
    Updated February 24, 2015 at 19:25 by SamMallinson (Figured out how to do what I asked)
 

All times are GMT -4. The time now is 22:03.