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-2016 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  << "Size of " << data.name()
224  << " field " << data.size()
225  << " does not match the number of particles " << c.size()
226  << abort(FatalError);
227  }
228 }
229 
230 
231 template<class ParticleType>
232 template<class DataType>
234 (
235  const Cloud<ParticleType>& c,
236  const CompactIOField<Field<DataType>, DataType>& data
237 ) const
238 {
239  if (data.size() != c.size())
240  {
242  << "Size of " << data.name()
243  << " field " << data.size()
244  << " does not match the number of particles " << c.size()
245  << abort(FatalError);
246  }
247 }
248 
249 
250 template<class ParticleType>
252 {}
253 
254 
255 template<class ParticleType>
257 {
258  if (this->size())
259  {
260  ParticleType::writeFields(*this);
261  }
262 }
263 
264 
265 template<class ParticleType>
267 (
271 ) const
272 {
273  writeCloudUniformProperties();
274 
275  if (this->size())
276  {
277  writeFields();
278  return cloud::writeObject(fmt, ver, cmp);
279  }
280  else
281  {
282  return true;
283  }
284 }
285 
286 
287 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
288 
289 template<class ParticleType>
290 Foam::Ostream& Foam::operator<<(Ostream& os, const Cloud<ParticleType>& pc)
291 {
292  pc.writeData(os);
293 
294  // Check state of Ostream
295  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
296 
297  return os;
298 }
299 
300 
301 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
void checkFieldIOobject(const Cloud< ParticleType > &c, const IOField< DataType > &data) const
Check lagrangian data field.
Definition: CloudIO.C:215
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
void checkFieldFieldIOobject(const Cloud< ParticleType > &c, const CompactIOField< Field< DataType >, DataType > &data) const
Check lagrangian data fieldfield.
Definition: CloudIO.C:234
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:453
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:126
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 writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp) const
Write using given format, version and compression.
Definition: CloudIO.C:267
Cloud(const polyMesh &mesh, const IDLList< ParticleType > &particles)
Construct from mesh and a list of particles.
Definition: Cloud.C:97
Pre-declare SubField and related Field type.
Definition: Field.H:57
A class for handling words, derived from string.
Definition: word.H:59
A cloud is a collection of lagrangian particles.
Definition: cloud.H:51
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
word timeName
Definition: getTimeIndex.H:3
List< label > labelList
A List of labels.
Definition: labelList.H:56
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Base cloud calls templated on particle type.
Definition: Cloud.H:52
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
static const char nl
Definition: Ostream.H:262
label size() const
Definition: Cloud.H:175
virtual void writeFields() const
Write the field data for the cloud of particles Dummy at.
Definition: CloudIO.C:256
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual void readFields()
Read the field data for the cloud of particles. Dummy at.
Definition: CloudIO.C:251
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.
Version number type.
Definition: IOstream.H:96
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
IOobject fieldIOobject(const word &fieldName, const IOobject::readOption r) const
Helper to construct IOobject for field and current time.
Definition: CloudIO.C:195
volScalarField & p
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50
const word & name() const
Return name.
Definition: IOobject.H:260