CArl
Code Arlequin / C++ implementation
carl::Mesh_restriction Class Reference

Class used to build a restriction of a parent mesh to the coupling region. More...

#include <restrict_mesh.h>

Inheritance diagram for carl::Mesh_restriction:
carl::Patch_construction

Public Member Functions

 Mesh_restriction (libMesh::Mesh &mesh, const libMesh::Parallel::Communicator &local_comm, bool debugOutput=false)
 Constructor with a pre-defined parent mesh and a local communicator. More...
 
void BuildRestriction (const libMesh::ReplicatedMesh &Coupling_mesh)
 Build the restriction of the parent mesh to the coupling region defined by Coupling_mesh. More...
 
libMesh::ReplicatedMesh & restricted_mesh ()
 Returns the restricted mesh. More...
 
void export_restriction_mesh (const std::string &filename_base)
 Export the restricted mesh to a file. More...
 
void BuildRestrictionFromSet (const std::unordered_set< unsigned int > *restricted_mesh_set)
 Build the restriction of the parent mesh from a given element set. This version is useful if the intersection search was already done. More...
 
- Public Member Functions inherited from carl::Patch_construction
 Patch_construction (libMesh::Mesh &mesh, const libMesh::Parallel::Communicator &local_comm, bool debugOutput=false)
 Constructor with a pre-defined parent mesh and a local communicator. More...
 
libMesh::ReplicatedMesh & parent_mesh ()
 Returns the parent mesh. More...
 
libMesh::ReplicatedMesh & patch_mesh ()
 Returns the patch mesh. More...
 
std::unordered_set< unsigned int > & elem_indexes ()
 Returns the set of elements inside the patch. More...
 
std::unordered_set< unsigned int > & node_indexes ()
 Returns the set of nodes inside the patch. More...
 
unsigned int size ()
 Returns the number of elements inside the patch. More...
 
const libMesh::Elem * elem (unsigned int idx)
 
std::unordered_map< unsigned int, std::unordered_set< unsigned int > > & patch_elem_neighbours ()
 Returns the patch mesh element neighbour table. More...
 
void BuildPatch (const libMesh::Elem *Query_elem)
 Build the patch mesh covering a given "Query_elem". More...
 
unsigned int convert_parent_to_patch_elem_id (unsigned int input)
 Convert an element index from the parent mesh to the patch mesh. More...
 
unsigned int convert_patch_to_parent_elem_id (unsigned int input)
 Convert an element index from the patch mesh to the parent mesh. More...
 
void export_patch_mesh (std::string &filename_base)
 Export the patch mesh to a file. More...
 
unsigned int current_elem_id ()
 
const libMesh::Elem * current_elem_pointer ()
 
void intersection_queue_push_back (unsigned int elem_id)
 [ADV. FRONT] Push back element to the deque containing the elements to be treated. More...
 
void set_elem_as_treated (unsigned int elem_id)
 [ADV. FRONT] Mark element as already treated. More...
 
void set_elem_as_inside_queue (unsigned int elem_id)
 [ADV. FRONT] Mark element as already inside the deque of elements to be treated. More...
 
bool intersection_queue_empty ()
 [ADV. FRONT] Check if deque of elements to be treated is empty. More...
 
bool test_queue_empty ()
 [ADV. FRONT] Check if deque of elements to be tested is empty. More...
 
unsigned int intersection_queue_extract_front_elem ()
 [ADV. FRONT] Pop and returns the first element to be treated. More...
 
unsigned int test_queue_extract_front_elem ()
 [ADV. FRONT] Pop and returns the first element to be tested. More...
 
std::unordered_set< unsigned int > & neighbors_to_search_next_pair ()
 [ADV. FRONT] Returns the current element's neighbors that must be tested yet. More...
 
bool set_neighbors_to_search_next_pairs ()
 [ADV. FRONT] Set the list of neighbors of the current element that must be tested yet. More...
 
void add_neighbors_to_test_list ()
 [ADV. FRONT] Adds element to test list. More...
 
void FrontSearch_initialize ()
 [ADV. FRONT] Initialize the advancing front search data structures. More...
 
void FrontSearch_reset ()
 [ADV. FRONT] Reset the advancing front search data structures, with the exception of list of elements to be treated (should be joined to "FrontSearch_prepare_for_probed_test") More...
 
unsigned int FrontSearch_prepare_for_probed_test ()
 [ADV. FRONT] Reset the advancing front search data structures, with the exception of list of elements to be treated. More...
 

Additional Inherited Members

- Protected Member Functions inherited from carl::Patch_construction
 Patch_construction ()
 
void insert_patch_element (const libMesh::Elem *Patch_elem)
 Insert an parent mesh element inside the patch, updating the data structures. More...
 
