CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   Get access to temperature in Openfoam (http://www.cfd-online.com/Forums/openfoam/117429-get-access-temperature-openfoam.html)

hz283 May 8, 2013 08:59

Get access to temperature in Openfoam
 
Hi All,

In rhoPimpleFoam, the total enthalpy is solved and based on that the temperature is updated. In creteFields, the enthalpy is defined as

autoPtr<basicPsiThermo> pThermo
(
basicPsiThermo::New(mesh)
);
basicPsiThermo& thermo = pThermo();

volScalarField& p = thermo.p();
volScalarField& h = thermo.h();

I checked the enthalpy function h() in basicThermo.C and found the following:

Foam::volScalarField& Foam::basicThermo::h()
{
notImplemented("basicThermo::h()");
return const_cast<volScalarField&>(volScalarField::null() );
}


There are other three enthalpy function definitions for h(), but the above one should be for the case where the enthalpy is solved as the governing equation.

I checked this because for my own work, I would like to update the temperature not through openfoam's enthalpy equation but through my own model (not solve the energy equations in Openfoam). So should I add the lines for T() as follows in basicThermo.C?

Foam::volScalarField& Foam::basicThermo::T()
{
notImplemented("basicThermo::T()");
return const_cast<volScalarField&>(volScalarField::null() );
}

Actually I also find that some other declare the temperature as follows ():

Info<< "Reading field T\n" <<endl; volScalarField T ( IOobject ( "T", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh );

Which one is correct for temperature update?

Does anybody know something about this? Thanks.

adhiraj May 9, 2013 00:21

Is this what you want to do?
http://openfoamwiki.net/index.php/Ho...ure_to_icoFoam

This is when you want to add another scalar to solve.
If you want to access the temperature from the thermo object and modify it, that needs a bit of more work.

hz283 May 9, 2013 05:32

Thanks. Not the link you mentioned. I am trying to do the latter. The temperature in the link will not affect the thermodynamic models because it is for the incompressible flows. So although it is updated, it will not affect the viscosity.

Thank you again!

Quote:

Originally Posted by adhiraj (Post 426252)
Is this what you want to do?
http://openfoamwiki.net/index.php/Ho...ure_to_icoFoam

This is when you want to add another scalar to solve.
If you want to access the temperature from the thermo object and modify it, that needs a bit of more work.


adhiraj May 9, 2013 06:25

Yes, you are right.
To modify temperature from the top-level solver you need to access the variable "T_" in basicThermo.
You can write a function (say Tmodifiable) that returns nonconstant access to T_.
Code:

volScalarField& T=thermo.Tmodifiable();
Then use T in the equations.

hz283 May 9, 2013 06:39

Yes, I agree with you. As you know, in rhoPimpleFoam, the total enthalpy equation is solved, it corresponds to the following function:

Foam::volScalarField& Foam::basicThermo::h()
{
notImplemented("basicThermo::h()");
return const_cast<volScalarField&>(volScalarField::null() );
}

But I do not know what is meaning of the two lines in this function. Can I similarly write a function for temperature as follows?

Foam::volScalarField& Foam::basicThermo::T()
{
notImplemented("basicThermo::T()");
return const_cast<volScalarField&>(volScalarField::null() );
}


Thank you very much!

best regards,
H

Quote:

Originally Posted by adhiraj (Post 426317)
Yes, you are right.
To modify temperature from the top-level solver you need to access the variable "T_" in basicThermo.
You can write a function (say Tmodifiable) that returns nonconstant access to T_.
Code:

volScalarField& T=thermo.Tmodifiable();
Then use T in the equations.


adhiraj May 9, 2013 06:49

There is a function T() in basicThermo.
Code:

const Foam::volScalarField& Foam::basicThermo::T() const
{
    return T_;
}

Define function similar to this,
Code:

Foam::volScalarField& Foam::basicThermo::TModifiable() const
{
    return T_;
}

This will return nonconstant access to the temperature variable.
In your createFields.H you can use
Code:

volScalarField& T=thermo.TModifiable();
Then simply use the variable T in your solver.

hz283 May 9, 2013 08:49

Thank you very much. In the following:

Foam::volScalarField& Foam::basicThermo::TModifiable() const
{
return T_;
}

Why should the const should be there? if it is there, that means this function cannot change the values of temperature. but actually, when the temperature is updated at each time step, the T_ should change simutaneously. Is this understanding correct? I am not proficient in C++.

Thanks.

Quote:

Originally Posted by hz283 (Post 426322)
Yes, I agree with you. As you know, in rhoPimpleFoam, the total enthalpy equation is solved, it corresponds to the following function:

Foam::volScalarField& Foam::basicThermo::h()
{
notImplemented("basicThermo::h()");
return const_cast<volScalarField&>(volScalarField::null() );
}

But I do not know what is meaning of the two lines in this function. Can I similarly write a function for temperature as follows?

Foam::volScalarField& Foam::basicThermo::T()
{
notImplemented("basicThermo::T()");
return const_cast<volScalarField&>(volScalarField::null() );
}


Thank you very much!

best regards,
H


hz283 May 9, 2013 10:55

Hi Adhiraj,

Thank you very much for your reply. If I make the modifications just mentioned by you, should I just compile it in the folder src/thermophysicalModels/basic (use wmake)? what else should be done to make this modification be active?

Thank you so much.

Quote:

Originally Posted by ;426325
There is a function T() in basicThermo.
Code:

const Foam::volScalarField& Foam::basicThermo::T() const
{
    return T_;
}

Define function similar to this,
Code:

Foam::volScalarField& Foam::basicThermo::TModifiable() const
{
    return T_;
}

This will return nonconstant access to the temperature variable.
In your createFields.H you can use
Code:

volScalarField& T=thermo.TModifiable();
Then simply use the variable T in your solver.


fredo490 May 11, 2013 10:58

Your topic is really interesting and I actually asked the same thing in another topic: http://www.cfd-online.com/Forums/ope...tml#post426707

Did you try the introduction of a new function ?

hz283 May 11, 2013 11:05

Hi

I am trying these days and I think the method mentioned in the above thread can work. Thanks .

Quote:

Originally Posted by fredo490 (Post 426708)
Your topic is really interesting and I actually asked the same thing in another topic: http://www.cfd-online.com/Forums/ope...tml#post426707

Did you try the introduction of a new function ?


fredo490 May 13, 2013 03:51

As told in the other topic, I'm getting close to the solution BUT I HAVE A BUG WITH THE THERMO MODEL.

Here is the procedure I used:
1) open a text editor in admin by writing in the terminal: "sudo gedit"
2) with this text editor, open: "basicThermo.C" located in /opt/openfoam220/src/thermophysicalModels/basic/basicThermo
3) Add the following line in the Member Functions (I put it at line 396 after the "basicThermo::T()" ):
Code:

