KinematicParcelIO.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) 2016-2019 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 "KinematicParcel.H"
30 #include "IOstreams.H"
31 #include "IOField.H"
32 #include "Cloud.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 template<class ParcelType>
39 
40 
41 template<class ParcelType>
43 (
44  sizeof(KinematicParcel<ParcelType>)
45  - offsetof(KinematicParcel<ParcelType>, active_)
46 );
47 
48 
49 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50 
51 template<class ParcelType>
53 (
54  const polyMesh& mesh,
55  Istream& is,
56  bool readFields,
57  bool newFormat
58 )
59 :
60  ParcelType(mesh, is, readFields, newFormat),
61  active_(false),
62  typeId_(0),
63  nParticle_(0.0),
64  d_(0.0),
65  dTarget_(0.0),
66  U_(Zero),
67  rho_(0.0),
68  age_(0.0),
69  tTurb_(0.0),
70  UTurb_(Zero)
71 {
72  if (readFields)
73  {
74  if (is.format() == IOstream::ASCII)
75  {
76  is >> active_
77  >> typeId_
78  >> nParticle_
79  >> d_
80  >> dTarget_
81  >> U_
82  >> rho_
83  >> age_
84  >> tTurb_
85  >> UTurb_;
86  }
87  else if (!is.checkLabelSize<>() || !is.checkScalarSize<>())
88  {
89  // Non-native label or scalar size
90 
91  is.beginRawRead();
92 
93  readRawLabel(is, &active_);
94  readRawLabel(is, &typeId_);
95  readRawScalar(is, &nParticle_);
96  readRawScalar(is, &d_);
97  readRawScalar(is, &dTarget_);
98  readRawScalar(is, U_.data(), vector::nComponents);
99  readRawScalar(is, &rho_);
100  readRawScalar(is, &age_);
101  readRawScalar(is, &tTurb_);
102  readRawScalar(is, UTurb_.data(), vector::nComponents);
103 
104  is.endRawRead();
105  }
106  else
107  {
108  is.read(reinterpret_cast<char*>(&active_), sizeofFields);
109  }
110  }
111 
112  is.check(FUNCTION_NAME);
113 }
114 
115 
116 template<class ParcelType>
117 template<class CloudType>
119 {
120  const bool valid = c.size();
121 
123 
124  IOField<label> active
125  (
126  c.fieldIOobject("active", IOobject::MUST_READ),
127  valid
128  );
129  c.checkFieldIOobject(c, active);
130 
131  IOField<label> typeId
132  (
133  c.fieldIOobject("typeId", IOobject::MUST_READ),
134  valid
135  );
136  c.checkFieldIOobject(c, typeId);
137 
138  IOField<scalar> nParticle
139  (
140  c.fieldIOobject("nParticle", IOobject::MUST_READ),
141  valid
142  );
143  c.checkFieldIOobject(c, nParticle);
144 
146  (
147  c.fieldIOobject("d", IOobject::MUST_READ),
148  valid
149  );
150  c.checkFieldIOobject(c, d);
151 
152  IOField<scalar> dTarget
153  (
154  c.fieldIOobject("dTarget", IOobject::MUST_READ),
155  valid
156  );
157  c.checkFieldIOobject(c, dTarget);
158 
160  (
161  c.fieldIOobject("U", IOobject::MUST_READ),
162  valid
163  );
164  c.checkFieldIOobject(c, U);
165 
167  (
168  c.fieldIOobject("rho", IOobject::MUST_READ),
169  valid
170  );
171  c.checkFieldIOobject(c, rho);
172 
173  IOField<scalar> age
174  (
175  c.fieldIOobject("age", IOobject::MUST_READ),
176  valid
177  );
178  c.checkFieldIOobject(c, age);
179 
180  IOField<scalar> tTurb
181  (
182  c.fieldIOobject("tTurb", IOobject::MUST_READ),
183  valid
184  );
185  c.checkFieldIOobject(c, tTurb);
186 
187  IOField<vector> UTurb
188  (
189  c.fieldIOobject("UTurb", IOobject::MUST_READ),
190  valid
191  );
192  c.checkFieldIOobject(c, UTurb);
193 
194  label i = 0;
195 
197  {
198  p.active_ = active[i];
199  p.typeId_ = typeId[i];
200  p.nParticle_ = nParticle[i];
201  p.d_ = d[i];
202  p.dTarget_ = dTarget[i];
203  p.U_ = U[i];
204  p.rho_ = rho[i];
205  p.age_ = age[i];
206  p.tTurb_ = tTurb[i];
207  p.UTurb_ = UTurb[i];
208 
209  ++i;
210  }
211 }
212 
213 
214 template<class ParcelType>
215 template<class CloudType>
217 {
219 
220  const label np = c.size();
221  const bool valid = np;
222 
223  IOField<label> active(c.fieldIOobject("active", IOobject::NO_READ), np);
224  IOField<label> typeId(c.fieldIOobject("typeId", IOobject::NO_READ), np);
225  IOField<scalar> nParticle
226  (
227  c.fieldIOobject("nParticle", IOobject::NO_READ),
228  np
229  );
230  IOField<scalar> d(c.fieldIOobject("d", IOobject::NO_READ), np);
231  IOField<scalar> dTarget(c.fieldIOobject("dTarget", IOobject::NO_READ), np);
232  IOField<vector> U(c.fieldIOobject("U", IOobject::NO_READ), np);
233  IOField<scalar> rho(c.fieldIOobject("rho", IOobject::NO_READ), np);
234  IOField<scalar> age(c.fieldIOobject("age", IOobject::NO_READ), np);
235  IOField<scalar> tTurb(c.fieldIOobject("tTurb", IOobject::NO_READ), np);
236  IOField<vector> UTurb(c.fieldIOobject("UTurb", IOobject::NO_READ), np);
237 
238  label i = 0;
239 
240  for (const KinematicParcel<ParcelType>& p : c)
241  {
242  active[i] = p.active();
243  typeId[i] = p.typeId();
244  nParticle[i] = p.nParticle();
245  d[i] = p.d();
246  dTarget[i] = p.dTarget();
247  U[i] = p.U();
248  rho[i] = p.rho();
249  age[i] = p.age();
250  tTurb[i] = p.tTurb();
251  UTurb[i] = p.UTurb();
252 
253  ++i;
254  }
255 
256  active.write(valid);
257  typeId.write(valid);
258  nParticle.write(valid);
259  d.write(valid);
260  dTarget.write(valid);
261  U.write(valid);
262  rho.write(valid);
263  age.write(valid);
264  tTurb.write(valid);
265  UTurb.write(valid);
266 }
267 
268 
269 template<class ParcelType>
271 (
272  Ostream& os,
273  const wordRes& filters,
274  const word& delim,
275  const bool namesOnly
276 ) const
277 {
278  ParcelType::writeProperties(os, filters, delim, namesOnly);
279 
280  #undef writeProp
281  #define writeProp(Name, Value) \
282  ParcelType::writeProperty(os, Name, Value, namesOnly, delim, filters)
283 
284  writeProp("active", active_);
285  writeProp("typeId", typeId_);
286  writeProp("nParticle", nParticle_);
287  writeProp("d", d_);
288  writeProp("dTarget", dTarget_);
289  writeProp("U", U_);
290  writeProp("rho", rho_);
291  writeProp("age", age_);
292  writeProp("tTurb", tTurb_);
293  writeProp("UTurb", UTurb_);
294 
295  #undef writeProp
296 }
297 
298 
299 template<class ParcelType>
300 template<class CloudType>
302 (
303  CloudType& c,
304  const objectRegistry& obr
305 )
306 {
307  ParcelType::readObjects(c, obr);
308 
309  if (!c.size()) return;
310 
311  const auto& active = cloud::lookupIOField<label>("active", obr);
312  const auto& typeId = cloud::lookupIOField<label>("typeId", obr);
313  const auto& nParticle = cloud::lookupIOField<scalar>("nParticle", obr);
314  const auto& d = cloud::lookupIOField<scalar>("d", obr);
315  const auto& dTarget = cloud::lookupIOField<scalar>("dTarget", obr);
316  const auto& U = cloud::lookupIOField<vector>("U", obr);
317  const auto& rho = cloud::lookupIOField<scalar>("rho", obr);
318  const auto& age = cloud::lookupIOField<scalar>("age", obr);
319  const auto& tTurb = cloud::lookupIOField<scalar>("tTurb", obr);
320  const auto& UTurb = cloud::lookupIOField<vector>("UTurb", obr);
321 
322  label i = 0;
323 
325  {
326  p.active_ = active[i];
327  p.typeId_ = typeId[i];
328  p.nParticle_ = nParticle[i];
329  p.d_ = d[i];
330  p.dTarget_ = dTarget[i];
331  p.U_ = U[i];
332  p.rho_ = rho[i];
333  p.age_ = age[i];
334  p.tTurb_ = tTurb[i];
335  p.UTurb_ = UTurb[i];
336 
337  ++i;
338  }
339 }
340 
341 
342 template<class ParcelType>
343 template<class CloudType>
345 (
346  const CloudType& c,
347  objectRegistry& obr
348 )
349 {
350  ParcelType::writeObjects(c, obr);
351 
352  const label np = c.size();
353 
354  auto& active = cloud::createIOField<label>("active", np, obr);
355  auto& typeId = cloud::createIOField<label>("typeId", np, obr);
356  auto& nParticle = cloud::createIOField<scalar>("nParticle", np, obr);
357  auto& d = cloud::createIOField<scalar>("d", np, obr);
358  auto& dTarget = cloud::createIOField<scalar>("dTarget", np, obr);
359  auto& U = cloud::createIOField<vector>("U", np, obr);
360  auto& rho = cloud::createIOField<scalar>("rho", np, obr);
361  auto& age = cloud::createIOField<scalar>("age", np, obr);
362  auto& tTurb = cloud::createIOField<scalar>("tTurb", np, obr);
363  auto&& UTurb = cloud::createIOField<vector>("UTurb", np, obr);
364 
365  label i = 0;
366 
367  for (const KinematicParcel<ParcelType>& p : c)
368  {
369  active[i] = p.active();
370  typeId[i] = p.typeId();
371  nParticle[i] = p.nParticle();
372  d[i] = p.d();
373  dTarget[i] = p.dTarget();
374  U[i] = p.U();
375  rho[i] = p.rho();
376  age[i] = p.age();
377  tTurb[i] = p.tTurb();
378  UTurb[i] = p.UTurb();
379 
380  ++i;
381  }
382 }
383 
384 
385 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
386 
387 template<class ParcelType>
388 Foam::Ostream& Foam::operator<<
389 (
390  Ostream& os,
392 )
393 {
394  if (os.format() == IOstream::ASCII)
395  {
396  os << static_cast<const ParcelType&>(p)
397  << token::SPACE << bool(p.active())
398  << token::SPACE << p.typeId()
399  << token::SPACE << p.nParticle()
400  << token::SPACE << p.d()
401  << token::SPACE << p.dTarget()
402  << token::SPACE << p.U()
403  << token::SPACE << p.rho()
404  << token::SPACE << p.age()
405  << token::SPACE << p.tTurb()
406  << token::SPACE << p.UTurb();
407  }
408  else
409  {
410  os << static_cast<const ParcelType&>(p);
411  os.write
412  (
413  reinterpret_cast<const char*>(&p.active_),
414  KinematicParcel<ParcelType>::sizeofFields
415  );
416  }
417 
418  os.check(FUNCTION_NAME);
419  return os;
420 }
421 
422 
423 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Cloud.H
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
rho
rho
Definition: readInitialConditions.H:96
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::KinematicParcel::writeFields
static void writeFields(const TrackCloudType &c)
Write.
KinematicParcel.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Istream::endRawRead
virtual bool endRawRead()=0
End of low-level raw binary read.
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOstream::checkLabelSize
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const
Definition: IOstream.H:283
Foam::KinematicParcel::KinematicParcel
KinematicParcel(const polyMesh &mesh, const barycentric &coordinates, const label celli, const label tetFacei, const label tetPti)
Construct from mesh, coordinates and topology.
Definition: KinematicParcelI.H:77
Foam::KinematicParcel::readObjects
static void readObjects(CloudType &c, const objectRegistry &obr)
Read particle fields as objects from the obr registry.
Definition: KinematicParcelIO.C:302
Foam::writeFields
void writeFields(const fvMesh &mesh, const wordHashSet &selectedFields)
writeProp
#define writeProp(Name, Value)
Foam::DSMCCloud
Templated base class for dsmc cloud.
Definition: DSMCCloud.H:71
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::KinematicParcel::writeObjects
static void writeObjects(const CloudType &c, objectRegistry &obr)
Write particle fields as objects into the obr registry.
Definition: KinematicParcelIO.C:345
Foam::KinematicParcel::sizeofFields
static const std::size_t sizeofFields
Size in bytes of the fields.
Definition: KinematicParcel.H:94
Foam::readFields
void readFields(const typename GeoFieldType::Mesh &mesh, const IOobjectList &objects, const wordHashSet &selectedFields, LIFOStack< regIOobject * > &storedObjects)
Read the selected GeometricFields of the templated type.
Definition: ReadFieldsTemplates.C:312
U
U
Definition: pEqn.H:72
Foam::Istream::beginRawRead
virtual bool beginRawRead()=0
Start of low-level raw binary read.
Foam::IOstream::checkScalarSize
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize() const
Definition: IOstream.H:292
IOField.H
Foam::KinematicParcel::readFields
static void readFields(TrackCloudType &c)
Read.
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::KinematicParcel
Kinematic parcel class with rotational motion (as spherical particles only) and one/two-way coupling ...
Definition: KinematicParcel.H:66
bool
bool
Definition: EEqn.H:20
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::readRawLabel
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:46
Foam::KinematicParcel::writeProperties
void writeProperties(Ostream &os, const wordRes &filters, const word &delim, const bool namesOnly=false) const
Write individual parcel properties to stream.
Definition: KinematicParcelIO.C:271
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.