Contrib/IOReferencer

From OpenFOAMWiki

IOReferencer allows you to retrieve non-IOobjects through the objectRegistry. Inspired by this thread.


Valid versions: OF version 15.png OF Version 15dev.png OF version 16.png OF Version 16ext.png OF Version 170.png OF Version 171.png OF Version 17x.png OF Version 200.png

1 Why do you need it?

OpenFOAM organizes its model-related data into a database, known as the objectRegistry. This database is used to share data between the solver and its components, such as boundary conditions, discretization schemes, and thermophysical models. This is accomplished with the ever-so-useful lookupObject function.

For example, say you are writing a custom viscosity model and you want to use the local temperature. You would write:

    const volScalarField& T = db().lookupObject<volScalarField>("T");
    // The "db()" above may vary

And you now have temperature. The problem is: not all objects are supported by the objectRegistry. Say you want to look up a scaling factor that you defined in your solver, e.g.: scalar lengthScale; but you can't seem to use lookupObject on scalars. That's because only "IOobjects" are supported by the objectRegistry.

IOReferencer solves this problem, allowing any non-IOobject can be looked up through the objectRegistry.

2 How do you use it?

In the code that creates the non-IOobject, before:

    scalar myObject(1.12 /*or some other expression*/);

In the code that creates the non-IOobject, after:

#include "IOReferencer.H"
 
    scalar myObject(1.12 /*or some other expression*/);
 
    // Once you've declared your non-IOobject, make its referencer:
    IOReferencer<scalar> myObjectRef
    (
        IOobject
        (
            "lookupName",
            runTime.constant(), /*can be anything*/
            runTime,            /*can be anything*/
            IOobject::NO_READ,  /*must be NO_READ*/
            IOobject::NO_WRITE  /*must be NO_WRITE*/
        ),
        myObject
    );

In the code that wants to look it up:

    const scalar& myObject = db().lookupObject<IOReferencer<scalar> >
    (
        "lookupName"
    )();
    // Note: the db() bit may be different, depending on which object you are in.

and you have a const reference to your non-IOobject.

A coupled of notes:

  • The referencer (e.g. myObjectRef above) and the object itself (e.g. myObject above) should be declared one after the other; and
  • When the referencer or object itself go out of scope, the entry is removed from the objectRegistry... so make sure they stay in scope when you want to use them.

3 How to install it

  1. Download and unpack or checkout by using one of the following possibilities:
    • Using SVN:
      svn checkout svn://svn.code.sf.net/p/openfoam-extend/svn/trunk/Breeder_1.6/libraries/IOReferencerObject IOReferencerObject
    • Using Git-SVN:
      git svn clone svn://svn.code.sf.net/p/openfoam-extend/svn/trunk/Breeder_1.6/libraries/IOReferencerObject IOReferencerObject
    • Using Git (from an unofficial mirror repository):
      git clone https://github.com/Unofficial-Extend-Project-Mirror/openfoam-extend-Breeder1.6-libraries-IOReferencerObject.git \
        IOReferencerObject
    • Using wget (from an unofficial mirror repository):
      wget -O IOReferencerObject.tar.gz \
        "https://github.com/Unofficial-Extend-Project-Mirror/openfoam-extend-Breeder1.6-libraries-IOReferencerObject/archive/master.tar.gz"

      Then unpack it by running:

      tar xzvf IOReferencerObject.tar.gz \
        --transform='s/openfoam-extend-Breeder1.6-libraries-IOReferencerObject-master/IOReferencerObject/'
  2. Copy the directory IOReferencer to src/OpenFOAM/db/IOobjects
  3. From src/OpenFOAM, execute these commands:
    rm -rf lnInclude
    wmakeLnInclude .

You are done. No compiling or anything is required. Enjoy!

4 Update info

  • 2011-08-11: Initial import

Marupio 17:37, 13 August 2011 (CEST)