void build_patch_mesh ()
 Build a patch mesh from the patch data structures. More...
 
- Protected Attributes inherited from carl::Patch_construction
const libMesh::Parallel::Communicator & m_comm
 Parent mesh communicator. More...
 
const unsigned int m_nodes
 Parent mesh number of processors. More...
 
const unsigned int m_rank
 Parent mesh processor rank. More...
 
const libMesh::Parallel::Communicator & m_local_comm
 Patch mesh communicator. More...
 
libMesh::ReplicatedMesh & m_Mesh_parent
 Parent mesh. More...
 
libMesh::ReplicatedMesh m_Mesh_patch
 Patch mesh. More...
 
std::unique_ptr< libMesh::PointLocatorBase > m_Parent_Point_Locator
 Parent mesh point locator. More...
 
Intersection_Tools m_Intersection_Test
 Intersection search and construction tools. More...
 
std::unordered_set< unsigned int > m_Patch_Elem_indexes
 Set of elements inside the patch. More...
 
std::unordered_set< unsigned int > m_Patch_Node_indexes
 Set of nodes inside the patch. More...
 
std::unordered_map< unsigned int, std::unordered_set< unsigned int > > m_Patch_Elem_Neighbours
 Element neighbour table for the patch. More...
 
unsigned int m_working_element_id
 [ADV. FRONT] Index of the patch element currently being tested More...
 
bool m_bTestNeighsForNewPairs
 !!! More...
 
std::deque< int > m_element_intersection_queue
 [ADV. FRONT] Deque containing the elements to be treated. More...
 
std::deque< int > m_element_test_queue
 [ADV. FRONT] Deque containing the elements to be tested More...
 
std::unordered_map< unsigned int, int > m_element_already_treated
 [ADV. FRONT] Marks if an element was already treated or not More...
 
std::unordered_map< unsigned int, int > m_element_inside_intersection_queue
 [ADV. FRONT] Marks if an element is already inside "m_element_intersection_queue" More...
 
std::unordered_set< unsigned int > m_element_neighbours_to_search
 [ADV. FRONT] Set containing the current element's neighbors that must be tested yet. More...
 
std::unordered_map< unsigned int, int > m_node_map_Parent_to_Patch
 Mapping between the parent and patch mesh nodes, using the former as keys. More...
 
std::unordered_map< unsigned int, int > m_node_map_Patch_to_Parent
 Mapping between the patch and parent mesh nodes, using the former as keys. More...
 
std::unordered_map< unsigned int, int > m_elem_map_Parent_to_Patch
 Mapping between the parent and patch mesh elements, using the former as keys. More...
 
std::unordered_map< unsigned int, int > m_elem_map_Patch_to_Parent
 Mapping between the patch and parent mesh elements, using the former as keys. More...
 
bool m_bPrintDebug
 Print debug information? Default: false. More...
 

Detailed Description

Class used to build a restriction of a parent mesh to the coupling region.

This class is derived from the carl::Patch_construction class, and it contains the methods needed to restrict a mesh to the region defined by the coupling mesh.

Definition at line 31 of file restrict_mesh.h.

Constructor & Destructor Documentation

carl::Mesh_restriction::Mesh_restriction ( libMesh::Mesh &  mesh,
const libMesh::Parallel::Communicator &  local_comm,
bool  debugOutput = false 
)
inline

Constructor with a pre-defined parent mesh and a local communicator.

Definition at line 37 of file restrict_mesh.h.

38  : Patch_construction(mesh, local_comm, debugOutput)
39  {
40 
41  };

Member Function Documentation

void carl::Mesh_restriction::BuildRestriction ( const libMesh::ReplicatedMesh &  Coupling_mesh)

Build the restriction of the parent mesh to the coupling region defined by Coupling_mesh.

Definition at line 36 of file restrict_mesh.cpp.

