All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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().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.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().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  patchNbrProc_(patchNbrProc(pMesh)),
153  patchNbrProcPatch_(patchNbrProcPatch(pMesh)),
154  patchNonConformalCyclicPatches_(patchNonConformalCyclicPatches(pMesh)),
155  globalPositionsPtr_()
156 {
157  checkPatches();
158 
159  polyMesh_.tetBasePtIs();
160  polyMesh_.oldCellCentres();
161 
162  initCloud(checkClass);
163 }
164 
165 
166 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
167 
168 template<class ParticleType>
170 (
171  const word& fieldName,
172  const IOobject::readOption r
173 ) const
174 {
175  return IOobject
176  (
177  fieldName,
178  time().timeName(),
179  *this,
180  r,
181  IOobject::NO_WRITE,
182  false
183  );
184 }
185 
186 
187 template<class ParticleType>
188 template<class DataType>
190 (
191  const Cloud<ParticleType>& c,
192  const IOField<DataType>& data
193 ) const
194 {
195  if (data.size() != c.size())
196  {
198  << "Size of " << data.name()
199  << " field " << data.size()
200  << " does not match the number of particles " << c.size()
201  << abort(FatalError);
202  }
203 }
204 
205 
206 template<class ParticleType>
207 template<class DataType>
209 (
210  const Cloud<ParticleType>& c,
211  const CompactIOField<Field<DataType>, DataType>& data
212 ) const
213 {
214  if (data.size() != c.size())
215  {
217  << "Size of " << data.name()
218  << " field " << data.size()
219  << " does not match the number of particles " << c.size()
220  << abort(FatalError);
221  }
222 }
223 
224 
225 template<class ParticleType>
227 {
228  ParticleType::writeFields(*this);
229 }
230 
231 
232 template<class ParticleType>
234 (
238  const bool
239 ) const
240 {
241  writeCloudUniformProperties();
242 
243  writeFields();
244  return cloud::writeObject(fmt, ver, cmp, this->size());
245 }
246 
247 
248 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
249 
250 template<class ParticleType>
251 Foam::Ostream& Foam::operator<<(Ostream& os, const Cloud<ParticleType>& pc)
252 {
253  pc.writeData(os);
254 
255  // Check state of Ostream
256  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
257 
258  return os;
259 }
260 
261 
262 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
const word & name() const
Return name.
Definition: IOobject.H:315
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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:209
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
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:234
Cloud(const polyMesh &mesh, const word &cloudName, const IDLList< ParticleType > &particles)
Construct from mesh and a list of particles.
Definition: Cloud.C:188
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label size() const
Return the number of particles in the cloud.
Definition: Cloud.H:190
readOption
Enumeration defining the read options.
Definition: IOobject.H:116
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:137
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:54
static const char nl
Definition: Ostream.H:260
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:226
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:190
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:76
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
IOobject fieldIOobject(const word &fieldName, const IOobject::readOption r) const
Helper to construct IOobject for field and current time.
Definition: CloudIO.C:170
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50