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

Introducing the IOReferencer

Register Blogs Community New Posts Updated Threads Search

Like Tree5Likes
  • 3 Post By marupio
  • 1 Post By winden
  • 1 Post By andyru

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 12, 2011, 14:54
Default Introducing the IOReferencer
  #1
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 21
marupio is on a distinguished road
Hi Foamers,

I've thrown together a simple piece of code that allows you to look up non-IOobjects from the objectRegistry.

It's fairly straightforward:

  • The installation involves copying two files (no compiling);
  • The example of how to use it is in the header description.

Download it from:

http://openfoam-extend.svn.sourcefor...ject/?view=tar

The wiki page is currently down, but once it's up again, I'll upload the more detailed instructions to:

http://openfoamwiki.net/index.php/Contrib_IOReferencer
marupio is offline   Reply With Quote

Old   August 12, 2011, 15:27
Default Introducing the IOReferencer
  #2
New Member
 
Brent Craven
Join Date: Mar 2009
Location: University Park, PA, USA
Posts: 21
Rep Power: 17
brent_craven is on a distinguished road
Very nice! Thanks for the contribution.
brent_craven is offline   Reply With Quote

Old   August 13, 2011, 11:38
Default
  #3
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 21
marupio is on a distinguished road
Wiki instructions uploaded.
marupio is offline   Reply With Quote

Old   December 7, 2012, 10:33
Default
  #4
Member
 
Björn Windén
Join Date: Feb 2012
Location: National Maritime Research Institute, Tokyo, Japan
Posts: 37
Rep Power: 14
winden is on a distinguished road
Hi.

Thanks for a great contribution. I'm having some trouble and I was wondering if it is the IOReferencer or if I have done something else that is stupid

I would like to keep a pointer to a class (which class gets decided in the solver based on a dictionary) in the registry to access its member functions in other parts of the code.

In the solver I have:

Code:
autoPtr<myClass> pPtr = myClass::New(dict);


IOReferencer< autoPtr<myClass> > myObject
    (
        IOobject
            (
        "pControl",
        runTime.constant(),
        mesh,
        IOobject::NO_READ,
                IOobject::NO_WRITE
        ),
    pPtr
    );
And I access and use it elsewhere as:
Code:
pC = db.lookupObject<IOReferencer<autoPtr<myClass> > >("pControl")();

pForce_ = (pC->myMemberFunction(*this,deltaT));
I'm getting some weird behaviour from this. Sometimes the lookup works fine and the pC->myMemberFunction works fine but when the solver reaches a timestep where the write command is executed, it crashes. On the other hand, if I recompile the same code and run it again I sometimes instead get the following error in the first timestep:
Code:
--> FOAM FATAL ERROR: 

    lookup of pControl from objectRegistry region0 successful
    but it is not a regIOobject, it is a regIOobject

    From function objectRegistry::lookupObject<Type>(const word&) const
    in file /opt/openfoam171/src/OpenFOAM/lnInclude/objectRegistryTemplates.C at line 120.

FOAM aborting
At first I didn't think it would work at all to send an autoPtr like this but I was surprised to see that it actually worked only to eventually be disappointed that it doesn't seem to be a stable way of doing it.

//Björn
atulkjoy likes this.
winden is offline   Reply With Quote

Old   December 7, 2012, 10:47
Default
  #5
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 21
marupio is on a distinguished road
I've encountered this before... it's ugly. That last error you get occurs during an object registry lookup when the type name is correct, but the dynamic_cast still fails. This is one of those compiler issues. If I recall, the cast fails because the object is not the correct size. I can only guess that this occurs when "myClass" is complex. Is it templated, by chance?

The IOReferencer was mostly designed for primitives. If you have a complex custom class, it may be easier to just make it an IOobject, if you can.

However, you may find that it still fails the object lookup even without IOReferencer... in which case I'd suggest trying to change the class structure.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   December 7, 2012, 11:08
Default
  #6
Member
 