// Add this
          //- Temperature [K]
            //  Non-const access allowed
            Foam::volScalarField& Foam::basicThermo::T()
            {
                return T_;
            }

4) with this text editor (still admin), open: "basicThermo.H" located in /opt/openfoam220/src/thermophysicalModels/basic/basicThermo
5) Add the following line after the comment "Fields derived from thermodynamic state variables" (I put it at line 316 after the other Temperature member ):
Code:

// Add this
            //- Temperature [K]
            //  Non-const access allowed for transport equations
            virtual volScalarField& T();

6) Save the two files (To check if it was saved: look at the top of the editor, if the star remains it means that you didn't have the admin / root access).
7) Go to /opt/openfoam220/src/thermophysicalModels and open a terminal
8) With the terminal located in this folder, we now want to get the full root access. To do so, write: "sudo -s". After typing your password, you will see that the command line will start with "root".
9) We need to recompile the thermo model of openfoam. To do so, simply write "./Allwmake". This step might take few minutes depending of your system (for me with virtualization, it took about 1 or 2 minutes).

But I think there is a problem with the new compilation because even a simple rhoSimplecFoam doesn't work anymore.

hz283 May 13, 2013 04:04

Hi,

Thank you very much. Since the temperature is updated by T, not from enthalpy. So should I also comment the calculating temperature from enthalpy?


