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-2026 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"
28 #include "IOstreams.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class Type, class GeoMesh, template<class> class PrimitiveField>
34 (
35  const dictionary& dict,
36  const word& keyword
37 )
38 {
39  const dimensionSet dims(dict.lookup("dimensions"));
40 
41  // Set the dimensions if invalid (not already set)
42  // otherwise check for consistency
43  if (dimensions_ == dimensions::invalid)
44  {
45  dimensions_.reset(dims);
46  }
47  else
48  {
49  if (dims != dimensions_)
50  {
52  << "Inconsistent dimensions specified for " << this->name()
53  << " " << dims
54  << ", required dimensions are " << this->dimensions()
55  << exit(FatalIOError);
56  }
57  }
58 
59  if (dict.isDict(keyword))
60  {
62  <
64  >::New
65  (
66  dict.subDict(keyword),
67  *this
68  )->evaluate();
69  }
70  else
71  {
72  PrimitiveField<Type> f
73  (
74  keyword,
75  dimensions_,
76  dict,
77  mesh_.size()
78  );
79 
80  this->transfer(f);
81  }
82 }
83 
84 
85 template<class Type, class GeoMesh, template<class> class PrimitiveField>
87 (
88  const word& fieldDictEntry
89 )
90 {
91  if
92  (
93  this->readOpt() == IOobject::MUST_READ
94  || this->readOpt() == IOobject::MUST_READ_IF_MODIFIED
95  )
96  {
98  << "read option IOobject::MUST_READ or MUST_READ_IF_MODIFIED"
99  << " suggests that a read constructor for field " << this->name()
100  << " would be more appropriate." << endl;
101  }
102  if
103  (
104  this->readOpt() == IOobject::READ_IF_PRESENT
105  && this->headerOk()
106  )
107  {
108  readField(dictionary(readStream(typeName)), fieldDictEntry);
109 
110  readOldTimeIfPresent();
111 
112  return true;
113  }
114 
115  return false;
116 }
117 
118 
119 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
120 
121 
122 template<class Type, class GeoMesh, template<class> class PrimitiveField>
124 (
125  const IOobject& io,
126  const GeoMesh& mesh,
127  const dictionary& fieldDict,
128  const dimensionSet& dims,
129  const word& fieldDictEntry
130 )
131 :
132  regIOobject(io),
133  PrimitiveField<Type>(0),
135  mesh_(mesh),
136  dimensions_(dims)
137 {
138  readField(fieldDict, fieldDictEntry);
139 }
140 
141 
142 template<class Type, class GeoMesh, template<class> class PrimitiveField>
144 (
145  const IOobject& io,
146  const GeoMesh& mesh,
147  const dimensionSet& dims,
148  const word& fieldDictEntry
149 )
150 :
151  regIOobject(io),
152  PrimitiveField<Type>(0),
154  mesh_(mesh),
155  dimensions_(dims)
156 {
157  readField(dictionary(readStream(typeName)), fieldDictEntry);
158 }
159 
160 
161 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
162 
163 template<class Type, class GeoMesh, template<class> class PrimitiveField>
165 (
166  Ostream& os,
167  const word& fieldDictEntry
168 ) const
169 {
170  writeEntry(os, "dimensions", dimensions());
171  os << nl;
172 
173  writeEntry
174  (
175  os,
176  fieldDictEntry,
177  static_cast<const PrimitiveField<Type>&>(*this)
178  );
179 
180  // Check state of Ostream
181  os.check
182  (
183  "bool DimensionedField<Type, GeoMesh, PrimitiveField>::writeData"
184  "(Ostream& os, const word& fieldDictEntry) const"
185  );
186 
187  return (os.good());
188 }
189 
190 
191 template<class Type, class GeoMesh, template<class> class PrimitiveField>
193 (
194  Ostream& os
195 ) const
196 {
197  return writeData(os, "value");
198 }
199 
200 
201 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
202 
203 template<class Type, class GeoMesh, template<class> class PrimitiveField>
204 Foam::Ostream& Foam::operator<<
205 (
206  Ostream& os,
208 )
209 {
210  df.writeData(os);
211 
212  return os;
213 }
214 
215 
216 template<class Type, class GeoMesh, template<class> class PrimitiveField>
217 Foam::Ostream& Foam::operator<<
218 (
219  Ostream& os,
220  const tmp<DimensionedField<Type, GeoMesh, PrimitiveField>>& tdf
221 )
222 {
223  tdf().writeData(os);
224  tdf.clear();
225 
226  return os;
227 }
228 
229 
230 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Base class for run-time selectable internal and patch field initialisation evaluation and update with...
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")
friend class DimensionedField
Declare friendship with other dimensioned fields.
bool writeData(Ostream &, const word &fieldDictEntry) const
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:108
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Class to add into field types to provide old-time storage and retrieval.
Definition: OldTimeField.H:115
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
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.
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define WarningInFunction
Report a warning using Foam::Warning.
const dimensionSet time
const dimensionSet invalid
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)
static const char nl
Definition: Ostream.H:297
label timeIndex
Definition: getTimeIndex.H:4
labelList f(nPoints)
dictionary dict
const bool writeData(readBool(pdfDictionary.lookup("writeData")))