DimensionedFieldIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "DimensionedField.H"
30 #include "IOstreams.H"
31 #include "localIOdictionary.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 template<class Type, class GeoMesh>
37 (
38  const dictionary& fieldDict,
39  const word& fieldDictEntry
40 )
41 {
42  dimensions_.readEntry("dimensions", fieldDict);
43 
44  // Note: oriented state may have already been set on construction
45  // - if so - do not reset by re-reading
46  // - required for backwards compatibility in case of restarting from
47  // an old run where the oriented state may not have been set
48  if (oriented_.oriented() != orientedType::ORIENTED)
49  {
50  oriented_.read(fieldDict); // The "oriented" entry (if present)
51  }
52 
53 
54  // The primitive field
55  auto& fld = static_cast<Field<Type>&>(*this);
56 
57  fld.resize_nocopy(GeoMesh::size(mesh_));
58  fld.assign(fieldDictEntry, fieldDict, fld.size()); // <- MUST_READ
59 }
60 
61 
62 template<class Type, class GeoMesh>
64 (
65  const word& fieldDictEntry
66 )
67 {
69  (
70  localIOdictionary::readContents
71  (
72  IOobject
73  (
74  this->name(),
75  this->instance(),
76  this->local(),
77  this->db(),
78  IOobjectOption::MUST_READ,
79  IOobjectOption::NO_WRITE,
80  IOobjectOption::NO_REGISTER
81  ),
82  typeName
83  )
84  );
85 
86  this->close();
87 
88  readField(dict, fieldDictEntry);
89 }
90 
91 
92 template<class Type, class GeoMesh>
94 (
95  const word& fieldDictEntry
96 )
97 {
98  if
99  (
100  this->isReadRequired()
101  || (this->isReadOptional() && this->headerOk())
102  )
103  {
104  readField(fieldDictEntry);
105  return true;
106  }
107 
108  return false;
109 }
110 
111 
112 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
113 
114 template<class Type, class GeoMesh>
116 (
117  const IOobject& io,
118  const Mesh& mesh,
119  const word& fieldDictEntry
120 )
121 :
122  regIOobject(io),
123  mesh_(mesh)
124 {
125  readField(fieldDictEntry);
126 }
127 
128 
129 template<class Type, class GeoMesh>
131 (
132  const IOobject& io,
133  const Mesh& mesh,
134  const dictionary& fieldDict,
135  const word& fieldDictEntry
136 )
137 :
138  regIOobject(io),
139  mesh_(mesh)
140 {
141  readField(fieldDict, fieldDictEntry);
142 }
143 
144 
145 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
146 
147 template<class Type, class GeoMesh>
149 (
150  Ostream& os,
151  const word& fieldDictEntry
152 ) const
153 {
154  os.writeEntry("dimensions", dimensions());
155  os << nl;
156 
157  if (oriented_.writeEntry(os))
158  {
159  os << nl;
160  }
161 
162  Field<Type>::writeEntry(fieldDictEntry, os);
163 
164  os.check(FUNCTION_NAME);
165  return os.good();
166 }
167 
168 
169 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
170 
171 template<class Type, class GeoMesh>
172 Foam::Ostream& Foam::operator<<
173 (
174  Ostream& os,
175  const DimensionedField<Type, GeoMesh>& fld
176 )
177 {
178  fld.writeData(os);
179 
180  return os;
181 }
182 
183 
184 template<class Type, class GeoMesh>
185 Foam::Ostream& Foam::operator<<
186 (
187  Ostream& os,
188  const tmp<DimensionedField<Type, GeoMesh>>& tfld
189 )
190 {
191  tfld().writeData(os);
192  tfld.clear();
193 
194  return os;
195 }
196 
197 
198 // ************************************************************************* //
dictionary dict
virtual bool read()
Read object.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
DimensionedField(const IOobject &io, const Mesh &mesh, const dimensionSet &dims, const Field< Type > &field)
Construct from components, copy initial field content.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Generic templated field type.
Definition: Field.H:63
bool writeData(Ostream &os, const word &fieldDictEntry) const
Write dimensions, oriented flag (if valid) and the field data as a dictionary entry with the specifie...
A class for handling words, derived from Foam::string.
Definition: word.H:63
bool local
Definition: EEqn.H:20
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180