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-2025 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  "cloudProperties"
37 );
38 
39 
40 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
41 
42 template<class ParticleType>
44 {
45  typeIOobject<timeIOdictionary> dictObj
46  (
47  cloudPropertiesName,
48  time().name(),
49  "uniform"/cloud::prefix/name(),
50  db(),
51  IOobject::MUST_READ_IF_MODIFIED,
52  IOobject::NO_WRITE,
53  false
54  );
55 
56  if (dictObj.headerOk())
57  {
58  const timeIOdictionary uniformPropsDict(dictObj);
59 
60  const word procName("processor" + Foam::name(Pstream::myProcNo()));
61  if (uniformPropsDict.found(procName))
62  {
63  uniformPropsDict.subDict(procName).lookup("particleCount")
64  >> ParticleType::particleCount;
65  }
66  }
67  else
68  {
69  ParticleType::particleCount = 0;
70  }
71 }
72 
73 
74 template<class ParticleType>
76 {
77  timeIOdictionary uniformPropsDict
78  (
79  IOobject
80  (
81  cloudPropertiesName,
82  time().name(),
83  "uniform"/cloud::prefix/name(),
84  db(),
85  IOobject::NO_READ,
86  IOobject::NO_WRITE,
87  false
88  )
89  );
90 
91  labelList np(Pstream::nProcs(), 0);
92  np[Pstream::myProcNo()] = ParticleType::particleCount;
93 
94  Pstream::listCombineGather(np, maxEqOp<label>());
95  Pstream::listCombineScatter(np);
96 
97  forAll(np, i)
98  {
99  word procName("processor" + Foam::name(i));
100  uniformPropsDict.add(procName, dictionary());
101  uniformPropsDict.subDict(procName).add("particleCount", np[i]);
102  }
103 
104  uniformPropsDict.writeObject
105  (
106  IOstream::ASCII,
107  IOstream::currentVersion,
108  time().writeCompression(),
109  true
110  );
111 }
112 
113 
114 template<class ParticleType>
115 void Foam::lagrangian::Cloud<ParticleType>::initCloud(const bool checkClass)
116 {
117  readCloudUniformProperties();
118 
119  IOPosition<Cloud<ParticleType>> ioP(*this);
120 
121  bool valid = ioP.headerOk();
122  Istream& is = ioP.readStream(checkClass ? typeName : "", valid);
123  if (valid)
124  {
125  ioP.readData(is, *this);
126  ioP.close();
127  }
128 
129  if (!valid && debug)
130  {
131  Pout<< "Cannot read particle positions file:" << nl
132  << " " << ioP.objectPath() << nl
133  << "Assuming the initial cloud contains 0 particles." << endl;
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  pMesh_.tetBasePtIs();
140 }
141 
142 
143 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
144 
145 template<class ParticleType>
147 (
148  const polyMesh& pMesh,
149  const word& cloudName,
150  const bool checkClass
151 )
152 :
153  cloud(pMesh, cloudName),
154  pMesh_(pMesh),
155  patchNbrProc_(patchNbrProc(pMesh)),
156  patchNbrProcPatch_(patchNbrProcPatch(pMesh)),
157  patchNonConformalCyclicPatches_(patchNonConformalCyclicPatches(pMesh)),
158  globalPositionsPtr_()
159 {
160  // See comments in the other constructor
161  pMesh_.tetBasePtIs();
162  if (!ParticleType::instantaneous) pMesh_.oldCellCentres();
163 
164  initCloud(checkClass);
165 }
166 
167 
168 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 
170 template<class ParticleType>
172 (
173  const word& fieldName,
174  const IOobject::readOption r
175 ) const
176 {
177  return IOobject
178  (
179  fieldName,
180  time().name(),
181  *this,
182  r,
183  IOobject::NO_WRITE,
184  false
185  );
186 }
187 
188 
189 template<class ParticleType>
190 template<class DataType>
192 (
193  const Cloud<ParticleType>& c,
194  const IOField<DataType>& data
195 ) const
196 {
197  if (data.size() != c.size())
198  {
200  << "Size of " << data.name()
201  << " field " << data.size()
202  << " does not match the number of particles " << c.size()
203  << abort(FatalError);
204  }
205 }
206 
207 
208 template<class ParticleType>
209 template<class DataType>
211 (
212  const Cloud<ParticleType>& c,
213  const CompactIOField<Field<DataType>>& data
214 ) const
215 {
216  if (data.size() != c.size())
217  {
219  << "Size of " << data.name()
220  << " field " << data.size()
221  << " does not match the number of particles " << c.size()
222  << abort(FatalError);
223  }
224 }
225 
226 
227 template<class ParticleType>
229 {
230  ParticleType::writeFields(*this);
231 }
232 
233 
234 template<class ParticleType>
236 (
240  const bool
241 ) const
242 {
243  writeCloudUniformProperties();
244 
245  writeFields();
246  return cloud::writeObject(fmt, ver, cmp, this->size());
247 }
248 
249 
250 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
251 
252 template<class ParticleType>
253 Foam::Ostream& Foam::operator<<
254 (
255  Ostream& os,
257 )
258 {
259  pc.writeData(os);
260 
261  // Check state of Ostream
262  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
263 
264  return os;
265 }
266 
267 
268 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
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:83
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:307
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:138
void checkFieldIOobject(const Cloud< ParticleType > &c, const IOField< DataType > &data) const
Check lagrangian data field.
Definition: CloudIO.C:192
virtual void writeFields() const
Write the field data for the cloud of particles Dummy at.
Definition: CloudIO.C:228
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:236
IOobject fieldIOobject(const word &fieldName, const IOobject::readOption r) const
Helper to construct IOobject for field and current time.
Definition: CloudIO.C:172
Cloud(const polyMesh &mesh, const word &cloudName, const IDLList< ParticleType > &particles)
Construct from mesh and a list of particles.
Definition: Cloud.C:189
void checkFieldFieldIOobject(const Cloud< ParticleType > &c, const CompactIOField< Field< DataType >> &data) const
Check lagrangian data fieldfield.
Definition: CloudIO.C:211
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:334
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:258
errorManip< error > abort(error &err)
Definition: errorManip.H:131
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
error FatalError
static const char nl
Definition: Ostream.H:267
const word cloudName(propsDict.lookup("cloudName"))