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-2019 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  polyMesh_.tetBasePtIs();
157  polyMesh_.oldCellCentres();
158 
159  initCloud(checkClass);
160 }
161 
162 
163 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
164 
165 template<class ParticleType>
167 (
168  const word& fieldName,
169  const IOobject::readOption r
170 ) const
171 {
172  return IOobject
173  (
174  fieldName,
175  time().timeName(),
176  *this,
177  r,
178  IOobject::NO_WRITE,
179  false
180  );
181 }
182 
183 
184 template<class ParticleType>
185 template<class DataType>
187 (
188  const Cloud<ParticleType>& c,
189  const IOField<DataType>& data
190 ) const
191 {
192  if (data.size() != c.size())
193  {
195  << "Size of " << data.name()
196  << " field " << data.size()
197  << " does not match the number of particles " << c.size()
198  << abort(FatalError);
199  }
200 }
201 
202 
203 template<class ParticleType>
204 template<class DataType>
206 (
207  const Cloud<ParticleType>& c,
208  const CompactIOField<Field<DataType>, DataType>& data
209 ) const
210 {
211  if (data.size() != c.size())
212  {
214  << "Size of " << data.name()
215  << " field " << data.size()
216  << " does not match the number of particles " << c.size()
217  << abort(FatalError);
218  }
219 }
220 
221 
222 template<class ParticleType>
224 {
225  ParticleType::writeFields(*this);
226 }
227 
228 
229 template<class ParticleType>
231 (
235  const bool
236 ) const
237 {
238  writeCloudUniformProperties();
239 
240  writeFields();
241  return cloud::writeObject(fmt, ver, cmp, this->size());
242 }
243 
244 
245 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
246 
247 template<class ParticleType>
248 Foam::Ostream& Foam::operator<<(Ostream& os, const Cloud<ParticleType>& pc)
249 {
250  pc.writeData(os);
251 
252  // Check state of Ostream
253  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
254 
255  return os;
256 }
257 
258 
259 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
const word & name() const
Return name.
Definition: IOobject.H:295
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:206
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write=true) const
Write using given format, version and compression.
Definition: CloudIO.C:231
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:56
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:223
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:187
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:167
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50