Difference between revisions of "OpenFOAM guide/Input and Output operations using dictionaries and the IOobject class"
m (Wyldckat moved page Input/Output operations using dictionaries and the IOobject class to OpenFOAM guide/Input and Output operations using dictionaries and the IOobject class: Since it's associated to one of OpenFOAM Guide's sub-category, I've m...) |
Pete Bachant (Talk | contribs) (→IOobject constructors: Minor formatting changes) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
''IOobject defines the attributes of an object for which implicit [[snip_objectRegistry|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.'' | ''IOobject defines the attributes of an object for which implicit [[snip_objectRegistry|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.'' | ||
− | == IOobject constructors == | + | == <code>IOobject</code> constructors == |
− | The constructor of an IOobject may have two forms: | + | The constructor of an <code>IOobject</code> may have two forms: |
+ | |||
+ | 1. Construct from name, instance, registry and IO options: | ||
− | |||
<cpp> | <cpp> | ||
IOobject | IOobject | ||
Line 20: | Line 21: | ||
</cpp> | </cpp> | ||
− | + | 2. Construct from name, instance, local, registry and IO options: | |
<cpp> | <cpp> | ||
Line 35: | Line 36: | ||
</cpp> | </cpp> | ||
− | While reading the two previous code snippets, remember that word inherits string, from which fileName is also derived. Moreover, both Time and polyMesh inherit objectRegistry. As a consequence fvMesh, inheriting polyMesh, indirectly inherits objectRegistry too. For further information, see the [http://foam.sourceforge.net/docs/cpp/a00926.html IOobject class reference]. | + | While reading the two previous code snippets, remember that <code>word</code> inherits <code>string</code>, from which <code>fileName</code> is also derived. Moreover, both <code>Time</code> and <code>polyMesh</code> inherit <code>objectRegistry</code>. As a consequence <code>fvMesh</code>, inheriting <code>polyMesh</code>, indirectly inherits <code>objectRegistry</code> too. For further information, see the [http://foam.sourceforge.net/docs/cpp/a00926.html IOobject class reference]. |
== Read options == | == Read options == | ||
Read options, which define what is done on object construction and explicit reads, are: | Read options, which define what is done on object construction and explicit reads, are: | ||
− | * MUST_READ | + | |
− | + | * <code>MUST_READ</code> – The object must be read from <code>Istream</code> on construction. An error message is produced if Istream does not exist or can't be read. | |
− | * READ_IF_PRESENT | + | * <code>READ_IF_PRESENT</code> – It reads the object from <code>Istream</code> if <code>Istream</code> exists, otherwise doesn't. An error message is produced only if <code>Istream</code> exists but can't be read. |
− | + | * <code>NO_READ</code> – Don't read the object. | |
− | * NO_READ | + | |
− | + | ||
== Write options == | == Write options == | ||
Write options, which define what is done on object destruction and explicit writes, are: | Write options, which define what is done on object destruction and explicit writes, are: | ||
− | * AUTO_WRITE | + | * <code>AUTO_WRITE</code> – The object is written automatically when requested to by the <code>objectRegistry</code>. |
− | + | * <code>NO_WRITE</code> – The object is not written automatically on destruction, but it can be written explicitly. | |
− | * NO_WRITE | + | |
− | + | ||
− | == IOobject and dictionaries == | + | == <code>IOobject</code> 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. | + | Dictionaries can be read using the <code>IOobject</code> class when they are declared. Usually the <code>readOption</code> for a dictionary is <code>MUST_READ</code>, while the <code>writeOption</code> is <code>NO_WRITE</code> not to overwrite the settings contained in the dictionary. |
− | For example, the code required to read the usual transportProperties dictionary is: | + | For example, the code required to read the usual <code>transportProperties</code> dictionary is: |
<cpp> | <cpp> | ||
Line 73: | Line 70: | ||
The first constructor form is adopted in this case, where: | The first constructor form is adopted in this case, where: | ||
− | * | + | * <code>transportProperties</code> is 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. | + | * <code>runTime.constant()</code>, the instance, gives the position of the dictionary, which is, in this case, contained in the constant directory of the considered case. |
− | * The objectRegistry is represented by the mesh. | + | * The <code>objectRegistry</code> is represented by the mesh. |
+ | |||
+ | == <code>IOobject</code> and fields == | ||
+ | Similarly to dictionaries, read and write options for a field can be set using the <code>IOobject</code> 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 <code>volScalarField</code> called <code>T</code>, saving it at user-defined intervals of time in a file called <code>T</code>, the code is: | ||
− | |||
− | |||
− | |||
<cpp> | <cpp> | ||
volScalarField T | volScalarField T | ||
Line 94: | Line 92: | ||
); | ); | ||
</cpp> | </cpp> | ||
+ | |||
where: | where: | ||
− | * | + | * <code>T</code> is the name of the file. |
− | * runTime.timeName() tells OpenFOAM to save the file in a directory called as the current run time. | + | * <code>runTime.timeName()</code> tells OpenFOAM to save the file in a directory called as the current run time. |
− | * mesh is the objectRegistry. | + | * <code>mesh</code> is the <code>objectRegistry</code>. |
− | * Read and write options are set to MUST_READ and AUTO_WRITE in order to make OpenFOAM read the field and to automatically save it. If reading the field is not required, the MUST_READ option has to be changed into NO_READ. | + | * Read and write options are set to <code>MUST_READ</code> and <code>AUTO_WRITE</code> in order to make OpenFOAM read the field and to automatically save it. If reading the field is not required, the <code>MUST_READ</code> option has to be changed into <code>NO_READ</code>. |
[[Category:OpenFOAM coding guide]] | [[Category:OpenFOAM coding guide]] |
Latest revision as of 16:47, 16 August 2014
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.
Contents
1 IOobject
constructors
The constructor of an IOobject
may have two forms:
1. 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 )
2. 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 word
inherits string
, from which fileName
is also 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.
2 Read options
Read options, which define what is done on object construction and explicit reads, are:
-
MUST_READ
– The object must be read fromIstream
on construction. An error message is produced if Istream does not exist or can't be read. -
READ_IF_PRESENT
– It reads the object fromIstream
ifIstream
exists, otherwise doesn't. An error message is produced only ifIstream
exists but can't be read. -
NO_READ
– Don't read the object.
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 theobjectRegistry
. -
NO_WRITE
– The object is not written automatically on destruction, but it can be written explicitly.
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, the code required to read the usual transportProperties
dictionary is:
IOdictionary transportProperties ( IOobject ( "transportProperties", runTime.constant(), mesh, IOobject::MUST_READ, IOobject::NO_WRITE ) );
The first constructor form is adopted in this case, where:
-
transportProperties
is 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. - The
objectRegistry
is represented by the mesh.
5 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 volScalarField
called T
, saving it at user-defined intervals of time in a file called T
, the code is:
volScalarField T ( IOobject ( "T", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh );
where:
-
T
is the name of the file. -
runTime.timeName()
tells OpenFOAM to save the file in a directory called as the current run time. -
mesh
is theobjectRegistry
. - Read and write options are set to
MUST_READ
andAUTO_WRITE
in order to make OpenFOAM read the field and to automatically save it. If reading the field is not required, theMUST_READ
option has to be changed intoNO_READ
.