DimensionedFieldIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "DimensionedField.H"
27 #include "IOstreams.H"
28 
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class Type, class GeoMesh>
34 (
35  const dictionary& fieldDict,
36  const word& fieldDictEntry
37 )
38 {
39  dimensions_.reset(dimensionSet(fieldDict.lookup("dimensions")));
40 
41  Field<Type> f(fieldDictEntry, dimensions_, fieldDict, GeoMesh::size(mesh_));
42 
43  this->transfer(f);
44 }
45 
46 
47 template<class Type, class GeoMesh>
49 (
50  const word& fieldDictEntry
51 )
52 {
53  if
54  (
55  this->readOpt() == IOobject::MUST_READ
56  || this->readOpt() == IOobject::MUST_READ_IF_MODIFIED
57  )
58  {
60  << "read option IOobject::MUST_READ or MUST_READ_IF_MODIFIED"
61  << " suggests that a read constructor for field " << this->name()
62  << " would be more appropriate." << endl;
63  }
64  if
65  (
66  this->readOpt() == IOobject::READ_IF_PRESENT
67  && this->headerOk()
68  )
69  {
70  readField(dictionary(readStream(typeName)), fieldDictEntry);
71 
72  readOldTimeIfPresent();
73 
74  return true;
75  }
76 
77  return false;
78 }
79 
80 
81 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
82 
83 template<class Type, class GeoMesh>
85 (
86  const IOobject& io,
87  const Mesh& mesh,
88  const word& fieldDictEntry
89 )
90 :
91  regIOobject(io),
92  Field<Type>(0),
93  OldTimeField<DimensionedField>(this->time().timeIndex()),
94  mesh_(mesh),
95  dimensions_(dimless)
96 {
97  readField(dictionary(readStream(typeName)), fieldDictEntry);
98 }
99 
100 
101 template<class Type, class GeoMesh>
103 (
104  const IOobject& io,
105  const Mesh& mesh,
106  const dictionary& fieldDict,
107  const word& fieldDictEntry
108 )
109 :
110  regIOobject(io),
111  Field<Type>(0),
112  OldTimeField<DimensionedField>(this->time().timeIndex()),
113  mesh_(mesh),
114  dimensions_(dimless)
115 {
116  readField(fieldDict, fieldDictEntry);
117 }
118 
119 
120 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
121 
122 template<class Type, class GeoMesh>
124 (
125  Ostream& os,
126  const word& fieldDictEntry
127 ) const
128 {
129  writeEntry(os, "dimensions", dimensions());
130  os << nl;
131 
132  writeEntry(os, fieldDictEntry, static_cast<const Field<Type>&>(*this));
133 
134  // Check state of Ostream
135  os.check
136  (
137  "bool DimensionedField<Type, GeoMesh>::writeData"
138  "(Ostream& os, const word& fieldDictEntry) const"
139  );
140 
141  return (os.good());
142 }
143 
144 
145 template<class Type, class GeoMesh>
147 {
148  return writeData(os, "value");
149 }
150 
151 
152 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
153 
154 template<class Type, class GeoMesh>
155 Foam::Ostream& Foam::operator<<
156 (
157  Ostream& os,
159 )
160 {
161  df.writeData(os);
162 
163  return os;
164 }
165 
166 
167 template<class Type, class GeoMesh>
168 Foam::Ostream& Foam::operator<<
169 (
170  Ostream& os,
172 )
173 {
174  tdf().writeData(os);
175  tdf.clear();
176 
177  return os;
178 }
179 
180 
181 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
void readField(const dictionary &fieldDict, const word &fieldDictEntry="value")
GeoMesh::Mesh Mesh
Type of mesh on which this DimensionedField is instantiated.
bool writeData(Ostream &, const word &fieldDictEntry) const
DimensionedField(const IOobject &, const Mesh &mesh, const dimensionSet &, const Field< Type > &)
Construct from components.
Pre-declare SubField and related Field type.
Definition: Field.H:83
static const char *const typeName
Definition: Field.H:106
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
Class to add into field types to provide old-time storage and retrieval.
Definition: OldTimeField.H:93
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:710
Dimension set for the base types.
Definition: dimensionSet.H:125
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
Istream & readStream(const word &, const bool read=true)
Return Istream and check object type against that given.
A class for managing temporary objects.
Definition: tmp.H:55
A class for handling words, derived from string.
Definition: word.H:62
#define WarningInFunction
Report a warning using Foam::Warning.
const HashTable< dimensionSet > & dimensions()
Get the table of dimension sets.
Definition: dimensionSets.C:96
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
const dimensionSet dimless
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
static const char nl
Definition: Ostream.H:266
label timeIndex
Definition: getTimeIndex.H:4
labelList f(nPoints)
const bool writeData(readBool(pdfDictionary.lookup("writeData")))