|
[Sponsors] |
November 5, 2017, 16:09 |
|
#21 |
Senior Member
Przemek
Join Date: Jun 2011
Posts: 249
Rep Power: 15 |
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.
__________________
best regards pblasiak |
|
November 5, 2017, 16:50 |
|
#22 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
Okay, and how do you access the function? I mean, you should have something like that somewhere:
Code:
volScalarField foo = pointerToModel->nuHelium();
__________________
Keep foaming, Tobias Holzmann |
|
November 6, 2017, 02:35 |
|
#23 |
Senior Member
Przemek
Join Date: Jun 2011
Posts: 249
Rep Power: 15 |
yes it is inside singlePhaseTransportModel
Code:
Foam::tmp<Foam::volScalarField> Foam::singlePhaseTransportModel::nu() const { return viscosityModelPtr_->nu(); } Why are you asking?
__________________
best regards pblasiak |
|
November 6, 2017, 03:38 |
|
#24 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
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> Foam::viscosityModels::Helium::calcNu() { volScalarField nu ( IOobject ( "nuLocal", U_.time().timeName(), U_.db(), IOobject::NO_READ, IOobject::NO_WRITE ), eta_/rho_ ); return nu; } To get your first approach running, this should be done: Code:
Foam::tmp<Foam::volScalarField> Foam::viscosityModels::Helium::calcNu() { tmp<volScalarField> nu ( IOobject ( "nuLocal", U_.time().timeName(), U_.db(), IOobject::NO_READ, IOobject::NO_WRITE ), eta_/rho_ ); return nu; } I hope I formulated it in a good way ... This will work: Code:
tmp<volScalarField> tnu = viscosityPtr_->nuHelium(); //- Do other stuff Code:
volScalarField nu = viscosityPtr_->nuHelium();
__________________
Keep foaming, Tobias Holzmann |
|
November 6, 2017, 07:14 |
|
#25 |
Senior Member
Przemek
Join Date: Jun 2011
Posts: 249
Rep Power: 15 |
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 deltaT = 9.999e-06 PIMPLE: iteration 1 --> FOAM FATAL ERROR: Field<scalar> f1(14000), Field<scalar> f2(0) and Field<scalar> f3(14000) for operation f1 = f2 + f3 From function void Foam::checkFields(const Foam::UList<T>&, const Foam::UList<Key>&, const Foam::UList<Type3>&, const char*) [with Type1 = double; Type2 = double; Type3 = double] in file /home/przemek/OpenFOAM/OpenFOAM-v1606+/src/OpenFOAM/lnInclude/FieldM.H at line 75. FOAM aborting #0 Foam::error::printStack(Foam::Ostream&) at ~/OpenFOAM/OpenFOAM-v1606+/src/OSspecific/POSIX/printStack.C:218 #1 Foam::error::abort() at ~/OpenFOAM/OpenFOAM-v1606+/src/OpenFOAM/lnInclude/error.C:246 #2 Foam::Ostream& Foam::operator<< <Foam::error>(Foam::Ostream&, Foam::errorManip<Foam::error>) at ~/OpenFOAM/OpenFOAM-v1606+/src/OpenFOAM/lnInclude/errorManip.H:85 (discriminator 4) #3 void Foam::checkFields<double, double, double>(Foam::UList<double> const&, Foam::UList<double> const&, Foam::UList<double> const&, char const*) at ~/OpenFOAM/OpenFOAM-v1606+/src/OpenFOAM/lnInclude/FieldM.H:75 #4 void Foam::add<double, double>(Foam::Field<Foam::typeOfSum<double, double>::type>&, Foam::UList<double> const&, Foam::UList<double> const&) at ~/OpenFOAM/OpenFOAM-v1606+/src/OpenFOAM/lnInclude/FieldFunctions.C:770 #5 void Foam::add<double, double, Foam::fvPatchField, Foam::volMesh>(Foam::GeometricField<Foam::typeOfSum<double, double>::type, Foam::fvPatchField, Foam::volMesh>&, Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> const&, Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> const&) at ~/OpenFOAM/OpenFOAM-v1606+/src/OpenFOAM/lnInclude/GeometricFieldFunctions.C:961 #6 Foam::tmp<Foam::GeometricField<Foam::typeOfSum<double, double>::type, Foam::fvPatchField, Foam::volMesh> > Foam::operator+<double, double, Foam::fvPatchField, Foam::volMesh>(Foam::tmp<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> > const&, Foam::tmp<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> > const&) at ~/OpenFOAM/OpenFOAM-v1606+/src/OpenFOAM/lnInclude/GeometricFieldFunctions.C:961 (discriminator 9) #7 Foam::RASModel<Foam::IncompressibleTurbulenceModel<Foam::transportModel> >::nuEff() const at ~/OpenFOAM/OpenFOAM-v1606+/src/TurbulenceModels/incompressible/../turbulenceModels/lnInclude/RASModel.H:226 (discriminator 8) #8 Foam::linearViscousStress<Foam::RASModel<Foam::IncompressibleTurbulenceModel<Foam::transportModel> > >::divDevRhoReff(Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>&) const at ~/OpenFOAM/OpenFOAM-v1606+/src/TurbulenceModels/incompressible/../turbulenceModels/lnInclude/linearViscousStress.C:101 #9 Foam::IncompressibleTurbulenceModel<Foam::transportModel>::divDevReff(Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>&) const at ~/OpenFOAM/OpenFOAM-v1606+/src/TurbulenceModels/incompressible/lnInclude/IncompressibleTurbulenceModel.C:116 #10 ? at ~/OpenFOAM/przemek-v1606+/applications/solvers/heatTransfer/buoyantBoussinesqSuperFluidPimpleFoam/UEqn.H:35 #11 __libc_start_main in "/lib/x86_64-linux-gnu/libc.so.6" #12 ? at ??:? Aborted (core dumped)
__________________
best regards pblasiak |
|
November 6, 2017, 07:46 |
|
#27 | |
Senior Member
Przemek
Join Date: Jun 2011
Posts: 249
Rep Power: 15 |
Quote:
Code:
... nut = turbulence->nut(); tmp<fvVectorMatrix> tUEqn ( fvm::ddt(U) + fvm::div(phi, U) + MRF.DDt(U) + turbulence->divDevReff(U) == fvOptions(U) ); Code:
nut = turbulence->nut()
__________________
best regards pblasiak |
||
November 6, 2017, 07:49 |
|
#28 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
How did you define your object nut. Can you give the definition of that object? And where do you use nut afterwards?
__________________
Keep foaming, Tobias Holzmann |
|
November 6, 2017, 08:01 |
|
#29 |
Senior Member
Przemek
Join Date: Jun 2011
Posts: 249
Rep Power: 15 |
nut definition in createFields.H:
Code:
volScalarField nut ( IOobject ( "nut", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE ), turbulence->nut() );
__________________
best regards pblasiak |
|
November 6, 2017, 08:08 |
|
#30 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
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 ( IOobject ( "nut", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE ), mesh, dimensionedScalar("nut", dimensionSet(...), value(0)) ); Code:
// Keep tmp alive tmp<volScalarField> tnut = turbulence->nut(); // Get reference to nut object volScalarField& nutIF = nut; // Set nut to be tnut from turbulence model nutIF = tnut(); Code:
tmp<volScalarField> tnut = turbulence->nut();
__________________
Keep foaming, Tobias Holzmann |
|
November 6, 2017, 16:44 |
|
#31 | |
Senior Member
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 21 |
Quote:
Code:
Foam::tmp<Foam::volScalarField> Foam::viscosityModels::Helium::calcNu() { tmp<volScalarField> nu ( new volScalarField ( IOobject ( "nuLocal", U_.time().timeName(), U_.db(), IOobject::NO_READ, IOobject::NO_WRITE ), eta_/rho_ ); ) return nu; } |
||
November 6, 2017, 18:10 |
|
#32 | ||
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
Quote:
Code:
Foam::tmp<Foam::volScalarField> Foam::viscosityModels::Helium::calcNu() { tmp<volScalarField> nu ( IOobject ( "nuLocal", U_.time().timeName(), U_.db(), IOobject::NO_READ, IOobject::NO_WRITE ), eta_/rho_ ); return nu; } Code:
tmp<volScalarField> tnut = viscouseModel->calcNu(); Quote:
__________________
Keep foaming, Tobias Holzmann |
|||
November 7, 2017, 16:12 |
|
#33 |
Senior Member
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 21 |
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.
|
|
November 7, 2017, 17:27 |
|
#34 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
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) { tmp<volScalarField> T ( new volScalarField ( IOobject ( "T", mesh.time().timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ) ); return T; } int main(int argc, char *argv[]) { #include "setRootCase.H" #include "createTime.H" #include "createMesh.H" //- Working - keeping object alive tmp<volScalarField> re = myCalc(mesh); Info<< re() << endl; //- Run-Time Error - object destroyed, reference point to some non-allocated memory const volScalarField& foo = myCalc(mesh); Info<< foo << endl; Info<< "End\n" << endl; return 0; }
__________________
Keep foaming, Tobias Holzmann |
|
November 8, 2017, 16:56 |
|
#35 |
Senior Member
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 21 |
Hi, Tobias
Code:
//- Run-Time Error - object destroyed, reference point to some non-allocated memory const volScalarField& foo = myCalc(mesh); Info<< foo << endl; Code:
const volScalarField& foo = myCalc(mesh)(); |
|
November 8, 2017, 17:01 |
|
#36 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
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()
__________________
Keep foaming, Tobias Holzmann |
|
November 8, 2017, 17:09 |
|
#37 |
Senior Member
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 21 |
Hopefully I could test it tomorrow and then get back to this thread.
|
|
November 9, 2017, 16:35 |
|
#38 |
Senior Member
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 21 |
I played around with the code and have to confirm that
Code:
const volScalarField& nut = turbulence->nut()(); |
|
November 9, 2017, 18:47 |
|
#39 |
Super Moderator
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51 |
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.
__________________
Keep foaming, Tobias Holzmann |
|
November 11, 2017, 08:45 |
|
#40 | |
Senior Member
Przemek
Join Date: Jun 2011
Posts: 249
Rep Power: 15 |
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(); alphaEff = turbulence->nu() + tnut();
__________________
best regards pblasiak |
||
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Calculating divDevReff | jposunz | OpenFOAM Running, Solving & CFD | 82 | June 12, 2022 14:01 |
Add new nuEff() law to simpleFoam | T.D. | OpenFOAM Running, Solving & CFD | 2 | July 16, 2015 07:27 |
Calculate and display eddy viscosity | ceyrows | OpenFOAM Running, Solving & CFD | 4 | February 16, 2009 12:57 |