37 {
38  bool bDoIntersect = false;
39 
40  // Deque containing the indices of the elements to test
41  std::deque<int> Restriction_Test_Queue;
42 
43  // Unordered set, used to avoid double testing elements
44  unsigned int treated_from_mesh_preallocation = m_Mesh_parent.n_elem();
45  std::unordered_set<int> Treated_From_Mesh;
46 
47  // Only do the work over a single processor, to avoid communications.
48  if(m_rank ==0)
49  {
50  Treated_From_Mesh.reserve(treated_from_mesh_preallocation);
51 
52  std::set<unsigned int> Intersecting_elems;
53 
54  // Index and pointer to element being tested right now
55  unsigned int Tested_idx;
56 
57  // Candidate index
58  unsigned int Candidate_idx;
59 
60  m_Patch_Elem_indexes.clear();
61  m_Patch_Node_indexes.clear();
63 
64  std::set<unsigned int>::iterator it_set_start;
65 
66  libMesh::Mesh::const_element_iterator it_coupling = Coupling_mesh.elements_begin();
67  libMesh::Mesh::const_element_iterator it_coupling_end = Coupling_mesh.elements_end();
68 
69  // Debug vars
70  int nbOfTests = 1;
71  int nbOfPositiveTests = 1;
72 
73  for( ; it_coupling != it_coupling_end; ++it_coupling)
74  {
75  const libMesh::Elem * Query_elem = * it_coupling;
76 
77  Treated_From_Mesh.clear();
78 
79  // Find elements from the original mesh intersecting the coupling element
80  m_Intersection_Test.FindAllIntersection(Query_elem,m_Parent_Point_Locator,Intersecting_elems);
81 
82  libMesh::Elem * First_Restriction_elems = NULL;
83  libMesh::Elem * elem_candidate = NULL;
84  it_set_start = Intersecting_elems.begin();
85 
86  for( ; it_set_start != Intersecting_elems.end(); ++it_set_start)
87  {
88  Treated_From_Mesh.insert(*it_set_start);
89  First_Restriction_elems = m_Mesh_parent.elem(*it_set_start);
90  insert_patch_element(First_Restriction_elems);
91 
92  for(unsigned int iii = 0; iii < First_Restriction_elems->n_neighbors(); ++iii)
93  {
94  elem_candidate = First_Restriction_elems->neighbor(iii);
95  if(elem_candidate != NULL && Treated_From_Mesh.find(elem_candidate->id())==Treated_From_Mesh.end())
96  {
97  Restriction_Test_Queue.push_back(elem_candidate->id());
98  Treated_From_Mesh.insert(elem_candidate->id());
99  }
100  }
101  }
102 
103  while(!Restriction_Test_Queue.empty())
104  {
105  // Extract element from the list
106  Tested_idx = Restriction_Test_Queue[0];
107  Restriction_Test_Queue.pop_front();
108  const libMesh::Elem * Tested_elem = m_Mesh_parent.elem(Tested_idx);
109 
110  // Test it
111  bDoIntersect = m_Intersection_Test.libMesh_exact_do_intersect(Query_elem,Tested_elem);
112  ++nbOfTests;
113 
114  // If it does intersect ...
115  if(bDoIntersect)
116  {
117  ++nbOfPositiveTests;
118 
119  // Add it to the output list ...
120  insert_patch_element(Tested_elem);
121 
122  // ... And add its neighbours (if they weren't tested yet)
123  for(unsigned int iii = 0; iii < Tested_elem->n_neighbors(); ++iii)
124  {
125  elem_candidate = Tested_elem->neighbor(iii);
126  if(elem_candidate != NULL)
127  {
128  Candidate_idx = elem_candidate->id();
129  if(Treated_From_Mesh.find(Candidate_idx)==Treated_From_Mesh.end())
130  {
131  Restriction_Test_Queue.push_back(Candidate_idx);
132  Treated_From_Mesh.insert(Candidate_idx);
133  }
134  }
135  }
136  }
137  }
138  }
139 
140  this->build_patch_mesh();
141 
142  if(m_bPrintDebug)
143  {
144  std::cout << " DEBUG: Restriction search results" << std::endl;
145  std::cout << " -> Nb. of intersections found : " << m_Patch_Elem_indexes.size() << std::endl << std::endl;
146 
147  std::cout << " -> Nb. of mesh elements : " << m_Mesh_parent.n_elem() << std::endl;
148  std::cout << " -> Nb. of Restriction elements : " << m_Patch_Elem_indexes.size() << std::endl;
149  std::cout << " -> Patch elem % : " << 100.*m_Patch_Elem_indexes.size()/m_Mesh_parent.n_elem() << " %" << std::endl << std::endl;
150 
151  std::cout << " -> Nb. of mesh nodes : " << m_Mesh_parent.n_nodes() << std::endl;
152  std::cout << " -> Nb. of patch nodes : " << m_Patch_Node_indexes.size() << std::endl;
153  std::cout << " -> Patch node % : " << 100.*m_Patch_Node_indexes.size()/m_Mesh_parent.n_nodes() << " %" << std::endl << std::endl;
154 
155  std::cout << " -> Nb. of tests : " << nbOfTests << std::endl;
156  std::cout << " -> Nb. of positive tests : " << nbOfPositiveTests << std::endl;
157  std::cout << " -> Positive % : " << 100.*nbOfPositiveTests/nbOfTests << " %" << std::endl << std::endl;
158  }
159  }
160 }
std::unordered_set< unsigned int > m_Patch_Elem_indexes
Set of elements inside the patch.
void insert_patch_element(const libMesh::Elem *Patch_elem)
Insert an parent mesh element inside the patch, updating the data structures.
Intersection_Tools m_Intersection_Test
Intersection search and construction tools.
bool FindAllIntersection(const libMesh::Elem *Query_elem, std::unique_ptr< libMesh::PointLocatorBase > &point_locator, std::set< unsigned int > &Intersecting_elems)
Find all elements from the mesh intersecting Query_elem, using the mesh's libMesh::PointLocatorBase.
libMesh::ReplicatedMesh & m_Mesh_parent
Parent mesh.
std::unordered_map< unsigned int, std::unordered_set< unsigned int > > m_Patch_Elem_Neighbours
Element neighbour table for the patch.
std::unordered_set< unsigned int > m_Patch_Node_indexes
Set of nodes inside the patch.
std::unique_ptr< libMesh::PointLocatorBase > m_Parent_Point_Locator
Parent mesh point locator.
bool libMesh_exact_do_intersect(const libMesh::Elem *elem_A, const libMesh::Elem *elem_B)
Determinate if two elements intersect.
bool m_bPrintDebug
Print debug information? Default: false.
void build_patch_mesh()
Build a patch mesh from the patch data structures.
const unsigned int m_rank
Parent mesh processor rank.
void carl::Mesh_restriction::BuildRestrictionFromSet ( const std::unordered_set< unsigned int > *  restricted_mesh_set)

