ReadFieldsTemplates.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-2014 OpenFOAM Foundation
9  Copyright (C) 2018-2023 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 "ReadFields.H"
30 #include "HashSet.H"
31 #include "IOobjectList.H"
32 
33 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
34 
35 template<class Type, template<class> class PatchField, class GeoMesh>
37 (
38  const typename GeoMesh::Mesh& mesh,
39  const IOobjectList& objects,
40  PtrList<GeometricField<Type, PatchField, GeoMesh>>& fields,
41  const bool syncPar,
42  const bool readOldTime
43 )
44 {
45  typedef GeometricField<Type, PatchField, GeoMesh> GeoField;
46 
47  // Names of GeoField objects, sorted order.
48  const wordList fieldNames(objects.sortedNames<GeoField>(syncPar));
49 
50  // Construct the fields - reading in consistent (master) order.
51  fields.resize_null(fieldNames.size());
52 
53  label nFields = 0;
54 
55  for (const word& fieldName : fieldNames)
56  {
57  if (!nFields)
58  {
59  Info<< "Reading " << GeoField::typeName << ':';
60  }
61  Info<< ' ' << fieldName;
62 
63  const IOobject& io = *objects[fieldName];
64 
65  fields.set
66  (
67  nFields++,
68  new GeoField
69  (
70  IOobject
71  (
72  io.name(),
73  io.instance(),
74  io.local(),
75  io.db(),
76  IOobjectOption::MUST_READ,
77  IOobjectOption::AUTO_WRITE,
78  io.registerObject()
79  ),
80  mesh,
81  readOldTime
82  )
83  );
84  }
85 
86  if (nFields) Info<< endl;
87 
88  return fieldNames;
89 }
90 
91 
92 template<class GeoField, class Mesh>
94 (
95  const Mesh& mesh,
96  const IOobjectList& objects,
97  PtrList<GeoField>& fields,
98  const bool syncPar
99 )
100 {
101  // Names of GeoField objects, sorted order.
102  const wordList fieldNames(objects.sortedNames<GeoField>(syncPar));
103 
104  // Construct the fields - reading in consistent (master) order.
105  fields.resize_null(fieldNames.size());
106 
107  label nFields = 0;
108 
109  for (const word& fieldName : fieldNames)
110  {
111  if (!nFields)
112  {
113  Info<< "Reading " << GeoField::typeName << ':';
114  }
115  Info<< ' ' << fieldName;
116 
117  const IOobject& io = *objects[fieldName];
118 
119  fields.set
120  (
121  nFields++,
122  new GeoField
123  (
124  IOobject
125  (
126  io.name(),
127  io.instance(),
128  io.local(),
129  io.db(),
130  IOobjectOption::MUST_READ,
131  IOobjectOption::AUTO_WRITE,
132  io.registerObject()
133  ),
134  mesh
135  )
136  );
137  }
138 
139  if (nFields) Info<< endl;
140 
141  return fieldNames;
142 }
143 
144 
145 template<class GeoField>
147 (
148  const IOobjectList& objects,
149  PtrList<GeoField>& fields,
150  const bool syncPar
151 )
152 {
153  // Names of GeoField objects, sorted order.
154  const wordList fieldNames(objects.sortedNames<GeoField>(syncPar));
155 
156  // Construct the fields - reading in consistent (master) order.
157  fields.resize_null(fieldNames.size());
158 
159  label nFields = 0;
160 
161  for (const word& fieldName : fieldNames)
162  {
163  if (!nFields)
164  {
165  Info<< "Reading " << GeoField::typeName << ':';
166  }
167  Info<< ' ' << fieldName;
168 
169  const IOobject& io = *objects[fieldName];
170 
171  fields.set
172  (
173  nFields++,
174  new GeoField
175  (
176  IOobject
177  (
178  io.name(),
179  io.instance(),
180  io.local(),
181  io.db(),
182  IOobjectOption::MUST_READ,
183  IOobjectOption::AUTO_WRITE,
184  io.registerObject()
185  )
186  )
187  );
188  }
189 
190  if (nFields) Info<< endl;
191 
192  return fieldNames;
193 }
194 
195 
196 template<class GeoField>
197 void Foam::ReadFields
198 (
199  const word& fieldName,
200  const typename GeoField::Mesh& mesh,
201  const wordList& timeNames,
202  objectRegistry& fieldsCache
203 )
204 {
205  // Unload times that are no longer used
206  {
207  wordHashSet unusedTimes(fieldsCache.toc());
208  unusedTimes.erase(timeNames);
209 
210  //Info<< "Unloading times " << unusedTimes << endl;
211 
212  for (const word& timeName : unusedTimes)
213  {
214  objectRegistry& timeCache =
215  fieldsCache.lookupObjectRef<objectRegistry>(timeName);
216 
217  fieldsCache.checkOut(timeCache);
218  }
219  }
220 
221 
222  // Load any new fields
223  for (const word& timeName : timeNames)
224  {
225  // Create if not found
226  if (!fieldsCache.found(timeName))
227  {
228  //Info<< "Creating registry for time " << timeName << endl;
229 
230  // Create objectRegistry if not found
231  objectRegistry* timeCachePtr = new objectRegistry
232  (
233  IOobject
234  (
235  timeName,
236  timeName,
237  fieldsCache,
238  IOobjectOption::NO_READ,
239  IOobjectOption::NO_WRITE,
240  IOobjectOption::REGISTER
241  )
242  );
243  timeCachePtr->store();
244  }
245 
246  // Obtain cache for current time
247  const objectRegistry& timeCache =
248  fieldsCache.lookupObject<objectRegistry>(timeName);
249 
250  // Store field if not found
251  if (!timeCache.found(fieldName))
252  {
253  //Info<< "Loading field " << fieldName
254  // << " for time " << timeName << endl;
255 
256  GeoField loadedFld
257  (
258  IOobject
259  (
260  fieldName,
261  timeName,
262  mesh.thisDb(),
263  IOobjectOption::MUST_READ,
264  IOobjectOption::NO_WRITE,
265  IOobjectOption::NO_REGISTER
266  ),
267  mesh
268  );
269 
270  // Transfer to timeCache (new objectRegistry and store flag)
271  GeoField* fldPtr = new GeoField
272  (
273  IOobject
274  (
275  fieldName,
276  timeName,
277  timeCache,
278  IOobjectOption::NO_READ,
279  IOobjectOption::NO_WRITE,
280  IOobjectOption::REGISTER
281  ),
282  loadedFld
283  );
284  fldPtr->store();
285  }
286  }
287 }
288 
289 
290 template<class GeoField>
291 void Foam::ReadFields
292 (
293  const word& fieldName,
294  const typename GeoField::Mesh& mesh,
295  const wordList& timeNames,
296  const word& registryName
297 )
298 {
299  ReadFields<GeoField>
300  (
301  fieldName,
302  mesh,
303  timeNames,
304  const_cast<objectRegistry&>
305  (
306  mesh.thisDb().subRegistry(registryName, true)
307  )
308  );
309 }
310 
311 
312 template<class GeoFieldType, class NameMatchPredicate>
313 void Foam::readFields
314 (
315  const typename GeoFieldType::Mesh& mesh,
316  const IOobjectList& objects,
317  const NameMatchPredicate& selectedFields,
318  DynamicList<regIOobject*>& storedObjects
319 )
320 {
321  // GeoField objects, sorted order. Not synchronised.
322  const UPtrList<const IOobject> fieldObjects
323  (
324  objects.csorted<GeoFieldType>(selectedFields)
325  );
326 
327 
328  // pre-extend reserve
329  storedObjects.reserve(storedObjects.size() + fieldObjects.size());
330 
331  label nFields = 0;
332 
333  for (const IOobject& io : fieldObjects)
334  {
335  if (!nFields)
336  {
337  Info<< " " << GeoFieldType::typeName << ':';
338  }
339  Info<< ' ' << io.name();
340 
341  GeoFieldType* fieldPtr = new GeoFieldType
342  (
343  IOobject
344  (
345  io.name(),
346  io.instance(),
347  io.local(),
348  io.db(),
349  IOobjectOption::MUST_READ,
350  IOobjectOption::NO_WRITE,
351  IOobjectOption::REGISTER
352  ),
353  mesh
354  );
355  fieldPtr->store();
356  storedObjects.push_back(fieldPtr);
357 
358  ++nFields;
359  }
361  if (nFields) Info<< endl;
362 }
363 
364 
365 template<class UniformFieldType, class NameMatchPredicate>
367 (
368  const IOobjectList& objects,
369  const NameMatchPredicate& selectedFields,
370  DynamicList<regIOobject*>& storedObjects,
371  const bool syncPar
372 )
373 {
374  // UniformField objects, sorted order, synchronised.
375  const UPtrList<const IOobject> fieldObjects
376  (
377  objects.csorted<UniformFieldType>(selectedFields, syncPar)
378  );
379 
380  // pre-extend reserve
381  storedObjects.reserve(storedObjects.size() + fieldObjects.size());
382 
383  label nFields = 0;
384 
385  for (const IOobject& io : fieldObjects)
386  {
387  if (!nFields)
388  {
389  Info<< " " << UniformFieldType::typeName << ':';
390  }
391  Info<< ' ' << io.name();
392 
393  UniformFieldType* fieldPtr = new UniformFieldType
394  (
395  IOobject
396  (
397  io.name(),
398  io.instance(),
399  io.local(),
400  io.db(),
401  IOobjectOption::MUST_READ,
402  IOobjectOption::NO_WRITE,
403  IOobjectOption::REGISTER
404  )
405  );
406  fieldPtr->store();
407  storedObjects.push_back(fieldPtr);
408 
409  ++nFields;
410  }
412  if (nFields) Info<< endl;
413 }
414 
415 
416 template<class GeoFieldType, class NameMatchPredicate>
417 void Foam::readFields
418 (
419  const typename GeoFieldType::Mesh& mesh,
420  const IOobjectList& objects,
421  const NameMatchPredicate& selectedFields,
422  LIFOStack<regIOobject*>& storedObjects
423 )
424 {
425  DynamicList<regIOobject*> newObjects;
426 
427  readFields<GeoFieldType, NameMatchPredicate>
428  (
429  mesh,
430  objects,
431  selectedFields,
432  newObjects
433  );
434 
435  // Transcribe from list to stack
436  for (regIOobject* fieldPtr : newObjects)
437  {
438  storedObjects.push(fieldPtr);
439  }
440 }
441 
442 
443 template<class UniformFieldType, class NameMatchPredicate>
445 (
446  const IOobjectList& objects,
447  const NameMatchPredicate& selectedFields,
448  LIFOStack<regIOobject*>& storedObjects,
449  const bool syncPar
450 )
451 {
452  DynamicList<regIOobject*> newObjects;
453 
454  readUniformFields<UniformFieldType, NameMatchPredicate>
455  (
456  objects,
457  selectedFields,
458  newObjects,
459  syncPar
460  );
461 
462  // Transcribe from list to stack
463  for (regIOobject* fieldPtr : newObjects)
464  {
465  storedObjects.push(fieldPtr);
466  }
467 }
468 
469 
470 // ************************************************************************* //
wordList ReadFields(const typename GeoMesh::Mesh &mesh, const IOobjectList &objects, PtrList< GeometricField< Type, PatchField, GeoMesh >> &fields, const bool syncPar=true, const bool readOldTime=false)
Read Geometric fields of templated type.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Field reading functions for post-processing utilities.
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97
word timeName
Definition: getTimeIndex.H:3
dynamicFvMesh & mesh
void readFields(const typename GeoFieldType::Mesh &mesh, const IOobjectList &objects, const NameMatchPredicate &selectedFields, DynamicList< regIOobject *> &storedObjects)
Read the selected GeometricFields of the templated type and store on the objectRegistry.
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:73
void readUniformFields(const IOobjectList &objects, const NameMatchPredicate &selectedFields, DynamicList< regIOobject *> &storedObjects, const bool syncPar=true)
Read the selected UniformDimensionedFields of the templated type and store on the objectRegistry...
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:472
List< word > wordList
List of word.
Definition: fileName.H:59
messageStream Info
Information stream (stdout output on master, null elsewhere)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)