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-2022 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 "timeIOdictionary.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 template<class ParticleType>
35 
36 
37 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
38 
39 template<class ParticleType>
41 {
42  typeIOobject<timeIOdictionary> dictObj
43  (
44  cloudPropertiesName,
45  time().name(),
46  "uniform"/cloud::prefix/name(),
47  db(),
48  IOobject::MUST_READ_IF_MODIFIED,
49  IOobject::NO_WRITE,
50  false
51  );
52 
53  if (dictObj.headerOk())
54  {
55  const timeIOdictionary 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  timeIOdictionary uniformPropsDict
75  (
76  IOobject
77  (
78  cloudPropertiesName,
79  time().name(),
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  pMesh_.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  pMesh_(pMesh),
152  patchNbrProc_(patchNbrProc(pMesh)),
153  patchNbrProcPatch_(patchNbrProcPatch(pMesh)),
154  patchNonConformalCyclicPatches_(patchNonConformalCyclicPatches(pMesh)),
155  globalPositionsPtr_()
156 {
157  pMesh_.tetBasePtIs();
158  pMesh_.oldCellCentres();
159 
160  initCloud(checkClass);
161 }
162 
163 
164 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
165 
166 template<class ParticleType>
168 (
169  const word& fieldName,
170  const IOobject::readOption r
171 ) const
172 {
173  return IOobject
174  (
175  fieldName,
176  time().name(),
177  *this,
178  r,
179  IOobject::NO_WRITE,
180  false
181  );
182 }
183 
184 
185 template<class ParticleType>
186 template<class DataType>
188 (
189  const Cloud<ParticleType>& c,
190  const IOField<DataType>& data
191 ) const
192 {
193  if (data.size() != c.size())
194  {
196  << "Size of " << data.name()
197  << " field " << data.size()
198  << " does not match the number of particles " << c.size()
199  << abort(FatalError);
200  }
201 }
202 
203 
204 template<class ParticleType>
205 template<class DataType>
207 (
208  const Cloud<ParticleType>& c,
210 ) const
211 {
212  if (data.size() != c.size())
213  {
215  << "Size of " << data.name()
216  << " field " << data.size()
217  << " does not match the number of particles " << c.size()
218  << abort(FatalError);
219  }
220 }
221 
222 
223 template<class ParticleType>
225 {
226  ParticleType::writeFields(*this);
227 }
228 
229 
230 template<class ParticleType>
232 (
236  const bool
237 ) const
238 {
239  writeCloudUniformProperties();
240 
241  writeFields();
242  return cloud::writeObject(fmt, ver, cmp, this->size());
243 }
244 
245 
246 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
247 
248 template<class ParticleType>
250 {
251  pc.writeData(os);
252 
253  // Check state of Ostream
254  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
255 
256  return os;
257 }
258 
259 
260 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Base cloud calls templated on particle type.
Definition: Cloud.H:74
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:137
void checkFieldIOobject(const Cloud< ParticleType > &c, const IOField< DataType > &data) const
Check lagrangian data field.
Definition: CloudIO.C:188
virtual void writeFields() const
Write the field data for the cloud of particles Dummy at.
Definition: CloudIO.C:224
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:232
IOobject fieldIOobject(const word &fieldName, const IOobject::readOption r) const
Helper to construct IOobject for field and current time.
Definition: CloudIO.C:168
Cloud(const polyMesh &mesh, const word &cloudName, const IDLList< ParticleType > &particles)
Construct from mesh and a list of particles.
Definition: Cloud.C:187
void checkFieldFieldIOobject(const Cloud< ParticleType > &c, const CompactIOField< Field< DataType >> &data) const
Check lagrangian data fieldfield.
Definition: CloudIO.C:207
A Field of objects of type <Type> with automated input and output using a compact storage....
Pre-declare SubField and related Field type.
Definition: Field.H:82
A primitive field of type <Type> with automated input and output.
Definition: IOField.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
readOption
Enumeration defining the read options.
Definition: IOobject.H:117
const word & name() const
Return name.
Definition: IOobject.H:310
Version number type.
Definition: IOstream.H:97
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A cloud is a collection of lagrangian particles.
Definition: cloud.H:55
Database for solution and other reduced data.
Definition: data.H:54
virtual bool writeData(Ostream &) const
writeData function required by regIOobject but not used
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
bool valid(const PtrList< ModelType > &l)
const dimensionedScalar c
Speed of light in a vacuum.
List< label > labelList
A List of labels.
Definition: labelList.H:56
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
error FatalError
Ostream & operator<<(Ostream &, const ensightPart &)
static const char nl
Definition: Ostream.H:260
const word cloudName(propsDict.lookup("cloudName"))