Loading...
Searching...
No Matches
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-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
32const Foam::Enum
33<
35>
37({
38 { fileExt3d::POLY_ASCII, "fpma" },
39 { fileExt3d::POLY_BINARY, "fpmb" },
41 { fileExt3d::POLY_BINARY_Z, "fpmbz" },
42});
43
44
45// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
46
48(
49 ISstream& is,
50 pointField& points
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 }
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{
91 {
92 fireInt_t ivalue;
93
94 is.stdStream().read
95 (
96 reinterpret_cast<char *>(&ivalue),
97 sizeof(ivalue)
98 );
99 is.syncState();
100
101 return ivalue;
102 }
103 else
104 {
105 return readLabel(is);
106 }
107}
108
109
111{
112 point pt;
113
114 if (is.format() == IOstreamOption::BINARY)
115 {
116 fireReal_t coord[3];
117
118 is.stdStream().read
119 (
120 reinterpret_cast<char *>(&coord),
121 sizeof(coord)
122 );
123 is.syncState();
124
125 pt.x() = coord[0];
126 pt.y() = coord[1];
127 pt.z() = coord[2];
128 }
129 else
130 {
131 pt.x() = readScalar(is);
132 pt.y() = readScalar(is);
133 pt.z() = readScalar(is);
134 }
135
136 return pt;
137}
138
139
141{
142 std::string str;
143
144 if (is.format() == IOstreamOption::BINARY)
145 {
146 long len;
147
148 is.stdStream().read
149 (
150 reinterpret_cast<char *>(&len),
151 sizeof(len)
152 );
153
154 str.resize(len);
155
156 for (std::size_t pos = 0; pos < str.size(); ++pos)
157 {
158 is.stdStream().read(&(str[pos]), sizeof(char));
159 }
160
161 is.syncState();
162 }
163 else
164 {
165 const std::string whitespace(" \t\f\v\n\r");
166
167 string s;
168
169 // use a low-level getline, but this means we must handle
170 // blank lines manually
171 while (s.empty())
172 {
173 is.getLine(s);
174 if (!s.empty())
175 {
176 // remove prefix whitespace
177 size_t pos = s.find_first_not_of(whitespace);
178
179 if (pos != std::string::npos)
180 {
181 s.erase(0, pos);
182
183 // remove suffix whitespace
184 pos = s.find_last_not_of(whitespace);
185 if (pos != std::string::npos)
186 {
187 s.erase(pos + 1);
188 }
189 }
190
191 if (pos == std::string::npos)
192 {
193 s.clear();
194 }
195 }
196 }
197
198 str.swap(s);
199 }
200
201 return str;
202}
203
204
206(
207 OSstream& os,
208 const label value
209)
210{
211 if (os.format() == IOstreamOption::BINARY)
212 {
213 fireInt_t ivalue(value);
214
215 os.stdStream().write
216 (
217 reinterpret_cast<char const *>(&ivalue),
218 sizeof(ivalue)
219 );
220
221 os.syncState();
222 }
223 else
224 {
225 os << value;
226 }
227}
228
229
231(
232 OSstream& os,
233 const labelUList& lst
234)
235{
236 if (os.format() == IOstreamOption::BINARY)
237 {
238 fireInt_t ivalue(lst.size());
239
240 os.stdStream().write
241 (
242 reinterpret_cast<char const *>(&ivalue),
243 sizeof(ivalue)
244 );
245
246 forAll(lst, i)
247 {
248 ivalue = lst[i];
249
250 os.stdStream().write
251 (
252 reinterpret_cast<char const *>(&ivalue),
253 sizeof(ivalue)
254 );
255 }
256
257 os.syncState();
258 }
259 else
260 {
261 os << ' ' << lst.size();
262 forAll(lst, i)
263 {
264 os << ' ' << lst[i];
265 }
266 os << '\n';
267 }
268}
269
270
272(
273 OSstream& os,
274 const label count,
275 const label start
276)
277{
278 if (os.format() == IOstreamOption::BINARY)
279 {
280 fireInt_t ivalue(count);
281
282 os.stdStream().write
283 (
284 reinterpret_cast<char const *>(&ivalue),
285 sizeof(ivalue)
286 );
287
288 ivalue = start;
289 for (label i=0; i < count; ++i, ++ivalue)
290 {
291 os.stdStream().write
292 (
293 reinterpret_cast<char const *>(&ivalue),
294 sizeof(ivalue)
295 );
296 }
297
298 os.syncState();
299 }
300 else
301 {
302 os << ' ' << count;
303
304 label ivalue = start;
305 for (label i = 0; i < count; ++i, ++ivalue)
306 {
307 os << ' ' << ivalue;
308 }
309 os << '\n';
310 }
311}
312
313
315(
316 OSstream& os,
317 const point& value
318)
319{
320 if (os.format() == IOstreamOption::BINARY)
321 {
322 fireReal_t fvalue[3];
323 fvalue[0] = value.x();
324 fvalue[1] = value.y();
325 fvalue[2] = value.z();
326
327 os.stdStream().write
328 (
329 reinterpret_cast<char const *>(&fvalue),
330 sizeof(fvalue)
331 );
332
333 os.syncState();
334 }
335 else
336 {
337 os << ' '
338 << value.x() << ' '
339 << value.y() << ' '
340 << value.z() << '\n';
341 }
342}
343
344
346(
347 OSstream& os,
348 const std::string& value
349)
350{
351 if (os.format() == IOstreamOption::BINARY)
352 {
353 long len(value.size());
354
355 os.stdStream().write
356 (
357 reinterpret_cast<char const *>(&len),
358 sizeof(len)
359 );
360
361 os.stdStream().write(value.data(), len);
362 }
363 else
364 {
365 // output without surrounding quotes
366 os.stdStream() << value << '\n';
367 }
368
369 os.syncState();
370}
371
372
373// ************************************************************************* //
label n
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
streamFormat format() const noexcept
Get the current stream format.
Generic input stream using a standard (STL) stream.
Definition ISstream.H:54
virtual const fileName & name() const override
The name of the input serial stream. (eg, the name of the Fstream file name).
Definition ISstream.H:147
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition ISstreamI.H:69
void syncState()
Set stream state to match that of the std::istream.
Definition ISstream.H:195
virtual const std::istream & stdStream() const
Const access to underlying std::istream.
Definition ISstream.H:163
Generic output stream using a standard (STL) stream.
Definition OSstream.H:53
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
const Cmpt & x() const noexcept
Access to the vector x component.
Definition Vector.H:135
const Cmpt & z() const noexcept
Access to the vector z component.
Definition Vector.H:145
const Cmpt & y() const noexcept
Access to the vector y component.
Definition Vector.H:140
static const Enum< fileExt3d > file3dExtensions
Definition FIRECore.H:111
static std::string getFireString(ISstream &)
Extract a string (ascii or binary).
Definition FIRECore.C:133
double fireReal_t
Float type (binary format).
Definition FIRECore.H:104
static void putFireLabel(OSstream &, const label)
Write an integer (ascii or binary).
Definition FIRECore.C:199
static fileName fireFileName(const fileName &baseName, const enum fileExt3d)
Resolve base file-name for the given file-type.
Definition FIRECore.C:72
static point getFirePoint(ISstream &)
Get an point x/y/z (ascii or binary).
Definition FIRECore.C:103
static void putFireString(OSstream &, const std::string &)
Write a string (ascii or binary).
Definition FIRECore.C:339
fileExt3d
Enumeration defining the file extensions for 3D types.
Definition FIRECore.H:88
static void putFireLabels(OSstream &, const labelUList &)
Write multiple integers (ascii or binary).
Definition FIRECore.C:224
static label readPoints(ISstream &, pointField &)
Read points.
Definition FIRECore.C:41
int32_t fireInt_t
Integer type (binary format).
Definition FIRECore.H:99
static void putFirePoint(OSstream &, const point &)
Write a point x/y/z (ascii or binary).
Definition FIRECore.C:308
static label getFireLabel(ISstream &)
Get an integer (ascii or binary).
Definition FIRECore.C:81
A class for handling file names.
Definition fileName.H:75
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
const pointField & points
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))
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition BitOps.H:73
dimensionedScalar pos(const dimensionedScalar &ds)
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition label.H:63
errorManip< error > abort(error &err)
Definition errorManip.H:139
vector point
Point is a vector.
Definition point.H:37
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
vectorField pointField
pointField is a vectorField.
UList< label > labelUList
A UList of labels.
Definition UList.H:75
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299