Build the restriction of the parent mesh from a given element set. This version is useful if the intersection search was already done.

Definition at line 12 of file restrict_mesh.cpp.

13 {
14  // Intersection and distribution work done already by the stitch algorithm.
15 
16  // Only do the work over a single processor, to avoid communications.
17  if(m_rank == 0)
18  {
19  m_Patch_Elem_indexes.clear();
20  m_Patch_Node_indexes.clear();
22 
23  std::unordered_set<unsigned int >::const_iterator elem_idx_it = restricted_mesh_set->begin();
24  std::unordered_set<unsigned int >::const_iterator elem_idx_it_end = restricted_mesh_set->end();
25 
26  for( ; elem_idx_it != elem_idx_it_end; ++ elem_idx_it)
27  {
28  const libMesh::Elem * elem_to_add = m_Mesh_parent.elem(*elem_idx_it);
29  insert_patch_element(elem_to_add);
30  }
31 
32  this->build_patch_mesh();
33  }
34 }
std::unordered_set< unsigned int > m_Patch_Elem_indexes
Set of elements inside the patch.
void insert_patch_element(const libMesh::Elem *Patch_elem)
Insert an parent mesh element inside the patch, updating the data structures.
libMesh::ReplicatedMesh & m_Mesh_parent
Parent mesh.
std::unordered_map< unsigned int, std::unordered_set< unsigned int > > m_Patch_Elem_Neighbours
Element neighbour table for the patch.
std::unordered_set< unsigned int > m_Patch_Node_indexes
Set of nodes inside the patch.
void build_patch_mesh()
Build a patch mesh from the patch data structures.
const unsigned int m_rank
Parent mesh processor rank.
void carl::Mesh_restriction::export_restriction_mesh ( const std::string &  filename_base)

Export the restricted mesh to a file.

Definition at line 162 of file restrict_mesh.cpp.

163 {
164  if(m_rank == 0)
165  {
166  std::string filename_mesh = filename_base + ".msh";
167 
168  // Print mesh
169  libMesh::GmshIO output_mesh(m_Mesh_patch);
170  output_mesh.binary() = true;
171  output_mesh.write(filename_mesh);
172 
173  std::string filename_elements = filename_base + "_restrict.dat";
174 
175  std::ofstream elems_out(filename_elements);
176 
177  elems_out << m_elem_map_Patch_to_Parent.size() << std::endl;
178  for(unsigned int iii = 0; iii < m_elem_map_Patch_to_Parent.size(); ++iii)
179  {
180  elems_out << iii << " " << m_elem_map_Patch_to_Parent[iii] << std::endl;
181  }
182 
183  elems_out.close();
184  }
185 }
libMesh::ReplicatedMesh m_Mesh_patch
Patch mesh.
std::unordered_map< unsigned int, int > m_elem_map_Patch_to_Parent
Mapping between the patch and parent mesh elements, using the former as keys.
const unsigned int m_rank
Parent mesh processor rank.
libMesh::ReplicatedMesh & carl::Mesh_restriction::restricted_mesh ( )

Returns the restricted mesh.

Definition at line 187 of file restrict_mesh.cpp.

188 {
189  return m_Mesh_patch;
190 }
libMesh::ReplicatedMesh m_Mesh_patch
Patch mesh.

The documentation for this class was generated from the following files: