FIRECore.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) 2016 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "FIRECore.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 const Foam::Enum
33 <
35 >
37 ({
38  { fileExt3d::POLY_ASCII, "fpma" },
39  { fileExt3d::POLY_BINARY, "fpmb" },
40  { fileExt3d::POLY_ASCII_Z, "fpmaz" },
41  { fileExt3d::POLY_BINARY_Z, "fpmbz" },
42 });
43 
44 
45 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
46 
48 (
49  ISstream& is,
51 )
52 {
53  const label n = getFireLabel(is);
54 
55  if (n > 0)
56  {
57  points.setSize(n);
58 
59  // read the coordinates
60  forAll(points, pointI)
61  {
62  points[pointI] = getFirePoint(is);
63  }
64  }
65  else
66  {
68  << "no points in file " << is.name()
69  << abort(FatalError);
70  }
71 
72  return n;
73 }
74 
75 
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77 
79 (
80  const fileName& base,
81  const enum fileExt3d ext
82 )
83 {
84  return base + '.' + file3dExtensions[ext];
85 }
86 
87 
89 {
90  if (is.format() == IOstream::BINARY)
91  {
92  fireInt_t ivalue;
93 
94  is.stdStream().read
95  (
96  reinterpret_cast<char *>(&ivalue),
97  sizeof(ivalue)
98  );
99 
100  return ivalue;
101  }
102  else
103  {
104  return readLabel(is);
105  }
106 }
107 
108 
110 {
111  point pt;
112 
113  if (is.format() == IOstream::BINARY)
114  {
115  fireReal_t coord[3];
116 
117  is.stdStream().read
118  (
119  reinterpret_cast<char *>(&coord),
120  sizeof(coord)
121  );
122 
123  pt.x() = coord[0];
124  pt.y() = coord[1];
125  pt.z() = coord[2];
126  }
127  else
128  {
129  pt.x() = readScalar(is);
130  pt.y() = readScalar(is);
131  pt.z() = readScalar(is);
132  }
133 
134  return pt;
135 }
136 
137 
139 {
140  std::string str;
141 
142  if (is.format() == IOstream::BINARY)
143  {
144  long len;
145 
146  is.stdStream().read
147  (
148  reinterpret_cast<char *>(&len),
149  sizeof(len)
150  );
151 
152  str.resize(len);
153 
154  for (std::size_t pos = 0; pos < str.size(); ++pos)
155  {
156  is.stdStream().read(&(str[pos]), sizeof(char));
157  }
158  }
159  else
160  {
161  const std::string whitespace(" \t\f\v\n\r");
162 
163  string s;
164 
165  // use a low-level getline, but this means we must handle
166  // blank lines manually
167  while (s.empty())
168  {
169  is.getLine(s);
170  if (!s.empty())
171  {
172  // remove prefix whitespace
173  size_t pos = s.find_first_not_of(whitespace);
174 
175  if (pos != std::string::npos)
176  {
177  s.erase(0, pos);
178 
179  // remove suffix whitespace
180  pos = s.find_last_not_of(whitespace);
181  if (pos != std::string::npos)
182  {
183  s.erase(pos + 1);
184  }
185  }
186 
187  if (pos == std::string::npos)
188  {
189  s.clear();
190  }
191  }
192  }
193 
194  str.swap(s);
195  }
196 
197  return str;
198 }
199 
200 
202 (
203  OSstream& os,
204  const label value
205 )
206 {
207  if (os.format() == IOstream::BINARY)
208  {
209  fireInt_t ivalue(value);
210 
211  os.stdStream().write
212  (
213  reinterpret_cast<char const *>(&ivalue),
214  sizeof(ivalue)
215  );
216  }
217  else
218  {
219  os << value;
220  }
221 }
222 
223 
225 (
226  OSstream& os,
227  const labelUList& lst
228 )
229 {
230  if (os.format() == IOstream::BINARY)
231  {
232  fireInt_t ivalue(lst.size());
233 
234  os.stdStream().write
235  (
236  reinterpret_cast<char const *>(&ivalue),
237  sizeof(ivalue)
238  );
239 
240  forAll(lst, i)
241  {
242  ivalue = lst[i];
243 
244  os.stdStream().write
245  (
246  reinterpret_cast<char const *>(&ivalue),
247  sizeof(ivalue)
248  );
249  }
250  }
251  else
252  {
253  os << ' ' << lst.size();
254  forAll(lst, i)
255  {
256  os << ' ' << lst[i];
257  }
258  os << '\n';
259  }
260 }
261 
262 
264 (
265  OSstream& os,
266  const label count,
267  const label start
268 )
269 {
270  if (os.format() == IOstream::BINARY)
271  {
272  fireInt_t ivalue(count);
273 
274  os.stdStream().write
275  (
276  reinterpret_cast<char const *>(&ivalue),
277  sizeof(ivalue)
278  );
279 
280  ivalue = start;
281  for (label i=0; i < count; ++i, ++ivalue)
282  {
283  os.stdStream().write
284  (
285  reinterpret_cast<char const *>(&ivalue),
286  sizeof(ivalue)
287  );
288  }
289  }
290  else
291  {
292  os << ' ' << count;
293 
294  label ivalue = start;
295  for (label i = 0; i < count; ++i, ++ivalue)
296  {
297  os << ' ' << ivalue;
298  }
299  os << '\n';
300  }
301 }
302 
303 
305 (
306  OSstream& os,
307  const point& value
308 )
309 {
310  if (os.format() == IOstream::BINARY)
311  {
312  fireReal_t fvalue[3];
313  fvalue[0] = value.x();
314  fvalue[1] = value.y();
315  fvalue[2] = value.z();
316 
317  os.stdStream().write
318  (
319  reinterpret_cast<char const *>(&fvalue),
320  sizeof(fvalue)
321  );
322  }
323  else
324  {
325  os << ' '
326  << value.x() << ' '
327  << value.y() << ' '
328  << value.z() << '\n';
329  }
330 }
331 
332 
334 (
335  OSstream& os,
336  const std::string& value
337 )
338 {
339  if (os.format() == IOstream::BINARY)
340  {
341  long len(value.size());
342 
343  os.stdStream().write
344  (
345  reinterpret_cast<char const *>(&len),
346  sizeof(len)
347  );
348 
349  os.stdStream().write(value.data(), len);
350  }
351  else
352  {
353  // output without surrounding quotes
354  os.stdStream() << value << '\n';
355  }
356 }
357 
358 
359 // ************************************************************************* //
Foam::Vector::x
const Cmpt & x() const
Access to the vector x component.
Definition: VectorI.H:73
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::ISstream::getLine
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:76
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::fileFormats::FIRECore::fireFileName
static fileName fireFileName(const fileName &baseName, const enum fileExt3d)
Resolve base file-name for the given file-type.
Definition: FIRECore.C:79
s
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;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::fileFormats::FIRECore::putFirePoint
static void putFirePoint(OSstream &, const point &)
Write a point x/y/z (ascii or binary)
Definition: FIRECore.C:305
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
FIRECore.H
Foam::Vector::z
const Cmpt & z() const
Access to the vector z component.
Definition: VectorI.H:85
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::Field< vector >
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::fileFormats::FIRECore::getFireString
static std::string getFireString(ISstream &)
Extract a string (ascii or binary)
Definition: FIRECore.C:138
Foam::fileFormats::FIRECore::getFirePoint
static point getFirePoint(ISstream &)
Get an point x/y/z (ascii or binary)
Definition: FIRECore.C:109
Foam::ISstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:113
Foam::fileFormats::FIRECore::putFireString
static void putFireString(OSstream &, const std::string &)
Write a string (ascii or binary)
Definition: FIRECore.C:334
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::fileFormats::FIRECore::fireInt_t
int32_t fireInt_t
Integer type (binary format)
Definition: FIRECore.H:96
Foam::ISstream::stdStream
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: ISstream.H:216
Foam::Vector::y
const Cmpt & y() const
Access to the vector y component.
Definition: VectorI.H:79
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::fileFormats::FIRECore::putFireLabels
static void putFireLabels(OSstream &, const labelUList &)
Write multiple integers (ascii or binary)
Definition: FIRECore.C:225
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::Vector< scalar >
Foam::readLabel
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
Foam::fileFormats::FIRECore::fileExt3d
fileExt3d
Enumeration defining the file extensions for 3D types.
Definition: FIRECore.H:86
Foam::UList< label >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::fileFormats::FIRECore::fireReal_t
double fireReal_t
Float type (binary format)
Definition: FIRECore.H:99
Foam::fileFormats::FIRECore::getFireLabel
static label getFireLabel(ISstream &)
Get an integer (ascii or binary)
Definition: FIRECore.C:88
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::fileFormats::FIRECore::file3dExtensions
static const Enum< fileExt3d > file3dExtensions
Definition: FIRECore.H:106
Foam::fileFormats::FIRECore::putFireLabel
static void putFireLabel(OSstream &, const label)
Write an integer (ascii or binary)
Definition: FIRECore.C:202
Foam::fileFormats::FIRECore::readPoints
static label readPoints(ISstream &, pointField &)
Read points.
Definition: FIRECore.C:48
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177