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
39using namespace std;
40
42{
43
44/**
45 * Returns an instance of the MeshGraphIO factory, held as a singleton.
46 */
48{
49 static MeshGraphIOFactory instance;
50 return instance;
51}
52
55 LibUtilities::DomainRangeShPtr rng, bool fillGraph,
57{
58 LibUtilities::CommSharedPtr comm = session->GetComm();
59 ASSERTL0(comm.get(), "Communication not initialised.");
60
61 // Populate SessionReader. This should be done only on the root process so
62 // that we can partition appropriately without all processes having to read
63 // in the input file.
64 const bool isRoot = comm->TreatAsRankZero();
65 std::string geomType;
66
67 if (isRoot)
68 {
69 // Parse the XML document.
70 session->InitSession();
71
72 // Get geometry type, i.e. XML (compressed/uncompressed) or HDF5.
73 geomType = session->GetGeometryType();
74
75 // Convert to a vector of chars so that we can broadcast.
76 std::vector<char> v(geomType.c_str(),
77 geomType.c_str() + geomType.length());
78
79 size_t length = v.size();
80 comm->Bcast(length, 0);
81 comm->Bcast(v, 0);
82 }
83 else
84 {
85 size_t length;
86 comm->Bcast(length, 0);
87
88 std::vector<char> v(length);
89 comm->Bcast(v, 0);
90
91 geomType = std::string(v.begin(), v.end());
92 }
93
94 // Every process then creates a mesh. Partitioning logic takes place inside
95 // the PartitionMesh function so that we can support different options for
96 // XML and HDF5.
99 meshIO->m_meshGraph =
101 meshIO->m_meshGraph->SetSession(session);
102
103 // For Parallel-in-Time
104 // In contrast to XML, a pre-partitioned mesh directory (_xml) is not
105 // produced when partitionning the mesh for the fine solver when using
106 // HDF5. In order to guarantee the same partition on all time level,
107 // the fine mesh partition has to be copied explicitly.
108 if (partitionedGraph && geomType == "HDF5")
109 {
110 meshIO->m_meshGraph->SetPartition(partitionedGraph);
111 meshIO->m_meshPartitioned = true;
112 }
113
114 meshIO->PartitionMesh(session);
115
116 // Finally, read the geometry information.
117 meshIO->ReadGeometry(rng, fillGraph);
118
119 return meshIO->m_meshGraph;
120}
121
122/**
123 * @brief Create mesh entities for this graph.
124 *
125 * This function will create a map of all mesh entities of the current graph,
126 * which can then be used within the mesh partitioner to construct an
127 * appropriate partitioning.
128 */
129std::map<int, MeshEntity> MeshGraphIO::CreateMeshEntities()
130{
131 std::map<int, MeshEntity> elements;
132 switch (m_meshGraph->GetMeshDimension())
133 {
134 case 1:
135 {
136 for (auto &i : m_meshGraph->GetAllSegGeoms())
137 {
138 MeshEntity e;
139 e.id = e.origId = i.first;
140 e.list.push_back(i.second->GetVertex(0)->GetGlobalID());
141 e.list.push_back(i.second->GetVertex(1)->GetGlobalID());
142 e.ghost = false;
143 elements[e.id] = e;
144 }
145 }
146 break;
147 case 2:
148 {
149 for (auto &i : m_meshGraph->GetAllTriGeoms())
150 {
151 MeshEntity e;
152 e.id = e.origId = i.first;
153 e.list.push_back(i.second->GetEdge(0)->GetGlobalID());
154 e.list.push_back(i.second->GetEdge(1)->GetGlobalID());
155 e.list.push_back(i.second->GetEdge(2)->GetGlobalID());
156 e.ghost = false;
157 elements[e.id] = e;
158 }
159 for (auto &i : m_meshGraph->GetAllQuadGeoms())
160 {
161 MeshEntity e;
162 e.id = e.origId = i.first;
163 e.list.push_back(i.second->GetEdge(0)->GetGlobalID());
164 e.list.push_back(i.second->GetEdge(1)->GetGlobalID());
165 e.list.push_back(i.second->GetEdge(2)->GetGlobalID());
166 e.list.push_back(i.second->GetEdge(3)->GetGlobalID());
167 e.ghost = false;
168 elements[e.id] = e;
169 }
170 }
171 break;
172 case 3:
173 {
174 for (auto &i : m_meshGraph->GetAllTetGeoms())
175 {
176 MeshEntity e;
177 e.id = e.origId = i.first;
178 e.list.push_back(i.second->GetFace(0)->GetGlobalID());
179 e.list.push_back(i.second->GetFace(1)->GetGlobalID());
180 e.list.push_back(i.second->GetFace(2)->GetGlobalID());
181 e.list.push_back(i.second->GetFace(3)->GetGlobalID());
182 e.ghost = false;
183 elements[e.id] = e;
184 }
185 for (auto &i : m_meshGraph->GetAllPyrGeoms())
186 {
187 MeshEntity e;
188 e.id = e.origId = i.first;
189 e.list.push_back(i.second->GetFace(0)->GetGlobalID());
190 e.list.push_back(i.second->GetFace(1)->GetGlobalID());
191 e.list.push_back(i.second->GetFace(2)->GetGlobalID());
192 e.list.push_back(i.second->GetFace(3)->GetGlobalID());
193 e.list.push_back(i.second->GetFace(4)->GetGlobalID());
194 e.ghost = false;
195 elements[e.id] = e;
196 }
197 for (auto &i : m_meshGraph->GetAllPrismGeoms())
198 {
199 MeshEntity e;
200 e.id = e.origId = i.first;
201 e.list.push_back(i.second->GetFace(0)->GetGlobalID());
202 e.list.push_back(i.second->GetFace(1)->GetGlobalID());
203 e.list.push_back(i.second->GetFace(2)->GetGlobalID());
204 e.list.push_back(i.second->GetFace(3)->GetGlobalID());
205 e.list.push_back(i.second->GetFace(4)->GetGlobalID());
206 e.ghost = false;
207 elements[e.id] = e;
208 }
209 for (auto &i : m_meshGraph->GetAllHexGeoms())
210 {
211 MeshEntity e;
212 e.id = e.origId = i.first;
213 e.list.push_back(i.second->GetFace(0)->GetGlobalID());
214 e.list.push_back(i.second->GetFace(1)->GetGlobalID());
215 e.list.push_back(i.second->GetFace(2)->GetGlobalID());
216 e.list.push_back(i.second->GetFace(3)->GetGlobalID());
217 e.list.push_back(i.second->GetFace(4)->GetGlobalID());
218 e.list.push_back(i.second->GetFace(5)->GetGlobalID());
219 e.ghost = false;
220 elements[e.id] = e;
221 }
222 }
223 break;
224 }
225
226 return elements;
227}
228
230{
231 auto meshComposites = &m_meshGraph->GetComposites();
233
234 for (auto &comp : *meshComposites)
235 {
236 std::pair<LibUtilities::ShapeType, vector<int>> tmp;
237 tmp.first = comp.second->m_geomVec[0]->GetShapeType();
238
239 tmp.second.resize(comp.second->m_geomVec.size());
240 for (size_t i = 0; i < tmp.second.size(); ++i)
241 {
242 tmp.second[i] = comp.second->m_geomVec[i]->GetGlobalID();
243 }
244
245 ret[comp.first] = tmp;
246 }
247
248 return ret;
249}
250
251/**
252 * @brief Returns a string representation of a composite.
253 */
255{
256 if (comp->m_geomVec.size() == 0)
257 {
258 return "";
259 }
260
261 // Create a map that gets around the issue of mapping faces -> F and edges
262 // -> E inside the tag.
263 map<LibUtilities::ShapeType, pair<string, string>> compMap;
264 compMap[LibUtilities::ePoint] = make_pair("V", "V");
265 compMap[LibUtilities::eSegment] = make_pair("S", "E");
266 compMap[LibUtilities::eQuadrilateral] = make_pair("Q", "F");
267 compMap[LibUtilities::eTriangle] = make_pair("T", "F");
268 compMap[LibUtilities::eTetrahedron] = make_pair("A", "A");
269 compMap[LibUtilities::ePyramid] = make_pair("P", "P");
270 compMap[LibUtilities::ePrism] = make_pair("R", "R");
271 compMap[LibUtilities::eHexahedron] = make_pair("H", "H");
272
273 stringstream s;
274
275 GeometrySharedPtr firstGeom = comp->m_geomVec[0];
276 int shapeDim = firstGeom->GetShapeDim();
277 string tag = (shapeDim < m_meshGraph->GetMeshDimension())
278 ? compMap[firstGeom->GetShapeType()].second
279 : compMap[firstGeom->GetShapeType()].first;
280
281 std::vector<unsigned int> idxList;
282 std::transform(comp->m_geomVec.begin(), comp->m_geomVec.end(),
283 std::back_inserter(idxList),
284 [](GeometrySharedPtr geom) { return geom->GetGlobalID(); });
285
286 s << " " << tag << "[" << ParseUtils::GenerateSeqString(idxList) << "] ";
287 return s.str();
288}
289
290} // 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:53
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:47
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::shared_ptr< Geometry > GeometrySharedPtr
Definition: Geometry.h:51
STL namespace.
std::vector< unsigned int > list