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 -------------------------------------------------------------------------------
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 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
44 
45 template<class Mesh, template<class> class MeshObjectType, class Type>
46 template<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 
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 
81 template<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  {
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 
104 
105 template<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 
145 template<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 
185 template<class Mesh, template<class> class MeshObjectType>
187 {
188  HashTable<MeshObjectType<Mesh>*> meshObjects
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 
211 template
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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::meshObject::updateMesh
static void updateMesh(objectRegistry &obr, const mapPolyMesh &mpm)
Definition: MeshObject.C:146
Foam::meshObject::clear
static void clear(objectRegistry &obr)
Definition: MeshObject.C:186
Foam::GeometricMeshObject
Definition: MeshObject.H:201
Foam::MeshObject::MeshObject
MeshObject(const Mesh &mesh)
Construct on Mesh type.
Definition: MeshObject.C:36
Foam::MeshObject::New
static const Type & New(const Mesh &mesh, Args &&... args)
Get existing or create a new MeshObject.
Definition: MeshObject.C:48
objectRegistry.H
Foam::MoveableMeshObject
Definition: MeshObject.H:220
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::UpdateableMeshObject
Definition: MeshObject.H:241
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::objectRegistry::lookupClass
HashTable< const Type * > lookupClass(const bool strict=false) const
Return all objects with a class satisfying isA<Type>
Foam::OSstream::name
virtual const fileName & name() const
Get the name of the stream.
Definition: OSstream.H:107
forAllIters
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:223
Foam::meshObject::movePoints
static void movePoints(objectRegistry &obr)
Definition: MeshObject.C:106
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::MeshObject::Delete
static bool Delete(const Mesh &mesh)
Static destructor.
Definition: MeshObject.C:82
Foam::meshObject::clearUpto
static void clearUpto(objectRegistry &obr)
Definition: MeshObject.C:217
Foam::objectRegistry::checkOut
bool checkOut(regIOobject *io) const
Definition: objectRegistry.C:254
MeshObject.H
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:161
args
Foam::argList args(argc, argv)