CloudIO.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) 2011 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 "Cloud.H"
27 #include "Time.H"
28 #include "IOPosition.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 template<class ParticleType>
34 
35 
36 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
37 
38 template<class ParticleType>
40 {
41  IOobject dictObj
42  (
43  cloudPropertiesName,
44  time().timeName(),
45  "uniform"/cloud::prefix/name(),
46  db(),
47  IOobject::MUST_READ_IF_MODIFIED,
48  IOobject::NO_WRITE,
49  false
50  );
51 
52  if (dictObj.headerOk())
53  {
54  const IOdictionary uniformPropsDict(dictObj);
55 
56  const word procName("processor" + Foam::name(Pstream::myProcNo()));
57  if (uniformPropsDict.found(procName))
58  {
59  uniformPropsDict.subDict(procName).lookup("particleCount")
60  >> ParticleType::particleCount_;
61  }
62  }
63  else
64  {
65  ParticleType::particleCount_ = 0;
66  }
67 }
68 
69 
70 template<class ParticleType>
72 {
73  IOdictionary uniformPropsDict
74  (
75  IOobject
76  (
77  cloudPropertiesName,
78  time().timeName(),
79  "uniform"/cloud::prefix/name(),
80  db(),
81  IOobject::NO_READ,
82  IOobject::NO_WRITE,
83  false
84  )
85  );
86 
87  labelList np(Pstream::nProcs(), 0);
88  np[Pstream::myProcNo()] = ParticleType::particleCount_;
89 
90  Pstream::listCombineGather(np, maxEqOp<label>());
91  Pstream::listCombineScatter(np);
92 
93  forAll(np, i)
94  {
95  word procName("processor" + Foam::name(i));
96  uniformPropsDict.add(procName, dictionary());
97  uniformPropsDict.subDict(procName).add("particleCount", np[i]);
98  }
99 
100  uniformPropsDict.writeObject
101  (
102  IOstream::ASCII,
103  IOstream::currentVersion,
104  time().writeCompression()
105  );
106 }
107 
108 
109 template<class ParticleType>
110 void Foam::Cloud<ParticleType>::initCloud(const bool checkClass)
111 {
112  readCloudUniformProperties();
113 
114  IOPosition<Cloud<ParticleType> > ioP(*this);
115 
116  if (ioP.headerOk())
117  {
118  ioP.readData(*this, checkClass);
119  ioP.close();
120 
121  if (this->size())
122  {
123  readFields();
124  }
125  }
126  else
127  {
128  if (debug)
129  {
130  Pout<< "Cannot read particle positions file:" << nl
131  << " " << ioP.objectPath() << nl
132  << "Assuming the initial cloud contains 0 particles." << endl;
133  }
134  }
135 
136  // Ask for the tetBasePtIs to trigger all processors to build
137  // them, otherwise, if some processors have no particles then
138  // there is a comms mismatch.
139  polyMesh_.tetBasePtIs();
140 
141  forAllIter(typename Cloud<ParticleType>, *this, pIter)
142  {
143  ParticleType& p = pIter();
144 
145  p.initCellFacePt();
146  }
147 }
148 
149 
150 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
151 
152 template<class ParticleType>
154 (
155  const polyMesh& pMesh,
156  const bool checkClass
157 )
158 :
159  cloud(pMesh),
160  polyMesh_(pMesh),
161  labels_(),
162  nTrackingRescues_(),
163  cellWallFacesPtr_()
164 {
165  checkPatches();
166 
167  initCloud(checkClass);
168 }
169 
170 
171 template<class ParticleType>
173 (
174  const polyMesh& pMesh,
175  const word& cloudName,
176  const bool checkClass
177 )
178 :
179  cloud(pMesh, cloudName),
180  polyMesh_(pMesh),
181  labels_(),
182  nTrackingRescues_(),
183  cellWallFacesPtr_()
184 {
185  checkPatches();
186 
187  initCloud(checkClass);
188 }
189 
190 
191 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
192 
193 template<class ParticleType>
195 (
196  const word& fieldName,
197  const IOobject::readOption r
198 ) const
199 {
200  return IOobject
201  (
202  fieldName,
203  time().timeName(),
204  *this,
205  r,
206  IOobject::NO_WRITE,
207  false
208  );
209 }
210 
211 
212 template<class ParticleType>
213 template<class DataType>
215 (
216  const Cloud<ParticleType>& c,
217  const IOField<DataType>& data
218 ) const
219 {
220  if (data.size() != c.size())
221  {
223  (
224  "void Cloud<ParticleType>::checkFieldIOobject"
225  "(const Cloud<ParticleType>&, const IOField<DataType>&) const"
226  ) << "Size of " << data.name()
227  << " field " << data.size()
228  << " does not match the number of particles " << c.size()
229  << abort(FatalError);
230  }
231 }
232 
233 
234 template<class ParticleType>
235 template<class DataType>
237 (
238  const Cloud<ParticleType>& c,
239  const CompactIOField<Field<DataType>, DataType>& data
240 ) const
241 {
242  if (data.size() != c.size())
243  {
245  (
246  "void Cloud<ParticleType>::checkFieldFieldIOobject"
247  "("
248  "const Cloud<ParticleType>&, "
249  "const CompactIOField<Field<DataType>, DataType>&"
250  ") const"
251  ) << "Size of " << data.name()
252  << " field " << data.size()
253  << " does not match the number of particles " << c.size()
254  << abort(FatalError);
255  }
256 }
257 
258 
259 template<class ParticleType>
261 {}
262 
263 
264 template<class ParticleType>
266 {
267  if (this->size())
268  {
269  ParticleType::writeFields(*this);
270  }
271 }
272 
273 
274 template<class ParticleType>
276 (
280 ) const
281 {
282  writeCloudUniformProperties();
283 
284  if (this->size())
285  {
286  writeFields();
287  return cloud::writeObject(fmt, ver, cmp);
288  }
289  else
290  {
291  return true;
292  }
293 }
294 
295 
296 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
297 
298 template<class ParticleType>
299 Foam::Ostream& Foam::operator<<(Ostream& os, const Cloud<ParticleType>& pc)
300 {
301  pc.writeData(os);
302 
303  // Check state of Ostream
304  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
305 
306  return os;
307 }
308 
309 
310 // ************************************************************************* //
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp) const
Write using given format, version and compression.
Definition: CloudIO.C:276
#define forAllIter(Container, container, iter)
Definition: UList.H:440
A Field of objects of type <T> with automated input and output using a compact storage. Behaves like IOField except when binary output in case it writes a CompactListList.
A class for handling words, derived from string.
Definition: word.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
virtual void writeFields() const
Write the field data for the cloud of particles Dummy at.
Definition: CloudIO.C:265
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
void readFields(const Mesh &mesh, const IOobjectList &objects, PtrList< GeoField > &fields)
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
volScalarField & p
Definition: createFields.H:51
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
#define forAll(list, i)
Definition: UList.H:421
A cloud is a collection of lagrangian particles.
Definition: cloud.H:51
Pre-declare SubField and related Field type.
Definition: Field.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word & name() const
Return name.
Definition: IOobject.H:260
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
void checkFieldIOobject(const Cloud< ParticleType > &c, const IOField< DataType > &data) const
Check lagrangian data field.
Definition: CloudIO.C:215
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
label size() const
Definition: Cloud.H:175
error FatalError
virtual void readFields()
Read the field data for the cloud of particles. Dummy at.
Definition: CloudIO.C:260
void checkFieldFieldIOobject(const Cloud< ParticleType > &c, const CompactIOField< Field< DataType >, DataType > &data) const
Check lagrangian data fieldfield.
Definition: CloudIO.C:237
List< label > labelList
A List of labels.
Definition: labelList.H:56
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:126
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
Base cloud calls templated on particle type.
Definition: Cloud.H:52
Cloud(const polyMesh &mesh, const IDLList< ParticleType > &particles)
Construct from mesh and a list of particles.
Definition: Cloud.C:97
Version number type.
Definition: IOstream.H:96
IOobject fieldIOobject(const word &fieldName, const IOobject::readOption r) const
Helper to construct IOobject for field and current time.
Definition: CloudIO.C:195
word timeName
Definition: getTimeIndex.H:3
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50