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