dune-grid-glue  2.6-git
standardmerge.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
8 #ifndef DUNE_GRIDGLUE_MERGING_STANDARDMERGE_HH
9 #define DUNE_GRIDGLUE_MERGING_STANDARDMERGE_HH
10 
11 
12 #include <iostream>
13 #include <iomanip>
14 #include <vector>
15 #include <stack>
16 #include <set>
17 #include <utility>
18 #include <map>
19 #include <memory>
20 #include <algorithm>
21 
22 #include <dune/common/fvector.hh>
23 #include <dune/common/bitsetvector.hh>
24 #include <dune/common/stdstreams.hh>
25 #include <dune/common/timer.hh>
26 
27 #include <dune/geometry/referenceelements.hh>
28 #include <dune/grid/common/grid.hh>
29 
33 
34 namespace Dune {
35 namespace GridGlue {
36 
53 template<class T, int grid1Dim, int grid2Dim, int dimworld>
55  : public Merger<T,grid1Dim,grid2Dim,dimworld>
56 {
58 
59 public:
60 
61  /* E X P O R T E D T Y P E S A N D C O N S T A N T S */
62 
64  typedef T ctype;
65 
67  using Grid1Coords = typename Base::Grid1Coords;
68 
70  using Grid2Coords = typename Base::Grid2Coords;
71 
73  using WorldCoords = typename Base::WorldCoords;
74 
76 
77 protected:
78 
83 
84  bool valid = false;
85 
89  {}
90 
95  virtual void computeIntersections(const Dune::GeometryType& grid1ElementType,
96  const std::vector<Dune::FieldVector<T,dimworld> >& grid1ElementCorners,
97  std::bitset<(1<<grid1Dim)>& neighborIntersects1,
98  unsigned int grid1Index,
99  const Dune::GeometryType& grid2ElementType,
100  const std::vector<Dune::FieldVector<T,dimworld> >& grid2ElementCorners,
101  std::bitset<(1<<grid2Dim)>& neighborIntersects2,
102  unsigned int grid2Index,
103  std::vector<SimplicialIntersection>& intersections) = 0;
104 
108  bool computeIntersection(unsigned int candidate0, unsigned int candidate1,
109  const std::vector<Dune::FieldVector<T,dimworld> >& grid1Coords,
110  const std::vector<Dune::GeometryType>& grid1_element_types,
111  std::bitset<(1<<grid1Dim)>& neighborIntersects1,
112  const std::vector<Dune::FieldVector<T,dimworld> >& grid2Coords,
113  const std::vector<Dune::GeometryType>& grid2_element_types,
114  std::bitset<(1<<grid2Dim)>& neighborIntersects2,
115  bool insert = true);
116 
117  /* M E M B E R V A R I A B L E S */
118 
119  std::shared_ptr<IntersectionListProvider> intersectionListProvider_;
120  std::shared_ptr<IntersectionList> intersectionList_;
121 
123  std::vector<std::vector<unsigned int> > grid1ElementCorners_;
124  std::vector<std::vector<unsigned int> > grid2ElementCorners_;
125 
126  std::vector<std::vector<int> > elementNeighbors1_;
127  std::vector<std::vector<int> > elementNeighbors2_;
128 
129 public:
130 
131  /* C O N C E P T I M P L E M E N T I N G I N T E R F A C E */
132 
136  void build(const std::vector<Dune::FieldVector<T,dimworld> >& grid1_Coords,
137  const std::vector<unsigned int>& grid1_elements,
138  const std::vector<Dune::GeometryType>& grid1_element_types,
139  const std::vector<Dune::FieldVector<T,dimworld> >& grid2_coords,
140  const std::vector<unsigned int>& grid2_elements,
141  const std::vector<Dune::GeometryType>& grid2_element_types) override;
142 
143 
144  /* P R O B I N G T H E M E R G E D G R I D */
145 
146  void clear() override
147  {
148  // Delete old internal data, from a possible previous run
149  intersectionListProvider_->clear();
150  purge(grid1ElementCorners_);
151  purge(grid2ElementCorners_);
152 
153  valid = false;
154  }
155 
156  std::shared_ptr<IntersectionList> intersectionList() const final
157  {
158  assert(valid);
159  return intersectionList_;
160  }
161 
162  void enableFallback(bool fallback)
163  {
164  m_enableFallback = fallback;
165  }
166 
167  void enableBruteForce(bool bruteForce)
168  {
169  m_enableBruteForce = bruteForce;
170  }
171 
172 private:
176  bool m_enableFallback = false;
177 
181  bool m_enableBruteForce = false;
182 
183  auto& intersections()
184  { return intersectionListProvider_->intersections(); }
185 
187  template<typename V>
188  static void purge(V & v)
189  {
190  v.clear();
191  V v2(v);
192  v.swap(v2);
193  }
194 
199  void generateSeed(std::vector<int>& seeds,
200  Dune::BitSetVector<1>& isHandled2,
201  std::stack<unsigned>& candidates2,
202  const std::vector<Dune::FieldVector<T, dimworld> >& grid1Coords,
203  const std::vector<Dune::GeometryType>& grid1_element_types,
204  const std::vector<Dune::FieldVector<T, dimworld> >& grid2Coords,
205  const std::vector<Dune::GeometryType>& grid2_element_types);
206 
210  int insertIntersections(unsigned int candidate1, unsigned int candidate2,std::vector<SimplicialIntersection>& intersections);
211 
215  int bruteForceSearch(int candidate1,
216  const std::vector<Dune::FieldVector<T,dimworld> >& grid1Coords,
217  const std::vector<Dune::GeometryType>& grid1_element_types,
218  const std::vector<Dune::FieldVector<T,dimworld> >& grid2Coords,
219  const std::vector<Dune::GeometryType>& grid2_element_types);
220 
224  std::pair<bool, unsigned int>
225  intersectionIndex(unsigned int grid1Index, unsigned int grid2Index,
226  SimplicialIntersection& intersection);
227 
231  template <int gridDim>
232  void computeNeighborsPerElement(const std::vector<Dune::GeometryType>& gridElementTypes,
233  const std::vector<std::vector<unsigned int> >& gridElementCorners,
234  std::vector<std::vector<int> >& elementNeighbors);
235 
236  void buildAdvancingFront(
237  const std::vector<Dune::FieldVector<T,dimworld> >& grid1_Coords,
238  const std::vector<unsigned int>& grid1_elements,
239  const std::vector<Dune::GeometryType>& grid1_element_types,
240  const std::vector<Dune::FieldVector<T,dimworld> >& grid2_coords,
241  const std::vector<unsigned int>& grid2_elements,
242  const std::vector<Dune::GeometryType>& grid2_element_types
243  );
244 
245  void buildBruteForce(
246  const std::vector<Dune::FieldVector<T,dimworld> >& grid1_Coords,
247  const std::vector<unsigned int>& grid1_elements,
248  const std::vector<Dune::GeometryType>& grid1_element_types,
249  const std::vector<Dune::FieldVector<T,dimworld> >& grid2_coords,
250  const std::vector<unsigned int>& grid2_elements,
251  const std::vector<Dune::GeometryType>& grid2_element_types
252  );
253 };
254 
255 
256 /* IMPLEMENTATION */
257 
258 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
259 bool StandardMerge<T,grid1Dim,grid2Dim,dimworld>::computeIntersection(unsigned int candidate0, unsigned int candidate1,
260  const std::vector<Dune::FieldVector<T,dimworld> >& grid1Coords,
261  const std::vector<Dune::GeometryType>& grid1_element_types,
262  std::bitset<(1<<grid1Dim)>& neighborIntersects1,
263  const std::vector<Dune::FieldVector<T,dimworld> >& grid2Coords,
264  const std::vector<Dune::GeometryType>& grid2_element_types,
265  std::bitset<(1<<grid2Dim)>& neighborIntersects2,
266  bool insert)
267 {
268  // Select vertices of the grid1 element
269  int grid1NumVertices = grid1ElementCorners_[candidate0].size();
270  std::vector<Dune::FieldVector<T,dimworld> > grid1ElementCorners(grid1NumVertices);
271  for (int i=0; i<grid1NumVertices; i++)
272  grid1ElementCorners[i] = grid1Coords[grid1ElementCorners_[candidate0][i]];
273 
274  // Select vertices of the grid2 element
275  int grid2NumVertices = grid2ElementCorners_[candidate1].size();
276  std::vector<Dune::FieldVector<T,dimworld> > grid2ElementCorners(grid2NumVertices);
277  for (int i=0; i<grid2NumVertices; i++)
278  grid2ElementCorners[i] = grid2Coords[grid2ElementCorners_[candidate1][i]];
279 
280  // ///////////////////////////////////////////////////////
281  // Compute the intersection between the two elements
282  // ///////////////////////////////////////////////////////
283 
284  std::vector<SimplicialIntersection> intersections(0);
285 
286  // compute the intersections
287  computeIntersections(grid1_element_types[candidate0], grid1ElementCorners,
288  neighborIntersects1, candidate0,
289  grid2_element_types[candidate1], grid2ElementCorners,
290  neighborIntersects2, candidate1,
291  intersections);
292 
293  // insert intersections if needed
294  if(insert && !intersections.empty())
295  insertIntersections(candidate0,candidate1,intersections);
296 
297  // Have we found an intersection?
298  return !intersections.empty() || neighborIntersects1.any() || neighborIntersects2.any();
299 
300 }
301 
302 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
304  const std::vector<Dune::FieldVector<T,dimworld> >& grid1Coords,
305  const std::vector<Dune::GeometryType>& grid1_element_types,
306  const std::vector<Dune::FieldVector<T,dimworld> >& grid2Coords,
307  const std::vector<Dune::GeometryType>& grid2_element_types)
308 {
309  std::bitset<(1<<grid1Dim)> neighborIntersects1;
310  std::bitset<(1<<grid2Dim)> neighborIntersects2;
311  for (std::size_t i=0; i<grid1_element_types.size(); i++) {
312 
313  bool intersectionFound = computeIntersection(i, candidate1,
314  grid1Coords, grid1_element_types, neighborIntersects1,
315  grid2Coords, grid2_element_types, neighborIntersects2,
316  false);
317 
318  // if there is an intersection, i is our new seed candidate on the grid1 side
319  if (intersectionFound)
320  return i;
321 
322  }
323 
324  return -1;
325 }
326 
327 
328 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
329 template<int gridDim>
330 void StandardMerge<T,grid1Dim,grid2Dim,dimworld>::
331 computeNeighborsPerElement(const std::vector<Dune::GeometryType>& gridElementTypes,
332  const std::vector<std::vector<unsigned int> >& gridElementCorners,
333  std::vector<std::vector<int> >& elementNeighbors)
334 {
335  typedef std::vector<unsigned int> FaceType;
336  typedef std::map<FaceType, std::pair<unsigned int, unsigned int> > FaceSetType;
337 
339  // First: grid 1
341  FaceSetType faces;
342  elementNeighbors.resize(gridElementTypes.size());
343 
344  for (size_t i=0; i<gridElementTypes.size(); i++)
345  elementNeighbors[i].resize(Dune::ReferenceElements<T,gridDim>::general(gridElementTypes[i]).size(1), -1);
346 
347  for (size_t i=0; i<gridElementTypes.size(); i++) { //iterate over all elements
348  const auto& refElement = Dune::ReferenceElements<T,gridDim>::general(gridElementTypes[i]);
349 
350  for (size_t j=0; j<(size_t)refElement.size(1); j++) { // iterate over all faces of the element
351 
352  FaceType face;
353  // extract element face
354  for (size_t k=0; k<(size_t)refElement.size(j,1,gridDim); k++)
355  face.push_back(gridElementCorners[i][refElement.subEntity(j,1,k,gridDim)]);
356 
357  // sort the face vertices to get rid of twists and other permutations
358  std::sort(face.begin(), face.end());
359 
360  typename FaceSetType::iterator faceHandle = faces.find(face);
361 
362  if (faceHandle == faces.end()) {
363 
364  // face has not been visited before
365  faces.insert(std::make_pair(face, std::make_pair(i,j)));
366 
367  } else {
368 
369  // face has been visited before: store the mutual neighbor information
370  elementNeighbors[i][j] = faceHandle->second.first;
371  elementNeighbors[faceHandle->second.first][faceHandle->second.second] = i;
372 
373  faces.erase(faceHandle);
374 
375  }
376 
377  }
378 
379  }
380 }
381 
382 // /////////////////////////////////////////////////////////////////////
383 // Compute the intersection of all pairs of elements
384 // Linear algorithm by Gander and Japhet, Proc. of DD18
385 // /////////////////////////////////////////////////////////////////////
386 
387 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
388 void StandardMerge<T,grid1Dim,grid2Dim,dimworld>::build(const std::vector<Dune::FieldVector<T,dimworld> >& grid1Coords,
389  const std::vector<unsigned int>& grid1_elements,
390  const std::vector<Dune::GeometryType>& grid1_element_types,
391  const std::vector<Dune::FieldVector<T,dimworld> >& grid2Coords,
392  const std::vector<unsigned int>& grid2_elements,
393  const std::vector<Dune::GeometryType>& grid2_element_types
394  )
395 {
396 
397  std::cout << "StandardMerge building merged grid..." << std::endl;
398  Dune::Timer watch;
399 
400  clear();
401  // clear global intersection list
402  intersectionListProvider_->clear();
403  this->counter = 0;
404 
405  // /////////////////////////////////////////////////////////////////////
406  // Copy element corners into a data structure with block-structure.
407  // This is not as efficient but a lot easier to use.
408  // We may think about efficiency later.
409  // /////////////////////////////////////////////////////////////////////
410 
411  // first the grid1 side
412  grid1ElementCorners_.resize(grid1_element_types.size());
413 
414  unsigned int grid1CornerCounter = 0;
415 
416  for (std::size_t i=0; i<grid1_element_types.size(); i++) {
417 
418  // Select vertices of the grid1 element
419  int numVertices = Dune::ReferenceElements<T,grid1Dim>::general(grid1_element_types[i]).size(grid1Dim);
420  grid1ElementCorners_[i].resize(numVertices);
421  for (int j=0; j<numVertices; j++)
422  grid1ElementCorners_[i][j] = grid1_elements[grid1CornerCounter++];
423 
424  }
425 
426  // then the grid2 side
427  grid2ElementCorners_.resize(grid2_element_types.size());
428 
429  unsigned int grid2CornerCounter = 0;
430 
431  for (std::size_t i=0; i<grid2_element_types.size(); i++) {
432 
433  // Select vertices of the grid2 element
434  int numVertices = Dune::ReferenceElements<T,grid2Dim>::general(grid2_element_types[i]).size(grid2Dim);
435  grid2ElementCorners_[i].resize(numVertices);
436  for (int j=0; j<numVertices; j++)
437  grid2ElementCorners_[i][j] = grid2_elements[grid2CornerCounter++];
438 
439  }
440 
442  // Compute the face neighbors for each element
444 
445  computeNeighborsPerElement<grid1Dim>(grid1_element_types, grid1ElementCorners_, elementNeighbors1_);
446  computeNeighborsPerElement<grid2Dim>(grid2_element_types, grid2ElementCorners_, elementNeighbors2_);
447 
448  std::cout << "setup took " << watch.elapsed() << " seconds." << std::endl;
449 
450  if (m_enableBruteForce)
451  buildBruteForce(grid1Coords, grid1_elements, grid1_element_types, grid2Coords, grid2_elements, grid2_element_types);
452  else
453  buildAdvancingFront(grid1Coords, grid1_elements, grid1_element_types, grid2Coords, grid2_elements, grid2_element_types);
454 
455  valid = true;
456  std::cout << "intersection construction took " << watch.elapsed() << " seconds." << std::endl;
457 }
458 
459 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
461  const std::vector<Dune::FieldVector<T,dimworld> >& grid1Coords,
462  const std::vector<unsigned int>& grid1_elements,
463  const std::vector<Dune::GeometryType>& grid1_element_types,
464  const std::vector<Dune::FieldVector<T,dimworld> >& grid2Coords,
465  const std::vector<unsigned int>& grid2_elements,
466  const std::vector<Dune::GeometryType>& grid2_element_types
467  )
468 {
470  // Data structures for the advancing-front algorithm
472 
473  std::stack<unsigned int> candidates1;
474  std::stack<unsigned int> candidates2;
475 
476  std::vector<int> seeds(grid2_element_types.size(), -1);
477 
478  // /////////////////////////////////////////////////////////////////////
479  // Do a brute-force search to find one pair of intersecting elements
480  // to start the advancing-front type algorithm with.
481  // /////////////////////////////////////////////////////////////////////
482 
483  // Set flag if element has been handled
484  Dune::BitSetVector<1> isHandled2(grid2_element_types.size());
485 
486  // Set flag if the element has been entered in the queue
487  Dune::BitSetVector<1> isCandidate2(grid2_element_types.size());
488 
489  generateSeed(seeds, isHandled2, candidates2, grid1Coords, grid1_element_types, grid2Coords, grid2_element_types);
490 
491  // /////////////////////////////////////////////////////////////////////
492  // Main loop
493  // /////////////////////////////////////////////////////////////////////
494 
495  std::set<unsigned int> isHandled1;
496 
497  std::set<unsigned int> isCandidate1;
498 
499  while (!candidates2.empty()) {
500 
501  // Get the next element on the grid2 side
502  unsigned int currentCandidate2 = candidates2.top();
503  int seed = seeds[currentCandidate2];
504  assert(seed >= 0);
505 
506  candidates2.pop();
507  isHandled2[currentCandidate2] = true;
508 
509  // Start advancing front algorithm on the grid1 side from the 'seed' element that
510  // we stored along with the current grid2 element
511  candidates1.push(seed);
512 
513  isHandled1.clear();
514  isCandidate1.clear();
515 
516  while (!candidates1.empty()) {
517 
518  unsigned int currentCandidate1 = candidates1.top();
519  candidates1.pop();
520  isHandled1.insert(currentCandidate1);
521 
522  // Test whether there is an intersection between currentCandidate0 and currentCandidate1
523  std::bitset<(1<<grid1Dim)> neighborIntersects1;
524  std::bitset<(1<<grid2Dim)> neighborIntersects2;
525  bool intersectionFound = computeIntersection(currentCandidate1, currentCandidate2,
526  grid1Coords,grid1_element_types, neighborIntersects1,
527  grid2Coords,grid2_element_types, neighborIntersects2);
528 
529  for (size_t i=0; i<neighborIntersects2.size(); i++)
530  if (neighborIntersects2[i] && elementNeighbors2_[currentCandidate2][i] != -1)
531  seeds[elementNeighbors2_[currentCandidate2][i]] = currentCandidate1;
532 
533  // add neighbors of candidate0 to the list of elements to be checked
534  if (intersectionFound) {
535 
536  for (size_t i=0; i<elementNeighbors1_[currentCandidate1].size(); i++) {
537 
538  int neighbor = elementNeighbors1_[currentCandidate1][i];
539 
540  if (neighbor == -1) // do nothing at the grid boundary
541  continue;
542 
543  if (isHandled1.find(neighbor) == isHandled1.end()
544  && isCandidate1.find(neighbor) == isCandidate1.end()) {
545  candidates1.push(neighbor);
546  isCandidate1.insert(neighbor);
547  }
548 
549  }
550 
551  }
552 
553  }
554 
555  // We have now found all intersections of elements in the grid1 side with currentCandidate2
556  // Now we add all neighbors of currentCandidate2 that have not been treated yet as new
557  // candidates.
558 
559  // Do we have an unhandled neighbor with a seed?
560  bool seedFound = !candidates2.empty();
561  for (size_t i=0; i<elementNeighbors2_[currentCandidate2].size(); i++) {
562 
563  int neighbor = elementNeighbors2_[currentCandidate2][i];
564 
565  if (neighbor == -1) // do nothing at the grid boundary
566  continue;
567 
568  // Add all unhandled intersecting neighbors to the queue
569  if (!isHandled2[neighbor][0] && !isCandidate2[neighbor][0] && seeds[neighbor]>-1) {
570 
571  isCandidate2[neighbor][0] = true;
572  candidates2.push(neighbor);
573  seedFound = true;
574  }
575  }
576 
577  if (seedFound || !m_enableFallback)
578  continue;
579 
580  // There is no neighbor with a seed, so we need to be a bit more aggressive...
581  // get all neighbors of currentCandidate2, but not currentCandidate2 itself
582  for (size_t i=0; i<elementNeighbors2_[currentCandidate2].size(); i++) {
583 
584  int neighbor = elementNeighbors2_[currentCandidate2][i];
585 
586  if (neighbor == -1) // do nothing at the grid boundary
587  continue;
588 
589  if (!isHandled2[neighbor][0] && !isCandidate2[neighbor][0]) {
590 
591  // Get a seed element for the new grid2 element
592  // Look for an element on the grid1 side that intersects the new grid2 element.
593  int seed = -1;
594 
595  // Look among the ones that have been tested during the last iteration.
596  for (typename std::set<unsigned int>::iterator seedIt = isHandled1.begin();
597  seedIt != isHandled1.end(); ++seedIt) {
598 
599  std::bitset<(1<<grid1Dim)> neighborIntersects1;
600  std::bitset<(1<<grid2Dim)> neighborIntersects2;
601  bool intersectionFound = computeIntersection(*seedIt, neighbor,
602  grid1Coords, grid1_element_types, neighborIntersects1,
603  grid2Coords, grid2_element_types, neighborIntersects2,
604  false);
605 
606  // if the intersection is nonempty, *seedIt is our new seed candidate on the grid1 side
607  if (intersectionFound) {
608  seed = *seedIt;
609  Dune::dwarn << "Algorithm entered first fallback method and found a new seed in the build algorithm." <<
610  "Probably, the neighborIntersects bitsets computed in computeIntersection specialization is wrong." << std::endl;
611  break;
612  }
613 
614  }
615 
616  if (seed < 0) {
617  // The fast method didn't find a grid1 element that intersects with
618  // the new grid2 candidate. We have to do a brute-force search.
619  seed = bruteForceSearch(neighbor,
620  grid1Coords,grid1_element_types,
621  grid2Coords,grid2_element_types);
622  Dune::dwarn << "Algorithm entered second fallback method. This probably should not happen." << std::endl;
623 
624  }
625 
626  // We have tried all we could: the candidate is 'handled' now
627  isCandidate2[neighbor] = true;
628 
629  // still no seed? Then the new grid2 candidate isn't overlapped by anything
630  if (seed < 0)
631  continue;
632 
633  // we have a seed now
634  candidates2.push(neighbor);
635  seeds[neighbor] = seed;
636  seedFound = true;
637 
638  }
639 
640  }
641 
642  /* Do a brute-force search if there is still no seed:
643  * There might still be a disconnected region out there.
644  */
645  if (!seedFound && candidates2.empty()) {
646  generateSeed(seeds, isHandled2, candidates2, grid1Coords, grid1_element_types, grid2Coords, grid2_element_types);
647  }
648  }
649 }
650 
651 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
652 void StandardMerge<T,grid1Dim,grid2Dim,dimworld>::buildBruteForce(
653  const std::vector<Dune::FieldVector<T,dimworld> >& grid1Coords,
654  const std::vector<unsigned int>& grid1_elements,
655  const std::vector<Dune::GeometryType>& grid1_element_types,
656  const std::vector<Dune::FieldVector<T,dimworld> >& grid2Coords,
657  const std::vector<unsigned int>& grid2_elements,
658  const std::vector<Dune::GeometryType>& grid2_element_types
659  )
660 {
661  std::bitset<(1<<grid1Dim)> neighborIntersects1;
662  std::bitset<(1<<grid2Dim)> neighborIntersects2;
663 
664  for (unsigned i = 0; i < grid1_element_types.size(); ++i) {
665  for (unsigned j = 0; j < grid2_element_types.size(); ++j) {
666  (void) computeIntersection(i, j,
667  grid1Coords, grid1_element_types, neighborIntersects1,
668  grid2Coords, grid2_element_types, neighborIntersects2);
669  }
670  }
671 }
672 
673 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
674 void StandardMerge<T,grid1Dim,grid2Dim,dimworld>::generateSeed(std::vector<int>& seeds, Dune::BitSetVector<1>& isHandled2, std::stack<unsigned>& candidates2, const std::vector<Dune::FieldVector<T, dimworld> >& grid1Coords, const std::vector<Dune::GeometryType>& grid1_element_types, const std::vector<Dune::FieldVector<T, dimworld> >& grid2Coords, const std::vector<Dune::GeometryType>& grid2_element_types)
675 {
676  for (std::size_t j=0; j<grid2_element_types.size(); j++) {
677 
678  if (seeds[j] > 0 || isHandled2[j][0])
679  continue;
680 
681  int seed = bruteForceSearch(j,grid1Coords,grid1_element_types,grid2Coords,grid2_element_types);
682 
683  if (seed >= 0) {
684  candidates2.push(j); // the candidate and a seed for the candidate
685  seeds[j] = seed;
686  break;
687  } else // If the brute force search did not find any intersection we can skip this element
688  isHandled2[j] = true;
689  }
690 }
691 
692 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
693 int StandardMerge<T,grid1Dim,grid2Dim,dimworld>::insertIntersections(unsigned int candidate1, unsigned int candidate2,
694  std::vector<SimplicialIntersection>& intersections)
695 {
696  typedef typename std::vector<SimplicialIntersection>::size_type size_t;
697  int count = 0;
698 
699  for (size_t i = 0; i < intersections.size(); ++i) {
700  // get the intersection index of the current intersection from intersections in this->intersections
701  bool found;
702  unsigned int index;
703  std::tie(found, index) = intersectionIndex(candidate1,candidate2,intersections[i]);
704 
705  if (found && index >= this->intersections().size()) { //the intersection is not yet contained in this->intersections
706  this->intersections().push_back(intersections[i]); // insert
707 
708  ++count;
709  } else if (found) {
710  auto& intersection = this->intersections()[index];
711 
712  // insert each grid1 element and local representation of intersections[i] with parent candidate1
713  for (size_t j = 0; j < intersections[i].parents0.size(); ++j) {
714  intersection.parents0.push_back(candidate1);
715  intersection.corners0.push_back(intersections[i].corners0[j]);
716  }
717 
718  // insert each grid2 element and local representation of intersections[i] with parent candidate2
719  for (size_t j = 0; j < intersections[i].parents1.size(); ++j) {
720  intersection.parents1.push_back(candidate2);
721  intersection.corners1.push_back(intersections[i].corners1[j]);
722  }
723 
724  ++count;
725  } else {
726  Dune::dwarn << "Computed the same intersection twice!" << std::endl;
727  }
728  }
729  return count;
730 }
731 
732 template<typename T, int grid1Dim, int grid2Dim, int dimworld>
733 std::pair<bool, unsigned int>
734 StandardMerge<T,grid1Dim,grid2Dim,dimworld>::intersectionIndex(unsigned int grid1Index, unsigned int grid2Index,
735  SimplicialIntersection& intersection) {
736 
737 
738  // return index in intersections_ if at least one local representation of a Simplicial Intersection (SI)
739  // of intersections_ is equal to the local representation of one element in intersections
740 
741  std::size_t n_intersections = this->intersections().size();
742  if (grid1Dim == grid2Dim)
743  return {true, n_intersections};
744 
745  T eps = 1e-10;
746 
747  for (std::size_t i = 0; i < n_intersections; ++i) {
748 
749  // compare the local representation of the subelements of the SI
750  for (std::size_t ei = 0; ei < this->intersections()[i].parents0.size(); ++ei) // merger subelement
751  {
752  if (this->intersections()[i].parents0[ei] == grid1Index)
753  {
754  for (std::size_t er = 0; er < intersection.parents0.size(); ++er) // list subelement
755  {
756  bool found_all = true;
757  // compare the local coordinate representations
758  for (std::size_t ci = 0; ci < this->intersections()[i].corners0[ei].size(); ++ci)
759  {
760  Dune::FieldVector<T,grid1Dim> ni = this->intersections()[i].corners0[ei][ci];
761  bool found_ni = false;
762  for (std::size_t cr = 0; cr < intersection.corners0[er].size(); ++cr)
763  {
764  Dune::FieldVector<T,grid1Dim> nr = intersection.corners0[er][cr];
765 
766  found_ni = found_ni || ((ni-nr).infinity_norm() < eps);
767  if (found_ni)
768  break;
769  }
770  found_all = found_all && found_ni;
771 
772  if (!found_ni)
773  break;
774  }
775 
776  if (found_all && (this->intersections()[i].parents1[ei] != grid2Index))
777  return {true, i};
778  else if (found_all)
779  return {false, 0};
780  }
781  }
782  }
783 
784  // compare the local representation of the subelements of the SI
785  for (std::size_t ei = 0; ei < this->intersections()[i].parents1.size(); ++ei) // merger subelement
786  {
787  if (this->intersections()[i].parents1[ei] == grid2Index)
788  {
789  for (std::size_t er = 0; er < intersection.parents1.size(); ++er) // list subelement
790  {
791  bool found_all = true;
792  // compare the local coordinate representations
793  for (std::size_t ci = 0; ci < this->intersections()[i].corners1[ei].size(); ++ci)
794  {
795  Dune::FieldVector<T,grid2Dim> ni = this->intersections()[i].corners1[ei][ci];
796  bool found_ni = false;
797  for (std::size_t cr = 0; cr < intersection.corners1[er].size(); ++cr)
798  {
799  Dune::FieldVector<T,grid2Dim> nr = intersection.corners1[er][cr];
800  found_ni = found_ni || ((ni-nr).infinity_norm() < eps);
801 
802  if (found_ni)
803  break;
804  }
805  found_all = found_all && found_ni;
806 
807  if (!found_ni)
808  break;
809  }
810 
811  if (found_all && (this->intersections()[i].parents0[ei] != grid1Index))
812  return {true, i};
813  else if (found_all)
814  return {false, 0};
815  }
816  }
817  }
818  }
819 
820  return {true, n_intersections};
821 }
822 
823 #define DECL extern
824 #define STANDARD_MERGE_INSTANTIATE(T,A,B,C) \
825  DECL template \
826  void StandardMerge<T,A,B,C>::build(const std::vector<Dune::FieldVector<T,C> >& grid1Coords, \
827  const std::vector<unsigned int>& grid1_elements, \
828  const std::vector<Dune::GeometryType>& grid1_element_types, \
829  const std::vector<Dune::FieldVector<T,C> >& grid2Coords, \
830  const std::vector<unsigned int>& grid2_elements, \
831  const std::vector<Dune::GeometryType>& grid2_element_types \
832  )
833 
834 STANDARD_MERGE_INSTANTIATE(double,1,1,1);
835 STANDARD_MERGE_INSTANTIATE(double,2,2,2);
836 STANDARD_MERGE_INSTANTIATE(double,3,3,3);
837 #undef STANDARD_MERGE_INSTANTIATE
838 #undef DECL
839 
840 } /* namespace GridGlue */
841 } /* namespace Dune */
842 
843 #endif // DUNE_GRIDGLUE_MERGING_STANDARDMERGE_HH
Dune::GridGlue::StandardMerge::elementNeighbors2_
std::vector< std::vector< int > > elementNeighbors2_
Definition: standardmerge.hh:127
Dune
Definition: gridglue.hh:35
Dune::GridGlue::IntersectionList
Definition: intersectionlist.hh:131
Dune::GridGlue::StandardMerge::intersectionList
std::shared_ptr< IntersectionList > intersectionList() const final
Definition: standardmerge.hh:156
Dune::GridGlue::StandardMerge
Common base class for many merger implementations: produce pairs of entities that may intersect.
Definition: standardmerge.hh:54
Dune::GridGlue::StandardMerge::grid1ElementCorners_
std::vector< std::vector< unsigned int > > grid1ElementCorners_
Temporary internal data.
Definition: standardmerge.hh:123
intersectionlist.hh
Dune::GridGlue::StandardMerge::enableFallback
void enableFallback(bool fallback)
Definition: standardmerge.hh:162
Dune::GridGlue::Merger< double, grid1Dim, grid2Dim, dimworld >::Grid1Coords
Dune::FieldVector< double, grid1Dim > Grid1Coords
the local coordinate type for the grid1 coordinates
Definition: merger.hh:29
Dune::GridGlue::SimplicialIntersectionListProvider::SimplicialIntersection
Definition: intersectionlist.hh:219
merger.hh
Dune::GridGlue::intersections
IteratorRange<... > intersections(const GridGlue<... > &glue, const Reverse<... > &reverse=!reversed)
Iterate over all intersections of a GridGlue.
Dune::GridGlue::StandardMerge::intersectionList_
std::shared_ptr< IntersectionList > intersectionList_
Definition: standardmerge.hh:120
Dune::GridGlue::StandardMerge::computeIntersections
virtual void computeIntersections(const Dune::GeometryType &grid1ElementType, const std::vector< Dune::FieldVector< T, dimworld > > &grid1ElementCorners, std::bitset<(1<< grid1Dim)> &neighborIntersects1, unsigned int grid1Index, const Dune::GeometryType &grid2ElementType, const std::vector< Dune::FieldVector< T, dimworld > > &grid2ElementCorners, std::bitset<(1<< grid2Dim)> &neighborIntersects2, unsigned int grid2Index, std::vector< SimplicialIntersection > &intersections)=0
Compute the intersection between two overlapping elements.
Dune::GridGlue::StandardMerge::grid2ElementCorners_
std::vector< std::vector< unsigned int > > grid2ElementCorners_
Definition: standardmerge.hh:124
Dune::GridGlue::Merger< double, grid1Dim, grid2Dim, dimworld >::Grid2Coords
Dune::FieldVector< double, grid2Dim > Grid2Coords
the local coordinate type for the grid2 coordinates
Definition: merger.hh:32
Dune::GridGlue::StandardMerge::intersectionListProvider_
std::shared_ptr< IntersectionListProvider > intersectionListProvider_
Definition: standardmerge.hh:119
Dune::GridGlue::StandardMerge< double, dim, dim, dimworld >::SimplicialIntersection
typename IntersectionListProvider::SimplicialIntersection SimplicialIntersection
Definition: standardmerge.hh:81
Dune::GridGlue::StandardMerge::elementNeighbors1_
std::vector< std::vector< int > > elementNeighbors1_
Definition: standardmerge.hh:126
computeintersection.hh
Dune::GridGlue::Merger< T, grid1Dim, grid2Dim, dimworld >::IntersectionList
Dune::GridGlue::IntersectionList< Grid1Coords, Grid2Coords > IntersectionList
Definition: merger.hh:37
Dune::GridGlue::StandardMerge::valid
bool valid
Definition: standardmerge.hh:84
Dune::GridGlue::STANDARD_MERGE_INSTANTIATE
STANDARD_MERGE_INSTANTIATE(double, 1, 1, 1)
Dune::GridGlue::StandardMerge::StandardMerge
StandardMerge()
Definition: standardmerge.hh:86
Dune::GridGlue::StandardMerge::ctype
T ctype
the numeric type used in this interface
Definition: standardmerge.hh:64
Dune::GridGlue::Merger< double, grid1Dim, grid2Dim, dimworld >::WorldCoords
Dune::FieldVector< double, dimworld > WorldCoords
the coordinate type used in this interface
Definition: merger.hh:35
Dune::GridGlue::StandardMerge::enableBruteForce
void enableBruteForce(bool bruteForce)
Definition: standardmerge.hh:167
Dune::GridGlue::Merger
Abstract base for all classes that take extracted grids and build sets of intersections.
Definition: merger.hh:24
Dune::GridGlue::StandardMerge::computeIntersection
bool computeIntersection(unsigned int candidate0, unsigned int candidate1, const std::vector< Dune::FieldVector< T, dimworld > > &grid1Coords, const std::vector< Dune::GeometryType > &grid1_element_types, std::bitset<(1<< grid1Dim)> &neighborIntersects1, const std::vector< Dune::FieldVector< T, dimworld > > &grid2Coords, const std::vector< Dune::GeometryType > &grid2_element_types, std::bitset<(1<< grid2Dim)> &neighborIntersects2, bool insert=true)
Compute the intersection between two overlapping elements.
Definition: standardmerge.hh:259
Dune::GridGlue::StandardMerge< double, dim, dim, dimworld >::RemoteSimplicialIntersection
SimplicialIntersection RemoteSimplicialIntersection
Definition: standardmerge.hh:82
Dune::GridGlue::StandardMerge::clear
void clear() override
Definition: standardmerge.hh:146
Dune::GridGlue::SimplicialIntersectionListProvider
Definition: intersectionlist.hh:203
Dune::GridGlue::StandardMerge::build
void build(const std::vector< Dune::FieldVector< T, dimworld > > &grid1_Coords, const std::vector< unsigned int > &grid1_elements, const std::vector< Dune::GeometryType > &grid1_element_types, const std::vector< Dune::FieldVector< T, dimworld > > &grid2_coords, const std::vector< unsigned int > &grid2_elements, const std::vector< Dune::GeometryType > &grid2_element_types) override
builds the merged grid
Definition: standardmerge.hh:388