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