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/)
-   -   snippet for a custom variable which is a product of two variables (https://www.cfd-online.com/Forums/openfoam-programming-development/180820-snippet-custom-variable-product-two-variables.html)

babakflame November 30, 2016 20:33

snippet for a custom variable which is a product of two variables
 
Dear Fellows

In my solver, by solving transport equations, I am achieving \rho_e which is bulk charge density and U which is velocity. I have added the following snippet in my createFields.H file:

I want to find the distribution of \rho_e * U_x from my code in which U_x is the component of velocity in x direction.

I have added the following snippet in my createFields.H file:

Code:

volScalarField Ux
        (
            IOobject
            (
                "Ux",
                runTime.timeName(),
                mesh,
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            U.component(vector::X)
        );

volScalarField rhoE     
    (
        IOobject
        (
            "rhoE",
            runTime.timeName(),
            mesh,
            IOobject::READ_IF_PRESENT,
            IOobject::AUTO_WRITE
        ),
        mesh
//    dimensionedScalar("rhoE", dimensionSet(0, -3, 1, 0, 0, 1, 0), scalar(0.0))
    );

  volScalarField rhoEUx
    (
    IOobject
        (
            "rhoEUx",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        rhoE*Ux
    );

And I have added this line to my Ueqn.H


Code:

    rhoEUx = rhoE * Ux;
   
 //    volVectorField temptemp = rhoE*E;


//    volVectorField temptemp = 3e-06*(fvc::grad(UeExt));
    fvVectorMatrix UEqn
    (
        fvm::ddt(rho, U)
      + fvm::div(rhoPhi, U)
      - fvm::laplacian(muEff, U)
      - (fvc::grad(U) & fvc::grad(muEff))
      - tmp2
      - tmp1
    );

    UEqn.relax();

Everything with the solver is fine, However, although the values for \rho_e and U are calculated correctly, the \rho_e * U_x is not correct from the code,


However if I use calculator filter in paraview, I would be able to create the distribution of \rho_e * U_x. Does any body know, where did I make mistake?

Regards

jherb December 1, 2016 11:32

You have to update the fields rhoE and Ux in every timestep, because at least for Ux in createFields.H you only do this once at the beginning of the simulation.

So your code should look something like this:
Code:

// rhoE = ???; // not sure what this is. Do you set this somewhere else in your solver?
Ux = U.component(vector::X);
rhoEUx = rhoE * Ux;


babakflame December 1, 2016 12:58

Many thanks Joachim

I will test it and give the feedback.
This is my main code:

Code:

while (runTime.run())
    {
        #include "readTimeControls.H"
        #include "CourantNo.H"
        #include "alphaCourantNo.H"
        #include "setDeltaT.H"
 
        runTime++;
 
        Info<< "Time = " << runTime.timeName() << nl << endl;
 
        twoPhaseProperties.correct();
 
        #include "alphaEqnSubCycle.H"
        #include "ElectricEqn.H"
    #include "SourceTerm.H"
        //Info<< "ElectricEqn over " << nl << endl;
        // --- Pressure-velocity PIMPLE corrector loop
        while (pimple.loop())
        {
       
            #include "UEqn.H"
 
            // --- Pressure corrector loop
            while (pimple.correct())
            {
                #include "pEqn.H"
            }
 
            if (pimple.turbCorr())
            {
                turbulence->correct();
            }
        }
 
        runTime.write();
 
        Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
            << "  ClockTime = " << runTime.elapsedClockTime() << " s"
            << nl << endl;
    }

\rho_e is calculated in ElectricEqn.H file, and then I need to multiply it with the U_x which is the x component of momentum equation.


I updated the Ueqn.H file as:

Code:


    Ux = U.component(vector::X);

      rhoEUx = rhoE * Ux;
   
 //    volVectorField temptemp = rhoE*E;


//    volVectorField temptemp = 3e-06*(fvc::grad(UeExt));
    fvVectorMatrix UEqn
    (
        fvm::ddt(rho, U)
      + fvm::div(rhoPhi, U)
      - fvm::laplacian(muEff, U)
      - (fvc::grad(U) & fvc::grad(muEff))
      - tmp2
      - tmp1
    );

    UEqn.relax();



Regards

babakflame December 1, 2016 15:29

Many Thanks Joachim.

It seems that the update of U_x at each time step was required, Just by adding the line:

Code:

Ux = U.component(vector::X);
In UEqn.H before everything else, Now everything is fine.:)

Regards

jherb December 1, 2016 17:35

Actually, I think, you don't need the Ux field. You could just use U.component(vector::X) in your calculation of rhoEUx. I would even expect the compiler to optimize it away.

babakflame December 1, 2016 18:06

Most likely, u are correct, since
Code:

U.component(vector::X)
is known by OpenFOAM. So I just needed to use that instead of U_x.
By invoking
Code:

U.component(vector::X)
in the formulation:
Code:

      rhoEUx = rhoE * U.component(vector::X);
it would be updated automatically, too.

Regards

Bdew8556 June 27, 2019 01:46

Hey guys,

Do you know how to create custom variables in postprocessing, ie after the simulation has run? in order to say mucking around with the C and H files?


All times are GMT -4. The time now is 18:19.