DSMCParcelIO.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 "DSMCParcel.H"
30 #include "IOstreams.H"
31 #include "IOField.H"
32 #include "Cloud.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 template<class ParcelType>
38 (
39  sizeof(DSMCParcel<ParcelType>) - sizeof(ParcelType)
40 );
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
45 template<class ParcelType>
47 (
48  const polyMesh& mesh,
49  Istream& is,
50  bool readFields,
51  bool newFormat
52 )
53 :
54  ParcelType(mesh, is, readFields, newFormat),
55  U_(Zero),
56  Ei_(0.0),
57  typeId_(-1)
58 {
59  if (readFields)
60  {
61  if (is.format() == IOstream::ASCII)
62  {
63  is >> U_ >> Ei_ >> typeId_;
64  }
65  else if (!is.checkLabelSize<>() || !is.checkScalarSize<>())
66  {
67  // Non-native label or scalar size
68 
69  is.beginRawRead();
70 
71  readRawScalar(is, U_.data(), vector::nComponents);
72  readRawScalar(is, &Ei_);
73  readRawLabel(is, &typeId_);
74 
75  is.endRawRead();
76  }
77  else
78  {
79  is.read(reinterpret_cast<char*>(&U_), sizeofFields);
80  }
81  }
82 
83  is.check(FUNCTION_NAME);
84 }
85 
86 
87 template<class ParcelType>
89 {
90  bool valid = c.size();
91 
93 
94  IOField<vector> U(c.fieldIOobject("U", IOobject::MUST_READ), valid);
95  c.checkFieldIOobject(c, U);
96 
97  IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::MUST_READ), valid);
98  c.checkFieldIOobject(c, Ei);
99 
100  IOField<label> typeId
101  (
102  c.fieldIOobject("typeId", IOobject::MUST_READ),
103  valid
104  );
105  c.checkFieldIOobject(c, typeId);
106 
107  label i = 0;
108  for (DSMCParcel<ParcelType>& p : c)
109  {
110  p.U_ = U[i];
111  p.Ei_ = Ei[i];
112  p.typeId_ = typeId[i];
113  ++i;
114  }
115 }
116 
117 
118 template<class ParcelType>
120 (
122 )
123 {
125 
126  label np = c.size();
127 
128  IOField<vector> U(c.fieldIOobject("U", IOobject::NO_READ), np);
129  IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::NO_READ), np);
130  IOField<label> typeId(c.fieldIOobject("typeId", IOobject::NO_READ), np);
131 
132  label i = 0;
133  for (const DSMCParcel<ParcelType>& p : c)
134  {
135  U[i] = p.U();
136  Ei[i] = p.Ei();
137  typeId[i] = p.typeId();
138  ++i;
139  }
140 
141  U.write(np > 0);
142  Ei.write(np > 0);
143  typeId.write(np > 0);
144 }
145 
146 
147 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
148 
149 template<class ParcelType>
150 Foam::Ostream& Foam::operator<<
151 (
152  Ostream& os,
154 )
155 {
156  if (os.format() == IOstream::ASCII)
157  {
158  os << static_cast<const ParcelType& >(p)
159  << token::SPACE << p.U()
160  << token::SPACE << p.Ei()
161  << token::SPACE << p.typeId();
162  }
163  else
164  {
165  os << static_cast<const ParcelType& >(p);
166  os.write
167  (
168  reinterpret_cast<const char*>(&p.U_),
169  DSMCParcel<ParcelType>::sizeofFields
170  );
171  }
172 
173  os.check(FUNCTION_NAME);
174  return os;
175 }
176 
177 
178 // ************************************************************************* //
Foam::DSMCParcel
DSMC parcel class.
Definition: DSMCParcel.H:54
Foam::DSMCParcel::DSMCParcel
DSMCParcel(const polyMesh &mesh, const barycentric &coordinates, const label celli, const label tetFacei, const label tetPti, const vector &U, const scalar Ei, const label typeId)
Construct from components.
Definition: DSMCParcelI.H:55
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::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 (0)
Definition: zero.H:131
Cloud.H
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::DSMCParcel::readFields
static void readFields(Cloud< DSMCParcel< ParcelType >> &c)
Definition: DSMCParcelIO.C:88
Foam::IOstream::checkLabelSize
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const noexcept
Definition: IOstream.H:300
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::checkScalarSize
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize() const noexcept
Definition: IOstream.H:309
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
DSMCParcel.H
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.
IOField.H
Foam::DSMCParcel::writeFields
static void writeFields(const Cloud< DSMCParcel< ParcelType >> &c)
Definition: DSMCParcelIO.C:120
Foam::Cloud
Base cloud calls templated on particle type.
Definition: Cloud.H:55
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
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::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::writeFields
void writeFields(const fvMesh &mesh, const wordHashSet &selectedFields, const bool writeFaceFields)
Foam::DSMCParcel::sizeofFields
static const std::size_t sizeofFields
Size in bytes of the fields.
Definition: DSMCParcel.H:77
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.