BackgroundMesh.cc 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "BackgroundMesh.h"
#include "VectorOperations.h"	// getMin, sort, CompairPair
#include "Initfile.h"

namespace experimental {
  
  std::map<const FiniteElemSpace*, std::pair<int, Box*> > Box::boxMap;


  Box::Box(int DOW_, std::vector<int> N_)
  : DOW(DOW_), N(N_), boxFilled(false)
  {
    min_corner.set(-1.0);
    max_corner.set(1.0);
    init();
  }


  Box::Box(int DOW_, PointType min_corner_, PointType max_corner_, std::vector<int> N_)
  : DOW(DOW_),
    min_corner(min_corner_),
    max_corner(max_corner_),
    N(N_), boxFilled(false)
  {
    init();
  }


  void Box::fillBox(const FiniteElemSpace* feSpace)
  {
    DOFVector<WorldVector<double> > coords(feSpace, "coords");
    feSpace->getMesh()->dofCompress();
33
    feSpace->getMesh()->getDofIndexCoords(coords);
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    for (DegreeOfFreedom i = 0; i < coords.getUsedSize(); i++) {
      PointType location(coords[i]);
      addData(location, std::make_pair(i, location));
    }
    boxFilled = true;
  }


  void Box::clearBox()
  {
    boxData.clear();
  }


  void Box::clearBoxData()
  {
    for (size_t i = 0; i < boxData.size(); i++) {
      boxData[i].clear();
    }
  }
  

  int Box::getMaxBoxSize()
  {
    size_t maxSize = 0;
    for (size_t i = 0; i < boxData.size(); i++) {
      maxSize = std::max(maxSize, boxData[i].size());
    }
    return maxSize;
  }


Praetorius, Simon's avatar
Praetorius, Simon committed
67
  bool Box::inBox(const PointType& x)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  {
    for (size_t i = 0; i < DOW; i++)
      if (min_corner[i] > x[i] || max_corner[i] < x[i])
	return false;
    return true;
  }


  /**
   *
   * Ny=2
   * ^----------------
   * | 4 | 5 | 6 | 7 |
   * ----+---+---+----
   * | 0 | 1 | 2 | 3 |
   * -----------------> Nx=4
   **/
Praetorius, Simon's avatar
Praetorius, Simon committed
85
  int Box::getBox(const PointType& x)
86
87
88
89
90
91
92
93
94
  {
    std::vector<int> idx(DOW);
    for (size_t i = 0; i < DOW; i++)
      idx[i] = static_cast<int>((x[i]-min_corner[i])*N[i]/(max_corner[i]-min_corner[i]));
//       idx[i] = static_cast<int>(floor((x[i]-min_corner[i])*N[i]/(max_corner[i]-min_corner[i])));
    return idx2nr(idx);
  }


Praetorius, Simon's avatar
Praetorius, Simon committed
95
  bool Box::getNearestData(const PointType& x, DataType& data)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  {
    if (!inBox(x))
      return false;

    int nr = getBox(x);
    unsigned int n = boxData[nr].size();
    // Box, in der x liegt, ist leer
    if (n == 0) {
      std::vector<DataType> datas;
      getNearestData(x, datas, 1);
      data = datas[0];
      return true;
    }
    // suche Punkt in aktueller Box, der am nächsten zu x liegt
    std::vector<double> distances;
    std::vector<DataType>::iterator dataIter;
    for (dataIter = boxData[nr].begin(); dataIter != boxData[nr].end(); dataIter++) {
      distances.push_back(distance2(x, (*dataIter).second, DOW));
    }
    double minDist = 1.e15;
    size_t minIdx = 0;
    vector_operations::getMin(distances, minDist, minIdx);
    data = boxData[nr][minIdx];
    return true;
  }


Praetorius, Simon's avatar
Praetorius, Simon committed
123
  bool Box::getNearestData(const PointType& x, std::vector<DataType>& data, int nData)
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  {
    if (!inBox(x))
      return false;
    int center_box = getBox(x);

    std::set<int> nrs; nrs.insert(center_box);
    double boxBoundaryDist = getBoxBoundaryDist(center_box, x);
    std::vector<double> distances;
    std::set<int>::iterator nrIter;
    int level = 0;
    for (int level = 0; data.size() < nData && nrs.size() > 0; level++) {
      int oldDataSize = data.size();
      for (nrIter = nrs.begin(); nrIter != nrs.end(); nrIter++) {
	data.insert(data.end(), boxData[*nrIter].begin(), boxData[*nrIter].end());
      }
      int addon = data.size()-oldDataSize;
      if (addon > 0) {
	// 1.) bestimme Abstand zu x
	int n = distances.size();
	TEST_EXIT(n + addon == data.size() && oldDataSize == n)("hier ist ein Index falsch: distances.size = %d, addon = %d, data.size = %d, oldDataSize = %d!\n",n,addon,data.size(),oldDataSize);
	for (size_t i = 0; i < addon; i++) {
	  distances.push_back(distance2(x, data[n+i].second, DOW));
	}
	// 2.) sortiere Paare aus dist und data
	comparePair<double, DataType, 0> comp;
	vector_operations::sort(distances, data, comp);
      }

      // 3.) wenn noch nicht genügend Punkte, dann umgebende Boxen mit durchsuchen
      if (data.size() < nData
	|| (level == 0 && distances[distances.size()-1] > boxBoundaryDist))
      {
	getSurroundingBoxes(center_box, level+1, nrs);
      }
    }
    data.resize(nData);
    return true;
  }


