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-2019 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 #include "objectRegistry.H"
28 #include "IOstreams.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Mesh, template<class> class MeshObjectType, class Type>
34 :
35  MeshObjectType<Mesh>(Type::typeName, mesh.thisDb()),
36  mesh_(mesh)
37 {}
38 
39 
40 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
41 
42 template<class Mesh, template<class> class MeshObjectType, class Type>
44 (
45  const Mesh& mesh
46 )
47 {
48  if
49  (
50  mesh.thisDb().objectRegistry::template foundObject<Type>
51  (
52  Type::typeName
53  )
54  )
55  {
56  return mesh.thisDb().objectRegistry::template lookupObject<Type>
57  (
58  Type::typeName
59  );
60  }
61  else
62  {
63  if (meshObject::debug)
64  {
65  Pout<< "MeshObject::New(const " << Mesh::typeName
66  << "&) : constructing " << Type::typeName
67  << " for region " << mesh.name() << endl;
68  }
69 
70  Type* objectPtr = new Type(mesh);
71 
72  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
73 
74  return *objectPtr;
75  }
76 }
77 
78 
79 template<class Mesh, template<class> class MeshObjectType, class Type>
80 template<class Data1>
82 (
83  const Mesh& mesh,
84  const Data1& d
85 )
86 {
87  if
88  (
89  mesh.thisDb().objectRegistry::template foundObject<Type>
90  (
91  Type::typeName
92  )
93  )
94  {
95  return mesh.thisDb().objectRegistry::template lookupObject<Type>
96  (
97  Type::typeName
98  );
99  }
100  else
101  {
102  if (meshObject::debug)
103  {
104  Pout<< "MeshObject::New(const " << Mesh::typeName
105  << "&, const Data1&) : constructing " << Type::typeName
106  << " for region " << mesh.name() << endl;
107  }
108 
109  Type* objectPtr = new Type(mesh, d);
110 
111  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
112 
113  return *objectPtr;
114  }
115 }
116 
117 
118 template<class Mesh, template<class> class MeshObjectType, class Type>
119 template<class Data1, class Data2>
121 (
122  const Mesh& mesh,
123  const Data1& d1,
124  const Data2& d2
125 )
126 {
127  if
128  (
129  mesh.thisDb().objectRegistry::template foundObject<Type>
130  (
131  Type::typeName
132  )
133  )
134  {
135  return mesh.thisDb().objectRegistry::template lookupObject<Type>
136  (
137  Type::typeName
138  );
139  }
140  else
141  {
142  if (meshObject::debug)
143  {
144  Pout<< "MeshObject::New(const " << Mesh::typeName
145  << "&, const Data[1-2]&) : constructing " << Type::typeName
146  << " for region " << mesh.name() << endl;
147  }
148 
149  Type* objectPtr = new Type(mesh, d1, d2);
150 
151  // Make sure to register the top level regIOobject for if Type itself
152  // is a regIOobject
153  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
154 
155  return *objectPtr;
156  }
157 }
158 
159 
160 template<class Mesh, template<class> class MeshObjectType, class Type>
161 template<class Data1, class Data2, class Data3>
163 (
164  const Mesh& mesh,
165  const Data1& d1,
166  const Data2& d2,
167  const Data3& d3
168 )
169 {
170  if
171  (
172  mesh.thisDb().objectRegistry::template foundObject<Type>
173  (
174  Type::typeName
175  )
176  )
177  {
178  return mesh.thisDb().objectRegistry::template lookupObject<Type>
179  (
180  Type::typeName
181  );
182  }
183  else
184  {
185  if (meshObject::debug)
186  {
187  Pout<< "MeshObject::New(const " << Mesh::typeName
188  << "&, const Data[1-3]&) : constructing " << Type::typeName
189  << " for region " << mesh.name() << endl;
190  }
191  Type* objectPtr = new Type(mesh, d1, d2, d3);
192 
193  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
194 
195  return *objectPtr;
196  }
197 }
198 
199 
200 template<class Mesh, template<class> class MeshObjectType, class Type>
201 template<class Data1, class Data2, class Data3, class Data4>
203 (
204  const Mesh& mesh,
205  const Data1& d1,
206  const Data2& d2,
207  const Data3& d3,
208  const Data4& d4
209 )
210 {
211  if
212  (
213  mesh.thisDb().objectRegistry::template foundObject<Type>
214  (
215  Type::typeName
216  )
217  )
218  {
219  return mesh.thisDb().objectRegistry::template lookupObject<Type>
220  (
221  Type::typeName
222  );
223  }
224  else
225  {
226  if (meshObject::debug)
227  {
228  Pout<< "MeshObject::New(const " << Mesh::typeName
229  << "&, const Data[1-4]&) : constructing " << Type::typeName
230  << " for region " << mesh.name() << endl;
231  }
232  Type* objectPtr = new Type(mesh, d1, d2, d3, d4);
233 
234  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
235 
236  return *objectPtr;
237  }
238 }
239 
240 
241 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
242 
243 template<class Mesh, template<class> class MeshObjectType, class Type>
245 {
246  if
247  (
248  mesh.thisDb().objectRegistry::template foundObject<Type>
249  (
250  Type::typeName
251  )
252  )
253  {
254  if (meshObject::debug)
255  {
256  Pout<< "MeshObject::Delete(const Mesh&) : deleting "
257  << Type::typeName << endl;
258  }
259 
260  return mesh.thisDb().checkOut
261  (
262  const_cast<Type&>
263  (
264  mesh.thisDb().objectRegistry::template lookupObject<Type>
265  (
266  Type::typeName
267  )
268  )
269  );
270  }
271  else
272  {
273  return false;
274  }
275 }
276 
277 
278 template<class Mesh, template<class> class MeshObjectType, class Type>
280 {
281  MeshObjectType<Mesh>::release();
282 }
283 
284 
285 template<class Mesh>
287 {
289  (
291  );
292 
293  if (meshObject::debug)
294  {
295  Pout<< "meshObject::movePoints(objectRegistry&) :"
296  << " moving " << Mesh::typeName
297  << " meshObjects for region " << obr.name() << endl;
298  }
299 
300  forAllIter
301  (
303  meshObjects,
304  iter
305  )
306  {
307  if (isA<MoveableMeshObject<Mesh>>(*iter()))
308  {
309  if (meshObject::debug)
310  {
311  Pout<< " Moving " << iter()->name() << endl;
312  }
313  dynamic_cast<MoveableMeshObject<Mesh>*>(iter())->movePoints();
314  }
315  else
316  {
317  if (meshObject::debug)
318  {
319  Pout<< " Destroying " << iter()->name() << endl;
320  }
321  obr.checkOut(*iter());
322  }
323  }
324 }
325 
326 
327 template<class Mesh>
329 {
331  (
333  );
334 
335  if (meshObject::debug)
336  {
337  Pout<< "meshObject::updateMesh(objectRegistry&, "
338  "const mapPolyMesh& mpm) : 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())->updateMesh(mpm);
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 {
373  (
375  );
376 
377  if (meshObject::debug)
378  {
379  Pout<< "meshObject::addPatch(objectRegistry&, "
380  "const label patchi) : updating " << Mesh::typeName
381  << " meshObjects for region " << obr.name() << endl;
382  }
383 
384  forAllIter
385  (
387  meshObjects,
388  iter
389  )
390  {
391  if (isA<PatchMeshObject<Mesh>>(*iter()))
392  {
393  if (meshObject::debug)
394  {
395  Pout<< " Adding patch to " << iter()->name() << endl;
396  }
397  dynamic_cast<PatchMeshObject<Mesh>*>(iter())->addPatch(patchi);
398  }
399  else
400  {
401  if (meshObject::debug)
402  {
403  Pout<< " Destroying " << iter()->name() << endl;
404  }
405  obr.checkOut(*iter());
406  }
407  }
408 }
409 
410 
411 template<class Mesh>
413 (
414  objectRegistry& obr,
415  const labelUList& newToOld,
416  const bool validBoundary
417 )
418 {
420  (
422  );
423 
424  if (meshObject::debug)
425  {
426  Pout<< "meshObject::addPatch(objectRegistry&, "
427  "const labelUList&, const bool) : updating " << Mesh::typeName
428  << " meshObjects for region " << obr.name() << endl;
429  }
430 
431  forAllIter
432  (
434  meshObjects,
435  iter
436  )
437  {
438  if (isA<PatchMeshObject<Mesh>>(*iter()))
439  {
440  if (meshObject::debug)
441  {
442  Pout<< " Adding patch to " << iter()->name() << endl;
443  }
444  dynamic_cast<PatchMeshObject<Mesh>*>(iter())->reorderPatches
445  (
446  newToOld,
447  validBoundary
448  );
449  }
450  else
451  {
452  if (meshObject::debug)
453  {
454  Pout<< " Destroying " << iter()->name() << endl;
455  }
456  obr.checkOut(*iter());
457  }
458  }
459 }
460 
461 
462 template<class Mesh, template<class> class MeshObjectType>
464 {
465  HashTable<MeshObjectType<Mesh>*> meshObjects
466  (
467  obr.lookupClass<MeshObjectType<Mesh>>()
468  );
469 
470  if (meshObject::debug)
471  {
472  Pout<< "meshObject::clear(objectRegistry&) :"
473  << " clearing " << Mesh::typeName
474  << " meshObjects for region " << obr.name() << endl;
475  }
476 
477  forAllIter(typename HashTable<MeshObjectType<Mesh>*>, meshObjects, iter)
478  {
479  if (meshObject::debug)
480  {
481  Pout<< " Destroying " << iter()->name() << endl;
482  }
483  obr.checkOut(*iter());
484  }
485 }
486 
487 
488 template
489 <
490  class Mesh,
491  template<class> class FromType,
492  template<class> class ToType
493 >
495 {
496  HashTable<FromType<Mesh>*> meshObjects
497  (
498  obr.lookupClass<FromType<Mesh>>()
499  );
500 
501  if (meshObject::debug)
502  {
503  Pout<< "meshObject::clearUpto(objectRegistry&) :"
504  << " clearing " << Mesh::typeName
505  << " meshObjects for region " << obr.name() << endl;
506  }
507 
508  forAllIter(typename HashTable<FromType<Mesh>*>, meshObjects, iter)
509  {
510  if (!isA<ToType<Mesh>>(*iter()))
511  {
512  if (meshObject::debug)
513  {
514  Pout<< " Destroying " << iter()->name() << endl;
515  }
516  obr.checkOut(*iter());
517  }
518  }
519 }
520 
521 
522 // ************************************************************************* //
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:295
static bool Delete(const Mesh &mesh)
Definition: MeshObject.C:244
#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:256
bool checkOut(regIOobject &) const
Remove an regIOobject from registry.
static void movePoints(objectRegistry &)
Definition: MeshObject.C:286
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
static const Type & New(const Mesh &mesh)
Definition: MeshObject.C:44
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
virtual ~MeshObject()
Definition: MeshObject.C:279
static void clearUpto(objectRegistry &)
Clear all meshObject derived from FromType up to (but not including)
Definition: MeshObject.C:494
static void updateMesh(objectRegistry &, const mapPolyMesh &)
Definition: MeshObject.C:328
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:370
static void reorderPatches(objectRegistry &, const labelUList &newToOld, const bool validBoundary)
Definition: MeshObject.C:413
label patchi
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Registry of regIOobjects.
MeshObject(const Mesh &mesh)
Definition: MeshObject.C:33
static void clear(objectRegistry &)
Definition: MeshObject.C:463
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.