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-2022 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  }
106 }
107 
108 
109 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
110 
111 template<class Type, class GeoMesh>
113 (
114  const IOobject& io,
115  const Mesh& mesh,
116  const word& fieldDictEntry
117 )
118 :
119  regIOobject(io),
120  Field<Type>(),
121  mesh_(mesh),
122  dimensions_(dimless),
123  oriented_()
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  Field<Type>(),
140  mesh_(mesh),
141  dimensions_(dimless),
142  oriented_()
143 {
144  readField(fieldDict, fieldDictEntry);
145 }
146 
147 
148 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
149 
150 template<class Type, class GeoMesh>
152 (
153  Ostream& os,
154  const word& fieldDictEntry
155 ) const
156 {
157  os.writeEntry("dimensions", dimensions());
158  os << nl;
159 
160  if (oriented_.writeEntry(os))
161  {
162  os << nl;
163  }
164 
165  Field<Type>::writeEntry(fieldDictEntry, os);
166 
167  os.check(FUNCTION_NAME);
168  return os.good();
169 }
170 
171 
172 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
173 
174 template<class Type, class GeoMesh>
175 Foam::Ostream& Foam::operator<<
176 (
177  Ostream& os,
178  const DimensionedField<Type, GeoMesh>& fld
179 )
180 {
181  fld.writeData(os);
182 
183  return os;
184 }
185 
186 
187 template<class Type, class GeoMesh>
188 Foam::Ostream& Foam::operator<<
189 (
190  Ostream& os,
191  const tmp<DimensionedField<Type, GeoMesh>>& tfld
192 )
193 {
194  tfld().writeData(os);
195  tfld.clear();
196 
197  return os;
198 }
199 
200 
201 // ************************************************************************* //
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
const dimensionSet dimless
Dimensionless.
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:62
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