FreeFOAM The Cross-Platform CFD Toolkit
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 | Copyright (C) 2009-2010 OpenCFD Ltd.
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 <OpenFOAM/IOstreams.H>
28 #include <OpenFOAM/IOField.H>
29 #include <lagrangian/Cloud.H>
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template <class ParcelType>
35 (
36  const Cloud<ParcelType>& cloud,
37  Istream& is,
38  bool readFields
39 )
40 :
41  Particle<ParcelType>(cloud, is, readFields),
42  U_(vector::zero),
43  Ei_(0.0),
44  typeId_(-1)
45 {
46  if (readFields)
47  {
48  if (is.format() == IOstream::ASCII)
49  {
50  is >> U_;
51  Ei_ = readScalar(is);
52  typeId_ = readLabel(is);
53  }
54  else
55  {
56  is.read
57  (
58  reinterpret_cast<char*>(&U_),
59  sizeof(U_)
60  + sizeof(Ei_)
61  + sizeof(typeId_)
62  );
63  }
64  }
65 
66  // Check state of Istream
67  is.check
68  (
69  "DsmcParcel<ParcelType>::DsmcParcel"
70  "(const Cloud<ParcelType>&, Istream&, bool)"
71  );
72 }
73 
74 
75 template<class ParcelType>
77 {
78  if (!c.size())
79  {
80  return;
81  }
82 
84 
85  IOField<vector> U(c.fieldIOobject("U", IOobject::MUST_READ));
86  c.checkFieldIOobject(c, U);
87 
88  IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::MUST_READ));
89  c.checkFieldIOobject(c, Ei);
90 
91  IOField<label> typeId(c.fieldIOobject("typeId", IOobject::MUST_READ));
92  c.checkFieldIOobject(c, typeId);
93 
94  label i = 0;
95  forAllIter(typename Cloud<ParcelType>, c, iter)
96  {
97  ParcelType& p = iter();
98 
99  p.U_ = U[i];
100  p.Ei_ = Ei[i];
101  p.typeId_ = typeId[i];
102  i++;
103  }
104 }
105 
106 
107 template<class ParcelType>
109 {
111 
112  label np = c.size();
113 
114  IOField<vector> U(c.fieldIOobject("U", IOobject::NO_READ), np);
115  IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::NO_READ), np);
116  IOField<label> typeId(c.fieldIOobject("typeId", IOobject::NO_READ), np);
117 
118  label i = 0;
119  forAllConstIter(typename Cloud<ParcelType>, c, iter)
120  {
121  const DsmcParcel<ParcelType>& p = iter();
122 
123  U[i] = p.U();
124  Ei[i] = p.Ei();
125  typeId[i] = p.typeId();
126  i++;
127  }
128 
129  U.write();
130  Ei.write();
131  typeId.write();
132 }
133 
134 
135 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
136 
137 template<class ParcelType>
138 Foam::Ostream& Foam::operator<<
139 (
140  Ostream& os,
142 )
143 {
144  if (os.format() == IOstream::ASCII)
145  {
146  os << static_cast<const Particle<ParcelType>& >(p)
147  << token::SPACE << p.U()
148  << token::SPACE << p.Ei()
149  << token::SPACE << p.typeId();
150  }
151  else
152  {
153  os << static_cast<const Particle<ParcelType>& >(p);
154  os.write
155  (
156  reinterpret_cast<const char*>(&p.U_),
157  sizeof(p.U())
158  + sizeof(p.Ei())
159  + sizeof(p.typeId())
160  );
161  }
162 
163  // Check state of Ostream
164  os.check
165  (
166  "Ostream& operator<<(Ostream&, const DsmcParcel<ParcelType>&)"
167  );
168 
169  return os;
170 }
171 
172 
173 // ************************ vim: set sw=4 sts=4 et: ************************ //