| [fcf7f6] | 1 | /*
|
|---|
| 2 | * vmg - a versatile multigrid solver
|
|---|
| 3 | * Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
|
|---|
| 4 | *
|
|---|
| 5 | * vmg is free software: you can redistribute it and/or modify
|
|---|
| 6 | * it under the terms of the GNU General Public License as published by
|
|---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
|---|
| 8 | * (at your option) any later version.
|
|---|
| 9 | *
|
|---|
| 10 | * vmg is distributed in the hope that it will be useful,
|
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | * GNU General Public License for more details.
|
|---|
| 14 | *
|
|---|
| 15 | * You should have received a copy of the GNU General Public License
|
|---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| [dfed1c] | 19 | /**
|
|---|
| 20 | * @file bspline.hpp
|
|---|
| 21 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 22 | * @date Mon Nov 21 13:27:22 2011
|
|---|
| 23 | *
|
|---|
| 24 | * @brief B-Splines for molecular dynamics.
|
|---|
| 25 | *
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | #ifndef BSPLINE_HPP_
|
|---|
| 29 | #define BSPLINE_HPP_
|
|---|
| 30 |
|
|---|
| 31 | #include "base/helper.hpp"
|
|---|
| [ab63b6] | 32 | #include "base/index.hpp"
|
|---|
| [dfed1c] | 33 | #include "base/polynomial.hpp"
|
|---|
| 34 | #include "base/vector.hpp"
|
|---|
| [ab63b6] | 35 | #include "grid/grid.hpp"
|
|---|
| [f003a9] | 36 | #include "units/particle/particle.hpp"
|
|---|
| [dfed1c] | 37 |
|
|---|
| 38 | namespace VMG
|
|---|
| 39 | {
|
|---|
| 40 |
|
|---|
| [f003a9] | 41 | namespace Particle
|
|---|
| 42 | {
|
|---|
| 43 |
|
|---|
| [dfed1c] | 44 | class BSpline
|
|---|
| 45 | {
|
|---|
| 46 | public:
|
|---|
| [36d56c] | 47 | BSpline(const int& near_field_cells, const vmg_float& h);
|
|---|
| [dfed1c] | 48 |
|
|---|
| [4571da] | 49 | vmg_float EvaluateSpline(const vmg_float& val) const
|
|---|
| [dfed1c] | 50 | {
|
|---|
| 51 | for (unsigned int i=0; i<intervals.size(); ++i)
|
|---|
| 52 | if (val < intervals[i])
|
|---|
| [8180d8] | 53 | return spline_nom[i](val) / spline_denom[i](val);
|
|---|
| [dfed1c] | 54 | return 0.0;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| [36d56c] | 57 | void SetSpline(Grid& grid, const Particle& p) const
|
|---|
| [ab63b6] | 58 | {
|
|---|
| [ac6d04] | 59 | assert(p.Pos().X() >= grid.Extent().Begin().X() && p.Pos().X() < grid.Extent().End().X());
|
|---|
| 60 | assert(p.Pos().Y() >= grid.Extent().Begin().Y() && p.Pos().Y() < grid.Extent().End().Y());
|
|---|
| 61 | assert(p.Pos().Z() >= grid.Extent().Begin().Z() && p.Pos().Z() < grid.Extent().End().Z());
|
|---|
| 62 |
|
|---|
| [36d56c] | 63 | vmg_float* vals = new vmg_float[Helper::intpow(2*near_field_cells+1,3)];
|
|---|
| 64 |
|
|---|
| [39a6d9] | 65 | vmg_float temp_val;
|
|---|
| [ab63b6] | 66 | vmg_float int_val = 0.0;
|
|---|
| [39a6d9] | 67 | int c = 0;
|
|---|
| 68 |
|
|---|
| [8180d8] | 69 | const int index_global_x = grid.Global().GlobalBegin().X() + (p.Pos().X() - grid.Extent().Begin().X()) / grid.Extent().MeshWidth().X();
|
|---|
| 70 | const int index_global_y = grid.Global().GlobalBegin().Y() + (p.Pos().Y() - grid.Extent().Begin().Y()) / grid.Extent().MeshWidth().Y();
|
|---|
| 71 | const int index_global_z = grid.Global().GlobalBegin().Z() + (p.Pos().Z() - grid.Extent().Begin().Z()) / grid.Extent().MeshWidth().Z();
|
|---|
| [ab63b6] | 72 |
|
|---|
| [06e153] | 73 | assert(index_global_x >= grid.Global().LocalBegin().X() && index_global_x < grid.Global().LocalEnd().X());
|
|---|
| 74 | assert(index_global_y >= grid.Global().LocalBegin().Y() && index_global_y < grid.Global().LocalEnd().Y());
|
|---|
| 75 | assert(index_global_z >= grid.Global().LocalBegin().Z() && index_global_z < grid.Global().LocalEnd().Z());
|
|---|
| [ac6d04] | 76 |
|
|---|
| [06e153] | 77 | const int index_local_x = index_global_x - grid.Global().LocalBegin().X() + grid.Local().Begin().X();
|
|---|
| 78 | const int index_local_y = index_global_y - grid.Global().LocalBegin().Y() + grid.Local().Begin().Y();
|
|---|
| 79 | const int index_local_z = index_global_z - grid.Global().LocalBegin().Z() + grid.Local().Begin().Z();
|
|---|
| [ab63b6] | 80 |
|
|---|
| [06e153] | 81 | assert(index_local_x >= grid.Local().Begin().X() && index_local_x < grid.Local().End().X());
|
|---|
| 82 | assert(index_local_y >= grid.Local().Begin().Y() && index_local_y < grid.Local().End().Y());
|
|---|
| 83 | assert(index_local_z >= grid.Local().Begin().Z() && index_local_z < grid.Local().End().Z());
|
|---|
| 84 |
|
|---|
| [8180d8] | 85 | const vmg_float pos_beg_x = p.Pos().X() - grid.Extent().Begin().X() - grid.Extent().MeshWidth().X() * (index_global_x - grid.Global().GlobalBegin().X() - near_field_cells);
|
|---|
| 86 | const vmg_float pos_beg_y = p.Pos().Y() - grid.Extent().Begin().Y() - grid.Extent().MeshWidth().Y() * (index_global_y - grid.Global().GlobalBegin().Y() - near_field_cells);
|
|---|
| 87 | const vmg_float pos_beg_z = p.Pos().Z() - grid.Extent().Begin().Z() - grid.Extent().MeshWidth().Z() * (index_global_z - grid.Global().GlobalBegin().Z() - near_field_cells);
|
|---|
| [06e153] | 88 |
|
|---|
| [36d56c] | 89 | const vmg_float& h_x = grid.Extent().MeshWidth().X();
|
|---|
| 90 | const vmg_float& h_y = grid.Extent().MeshWidth().Y();
|
|---|
| 91 | const vmg_float& h_z = grid.Extent().MeshWidth().Z();
|
|---|
| [39a6d9] | 92 |
|
|---|
| [ab63b6] | 93 | // Iterate over all grid points which lie in the support of the interpolating B-Spline
|
|---|
| [36d56c] | 94 | vmg_float dir_x = pos_beg_x;
|
|---|
| [06e153] | 95 | for (int i=-1*near_field_cells; i<=near_field_cells; ++i) {
|
|---|
| [36d56c] | 96 | vmg_float dir_y = pos_beg_y;
|
|---|
| [06e153] | 97 | for (int j=-1*near_field_cells; j<=near_field_cells; ++j) {
|
|---|
| [8180d8] | 98 | vmg_float dir_z = pos_beg_z;
|
|---|
| 99 | for (int k=-1*near_field_cells; k<=near_field_cells; ++k) {
|
|---|
| [ab63b6] | 100 |
|
|---|
| [8180d8] | 101 | // Compute distance from grid point to particle
|
|---|
| 102 | temp_val = EvaluateSpline(std::sqrt(dir_x*dir_x+dir_y*dir_y+dir_z*dir_z));
|
|---|
| 103 | vals[c++] = temp_val * p.Charge();
|
|---|
| 104 | int_val += temp_val;
|
|---|
| [ac6d04] | 105 |
|
|---|
| [8180d8] | 106 | dir_z -= h_z;
|
|---|
| 107 | }
|
|---|
| 108 | dir_y -= h_y;
|
|---|
| [39a6d9] | 109 | }
|
|---|
| [06e153] | 110 | dir_x -= h_x;
|
|---|
| [39a6d9] | 111 | }
|
|---|
| [ab63b6] | 112 |
|
|---|
| [ac6d04] | 113 | // Reciprocal value of the numerically integrated spline
|
|---|
| [06e153] | 114 | int_val = 1.0 / (int_val * h_x * h_y * h_z);
|
|---|
| [ab63b6] | 115 |
|
|---|
| [39a6d9] | 116 | c = 0;
|
|---|
| [06e153] | 117 | for (int i=-1*near_field_cells; i<=near_field_cells; ++i)
|
|---|
| 118 | for (int j=-1*near_field_cells; j<=near_field_cells; ++j)
|
|---|
| [8180d8] | 119 | for (int k=-1*near_field_cells; k<=near_field_cells; ++k)
|
|---|
| 120 | grid(index_local_x + i,
|
|---|
| 121 | index_local_y + j,
|
|---|
| 122 | index_local_z + k) += vals[c++] * int_val;
|
|---|
| [ab63b6] | 123 |
|
|---|
| [36d56c] | 124 | delete [] vals;
|
|---|
| [ab63b6] | 125 | }
|
|---|
| 126 |
|
|---|
| [4571da] | 127 | vmg_float EvaluatePotential(const vmg_float& val) const
|
|---|
| [dfed1c] | 128 | {
|
|---|
| 129 | for (unsigned int i=0; i<intervals.size(); ++i)
|
|---|
| 130 | if (val < intervals[i])
|
|---|
| [8180d8] | 131 | return potential_nom[i](val) / potential_denom[i](val);
|
|---|
| [dfed1c] | 132 | return potential_nom.back()(val) / potential_denom.back()(val);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| [1a92cf] | 135 | vmg_float EvaluateField(const vmg_float& val) const
|
|---|
| 136 | {
|
|---|
| 137 | for (unsigned int i=0; i<intervals.size(); ++i)
|
|---|
| 138 | if (val < intervals[i])
|
|---|
| [8180d8] | 139 | return field_nom[i](val) / field_denom[i](val);
|
|---|
| [1a92cf] | 140 | return 0.0;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| [ac6d04] | 143 | const vmg_float& GetAntiDerivativeAtZero() const
|
|---|
| [dfed1c] | 144 | {
|
|---|
| 145 | return antid;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | private:
|
|---|
| 149 | std::vector<Polynomial> spline_nom, spline_denom;
|
|---|
| 150 | std::vector<Polynomial> potential_nom, potential_denom;
|
|---|
| [1a92cf] | 151 | std::vector<Polynomial> field_nom, field_denom;
|
|---|
| [dfed1c] | 152 | vmg_float antid;
|
|---|
| 153 | std::vector<vmg_float> intervals;
|
|---|
| 154 |
|
|---|
| [36d56c] | 155 | const vmg_float R;
|
|---|
| 156 | const int near_field_cells;
|
|---|
| [dfed1c] | 157 | };
|
|---|
| 158 |
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| [f003a9] | 161 | }
|
|---|
| 162 |
|
|---|
| [dfed1c] | 163 | #endif /* BSPLINE_HPP_ */
|
|---|