MeshObject.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 "MeshObject.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class Mesh, template<class> class MeshObjectType, class Type>
32 :
33  MeshObjectType<Mesh>(Type::typeName, mesh.thisDb()),
34  mesh_(mesh)
35 {}
36 
37 
38 template<class Mesh, template<class> class MeshObjectType, class Type>
40 (
41  const Mesh& mesh,
42  const IOobject& io
43 )
44 :
45  MeshObjectType<Mesh>(Type::typeName, io),
46  mesh_(mesh)
47 {}
48 
49 
50 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
51 
52 template<class Mesh, template<class> class MeshObjectType, class Type>
54 (
55  Mesh& mesh
56 )
57 {
58  if (found(mesh))
59  {
60  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
61  (
62  Type::typeName
63  );
64  }
65  else
66  {
67  if (meshObject::debug)
68  {
69  Pout<< "MeshObject::New(" << Mesh::typeName
70  << "&) : constructing " << Type::typeName
71  << " for region " << mesh.name() << endl;
72  }
73 
74  Type* objectPtr = new Type(mesh);
75 
76  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
77 
78  return *objectPtr;
79  }
80 }
81 
82 
83 template<class Mesh, template<class> class MeshObjectType, class Type>
85 (
86  const Mesh& mesh
87 )
88 {
89  if (found(mesh))
90  {
91  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
92  (
93  Type::typeName
94  );
95  }
96  else
97  {
98  if (meshObject::debug)
99  {
100  Pout<< "MeshObject::New(const " << Mesh::typeName
101  << "&) : constructing " << Type::typeName
102  << " for region " << mesh.name() << endl;
103  }
104 
105  Type* objectPtr = new Type(mesh);
106 
107  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
108 
109  return *objectPtr;
110  }
111 }
112 
113 
114 template<class Mesh, template<class> class MeshObjectType, class Type>
115 template<class... Args>
117 (
118  Mesh& mesh,
119  const Args&... args
120 )
121 {
122  if (found(mesh))
123  {
124  return mesh.thisDb().objectRegistry::template lookupObject<Type>
125  (
126  Type::typeName
127  );
128  }
129  else
130  {
131  if (meshObject::debug)
132  {
133  Pout<< "MeshObject::New(" << Mesh::typeName
134  << "&, const Data1&) : constructing " << Type::typeName
135  << " for region " << mesh.name() << endl;
136  }
137 
138  Type* objectPtr = new Type(mesh, args...);
139 
140  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
141 
142  return *objectPtr;
143  }
144 }
145 
146 
147 template<class Mesh, template<class> class MeshObjectType, class Type>
148 template<class... Args>
150 (
151  const Mesh& mesh,
152  const Args&... args
153 )
154 {
155  if (found(mesh))
156  {
157  return mesh.thisDb().objectRegistry::template lookupObject<Type>
158  (
159  Type::typeName
160  );
161  }
162  else
163  {
164  if (meshObject::debug)
165  {
166  Pout<< "MeshObject::New(const " << Mesh::typeName
167  << "&, const Data1&) : constructing " << Type::typeName
168  << " for region " << mesh.name() << endl;
169  }
170 
171  Type* objectPtr = new Type(mesh, args...);
172 
173  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
174 
175  return *objectPtr;
176  }
177 }
178 
179 
180 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
181 
182 template<class Mesh, template<class> class MeshObjectType, class Type>
184 {
185  if (found(mesh))
186  {
187  if (meshObject::debug)
188  {
189  Pout<< "MeshObject::Delete(const Mesh&) : deleting "
190  << Type::typeName << endl;
191  }
192 
193  return mesh.thisDb().checkOut
194  (
195  const_cast<Type&>
196  (
197  mesh.thisDb().objectRegistry::template lookupObject<Type>
198  (
199  Type::typeName
200  )
201  )
202  );
203  }
204  else
205  {
206  return false;
207  }
208 }
209 
210 
211 template<class Mesh, template<class> class MeshObjectType, class Type>
213 {
214  MeshObjectType<Mesh>::release();
215 }
216 
217 
218 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
219 
220 template<class Mesh, template<class> class MeshObjectType, class Type>
222 (
223  const Mesh& mesh
224 )
225 {
226  return mesh.thisDb().objectRegistry::template foundObject<Type>
227  (
228  Type::typeName
229  );
230 }
231 
232 
233 template<class Mesh>
235 {
237  (
239  );
240 
241  if (meshObject::debug)
242  {
243  Pout<< "meshObject::movePoints(objectRegistry&) :"
244  << " moving " << Mesh::typeName
245  << " meshObjects for region " << obr.name() << endl;
246  }
247 
248  forAllIter
249  (
251  meshObjects,
252  iter
253  )
254  {
255  if (isA<MoveableMeshObject<Mesh>>(*iter()))
256  {
257  if (meshObject::debug)
258  {
259  Pout<< " Moving " << iter()->name() << endl;
260  }
261  dynamic_cast<MoveableMeshObject<Mesh>*>(iter())->movePoints();
262  }
263  else
264  {
265  if (meshObject::debug)
266  {
267  Pout<< " Destroying " << iter()->name() << endl;
268  }
269  obr.checkOut(*iter());
270  }
271  }
272 }
273 
274 
275 template<class Mesh>
277 (
278  objectRegistry& obr,
279  const polyDistributionMap& map
280 )
281 {
283  (
285  );
286 
287  if (meshObject::debug)
288  {
289  Pout<< "meshObject::distribute(objectRegistry&, "
290  "const polyDistributionMap& map) : updating "
291  << Mesh::typeName
292  << " meshObjects for region " << obr.name() << endl;
293  }
294 
295  forAllIter
296  (
298  meshObjects,
299  iter
300  )
301  {
302  if (isA<UpdateableMeshObject<Mesh>>(*iter()))
303  {
304  if (meshObject::debug)
305  {
306  Pout<< " Distributing " << iter()->name() << endl;
307  }
308  dynamic_cast<DistributeableMeshObject<Mesh>*>(iter())
309  ->distribute(map);
310  }
311  else
312  {
313  if (meshObject::debug)
314  {
315  Pout<< " Destroying " << iter()->name() << endl;
316  }
317  obr.checkOut(*iter());
318  }
319  }
320 }
321 
322 
323 template<class Mesh>
325 (
326  objectRegistry& obr,
327  const polyTopoChangeMap& map
328 )
329 {
331  (
333  );
334 
335  if (meshObject::debug)
336  {
337  Pout<< "meshObject::topoChange(objectRegistry&, "
338  "const polyTopoChangeMap& map) : updating " << Mesh::typeName
339  << " meshObjects for region " << obr.name() << endl;
340  }
341 
342  forAllIter
343  (
345  meshObjects,
346  iter
347  )
348  {
349  if (isA<UpdateableMeshObject<Mesh>>(*iter()))
350  {
351  if (meshObject::debug)
352  {
353  Pout<< " Updating " << iter()->name() << endl;
354  }
355  dynamic_cast<UpdateableMeshObject<Mesh>*>(iter())->topoChange(map);
356  }
357  else
358  {
359  if (meshObject::debug)
360  {
361  Pout<< " Destroying " << iter()->name() << endl;
362  }
363  obr.checkOut(*iter());
364  }
365  }
366 }
367 
368 
369 template<class Mesh>
371 (
372  objectRegistry& obr,
373  const polyMeshMap& map
374 )
375 {
377  (
379  );
380 
381  if (meshObject::debug)
382  {
383  Pout<< "meshObject::mapMesh(objectRegistry&, "
384  "const polyMeshMap& map) : updating " << Mesh::typeName
385  << " meshObjects for region " << obr.name() << endl;
386  }
387 
388  forAllIter
389  (
391  meshObjects,
392  iter
393  )
394  {
395  if (isA<UpdateableMeshObject<Mesh>>(*iter()))
396  {
397  if (meshObject::debug)
398  {
399  Pout<< " Updating " << iter()->name() << endl;
400  }
401  dynamic_cast<UpdateableMeshObject<Mesh>*>(iter())->mapMesh(map);
402  }
403  else
404  {
405  if (meshObject::debug)
406  {
407  Pout<< " Destroying " << iter()->name() << endl;
408  }
409  obr.checkOut(*iter());
410  }
411  }
412 }
413 
414 
415 template<class Mesh>
417 {
419  (
421  );
422 
423  if (meshObject::debug)
424  {
425  Pout<< "meshObject::addPatch(objectRegistry&, "
426  "const label patchi) : updating " << Mesh::typeName
427  << " meshObjects for region " << obr.name() << endl;
428  }
429 
430  forAllIter
431  (
433  meshObjects,
434  iter
435  )
436  {
437  if (isA<PatchMeshObject<Mesh>>(*iter()))
438  {
439  if (meshObject::debug)
440  {
441  Pout<< " Adding patch to " << iter()->name() << endl;
442  }
443  dynamic_cast<PatchMeshObject<Mesh>*>(iter())->addPatch(patchi);
444  }
445  else
446  {
447  if (meshObject::debug)
448  {
449  Pout<< " Destroying " << iter()->name() << endl;
450  }
451  obr.checkOut(*iter());
452  }
453  }
454 }
455 
456 
457 template<class Mesh>
459 (
460  objectRegistry& obr,
461  const labelUList& newToOld,
462  const bool validBoundary
463 )
464 {
466  (
468  );
469 
470  if (meshObject::debug)
471  {
472  Pout<< "meshObject::addPatch(objectRegistry&, "
473  "const labelUList&, const bool) : updating " << Mesh::typeName
474  << " meshObjects for region " << obr.name() << endl;
475  }
476 
477  forAllIter
478  (
480  meshObjects,
481  iter
482  )
483  {
484  if (isA<PatchMeshObject<Mesh>>(*iter()))
485  {
486  if (meshObject::debug)
487  {
488  Pout<< " Adding patch to " << iter()->name() << endl;
489  }
490  dynamic_cast<PatchMeshObject<Mesh>*>(iter())->reorderPatches
491  (
492  newToOld,
493  validBoundary
494  );
495  }
496  else
497  {
498  if (meshObject::debug)
499  {
500  Pout<< " Destroying " << iter()->name() << endl;
501  }
502  obr.checkOut(*iter());
503  }
504  }
505 }
506 
507 
508 template<class Mesh, template<class> class MeshObjectType>
510 {
511  HashTable<MeshObjectType<Mesh>*> meshObjects
512  (
513  obr.lookupClass<MeshObjectType<Mesh>>()
514  );
515 
516  if (meshObject::debug)
517  {
518  Pout<< "meshObject::clear(objectRegistry&) :"
519  << " clearing " << Mesh::typeName
520  << " meshObjects for region " << obr.name() << endl;
521  }
522 
523  forAllIter(typename HashTable<MeshObjectType<Mesh>*>, meshObjects, iter)
524  {
525  if (meshObject::debug)
526  {
527  Pout<< " Destroying " << iter()->name() << endl;
528  }
529  obr.checkOut(*iter());
530  }
531 }
532 
533 
534 template
535 <
536  class Mesh,
537  template<class> class FromType,
538  template<class> class ToType
539 >
541 {
542  HashTable<FromType<Mesh>*> meshObjects
543  (
544  obr.lookupClass<FromType<Mesh>>()
545  );
546 
547  if (meshObject::debug)
548  {
549  Pout<< "meshObject::clearUpto(objectRegistry&) :"
550  << " clearing " << Mesh::typeName
551  << " meshObjects for region " << obr.name() << endl;
552  }
553 
554  forAllIter(typename HashTable<FromType<Mesh>*>, meshObjects, iter)
555  {
556  if (!isA<ToType<Mesh>>(*iter()))
557  {
558  if (meshObject::debug)
559  {
560  Pout<< " Destroying " << iter()->name() << endl;
561  }
562  obr.checkOut(*iter());
563  }
564  }
565 }
566 
567 
568 // ************************************************************************* //
bool isA(const Type &t)
Check if a dynamic_cast to typeid is possible.
Definition: typeInfo.H:134
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:82
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
const word & name() const
Return name.
Definition: IOobject.H:315
static bool Delete(const Mesh &mesh)
Definition: MeshObject.C:183
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:459
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool checkOut(regIOobject &) const
Remove an regIOobject from registry.
static void movePoints(objectRegistry &)
Definition: MeshObject.C:234
static bool found(const Mesh &mesh)
Return true if this MeshObject is found in the mesh registry.
Definition: MeshObject.C:222
virtual ~MeshObject()
Definition: MeshObject.C:212
static void mapMesh(objectRegistry &, const polyMeshMap &)
Definition: MeshObject.C:371
static void distribute(objectRegistry &, const polyDistributionMap &)
Definition: MeshObject.C:277
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
void mapMesh(const meshToMesh &interp, const HashSet< word > &selectedFields, const bool noLagrangian)
static void clearUpto(objectRegistry &)
Clear all meshObject derived from FromType up to (but not including)
Definition: MeshObject.C:540
An STL-conforming hash table.
Definition: HashTable.H:61
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
static void addPatch(objectRegistry &, const label patchi)
Definition: MeshObject.C:416
static Type & New(Mesh &mesh)
Definition: MeshObject.C:54
static void reorderPatches(objectRegistry &, const labelUList &newToOld, const bool validBoundary)
Definition: MeshObject.C:459
label patchi
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
static void topoChange(objectRegistry &, const polyTopoChangeMap &)
Definition: MeshObject.C:325
Registry of regIOobjects.
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:50
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
MeshObject(const Mesh &mesh)
Definition: MeshObject.C:31
static void clear(objectRegistry &)
Definition: MeshObject.C:509
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.
bool found