FreeFOAM The Cross-Platform CFD Toolkit
ParticleIO.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) 1991-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 "Particle.H"
27 #include <OpenFOAM/IOstreams.H>
28 #include <lagrangian/IOPosition.H>
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 template<class ParticleType>
34  "(Px Py Pz) cellI origProc origId";
35 
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 
38 template<class ParticleType>
40 (
42  Istream& is,
43  bool readFields
44 )
45 :
46  cloud_(cloud),
47  facei_(-1),
48  stepFraction_(0.0),
49  origProc_(Pstream::myProcNo()),
50  origId_(-1)
51 {
52 
53  // readFields : read additional data. Should be consistent with writeFields.
54 
55  if (is.format() == IOstream::ASCII)
56  {
57  is >> position_ >> celli_;
58  if (readFields)
59  {
60  is >> origProc_ >> origId_;
61  }
62  }
63  else
64  {
65  // In binary read all particle data - needed for parallel transfer
66  if (readFields)
67  {
68  is.read
69  (
70  reinterpret_cast<char*>(&position_),
71  sizeof(position_)
72  + sizeof(celli_)
73  + sizeof(facei_)
74  + sizeof(stepFraction_)
75  + sizeof(origProc_)
76  + sizeof(origId_)
77  );
78  }
79  else
80  {
81  is.read
82  (
83  reinterpret_cast<char*>(&position_),
84  sizeof(position_)
85  + sizeof(celli_)
86  + sizeof(facei_)
87  + sizeof(stepFraction_)
88  );
89  }
90  }
91 
92  if (celli_ == -1)
93  {
94  celli_ = cloud_.pMesh().findCell(position_);
95  }
96 
97  // Check state of Istream
98  is.check("Particle<ParticleType>::Particle(Istream&)");
99 }
100 
101 
102 template<class ParticleType>
104 (
106 )
107 {
108  if (!c.size())
109  {
110  return;
111  }
112 
113  IOobject procIO(c.fieldIOobject("origProcId", IOobject::MUST_READ));
114 
115  if (procIO.headerOk())
116  {
117  IOField<label> origProcId(procIO);
118  c.checkFieldIOobject(c, origProcId);
119  IOField<label> origId(c.fieldIOobject("origId", IOobject::MUST_READ));
120  c.checkFieldIOobject(c, origId);
121 
122  label i = 0;
123  forAllIter(typename Cloud<ParticleType>, c, iter)
124  {
125  ParticleType& p = iter();
126 
127  p.origProc_ = origProcId[i];
128  p.origId_ = origId[i];
129  i++;
130  }
131  }
132 }
133 
134 
135 template<class ParticleType>
137 (
138  const Cloud<ParticleType>& c
139 )
140 {
141  // Write the cloud position file
143  ioP.write();
144 
145  label np = c.size();
146 
147  IOField<label> origProc
148  (
149  c.fieldIOobject
150  (
151  "origProcId",
152  IOobject::NO_READ
153  ),
154  np
155  );
156  IOField<label> origId(c.fieldIOobject("origId", IOobject::NO_READ), np);
157 
158  label i = 0;
159  forAllConstIter(typename Cloud<ParticleType>, c, iter)
160  {
161  origProc[i] = iter().origProc_;
162  origId[i] = iter().origId_;
163  i++;
164  }
165 
166  origProc.write();
167  origId.write();
168 }
169 
170 
171 template<class ParticleType>
172 void Foam::Particle<ParticleType>::write(Ostream& os, bool writeFields) const
173 {
174  if (os.format() == IOstream::ASCII)
175  {
176  if (writeFields)
177  {
178  // Write the additional entries
179  os << position_
180  << token::SPACE << celli_
181  << token::SPACE << origProc_
182  << token::SPACE << origId_;
183  }
184  else
185  {
186  os << position_
187  << token::SPACE << celli_;
188  }
189  }
190  else
191  {
192  // In binary write both celli_ and facei_, needed for parallel transfer
193  if (writeFields)
194  {
195  os.write
196  (
197  reinterpret_cast<const char*>(&position_),
198  sizeof(position_)
199  + sizeof(celli_)
200  + sizeof(facei_)
201  + sizeof(stepFraction_)
202  + sizeof(origProc_)
203  + sizeof(origId_)
204  );
205  }
206  else
207  {
208  os.write
209  (
210  reinterpret_cast<const char*>(&position_),
211  sizeof(position_)
212  + sizeof(celli_)
213  + sizeof(facei_)
214  + sizeof(stepFraction_)
215  );
216  }
217  }
218 
219  // Check state of Ostream
220  os.check("Particle<ParticleType>::write(Ostream& os, bool) const");
221 }
222 
223 
224 template<class ParticleType>
225 Foam::Ostream& Foam::operator<<(Ostream& os, const Particle<ParticleType>& p)
226 {
227  // Write all data
228  p.write(os, true);
229 
230  return os;
231 }
232 
233 
234 // ************************ vim: set sw=4 sts=4 et: ************************ //