  Box* Box::provideBackgroundMesh(const FiniteElemSpace* feSpace)
  {
    if (boxMap.find(feSpace) != boxMap.end()) {
      if (boxMap[feSpace].first != feSpace->getMesh()->getChangeIndex()) {
	boxMap[feSpace].second->clearBox();
	boxMap[feSpace].second->init();
	boxMap[feSpace].second->fillBox(feSpace);
	boxMap[feSpace].first = feSpace->getMesh()->getChangeIndex();
	std::cout<<"max #data/box = "<<boxMap[feSpace].second->getMaxBoxSize()<<"\n";
      }
    } else {
      int N_const = 1<<Global::getGeo(WORLD);
      PointType min_corner_; min_corner_.set(-1.0);
      PointType max_corner_; max_corner_.set(1.0);
      Parameters::get("backgroundMesh->N", N_const); // Gesamtanzahl an Boxen
      Parameters::get("backgroundMesh->min_corner", min_corner_);
      Parameters::get("backgroundMesh->max_corner", max_corner_);

      // Anzahl an Box je Dimension anhand der Größe des gesamten Box bestimmen
      double sum_lengths = 0.0;
      std::vector<double> lengths;
      for (int i = 0; i < Global::getGeo(WORLD); i++) {
	lengths.push_back(abs(max_corner_[i]-min_corner_[i]));
	sum_lengths += lengths[i];
      }
      std::vector<int> N_;
      int N_sum = 0;
      for (int i = 0; i < Global::getGeo(WORLD); i++) {
	N_.push_back(static_cast<int>(round(pow(static_cast<double>(N_const),lengths[i]/sum_lengths))));
	N_sum += N_[i];
      }
      
      Box* box = new Box(Global::getGeo(WORLD), min_corner_, max_corner_, N_);
      box->fillBox(feSpace);

      boxMap[feSpace] = std::make_pair(feSpace->getMesh()->getChangeIndex(), box);
      std::cout<<"max #data/box = "<<boxMap[feSpace].second->getMaxBoxSize()<<"\n";
    }

    return boxMap[feSpace].second;
  }


  void Box::delete_all()
  {
    std::map<const FiniteElemSpace*, std::pair<int, Box*> >::iterator boxIter;
    for (boxIter = boxMap.begin(); boxIter != boxMap.end(); boxIter++) {
      (*boxIter).second.second->clearBox();
      delete (*boxIter).second.second;
    }
    boxMap.clear();
  }


  void Box::init()
  {
220
    int maxNr = 1;
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
    for (size_t i = 0; i < DOW; i++)
      maxNr *= N[i];
    boxData.resize(maxNr);

    std::cout<<"\n background mesh\n";
    std::cout<<"=================\n";
    std::cout<<"min_corner: "<<min_corner<<"\n";
    std::cout<<"max_corner: "<<max_corner<<"\n";
    std::cout<<"N: "<<N<<"\n\n";

    PointType tolerance; tolerance.set(2.0*DBL_TOL);
    min_corner -= tolerance;
    max_corner += tolerance;
  }


/// protected methods
///_____________________________________________________________________________________________


  inline int Box::idx2nr(std::vector<int>& idx)
  {
    int nr = idx.back();
    std::vector<int>::reverse_iterator iIter, nIter;
    for (iIter = idx.rbegin()+1, nIter = N.rbegin()+1; iIter < idx.rend(); iIter++,nIter++) {
      nr = *iIter + nr*(*nIter);
    }
    return nr;
  }


  inline void Box::nr2idx(int nr, std::vector<int>& idx)
  {
    idx.resize(DOW);
    switch (DOW) {
      case 1:
	idx[0] = nr % N[0];
	break;
      case 2:
	idx[1] = nr / N[0];
	idx[0] = nr % N[0];
	break;
      case 3:
	idx[2] = nr / (N[1]*N[0]);
	nr = nr % (N[1]*N[0]);
	idx[1] = nr / N[0];
	idx[0] = nr % N[0];
	break;
      default:
	throw(std::runtime_error("DOW="+boost::lexical_cast<std::string>(DOW)+", nur die Version für DOW=1 verwendet!"));
	break;
    }
  }


