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 (isReadRequired() || (isReadOptional() && headerOk()))
38  {
39  // Do reading
40  Istream& is = readStream(word::null, readOnProc);
41 
42  if (readOnProc)
43  {
44  if (headerClassName() == IOField<T>::typeName)
45  {
46  is >> static_cast<Field<T>&>(*this);
47  close();
48  }
49  else if (headerClassName() == typeName)
50  {
51  is >> *this;
52  close();
53  }
54  else
55  {
57  << "Unexpected class name " << headerClassName()
58  << " expected " << typeName
59  << " or " << IOField<T>::typeName << nl
60  << " while reading object " << name()
61  << exit(FatalIOError);
62  }
63  }
64 
65  return true;
66  }
67 
68  return false;
69 }
70 
71 
72 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
73 
74 template<class T, class BaseType>
76 :
78 {
79  readIOcontents();
80 }
81 
82 
83 template<class T, class BaseType>
85 (
86  const IOobject& io,
87  const bool readOnProc
88 )
89 :
91 {
92  readIOcontents(readOnProc);
93 }
94 
95 
96 template<class T, class BaseType>
98 (
99  const IOobject& io,
100  Foam::zero
101 )
102 :
103  regIOobject(io)
104 {
105  readIOcontents();
106 }
107 
108 
109 template<class T, class BaseType>
111 (
112  const IOobject& io,
113  const label len
114 )
115 :
116  regIOobject(io)
117 {
118  if (!readIOcontents())
119  {
121  }
122 }
123 
124 
125 template<class T, class BaseType>
127 (
128  const IOobject& io,
129  const UList<T>& content
130 )
131 :
132  regIOobject(io)
133 {
134  if (!readIOcontents())
135  {
137  }
138 }
139 
140 
141 template<class T, class BaseType>
143 (
144  const IOobject& io,
145  Field<T>&& content
146 )
147 :
148  regIOobject(io)
149 {
150  Field<T>::transfer(content);
151 
152  readIOcontents();
153 }
154 
155 
156 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
157 
158 template<class T, class BaseType>
160 (
161  IOstreamOption streamOpt,
162  const bool writeOnProc
163 ) const
164 {
165  if (streamOpt.format() == IOstreamOption::ASCII)
166  {
167  // Change type to be non-compact format type
168  const word oldTypeName(typeName);
169 
170  const_cast<word&>(typeName) = IOField<T>::typeName;
171 
172  bool good = regIOobject::writeObject(streamOpt, writeOnProc);
173 
174  // Restore type
175  const_cast<word&>(typeName) = oldTypeName;
176 
177  return good;
178  }
179 
180  return regIOobject::writeObject(streamOpt, writeOnProc);
181 }
182 
183 
184 template<class T, class BaseType>
186 {
187  return (os << *this).good();
188 }
189 
190 
191 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
192 
193 template<class T, class BaseType>
195 (
196  const CompactIOField<T, BaseType>& rhs
197 )
198 {
199  if (this == &rhs)
200  {
201  return; // Self-assigment is a no-op
202  }
203 
204  Field<T>::operator=(rhs);
205 }
206 
207 
208 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
209 
210 template<class T, class BaseType>
211 Foam::Istream& Foam::operator>>
212 (
213  Foam::Istream& is,
215 )
216 {
217  // Read compact
218  const labelList start(is);
219  const Field<BaseType> elems(is);
220 
221  // Convert
222  L.setSize(start.size()-1);
223 
224  forAll(L, i)
225  {
226  T& subField = L[i];
227 
228  label index = start[i];
229  subField.setSize(start[i+1] - index);
230 
231  forAll(subField, j)
232  {
233  subField[j] = elems[index++];
234  }
235  }
236 
237  return is;
238 }
239 
240 
241 template<class T, class BaseType>
242 Foam::Ostream& Foam::operator<<
243 (
244  Foam::Ostream& os,
246 )
247 {
248  // Keep ASCII writing same
250  {
251  os << static_cast<const Field<T>&>(L);
252  }
253  else
254  {
255  // Convert to compact format
256  labelList start(L.size()+1);
257 
258  start[0] = 0;
259  for (label i = 1; i < start.size(); i++)
260  {
261  start[i] = start[i-1]+L[i-1].size();
262  }
263 
264  Field<BaseType> elems(start[start.size()-1]);
265 
266  label elemI = 0;
267  forAll(L, i)
268  {
269  const T& subField = L[i];
270 
271  forAll(subField, j)
272  {
273  elems[elemI++] = subField[j];
274  }
275  }
276  os << start << elems;
277  }
278 
279  return os;
280 }
281 
282 
283 // ************************************************************************* //
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 ...