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, template<class> class PrimitiveField>
34 (
35  const dictionary& fieldDict,
36  const word& fieldDictEntry
37 )
38 {
39  dimensions_.reset(dimensionSet(fieldDict.lookup("dimensions")));
40 
41  PrimitiveField<Type> f
42  (
43  fieldDictEntry,
44  dimensions_,
45  fieldDict,
46  GeoMesh::size(mesh_)
47  );
48 
49  this->transfer(f);
50 }
51 
52 
53 template<class Type, class GeoMesh, template<class> class PrimitiveField>
55 (
56  const word& fieldDictEntry
57 )
58 {
59  if
60  (
61  this->readOpt() == IOobject::MUST_READ
62  || this->readOpt() == IOobject::MUST_READ_IF_MODIFIED
63  )
64  {
66  << "read option IOobject::MUST_READ or MUST_READ_IF_MODIFIED"
67  << " suggests that a read constructor for field " << this->name()
68  << " would be more appropriate." << endl;
69  }
70  if
71  (
72  this->readOpt() == IOobject::READ_IF_PRESENT
73  && this->headerOk()
74  )
75  {
76  readField(dictionary(readStream(typeName)), fieldDictEntry);
77 
78  readOldTimeIfPresent();
79 
80  return true;
81  }
82 
83  return false;
84 }
85 
86 
87 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
88 
89 template<class Type, class GeoMesh, template<class> class PrimitiveField>
91 (
92  const IOobject& io,
93  const Mesh& mesh,
94  const word& fieldDictEntry
95 )
96 :
97  regIOobject(io),
98  PrimitiveField<Type>(0),
99  OldTimeField<DimensionedField>(this->time().timeIndex()),
100  mesh_(mesh),
101  dimensions_(dimless)
102 {
103  readField(dictionary(readStream(typeName)), fieldDictEntry);
104 }
105 
106 
107 template<class Type, class GeoMesh, template<class> class PrimitiveField>
109 (
110  const IOobject& io,
111  const Mesh& mesh,
112  const dictionary& fieldDict,
113  const word& fieldDictEntry
114 )
115 :
116  regIOobject(io),
117  PrimitiveField<Type>(0),
118  OldTimeField<DimensionedField>(this->time().timeIndex()),
119  mesh_(mesh),
120  dimensions_(dimless)
121 {
122  readField(fieldDict, fieldDictEntry);
123 }
124 
125 
126 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127 
128 template<class Type, class GeoMesh, template<class> class PrimitiveField>
130 (
131  Ostream& os,
132  const word& fieldDictEntry
133 ) const
134 {
135  writeEntry(os, "dimensions", dimensions());
136  os << nl;
137 
138  writeEntry
139  (
140  os,
141  fieldDictEntry,
142  static_cast<const PrimitiveField<Type>&>(*this)
143  );
144 
145  // Check state of Ostream
146  os.check
147  (
148  "bool DimensionedField<Type, GeoMesh, PrimitiveField>::writeData"
149  "(Ostream& os, const word& fieldDictEntry) const"
150  );
151 
152  return (os.good());
153 }
154 
155 
156 template<class Type, class GeoMesh, template<class> class PrimitiveField>
158 (
159  Ostream& os
160 ) const
161 {
162  return writeData(os, "value");
163 }
164 
165 
166 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
167 
168 template<class Type, class GeoMesh, template<class> class PrimitiveField>
169 Foam::Ostream& Foam::operator<<
170 (
171  Ostream& os,
173 )
174 {
175  df.writeData(os);
176 
177  return os;
178 }
179 
180 
181 template<class Type, class GeoMesh, template<class> class PrimitiveField>
182 Foam::Ostream& Foam::operator<<
183 (
184  Ostream& os,
186 )
187 {
188  tdf().writeData(os);
189  tdf.clear();
190 
191  return os;
192 }
193 
194 
195 // ************************************************************************* //
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")
friend class DimensionedField
Declare friendship with other dimensioned fields.
GeoMesh::Mesh Mesh
Type of mesh on which this DimensionedField is instantiated.
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:92
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:113
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
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:740
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
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#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:258
const dimensionSet dimless
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
static const char nl
Definition: Ostream.H:267
label timeIndex
Definition: getTimeIndex.H:4
labelList f(nPoints)
const bool writeData(readBool(pdfDictionary.lookup("writeData")))