Björn Windén
Join Date: Feb 2012
Location: National Maritime Research Institute, Tokyo, Japan
Posts: 37
Rep Power: 14
winden is on a distinguished road
Hi. Thanks for the quick reply. myClass is indeed complex. It is a base class for selecting different propeller modelling techniques. (I modified it from the way constraints are selected for sixdofrigidbodymotion and I can't say I fully understand what all of it does.)

Hopefully, I can find another solution. There is always the possibility to change the structure so that I have direct access to the class where I wanted to use it. However that would mean having to create a lot of custom solutions for many of the base classes that lie above the class where I want to access it so I was hoping to avoid that. Anyway, thanks for letting me know what the problem was.

//Björn
winden is offline   Reply With Quote

Old   September 3, 2014, 12:54
Default why not using scalarIOField?
  #7
Member
 
Andreas Ruopp
Join Date: Aug 2009
Location: Stuttgart / Germany
Posts: 31
Rep Power: 16
andyru is on a distinguished road
Hi,

maybe too late but why not using for example in main solver:
Code:
scalarIOField test
(
        IOobject
        (
            "test",
        runTime.constant(),
        mesh,
        IOobject::NO_READ,
        IOobject::NO_WRITE
        ),
        <sizeOfWishedScalarList<
);
test = <valuesToBePutIntotest>;
then in boundary condition, for instance:
Code:
scalarIOField test =
        const_cast<scalarIOField&>
        (
        db().lookupObject<scalarIOField>("test")
        );
Works also for vectorIOField.

If you want to get only one scalar, then you have to use scalarIOField with size one, since there is no scalarIO.H...

I hope this helps!

Andy
stingph likes this.
andyru is offline   Reply With Quote

Old   July 28, 2016, 10:07
Default
  #8
New Member
 
Join Date: Jul 2016
Posts: 3
Rep Power: 9
suunto is on a distinguished road
Hello,

I am trying to use the IOReferencer to pass a vector from a custom fvOptions to a custom dynamicFvMesh class. Is this possible? When executing a solver, I get the following error:

Code:
--> FOAM FATAL ERROR: 

    request for regIOobject testVector from objectRegistry region0 failed
    available objects of type regIOobject are
0()

    From function objectRegistry::lookupObject<Type>(const word&) const
    in file /home/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/objectRegistryTemplates.C at line 198.

FOAM aborting
In my fvOptions source I added:
Code:
    vector testVector = vector(1, 2, 3);

    IOReferencer<vector> testVectorRef 
    (
        IOobject
        (
            "testVector",
            mesh_.time().timeName(), //can be anything
            mesh_,                            //can be anything
            IOobject::NO_READ,       //must be NO_READ
            IOobject::NO_WRITE       //must be NO_WRITE
        ),
        testVector
    );
and then in the dynamicFvMesh class:
Code:
const fvMesh& mesh = time().db().parent().lookupObject<fvMesh>("region0");
const vector& Tout = mesh.lookupObject<IOReferencer<vector> >
(
    "testVector"
)();
Is this a scope issue or am I using the code incorrectly? Thanks for any hints.
suunto is offline   Reply With Quote

Old   July 28, 2016, 10:46
Default
  #9
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 21
marupio is on a distinguished road
It looks okay. Probably a scope issue. Does the testVector thing go out of scope? If it does, you can make it a member variable <-- probably the best option. Otherwise, try adding testVector.store(); just before the end of the function... but then I'm not sure how it will behave if you do this on the second iteration.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   July 28, 2016, 10:46
Default
  #10
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 21
marupio is on a distinguished road
Also note that IOReferencer was incorporated by the community into foam-extend, and has since been improved there.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   July 28, 2016, 11:32
Default
  #11
New Member
 
Join Date: Jul 2016
Posts: 3
Rep Power: 9
suunto is on a distinguished road
Hi David,

The compiler doesn't complain about going out of scope. I tried adding testVector.store(); at the end of the fvOptions source but store() does not appear to be a member function of a vector:
Code:
error: ‘Foam::vector’ has no member named ‘store’
I will also look into the foam-extend implementation. Thanks for your help!
suunto is offline   Reply With Quote

Old   July 28, 2016, 15:54
Default
  #12
New Member
 
Join Date: Jul 2016
Posts: 3
Rep Power: 9
suunto is on a distinguished road
I've looked into using the foam-extend version of the IOReferencer and I get the following error upon compilation:

Code:
test.C:100:5: error: no matching function for call to ‘Foam::IOReferencer<Foam::Vector<double> >::IOReferencer(Foam::IOobject, Foam::vector&)’
test.C:100:5: note: candidates are:
/home/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOReferencer.C:53:1: note: Foam::IOReferencer<Type>::IOReferencer(const Foam::IOobject&, Type*) [with Type = Foam::Vector<double>, Foam::IOobject = Foam::IOobject]
/home/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOReferencer.C:53:1: note:   no known conversion for argument 2 from ‘Foam::vector {aka Foam::Vector<double>}’ to ‘Foam::Vector<double>*’
/home/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOReferencer.C:31:1: note: Foam::IOReferencer<Type>::IOReferencer(const Foam::IOobject&) [with Type = Foam::Vector<double>, Foam::IOobject = Foam::IOobject]
/home/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOReferencer.C:31:1: note:   candidate expects 1 argument, 2 provided
/home/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOReferencer.H:85:7: note: Foam::IOReferencer<Foam::Vector<double> >::IOReferencer(const Foam::IOReferencer<Foam::Vector<double> >&)
/home/OpenFOAM/OpenFOAM-2.4.x/src/OpenFOAM/lnInclude/IOReferencer.H:85:7: note:   candidate expects 1 argument, 2 provided
Sorry for a dumb question but is this related to the difference between foam-extend and OpenFOAM or do I simply need to change the syntax of the IOReferencer object? I am not really sure which 2 arguments are the offenders in this case.
suunto is offline   Reply With Quote

Old   March 9, 2022, 05:34
Default
  #13
Member
 
UOCFD
Join Date: Oct 2020
Posts: 40
Rep Power: 5
uosilos is on a distinguished road
Is this tool available for OpenFOAM8??
uosilos is offline   Reply With Quote

Old   May 18, 2022, 15:26
Default
  #14
Member
 
Al
Join Date: May 2019
Posts: 37
Rep Power: 6
aliyah. is on a distinguished road
Quote:
Originally Posted by andyru View Post
Hi,

maybe too late but why not using for example in main solver:
Code:
scalarIOField test
(
        IOobject
        (
            "test",
        runTime.constant(),
        mesh,
        IOobject::NO_READ,
        IOobject::NO_WRITE
        ),
        <sizeOfWishedScalarList<
);
test = <valuesToBePutIntotest>;
then in boundary condition, for instance:
Code:
scalarIOField test =
        const_cast<scalarIOField&>
        (
        db().lookupObject<scalarIOField>("test")
        );
Works also for vectorIOField.

If you want to get only one scalar, then you have to use scalarIOField with size one, since there is no scalarIO.H...

I hope this helps!

Andy
Hello,

I am using the method you suggested,

first in the createFields I will write

Code:
scalarIOField phiNF
(
 IOobject
 (
  "test",
  runTime.constant(),
  mesh,
  IOobject::NO_READ,
  IOobject::NO_WRITE
),
 1
  );
phiNF = phiP_*liquidToTotal_*domainVolume/liquidVolume;

and then in the library that I want use phiNF I will write
Code:
template<class Thermo, int PolySize>
inline Foam::scalar Foam::myPolynomialTransport<Thermo, PolySize>::mu
(
    const fvMesh& mesh, const scalar p,
    const scalar T
) const
{

scalarIOField phiNF_ = 
  const_cast<scalarIOField&>(db().lookupObject<scalarIOField>("phiNF"));
the first error I get says I need to declare db; for declaration, I did some things like using

Code:
 const_cast<scalarIOField&>(T.db().lookupObject<scalarIOField>("phiNF"));
or

Code:
const objectRegistry& db = Foam::IOobject::db();
But none of them is working, as following for the reason

Code:
 request for member ‘db’ in ‘T’, which is of non-class type ‘const scalar’ {aka ‘const double’}
any suggestion?

Thanks

Last edited by aliyah.; May 19, 2022 at 03:23. Reason: I am explaining my question with more details now
aliyah. is offline   Reply With Quote

Old   May 19, 2022, 03:40
Default
  #15
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Since you registered it on the 'mesh' db, this would be the place you would expect to find it.
It is beyond me why a primitive like bool/int/scalar etc would be expected to have a db() method.
olesen is offline   Reply With Quote

Old   May 19, 2022, 08:04
Default
  #16
Member
 
Al
Join Date: May 2019
Posts: 37
Rep Power: 6
aliyah. is on a distinguished road
Quote:
Originally Posted by olesen View Post
Since you registered it on the 'mesh' db, this would be the place you would expect to find it.
It is beyond me why a primitive like bool/int/scalar etc would be expected to have a db() method.
Hello, and thanks for the suggestion and your time,

I tried mesh and got the following error,

Code:
 In member function ‘Foam::scalar Foam::CorcioneCorcionePolynomialTransport<Thermo, PolySize>::mu(const Foam::fvMesh&, Foam::scalar, Foam::scalar) const’: 

 lnInclude/APolynomialTransportI.H:138:35: error: request for member ‘db’ is ambiguous   
 138 |   const_cast<scalarIOField&>(mesh.db().lookupObject<scalarIOField>("phiNF"));
    

candidates are: ‘const Foam::objectRegistry& Foam::IOobject::db() const’  
  361 |         const objectRegistry& db() const;
Another question: I'm not familiar with the idea of using db(). I think that refers to a database, but for which parameters can we expect to find a db() member on it?


Thanks


Edited, and added

I tried again and just used

Code:
scalarIOField phiNF_ =  const_cast<scalarIOField&>(mesh.lookupObject<scalarIOField>("phiNF"));
and it is working now. I am using this phiNF_ in

Code:
 return muBF(mesh,p,T)/pow(phiNF_.value()  , 1.03));
and i should use it as a scalar. normally I just use .value() in such situation but here it says that

Code:
error: ‘Foam::scalarIOField’ {aka ‘class Foam::IOField<double>’} has no member named ‘value’
I defined scalarIOField as a scalarfield with the size of one in the createFields of my solver as

Code:
scalarIOField phiNF
(
 IOobject
 (
  "phiNF",
  runTime.constant(),
  mesh,
  IOobject::NO_READ,
  IOobject::NO_WRITE
),
 1//here I specified the size of field, Is it correct? 
  );
phiNF = phiP_*liquidToTotal_*domainVolume/liquidVolume;
Any suggestion on how should I turn this scalarIOField to jst scalar in my equation?

Thanks

Last edited by aliyah.; May 19, 2022 at 09:25. Reason: I am explaining my question with more details now
aliyah. is offline   Reply With Quote

Old   May 19, 2022, 09:52
Default
  #17
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Often mesh.thisDb() but mesh.objectRegistry::thisDb() should do it without ambiguity. BTW: generally less messy to use lookupObjectRef() and avoid the const_cast
olesen is offline   Reply With Quote

Old   May 19, 2022, 11:16
Default
  #18
Member
 
Al
Join Date: May 2019
Posts: 37
Rep Power: 6
aliyah. is on a distinguished road
Quote:
Originally Posted by olesen View Post
Often mesh.thisDb() but mesh.objectRegistry::thisDb() should do it without ambiguity. BTW: generally less messy to use lookupObjectRef() and avoid the const_cast
Thanks, It is working now,

I am using this scalarIOFIeld phiNF_ in

Code:
 return muBF(mesh,p,T)/pow(phiNF_.value()  , 1.03));
