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  dimensions_.reset(dimensionSet(dict.lookup("dimensions")));
40 
41  if (dict.isDict(keyword))
42  {
44  <
46  >::New
47  (
48  dict.subDict(keyword),
49  *this
50  )->evaluate();
51  }
52  else
53  {
54  PrimitiveField<Type> f
55  (
56  keyword,
57  dimensions_,
58  dict,
59  mesh_.size()
60  );
61 
62  this->transfer(f);
63  }
64 }
65 
66 
67 template<class Type, class GeoMesh, template<class> class PrimitiveField>
69 (
70  const word& fieldDictEntry
71 )
72 {
73  if
74  (
75  this->readOpt() == IOobject::MUST_READ
76  || this->readOpt() == IOobject::MUST_READ_IF_MODIFIED
77  )
78  {
80  << "read option IOobject::MUST_READ or MUST_READ_IF_MODIFIED"
81  << " suggests that a read constructor for field " << this->name()
82  << " would be more appropriate." << endl;
83  }
84  if
85  (
86  this->readOpt() == IOobject::READ_IF_PRESENT
87  && this->headerOk()
88  )
89  {
90  readField(dictionary(readStream(typeName)), fieldDictEntry);
91 
92  readOldTimeIfPresent();
93 
94  return true;
95  }
96 
97  return false;
98 }
99 
100 
101 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
102 
103 template<class Type, class GeoMesh, template<class> class PrimitiveField>
105 (
106  const IOobject& io,
107  const GeoMesh& mesh,
108  const word& fieldDictEntry
109 )
110 :
111  regIOobject(io),
112  PrimitiveField<Type>(0),
114  mesh_(mesh),
115  dimensions_(dimless)
116 {
117  readField(dictionary(readStream(typeName)), fieldDictEntry);
118 }
119 
120 
121 template<class Type, class GeoMesh, template<class> class PrimitiveField>
123 (
124  const IOobject& io,
125  const GeoMesh& mesh,
126  const dictionary& fieldDict,
127  const word& fieldDictEntry
128 )
129 :
130  regIOobject(io),
131  PrimitiveField<Type>(0),
133  mesh_(mesh),
134  dimensions_(dimless)
135 {
136  readField(fieldDict, fieldDictEntry);
137 }
138 
139 
140 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
141 
142 template<class Type, class GeoMesh, template<class> class PrimitiveField>
144 (
145  Ostream& os,
146  const word& fieldDictEntry
147 ) const
148 {
149  writeEntry(os, "dimensions", dimensions());
150  os << nl;
151 
152  writeEntry
153  (
154  os,
155  fieldDictEntry,
156  static_cast<const PrimitiveField<Type>&>(*this)
157  );
158 
159  // Check state of Ostream
160  os.check
161  (
162  "bool DimensionedField<Type, GeoMesh, PrimitiveField>::writeData"
163  "(Ostream& os, const word& fieldDictEntry) const"
164  );
165 
166  return (os.good());
167 }
168 
169 
170 template<class Type, class GeoMesh, template<class> class PrimitiveField>
172 (
173  Ostream& os
174 ) const
175 {
176  return writeData(os, "value");
177 }
178 
179 
180 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
181 
182 template<class Type, class GeoMesh, template<class> class PrimitiveField>
183 Foam::Ostream& Foam::operator<<
184 (
185  Ostream& os,
187 )
188 {
189  df.writeData(os);
190 
191  return os;
192 }
193 
194 
195 template<class Type, class GeoMesh, template<class> class PrimitiveField>
196 Foam::Ostream& Foam::operator<<
197 (
198  Ostream& os,
200 )
201 {
202  tdf().writeData(os);
203  tdf.clear();
204 
205  return os;
206 }
207 
208 
209 // ************************************************************************* //
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.
A class for managing temporary objects.
Definition: tmp.H:55
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 WarningInFunction
Report a warning using Foam::Warning.
const dimensionSet dimless
const dimensionSet time
const dimensionSet & dimless
Definition: dimensions.C:138
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
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")))