MeshObject.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-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2024 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 "MeshObject.H"
30 #include "objectRegistry.H"
31 #include "IOstreams.H"
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
35 template<class Mesh, template<class> class MeshObjectType, class Type>
37 :
38  MeshObjectType<Mesh>(Type::typeName, mesh.thisDb()),
39  mesh_(mesh)
40 {}
41 
42 
43 template<class Mesh, template<class> class MeshObjectType, class Type>
45 (
46  const word& objName,
47  const Mesh& mesh
48 )
49 :
50  MeshObjectType<Mesh>(objName, mesh.thisDb()),
51  mesh_(mesh)
52 {}
53 
54 
55 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
56 
57 template<class Mesh, template<class> class MeshObjectType, class Type>
58 template<class... Args>
60 (
61  const Mesh& mesh,
62  Args&&... args
63 )
64 {
65  Type* ptr =
66  mesh.thisDb().objectRegistry::template
67  getObjectPtr<Type>(Type::typeName);
68 
69  if (ptr)
70  {
71  return *ptr;
72  }
73 
75  {
76  Pout<< "MeshObject::New(const " << Mesh::typeName
77  << "&, ...) : constructing <" << Type::typeName
78  << ">, region=" << mesh.name() << endl;
79  }
80 
81  ptr = new Type(mesh, std::forward<Args>(args)...);
82 
83  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(ptr));
84 
85  return *ptr;
86 }
87 
88 
89 template<class Mesh, template<class> class MeshObjectType, class Type>
90 template<class... Args>
92 (
93  const word& objName,
94  const Mesh& mesh,
95  Args&&... args
96 )
97 {
98  Type* ptr =
99  mesh.thisDb().objectRegistry::template
100  getObjectPtr<Type>(objName);
101 
102  if (ptr)
103  {
104  return *ptr;
105  }
106 
107  if (meshObject::debug)
108  {
109  Pout<< "MeshObject::New('" << objName
110  << "', const " << Mesh::typeName
111  << "&, ...) : constructing <" << Type::typeName
112  << ">, region=" << mesh.name() << endl;
113  }
114 
115  ptr = new Type(objName, mesh, std::forward<Args>(args)...);
116 
117  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(ptr));
118 
119  return *ptr;
120 }
121 
122 
123 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
124 
125 template<class Mesh, template<class> class MeshObjectType, class Type>
127 (
128  const word& objName,
129  const Mesh& mesh
130 )
131 {
132  Type* ptr =
133  mesh.thisDb().objectRegistry::template
134  getObjectPtr<Type>(objName);
135 
136  if (ptr)
137  {
138  if (meshObject::debug)
139  {
140  Pout<< "MeshObject::Delete() : deleting <" << Type::typeName
141  << "> " << objName << endl;
142  }
143 
144  return mesh.thisDb().checkOut(static_cast<MeshObjectType<Mesh>*>(ptr));
145  }
147  return false;
148 }
149 
150 
151 template<class Mesh, template<class> class MeshObjectType, class Type>
153 (
154  const word& objName,
155  const Mesh& mesh,
156  const bool checkout
157 )
158 {
159  Type* ptr =
160  mesh.thisDb().objectRegistry::template
161  getObjectPtr<Type>(objName);
162 
163  std::unique_ptr<Type> released;
164 
165  if (ptr)
166  {
167  auto* casted = static_cast<MeshObjectType<Mesh>*>(ptr);
168 
169  if (casted->regIOobject::ownedByRegistry())
170  {
171  // Release ownership from registry and transfer to unique_ptr
172  casted->regIOobject::release();
173  released.reset(ptr);
174 
175  // Allow removal from the registry (ie, checkOut) but leave its
176  // 'registered' status untouched since this is equivalent to
177  // IOobject::registerObject().
178  //
179  // Do not use regIOobject::release(unregister) since this
180  // will prevent later re-storing
181 
182  if (checkout)
183  {
184  casted->regIOobject::checkOut();
185  }
186  }
187 
188  if (meshObject::debug)
189  {
190  Pout<< "MeshObject::Release() : release <" << Type::typeName
191  << "> " << objName << ", owned=" << bool(released) << endl;
192  }
193  }
194 
195  return released;
196 }
197 
198 
199 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
200 
201 template<class Mesh, template<class> class MeshObjectType, class Type>
203 (
204  std::unique_ptr<Type>&& ptr
205 )
206 {
207  bool ok = false;
208 
209  if (ptr)
210  {
211  auto* casted = static_cast<MeshObjectType<Mesh>*>(ptr.get());
212 
213  ok = casted->regIOobject::store();
214 
215  if (ok)
216  {
217  // Took ownership
218  (void) ptr.release();
219  }
220 
221  if (meshObject::debug)
222  {
223  Pout<< "MeshObject::Store() : store <" << Type::typeName
224  << ">, owned=" << ok << endl;
225  }
226  }
227 
228  return ok;
229 }
230 
231 
232 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
233 
234 template<class Mesh>
236 {
238  (
240  );
241 
242  if (meshObject::debug)
243  {
244  Pout<< "meshObject::movePoints() : moving "
245  << meshObjects.size() << " <" << Mesh::typeName
246  << "> meshObjects, region=" << obr.name() << endl;
247  }
248 
249  for (auto& item : meshObjects)
250  {
251  // isA_constCast<MoveableMeshObject<Mesh>>
252  auto* objectPtr = dynamic_cast<MoveableMeshObject<Mesh>*>(&item);
253 
254  if (objectPtr)
255  {
256  if (meshObject::debug)
257  {
258  Pout<< " Moving " << item.name() << endl;
259  }
260  objectPtr->movePoints();
261  }
262  else
263  {
264  if (meshObject::debug)
265  {
266  Pout<< " Destroying " << item.name() << endl;
267  }
268  obr.checkOut(item);
269  }
270  }
271 }
272 
273 
274 template<class Mesh>
275 void Foam::meshObject::updateMesh(objectRegistry& obr, const mapPolyMesh& mpm)
276 {
277  UPtrList<GeometricMeshObject<Mesh>> meshObjects
278  (
279  obr.sorted<GeometricMeshObject<Mesh>>()
280  );
281 
282  if (meshObject::debug)
283  {
284  Pout<< "meshObject::updateMesh() : updating "
285  << meshObjects.size() << " <" << Mesh::typeName
286  << "> meshObjects, region=" << obr.name() << endl;
287  }
288 
289  for (auto& item : meshObjects)
290  {
291  // isA_constCast<UpdateableMeshObject<Mesh>>
292  auto* objectPtr = dynamic_cast<UpdateableMeshObject<Mesh>*>(&item);
293 
294  if (objectPtr)
295  {
296  if (meshObject::debug)
297  {
298  Pout<< " Updating " << item.name() << endl;
299  }
300  objectPtr->updateMesh(mpm);
301  }
302  else
303  {
304  if (meshObject::debug)
305  {
306  Pout<< " Destroying " << item.name() << endl;
307  }
308  obr.checkOut(item);
309  }
310  }
311 }
312 
313 
314 template<class Mesh, template<class> class MeshObjectType>
315 void Foam::meshObject::clear(objectRegistry& obr)
316 {
317  UPtrList<MeshObjectType<Mesh>> meshObjects
318  (
319  obr.sorted<MeshObjectType<Mesh>>()
320  );
321 
322  if (meshObject::debug)
323  {
324  Pout<< "meshObject::clear() : clearing "
325  << meshObjects.size() << " <" << Mesh::typeName
326  << "> meshObjects, region=" << obr.name() << endl;
327  }
328 
329  for (auto& item : meshObjects)
330  {
331  if (meshObject::debug)
332  {
333  Pout<< " Destroying " << item.name() << endl;
334  }
335  obr.checkOut(item);
336  }
337 }
338 
340 template
341 <
342  class Mesh,
343  template<class> class FromType,
344  template<class> class ToType
345 >
347 {
348  UPtrList<FromType<Mesh>> meshObjects
349  (
350  obr.sorted<FromType<Mesh>>()
351  );
352 
353  if (meshObject::debug)
354  {
355  Pout<< "meshObject::clearUpto() : clearing "
356  << meshObjects.size() << " <" << Mesh::typeName
357  << "> meshObjects, region=" << obr.name() << endl;
358  }
359 
360  for (auto& item : meshObjects)
361  {
362  // isA_constCast<ToType<Mesh>>
363  auto* objectPtr = dynamic_cast<ToType<Mesh>*>(&item);
364 
365  if (!objectPtr)
366  {
367  if (meshObject::debug)
368  {
369  Pout<< " Destroying " << item.name() << endl;
370  }
371  obr.checkOut(item);
372  }
373  }
374 }
375 
376 
377 // ************************************************************************* //
static void updateMesh(objectRegistry &obr, const mapPolyMesh &mpm)
Update topology using the given map.
Definition: MeshObject.C:268
static void clearUpto(objectRegistry &obr)
Clear all meshObject derived from FromType up to (but not including) ToType.
Definition: MeshObject.C:339
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
static const Type & New(const Mesh &mesh, Args &&... args)
Get existing or create MeshObject registered with typeName.
Definition: MeshObject.C:53
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool Store(std::unique_ptr< Type > &&ptr)
Transfer ownership of meshObject to registry.
Definition: MeshObject.C:196
bool checkOut(regIOobject *io) const
Remove a regIOobject from registry and free memory if the object is ownedByRegistry. A nullptr is ignored.
static void clear(objectRegistry &obr)
Clear/remove all meshObject of MeshObjectType via objectRegistry::checkOut()
Definition: MeshObject.C:308
virtual const fileName & name() const override
Get the name of the output serial stream. (eg, the name of the Fstream file name) ...
Definition: OSstream.H:134
UPtrList< Type > sorted()
Return sorted list of objects with a class satisfying isA<Type> or isType<Type> (with Strict) ...
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
dynamicFvMesh & mesh
A class for handling words, derived from Foam::string.
Definition: word.H:63
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
int debug
Static debugging option.
static std::unique_ptr< Type > Release(const word &objName, const Mesh &mesh, const bool checkout=false)
Release ownership of meshObject (with given registration name) from registry. Returns nullptr if not ...
Definition: MeshObject.C:146
static void movePoints(objectRegistry &obr)
Update for mesh motion.
Definition: MeshObject.C:228
static bool Delete(const word &objName, const Mesh &mesh)
Static destructor using given registration name.
Definition: MeshObject.C:120
Registry of regIOobjects.
Foam::argList args(argc, argv)
MeshObject(const Mesh &mesh)
Construct with Type::typeName on Mesh.
Definition: MeshObject.C:29
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.