|
[Sponsors] |
Register object inside functionObject without static pointer |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
February 10, 2021, 08:41 |
Register object inside functionObject without static pointer
|
#1 |
Member
Tom Lauriks
Join Date: Apr 2020
Posts: 34
Rep Power: 6 |
I'm trying to store an object to the register inside a function object. A closely related post which provided me with a working solution is: https://www.cfd-online.com/Forums/openfoam-programming-development/99207-create-registered-object-runtime-using-functionobject.html
Following the comments in this thread, I wrote: Code:
if (!obr_.foundObject<volScalarField>("k"))//Check if k is registered variable { static autoPtr<volScalarField> kField;//If not, initialize pointer to what will be registered as k if(!kField.valid())//Check if the pointer is set { kField.set//set sets pointer to what is given ( new volScalarField ( IOobject ( "k", obr_.time().timeName(), obr_, IOobject::NO_READ, IOobject::NO_WRITE ), model.k() ) ); Info << "function object TKEsgs: pField was not set" << nl << endl; } volScalarField& k = kField();//Create reference to the pointer k.checkIn();//Add k to the register Info << "function object TKEsgs: using checkIn" << nl << endl; if (obr_.foundObject<volScalarField>("k")) { Info << "function object TKEsgs: k successfully stored" << nl << endl; } } The code above can either be inserted in the constructor of my function object (the body of the function) or e.g. in the execute function and it will work. However, when I try to insert similar code used in fieldAverage at said locations, the object is not stored to the register, or I cannot find it afterwards. Said code is: Code:
if (!obr_.foundObject<volScalarField>("k")) { obr_.store ( new volScalarField ( IOobject ( "k", obr_.time().timeName(), obr_, IOobject::NO_READ, IOobject::NO_WRITE ), model.k() ) ); Info << type() << " " << name() << " storing k to register" << nl << endl; if (obr_.foundObject<volScalarField>("k")) { Info << type() << " " << name() << " k successfully stored" << nl << endl; } } In fieldAverage, similar code stores the mean fields to the register, which evidently works. However, I cannot spot what is different to what I'm trying with the code above. The code of fieldAverage is present in the fieldAverageTemplates.C file, and is ultimately called from the initialize function, it seems to me. With the code being: Code:
if (obr_.foundObject<Type>(meanFieldName)) {} else { const Type& baseField = obr_.lookupObject<Type>(fieldName); // Store on registry obr_.store ( new Type ( IOobject ( meanFieldName, obr_.time().timeName(obr_.time().startTime().value()), obr_, restartOnOutput_ ? IOobject::NO_READ : IOobject::READ_IF_PRESENT, IOobject::NO_WRITE ), 1*baseField ) ); } } Can anyone explain me why this is working in fieldAverage and not in my function object? |
|
April 13, 2023, 04:49 |
|
#3 | |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40 |
Quote:
In fieldAverage (which is the correct approach), the existing field is retrieved from the registry if it exists. If it does not exist, a new field (notice the raw pointer) is created and passed to the registry for storage. This means that the field (as a pointer) is now contained and managed by the registry. The lifetime of the local pointer then doesn't matter, there is no need to have a 'delete' or worry about memory leaks since the objectRegistry now manages it a bit like a smart pointer. When the registry is cleared, or goes out of scope it will trigger the deletion of that pointer. |
||
April 13, 2023, 05:14 |
|
#4 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40 |
Now added information in https://develop.openfoam.com/Develop...terns/registry
|
|
April 16, 2023, 13:55 |
|
#5 |
Senior Member
|
Does it make sense to replace in https://develop.openfoam.com/Develop...terns/registry
Code:
fieldPtr = new volScalarField ( IOobject ( resultName_, mesh.time().timeName(), mesh.thisDb(), IOobject::NO_READ, IOobject::NO_WRITE // implicit: IOobject::REGISTER ), mesh, dimensionedScalar(dimless, Zero) // implicit: calculatedType() ); Code:
fieldPtr = new volScalarField ( IOobject ( resultName_, mesh().time().timeName(), mesh().thisDb(), IOobject::NO_READ, IOobject::NO_WRITE // implicit: IOobject::REGISTER ), mesh(), dimensionedScalar(dimless, Zero) // implicit: calculatedType() ); Thanks for this wonderful post! |
|
April 18, 2023, 11:24 |
|
#6 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40 |
Context is everything here. You might have a mesh reference available directly as a 'mesh' variable, or as a private/protected 'mesh_' variable, or a 'mesh()' method.
Having 'mesh()' would be even more confusing, when some has mesh' as a variable. |
|
April 18, 2023, 13:59 |
|
#7 |
Senior Member
|
Aha, Thx.
In case that an example illustrating the post is seen to be beneficial, here is one Code:
functions { // here autoPtr is used to avoid that Tsclaled goes out of scope as soon as the function is left; // for an example, see e.g. https://www.cfd-online.com/Forums/openfoam-programming-development/99207-create-registered-object-runtime-using-functionobject.html // how to share this data among functionObjects: using object registry (currently checkin() fails) or using shared pointer // scales the temperature with a constant and stores scaled value in the object registry scalingNut { type coded; libs ("libutilityFunctionObjects.so"); name scalingNut; codeOptions // this seems mandatory: without it, code fails to compile #{ -I$(LIB_SRC)/meshTools/lnInclude \ -I$(LIB_SRC)/OpenFOAM/lnInclude #}; codeExecute #{ #}; codeWrite // "to avoid No critical "code" prefixed keywords found." #{ Info<<" Scaling the kinematic viscosity at time "<<endl; Info<<" t = " << mesh().time().timeName() <<" with scaling factor "<< endl; #}; codeWrite #{ //..Defines scaling constant as a fixed scalar const scalar sigmat = .5; //..Defines const pointer to existing T-field const volScalarField& nut = mesh().lookupObject<volScalarField>("nut"); //..Check whether nutscaled is a registered variable.. if (!mesh().foundObject<volScalarField>("nutscaled")) { //..If not, initialize auto pointer to what will be registered as nutscaled static autoPtr<volScalarField> nutscaledField; //..Check whether the auto pointer is set if(!nutscaledField.valid()) { //..If not, set auto pointer nutscaledField.set ( new volScalarField ( IOobject ( "nutscaled", mesh().time().timeName(), mesh().thisDb(), IOobject::NO_READ, IOobject::AUTO_WRITE ), mesh(), dimensionedScalar("nut",nut.dimensions(), 0.0) ) ); Info<<" [fct-obj-scalingnut]:: nutscaledField was auto-ptr not set" << endl; } volScalarField& nutscaled = nutscaledField();//..Create reference to the pointer forAll(nut,cellI){ nutscaled[cellI] = nut[cellI]/sigmat; } Info<<" [fct-obj-scalingnut]:: checkIn of nutscaled" << endl; nutscaled.checkIn(); //..Add nutscaled to the registry } if (mesh().foundObject<volScalarField>("nutscaled")) { Info<<" [fct-obj-scalingnut]:: nutscaled successfully stored in object registry" << endl; } #}; } lookupScalednut { type coded; libs ("libutilityFunctionObjects.so"); name lookupScalednut; codeOptions // this seems mandatory: without it, code fails to compile #{ -I$(LIB_SRC)/meshTools/lnInclude \ -I$(LIB_SRC)/OpenFOAM/lnInclude #}; codeExecute #{ #}; codeWrite // "to avoid No critical "code" prefixed keywords found." #{ const volScalarField& nut = mesh().lookupObject<volScalarField>("nut"); const volScalarField& nutscaled = mesh().lookupObject<volScalarField>("nutscaled"); Info<<" Looking up the scaled temperature at time "<<endl; Info<<" t = " << mesh().time().timeName() << endl; Info<<" with magnitude = " << gSumMag(nut) << endl; Info<<" with magnitude = " << gSumMag(nutscaled) << endl; #}; } mixtureFraction { type scalarTransport; libs ("libsolverFunctionObjects.so"); // Name of scalar field to transport, default = 's' field mf; // Name of flux field, default = 'phi' phi phi; // Name of density field for compressible cases, default = 'rho' rho rho; // Set the scalar to zero on start/re-start resetOnStartUp no; // Name of field to use when looking up schemes from fvSchemes // default = <field> schemesField Yi; // Diffusivity // Fixed value diffusivity D 0.001; // Name of field to use as diffusivity, default = 'none' nut nutScaled; // Run-time selectable sources // fvOptions //{ //... //} } } |
|
April 18, 2023, 15:30 |
|
#8 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40 |
Okay, but this seems even more confusing to me, or at least needs some more comments about why you are trying to store a field locally (in the autoPtr), and register it with the objectRegistry. The only advantage I can see (versus just storing it on the objectRegistry directly) is if you expect your functionObject to be removed during the run time if the user edits the controlDict and you don't want the nut field to hang around any longer than needed. Otherwise, it is quite a few more lines of code.
|
|
April 18, 2023, 15:32 |
|
#9 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40 |
Since you are modifying the internal field directly for whatever reason, you will also need to have correctBoundaryConditions() - otherwise the calculated patch values will be inconsistent and retain their initial 0.0 value.
|
|
April 19, 2023, 04:26 |
|
#10 |
Senior Member
|
Dear Mark,
Many thanks. The scope is to perform scalar transport with a scaled variant of nut. What your seeing are first attempts. These attempts are blurred by partial understanding on by behalf. I will look into the matter and get here. Cheers, Domenico. |
|
April 19, 2023, 06:12 |
|
#11 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40 |
Not an issue, but gSumMag() is redundant on GeometricField (eg, volScalarField). Can just write sumMag() if you want.
|
|
April 19, 2023, 12:54 |
|
#12 | |
Senior Member
|
Quote:
Thx, D. |
||
Tags |
function object, functionobject, object, register, registry |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
GGI: rotating object completely within a static object | Tobb | OpenFOAM | 2 | October 15, 2010 07:29 |
register a new object sprayRASModel | yejungong | OpenFOAM Running, Solving & CFD | 0 | May 22, 2009 05:04 |
Compilation error OF1.5-dev on Suse10.3 | darenyang | OpenFOAM Installation | 0 | April 29, 2009 05:55 |
[blockMesh] BlockMeshmergePatchPairs | hjasak | OpenFOAM Meshing & Mesh Conversion | 11 | August 15, 2008 08:36 |
OpenFOAM141dev linking error on IBM AIX 52 | matthias | OpenFOAM Installation | 24 | April 28, 2008 16:49 |