OpenFOAM guide/Input and Output operations using dictionaries and the IOobject class

From OpenFOAMWiki
< OpenFOAM guide
Revision as of 01:13, 13 August 2005 by Alberto (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

1 Input/Output operations using dictionaries and the IOobject class

Many input/output operations are performed in OpenFOAM using the IOobject class, which is described in its header file as follows:

IOobject defines the attributes of an object for which implicit objectRegistry management is supported, and provides the infrastructure for performing stream I/O. An IOobject is constructed with an object name, a class name, an instance path, a reference to a objectRegistry, and parameters determining its storage status.

1.1 IOobject constructors

The constructor of an IOobject may have two forms:

  • Construct from name, instance, registry and IO options
 
IOobject  	
(  
    const word &   	 	name,
    const word &  		instance,
    const objectRegistry &  	registry,
    readOption  		r = NO_READ,
    writeOption  		w = NO_WRITE,
    bool  			registerObject = true
)
  • Construct from name, instance, local, registry and IO options
 
IOobject  	
(
    const word &   	 	name,
    const word &  		instance,
    const fileName &  		local,
    const objectRegistry &  	registry,
    readOption  		r = NO_READ,
    writeOption  		w = NO_WRITE,
    bool  			registerObject = true
)

While reading the two previous code snippets, remember that string inherits word, from which fileName is derived. Moreover, both Time and polyMesh inherit objectRegistry. As a consequence fvMesh, inheriting polyMesh, indirectly inherits objectRegistry too. For further information, see the IOobject class reference.

1.2 Read options

Read options, which define what is done on object construction and explicit reads, are:

  • MUST_READ
 The object must be read from Istream on construction. An error message is produced if Istream does not exist or can't be read.
  • READ_IF_PRESENT
 It reads the object from Istream if Istream exists, otherwise doesn't. An error message is produced only if Istream exists but can't be read.
  • NO_READ
 Don't read the object.

1.3 Write options

Write options, which define what is done on object destruction and explicit writes, are:

  • AUTO_WRITE
 The object is written automatically when requested to by the objectRegistry.
  • NO_WRITE
 The object is not written automatically on destruction, but it can be written explicitly.

1.4 IOobject and dictionaries

Dictionaries can be read using the IOobject class when they are declared. Usually the readOption for a dictionary is MUST_READ, while the writeOption is NO_WRITE not to overwrite the settings contained in the dictionary. For example, for the usual transportProperties dictionary, the syntax is:

 
IOdictionary transportProperties
(
     IOobject
     (
          "transportProperties",
          runTime.constant(),
          mesh,
          IOobject::MUST_READ,
          IOobject::NO_WRITE
     )
);

where runTime.constant() gives the position of the dictionary, which is, in this case, contained in the constant directory of the considered case.

1.5 IOobect and fields

Input/output options for a field can be set similarly to