Contrib/IOReferencer

From OpenFOAMWiki
< Contrib
Revision as of 19:20, 11 April 2013 by Marupio (Talk | contribs)

The repo is currently down. See my user page for details and access to the code.

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 it from here.
  2. Untar it using: tar xvfz openfoam-extend-IOReferencerObject.tar.gz, or browse the archive;
  3. Copy the directory IOReferencer to src/OpenFOAM/db/IOobjects
  4. 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)