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>
88 void Foam::DSMCParcel<ParcelType>::readFields(Cloud<DSMCParcel<ParcelType>>& c)
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 (
121  const Cloud<DSMCParcel<ParcelType>>& c
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,
153  const DSMCParcel<ParcelType>& p
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_),
170  );
171  }
172 
174  return os;
175 }
176 
177 
178 // ************************************************************************* //
Foam::DSMCParcel
DSMC parcel class.
Definition: DSMCParcel.H:50
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)
Definition: DSMCParcelI.H:48
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:57
Foam::Zero
static constexpr const zero Zero
Definition: zero.H:131
Cloud.H
Foam::IOstreamOption::format
streamFormat format() const noexcept
Definition: IOstreamOption.H:282
Foam::VectorSpace::data
Cmpt * data() noexcept
Definition: VectorSpaceI.H:178
Foam::OBJstream::write
virtual Ostream & write(const char c)
Definition: OBJstream.C:71
Foam::DSMCParcel::typeId_
label typeId_
Definition: DSMCParcel.H:144
Foam::DSMCParcel::readFields
static void readFields(Cloud< DSMCParcel< ParcelType >> &c)
Definition: DSMCParcelIO.C:81
Foam::DSMCParcel::U_
vector U_
Definition: DSMCParcel.H:137
Foam::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.C:51
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
DSMCParcel.H
Foam::DSMCParcel::Ei_
scalar Ei_
Definition: DSMCParcel.H:141
Foam::readFields
void readFields(const typename GeoFieldType::Mesh &mesh, const IOobjectList &objects, const wordHashSet &selectedFields, LIFOStack< regIOobject * > &storedObjects)
Definition: ReadFieldsTemplates.C:305
U
U
Definition: pEqn.H:72
Foam::IOstreamOption::ASCII
@ ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:68
IOField.H
Foam::DSMCParcel::writeFields
static void writeFields(const Cloud< DSMCParcel< ParcelType >> &c)
Definition: DSMCParcelIO.C:113
Foam::token::SPACE
@ SPACE
Space [isspace].
Definition: token.H:121
Foam::Cloud
Base cloud calls templated on particle type.
Definition: Cloud.H:51
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:302
Foam::constant::universal::c
const dimensionedScalar c
Foam::readRawLabel
label readRawLabel(Istream &is)
Definition: label.C:39
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:184
Foam::VectorSpace< Vector< scalar >, scalar, 3 >::nComponents
static constexpr direction nComponents
Definition: VectorSpace.H:97
Foam::writeFields
void writeFields(const fvMesh &mesh, const wordHashSet &selectedFields, const bool writeFaceFields)
Foam::DSMCParcel::sizeofFields
static const std::size_t sizeofFields
Definition: DSMCParcel.H:73
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181