CArl
Code Arlequin / C++ implementation
CArl_build_intersections.cpp
Go to the documentation of this file.
1 /*
2  * main.cpp
3  *
4  * Created on: Apr 14, 2016
5  * Author: Thiago Milanetto Schlittler
6  */
7 
32 
33 int main(int argc, char *argv[])
34 {
35  // --- Initialize libMesh
36  libMesh::LibMeshInit init(argc, argv);
37 
38  // libMesh's C++ / MPI communicator wrapper
39  libMesh::Parallel::Communicator& WorldComm = init.comm();
40 
41  // Number of processors and processor rank.
42  unsigned int nodes = WorldComm.size();
43  unsigned int rank = WorldComm.rank();
44 
45  // Create local communicator
46  libMesh::Parallel::Communicator LocalComm;
47  WorldComm.split(rank,rank,LocalComm);
48 
49  // Main program performance log
50  libMesh::PerfLog perf_log("Main program");
51 
52  // --- Set up inputs
53 
54  // Command line parser
55  GetPot command_line(argc, argv);
56 
57  // File parser
58  GetPot field_parser;
59 
60  // If there is an input file, parse it to get the parameters. Else, parse the command line
61  std::string input_filename;
62  if (command_line.search(2, "--inputfile", "-i")) {
63  input_filename = command_line.next(input_filename);
64  field_parser.parse_input_file(input_filename, "#", "\n", " \t\n");
65  } else {
66  field_parser = command_line;
67  }
68 
70  carl::get_intersection_input_params(field_parser, input_params);
71 
72  // Declare the three meshes to be intersected
73  libMesh::Mesh test_mesh_A(WorldComm);
74  libMesh::Mesh test_mesh_B(WorldComm);
75  libMesh::Mesh test_mesh_C(WorldComm);
76 
77  // Declare the LOCAL output mesh
78  libMesh::Mesh test_mesh_I(LocalComm);
79 
80  // Read the mesh files and prepare for use
81  test_mesh_A.read(input_params.mesh_A);
82  test_mesh_B.read(input_params.mesh_B);
83  test_mesh_C.read(input_params.mesh_C);
84 
85  test_mesh_A.prepare_for_use();
86  test_mesh_B.prepare_for_use();
87  test_mesh_C.prepare_for_use();
88 
89  // --- Set up the Intersection_search object
90  /*
91  * This object is the main userinterface with the intersection search algorithms.
92  */
93  perf_log.push("Set up");
94  carl::Intersection_Search search_coupling_intersections(
95  test_mesh_A, // Input meshes
96  test_mesh_B, //
97  test_mesh_C, //
98 
99  test_mesh_I, // LOCAL output mesh
100 
101  input_params.output_base, // Common output filename path
102  input_params.inter_meshing_method // Intersection meshing method
103  );
104  perf_log.pop("Set up");
105 
106 
107  // If set to do "verbose" output, print the current partitioning of the intersection search
108  if(input_params.bVerbose)
109  {
110  std::cout << " -> Nb. coupling elements before repartitioning (intersection partition) : "
111  << test_mesh_C.n_partitions() << " ( ";
112  for(unsigned int iii = 0; iii < nodes; ++iii)
113  {
114  std::cout << test_mesh_C.n_elem_on_proc(iii) << " ";
115  }
116  std::cout << ")" << std::endl << std::endl;
117  }
118 
119  // Preallocate the intersection data structures (intersection tables, etc ...)
120  perf_log.push("Prepare intersection load");
121  search_coupling_intersections.PreparePreallocationAndLoad(/* SearchMethod = BRUTE */);
122  search_coupling_intersections.PreallocateAndPartitionCoupling();
123  perf_log.push("Prepare intersection load");
124 
125  // Print the current partitioning of the intersection search
126  if(input_params.bVerbose)
127  {
128  std::cout << " -> Nb. coupling elements after repartitioning (intersection partition) : "
129  << test_mesh_C.n_partitions() << " ( ";
130  for(unsigned int iii = 0; iii < nodes; ++iii)
131  {
132  std::cout << test_mesh_C.n_elem_on_proc(iii) << " ";
133  }
134  std::cout << ")" << std::endl << std::endl;
135  }
136 
137  // --- Do the intersection search!
138  perf_log.push("Search intersection");
139  search_coupling_intersections.BuildIntersections(/* SearchMethod = BRUTE */);
140  search_coupling_intersections.CalculateGlobalVolume();
141  perf_log.pop("Search intersection");
142 
143  // --- Join the intersection tables, stitch the meshes, build the restrictions!
144  // - Declarations
145  // LOCAL stitched mesh
146  libMesh::Mesh test_mesh_full_I(LocalComm,3);
147 
148  // Object used to stitch the meshes and to join the local intersection tables into a global one
149  carl::Stitch_Meshes join_meshes(test_mesh_full_I,input_params.output_base + "_global");
150  join_meshes.set_grid_constraints(test_mesh_A,test_mesh_B);
151 
152  // Set filenames
153  if(rank == 0)
154  {
155  join_meshes.set_base_filenames(input_params.output_base,".e",nodes);
156 
157  // Join the intersection tables
158  perf_log.push("Join intersection tables");
159  join_meshes.join_tables();
160  perf_log.pop("Join intersection tables");
161 
162  // Stitch the intersection meshes
163  if(input_params.bStitchInterMeshes)
164  {
165  perf_log.push("Stitch intersection meshes");
166  join_meshes.stitch_meshes();
167  perf_log.pop("Stitch intersection meshes");
168  }
169  }
170 
171  // Build and save the mesh restrictions
172 
173  // Get set of elements forming the restricted mesh
174  const std::unordered_set<unsigned int> * restrict_set_A_ptr = join_meshes.get_restricted_set_pointer_first();
175  const std::unordered_set<unsigned int> * restrict_set_B_ptr = join_meshes.get_restricted_set_pointer_second();
176 
177  // Export the meshes and the element equivalence tables
178  perf_log.push("Restrict meshes");
179  carl::Mesh_restriction restrict_A(test_mesh_A,LocalComm);
180  restrict_A.BuildRestrictionFromSet(restrict_set_A_ptr);
181  restrict_A.export_restriction_mesh(input_params.output_base + "_A_restriction");
182 
183  carl::Mesh_restriction restrict_B(test_mesh_B,LocalComm);
184  restrict_B.BuildRestrictionFromSet(restrict_set_B_ptr);
185  restrict_B.export_restriction_mesh(input_params.output_base + "_B_restriction");
186  perf_log.pop("Restrict meshes");
187 
188  return 0;
189 }
190 
191 
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 inte...
bool bStitchInterMeshes
Stitch the intersection meshes?
void get_intersection_input_params(GetPot &field_parser, parallel_intersection_params &input_params)
Parser function for the parallel intersection search program (source: CArl_build_intersections.cpp)
void export_restriction_mesh(const std::string &filename_base)
Export the restricted mesh to a file.
void PreparePreallocationAndLoad(SearchMethod search_type=BRUTE)
Do a preliminary search, to optimize the intersection search and construction.
void PreallocateAndPartitionCoupling()
Preallocate Intersection_Search::m_Intersection_Pairs_multimap and repartition the coupling mesh...
Class used to build a restriction of a parent mesh to the coupling region.
Definition: restrict_mesh.h:31
void set_base_filenames(std::vector< std::string > &mesh_filenames, std::vector< std::string > &table_filenames)
Set by hand the filenames used to build the stitched mesh.
Class containing the structure needed to find all the intersections between two meshes, inside the coupling region mesh.
void stitch_meshes()
Stitch the meshes.
void set_grid_constraints(const libMesh::Mesh &mesh_A, const libMesh::Mesh &mesh_B, double vol_tol=-1)
Set the boundaries of the discrete points grid, using the intersected meshes as a base...
carl::IntersectionMeshingMethod inter_meshing_method
Intersection meshing method. Values: carl::IntersectionMeshingMethod::CGAL or carl::IntersectionMeshi...
void join_tables()
Join the intersection tables.
std::string output_base
Output filename base.
Class used to stitch together different meshes.
Definition: stitch_meshes.h:35
void CalculateGlobalVolume()
Calculate the total intersection volume over all the processors.
const std::unordered_set< unsigned int > * get_restricted_set_pointer_second()
Returns the pointer to the set with the elements used to form the second restricted mesh...
Structure containing the parameters for the parallel intersection search test program (source: CArl_b...
const std::unordered_set< unsigned int > * get_restricted_set_pointer_first()
Returns the pointer to the set with the elements used to form the first restricted mesh...
bool bVerbose
Print coupling partitioning?
void BuildIntersections(SearchMethod search_type=BRUTE)
Find and build all the intersections.
int main(int argc, char *argv[])
std::string mesh_C
Coupling mesh path.