CompactIOField.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-2017 OpenFOAM Foundation
9 Copyright (C) 2018-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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 "CompactIOField.H"
30#include "labelList.H"
31
32// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33
34template<class T, class BaseType>
36{
37 Istream& is = readStream(word::null, valid);
38
39 if (valid)
40 {
41 if (headerClassName() == IOField<T>::typeName)
42 {
43 is >> static_cast<Field<T>&>(*this);
44 close();
45 }
46 else if (headerClassName() == typeName)
47 {
48 is >> *this;
49 close();
50 }
51 else
52 {
54 << "Unexpected class name " << headerClassName()
55 << " expected " << typeName
56 << " or " << IOField<T>::typeName << nl
57 << " while reading object " << name()
58 << exit(FatalIOError);
59 }
60 }
61}
62
63
64// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
65
66template<class T, class BaseType>
68:
70{
71 if
72 (
75 )
76 {
77 readFromStream();
78 }
79}
80
81
82template<class T, class BaseType>
84(
85 const IOobject& io,
86 const bool valid
87)
88:
90{
92 {
93 readFromStream(valid);
94 }
96 {
97 bool haveFile = headerOk();
98 readFromStream(valid && haveFile);
99 }
100}
101
103template<class T, class BaseType>
106 const IOobject& io,
109:
111{
112 if
113 (
115 || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
116 )
117 {
118 readFromStream();
119 }
120}
121
122
123template<class T, class BaseType>
125(
126 const IOobject& io,
127 const label len
128)
129:
131{
132 if
133 (
135 || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
136 )
137 {
138 readFromStream();
139 }
140 else
141 {
142 Field<T>::resize(len);
143 }
144}
145
146
147template<class T, class BaseType>
149(
150 const IOobject& io,
151 const UList<T>& content
152)
153:
155{
156 if
157 (
160 )
161 {
162 readFromStream();
163 }
164 else
165 {
166 Field<T>::operator=(content);
167 }
168}
169
170
171template<class T, class BaseType>
173(
174 const IOobject& io,
175 Field<T>&& content
176)
177:
179{
180 Field<T>::transfer(content);
181
182 if
183 (
186 )
187 {
188 readFromStream();
189 }
190}
191
192
193// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
194
195template<class T, class BaseType>
197(
198 IOstreamOption streamOpt,
199 const bool valid
200) const
201{
202 if (streamOpt.format() == IOstream::ASCII)
203 {
204 // Change type to be non-compact format type
205 const word oldTypeName(typeName);
206
207 const_cast<word&>(typeName) = IOField<T>::typeName;
208
209 bool good = regIOobject::writeObject(streamOpt, valid);
210
211 // Restore type
212 const_cast<word&>(typeName) = oldTypeName;
213
214 return good;
215 }
216
217 return regIOobject::writeObject(streamOpt, valid);
218}
219
220
221template<class T, class BaseType>
223{
224 return (os << *this).good();
225}
226
227
228// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
229
230template<class T, class BaseType>
232(
234)
235{
236 if (this == &rhs)
237 {
238 return; // Self-assigment is a no-op
239 }
240
242}
243
244
245// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
246
247template<class T, class BaseType>
248Foam::Istream& Foam::operator>>
249(
250 Foam::Istream& is,
252)
253{
254 // Read compact
255 const labelList start(is);
256 const Field<BaseType> elems(is);
257
258 // Convert
259 L.setSize(start.size()-1);
260
261 forAll(L, i)
262 {
263 T& subField = L[i];
264
265 label index = start[i];
266 subField.setSize(start[i+1] - index);
267
268 forAll(subField, j)
269 {
270 subField[j] = elems[index++];
271 }
272 }
273
274 return is;
275}
276
277
278template<class T, class BaseType>
279Foam::Ostream& Foam::operator<<
280(
283)
284{
285 // Keep ascii writing same.
286 if (os.format() == IOstream::ASCII)
287 {
288 os << static_cast<const Field<T>&>(L);
289 }
290 else
291 {
292 // Convert to compact format
293 labelList start(L.size()+1);
294
295 start[0] = 0;
296 for (label i = 1; i < start.size(); i++)
297 {
298 start[i] = start[i-1]+L[i-1].size();
299 }
300
301 Field<BaseType> elems(start[start.size()-1]);
302
303 label elemI = 0;
304 forAll(L, i)
305 {
306 const T& subField = L[i];
307
308 forAll(subField, j)
309 {
310 elems[elemI++] = subField[j];
311 }
312 }
313 os << start << elems;
314 }
315
316 return os;
317}
318
319
320// ************************************************************************* //
A Field of objects of type <T> with automated input and output using a compact storage....
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
Generic templated field type.
Definition: Field.H:82
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:641
A primitive field of type <T> with automated input and output.
Definition: IOField.H:58
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
bool good() const noexcept
Did last readHeader() succeed?
Definition: IOobjectI.H:222
The IOstreamOption is a simple container for options an IOstream can normally have.
streamFormat format() const noexcept
Get the current stream format.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
static constexpr direction size() noexcept
The number of elements in the VectorSpace = Ncmpts.
Definition: VectorSpace.H:176
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:76
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:438
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
A class for handling words, derived from Foam::string.
Definition: word.H:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
List< label > labelList
A List of labels.
Definition: List.H:66
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
const vector L(dict.get< vector >("L"))