BackgroundMesh.hh 4.12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/******************************************************************************
 *
 * Extension of AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: Simon Praetorius et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/


19
20
#include "Tools.h"	// tools::Regression

21
namespace AMDiS { namespace extensions {
22
23
24
25
26
27
28
29
30

  /**
   * strategies:
   * 0 .. get nearest point to x and eval DOFVector at this DOF-index
   * 1 .. get n nearest points, calc weighted sum of data at these points
   * 2 .. get n nearest points, calc regression plane and eval at x
   * 3 .. get n nearest points, calc weighted regression plane and eval at x
   **/
  template<typename T>
Praetorius, Simon's avatar
Praetorius, Simon committed
31
  T Box::evalAtPoint(const DOFVector<T>& vec, const PointType& x, int strategy, int nrOfPoints)
32
  {
33
    T value = 0;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    switch (strategy) {
      case 0:
	value = evalAtPoint_simple(vec, x);
	break;
      case 1:
	value = evalAtPoint_weighted_sum(vec, x, nrOfPoints);
	break;
      case 2:
	value = evalAtPoint_regression(vec, x, nrOfPoints);
	break;
      case 3:
	value = evalAtPoint_weighted_regression(vec, x, nrOfPoints);
	break;
      default:
	ERROR("ERROR: unknown strategy [%d]!\n",strategy);
	break;
    }
    return value;
  }

  
  template<typename T>
Praetorius, Simon's avatar
Praetorius, Simon committed
56
  T Box::evalAtPoint_simple(const DOFVector<T>& vec, const PointType& x)
57
58
59
60
61
62
63
64
65
66
  {
    DataType dof;
    T value; nullify(value);
    if (getNearestData(x, dof))
      value = vec[dof.first];
    return value;
  }

  
  template<typename T>
Praetorius, Simon's avatar
Praetorius, Simon committed
67
  T Box::evalAtPoint_weighted_sum(const DOFVector<T>& vec, const PointType& x, int nrOfPoints)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  {
    T value; nullify(value);

    std::vector<DataType> dofs;
    int nPoints = (nrOfPoints > 0 ? nrOfPoints : vec.getFeSpace()->getBasisFcts()->getNumber()+1);

    bool inside = getNearestData(x, dofs, nPoints);
    if (!inside)
      return value;

    // gewichtete Summe der einzelnen Funktionswerte in der Nähe von x
    std::vector<double> weights;
    for (size_t i = 0; i < dofs.size(); i++) {
      double dist = distance(x, dofs[i].second, DOW);
      weights.push_back(1.0/std::max(1.e-8, dist));
    }

    double weight = 0.0;
    for (size_t i = 0; i < dofs.size(); i++) {
      value += vec[dofs[i].first] * weights[i];
      weight += weights[i];
    }
    value *= 1.0/weight;

    return value;
  }


  template<typename T>
Praetorius, Simon's avatar
Praetorius, Simon committed
97
  T Box::evalAtPoint_regression(const DOFVector<T>& vec, const PointType& x, int nrOfPoints)
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
  {
    T value; nullify(value);

    std::vector<DataType> dofs;
    int nPoints = (nrOfPoints > 0 ? nrOfPoints : vec.getFeSpace()->getBasisFcts()->getNumber()+1);

    bool inside = getNearestData(x, dofs, nPoints);
    if (!inside)
      return value;

    std::vector<WorldVector<double> > points;
    std::vector<double> values;
    for (size_t i = 0; i < dofs.size(); i++) {
      points.push_back(dofs[i].second);
      values.push_back(vec[dofs[i].first]);
    }

    tools::Regression reg(DOW);
    reg.apply(points, values);
    value = reg.value(x);
    return value;
  }


  template<typename T>
Praetorius, Simon's avatar
Praetorius, Simon committed
123
  T Box::evalAtPoint_weighted_regression(const DOFVector<T>& vec, const PointType& x, int nrOfPoints)
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
  {
    T value; nullify(value);

    std::vector<DataType> dofs;
    int nPoints = (nrOfPoints > 0 ? nrOfPoints : vec.getFeSpace()->getBasisFcts()->getNumber()+1);

    bool inside = getNearestData(x, dofs, nPoints);
    if (!inside)
      return value;

    std::vector<WorldVector<double> > points;
    std::vector<double> values,weights;
    for (size_t i = 0; i < dofs.size(); i++) {
      points.push_back(dofs[i].second);
      values.push_back(vec[dofs[i].first]);

      double dist = distance(x, dofs[i].second, DOW);
      weights.push_back(1.0/std::max(1.e-8, dist));
    }

    tools::Regression reg(DOW);
    reg.apply(points, values, weights);
    value = reg.value(x);

    return value;
  }

151
} }