Quote:

Originally Posted by fredo490 (Post 427100)
I have tried some modification and I think I'm getting the solution. I've succeed to compile my custom solver and I will run a test case tonight.

Here is the procedure I used:
1) open a text editor in admin by writing in the terminal: "sudo gedit"
2) with this text editor, open: "basicThermo.C" located in /opt/openfoam220/src/thermophysicalModels/basic/basicThermo
3) Add the following line in the Member Functions (I put it at line 396 after the "basicThermo::T()" ):
Code:

// Add this
          //- Temperature [K]
            //  Non-const access allowed
            Foam::volScalarField& Foam::basicThermo::T()
            {
                return T_;
            }

4) with this text editor (still admin), open: "basicThermo.H" located in /opt/openfoam220/src/thermophysicalModels/basic/basicThermo
5) Add the following line after the comment "Fields derived from thermodynamic state variables" (I put it at line 316 after the other Temperature member ):
Code:

// Add this
            //- Temperature [K]
            //  Non-const access allowed for transport equations
            virtual volScalarField& T();

6) Save the two files (To check if it was saved: look at the top of the editor, if the star remains it means that you didn't have the admin / root access).
7) Go to /opt/openfoam220/src/thermophysicalModels and open a terminal
8) With the terminal located in this folder, we now want to get the full root access. To do so, write: "sudo -s". After typing your password, you will see that the command line will start with "root".
9) We need to recompile the thermo model of openfoam. To do so, simply write "./Allwmake". This step might take few minutes depending of your system (for me with virtualization, it took about 1 or 2 minutes).

You can now use your new thermo model !


fredo490 May 13, 2013 04:17

Just be careful, I have updated my post because I encounter a bug with the thermo model now...

I'm actually running my test on a virtual machine so I don't damage my openfoam installation. I tried to run a tutorial case to check and a bug occurs.

So my solution is NOT valid yet

fredo490 May 13, 2013 04:24

I have made some test and it seems that the newly compiled thermo model has a problem.

I ran a simpleFoam tutorial (pitzDaily) = OK

I ran a rhoSimplecFoam tutorial (squareBend) = Crash

Here is the log:
Code:

Create mesh for time = 0


SIMPLE: convergence criteria
    field p        tolerance 0.001
    field U        tolerance 0.0001
    field e        tolerance 0.001
    field "(k|epsilon|omega)"        tolerance 0.001

Reading thermophysical properties

Selecting thermodynamics package
{
    type            hePsiThermo;
    mixture        pureMixture;
    transport      sutherland;
    thermo          hConst;
    equationOfState perfectGas;
    specie          specie;
    energy          sensibleInternalEnergy;
}

#0  Foam::error::printStack(Foam::Ostream&) at ??:?
#1  Foam::sigSegv::sigHandler(int) at ??:?
#2  in "/lib/x86_64-linux-gnu/libc.so.6"
#3  Foam::PtrList<Foam::fvPatchField<double> >::operator[](int) const at ??:?
#4  Foam::heThermo<Foam::psiThermo, Foam::pureMixture<Foam::sutherlandTransport<Foam::species::thermo<Foam::hConstThermo<Foam::perfectGas<Foam::specie> >, Foam::sensibleInternalEnergy> > > >::alphaEff(Foam::Field<double> const&, int) const at ??:?
#5 
 at ??:?
#6  __libc_start_main in "/lib/x86_64-linux-gnu/libc.so.6"
#7 
 at ??:?
Segmentation fault (core dumped)

I will run some extra test and keep you updated.

fredo490 May 13, 2013 04:34

