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-2019 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
35template<class Mesh, template<class> class MeshObjectType, class Type>
37:
38 MeshObjectType<Mesh>(Type::typeName, mesh.thisDb()),
39 mesh_(mesh)
40{}
41
42
43// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
44
45template<class Mesh, template<class> class MeshObjectType, class Type>
46template<class... Args>
48(
49 const Mesh& mesh,
50 Args&&... args
51)
52{
53 const Type* ptr =
54 mesh.thisDb().objectRegistry::template cfindObject<Type>
55 (
56 Type::typeName
57 );
58
59 if (ptr)
60 {
61 return *ptr;
62 }
63
64 if (meshObject::debug)
65 {
66 Pout<< "MeshObject::New(const " << Mesh::typeName
67 << "&, ...) : constructing " << Type::typeName
68 << " for region " << mesh.name() << endl;
69 }
70
71 Type* objectPtr = new Type(mesh, std::forward<Args>(args)...);
72
73 regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
74
75 return *objectPtr;
76}
77
78
79// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
80
81template<class Mesh, template<class> class MeshObjectType, class Type>
83{
84 Type* ptr =
85 mesh.thisDb().objectRegistry::template getObjectPtr<Type>
86 (
87 Type::typeName
88 );
89
90 if (ptr)
91 {
92 if (meshObject::debug)
93 {
94 Pout<< "MeshObject::Delete(const Mesh&) : deleting "
95 << Type::typeName << endl;
96 }
97
98 return mesh.thisDb().checkOut(ptr);
99 }
100
101 return false;
102}
103
105template<class Mesh>
107{
109 (
111 );
112
113 if (meshObject::debug)
114 {
115 Pout<< "meshObject::movePoints(objectRegistry&) :"
116 << " moving " << Mesh::typeName
117 << " meshObjects for region " << obr.name() << endl;
118 }
119
120 forAllIters(meshObjects, iter)
121 {
122 // isA<MoveableMeshObject<Mesh>>
123 auto* objectPtr = dynamic_cast<MoveableMeshObject<Mesh>*>(*iter);
124
125 if (objectPtr)
126 {
127 if (meshObject::debug)
128 {
129 Pout<< " Moving " << (*iter)->name() << endl;
130 }
131 objectPtr->movePoints();
132 }
133 else
134 {
135 if (meshObject::debug)
136 {
137 Pout<< " Destroying " << (*iter)->name() << endl;
138 }
139 obr.checkOut(*iter);
140 }
141 }
142}
143
144
145template<class Mesh>
147{
149 (
151 );
152
153 if (meshObject::debug)
154 {
155 Pout<< "meshObject::updateMesh(objectRegistry&, "
156 "const mapPolyMesh& mpm) : updating " << Mesh::typeName
157 << " meshObjects for region " << obr.name() << endl;
158 }
159
160 forAllIters(meshObjects, iter)
161 {
162 // isA<MoveableMeshObject<Mesh>>
163 auto* objectPtr = dynamic_cast<UpdateableMeshObject<Mesh>*>(*iter);
164
165 if (objectPtr)
166 {
167 if (meshObject::debug)
168 {
169 Pout<< " Updating " << (*iter)->name() << endl;
170 }
171 objectPtr->updateMesh(mpm);
172 }
173 else
174 {
175 if (meshObject::debug)
176 {
177 Pout<< " Destroying " << (*iter)->name() << endl;
178 }
179 obr.checkOut(*iter);
180 }
181 }
182}
183
184
185template<class Mesh, template<class> class MeshObjectType>
187{
189 (
190 obr.lookupClass<MeshObjectType<Mesh>>()
191 );
192
193 if (meshObject::debug)
194 {
195 Pout<< "meshObject::clear(objectRegistry&) :"
196 << " clearing " << Mesh::typeName
197 << " meshObjects for region " << obr.name() << endl;
198 }
199
200 forAllIters(meshObjects, iter)
201 {
202 if (meshObject::debug)
203 {
204 Pout<< " Destroying " << (*iter)->name() << endl;
205 }
206 obr.checkOut(*iter);
207 }
208}
209
210
211template
212<
213 class Mesh,
214 template<class> class FromType,
215 template<class> class ToType
216>
218{
219 HashTable<FromType<Mesh>*> meshObjects
220 (
221 obr.lookupClass<FromType<Mesh>>()
222 );
223
224 if (meshObject::debug)
225 {
226 Pout<< "meshObject::clearUpto(objectRegistry&) :"
227 << " clearing " << Mesh::typeName
228 << " meshObjects for region " << obr.name() << endl;
229 }
230
231 forAllIters(meshObjects, iter)
232 {
233 // isA<ToType<Mesh>
234 auto* objectPtr = dynamic_cast<ToType<Mesh>*>(*iter);
235
236 if (!objectPtr)
237 {
238 if (meshObject::debug)
239 {
240 Pout<< " Destroying " << (*iter)->name() << endl;
241 }
242 obr.checkOut(*iter);
243 }
244 }
245}
246
247
248// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
Templated abstract base-class for optional mesh objects used to automate their allocation to the mesh...
Definition: MeshObject.H:91
static bool Delete(const Mesh &mesh)
Static destructor.
Definition: MeshObject.C:82
virtual const fileName & name() const
Get the name of the stream.
Definition: OSstream.H:107
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
virtual const objectRegistry & thisDb() const
Return the object registry - resolve conflict polyMesh/lduMesh.
Definition: fvMesh.H:302
const word & name() const
Return reference to name.
Definition: fvMesh.H:310
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:162
static void clearUpto(objectRegistry &obr)
Definition: MeshObject.C:217
void movePoints()
Update for new mesh geometry.
void updateMesh()
Update for new mesh topology.
Registry of regIOobjects.
HashTable< const Type * > lookupClass(const bool strict=false) const
Return all objects with a class satisfying isA<Type>
bool checkOut(regIOobject *io) const
dynamicFvMesh & mesh
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::argList args(argc, argv)
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:260