Input and 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.
The constructor of an
IOobject may have two forms:
1. Construct from name, instance, registry and IO options:
<cpp> IOobject (
const word & name, const word & instance, const objectRegistry & registry, readOption r = NO_READ, writeOption w = NO_WRITE, bool registerObject = true
2. Construct from name, instance, local, registry and IO options:
<cpp> 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, from which
fileName is also derived. Moreover, both
objectRegistry. As a consequence
polyMesh, indirectly inherits
objectRegistry too. For further information, see the IOobject class reference.
Read options, which define what is done on object construction and explicit reads, are:
MUST_READ– The object must be read from
Istreamon construction. An error message is produced if Istream does not exist or can't be read.
READ_IF_PRESENT– It reads the object from
Istreamexists, otherwise doesn't. An error message is produced only if
Istreamexists but can't be read.
NO_READ– Don't read the object.
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
NO_WRITE– The object is not written automatically on destruction, but it can be written explicitly.
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
NO_WRITE not to overwrite the settings contained in the dictionary.
For example, the code required to read the usual
transportProperties dictionary is:
<cpp> IOdictionary transportProperties (
IOobject ( "transportProperties", runTime.constant(), mesh, IOobject::MUST_READ, IOobject::NO_WRITE )
); </cpp> The first constructor form is adopted in this case, where:
transportPropertiesis the name of the file containing the dictionary.
runTime.constant(), the instance, gives the position of the dictionary, which is, in this case, contained in the constant directory of the considered case.
objectRegistryis represented by the mesh.
IOobject and fields
Similarly to dictionaries, read and write options for a field can be set using the
IOobject class. The syntax is almost the same for all kind of field and it is clarified by the following example.
If we would like to define a
T, saving it at user-defined intervals of time in a file called
T, the code is:
<cpp> volScalarField T (
IOobject ( "T", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh
Tis the name of the file.
runTime.timeName()tells OpenFOAM to save the file in a directory called as the current run time.
- Read and write options are set to
AUTO_WRITEin order to make OpenFOAM read the field and to automatically save it. If reading the field is not required, the
MUST_READoption has to be changed into