It is really strange... When I comment the code I've wrote, openFoam runs smoothly again. If I put the code, it crashes all the thermo model.

I will work to find why.

hz283 May 13, 2013 09:53

Dear Fredo,

Thank you. When does it crash? when reading the thermodynamic model or start to solve the momentum equations?



Quote:

Originally Posted by fredo490 (Post 427115)
It is really strange... When I comment the code I've wrote, openFoam runs smoothly again. If I put the code, it crashes all the thermo model.

I will work to find why.


fredo490 May 13, 2013 11:30

If I put any code (even a stupid function) into the basic thermo, all solvers encounter a bug when they start solving the momentum equation. I've tried with a custom solver, it is really the first momentum equation that crashes the solver. The problem doesn't seem to come from my compiler because if I comment my code, all the solvers work again.

I also had another very strange thing with one of my custom solver. I get a non matching dimension between the left and right hand side of the equation instead of the momentum bug.

My knowledge in C++ is really too weak :(

hz283 May 13, 2013 11:34

If I put any code (even a stupid function) into the basic thermo, all solvers encounter a bug when they start solving the momentum equation.

The same symptom as my case. I am working on it..... also crash when the code move into the momentum equation.

Pray to CFD god first...

Quote:

Originally Posted by fredo490 (Post 427226)
If I put any code (even a stupid function) into the basic thermo, all solvers encounter a bug when they start solving the momentum equation. I've tried with a custom solver, it is really the first momentum equation that crashes the solver. The problem doesn't seem to come from my compiler because if I comment my code, all the solvers work again.

I also had another very strange thing with one of my custom solver. I get a non matching dimension between the left and right hand side of the equation.

My knowledge in C++ is really too weak :(


fredo490 May 14, 2013 04:56

Something I really don't understand is that:

basicThermo.C has the following members:
Code:

// Non Constant access to p
Foam::volScalarField& Foam::basicThermo::p()
{
    return p_;
}

// Constant access to p
const Foam::volScalarField& Foam::basicThermo::p() const
{
    return p_;
}

// Constant access to T
const Foam::volScalarField& Foam::basicThermo::T() const
{
    return T_;
}

Then basicThermo.H has the following members:
Code:

        // Access to thermodynamic state variables

            //- Pressure [Pa]
            //  Non-const access allowed for transport equations

            virtual volScalarField& p();

            //- Pressure [Pa]
            virtual const volScalarField& p() const;

            //- Temperature [K]
            virtual const volScalarField& T() const;

Therefor, why the #&{$ adding the same non constant member as p but with T doesn't work ?! If it works for p, why it doesn't work for T ?

hz283 May 14, 2013 06:27

In basicThermo.C and basicthermo.H, the non-constant access function for enthalpy is as follows:

Foam::volScalarFields& Foam::basicThermo::h()
{
notimplemented("basicThermo::h()")
return const_cast<volscalarField&>(volScalarField::null() )
}

is this the function called by the following line in createField.H?

volScalarField& h=thermo.h()


Quote:

Originally Posted by fredo490 (Post 427373)
Something I really don't understand is that:

basicThermo.C has the following members:
Code:

// Non Constant access to p
Foam::volScalarField& Foam::basicThermo::p()
{
    return p_;
}

// Constant access to p
const Foam::volScalarField& Foam::basicThermo::p() const
{
    return p_;
}

// Constant access to T
const Foam::volScalarField& Foam::basicThermo::T() const
{
    return T_;
}

Then basicThermo.H has the following members:
Code:

        // Access to thermodynamic state variables

            //- Pressure [Pa]
            //  Non-const access allowed for transport equations

            virtual volScalarField& p();

            //- Pressure [Pa]
            virtual const volScalarField& p() const;

            //- Temperature [K]
            virtual const volScalarField& T() const;

Therefor, why the #&{$ adding the same non constant member as p but with T doesn't work ?! If it works for p, why it doesn't work for T ?



All times are GMT -4. The time now is 04:41.