| 1 | #ifdef HAVE_CONFIG_H
|
|---|
| 2 | #include <config.h>
|
|---|
| 3 | #endif
|
|---|
| 4 |
|
|---|
| 5 | #include <vector>
|
|---|
| 6 |
|
|---|
| 7 | #include "base/helper.hpp"
|
|---|
| 8 | #include "base/index.hpp"
|
|---|
| 9 | #include "base/interpolate_polynomial.hpp"
|
|---|
| 10 | #include "base/vector.hpp"
|
|---|
| 11 | #include "grid/grid.hpp"
|
|---|
| 12 |
|
|---|
| 13 | using namespace VMG;
|
|---|
| 14 |
|
|---|
| 15 | InterpolatePolynomial::InterpolatePolynomial(const unsigned int& degree) :
|
|---|
| 16 | deg_1(degree+1),
|
|---|
| 17 | pos_begin(0.0)
|
|---|
| 18 | {
|
|---|
| 19 | coeff = new vmg_float[Helper::intpow(deg_1, 3)];
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | InterpolatePolynomial::~InterpolatePolynomial()
|
|---|
| 23 | {
|
|---|
| 24 | delete [] coeff;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | void InterpolatePolynomial::ComputeCoefficients(const Grid& grid, const Index& index)
|
|---|
| 28 | {
|
|---|
| 29 | Index i;
|
|---|
| 30 |
|
|---|
| 31 | const Index begin = index - (static_cast<int>(deg_1)-1)/2 - 1;
|
|---|
| 32 |
|
|---|
| 33 | h = grid.Extent().MeshWidth();
|
|---|
| 34 |
|
|---|
| 35 | for (i[0]=0; i[0]<deg_1; ++i[0])
|
|---|
| 36 | for (i[1]=0; i[1]<deg_1; ++i[1])
|
|---|
| 37 | for (i[2]=0; i[2]<deg_1; ++i[2])
|
|---|
| 38 | _access_coeff(i) = grid.GetVal(i+begin);
|
|---|
| 39 |
|
|---|
| 40 | pos_begin = grid.Extent().Begin()
|
|---|
| 41 | + (begin - grid.Local().Begin() + grid.Global().LocalBegin()) * grid.Extent().MeshWidth();
|
|---|
| 42 |
|
|---|
| 43 | // compute coefficients x-direction
|
|---|
| 44 | for (i=0; i[1]<deg_1; ++i[1])
|
|---|
| 45 | for (i[2]=0; i[2]<deg_1; ++i[2])
|
|---|
| 46 | _compute_coefficients_1d(i, 0);
|
|---|
| 47 |
|
|---|
| 48 | // compute coefficients y-direction
|
|---|
| 49 | for (i=0; i[0]<deg_1; ++i[0])
|
|---|
| 50 | for (i[2]=0; i[2]<deg_1; ++i[2])
|
|---|
| 51 | _compute_coefficients_1d(i, 1);
|
|---|
| 52 |
|
|---|
| 53 | // compute coefficients z-direction
|
|---|
| 54 | for (i=0; i[0]<deg_1; ++i[0])
|
|---|
| 55 | for (i[1]=0; i[1]<deg_1; ++i[1])
|
|---|
| 56 | _compute_coefficients_1d(i, 2);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void InterpolatePolynomial::_compute_coefficients_1d(const Index& index, const unsigned int& direction)
|
|---|
| 60 | {
|
|---|
| 61 | vmg_float temp_array[deg_1];
|
|---|
| 62 | vmg_float power = 1.0;
|
|---|
| 63 | unsigned long faculty = 1;
|
|---|
| 64 | unsigned int c;
|
|---|
| 65 | Index i;
|
|---|
| 66 |
|
|---|
| 67 | for (i=index, c=0; c<deg_1; ++i[direction], ++c)
|
|---|
| 68 | temp_array[c] = _access_coeff(i);
|
|---|
| 69 |
|
|---|
| 70 | i=index;
|
|---|
| 71 | ++i[direction];
|
|---|
| 72 | for (c=1; c<deg_1; ++i[direction], ++c) {
|
|---|
| 73 | for (unsigned int j=0; j<deg_1-c; ++j)
|
|---|
| 74 | temp_array[j] = temp_array[j+1] - temp_array[j];
|
|---|
| 75 | faculty *= c;
|
|---|
| 76 | power *= h[direction];
|
|---|
| 77 | _access_coeff(i) = temp_array[0] / (faculty*power);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | vmg_float InterpolatePolynomial::Evaluate(const Vector& pos)
|
|---|
| 82 | {
|
|---|
| 83 | vmg_float result = 0.0;
|
|---|
| 84 | Vector prod, offset;
|
|---|
| 85 | Index i;
|
|---|
| 86 |
|
|---|
| 87 | prod[0] = 1.0;
|
|---|
| 88 | offset[0] = pos_begin[0];
|
|---|
| 89 | for (i[0]=0; i[0]<deg_1; ++i[0]) {
|
|---|
| 90 | prod[1] = prod[0];
|
|---|
| 91 | offset[1] = pos_begin[1];
|
|---|
| 92 | for (i[1]=0; i[1]<deg_1; ++i[1]) {
|
|---|
| 93 | prod[2] = prod[1];
|
|---|
| 94 | offset[2] = pos_begin[2];
|
|---|
| 95 | for (i[2]=0; i[2]<deg_1; ++i[2]) {
|
|---|
| 96 | result += _access_coeff(i) * prod[2];
|
|---|
| 97 | prod[2] *= pos[2] - offset[2];
|
|---|
| 98 | offset[2] += h[2];
|
|---|
| 99 | }
|
|---|
| 100 | prod[1] *= pos[1] - offset[1];
|
|---|
| 101 | offset[1] += h[1];
|
|---|
| 102 | }
|
|---|
| 103 | prod[0] *= pos[0] - offset[0];
|
|---|
| 104 | offset[0] += h[0];
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | return result;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | Vector InterpolatePolynomial::EvaluateGradient(const Vector& pos)
|
|---|
| 111 | {
|
|---|
| 112 | Vector buffer[deg_1];
|
|---|
| 113 | std::vector<Vector> buffer_diff[deg_1-1];
|
|---|
| 114 | Vector offset, result;
|
|---|
| 115 |
|
|---|
| 116 | buffer[0] = 1.0;
|
|---|
| 117 | offset = pos - pos_begin;
|
|---|
| 118 | for (int i=1; i<deg_1; ++i) {
|
|---|
| 119 | buffer[i] = buffer[i-1] * offset;
|
|---|
| 120 | offset += h;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | offset = pos - pos_begin;
|
|---|
| 124 | for (int i=0; i<deg_1-1; ++i) {
|
|---|
| 125 | buffer_diff[i].resize(i+1);
|
|---|
| 126 | offset += h;
|
|---|
| 127 | for (int j=0; j<i; ++j)
|
|---|
| 128 | buffer_diff[i][j] = buffer_diff[i-1][j] * offset;
|
|---|
| 129 | buffer_diff[i][i] = buffer[i+1];
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | for (int i=0; i<deg_1; ++i)
|
|---|
| 133 | for (int j=0; j<deg_1; ++j)
|
|---|
| 134 | for (int k=0; k<deg_1-1; ++k) {
|
|---|
| 135 | Vector sum;
|
|---|
| 136 | for (int l=0; l<=k; ++l)
|
|---|
| 137 | sum += buffer_diff[k][l];
|
|---|
| 138 | result[0] += _access_coeff(k+1, i, j) * buffer[i][1] * buffer[j][2] * sum[0];
|
|---|
| 139 | result[1] += _access_coeff(i, k+1, j) * buffer[i][0] * buffer[j][2] * sum[1];
|
|---|
| 140 | result[2] += _access_coeff(i, j, k+1) * buffer[i][0] * buffer[j][1] * sum[2];
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | return result;
|
|---|
| 144 | }
|
|---|