and i should use it as a scalar. normally I just use .value() in such situation but here it says that

Code:
error: ‘Foam::scalarIOField’ {aka ‘class Foam::IOField<double>’} has no member named ‘value’
I defined scalarIOField as a scalarfield with the size of one in the createFields of my solver as

Code:
scalarIOField phiNF
(
 IOobject
 (
  "phiNF",
  runTime.constant(),
  mesh,
  IOobject::NO_READ,
  IOobject::NO_WRITE
),
 1//here I specified the size of field, Is it correct? 
  );
phiNF = phiP_*liquidToTotal_*domainVolume/liquidVolume;
as can be seen .value() is not working. Any suggestion on how should I convert this scalarIOField to just scalar in this piece of code?

Thanks
aliyah. 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
Discreate Particle Method: Introducing solids kris FLUENT 3 August 18, 2010 02:39
introducing angle of attack on ICEMCFD HEXA icem beginner Main CFD Forum 3 December 17, 2008 05:05
introducing angle of attack on ICEMCFD HEXA icem beginner FLUENT 2 December 6, 2008 15:34
Introducing Tracer jan FLUENT 0 December 2, 2005 09:17
Introducing revolutionary product MBAEdge- smarter surya_mba Siemens 0 February 16, 2005 04:20


All times are GMT -4. The time now is 00:48.