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-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 "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  << " for 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 " << objName
112  << " of type " << Type::typeName
113  << " for region " << mesh.name() << endl;
114  }
115 
116  ptr = new Type(objName, mesh, std::forward<Args>(args)...);
117 
118  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(ptr));
119 
120  return *ptr;
121 }
122 
123 
124 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
125 
126 template<class Mesh, template<class> class MeshObjectType, class Type>
128 (
129  const word& objName,
130  const Mesh& mesh
131 )
132 {
133  Type* ptr =
134  mesh.thisDb().objectRegistry::template
135  getObjectPtr<Type>(objName);
136 
137  if (ptr)
138  {
139  if (meshObject::debug)
140  {
141  Pout<< "MeshObject::Delete(const Mesh&) : deleting "
142  << objName << endl;
143  }
144 
145  return mesh.thisDb().checkOut(static_cast<MeshObjectType<Mesh>*>(ptr));
146  }
147 
148  return false;
149 }
150 
151 
152 template<class Mesh>
153 void Foam::meshObject::movePoints(objectRegistry& obr)
154 {
155  HashTable<GeometricMeshObject<Mesh>*> meshObjects
156  (
157  obr.lookupClass<GeometricMeshObject<Mesh>>()
158  );
159 
160  if (meshObject::debug)
161  {
162  Pout<< "meshObject::movePoints(objectRegistry&) :"
163  << " moving " << Mesh::typeName
164  << " meshObjects for region " << obr.name() << endl;
165  }
166 
167  forAllIters(meshObjects, iter)
168  {
169  // isA<MoveableMeshObject<Mesh>>
170  auto* objectPtr = dynamic_cast<MoveableMeshObject<Mesh>*>(*iter);
171 
172  if (objectPtr)
173  {
174  if (meshObject::debug)
175  {
176  Pout<< " Moving " << (*iter)->name() << endl;
177  }
178  objectPtr->movePoints();
179  }
180  else
181  {
182  if (meshObject::debug)
183  {
184  Pout<< " Destroying " << (*iter)->name() << endl;
185  }
186  obr.checkOut(*iter);
187  }
188  }
189 }
190 
191 
192 template<class Mesh>
193 void Foam::meshObject::updateMesh(objectRegistry& obr, const mapPolyMesh& mpm)
194 {
195  HashTable<GeometricMeshObject<Mesh>*> meshObjects
196  (
197  obr.lookupClass<GeometricMeshObject<Mesh>>()
198  );
199 
200  if (meshObject::debug)
201  {
202  Pout<< "meshObject::updateMesh(objectRegistry&, "
203  "const mapPolyMesh& mpm) : updating " << Mesh::typeName
204  << " meshObjects for region " << obr.name() << endl;
205  }
206 
207  forAllIters(meshObjects, iter)
208  {
209  // isA<UpdateableMeshObject<Mesh>>
210  auto* objectPtr = dynamic_cast<UpdateableMeshObject<Mesh>*>(*iter);
211 
212  if (objectPtr)
213  {
214  if (meshObject::debug)
215  {
216  Pout<< " Updating " << (*iter)->name() << endl;
217  }
218  objectPtr->updateMesh(mpm);
219  }
220  else
221  {
222  if (meshObject::debug)
223  {
224  Pout<< " Destroying " << (*iter)->name() << endl;
225  }
226  obr.checkOut(*iter);
227  }
228  }
229 }
230 
231 
232 template<class Mesh, template<class> class MeshObjectType>
233 void Foam::meshObject::clear(objectRegistry& obr)
234 {
235  HashTable<MeshObjectType<Mesh>*> meshObjects
236  (
237  obr.lookupClass<MeshObjectType<Mesh>>()
238  );
239 
240  if (meshObject::debug)
241  {
242  Pout<< "meshObject::clear(objectRegistry&) :"
243  << " clearing " << Mesh::typeName
244  << " meshObjects for region " << obr.name() << endl;
245  }
246 
247  forAllIters(meshObjects, iter)
248  {
249  if (meshObject::debug)
250  {
251  Pout<< " Destroying " << (*iter)->name() << endl;
252  }
253  obr.checkOut(*iter);
254  }
255 }
256 
258 template
259 <
260  class Mesh,
261  template<class> class FromType,
262  template<class> class ToType
263 >
265 {
266  HashTable<FromType<Mesh>*> meshObjects
267  (
268  obr.lookupClass<FromType<Mesh>>()
269  );
270 
271  if (meshObject::debug)
272  {
273  Pout<< "meshObject::clearUpto(objectRegistry&) :"
274  << " clearing " << Mesh::typeName
275  << " meshObjects for region " << obr.name() << endl;
276  }
277 
278  forAllIters(meshObjects, iter)
279  {
280  // isA<ToType<Mesh>>
281  auto* objectPtr = dynamic_cast<ToType<Mesh>*>(*iter);
282 
283  if (!objectPtr)
284  {
285  if (meshObject::debug)
286  {
287  Pout<< " Destroying " << (*iter)->name() << endl;
288  }
289  obr.checkOut(*iter);
290  }
291  }
292 }
293 
294 
295 // ************************************************************************* //
static void updateMesh(objectRegistry &obr, const mapPolyMesh &mpm)
Definition: MeshObject.C:186
static void clearUpto(objectRegistry &obr)
Clear all meshObject derived from FromType up to (but not including) ToType.
Definition: MeshObject.C:257
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 a new MeshObject. Registered with typeName.
Definition: MeshObject.C:53
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
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)
Definition: MeshObject.C:226
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:128
HashTable< const Type * > lookupClass() const
Return all 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
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:336
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
int debug
Static debugging option.
static void movePoints(objectRegistry &obr)
Definition: MeshObject.C:146
static bool Delete(const word &objName, const Mesh &mesh)
Static destructor using supplied registration name.
Definition: MeshObject.C:121
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.