CloudIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "Cloud.H"
30 #include "Time.H"
31 #include "IOPosition.H"
32 #include "IOdictionary.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 template<class ParticleType>
38 
39 
40 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
41 
42 template<class ParticleType>
44 {
45  IOobject dictObj
46  (
47  cloudPropertiesName,
48  time().timeName(),
49  "uniform"/cloud::prefix/name(),
50  db(),
51  IOobject::MUST_READ_IF_MODIFIED,
52  IOobject::NO_WRITE,
53  false
54  );
55 
56  if (dictObj.typeHeaderOk<IOdictionary>(true))
57  {
58  const IOdictionary uniformPropsDict(dictObj);
59 
60  // Fall back to positions mode if the entry is not present for
61  // backwards compatibility
62  geometryType_ =
63  cloud::geometryTypeNames.lookupOrDefault
64  (
65  "geometry",
66  uniformPropsDict,
67  cloud::geometryType::POSITIONS
68  );
69 
70  const word procName("processor" + Foam::name(Pstream::myProcNo()));
71 
72  const dictionary* dictptr = uniformPropsDict.findDict(procName);
73 
74  if (dictptr)
75  {
76  dictptr->readEntry("particleCount", ParticleType::particleCount_);
77  }
78  }
79  else
80  {
81  ParticleType::particleCount_ = 0;
82  }
83 }
84 
85 
86 template<class ParticleType>
88 {
89  IOdictionary uniformPropsDict
90  (
91  IOobject
92  (
93  cloudPropertiesName,
94  time().timeName(),
95  "uniform"/cloud::prefix/name(),
96  db(),
97  IOobject::NO_READ,
98  IOobject::NO_WRITE,
99  false
100  )
101  );
102 
103  labelList np(Pstream::nProcs(), Zero);
104  np[Pstream::myProcNo()] = ParticleType::particleCount_;
105 
106  Pstream::listCombineGather(np, maxEqOp<label>());
107  Pstream::listCombineScatter(np);
108 
109  uniformPropsDict.add
110  (
111  "geometry",
112  cloud::geometryTypeNames[geometryType_]
113  );
114 
115  forAll(np, i)
116  {
117  word procName("processor" + Foam::name(i));
118  uniformPropsDict.add(procName, dictionary());
119  uniformPropsDict.subDict(procName).add("particleCount", np[i]);
120  }
121 
122  uniformPropsDict.writeObject
123  (
124  IOstream::ASCII,
125  IOstream::currentVersion,
126  time().writeCompression(),
127  true
128  );
129 }
130 
131 
132 template<class ParticleType>
133 void Foam::Cloud<ParticleType>::initCloud(const bool checkClass)
134 {
135  readCloudUniformProperties();
136 
137  IOPosition<Cloud<ParticleType>> ioP(*this, geometryType_);
138 
139  const bool valid = ioP.headerOk();
140  Istream& is = ioP.readStream(checkClass ? typeName : "", valid);
141  if (valid)
142  {
143  ioP.readData(is, *this);
144  ioP.close();
145  }
146 
147  if (!valid && debug)
148  {
149  Pout<< "Cannot read particle positions file:" << nl
150  << " " << ioP.objectPath() << nl
151  << "Assuming the initial cloud contains 0 particles." << endl;
152  }
153 
154  // Always operate in coordinates mode after reading
155  geometryType_ = cloud::geometryType::COORDINATES;
156 
157  // Ask for the tetBasePtIs to trigger all processors to build
158  // them, otherwise, if some processors have no particles then
159  // there is a comms mismatch.
160  polyMesh_.tetBasePtIs();
161 }
162 
163 
164 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
165 
166 template<class ParticleType>
168 (
169  const polyMesh& pMesh,
170  const word& cloudName,
171  const bool checkClass
172 )
173 :
174  cloud(pMesh, cloudName),
175  polyMesh_(pMesh),
176  labels_(),
177  cellWallFacesPtr_(),
178  geometryType_(cloud::geometryType::COORDINATES)
179 {
180  checkPatches();
181 
182  initCloud(checkClass);
183 }
184 
185 
186 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
187 
188 template<class ParticleType>
190 (
191  const word& fieldName,
192  const IOobject::readOption r
193 ) const
194 {
195  return IOobject
196  (
197  fieldName,
198  time().timeName(),
199  *this,
200  r,
201  IOobject::NO_WRITE,
202  false
203  );
204 }
205 
206 
207 template<class ParticleType>
208 template<class DataType>
210 (
211  const Cloud<ParticleType>& c,
212  const IOField<DataType>& data
213 ) const
214 {
215  if (data.size() != c.size())
216  {
218  << "Size of " << data.name()
219  << " field " << data.size()
220  << " does not match the number of particles " << c.size()
221  << abort(FatalError);
222  }
223 }
224 
225 
226 template<class ParticleType>
227 template<class DataType>
229 (
230  const Cloud<ParticleType>& c,
231  const CompactIOField<Field<DataType>, DataType>& data
232 ) const
233 {
234  if (data.size() != c.size())
235  {
237  << "Size of " << data.name()
238  << " field " << data.size()
239  << " does not match the number of particles " << c.size()
240  << abort(FatalError);
241  }
242 }
243 
244 
245 template<class ParticleType>
247 {
249 }
250 
251 
252 template<class ParticleType>
254 (
258  const bool
259 ) const
260 {
261  writeCloudUniformProperties();
262 
263  writeFields();
264  return cloud::writeObject(fmt, ver, cmp, this->size());
265 }
266 
267 
268 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
269 
270 template<class ParticleType>
272 {
273  c.writeData(os);
274 
275  os.check(FUNCTION_NAME);
276  return os;
277 }
278 
279 
280 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
IOPosition.H
Foam::Cloud::writeFields
virtual void writeFields() const
Write the field data for the cloud of particles Dummy at.
Definition: CloudIO.C:246
cloudName
const word cloudName(propsDict.get< word >("cloud"))
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::Cloud::checkFieldIOobject
void checkFieldIOobject(const Cloud< ParticleType > &c, const IOField< DataType > &data) const
Check lagrangian data field.
Definition: CloudIO.C:210
Cloud.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::CompactIOField
A Field of objects of type <T> with automated input and output using a compact storage....
Definition: CompactIOField.H:52
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
Foam::baseIOdictionary::name
const word & name() const
Name function is needed to disambiguate those inherited.
Definition: baseIOdictionary.C:88
Foam::Cloud::fieldIOobject
IOobject fieldIOobject(const word &fieldName, const IOobject::readOption r) const
Helper to construct IOobject for field and current time.
Definition: CloudIO.C:190
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::Cloud::writeObject
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:254
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::writeFields
void writeFields(const fvMesh &mesh, const wordHashSet &selectedFields)
Foam::Cloud::cloudPropertiesName
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:126
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
IOdictionary.H
Time.H
Foam::cloud
A cloud is a registry collection of lagrangian particles.
Definition: cloud.H:57
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::Cloud
Base cloud calls templated on particle type.
Definition: Cloud.H:54
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::Cloud::Cloud
Cloud(const polyMesh &mesh, const word &cloudName, const IDLList< ParticleType > &particles)
Construct from mesh and a list of particles.
Definition: Cloud.C:73
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:118
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:54
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::Cloud::checkFieldFieldIOobject
void checkFieldFieldIOobject(const Cloud< ParticleType > &c, const CompactIOField< Field< DataType >, DataType > &data) const
Check lagrangian data fieldfield.
Definition: CloudIO.C:229