Nektar++
Loading...
Searching...
No Matches
MeshGraphIO.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: MeshGraphIO.cpp
4//
5// For more information, please see: http://www.nektar.info/
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33////////////////////////////////////////////////////////////////////////////////
34
38
40{
41
42/**
43 * Returns an instance of the MeshGraphIO factory, held as a singleton.
44 */
46{
47 static MeshGraphIOFactory instance;
48 return instance;
49}
50
53 LibUtilities::DomainRangeShPtr rng, bool fillGraph,
55{
56 LibUtilities::CommSharedPtr comm = session->GetComm();
57 ASSERTL0(comm.get(), "Communication not initialised.");
58
59 // Populate SessionReader. This should be done only on the root process so
60 // that we can partition appropriately without all processes having to read
61 // in the input file.
62 const bool isRoot = comm->TreatAsRankZero();
63 std::string geomType;
64
65 if (isRoot)
66 {
67 // Parse the XML document.
68 session->InitSession();
69
70 // Get geometry type, i.e. XML (compressed/uncompressed) or HDF5.
71 geomType = session->GetGeometryType();
72
73 // Convert to a vector of chars so that we can broadcast.
74 std::vector<char> v(geomType.c_str(),
75 geomType.c_str() + geomType.length());
76
77 size_t length = v.size();
78 comm->Bcast(length, 0);
79 comm->Bcast(v, 0);
80 }
81 else
82 {
83 size_t length;
84 comm->Bcast(length, 0);
85
86 std::vector<char> v(length);
87 comm->Bcast(v, 0);
88
89 geomType = std::string(v.begin(), v.end());
90 }
91
92 // Every process then creates a mesh. Partitioning logic takes place inside
93 // the PartitionMesh function so that we can support different options for
94 // XML and HDF5.
97 meshIO->m_meshGraph =
99 meshIO->m_meshGraph->SetSession(session);
100
101 // For Parallel-in-Time
102 // In contrast to XML, a pre-partitioned mesh directory (_xml) is not
103 // produced when partitionning the mesh for the fine solver when using
104 // HDF5. In order to guarantee the same partition on all time level,
105 // the fine mesh partition has to be copied explicitly.
106 if (partitionedGraph && geomType == "HDF5")
107 {
108 meshIO->m_meshGraph->SetPartition(partitionedGraph);
109 meshIO->m_meshPartitioned = true;
110 }
111
112 // set up domain range info before partitioning
113 meshIO->m_meshGraph->SetDomainRange(rng);
114
115 meshIO->PartitionMesh(session);
116
117 // Finally, read the geometry information.
118 meshIO->ReadGeometry(fillGraph);
119
120 return meshIO->m_meshGraph;
121}
122
123/**
124 * @brief Create mesh entities for this graph.
125 *
126 * This function will create a map of all mesh entities of the current graph,
127 * which can then be used within the mesh partitioner to construct an
128 * appropriate partitioning.
129 */
130std::map<int, MeshEntity> MeshGraphIO::CreateMeshEntities()
131{
132 std::map<int, MeshEntity> elements;
133 switch (m_meshGraph->GetMeshDimension())
134 {
135 case 1:
136 {
137 for (auto [id, geom] : m_meshGraph->GetGeomMap<SegGeom>())
138 {
139 MeshEntity e;
140 e.id = e.origId = id;
141 e.list.push_back(geom->GetVertex(0)->GetGlobalID());
142 e.list.push_back(geom->GetVertex(1)->GetGlobalID());
143 e.ghost = false;
144 elements[e.id] = e;
145 }
146 }
147 break;
148 case 2:
149 {
150 for (auto [id, geom] : m_meshGraph->GetGeomMap<TriGeom>())
151 {
152 if (m_meshGraph->CheckRange(*geom))
153 {
154 MeshEntity e;
155 e.id = e.origId = id;
156 e.list.push_back(geom->GetEdge(0)->GetGlobalID());
157 e.list.push_back(geom->GetEdge(1)->GetGlobalID());
158 e.list.push_back(geom->GetEdge(2)->GetGlobalID());
159 e.ghost = false;
160 elements[e.id] = e;
161 }
162 }
163 for (auto [id, geom] : m_meshGraph->GetGeomMap<QuadGeom>())
164 {
165 if (m_meshGraph->CheckRange(*geom))
166 {
167 MeshEntity e;
168 e.id = e.origId = id;
169 e.list.push_back(geom->GetEdge(0)->GetGlobalID());
170 e.list.push_back(geom->GetEdge(1)->GetGlobalID());
171 e.list.push_back(geom->GetEdge(2)->GetGlobalID());
172 e.list.push_back(geom->GetEdge(3)->GetGlobalID());
173 e.ghost = false;
174 elements[e.id] = e;
175 }
176 }
177 }
178 break;
179 case 3:
180 {
181 for (auto [id, geom] : m_meshGraph->GetGeomMap<TetGeom>())
182 {
183 if (m_meshGraph->CheckRange(*geom))
184 {
185 MeshEntity e;
186 e.id = e.origId = id;
187 e.list.push_back(geom->GetFace(0)->GetGlobalID());
188 e.list.push_back(geom->GetFace(1)->GetGlobalID());
189 e.list.push_back(geom->GetFace(2)->GetGlobalID());
190 e.list.push_back(geom->GetFace(3)->GetGlobalID());
191 e.ghost = false;
192 elements[e.id] = e;
193 }
194 }
195 for (auto [id, geom] : m_meshGraph->GetGeomMap<PyrGeom>())
196 {
197 if (m_meshGraph->CheckRange(*geom))
198 {
199 MeshEntity e;
200 e.id = e.origId = id;
201 e.list.push_back(geom->GetFace(0)->GetGlobalID());
202 e.list.push_back(geom->GetFace(1)->GetGlobalID());
203 e.list.push_back(geom->GetFace(2)->GetGlobalID());
204 e.list.push_back(geom->GetFace(3)->GetGlobalID());
205 e.list.push_back(geom->GetFace(4)->GetGlobalID());
206 e.ghost = false;
207 elements[e.id] = e;
208 }
209 }
210 for (auto [id, geom] : m_meshGraph->GetGeomMap<PrismGeom>())
211 {
212 if (m_meshGraph->CheckRange(*geom))
213 {
214 MeshEntity e;
215 e.id = e.origId = id;
216 e.list.push_back(geom->GetFace(0)->GetGlobalID());
217 e.list.push_back(geom->GetFace(1)->GetGlobalID());
218 e.list.push_back(geom->GetFace(2)->GetGlobalID());
219 e.list.push_back(geom->GetFace(3)->GetGlobalID());
220 e.list.push_back(geom->GetFace(4)->GetGlobalID());
221 e.ghost = false;
222 elements[e.id] = e;
223 }
224 }
225 for (auto [id, geom] : m_meshGraph->GetGeomMap<HexGeom>())
226 {
227 if (m_meshGraph->CheckRange(*geom))
228 {
229 MeshEntity e;
230 e.id = e.origId = id;
231 e.list.push_back(geom->GetFace(0)->GetGlobalID());
232 e.list.push_back(geom->GetFace(1)->GetGlobalID());
233 e.list.push_back(geom->GetFace(2)->GetGlobalID());
234 e.list.push_back(geom->GetFace(3)->GetGlobalID());
235 e.list.push_back(geom->GetFace(4)->GetGlobalID());
236 e.list.push_back(geom->GetFace(5)->GetGlobalID());
237 e.ghost = false;
238 elements[e.id] = e;
239 }
240 }
241 }
242 break;
243 }
244
245 return elements;
246}
247
249{
250 auto meshComposites = &m_meshGraph->GetComposites();
252
253 for (auto &comp : *meshComposites)
254 {
255 std::pair<LibUtilities::ShapeType, std::vector<int>> tmp;
256 tmp.first = comp.second->m_geomVec[0]->GetShapeType();
257
258 tmp.second.resize(comp.second->m_geomVec.size());
259 for (size_t i = 0; i < tmp.second.size(); ++i)
260 {
261 tmp.second[i] = comp.second->m_geomVec[i]->GetGlobalID();
262 }
263
264 ret[comp.first] = tmp;
265 }
266
267 return ret;
268}
269
270/**
271 * @brief Returns a string representation of a composite.
272 */
274{
275 if (comp->m_geomVec.size() == 0)
276 {
277 return "";
278 }
279
280 // Create a map that gets around the issue of mapping faces -> F and edges
281 // -> E inside the tag.
282 std::map<LibUtilities::ShapeType, std::pair<std::string, std::string>>
283 compMap;
284 compMap[LibUtilities::ePoint] = std::make_pair("V", "V");
285 compMap[LibUtilities::eSegment] = std::make_pair("S", "E");
286 compMap[LibUtilities::eQuadrilateral] = std::make_pair("Q", "F");
287 compMap[LibUtilities::eTriangle] = std::make_pair("T", "F");
288 compMap[LibUtilities::eTetrahedron] = std::make_pair("A", "A");
289 compMap[LibUtilities::ePyramid] = std::make_pair("P", "P");
290 compMap[LibUtilities::ePrism] = std::make_pair("R", "R");
291 compMap[LibUtilities::eHexahedron] = std::make_pair("H", "H");
292
293 std::stringstream s;
294
295 Geometry *firstGeom = comp->m_geomVec[0];
296 int shapeDim = firstGeom->GetShapeDim();
297 std::string tag = (shapeDim < m_meshGraph->GetMeshDimension())
298 ? compMap[firstGeom->GetShapeType()].second
299 : compMap[firstGeom->GetShapeType()].first;
300
301 std::vector<unsigned int> idxList;
302 std::transform(comp->m_geomVec.begin(), comp->m_geomVec.end(),
303 std::back_inserter(idxList),
304 [](Geometry *geom) { return geom->GetGlobalID(); });
305
306 s << " " << tag << "[" << ParseUtils::GenerateSeqString(idxList) << "] ";
307 return s.str();
308}
309
310} // namespace Nektar::SpatialDomains
#define ASSERTL0(condition, msg)
Provides a generic Factory class.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static std::string GenerateSeqString(const std::vector< T > &v)
Generate a compressed comma-separated string representation of a vector of unsigned integers.
Definition ParseUtils.h:72
Base class for shape geometry information.
Definition Geometry.h:74
LibUtilities::ShapeType GetShapeType(void)
Get the geometric shape type of this object.
Definition Geometry.h:314
int GetShapeDim() const
Get the object's shape dimension.
Definition Geometry.h:430
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
std::string GetCompositeString(CompositeSharedPtr comp)
Returns a string representation of a composite.
CompositeDescriptor CreateCompositeDescriptor()
std::map< int, MeshEntity > CreateMeshEntities()
Create mesh entities for this graph.
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< DomainRange > DomainRangeShPtr
Definition DomainRange.h:69
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:55
std::shared_ptr< MeshGraphIO > MeshGraphIOSharedPtr
Definition MeshGraphIO.h:46
std::map< int, std::pair< LibUtilities::ShapeType, std::vector< int > > > CompositeDescriptor
Definition MeshGraph.h:124
std::shared_ptr< Composite > CompositeSharedPtr
Definition MeshGraph.h:178
MeshGraphIOFactory & GetMeshGraphIOFactory()
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition MeshGraph.h:217
std::vector< unsigned int > list