intersect_r3d.h
Go to the documentation of this file.
1 /*
2 This file is part of the Ristra portage project.
3 Please see the license file at the root of this repository, or at:
4  https://github.com/laristra/portage/blob/master/LICENSE
5 */
6 
7 #ifndef PORTAGE_INTERSECT_INTERSECT_R3D_H_
8 #define PORTAGE_INTERSECT_INTERSECT_R3D_H_
9 
10 #include <array>
11 #include <stdexcept>
12 #include <vector>
13 #include <algorithm>
14 
15 // portage includes
16 extern "C" {
17 #include "wonton/intersect/r3d/r3d.h"
18 }
22 
23 #ifdef HAVE_TANGRAM
24 #include "tangram/driver/CellMatPoly.h"
25 #include "tangram/driver/driver.h"
26 #include "tangram/support/MatPoly.h"
27 #endif
28 
29 namespace Portage {
30 
49 
50 template <Entity_kind on_what, class SourceMeshType,
51  class SourceStateType, class TargetMeshType,
52  template <class, int, class, class> class InterfaceReconstructorType =
53  DummyInterfaceReconstructor,
54  class Matpoly_Splitter = void,
55  class Matpoly_Clipper = void>
56 class IntersectR3D {
57 
58 #ifdef HAVE_TANGRAM
59  using InterfaceReconstructor3D =
60  Tangram::Driver<InterfaceReconstructorType, 3, SourceMeshType,
61  Matpoly_Splitter, Matpoly_Clipper>;
62 #endif
63 
64  public:
65 #ifdef HAVE_TANGRAM
66 
68  IntersectR3D(SourceMeshType const & source_mesh,
69  SourceStateType const & source_state,
70  TargetMeshType const & target_mesh,
71  NumericTolerances_t num_tols,
72  std::shared_ptr<InterfaceReconstructor3D> ir,
73  bool rectangular_mesh = false)
74  : sourceMeshWrapper(source_mesh), sourceStateWrapper(source_state),
75  targetMeshWrapper(target_mesh), interface_reconstructor(ir),
76  rectangular_mesh_(rectangular_mesh), num_tols_(num_tols) {}
77 #endif
78 
80 
81  IntersectR3D(SourceMeshType const & source_mesh,
82  SourceStateType const & source_state,
83  TargetMeshType const & target_mesh,
84  NumericTolerances_t num_tols,
85  bool rectangular_mesh = false)
86  : sourceMeshWrapper(source_mesh), sourceStateWrapper(source_state),
87  targetMeshWrapper(target_mesh), rectangular_mesh_(rectangular_mesh),
88  num_tols_(num_tols) {}
89 
91 
92  void set_material(int m) {
93  matid_ = m;
94  }
95 
101 
102  std::vector<Weights_t> operator() (int tgt_entity, std::vector<int> const& src_entities) const {
103  throw std::runtime_error("not implemented for this entity type");
104  }
105 
106 
107 
108  IntersectR3D() = delete;
109 
111  IntersectR3D & operator = (const IntersectR3D &) = delete;
112 
113  private:
114  SourceMeshType const & sourceMeshWrapper;
115  SourceStateType const & sourceStateWrapper;
116  TargetMeshType const & targetMeshWrapper;
117 #ifdef HAVE_TANGRAM
118  std::shared_ptr<InterfaceReconstructor3D> interface_reconstructor;
119 #endif
120  bool rectangular_mesh_ = false;
121  int matid_ = -1;
122  NumericTolerances_t num_tols_ {};
123 };
124 
125 
126 // Specialization of Intersect3D class for cells
127 
128 template <class SourceMeshType, class SourceStateType,
129  class TargetMeshType,
130  template <class, int, class, class> class InterfaceReconstructorType,
131  class Matpoly_Splitter, class Matpoly_Clipper>
132 class IntersectR3D<Entity_kind::CELL, SourceMeshType, SourceStateType, TargetMeshType,
133  InterfaceReconstructorType,
134  Matpoly_Splitter, Matpoly_Clipper> {
135 
136 #ifdef HAVE_TANGRAM
137  using InterfaceReconstructor3D =
138  Tangram::Driver<InterfaceReconstructorType, 3, SourceMeshType,
139  Matpoly_Splitter, Matpoly_Clipper>;
140 #endif
141 
142  public:
143 
144 #ifdef HAVE_TANGRAM
145 
147  IntersectR3D(SourceMeshType const & source_mesh,
148  SourceStateType const & source_state,
149  TargetMeshType const & target_mesh,
150  NumericTolerances_t num_tols,
151  std::shared_ptr<InterfaceReconstructor3D> ir,
152  bool rectangular_mesh = false)
153  : sourceMeshWrapper(source_mesh), sourceStateWrapper(source_state),
154  targetMeshWrapper(target_mesh), interface_reconstructor(ir),
155  rectangular_mesh_(rectangular_mesh), num_tols_(num_tols) {}
156 #endif
157 
159 
160  IntersectR3D(SourceMeshType const & source_mesh,
161  SourceStateType const & source_state,
162  TargetMeshType const & target_mesh,
163  NumericTolerances_t num_tols,
164  bool rectangular_mesh = false)
165  : sourceMeshWrapper(source_mesh), sourceStateWrapper(source_state),
166  targetMeshWrapper(target_mesh), rectangular_mesh_(rectangular_mesh),
167  num_tols_(num_tols) {}
168 
169 
171 
172  void set_material(int m) {
173  matid_ = m;
174  }
175 
181 
182  std::vector<Weights_t> operator() (const int tgt_cell,
183  const std::vector<int>& src_cells) const {
184 
185  std::vector<std::array<Point<3>, 4>> target_tet_coords;
186 
187  // We should avoid any decomposition for cells of a rectangular
188  // mesh but for now we will decompose the target all the time
189 
190  targetMeshWrapper.decompose_cell_into_tets(tgt_cell, &target_tet_coords,
191  rectangular_mesh_);
192 
193  // CAN MAKE THIS INTO A THRUST::TRANSFORM CALL
194  int nsrc = src_cells.size();
195  std::vector<Weights_t> sources_and_weights(nsrc);
196  int ninserted = 0;
197  for (int i = 0; i < nsrc; i++) {
198  int s = src_cells[i];
199 
200  Weights_t & this_wt = sources_and_weights[ninserted];
201  this_wt.entityID = s;
202 
203 #ifdef HAVE_TANGRAM
204  int nmats = sourceStateWrapper.cell_get_num_mats(s);
205  std::vector<int> cellmats;
206  sourceStateWrapper.cell_get_mats(s, &cellmats);
207 
208  if (!nmats || (matid_ == -1) || (nmats == 1 && cellmats[0] == matid_)) {
209  // ---------- Intersection with pure cell ---------------
210  // nmats == 0 -- no materials ==> single material
211  // matid_ == -1 -- intersect with mesh not a particular material
212  // nmats == 1 && cellmats[0] == matid -- intersection with pure cell
213  // containing matid
214 
215  facetedpoly_t srcpoly;
216  sourceMeshWrapper.cell_get_facetization(s, &srcpoly.facetpoints,
217  &srcpoly.points);
218 
219  this_wt.weights = intersect_polys_r3d(srcpoly, target_tet_coords,
220  num_tols_);
221 
222  } else if (std::find(cellmats.begin(), cellmats.end(), matid_) !=
223  cellmats.end()) {
224  // mixed cell containing this material - intersect with
225  // polygon approximation of this material in the cell
226  // (obtained from interface reconstruction)
227 
228  Tangram::CellMatPoly<3> const& cellmatpoly =
229  interface_reconstructor->cell_matpoly_data(s);
230  std::vector<Tangram::MatPoly<3>> matpolys =
231  cellmatpoly.get_matpolys(matid_);
232 
233  this_wt.weights.resize(4,0.0);
234  for (const auto& matpoly : matpolys) {
235  facetedpoly_t srcpoly = get_faceted_matpoly(matpoly);
236 
237  std::vector<double> momvec = intersect_polys_r3d(srcpoly,
238  target_tet_coords, num_tols_);
239  for (int k = 0; k < 4; k++)
240  this_wt.weights[k] += momvec[k];
241  }
242 
243  }
244 #else
245  facetedpoly_t srcpoly;
246  sourceMeshWrapper.cell_get_facetization(s, &srcpoly.facetpoints,
247  &srcpoly.points);
248 
249  this_wt.weights = intersect_polys_r3d(srcpoly, target_tet_coords,
250  num_tols_);
251 #endif
252  // Increment if vol of intersection > 0; otherwise, allow overwrite
253  if (!this_wt.weights.empty() && this_wt.weights[0] > 0.0)
254  ninserted++;
255  }
256 
257  sources_and_weights.resize(ninserted);
258  return sources_and_weights;
259  }
260 
261 
262  IntersectR3D() = delete;
263 
265  IntersectR3D & operator = (const IntersectR3D &) = delete;
266 
267  private:
268  SourceMeshType const & sourceMeshWrapper;
269  SourceStateType const & sourceStateWrapper;
270  TargetMeshType const & targetMeshWrapper;
271 #ifdef HAVE_TANGRAM
272  std::shared_ptr<InterfaceReconstructor3D> interface_reconstructor;
273 #endif
274  bool rectangular_mesh_ = false;
275  int matid_ = -1;
276  NumericTolerances_t num_tols_ {};
277 };
278 
279 
280 // Specialization of IntersectR3D class for nodes
281 
282 template <class SourceMeshType, class SourceStateType,
283  class TargetMeshType,
284  template <class, int, class, class> class InterfaceReconstructorType,
285  class Matpoly_Splitter, class Matpoly_Clipper>
286 class IntersectR3D<Entity_kind::NODE, SourceMeshType, SourceStateType, TargetMeshType,
287  InterfaceReconstructorType,
288  Matpoly_Splitter, Matpoly_Clipper> {
289 
290 #ifdef HAVE_TANGRAM
291  using InterfaceReconstructor3D =
292  Tangram::Driver<InterfaceReconstructorType, 3, SourceMeshType,
293  Matpoly_Splitter, Matpoly_Clipper>;
294 #endif
295 
296  public:
297 
298 #ifdef HAVE_TANGRAM
299 
301  IntersectR3D(SourceMeshType const & source_mesh,
302  SourceStateType const & source_state,
303  TargetMeshType const & target_mesh,
304  NumericTolerances_t num_tols,
305  std::shared_ptr<InterfaceReconstructor3D> ir,
306  bool rectangular_mesh = false)
307  : sourceMeshWrapper(source_mesh), sourceStateWrapper(source_state),
308  targetMeshWrapper(target_mesh), interface_reconstructor(ir),
309  rectangular_mesh_(rectangular_mesh), num_tols_(num_tols) {}
310 #endif
311 
313 
314  IntersectR3D(SourceMeshType const & source_mesh,
315  SourceStateType const & source_state,
316  TargetMeshType const & target_mesh,
317  NumericTolerances_t num_tols,
318  bool rectangular_mesh = false)
319  : sourceMeshWrapper(source_mesh), sourceStateWrapper(source_state),
320  targetMeshWrapper(target_mesh), rectangular_mesh_(rectangular_mesh),
321  num_tols_(num_tols) {}
322 
324 
325  void set_material(int m) {
326  matid_ = m;
327  }
328 
336 
337  std::vector<Weights_t> operator() (const int tgt_node,
338  const std::vector<int>& src_nodes) const {
339 
340  // for debug
341  Point<3> tgtxyz;
342  targetMeshWrapper.node_get_coordinates(tgt_node, &tgtxyz);
343 
344  std::vector<std::array<Point<3>, 4>> target_tet_coords;
345 
346  // We should avoid any decomposition for duall cells of a
347  // rectangular mesh but for now we will decompose the target all
348  // the time
349 
350  targetMeshWrapper.dual_wedges_get_coordinates(tgt_node, &target_tet_coords);
351 
352 
353  // CAN MAKE THIS INTO A THRUST TRANSFORM CALL
354  int nsrc = src_nodes.size();
355  std::vector<Weights_t> sources_and_weights(nsrc);
356  int ninserted = 0;
357  for (int i = 0; i < nsrc; i++) {
358  int s = src_nodes[i];
359 
360  facetedpoly_t srcpoly;
361  sourceMeshWrapper.dual_cell_get_facetization(s, &srcpoly.facetpoints,
362  &srcpoly.points);
363 
364  Weights_t & this_wt = sources_and_weights[ninserted];
365  this_wt.entityID = s;
366  this_wt.weights = intersect_polys_r3d(srcpoly, target_tet_coords,
367  num_tols_);
368 
369  // Increment if vol of intersection > 0; otherwise, allow overwrite
370  if (!this_wt.weights.empty() && this_wt.weights[0] > 0.0)
371  ninserted++;
372  }
373 
374  sources_and_weights.resize(ninserted);
375  return sources_and_weights;
376  }
377 
378 
379  IntersectR3D() = delete;
380 
382  IntersectR3D & operator = (const IntersectR3D &) = delete;
383 
384  private:
385  SourceMeshType const & sourceMeshWrapper;
386  SourceStateType const & sourceStateWrapper;
387  TargetMeshType const & targetMeshWrapper;
388 #ifdef HAVE_TANGRAM
389  std::shared_ptr<InterfaceReconstructor3D> interface_reconstructor;
390 #endif
391  bool rectangular_mesh_ = false;
392  int matid_ = -1;
393  NumericTolerances_t num_tols_ {};
394 }; // class IntersectR3D
395 
396 
397 } // namespace Portage
398 
399 #endif // PORTAGE_INTERSECT_INTERSECT_R3D_H_
Intersection and other tolerances to handle tiny values.
Definition: portage.h:152
Definition: intersect_polys_r3d.h:34
std::vector< double > intersect_polys_r3d(const facetedpoly_t &srcpoly, const std::vector< std::array< Point< 3 >, 4 >> &target_tet_coords, NumericTolerances_t num_tols)
Definition: intersect_polys_r3d.h:72
IntersectR3D & operator=(const IntersectR3D &)=delete
Assignment operator (disabled)
Definition: intersect_r3d.h:56
void set_material(int m)
Set the source mesh material that we have to intersect against.
Definition: intersect_r3d.h:172
void set_material(int m)
Set the source mesh material that we have to intersect against.
Definition: intersect_r3d.h:92
IntersectR3D(SourceMeshType const &source_mesh, SourceStateType const &source_state, TargetMeshType const &target_mesh, NumericTolerances_t num_tols, bool rectangular_mesh=false)
Constructor WITHOUT interface reconstructor.
Definition: intersect_r3d.h:81
void set_material(int m)
Set the source mesh material that we have to intersect against.
Definition: intersect_r3d.h:325
Definition: coredriver.h:42
std::vector< Point< 3 > > points
Definition: intersect_polys_r3d.h:41
IntersectR3D(SourceMeshType const &source_mesh, SourceStateType const &source_state, TargetMeshType const &target_mesh, NumericTolerances_t num_tols, bool rectangular_mesh=false)
Constructor WITHOUT interface reconstructor.
Definition: intersect_r3d.h:314
std::vector< Weights_t > operator()(int tgt_entity, std::vector< int > const &src_entities) const
Intersect a control volume of a target_entity with control volumes of a set of source entities...
Definition: intersect_r3d.h:102
std::vector< std::vector< int > > facetpoints
Definition: intersect_polys_r3d.h:36
IntersectR3D(SourceMeshType const &source_mesh, SourceStateType const &source_state, TargetMeshType const &target_mesh, NumericTolerances_t num_tols, bool rectangular_mesh=false)
Constructor WITHOUT interface reconstructor.
Definition: intersect_r3d.h:160