You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.6 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2004-2012 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /** @file gmm_domain_decomp.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date May 21, 2004.
  29. @brief Domain decomposition.
  30. */
  31. #ifndef GMM_DOMAIN_DECOMP_H__
  32. #define GMM_DOMAIN_DECOMP_H__
  33. #include "gmm_kernel.h"
  34. #include <map>
  35. namespace gmm {
  36. /** This function separates into small boxes of size msize with a ratio
  37. * of overlap (in [0,1[) a set of points. The result is given into a
  38. * vector of sparse matrices vB.
  39. */
  40. template <typename Matrix, typename Point>
  41. void rudimentary_regular_decomposition(std::vector<Point> pts,
  42. double msize,
  43. double overlap,
  44. std::vector<Matrix> &vB) {
  45. typedef typename linalg_traits<Matrix>::value_type value_type;
  46. typedef abstract_null_type void_type;
  47. typedef std::map<size_type, void_type> map_type;
  48. size_type nbpts = pts.size();
  49. if (!nbpts || pts[0].size() == 0) { vB.resize(0); return; }
  50. int dim = int(pts[0].size());
  51. // computation of the global box and the number of sub-domains
  52. Point pmin = pts[0], pmax = pts[0];
  53. for (size_type i = 1; i < nbpts; ++i)
  54. for (int k = 0; k < dim; ++k) {
  55. pmin[k] = std::min(pmin[k], pts[i][k]);
  56. pmax[k] = std::max(pmax[k], pts[i][k]);
  57. }
  58. std::vector<size_type> nbsub(dim), mult(dim);
  59. std::vector<int> pts1(dim), pts2(dim);
  60. size_type nbtotsub = 1;
  61. for (int k = 0; k < dim; ++k) {
  62. nbsub[k] = size_type((pmax[k] - pmin[k]) / msize)+1;
  63. mult[k] = nbtotsub; nbtotsub *= nbsub[k];
  64. }
  65. std::vector<map_type> subs(nbtotsub);
  66. // points ventilation
  67. std::vector<size_type> ns(dim), na(dim), nu(dim);
  68. for (size_type i = 0; i < nbpts; ++i) {
  69. for (int k = 0; k < dim; ++k) {
  70. register double a = (pts[i][k] - pmin[k]) / msize;
  71. ns[k] = size_type(a) - 1; na[k] = 0;
  72. pts1[k] = int(a + overlap); pts2[k] = int(ceil(a-1.0-overlap));
  73. }
  74. size_type sum = 0;
  75. do {
  76. bool ok = 1;
  77. for (int k = 0; k < dim; ++k)
  78. if ((ns[k] >= nbsub[k]) || (pts1[k] < int(ns[k]))
  79. || (pts2[k] > int(ns[k]))) { ok = false; break; }
  80. if (ok) {
  81. size_type ind = ns[0];
  82. for (int k=1; k < dim; ++k) ind += ns[k]*mult[k];
  83. subs[ind][i] = void_type();
  84. }
  85. for (int k = 0; k < dim; ++k) {
  86. if (na[k] < 2) { na[k]++; ns[k]++; ++sum; break; }
  87. na[k] = 0; ns[k] -= 2; sum -= 2;
  88. }
  89. } while (sum);
  90. }
  91. // delete too small domains.
  92. size_type nbmaxinsub = 0;
  93. for (size_type i = 0; i < nbtotsub; ++i)
  94. nbmaxinsub = std::max(nbmaxinsub, subs[i].size());
  95. std::fill(ns.begin(), ns.end(), size_type(0));
  96. for (size_type i = 0; i < nbtotsub; ++i) {
  97. if (subs[i].size() > 0 && subs[i].size() < nbmaxinsub / 10) {
  98. for (int k = 0; k < dim; ++k) nu[k] = ns[k];
  99. size_type nbmax = 0, imax = 0;
  100. for (int l = 0; l < dim; ++l) {
  101. nu[l]--;
  102. for (int m = 0; m < 2; ++m, nu[l]+=2) {
  103. bool ok = true;
  104. for (int k = 0; k < dim && ok; ++k)
  105. if (nu[k] >= nbsub[k]) ok = false;
  106. if (ok) {
  107. size_type ind = ns[0];
  108. for (int k=1; k < dim; ++k) ind += ns[k]*mult[k];
  109. if (subs[ind].size() > nbmax)
  110. { nbmax = subs[ind].size(); imax = ind; }
  111. }
  112. }
  113. nu[l]--;
  114. }
  115. if (nbmax > subs[i].size()) {
  116. for (map_type::iterator it=subs[i].begin(); it!=subs[i].end(); ++it)
  117. subs[imax][it->first] = void_type();
  118. subs[i].clear();
  119. }
  120. }
  121. for (int k = 0; k < dim; ++k)
  122. { ns[k]++; if (ns[k] < nbsub[k]) break; ns[k] = 0; }
  123. }
  124. // delete empty domains.
  125. size_type effnb = 0;
  126. for (size_type i = 0; i < nbtotsub; ++i) {
  127. if (subs[i].size() > 0)
  128. { if (i != effnb) std::swap(subs[i], subs[effnb]); ++effnb; }
  129. }
  130. // build matrices
  131. subs.resize(effnb);
  132. vB.resize(effnb);
  133. for (size_type i = 0; i < effnb; ++i) {
  134. clear(vB[i]); resize(vB[i], nbpts, subs[i].size());
  135. size_type j = 0;
  136. for (map_type::iterator it=subs[i].begin(); it!=subs[i].end(); ++it, ++j)
  137. vB[i](it->first, j) = value_type(1);
  138. }
  139. }
  140. }
  141. #endif