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-2024 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 "CompactIOField.H"
30 #include "labelList.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T, class BaseType>
36 {
37  if (readOpt() == IOobject::MUST_READ)
38  {
39  // Reading
40  }
41  else if (isReadOptional())
42  {
43  if (!headerOk())
44  {
45  readOnProc = false;
46  }
47  }
48  else
49  {
50  return false;
51  }
52 
53 
54  // Do reading
55  Istream& is = readStream(word::null, readOnProc);
56 
57  if (readOnProc)
58  {
59  if (headerClassName() == IOField<T>::typeName)
60  {
61  is >> static_cast<Field<T>&>(*this);
62  close();
63  }
64  else if (headerClassName() == typeName)
65  {
66  is >> *this;
67  close();
68  }
69  else
70  {
72  << "Unexpected class name " << headerClassName()
73  << " expected " << typeName
74  << " or " << IOField<T>::typeName << nl
75  << " while reading object " << name()
76  << exit(FatalIOError);
77  }
78  }
79 
80  return true;
81 }
82 
83 
84 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
85 
86 template<class T, class BaseType>
88 :
90 {
91  readIOcontents();
92 }
93 
94 
95 template<class T, class BaseType>
97 (
98  const IOobject& io,
99  const bool readOnProc
100 )
101 :
102  regIOobject(io)
103 {
104  readIOcontents(readOnProc);
105 }
106 
107 
108 template<class T, class BaseType>
110 (
111  const IOobject& io,
112  Foam::zero
113 )
114 :
115  regIOobject(io)
116 {
117  readIOcontents();
118 }
119 
120 
121 template<class T, class BaseType>
123 (
124  const IOobject& io,
125  const label len
126 )
127 :
128  regIOobject(io)
129 {
130  if (!readIOcontents())
131  {
133  }
134 }
135 
136 
137 template<class T, class BaseType>
139 (
140  const IOobject& io,
141  const UList<T>& content
142 )
143 :
144  regIOobject(io)
145 {
146  if (!readIOcontents())
147  {
149  }
150 }
151 
152 
153 template<class T, class BaseType>
155 (
156  const IOobject& io,
157  Field<T>&& content
158 )
159 :
160  regIOobject(io)
161 {
162  Field<T>::transfer(content);
163 
164  readIOcontents();
165 }
166 
167 
168 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 
170 template<class T, class BaseType>
172 (
173  IOstreamOption streamOpt,
174  const bool writeOnProc
175 ) const
176 {
177  if (streamOpt.format() == IOstreamOption::ASCII)
178  {
179  // Change type to be non-compact format type
180  const word oldTypeName(typeName);
181 
182  const_cast<word&>(typeName) = IOField<T>::typeName;
183 
184  bool good = regIOobject::writeObject(streamOpt, writeOnProc);
185 
186  // Restore type
187  const_cast<word&>(typeName) = oldTypeName;
188 
189  return good;
190  }
191 
192  return regIOobject::writeObject(streamOpt, writeOnProc);
193 }
194 
195 
196 template<class T, class BaseType>
198 {
199  return (os << *this).good();
200 }
201 
202 
203 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
204 
205 template<class T, class BaseType>
207 (
208  const CompactIOField<T, BaseType>& rhs
209 )
210 {
211  if (this == &rhs)
212  {
213  return; // Self-assigment is a no-op
214  }
215 
216  Field<T>::operator=(rhs);
217 }
218 
219 
220 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
221 
222 template<class T, class BaseType>
223 Foam::Istream& Foam::operator>>
224 (
225  Foam::Istream& is,
227 )
228 {
229  // Read compact
230  const labelList start(is);
231  const Field<BaseType> elems(is);
232 
233  // Convert
234  L.setSize(start.size()-1);
235 
236  forAll(L, i)
237  {
238  T& subField = L[i];
239 
240  label index = start[i];
241  subField.setSize(start[i+1] - index);
242 
243  forAll(subField, j)
244  {
245  subField[j] = elems[index++];
246  }
247  }
248 
249  return is;
250 }
251 
252 
253 template<class T, class BaseType>
254 Foam::Ostream& Foam::operator<<
255 (
256  Foam::Ostream& os,
258 )
259 {
260  // Keep ASCII writing same
262  {
263  os << static_cast<const Field<T>&>(L);
264  }
265  else
266  {
267  // Convert to compact format
268  labelList start(L.size()+1);
269 
270  start[0] = 0;
271  for (label i = 1; i < start.size(); i++)
272  {
273  start[i] = start[i-1]+L[i-1].size();
274  }
275 
276  Field<BaseType> elems(start[start.size()-1]);
277 
278  label elemI = 0;
279  forAll(L, i)
280  {
281  const T& subField = L[i];
282 
283  forAll(subField, j)
284  {
285  elems[elemI++] = subField[j];
286  }
287  }
288  os << start << elems;
289  }
290 
291  return os;
292 }
293 
294 
295 // ************************************************************************* //
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
CompactIOField(const CompactIOField &)=default
Default copy construct.
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:153
void transfer(List< Type > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
const vector L(dict.get< vector >("L"))
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
"ascii" (normal default)
virtual bool writeData(Ostream &os) const
Pure virtual writeData function.
A simple container for options an IOstream can normally have.
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:747
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:637
A Field of objects of type <T> with automated input and output using a compact storage. Behaves like IOField except when binary output in case it writes a CompactListList.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
List< label > labelList
A List of labels.
Definition: List.H:62
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
A primitive field of type <T> with automated input and output.
streamFormat format() const noexcept
Get the current stream format.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...