  void Box::getSurroundingBoxes(int center, int radius, std::set<int> &nrs)
  {
    std::set<int> newNrs;
    std::vector<int> idx, newIdx;
    nr2idx(center, idx); // --> idx
    newIdx.resize(idx.size());
    double incr = m_pi/3.0/radius;
    switch(DOW) {
      case 1:
	if (idx[0]-radius >= 0) { newIdx[0] = idx[0]-radius; newNrs.insert(idx2nr(newIdx)); }
	if (idx[0]+radius < N[0]) { newIdx[0] = idx[0]+radius; newNrs.insert(idx2nr(newIdx)); }
	break;
      case 2:
	for (double phi = 0.0; phi < 2.0*m_pi; phi += incr) {
	  newIdx[0] = idx[0] + static_cast<int>(round(radius*cos(phi)));
	  newIdx[1] = idx[1] + static_cast<int>(round(radius*sin(phi)));
	  if (newIdx[0] >= 0 && newIdx[0] < N[0]
	   && newIdx[1] >= 0 && newIdx[1] < N[1])
	    newNrs.insert(idx2nr(newIdx));
	}
	break;
      case 3:
	for (double phi = -m_pi; phi < m_pi; phi += incr) {
	for (double theta = 0.0; theta < m_pi; theta += incr) {
	  newIdx[0] = idx[0] + static_cast<int>(round(radius*sin(theta)*cos(phi)));
	  newIdx[1] = idx[1] + static_cast<int>(round(radius*sin(theta)*sin(phi)));
	  newIdx[2] = idx[2] + static_cast<int>(round(radius*cos(theta)));
	  if (newIdx[0] >= 0 && newIdx[0] < N[0]
	   && newIdx[1] >= 0 && newIdx[1] < N[1]
	   && newIdx[2] >= 0 && newIdx[2] < N[2])
	    newNrs.insert(idx2nr(newIdx));
	}
	}
	break;
      default:
	ERROR("unknown dimension!\n");
    }
    
    std::set<int>::iterator nrsIter;
    for (nrsIter = nrs.begin(); nrsIter != nrs.end(); nrsIter++)
      newNrs.erase(*nrsIter);
    swap(nrs, newNrs);
  }
  

  void Box::getSurroundingBoxes(int nr, std::set<int> &surrounding_nrs)
  {
    std::vector<int> idx;
    nr2idx(nr, idx); // --> idx

    for (size_t i = 0; i < DOW; i++) {
      std::vector<int> idx_i = idx;
      idx_i[i] = std::min(N[i]-1, idx_i[i]+1);
      surrounding_nrs.insert(idx2nr(idx_i));
      idx_i[i] = std::max(0, idx_i[i]-2);
      surrounding_nrs.insert(idx2nr(idx_i));
    }
    surrounding_nrs.erase(nr);
  }


  void Box::getSurroundingBoxes(std::set<int> &nrs, std::set<int> &surrounding_nrs)
  {
    std::set<int>::iterator nrsIter;
    for (nrsIter = nrs.begin(); nrsIter != nrs.end(); nrsIter++)
      getSurroundingBoxes(*nrsIter, surrounding_nrs);

    for (nrsIter = nrs.begin(); nrsIter != nrs.end(); nrsIter++)
      surrounding_nrs.erase(*nrsIter);
  }


  /// berechne die minimale Entfernung der Ränder der Box zu x
Praetorius, Simon's avatar
Praetorius, Simon committed
349
  double Box::getBoxBoundaryDist(int nr, const PointType& x)
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  {
    std::vector<int> idx;
    nr2idx(nr, idx);
    PointType min_c, max_c;
    for (size_t i = 0; i < DOW; i++) {
      min_c[i] = min_corner[i] + (max_corner[i]-min_corner[i])*idx[i]/N[i];
      max_c[i] = min_corner[i] + (max_corner[i]-min_corner[i])*(idx[i]+1)/N[i];
    }
    double dist = 1.e15;
    for (int i = 0; i < DOW; i++)
      dist = std::min(dist, std::min(abs(max_c[i]-x[i]), abs(min_c[i]-x[i])));
    return dist;
  }


  void Box::addData(PointType x, DataType data)
  {
    int nr = getBox(x);
    if (nr < 0 || nr >= boxData.size())
      throw(std::runtime_error("box-nr out of range: nr = "+boost::lexical_cast<std::string>(nr)+", x = "+boost::lexical_cast<std::string>(x)+", boxSize = "+boost::lexical_cast<std::string>(boxData.size())));
    boxData[nr].push_back(data);
  }
  
373
}