DSMCParcelIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "DSMCParcel.H"
27 #include "IOstreams.H"
28 #include "IOField.H"
29 #include "Cloud.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 template<class ParcelType>
35 (
36  sizeof(DSMCParcel<ParcelType>) - sizeof(ParcelType)
37 );
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
42 template<class ParcelType>
44 (
45  const polyMesh& mesh,
46  Istream& is,
47  bool readFields
48 )
49 :
50  ParcelType(mesh, is, readFields),
51  U_(Zero),
52  Ei_(0.0),
53  typeId_(-1)
54 {
55  if (readFields)
56  {
57  if (is.format() == IOstream::ASCII)
58  {
59  is >> U_;
60  Ei_ = readScalar(is);
61  typeId_ = readLabel(is);
62  }
63  else
64  {
65  is.read(reinterpret_cast<char*>(&U_), sizeofFields_);
66  }
67  }
68 
69  // Check state of Istream
70  is.check
71  (
72  "DSMCParcel<ParcelType>::DSMCParcel"
73  "(const Cloud<ParcelType>&, Istream&, bool)"
74  );
75 }
76 
77 
78 template<class ParcelType>
80 {
81  bool valid = c.size();
82 
84 
85  IOField<vector> U(c.fieldIOobject("U", IOobject::MUST_READ), valid);
86  c.checkFieldIOobject(c, U);
87 
88  IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::MUST_READ), valid);
89  c.checkFieldIOobject(c, Ei);
90 
91  IOField<label> typeId
92  (
93  c.fieldIOobject("typeId", IOobject::MUST_READ),
94  valid
95  );
96  c.checkFieldIOobject(c, typeId);
97 
98  label i = 0;
99  forAllIter(typename Cloud<DSMCParcel<ParcelType>>, c, iter)
100  {
101  DSMCParcel<ParcelType>& p = iter();
102 
103  p.U_ = U[i];
104  p.Ei_ = Ei[i];
105  p.typeId_ = typeId[i];
106  i++;
107  }
108 }
109 
110 
111 template<class ParcelType>
113 (
115 )
116 {
117  ParcelType::writeFields(c);
118 
119  label np = c.size();
120 
121  IOField<vector> U(c.fieldIOobject("U", IOobject::NO_READ), np);
122  IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::NO_READ), np);
123  IOField<label> typeId(c.fieldIOobject("typeId", IOobject::NO_READ), np);
124 
125  label i = 0;
127  {
128  const DSMCParcel<ParcelType>& p = iter();
129 
130  U[i] = p.U();
131  Ei[i] = p.Ei();
132  typeId[i] = p.typeId();
133  i++;
134  }
135 
136  U.write(np > 0);
137  Ei.write(np > 0);
138  typeId.write(np > 0);
139 }
140 
141 
142 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
143 
144 template<class ParcelType>
145 Foam::Ostream& Foam::operator<<
146 (
147  Ostream& os,
149 )
150 {
151  if (os.format() == IOstream::ASCII)
152  {
153  os << static_cast<const ParcelType& >(p)
154  << token::SPACE << p.U()
155  << token::SPACE << p.Ei()
156  << token::SPACE << p.typeId();
157  }
158  else
159  {
160  os << static_cast<const ParcelType& >(p);
161  os.write
162  (
163  reinterpret_cast<const char*>(&p.U_),
165  );
166  }
167 
168  // Check state of Ostream
169  os.check
170  (
171  "Ostream& operator<<(Ostream&, const DSMCParcel<ParcelType>&)"
172  );
173 
174  return os;
175 }
176 
177 
178 // ************************************************************************* //
#define readScalar
Definition: doubleScalar.C:38
U
Definition: pEqn.H:72
void readFields(const typename GeoFieldType::Mesh &mesh, const IOobjectList &objects, const HashSet< word > &selectedFields, LIFOStack< regIOobject *> &storedObjects)
Read the selected GeometricFields of the specified type.
Definition: ReadFields.C:244
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:459
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
label typeId_
Parcel type id.
Definition: DSMCParcel.H:147
DSMC parcel class.
Definition: DSMCParcel.H:51
const dimensionedScalar c
Speed of light in a vacuum.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static void readFields(Cloud< DSMCParcel< ParcelType >> &c)
Definition: DSMCParcelIO.C:79
static void writeFields(const Cloud< DSMCParcel< ParcelType >> &c)
Definition: DSMCParcelIO.C:113
virtual Istream & read(token &)=0
Return next token from stream.
static const zero Zero
Definition: zero.H:97
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
label readLabel(Istream &is)
Definition: label.H:64
Base cloud calls templated on particle type.
Definition: Cloud.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
scalar Ei_
Internal energy of the Parcel, covering all non-translational.
Definition: DSMCParcel.H:144
scalar Ei() const
Return const access to internal energy.
Definition: DSMCParcelI.H:151
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:76
vector U_
Velocity of Parcel [m/s].
Definition: DSMCParcel.H:140
volScalarField & p
label typeId() const
Return type id.
Definition: DSMCParcelI.H:137
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:56
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50
const vector & U() const
Return const access to velocity.
Definition: DSMCParcelI.H:144