| 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/particle.hpp"
|
|---|
| 16 | #include "base/polynomial.hpp"
|
|---|
| 17 | #include "base/vector.hpp"
|
|---|
| 18 | #include "grid/grid.hpp"
|
|---|
| 19 |
|
|---|
| 20 | namespace VMG
|
|---|
| 21 | {
|
|---|
| 22 |
|
|---|
| 23 | class BSpline
|
|---|
| 24 | {
|
|---|
| 25 | public:
|
|---|
| 26 | BSpline(const vmg_float& width);
|
|---|
| 27 |
|
|---|
| 28 | vmg_float EvaluateSpline(const vmg_float& val)
|
|---|
| 29 | {
|
|---|
| 30 | for (unsigned int i=0; i<intervals.size(); ++i)
|
|---|
| 31 | if (val < intervals[i])
|
|---|
| 32 | return spline_nom[i](val) / spline_denom[i](val);
|
|---|
| 33 | return 0.0;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | void SetSpline(Grid& grid, const Particle::Particle p, const int& near_field_cells)
|
|---|
| 37 | {
|
|---|
| 38 | Index i, index_global, index_local;
|
|---|
| 39 | vmg_float temp_val, length;
|
|---|
| 40 | std::vector<vmg_float> vals;
|
|---|
| 41 | std::vector<vmg_float>::const_iterator iter;
|
|---|
| 42 |
|
|---|
| 43 | vmg_float int_val = 0.0;
|
|---|
| 44 |
|
|---|
| 45 | index_global = static_cast<Index>((p.Pos() - grid.Extent().Begin()) / grid.Extent().MeshWidth());
|
|---|
| 46 | index_local = index_global - grid.Global().BeginLocal() + grid.Local().Begin();
|
|---|
| 47 |
|
|---|
| 48 | // Iterate over all grid points which lie in the support of the interpolating B-Spline
|
|---|
| 49 | for (i.X()=-1*near_field_cells-1; i.X()<=near_field_cells+1; ++i.X())
|
|---|
| 50 | for (i.Y()=-1*near_field_cells-1; i.Y()<=near_field_cells+1; ++i.Y())
|
|---|
| 51 | for (i.Z()=-1*near_field_cells-1; i.Z()<=near_field_cells+1; ++i.Z()) {
|
|---|
| 52 |
|
|---|
| 53 | // Compute distance from grid point to particle
|
|---|
| 54 | length = ( p.Pos() - grid.Extent().Begin() - grid.Extent().MeshWidth() * (index_global+i) ).Length();
|
|---|
| 55 |
|
|---|
| 56 | temp_val = EvaluateSpline(length);
|
|---|
| 57 | vals.push_back(temp_val * p.Charge());
|
|---|
| 58 | int_val += temp_val;
|
|---|
| 59 |
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | int_val = 1.0 / (Helper::pow(grid.MeshWidth(), 3) * int_val);
|
|---|
| 63 |
|
|---|
| 64 | iter = vals.begin();
|
|---|
| 65 |
|
|---|
| 66 | for (i.X()=-1*near_field_cells-1; i.X()<=near_field_cells+1; ++i.X())
|
|---|
| 67 | for (i.Y()=-1*near_field_cells-1; i.Y()<=near_field_cells+1; ++i.Y())
|
|---|
| 68 | for (i.Z()=-1*near_field_cells-1; i.Z()<=near_field_cells+1; ++i.Z())
|
|---|
| 69 | grid(index_local+i) += *iter++ * int_val;
|
|---|
| 70 |
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | vmg_float EvaluatePotential(const vmg_float& val)
|
|---|
| 74 | {
|
|---|
| 75 | for (unsigned int i=0; i<intervals.size(); ++i)
|
|---|
| 76 | if (val < intervals[i])
|
|---|
| 77 | return potential_nom[i](val) / potential_denom[i](val);
|
|---|
| 78 | return potential_nom.back()(val) / potential_denom.back()(val);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | vmg_float EvaluateDerivative(const Vector& v, const int& dv);
|
|---|
| 82 |
|
|---|
| 83 | const vmg_float& GetAntiDerivativeZero() const
|
|---|
| 84 | {
|
|---|
| 85 | return antid;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | private:
|
|---|
| 89 | std::vector<Polynomial> spline_nom, spline_denom;
|
|---|
| 90 | std::vector<Polynomial> potential_nom, potential_denom;
|
|---|
| 91 | vmg_float antid;
|
|---|
| 92 | std::vector<vmg_float> intervals;
|
|---|
| 93 |
|
|---|
| 94 | vmg_float R;
|
|---|
| 95 | };
|
|---|
| 96 |
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | #endif /* BSPLINE_HPP_ */
|
|---|