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-2021 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
59  (
60  mesh.thisDb().objectRegistry::template foundObject<Type>
61  (
62  Type::typeName
63  )
64  )
65  {
66  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
67  (
68  Type::typeName
69  );
70  }
71  else
72  {
73  if (meshObject::debug)
74  {
75  Pout<< "MeshObject::New(" << Mesh::typeName
76  << "&) : constructing " << Type::typeName
77  << " for region " << mesh.name() << endl;
78  }
79 
80  Type* objectPtr = new Type(mesh);
81 
82  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
83 
84  return *objectPtr;
85  }
86 }
87 
88 
89 template<class Mesh, template<class> class MeshObjectType, class Type>
91 (
92  const Mesh& mesh
93 )
94 {
95  if
96  (
97  mesh.thisDb().objectRegistry::template foundObject<Type>
98  (
99  Type::typeName
100  )
101  )
102  {
103  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
104  (
105  Type::typeName
106  );
107  }
108  else
109  {
110  if (meshObject::debug)
111  {
112  Pout<< "MeshObject::New(const " << Mesh::typeName
113  << "&) : constructing " << Type::typeName
114  << " for region " << mesh.name() << endl;
115  }
116 
117  Type* objectPtr = new Type(mesh);
118 
119  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
120 
121  return *objectPtr;
122  }
123 }
124 
125 
126 template<class Mesh, template<class> class MeshObjectType, class Type>
127 template<class... Args>
129 (
130  Mesh& mesh,
131  const Args&... args
132 )
133 {
134  if
135  (
136  mesh.thisDb().objectRegistry::template foundObject<Type>
137  (
138  Type::typeName
139  )
140  )
141  {
142  return mesh.thisDb().objectRegistry::template lookupObject<Type>
143  (
144  Type::typeName
145  );
146  }
147  else
148  {
149  if (meshObject::debug)
150  {
151  Pout<< "MeshObject::New(" << Mesh::typeName
152  << "&, const Data1&) : constructing " << Type::typeName
153  << " for region " << mesh.name() << endl;
154  }
155 
156  Type* objectPtr = new Type(mesh, args...);
157 
158  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
159 
160  return *objectPtr;
161  }
162 }
163 
164 
165 template<class Mesh, template<class> class MeshObjectType, class Type>
166 template<class... Args>
168 (
169  const Mesh& mesh,
170  const Args&... args
171 )
172 {
173  if
174  (
175  mesh.thisDb().objectRegistry::template foundObject<Type>
176  (
177  Type::typeName
178  )
179  )
180  {
181  return mesh.thisDb().objectRegistry::template lookupObject<Type>
182  (
183  Type::typeName
184  );
185  }
186  else
187  {
188  if (meshObject::debug)
189  {
190  Pout<< "MeshObject::New(const " << Mesh::typeName
191  << "&, const Data1&) : constructing " << Type::typeName
192  << " for region " << mesh.name() << endl;
193  }
194 
195  Type* objectPtr = new Type(mesh, args...);
196 
197  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
198 
199  return *objectPtr;
200  }
201 }
202 
203 
204 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
205 
206 template<class Mesh, template<class> class MeshObjectType, class Type>
208 {
209  if
210  (
211  mesh.thisDb().objectRegistry::template foundObject<Type>
212  (
213  Type::typeName
214  )
215  )
216  {
217  if (meshObject::debug)
218  {
219  Pout<< "MeshObject::Delete(const Mesh&) : deleting "
220  << Type::typeName << endl;
221  }
222 
223  return mesh.thisDb().checkOut
224  (
225  const_cast<Type&>
226  (
227  mesh.thisDb().objectRegistry::template lookupObject<Type>
228  (
229  Type::typeName
230  )
231  )
232  );
233  }
234  else
235  {
236  return false;
237  }
238 }
239 
240 
241 template<class Mesh, template<class> class MeshObjectType, class Type>
243 {
244  MeshObjectType<Mesh>::release();
245 }
246 
247 
248 template<class Mesh>
250 {
252  (
254  );
255 
256  if (meshObject::debug)
257  {
258  Pout<< "meshObject::movePoints(objectRegistry&) :"
259  << " moving " << Mesh::typeName
260  << " meshObjects for region " << obr.name() << endl;
261  }
262 
263  forAllIter
264  (
266  meshObjects,
267  iter
268  )
269  {
270  if (isA<MoveableMeshObject<Mesh>>(*iter()))
271  {
272  if (meshObject::debug)
273  {
274  Pout<< " Moving " << iter()->name() << endl;
275  }
276  dynamic_cast<MoveableMeshObject<Mesh>*>(iter())->movePoints();
277  }
278  else
279  {
280  if (meshObject::debug)
281  {
282  Pout<< " Destroying " << iter()->name() << endl;
283  }
284  obr.checkOut(*iter());
285  }
286  }
287 }
288 
289 
290 template<class Mesh>
292 {
294  (
296  );
297 
298  if (meshObject::debug)
299  {
300  Pout<< "meshObject::updateMesh(objectRegistry&, "
301  "const mapPolyMesh& mpm) : updating " << Mesh::typeName
302  << " meshObjects for region " << obr.name() << endl;
303  }
304 
305  forAllIter
306  (
308  meshObjects,
309  iter
310  )
311  {
312  if (isA<UpdateableMeshObject<Mesh>>(*iter()))
313  {
314  if (meshObject::debug)
315  {
316  Pout<< " Updating " << iter()->name() << endl;
317  }
318  dynamic_cast<UpdateableMeshObject<Mesh>*>(iter())->updateMesh(mpm);
319  }
320  else
321  {
322  if (meshObject::debug)
323  {
324  Pout<< " Destroying " << iter()->name() << endl;
325  }
326  obr.checkOut(*iter());
327  }
328  }
329 }
330 
331 
332 template<class Mesh>
334 {
336  (
338  );
339 
340  if (meshObject::debug)
341  {
342  Pout<< "meshObject::addPatch(objectRegistry&, "
343  "const label patchi) : updating " << Mesh::typeName
344  << " meshObjects for region " << obr.name() << endl;
345  }
346 
347  forAllIter
348  (
350  meshObjects,
351  iter
352  )
353  {
354  if (isA<PatchMeshObject<Mesh>>(*iter()))
355  {
356  if (meshObject::debug)
357  {
358  Pout<< " Adding patch to " << iter()->name() << endl;
359  }
360  dynamic_cast<PatchMeshObject<Mesh>*>(iter())->addPatch(patchi);
361  }
362  else
363  {
364  if (meshObject::debug)
365  {
366  Pout<< " Destroying " << iter()->name() << endl;
367  }
368  obr.checkOut(*iter());
369  }
370  }
371 }
372 
373 
374 template<class Mesh>
376 (
377  objectRegistry& obr,
378  const labelUList& newToOld,
379  const bool validBoundary
380 )
381 {
383  (
385  );
386 
387  if (meshObject::debug)
388  {
389  Pout<< "meshObject::addPatch(objectRegistry&, "
390  "const labelUList&, const bool) : updating " << Mesh::typeName
391  << " meshObjects for region " << obr.name() << endl;
392  }
393 
394  forAllIter
395  (
397  meshObjects,
398  iter
399  )
400  {
401  if (isA<PatchMeshObject<Mesh>>(*iter()))
402  {
403  if (meshObject::debug)
404  {
405  Pout<< " Adding patch to " << iter()->name() << endl;
406  }
407  dynamic_cast<PatchMeshObject<Mesh>*>(iter())->reorderPatches
408  (
409  newToOld,
410  validBoundary
411  );
412  }
413  else
414  {
415  if (meshObject::debug)
416  {
417  Pout<< " Destroying " << iter()->name() << endl;
418  }
419  obr.checkOut(*iter());
420  }
421  }
422 }
423 
424 
425 template<class Mesh, template<class> class MeshObjectType>
427 {
428  HashTable<MeshObjectType<Mesh>*> meshObjects
429  (
430  obr.lookupClass<MeshObjectType<Mesh>>()
431  );
432 
433  if (meshObject::debug)
434  {
435  Pout<< "meshObject::clear(objectRegistry&) :"
436  << " clearing " << Mesh::typeName
437  << " meshObjects for region " << obr.name() << endl;
438  }
439 
440  forAllIter(typename HashTable<MeshObjectType<Mesh>*>, meshObjects, iter)
441  {
442  if (meshObject::debug)
443  {
444  Pout<< " Destroying " << iter()->name() << endl;
445  }
446  obr.checkOut(*iter());
447  }
448 }
449 
450 
451 template
452 <
453  class Mesh,
454  template<class> class FromType,
455  template<class> class ToType
456 >
458 {
459  HashTable<FromType<Mesh>*> meshObjects
460  (
461  obr.lookupClass<FromType<Mesh>>()
462  );
463 
464  if (meshObject::debug)
465  {
466  Pout<< "meshObject::clearUpto(objectRegistry&) :"
467  << " clearing " << Mesh::typeName
468  << " meshObjects for region " << obr.name() << endl;
469  }
470 
471  forAllIter(typename HashTable<FromType<Mesh>*>, meshObjects, iter)
472  {
473  if (!isA<ToType<Mesh>>(*iter()))
474  {
475  if (meshObject::debug)
476  {
477  Pout<< " Destroying " << iter()->name() << endl;
478  }
479  obr.checkOut(*iter());
480  }
481  }
482 }
483 
484 
485 // ************************************************************************* //
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
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const word & name() const
Return name.
Definition: IOobject.H:303
static bool Delete(const Mesh &mesh)
Definition: MeshObject.C:207
#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:249
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
virtual ~MeshObject()
Definition: MeshObject.C:242
static void clearUpto(objectRegistry &)
Clear all meshObject derived from FromType up to (but not including)
Definition: MeshObject.C:457
static void updateMesh(objectRegistry &, const mapPolyMesh &)
Definition: MeshObject.C:291
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:333
static Type & New(Mesh &mesh)
Definition: MeshObject.C:54
static void reorderPatches(objectRegistry &, const labelUList &newToOld, const bool validBoundary)
Definition: MeshObject.C:376
label patchi
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Registry of regIOobjects.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
MeshObject(const Mesh &mesh)
Definition: MeshObject.C:31
static void clear(objectRegistry &)
Definition: MeshObject.C:426
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.