Hi Tobi,
I have a class that derives from singlePhaseTransportModel class. Inside this class is a pointer to viscosity model. This pointer was private and now I changed it into protected to be able to use it. So I have an access to viscosity model and nu is calculated inside helium viscosity model class as shown in my previous post. |
Okay, and how do you access the function? I mean, you should have something like that somewhere:
Code:
volScalarField foo = pointerToModel->nuHelium(); |
yes it is inside singlePhaseTransportModel
Code:
Foam::tmp<Foam::volScalarField> Why are you asking? |
Hi, actually I ment something different but it does not matter. I guess I know the answer to your question:
Code:
Foam::tmp<Foam::volScalarField> To get your first approach running, this should be done: Code:
Foam::tmp<Foam::volScalarField> I hope I formulated it in a good way :) ... This will work: Code:
tmp<volScalarField> tnu = viscosityPtr_->nuHelium(); Code:
volScalarField nu = viscosityPtr_->nuHelium(); |
Hi Tobi,
Thank you very much for explanation, now it is clear for me. Unfortunately I still have an error with nuEff() function. This function is run in the line Code:
turbulence->divDevReff(U) Code:
Courant Number mean: 0 max: 0 |
There are too less information on what you changed. So it is not possible to give any advice, sorry.
|
Quote:
Code:
... Code:
nut = turbulence->nut() |
How did you define your object nut. Can you give the definition of that object? And where do you use nut afterwards?
|
nut definition in createFields.H:
Code:
volScalarField nut |
This cannot work because turbulence->nut() will return a tmp<> object which will be destoyed in your case :). So build your own field such as:
Code:
volScalarField nut Code:
Code:
tmp<volScalarField> tnut = turbulence->nut(); |
Quote:
Code:
Foam::tmp<Foam::volScalarField> |
Quote:
Code:
Foam::tmp<Foam::volScalarField> Code:
tmp<volScalarField> tnut = viscouseModel->calcNu(); Quote:
|
Any automatic object created within the body of a function can live untill the execution flow leaves the function. Then a destructor of the object is invoked rendering the object basically dead. A pointer to the automatic object can be incapsulated into a smart pointer object and returned from the function, but it will be useless anyway because the object it points to is dead. If you want an object to stay alive when the execution flow leaves the function you should create it with operator new as a dynamically allocated memory object.
|
Hmmm,.... actually without the new keyword it is not working at all based on the fact that the pointer has to point to an explicit allocated memory build by new. So you are right. Thanks for correcting me. For those who want to test it:
Code:
Foam::tmp<Foam::volScalarField> myCalc (const Foam::fvMesh& mesh) |
Hi, Tobias
Code:
//- Run-Time Error - object destroyed, reference point to some non-allocated memory Code:
const volScalarField& foo = myCalc(mesh)(); |
Hi,
yes it will give an runtime error. If one test the above code, the error appears. I was struggling about that in my openComfort library too because here I need access to the nut field which comes from the turbulence model. Right at the beginning I did this one: Code:
const volScalarField& nut = turbulence->nut(); Code:
const volScalarField nut = turbulence->nut() Code:
const tmp<volScalarField> tnut = turbulence->nut() |
Hopefully I could test it tomorrow and then get back to this thread.
|
I played around with the code and have to confirm that
Code:
const volScalarField& nut = turbulence->nut()(); |
Hmmm.... :/
Sure, it is obvious now. After the operator () we have access to the object and can take the reference. My way was wrong. Should think more before I reply. As always Sergei. Good point. Thanks for sharing. |
Quote:
Thank you guys for explanation all of these things and help. I solved my problem mentioned in #25. It was caused by the bed use of tmp object. I fixed the code according Zeppo solution as in the citation. Also I added this line according to Tobi: Code:
tmp<volScalarField> tnut = turbulence->nut(); |
All times are GMT -4. The time now is 03:09. |