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, 2020 OpenFOAM Foundation
9 Copyright (C) 2017-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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#include "IOobjectList.H"
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37template<class ParticleType>
39
40
41// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
42
43template<class ParticleType>
45{
46 IOobject dictObj
47 (
48 cloudPropertiesName,
49 time().timeName(),
50 "uniform"/cloud::prefix/name(),
51 db(),
52 IOobject::MUST_READ_IF_MODIFIED,
53 IOobject::NO_WRITE,
54 false
55 );
56
57 if (dictObj.typeHeaderOk<IOdictionary>(true))
58 {
59 const IOdictionary uniformPropsDict(dictObj);
60
61 // Fall back to positions mode if the entry is not present for
62 // backwards compatibility
63 geometryType_ =
64 cloud::geometryTypeNames.getOrDefault
65 (
66 "geometry",
67 uniformPropsDict,
68 cloud::geometryType::POSITIONS
69 );
70
71 const word procName("processor" + Foam::name(Pstream::myProcNo()));
72
73 const dictionary* dictptr = uniformPropsDict.findDict(procName);
74
75 if (dictptr)
76 {
77 dictptr->readEntry("particleCount", ParticleType::particleCount_);
78 }
79 }
80 else
81 {
83 }
84}
85
86
87template<class ParticleType>
89{
90 IOdictionary uniformPropsDict
91 (
92 IOobject
93 (
94 cloudPropertiesName,
95 time().timeName(),
96 "uniform"/cloud::prefix/name(),
97 db(),
98 IOobject::NO_READ,
99 IOobject::NO_WRITE,
100 false
101 )
102 );
103
104 labelList np(Pstream::nProcs(), Zero);
105 np[Pstream::myProcNo()] = ParticleType::particleCount_;
106
107 Pstream::listCombineAllGather(np, maxEqOp<label>());
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 IOstreamOption(IOstream::ASCII, time().writeCompression()),
125 true
126 );
127}
128
129
130template<class ParticleType>
131void Foam::Cloud<ParticleType>::initCloud(const bool checkClass)
132{
133 readCloudUniformProperties();
134
135 IOPosition<Cloud<ParticleType>> ioP(*this, geometryType_);
136
137 const bool valid = ioP.headerOk();
138 Istream& is = ioP.readStream(checkClass ? typeName : "", valid);
139 if (valid)
140 {
141 ioP.readData(is, *this);
142 ioP.close();
143 }
144
145 if (!valid && debug)
146 {
147 Pout<< "Cannot read particle positions file:" << nl
148 << " " << ioP.objectPath() << nl
149 << "Assuming the initial cloud contains 0 particles." << endl;
150 }
151
152 // Always operate in coordinates mode after reading
153 geometryType_ = cloud::geometryType::COORDINATES;
154
155 // Ask for the tetBasePtIs to trigger all processors to build
156 // them, otherwise, if some processors have no particles then
157 // there is a comms mismatch.
158 polyMesh_.tetBasePtIs();
159}
160
161
162// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
163
164template<class ParticleType>
166(
167 const polyMesh& pMesh,
168 const word& cloudName,
169 const bool checkClass
170)
171:
172 cloud(pMesh, cloudName),
173 polyMesh_(pMesh),
174 labels_(),
175 cellWallFacesPtr_(),
176 geometryType_(cloud::geometryType::COORDINATES)
177{
178 checkPatches();
179
180 polyMesh_.tetBasePtIs();
181 polyMesh_.oldCellCentres();
182
183 initCloud(checkClass);
184}
185
186
187// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
188
189template<class ParticleType>
191(
192 const word& fieldName,
194) const
195{
196 return IOobject
197 (
198 fieldName,
199 time().timeName(),
200 *this,
201 r,
203 false
204 );
205}
206
207
208template<class ParticleType>
209template<class DataType>
211(
212 const Cloud<ParticleType>& c,
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
227template<class ParticleType>
228template<class DataType>
230(
231 const Cloud<ParticleType>& c,
232 const CompactIOField<Field<DataType>, DataType>& data
233) const
234{
235 if (data.size() != c.size())
236 {
238 << "Size of " << data.name()
239 << " field " << data.size()
240 << " does not match the number of particles " << c.size()
241 << abort(FatalError);
242 }
243}
244
246template<class ParticleType>
247template<class Type>
249(
250 const IOobject& io,
251 const IOobject& ioNew
252) const
253{
255 {
257 auto* fldNewPtr = new IOField<Type>(ioNew, std::move(fld));
258 return fldNewPtr->store();
259 }
260
261 return false;
262}
263
265template<class ParticleType>
267(
269 const wordRes& selectFields,
271) const
272{
273 IOobjectList cloudObjects
274 (
275 *this,
276 time().timeName(),
277 "",
280 false
281 );
282
283 const wordRes::filter pred(selectFields, excludeFields);
284
285 forAllConstIters(cloudObjects, iter)
286 {
287 const IOobject& io = *(iter.val());
288 const word& fldName = io.name();
289
290 if (!pred(fldName))
291 {
292 continue; // reject
293 }
294
295 IOobject ioNew
296 (
297 fldName,
298 time().timeName(),
299 obr,
302 );
303
304 const bool stored
305 (
306 readStoreFile<label>(io, ioNew)
307 || readStoreFile<scalar>(io, ioNew)
308 || readStoreFile<vector>(io, ioNew)
309 || readStoreFile<sphericalTensor>(io, ioNew)
310 || readStoreFile<symmTensor>(io, ioNew)
311 || readStoreFile<tensor>(io, ioNew)
312 );
313
314 if (!stored)
315 {
317 << "Unhandled field:" << fldName
318 << " type:" << io.headerClassName() << endl;
319 }
320 }
321}
322
323
324template<class ParticleType>
326{
327 ParticleType::writeFields(*this);
328}
329
330
331template<class ParticleType>
333(
334 IOstreamOption streamOpt,
335 const bool
336) const
337{
338 writeCloudUniformProperties();
339
340 writeFields();
341 return cloud::writeObject(streamOpt, this->size());
342}
343
344
345// * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
346
347template<class ParticleType>
349{
350 c.writeData(os);
351
353 return os;
354}
355
356
357// ************************************************************************* //
Info<< nl<< "Wrote faMesh in vtk format: "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.mesh().time().globalPath()/"finiteArea-edges"));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
Base cloud calls templated on particle type.
Definition: Cloud.H:68
bool readStoreFile(const IOobject &io, const IOobject &ioNew) const
Helper function to store a cloud field on its registry.
Definition: CloudIO.C:249
static word cloudPropertiesName
Name of cloud properties dictionary.
Definition: Cloud.H:127
void readFromFiles(objectRegistry &obr, const wordRes &selectFields, const wordRes &excludeFields=wordRes::null()) const
Read from files into objectRegistry.
Definition: CloudIO.C:267
void checkFieldIOobject(const Cloud< ParticleType > &c, const IOField< DataType > &data) const
Check lagrangian data field.
Definition: CloudIO.C:211
virtual void writeFields() const
Write the field data for the cloud of particles Dummy at.
Definition: CloudIO.C:325
IOobject fieldIOobject(const word &fieldName, const IOobject::readOption r) const
Helper to construct IOobject for field and current time.
Definition: CloudIO.C:191
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
Definition: CloudIO.C:333
void checkFieldFieldIOobject(const Cloud< ParticleType > &c, const CompactIOField< Field< DataType >, DataType > &data) const
Check lagrangian data fieldfield.
Definition: CloudIO.C:230
A Field of objects of type <T> with automated input and output using a compact storage....
Generic templated field type.
Definition: Field.H:82
A primitive field of type <T> with automated input and output.
Definition: IOField.H:58
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:59
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
readOption
Enumeration defining the read options.
Definition: IOobject.H:177
The IOstreamOption is a simple container for options an IOstream can normally have.
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
const word & name() const
A cloud is a registry collection of lagrangian particles.
Definition: cloud.H:60
geometryType
Cloud geometry type (internal or IO representations)
Definition: cloud.H:75
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
Registry of regIOobjects.
static label particleCount_
Cumulative particle counter - used to provide unique ID.
Definition: particle.H:375
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
const labelIOList & tetBasePtIs() const
Return the tetBasePtIs.
Definition: polyMesh.C:906
virtual const pointField & oldCellCentres() const
Return old cellCentres (mesh motion)
Definition: polyMesh.C:1155
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:54
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
word timeName
Definition: getTimeIndex.H:3
#define DebugInfo
Report an information message using Foam::Info.
#define FUNCTION_NAME
List< label > labelList
A List of labels.
Definition: List.H:66
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
void writeFields(const fvMesh &mesh, const wordHashSet &selectedFields, const bool writeFaceFields)
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278
Functor wrapper of allow/deny lists of wordRe for filtering.
Definition: wordRes.H:174
wordRes excludeFields
const word cloudName(propsDict.get< word >("cloud"))