Nektar++
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 meshIO->PartitionMesh(session);
113
114 // Finally, read the geometry information.
115 meshIO->ReadGeometry(rng, fillGraph);
116
117 return meshIO->m_meshGraph;
118}
119
120/**
121 * @brief Create mesh entities for this graph.
122 *
123 * This function will create a map of all mesh entities of the current graph,
124 * which can then be used within the mesh partitioner to construct an
125 * appropriate partitioning.
126 */
127std::map<int, MeshEntity> MeshGraphIO::CreateMeshEntities()
128{
129 std::map<int, MeshEntity> elements;
130 switch (m_meshGraph->GetMeshDimension())
131 {
132 case 1:
133 {
134 for (auto &i : m_meshGraph->GetAllSegGeoms())
135 {
136 MeshEntity e;
137 e.id = e.origId = i.first;
138 e.list.push_back(i.second->GetVertex(0)->GetGlobalID());
139 e.list.push_back(i.second->GetVertex(1)->GetGlobalID());
140 e.ghost = false;
141 elements[e.id] = e;
142 }
143 }
144 break;
145 case 2:
146 {
147 for (auto &i : m_meshGraph->GetAllTriGeoms())
148 {
149 MeshEntity e;
150 e.id = e.origId = i.first;
151 e.list.push_back(i.second->GetEdge(0)->GetGlobalID());
152 e.list.push_back(i.second->GetEdge(1)->GetGlobalID());
153 e.list.push_back(i.second->GetEdge(2)->GetGlobalID());
154 e.ghost = false;
155 elements[e.id] = e;
156 }
157 for (auto &i : m_meshGraph->GetAllQuadGeoms())
158 {
159 MeshEntity e;
160 e.id = e.origId = i.first;
161 e.list.push_back(i.second->GetEdge(0)->GetGlobalID());
162 e.list.push_back(i.second->GetEdge(1)->GetGlobalID());
163 e.list.push_back(i.second->GetEdge(2)->GetGlobalID());
164 e.list.push_back(i.second->GetEdge(3)->GetGlobalID());
165 e.ghost = false;
166 elements[e.id] = e;
167 }
168 }
169 break;
170 case 3:
171 {
172 for (auto &i : m_meshGraph->GetAllTetGeoms())
173 {
174 MeshEntity e;
175 e.id = e.origId = i.first;
176 e.list.push_back(i.second->GetFace(0)->GetGlobalID());
177 e.list.push_back(i.second->GetFace(1)->GetGlobalID());
178 e.list.push_back(i.second->GetFace(2)->GetGlobalID());
179 e.list.push_back(i.second->GetFace(3)->GetGlobalID());
180 e.ghost = false;
181 elements[e.id] = e;
182 }
183 for (auto &i : m_meshGraph->GetAllPyrGeoms())
184 {
185 MeshEntity e;
186 e.id = e.origId = i.first;
187 e.list.push_back(i.second->GetFace(0)->GetGlobalID());
188 e.list.push_back(i.second->GetFace(1)->GetGlobalID());
189 e.list.push_back(i.second->GetFace(2)->GetGlobalID());
190 e.list.push_back(i.second->GetFace(3)->GetGlobalID());
191 e.list.push_back(i.second->GetFace(4)->GetGlobalID());
192 e.ghost = false;
193 elements[e.id] = e;
194 }
195 for (auto &i : m_meshGraph->GetAllPrismGeoms())
196 {
197 MeshEntity e;
198 e.id = e.origId = i.first;
199 e.list.push_back(i.second->GetFace(0)->GetGlobalID());
200 e.list.push_back(i.second->GetFace(1)->GetGlobalID());
201 e.list.push_back(i.second->GetFace(2)->GetGlobalID());
202 e.list.push_back(i.second->GetFace(3)->GetGlobalID());
203 e.list.push_back(i.second->GetFace(4)->GetGlobalID());
204 e.ghost = false;
205 elements[e.id] = e;
206 }
207 for (auto &i : m_meshGraph->GetAllHexGeoms())
208 {
209 MeshEntity e;
210 e.id = e.origId = i.first;
211 e.list.push_back(i.second->GetFace(0)->GetGlobalID());
212 e.list.push_back(i.second->GetFace(1)->GetGlobalID());
213 e.list.push_back(i.second->GetFace(2)->GetGlobalID());
214 e.list.push_back(i.second->GetFace(3)->GetGlobalID());
215 e.list.push_back(i.second->GetFace(4)->GetGlobalID());
216 e.list.push_back(i.second->GetFace(5)->GetGlobalID());
217 e.ghost = false;
218 elements[e.id] = e;
219 }
220 }
221 break;
222 }
223
224 return elements;
225}
226
228{
229 auto meshComposites = &m_meshGraph->GetComposites();
231
232 for (auto &comp : *meshComposites)
233 {
234 std::pair<LibUtilities::ShapeType, std::vector<int>> tmp;
235 tmp.first = comp.second->m_geomVec[0]->GetShapeType();
236
237 tmp.second.resize(comp.second->m_geomVec.size());
238 for (size_t i = 0; i < tmp.second.size(); ++i)
239 {
240 tmp.second[i] = comp.second->m_geomVec[i]->GetGlobalID();
241 }
242
243 ret[comp.first] = tmp;
244 }
245
246 return ret;
247}
248
249/**
250 * @brief Returns a string representation of a composite.
251 */
253{
254 if (comp->m_geomVec.size() == 0)
255 {
256 return "";
257 }
258
259 // Create a map that gets around the issue of mapping faces -> F and edges
260 // -> E inside the tag.
261 std::map<LibUtilities::ShapeType, std::pair<std::string, std::string>>
262 compMap;
263 compMap[LibUtilities::ePoint] = std::make_pair("V", "V");
264 compMap[LibUtilities::eSegment] = std::make_pair("S", "E");
265 compMap[LibUtilities::eQuadrilateral] = std::make_pair("Q", "F");
266 compMap[LibUtilities::eTriangle] = std::make_pair("T", "F");
267 compMap[LibUtilities::eTetrahedron] = std::make_pair("A", "A");
268 compMap[LibUtilities::ePyramid] = std::make_pair("P", "P");
269 compMap[LibUtilities::ePrism] = std::make_pair("R", "R");
270 compMap[LibUtilities::eHexahedron] = std::make_pair("H", "H");
271
272 std::stringstream s;
273
274 GeometrySharedPtr firstGeom = comp->m_geomVec[0];
275 int shapeDim = firstGeom->GetShapeDim();
276 std::string tag = (shapeDim < m_meshGraph->GetMeshDimension())
277 ? compMap[firstGeom->GetShapeType()].second
278 : compMap[firstGeom->GetShapeType()].first;
279
280 std::vector<unsigned int> idxList;
281 std::transform(comp->m_geomVec.begin(), comp->m_geomVec.end(),
282 std::back_inserter(idxList),
283 [](GeometrySharedPtr geom) { return geom->GetGlobalID(); });
284
285 s << " " << tag << "[" << ParseUtils::GenerateSeqString(idxList) << "] ";
286 return s.str();
287}
288
289} // namespace Nektar::SpatialDomains
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
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
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
Definition: MeshGraphIO.cpp:51
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:64
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:61
std::shared_ptr< Composite > CompositeSharedPtr
Definition: MeshGraph.h:135
MeshGraphIOFactory & GetMeshGraphIOFactory()
Definition: MeshGraphIO.cpp:45
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::shared_ptr< Geometry > GeometrySharedPtr
Definition: Geometry.h:51
std::vector< unsigned int > list