| 1 | /**
|
|---|
| 2 | * @file bspline.hpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Mon Nov 21 13:27:22 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief B-Splines for molecular dynamics.
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifndef BSPLINE_HPP_
|
|---|
| 11 | #define BSPLINE_HPP_
|
|---|
| 12 |
|
|---|
| 13 | #include "base/helper.hpp"
|
|---|
| 14 | #include "base/index.hpp"
|
|---|
| 15 | #include "base/polynomial.hpp"
|
|---|
| 16 | #include "base/vector.hpp"
|
|---|
| 17 | #include "grid/grid.hpp"
|
|---|
| 18 | #include "units/particle/particle.hpp"
|
|---|
| 19 |
|
|---|
| 20 | namespace VMG
|
|---|
| 21 | {
|
|---|
| 22 |
|
|---|
| 23 | namespace Particle
|
|---|
| 24 | {
|
|---|
| 25 |
|
|---|
| 26 | class BSpline
|
|---|
| 27 | {
|
|---|
| 28 | public:
|
|---|
| 29 | BSpline(const vmg_float& width);
|
|---|
| 30 |
|
|---|
| 31 | vmg_float EvaluateSpline(const vmg_float& val) const
|
|---|
| 32 | {
|
|---|
| 33 | for (unsigned int i=0; i<intervals.size(); ++i)
|
|---|
| 34 | if (val < intervals[i])
|
|---|
| 35 | return spline_nom[i](val) / spline_denom[i](val);
|
|---|
| 36 | return 0.0;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | void SetSpline(Grid& grid, const Particle& p, const int& near_field_cells) const
|
|---|
| 40 | {
|
|---|
| 41 | assert(p.Pos().X() >= grid.Extent().Begin().X() && p.Pos().X() < grid.Extent().End().X());
|
|---|
| 42 | assert(p.Pos().Y() >= grid.Extent().Begin().Y() && p.Pos().Y() < grid.Extent().End().Y());
|
|---|
| 43 | assert(p.Pos().Z() >= grid.Extent().Begin().Z() && p.Pos().Z() < grid.Extent().End().Z());
|
|---|
| 44 |
|
|---|
| 45 | std::vector<vmg_float> vals(Helper::intpow(2*near_field_cells+1,3));
|
|---|
| 46 | vmg_float temp_val;
|
|---|
| 47 | vmg_float int_val = 0.0;
|
|---|
| 48 | int c = 0;
|
|---|
| 49 |
|
|---|
| 50 | const int index_global_x = (p.Pos().X() - grid.Extent().Begin().X()) / grid.Extent().MeshWidth().X();
|
|---|
| 51 | const int index_global_y = (p.Pos().Y() - grid.Extent().Begin().Y()) / grid.Extent().MeshWidth().Y();
|
|---|
| 52 | const int index_global_z = (p.Pos().Z() - grid.Extent().Begin().Z()) / grid.Extent().MeshWidth().Z();
|
|---|
| 53 |
|
|---|
| 54 | assert(index_global_x >= grid.Global().LocalBegin().X() && index_global_x < grid.Global().LocalEnd().X());
|
|---|
| 55 | assert(index_global_y >= grid.Global().LocalBegin().Y() && index_global_y < grid.Global().LocalEnd().Y());
|
|---|
| 56 | assert(index_global_z >= grid.Global().LocalBegin().Z() && index_global_z < grid.Global().LocalEnd().Z());
|
|---|
| 57 |
|
|---|
| 58 | const int index_local_x = index_global_x - grid.Global().LocalBegin().X() + grid.Local().Begin().X();
|
|---|
| 59 | const int index_local_y = index_global_y - grid.Global().LocalBegin().Y() + grid.Local().Begin().Y();
|
|---|
| 60 | const int index_local_z = index_global_z - grid.Global().LocalBegin().Z() + grid.Local().Begin().Z();
|
|---|
| 61 |
|
|---|
| 62 | assert(index_local_x >= grid.Local().Begin().X() && index_local_x < grid.Local().End().X());
|
|---|
| 63 | assert(index_local_y >= grid.Local().Begin().Y() && index_local_y < grid.Local().End().Y());
|
|---|
| 64 | assert(index_local_z >= grid.Local().Begin().Z() && index_local_z < grid.Local().End().Z());
|
|---|
| 65 |
|
|---|
| 66 | const vmg_float pos_beg_x = p.Pos().X() - grid.Extent().Begin().X() - grid.Extent().MeshWidth().X() * (index_global_x - near_field_cells);
|
|---|
| 67 | const vmg_float pos_beg_y = p.Pos().Y() - grid.Extent().Begin().Y() - grid.Extent().MeshWidth().Y() * (index_global_y - near_field_cells);
|
|---|
| 68 | const vmg_float pos_beg_z = p.Pos().Z() - grid.Extent().Begin().Z() - grid.Extent().MeshWidth().Z() * (index_global_z - near_field_cells);
|
|---|
| 69 |
|
|---|
| 70 | const vmg_float h_x = grid.Extent().MeshWidth().X();
|
|---|
| 71 | const vmg_float h_y = grid.Extent().MeshWidth().Y();
|
|---|
| 72 | const vmg_float h_z = grid.Extent().MeshWidth().Z();
|
|---|
| 73 |
|
|---|
| 74 | // Iterate over all grid points which lie in the support of the interpolating B-Spline
|
|---|
| 75 |
|
|---|
| 76 | vmg_float dir_x, dir_y, dir_z;
|
|---|
| 77 | dir_x = pos_beg_x;
|
|---|
| 78 | for (int i=-1*near_field_cells; i<=near_field_cells; ++i) {
|
|---|
| 79 | dir_y = pos_beg_y;
|
|---|
| 80 | for (int j=-1*near_field_cells; j<=near_field_cells; ++j) {
|
|---|
| 81 | dir_z = pos_beg_z;
|
|---|
| 82 | for (int k=-1*near_field_cells; k<=near_field_cells; ++k) {
|
|---|
| 83 |
|
|---|
| 84 | // Compute distance from grid point to particle
|
|---|
| 85 | temp_val = EvaluateSpline(std::sqrt(dir_x*dir_x+dir_y*dir_y+dir_z*dir_z));
|
|---|
| 86 | vals[c++] = temp_val * p.Charge();
|
|---|
| 87 | int_val += temp_val;
|
|---|
| 88 |
|
|---|
| 89 | dir_z -= h_z;
|
|---|
| 90 | }
|
|---|
| 91 | dir_y -= h_y;
|
|---|
| 92 | }
|
|---|
| 93 | dir_x -= h_x;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | // Reciprocal value of the numerically integrated spline
|
|---|
| 97 | int_val = 1.0 / (int_val * h_x * h_y * h_z);
|
|---|
| 98 |
|
|---|
| 99 | c = 0;
|
|---|
| 100 | for (int i=-1*near_field_cells; i<=near_field_cells; ++i)
|
|---|
| 101 | for (int j=-1*near_field_cells; j<=near_field_cells; ++j)
|
|---|
| 102 | for (int k=-1*near_field_cells; k<=near_field_cells; ++k)
|
|---|
| 103 | grid(index_local_x + i,
|
|---|
| 104 | index_local_y + j,
|
|---|
| 105 | index_local_z + k) += vals[c++] * int_val;
|
|---|
| 106 |
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | vmg_float EvaluatePotential(const vmg_float& val) const
|
|---|
| 110 | {
|
|---|
| 111 | for (unsigned int i=0; i<intervals.size(); ++i)
|
|---|
| 112 | if (val < intervals[i])
|
|---|
| 113 | return potential_nom[i](val) / potential_denom[i](val);
|
|---|
| 114 | return potential_nom.back()(val) / potential_denom.back()(val);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | vmg_float EvaluateField(const vmg_float& val) const
|
|---|
| 118 | {
|
|---|
| 119 | for (unsigned int i=0; i<intervals.size(); ++i)
|
|---|
| 120 | if (val < intervals[i])
|
|---|
| 121 | return field_nom[i](val) / field_denom[i](val);
|
|---|
| 122 | return 0.0;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | const vmg_float& GetAntiDerivativeAtZero() const
|
|---|
| 126 | {
|
|---|
| 127 | return antid;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | private:
|
|---|
| 131 | std::vector<Polynomial> spline_nom, spline_denom;
|
|---|
| 132 | std::vector<Polynomial> potential_nom, potential_denom;
|
|---|
| 133 | std::vector<Polynomial> field_nom, field_denom;
|
|---|
| 134 | vmg_float antid;
|
|---|
| 135 | std::vector<vmg_float> intervals;
|
|---|
| 136 |
|
|---|
| 137 | vmg_float R;
|
|---|
| 138 | };
|
|---|
| 139 |
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | #endif /* BSPLINE_HPP_ */
|
|---|