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

Set (Initial) Internal Fields Using #codeStream

Register Blogs Community New Posts Updated Threads Search

In this blog, quick reference notes about OpenFOAM are posted in a form of a summary to address a specific topic per post.
Rating: 2 votes, 4.50 average.

Set (Initial) Internal Fields Using #codeStream

Posted April 23, 2012 at 14: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 3976 Comments 2 Edit Tags Email Blog Entry
« Prev     Main     Next »
Total Comments 2

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)
  2. Old Comment
    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 merge
    permalink
    Posted October 31, 2019 at 22:04 by SamMallinson SamMallinson is offline
 

All times are GMT -4. The time now is 16:51.