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