CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   PROBLEMS DEFINING A LIST USING #CODESTREAM !! Help please !! (https://www.cfd-online.com/Forums/openfoam-programming-development/229659-problems-defining-list-using-codestream-help-please.html)

angatri_14 August 18, 2020 08:07

PROBLEMS DEFINING A LIST USING #CODESTREAM !! Help please !!
 
Dear foamers,
I'm trying to change the initial condition of my case using #codeStream procedure after some time steps. The idea is to add a modelled vortex after some iterations. However, I'm not able to overlap both fields, the vortex and the solution from the last time step. The problem is that I'm not a C++ user, so I don't know how to define a list (scalarField or vectorField) inside the #codeStream directive using the results from the last time step.

I have tried to use the #include directive creating a file with the list for both pressure and velocity results from the last time step, but it did not work. Also, I have seen the different list constructors but I think that anyone fits with my case (using the list presented in p and U file for the last time step).

This is my code for the initial conditions using #codeStream :
Code:


/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  v1912                                |
|  \\  /    A nd          | Website:  www.openfoam.com                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      ascii;
    class      volScalarField;
    location    "0.22";
    object      p;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 2 -2 0 0 0 0];

internalField #codeStream
{

    codeInclude
    #{
        #include "fvMesh.H"
        #include "fvCFD.H"
       
    #};

    codeOptions
    #{
        -I$(LIB_SRC)/finiteVolume/lnInclude \
        -I$(LIB_SRC)/meshTools/lnInclude
    #};
    codeLibs
    #{
        -lmeshTools \
        -lfiniteVolume
    #};


    code
    #{
        const IOdictionary& d = static_cast<const IOdictionary&>(dict);
        const fvMesh& mesh = refCast<const fvMesh>(d.db());

        scalarField p(mesh.nCells(), 0.);

        scalar Cv = 0.0412; // Vortex strenght
        scalar Rv = 0.05; // Vortex core radius
        scalar rho_inf = 1.225; //Ambient density [kg/m^3]

        scalar x_v =0.1; // X vortex-centre position [m]
        scalar y_v = 0; // Y vortex-centre position [m]

        forAll(p, i)
        {
            const scalar x = mesh.C()[i][0];
            const scalar y = mesh.C()[i][1];
           
          //Here I want to define a scalarFied with the pressure values from the last time step called list_pressure

            p[i] = -rho_inf/2*pow(Cv/Rv, 2)*exp(-((pow(x-x_v,2))+(pow(y-y_v,2)))/(pow(Rv,2)))+list_pressure[i];

           
        }

        //void Foam::Field<Type>::writeEntry(const word& keyword, Ostream& os) const

        p.writeEntry("", os);

    #};

};



boundaryField
{
    inlet
    {
        type            zeroGradient;
    }
    outlet
    {
        type            fixedValue;
        value          uniform 0;
    }
    upperWall
    {
        type            zeroGradient;
    }
    lowerWall
    {
        type            zeroGradient;
    }
    frontAndBack
    {
        type            empty;
    }
}


// ************************************************************************* //

and the file "initialConditions" with the pressure field for the last time step (the list I have to use in #codeStream)
Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  v1912                                |
|  \\  /    A nd          | Website:  www.openfoam.com                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      ascii;
    class      volScalarField;
    location    "0.22";
    object      p;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 2 -2 0 0 0 0];

internalField #codeStream
{

    codeInclude
    #{
        #include "fvMesh.H"
        #include "fvCFD.H"
       
    #};

    codeOptions
    #{
        -I$(LIB_SRC)/finiteVolume/lnInclude \
        -I$(LIB_SRC)/meshTools/lnInclude
    #};
    codeLibs
    #{
        -lmeshTools \
        -lfiniteVolume
    #};


    code
    #{
        const IOdictionary& d = static_cast<const IOdictionary&>(dict);
        const fvMesh& mesh = refCast<const fvMesh>(d.db());

        scalarField p(mesh.nCells(), 0.);

        scalar Cv = 0.0412; // Vortex strenght
        scalar Rv = 0.05; // Vortex core radius
        scalar rho_inf = 1.225; //Ambient density [kg/m^3]

        scalar x_v =0.1; // X vortex-centre position [m]
        scalar y_v = 0; // Y vortex-centre position [m]

        forAll(p, i)
        {
            const scalar x = mesh.C()[i][0];
            const scalar y = mesh.C()[i][1];
           
         

            p[i] = -rho_inf/2*pow(Cv/Rv, 2)*exp(-((pow(x-x_v,2))+(pow(y-y_v,2)))/(pow(Rv,2)));

           
        }

        //void Foam::Field<Type>::writeEntry(const word& keyword, Ostream& os) const

        p.writeEntry("", os);

    #};

};



boundaryField
{
    inlet
    {
        type            zeroGradient;
    }
    outlet
    {
        type            fixedValue;
        value          uniform 0;
    }
    upperWall
    {
        type            zeroGradient;
    }
    lowerWall
    {
        type            zeroGradient;
    }
    frontAndBack
    {
        type            empty;
    }
}


// ************************************************************************* //

Any help is useful and really appreciated !! Thanks you all.

joegi.geo August 21, 2020 17:16

Well, you cannot add initial conditions after the solution have been computed.
It is an initial condition, just used to get your solver started, then your field will be whatever the solver computes.



In your case, I guess it is better to add a source term to the equations. You can do it using fvOptons or a programmable source term using codeStream.



You don't need tables, you need to program the mathematical function that describes that source term. Accessing tables with codeStream is not very easy, or even not possible at all (I don't recall).


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