CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

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

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 1 Post By jherb
  • 2 Post By jherb

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 30, 2016, 20:33
Default snippet for a custom variable which is a product of two variables
  #1
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 15
babakflame is on a distinguished road
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
babakflame is offline   Reply With Quote

Old   December 1, 2016, 11:32
Default
  #2
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 21
jherb is on a distinguished road
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 likes this.
jherb is offline   Reply With Quote

Old   December 1, 2016, 12:58
Default
  #3
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 15
babakflame is on a distinguished road
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 is offline   Reply With Quote

Old   December 1, 2016, 15:29
Default
  #4
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 15
babakflame is on a distinguished road
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
babakflame is offline   Reply With Quote

Old   December 1, 2016, 17:35
Default
  #5
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 21
jherb is on a distinguished road
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 and Liangyuan like this.
jherb is offline   Reply With Quote

Old   December 1, 2016, 18:06
Default
  #6
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 15
babakflame is on a distinguished road
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
babakflame is offline   Reply With Quote

Old   June 27, 2019, 01:46
Default
  #7
Senior Member
 
Brett
Join Date: May 2013
Posts: 212
Rep Power: 13
Bdew8556 is on a distinguished road
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?
Bdew8556 is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[OpenFOAM] Integrating a custom variable over a line babakflame ParaView 9 May 19, 2019 19:45
emag beta feature: charge density charlotte CFX 4 March 22, 2011 09:14
error in COMSOL:'ERROR:6164 Duplicate Variable' bhushas COMSOL 1 May 30, 2008 04:35
Env variable not set gruber2 OpenFOAM Installation 5 December 30, 2005 04:27
Replace periodic by inlet-outlet pair lego CFX 3 November 5, 2002 20:09


All times are GMT -4. The time now is 11:07.