Introducing the IOReferencer
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:
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 |
Introducing the IOReferencer
Very nice! Thanks for the contribution.
|
Wiki instructions uploaded.
|
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); Code:
pC = db.lookupObject<IOReferencer<autoPtr<myClass> > >("pControl")(); Code:
--> FOAM FATAL ERROR: //Björn |
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. |
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 |
why not using scalarIOField?
Hi,
maybe too late but why not using for example in main solver: Code:
scalarIOField test Code:
scalarIOField test = 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 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: Code:
vector testVector = vector(1, 2, 3); Code:
const fvMesh& mesh = time().db().parent().lookupObject<fvMesh>("region0"); |
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.
|
Also note that IOReferencer was incorporated by the community into foam-extend, and has since been improved there.
|
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'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&)’ |
Is this tool available for OpenFOAM8??
|
Quote:
I am using the method you suggested, first in the createFields I will write Code:
and then in the library that I want use phiNF I will write Code:
template<class Thermo, int PolySize> Code:
const_cast<scalarIOField&>(T.db().lookupObject<scalarIOField>("phiNF")); Code:
const objectRegistry& db = Foam::IOobject::db(); Code:
request for member ‘db’ in ‘T’, which is of non-class type ‘const scalar’ {aka ‘const double’} Thanks |
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. |
Quote:
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’: Thanks Edited, and added I tried again and just used Code:
scalarIOField phiNF_ = const_cast<scalarIOField&>(mesh.lookupObject<scalarIOField>("phiNF")); Code:
return muBF(mesh,p,T)/pow(phiNF_.value() , 1.03)); Code:
error: ‘Foam::scalarIOField’ {aka ‘class Foam::IOField<double>’} has no member named ‘value’ Code:
scalarIOField phiNF Thanks |
Often mesh.thisDb() but mesh.objectRegistry::thisDb() should do it without ambiguity. BTW: generally less messy to use lookupObjectRef() and avoid the const_cast
|
Quote:
I am using this scalarIOFIeld phiNF_ in Code:
return muBF(mesh,p,T)/pow(phiNF_.value() , 1.03)); Code:
error: ‘Foam::scalarIOField’ {aka ‘class Foam::IOField<double>’} has no member named ‘value’ Code:
scalarIOField phiNF Thanks |
All times are GMT -4. The time now is 19:18. |