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-2017 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 #include "IOdictionary.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 template<class ParticleType>
35 
36 
37 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
38 
39 template<class ParticleType>
41 {
42  IOobject dictObj
43  (
44  cloudPropertiesName,
45  time().timeName(),
46  "uniform"/cloud::prefix/name(),
47  db(),
48  IOobject::MUST_READ_IF_MODIFIED,
49  IOobject::NO_WRITE,
50  false
51  );
52 
53  if (dictObj.typeHeaderOk<IOdictionary>(true))
54  {
55  const IOdictionary uniformPropsDict(dictObj);
56 
57  const word procName("processor" + Foam::name(Pstream::myProcNo()));
58  if (uniformPropsDict.found(procName))
59  {
60  uniformPropsDict.subDict(procName).lookup("particleCount")
61  >> ParticleType::particleCount_;
62  }
63  }
64  else
65  {
66  ParticleType::particleCount_ = 0;
67  }
68 }
69 
70 
71 template<class ParticleType>
73 {
74  IOdictionary uniformPropsDict
75  (
76  IOobject
77  (
78  cloudPropertiesName,
79  time().timeName(),
80  "uniform"/cloud::prefix/name(),
81  db(),
82  IOobject::NO_READ,
83  IOobject::NO_WRITE,
84  false
85  )
86  );
87 
88  labelList np(Pstream::nProcs(), 0);
89  np[Pstream::myProcNo()] = ParticleType::particleCount_;
90 
91  Pstream::listCombineGather(np, maxEqOp<label>());
92  Pstream::listCombineScatter(np);
93 
94  forAll(np, i)
95  {
96  word procName("processor" + Foam::name(i));
97  uniformPropsDict.add(procName, dictionary());
98  uniformPropsDict.subDict(procName).add("particleCount", np[i]);
99  }
100 
101  uniformPropsDict.writeObject
102  (
103  IOstream::ASCII,
104  IOstream::currentVersion,
105  time().writeCompression(),
106  true
107  );
108 }
109 
110 
111 template<class ParticleType>
112 void Foam::Cloud<ParticleType>::initCloud(const bool checkClass)
113 {
114  readCloudUniformProperties();
115 
116  IOPosition<Cloud<ParticleType>> ioP(*this);
117 
118  bool valid = ioP.headerOk();
119  Istream& is = ioP.readStream(checkClass ? typeName : "", valid);
120  if (valid)
121  {
122  ioP.readData(is, *this);
123  ioP.close();
124  }
125 
126  if (!valid && debug)
127  {
128  Pout<< "Cannot read particle positions file:" << nl
129  << " " << ioP.objectPath() << nl
130  << "Assuming the initial cloud contains 0 particles." << endl;
131  }
132 
133  // Ask for the tetBasePtIs to trigger all processors to build
134  // them, otherwise, if some processors have no particles then
135  // there is a comms mismatch.
136  polyMesh_.tetBasePtIs();
137 }
138 
139 
140 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
141 
142 template<class ParticleType>
144 (
145  const polyMesh& pMesh,
146  const word& cloudName,
147  const bool checkClass
148 )
149 :
150  cloud(pMesh, cloudName),
151  polyMesh_(pMesh),
152  labels_(),
153  nTrackingRescues_(),
154  cellWallFacesPtr_()
155 {
156  checkPatches();
157 
158  initCloud(checkClass);
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
163 
164 template<class ParticleType>
166 (
167  const word& fieldName,
168  const IOobject::readOption r
169 ) const
170 {
171  return IOobject
172  (
173  fieldName,
174  time().timeName(),
175  *this,
176  r,
177  IOobject::NO_WRITE,
178  false
179  );
180 }
181 
182 
183 template<class ParticleType>
184 template<class DataType>
186 (
187  const Cloud<ParticleType>& c,
188  const IOField<DataType>& data
189 ) const
190 {
191  if (data.size() != c.size())
192  {
194  << "Size of " << data.name()
195  << " field " << data.size()
196  << " does not match the number of particles " << c.size()
197  << abort(FatalError);
198  }
199 }
200 
201 
202 template<class ParticleType>
203 template<class DataType>
205 (
206  const Cloud<ParticleType>& c,
207  const CompactIOField<Field<DataType>, DataType>& data
208 ) const
209 {
210  if (data.size() != c.size())
211  {
213  << "Size of " << data.name()
214  << " field " << data.size()
215  << " does not match the number of particles " << c.size()
216  << abort(FatalError);
217  }
218 }
219 
220 
221 template<class ParticleType>
223 {
224  ParticleType::writeFields(*this);
225 }
226 
227 
228 template<class ParticleType>
230 (
234  const bool
235 ) const
236 {
237  writeCloudUniformProperties();
238 
239  writeFields();
240  return cloud::writeObject(fmt, ver, cmp, this->size());
241 }
242 
243 
244 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
245 
246 template<class ParticleType>
247 Foam::Ostream& Foam::operator<<(Ostream& os, const Cloud<ParticleType>& pc)
248 {
249  pc.writeData(os);
250 
251  // Check state of Ostream
252  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
253 
254  return os;
255 }
256 
257 
258 // ************************************************************************* //
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool valid) const
Write using given format, version and compression.
Definition: CloudIO.C:230
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
const word & name() const
Return name.
Definition: IOobject.H:291
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
void checkFieldFieldIOobject(const Cloud< ParticleType > &c, const CompactIOField< Field< DataType >, DataType > &data) const
Check lagrangian data fieldfield.
Definition: CloudIO.C:205
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Cloud(const polyMesh &mesh, const word &cloudName, const IDLList< ParticleType > &particles)
Construct from mesh and a list of particles.
Definition: Cloud.C:97
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
label size() const
Definition: Cloud.H:162
readOption
Enumeration defining the read options.
Definition: IOobject.H:107
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:129
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
static const char nl
Definition: Ostream.H:262
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual void writeFields() const
Write the field data for the cloud of particles Dummy at.
Definition: CloudIO.C:222
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
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
void checkFieldIOobject(const Cloud< ParticleType > &c, const IOField< DataType > &data) const
Check lagrangian data field.
Definition: CloudIO.C:186
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
IOobject fieldIOobject(const word &fieldName, const IOobject::readOption r) const
Helper to construct IOobject for field and current time.
Definition: CloudIO.C:166
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50