MFEM  v3.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
pncmesh.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_PNCMESH
13 #define MFEM_PNCMESH
14 
15 #include "../config/config.hpp"
16 
17 #ifdef MFEM_USE_MPI
18 
19 #include <map>
20 #include <set>
21 
22 #include "ncmesh.hpp"
23 #include "../general/communication.hpp"
24 #include "../general/sort_pairs.hpp"
25 
26 namespace mfem
27 {
28 
66 class ParNCMesh : public NCMesh
67 {
68 public:
69  ParNCMesh(MPI_Comm comm, const NCMesh& ncmesh);
70 
74  virtual void Refine(const Array<Refinement> &refinements);
75 
77  virtual void LimitNCLevel(int max_level);
78 
81  const NCList& GetSharedVertices()
82  {
84  return shared_vertices;
85  }
86 
90  const NCList& GetSharedEdges()
91  {
92  if (edge_list.Empty()) { BuildEdgeList(); }
93  return shared_edges;
94  }
95 
98  const NCList& GetSharedFaces()
99  {
100  if (face_list.Empty()) { BuildFaceList(); }
101  return shared_faces;
102  }
103 
105  const NCList& GetSharedList(int type)
106  {
107  switch (type)
108  {
109  case 0: return GetSharedVertices();
110  case 1: return GetSharedEdges();
111  default: return GetSharedFaces();
112  }
113  }
114 
116  int GetFaceOrientation(int index) const
117  {
118  return face_orient[index];
119  }
120 
122  int GetOwner(int type, int index) const
123  {
124  switch (type)
125  {
126  case 0: return vertex_owner[index];
127  case 1: return edge_owner[index];
128  default: return face_owner[index];
129  }
130  }
131 
134  const int* GetGroup(int type, int index, int &size) const
135  {
136  const Table* table;
137  switch (type)
138  {
139  case 0: table = &vertex_group; break;
140  case 1: table = &edge_group; break;
141  default: table = &face_group;
142  }
143  size = table->RowSize(index);
144  return table->GetRow(index);
145  }
146 
149  bool RankInGroup(int type, int index, int rank) const
150  {
151  int size;
152  const int* group = GetGroup(type, index, size);
153  for (int i = 0; i < size; i++)
154  {
155  if (group[i] == rank) { return true; }
156  }
157  return false;
158  }
159 
161  bool IsGhost(int type, int index) const
162  {
163  switch (type)
164  {
165  case 0: return index >= NVertices;
166  case 1: return index >= NEdges;
167  default: return index >= NFaces;
168  }
169  }
170 
173  virtual void GetBoundaryClosure(const Array<int> &bdr_attr_is_ess,
174  Array<int> &bdr_vertices,
175  Array<int> &bdr_edges);
176 
180  void GetDebugMesh(Mesh &debug_mesh) const;
181 
182 
183 protected:
184  MPI_Comm MyComm;
186 
191 
192  // lists of vertices/edges/faces shared by us and at least one more processor
194  NCList shared_edges;
195  NCList shared_faces;
196 
197  // owner processor for each vertex/edge/face
201 
202  // list of processors sharing each vertex/edge/face
206 
207  Array<char> face_orient; // see CalcFaceOrientations
208 
216 
218 
219  virtual void Update();
220 
222  int InitialPartition(int index) const
223  { return index * NRanks / leaf_elements.Size(); }
224 
225  virtual void UpdateVertices();
226  virtual void AssignLeafIndices();
227 
228  virtual bool IsGhost(const Element* elem) const
229  { return elem->rank != MyRank; }
230 
231  virtual int GetNumGhosts() const { return NGhostElements; }
232 
233  virtual void OnMeshUpdated(Mesh *mesh);
234 
235  virtual void BuildEdgeList();
236  virtual void BuildFaceList();
237 
238  virtual void ElementSharesEdge(Element* elem, Edge* edge);
239  virtual void ElementSharesFace(Element* elem, Face* face);
240 
241  void BuildSharedVertices();
242 
243  void CalcFaceOrientations();
244 
245  void UpdateLayers();
246 
248 
249  void AddMasterSlaveRanks(int nitems, const NCList& list);
250  void MakeShared(const Table &groups, const NCList &list, NCList &shared);
251 
258  {
259  public:
260  ElementSet(const std::set<Element*> &elements,
261  const Array<Element*> &ncmesh_roots);
262  void Dump(std::ostream &os) const;
263 
265  ElementSet(std::istream &is) { Load(is); }
266  void Load(std::istream &is);
267  void Decode(Array<Element*> &elements,
268  const Array<Element*> &ncmesh_roots) const;
269 
270  protected:
272 
273  bool EncodeTree(Element* elem, const std::set<Element*> &elements);
274  void DecodeTree(Element* elem, int &pos, Array<Element*> &elements) const;
275 
276  void SetInt(int pos, int value);
277  int GetInt(int pos) const;
278  };
279 
281  void EncodeMeshIds(std::ostream &os, Array<MeshId> ids[], int dim) const;
282 
284  void DecodeMeshIds(std::istream &is, Array<MeshId> ids[], int dim,
285  bool decode_indices) const;
286 
287  Array<Element*> tmp_neighbors; // temporary used by ElementNeighborProcessors
288 
292  void ElementNeighborProcessors(Element* elem, Array<int> &ranks);
293 
296  void NeighborProcessors(Array<int> &neighbors);
297 
301  void Prune();
302 
304  bool PruneTree(Element* elem);
305 
306 
312  {
313  public:
314  std::vector<ElemRefType> refinements;
315 
316  void AddRefinement(Element* elem, int ref_type)
317  { refinements.push_back(ElemRefType(elem, ref_type)); }
318 
320  void SetNCMesh(ParNCMesh* pncmesh) { this->pncmesh = pncmesh; }
321 
322  typedef std::map<int, NeighborRefinementMessage> Map;
323 
324  protected:
326 
327  virtual void Encode();
328  virtual void Decode();
329  };
330 
331 
332  friend class ParMesh;
333  friend class NeighborDofMessage;
334 };
335 
336 
337 class FiniteElementCollection; // needed for edge orientation handling
338 
344 class NeighborDofMessage : public VarMessage<135>
345 {
346 public:
348  void AddDofs(int type, const NCMesh::MeshId &id, const Array<int> &dofs);
349 
353  { this->pncmesh = pncmesh; this->fec = fec; this->ndofs = ndofs; }
354 
357  void GetDofs(int type, const NCMesh::MeshId& id,
358  Array<int>& dofs, int &ndofs);
359 
360  typedef std::map<int, NeighborDofMessage> Map;
361 
362 protected:
363  typedef std::map<NCMesh::MeshId, std::vector<int> > IdToDofs;
365 
368  int ndofs;
369 
370  virtual void Encode();
371  virtual void Decode();
372 
373  void ReorderEdgeDofs(const NCMesh::MeshId &id, std::vector<int> &dofs);
374 };
375 
380 class NeighborRowRequest: public VarMessage<312>
381 {
382 public:
383  std::set<int> rows;
384 
385  void RequestRow(int row) { rows.insert(row); }
386  void RemoveRequest(int row) { rows.erase(row); }
387 
388  typedef std::map<int, NeighborRowRequest> Map;
389 
390 protected:
391  virtual void Encode();
392  virtual void Decode();
393 };
394 
401 class NeighborRowReply: public VarMessage<313>
402 {
403 public:
404  void AddRow(int row, const Array<int> &cols, const Vector &srow);
405 
406  bool HaveRow(int row) const { return rows.find(row) != rows.end(); }
407  void GetRow(int row, Array<int> &cols, Vector &srow);
408 
409  typedef std::map<int, NeighborRowReply> Map;
410 
411 protected:
412  struct Row { std::vector<int> cols; Vector srow; };
413  std::map<int, Row> rows;
414 
415  virtual void Encode();
416  virtual void Decode();
417 };
418 
419 
420 // comparison operator so that MeshId can be used as key in std::map
421 inline bool operator< (const NCMesh::MeshId &a, const NCMesh::MeshId &b)
422 {
423  return a.index < b.index;
424 }
425 
426 } // namespace mfem
427 
428 #endif // MFEM_USE_MPI
429 
430 #endif // MFEM_PNCMESH
NCList face_list
lazy-initialized list of faces, see GetFaceList
Definition: ncmesh.hpp:383
NCList edge_list
lazy-initialized list of edges, see GetEdgeList
Definition: ncmesh.hpp:384
NCList shared_edges
Definition: pncmesh.hpp:194
Array< Element * > ghost_layer
list of elements whose &#39;element_type&#39; == 2.
Definition: pncmesh.hpp:217
Array< char > element_type
Definition: pncmesh.hpp:215
void EncodeMeshIds(std::ostream &os, Array< MeshId > ids[], int dim) const
Write to &#39;os&#39; a processor-independent encoding of vertex/edge/face IDs.
Definition: pncmesh.cpp:819
void CalcFaceOrientations()
Definition: pncmesh.cpp:387
bool EncodeTree(Element *elem, const std::set< Element * > &elements)
Definition: pncmesh.cpp:692
void AddDofs(int type, const NCMesh::MeshId &id, const Array< int > &dofs)
Add vertex/edge/face DOFs to an outgoing message.
Definition: pncmesh.cpp:920
Array< char > face_orient
Definition: pncmesh.hpp:207
void AddRefinement(Element *elem, int ref_type)
Definition: pncmesh.hpp:316
virtual int GetNumGhosts() const
Definition: pncmesh.hpp:231
MPI_Comm MyComm
Definition: pncmesh.hpp:184
virtual void BuildFaceList()
Definition: pncmesh.cpp:209
void Dump(std::ostream &os) const
Definition: pncmesh.cpp:804
const NCList & GetSharedVertices()
Definition: pncmesh.hpp:81
Array< int > face_owner
Definition: pncmesh.hpp:200
void BuildSharedVertices()
Definition: pncmesh.cpp:337
void Load(std::istream &is)
Definition: pncmesh.cpp:810
void GetRow(int i, Array< int > &row) const
Return row i in array row (the Table must be finalized)
Definition: table.cpp:179
void DecodeTree(Element *elem, int &pos, Array< Element * > &elements) const
Definition: pncmesh.cpp:759
int GetInt(int pos) const
Definition: pncmesh.cpp:682
virtual void OnMeshUpdated(Mesh *mesh)
Definition: pncmesh.cpp:127
bool operator<(const NCMesh::MeshId &a, const NCMesh::MeshId &b)
Definition: pncmesh.hpp:421
std::map< int, NeighborRowRequest > Map
Definition: pncmesh.hpp:388
int GetOwner(int type, int index) const
Return vertex/edge/face (&#39;type&#39; == 0/1/2, resp.) owner.
Definition: pncmesh.hpp:122
virtual void AssignLeafIndices()
Definition: pncmesh.cpp:55
const NCList & GetSharedEdges()
Definition: pncmesh.hpp:90
void MakeShared(const Table &groups, const NCList &list, NCList &shared)
Definition: pncmesh.cpp:309
bool RankInGroup(int type, int index, int rank) const
Definition: pncmesh.hpp:149
virtual bool IsGhost(const Element *elem) const
Definition: pncmesh.hpp:228
virtual void Decode()
Definition: pncmesh.cpp:1073
std::vector< int > cols
Definition: pncmesh.hpp:412
A parallel extension of the NCMesh class.
Definition: pncmesh.hpp:66
ParNCMesh(MPI_Comm comm, const NCMesh &ncmesh)
Definition: pncmesh.cpp:26
bool Empty() const
Definition: ncmesh.hpp:132
Array< int > vertex_owner
Definition: pncmesh.hpp:198
virtual void Update()
Definition: pncmesh.cpp:43
const FiniteElementCollection * fec
Definition: pncmesh.hpp:367
virtual void Decode()
Definition: pncmesh.cpp:1032
Table face_group
Definition: pncmesh.hpp:205
void DecodeMeshIds(std::istream &is, Array< MeshId > ids[], int dim, bool decode_indices) const
Read from &#39;is&#39; a processor-independent encoding of vertex/edge/face IDs.
Definition: pncmesh.cpp:861
int dim
Definition: ex3.cpp:48
bool PruneTree(Element *elem)
Internal. Recursive part of Prune().
Definition: pncmesh.cpp:530
int InitialPartition(int index) const
Assigns elements to processors at the initial stage (ParMesh creation).
Definition: pncmesh.hpp:222
bool HaveRow(int row) const
Definition: pncmesh.hpp:406
NCList shared_vertices
Definition: pncmesh.hpp:193
Identifies a vertex/edge/face in both Mesh and NCMesh.
Definition: ncmesh.hpp:93
std::map< int, NeighborRefinementMessage > Map
Definition: pncmesh.hpp:322
void SetInt(int pos, int value)
Definition: pncmesh.cpp:673
Table edge_group
Definition: pncmesh.hpp:204
A class for non-conforming AMR on higher-order hexahedral, quadrilateral or triangular meshes...
Definition: ncmesh.hpp:63
NCList shared_faces
Definition: pncmesh.hpp:195
ElementSet(std::istream &is)
Definition: pncmesh.hpp:265
virtual void GetBoundaryClosure(const Array< int > &bdr_attr_is_ess, Array< int > &bdr_vertices, Array< int > &bdr_edges)
Definition: pncmesh.cpp:424
void RemoveRequest(int row)
Definition: pncmesh.hpp:386
Table vertex_group
Definition: pncmesh.hpp:203
virtual void BuildEdgeList()
Definition: pncmesh.cpp:185
void GetDebugMesh(Mesh &debug_mesh) const
Definition: pncmesh.cpp:1187
void GetDofs(int type, const NCMesh::MeshId &id, Array< int > &dofs, int &ndofs)
Definition: pncmesh.cpp:927
std::map< int, Row > rows
Definition: pncmesh.hpp:413
Array< Element * > leaf_elements
Definition: ncmesh.hpp:379
std::map< NCMesh::MeshId, std::vector< int > > IdToDofs
Definition: pncmesh.hpp:363
void Decode(Array< Element * > &elements, const Array< Element * > &ncmesh_roots) const
Definition: pncmesh.cpp:779
virtual void Encode()
Definition: pncmesh.cpp:1107
void UpdateLayers()
Definition: pncmesh.cpp:448
void ElementNeighborProcessors(Element *elem, Array< int > &ranks)
Definition: pncmesh.cpp:483
virtual void ElementSharesFace(Element *elem, Face *face)
Definition: pncmesh.cpp:175
virtual void ElementSharesEdge(Element *elem, Edge *edge)
Definition: pncmesh.cpp:163
virtual void Decode()
Definition: pncmesh.cpp:1127
void Init(ParNCMesh *pncmesh, const FiniteElementCollection *fec, int ndofs)
Definition: pncmesh.hpp:352
virtual void Refine(const Array< Refinement > &refinements)
Definition: pncmesh.cpp:588
virtual void Encode()
Definition: pncmesh.cpp:995
void GetRow(int row, Array< int > &cols, Vector &srow)
Definition: pncmesh.cpp:1097
std::vector< ElemRefType > refinements
Definition: pncmesh.hpp:314
Array< Connection > index_rank
Definition: pncmesh.hpp:247
Array< int > edge_owner
Definition: pncmesh.hpp:199
void SetNCMesh(ParNCMesh *pncmesh)
Set pointer to ParNCMesh (needed to encode the message).
Definition: pncmesh.hpp:320
int GetFaceOrientation(int index) const
Return (shared) face orientation relative to the owner element.
Definition: pncmesh.hpp:116
std::map< int, NeighborDofMessage > Map
Definition: pncmesh.hpp:360
const NCList & GetSharedList(int type)
Helper to get shared vertices/edges/faces (&#39;type&#39; == 0/1/2 resp.).
Definition: pncmesh.hpp:105
Vector data type.
Definition: vector.hpp:33
void AddRow(int row, const Array< int > &cols, const Vector &srow)
Definition: pncmesh.cpp:1088
void AddMasterSlaveRanks(int nitems, const NCList &list)
Definition: pncmesh.cpp:242
std::set< int > rows
Definition: pncmesh.hpp:383
bool IsGhost(int type, int index) const
Returns true if the specified vertex/edge/face is a ghost.
Definition: pncmesh.hpp:161
const NCList & GetSharedFaces()
Definition: pncmesh.hpp:98
std::map< int, NeighborRowReply > Map
Definition: pncmesh.hpp:409
void RequestRow(int row)
Definition: pncmesh.hpp:385
void ReorderEdgeDofs(const NCMesh::MeshId &id, std::vector< int > &dofs)
Definition: pncmesh.cpp:944
Array< Element * > tmp_neighbors
Definition: pncmesh.hpp:287
const int * GetGroup(int type, int index, int &size) const
Definition: pncmesh.hpp:134
void NeighborProcessors(Array< int > &neighbors)
Definition: pncmesh.cpp:507
int RowSize(int i) const
Definition: table.hpp:102
virtual void Encode()
Definition: pncmesh.cpp:1058
virtual void UpdateVertices()
update Vertex::index and vertex_nodeId
Definition: pncmesh.cpp:81
int index
Mesh number.
Definition: ncmesh.hpp:95
Class for parallel meshes.
Definition: pmesh.hpp:28
Abstract data type element.
Definition: element.hpp:27
virtual void LimitNCLevel(int max_level)
To be implemented.
Definition: pncmesh.cpp:661
Variable-length MPI message containing unspecific binary data.
Array< unsigned char > data
encoded refinement (sub-)trees
Definition: pncmesh.hpp:271