CloudIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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  globalPositionsPtr_()
153 {
154  checkPatches();
155 
156  initCloud(checkClass);
157 }
158 
159 
160 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
161 
162 template<class ParticleType>
164 (
165  const word& fieldName,
166  const IOobject::readOption r
167 ) const
168 {
169  return IOobject
170  (
171  fieldName,
172  time().timeName(),
173  *this,
174  r,
175  IOobject::NO_WRITE,
176  false
177  );
178 }
179 
180 
181 template<class ParticleType>
182 template<class DataType>
184 (
185  const Cloud<ParticleType>& c,
186  const IOField<DataType>& data
187 ) const
188 {
189  if (data.size() != c.size())
190  {
192  << "Size of " << data.name()
193  << " field " << data.size()
194  << " does not match the number of particles " << c.size()
195  << abort(FatalError);
196  }
197 }
198 
199 
200 template<class ParticleType>
201 template<class DataType>
203 (
204  const Cloud<ParticleType>& c,
205  const CompactIOField<Field<DataType>, DataType>& data
206 ) const
207 {
208  if (data.size() != c.size())
209  {
211  << "Size of " << data.name()
212  << " field " << data.size()
213  << " does not match the number of particles " << c.size()
214  << abort(FatalError);
215  }
216 }
217 
218 
219 template<class ParticleType>
221 {
222  ParticleType::writeFields(*this);
223 }
224 
225 
226 template<class ParticleType>
228 (
232  const bool
233 ) const
234 {
235  writeCloudUniformProperties();
236 
237  writeFields();
238  return cloud::writeObject(fmt, ver, cmp, this->size());
239 }
240 
241 
242 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
243 
244 template<class ParticleType>
245 Foam::Ostream& Foam::operator<<(Ostream& os, const Cloud<ParticleType>& pc)
246 {
247  pc.writeData(os);
248 
249  // Check state of Ostream
250  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
251 
252  return os;
253 }
254 
255 
256 // ************************************************************************* //
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:228
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
const word & name() const
Return name.
Definition: IOobject.H:297
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:203
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:68
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
label size() const
Return the number of particles in the cloud.
Definition: Cloud.H:151
readOption
Enumeration defining the read options.
Definition: IOobject.H:107
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:117
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:265
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:220
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:184
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:164
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50