source: src/samples/bspline.hpp@ 4571da

Last change on this file since 4571da was 4571da, checked in by Julian Iseringhausen <isering@…>, 14 years ago

vmg: Implement fourth-order discretization of the Poisson equation.

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1762 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[dfed1c]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"
[ab63b6]14#include "base/index.hpp"
15#include "base/particle.hpp"
[dfed1c]16#include "base/polynomial.hpp"
17#include "base/vector.hpp"
[ab63b6]18#include "grid/grid.hpp"
[dfed1c]19
20namespace VMG
21{
22
23class BSpline
24{
25public:
26 BSpline(const vmg_float& width);
27
[4571da]28 vmg_float EvaluateSpline(const vmg_float& val) const
[dfed1c]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
[4571da]36 void SetSpline(Grid& grid, const Particle::Particle p, const int& near_field_cells) const
[ab63b6]37 {
[ac6d04]38 assert(p.Pos().X() >= grid.Extent().Begin().X() && p.Pos().X() < grid.Extent().End().X());
39 assert(p.Pos().Y() >= grid.Extent().Begin().Y() && p.Pos().Y() < grid.Extent().End().Y());
40 assert(p.Pos().Z() >= grid.Extent().Begin().Z() && p.Pos().Z() < grid.Extent().End().Z());
41
[ab63b6]42 Index i, index_global, index_local;
43 vmg_float temp_val, length;
44 std::vector<vmg_float> vals;
45 std::vector<vmg_float>::const_iterator iter;
46
47 vmg_float int_val = 0.0;
48
49 index_global = static_cast<Index>((p.Pos() - grid.Extent().Begin()) / grid.Extent().MeshWidth());
[ac6d04]50 assert(index_global.X() >= grid.Global().LocalBegin().X() && index_global.X() < grid.Global().LocalEnd().X());
51 assert(index_global.Y() >= grid.Global().LocalBegin().Y() && index_global.Y() < grid.Global().LocalEnd().Y());
52 assert(index_global.Z() >= grid.Global().LocalBegin().Z() && index_global.Z() < grid.Global().LocalEnd().Z());
53
54 index_local = index_global - grid.Global().LocalBegin() + grid.Local().Begin();
55 assert(index_local.X() >= grid.Local().Begin().X() && index_local.X() < grid.Local().End().X());
56 assert(index_local.Y() >= grid.Local().Begin().Y() && index_local.Y() < grid.Local().End().Y());
57 assert(index_local.Z() >= grid.Local().Begin().Z() && index_local.Z() < grid.Local().End().Z());
[ab63b6]58
59 // Iterate over all grid points which lie in the support of the interpolating B-Spline
[ac6d04]60 for (i.X()=-1*near_field_cells; i.X()<=near_field_cells; ++i.X())
61 for (i.Y()=-1*near_field_cells; i.Y()<=near_field_cells; ++i.Y())
62 for (i.Z()=-1*near_field_cells; i.Z()<=near_field_cells; ++i.Z()) {
[ab63b6]63
64 // Compute distance from grid point to particle
65 length = ( p.Pos() - grid.Extent().Begin() - grid.Extent().MeshWidth() * (index_global+i) ).Length();
66
67 temp_val = EvaluateSpline(length);
68 vals.push_back(temp_val * p.Charge());
69 int_val += temp_val;
[ac6d04]70
[ab63b6]71 }
72
[ac6d04]73 // Reciprocal value of the numerically integrated spline
74 int_val = 1.0 / (int_val * grid.Extent().MeshWidth().Product());
[ab63b6]75
76 iter = vals.begin();
[ac6d04]77 for (i.X()=-1*near_field_cells; i.X()<=near_field_cells; ++i.X())
78 for (i.Y()=-1*near_field_cells; i.Y()<=near_field_cells; ++i.Y())
79 for (i.Z()=-1*near_field_cells; i.Z()<=near_field_cells; ++i.Z())
[ab63b6]80 grid(index_local+i) += *iter++ * int_val;
81
[ac6d04]82 assert(iter == vals.end());
[ab63b6]83 }
84
[4571da]85 vmg_float EvaluatePotential(const vmg_float& val) const
[dfed1c]86 {
87 for (unsigned int i=0; i<intervals.size(); ++i)
88 if (val < intervals[i])
89 return potential_nom[i](val) / potential_denom[i](val);
90 return potential_nom.back()(val) / potential_denom.back()(val);
91 }
92
[ac6d04]93 const vmg_float& GetAntiDerivativeAtZero() const
[dfed1c]94 {
95 return antid;
96 }
97
98private:
99 std::vector<Polynomial> spline_nom, spline_denom;
100 std::vector<Polynomial> potential_nom, potential_denom;
101 vmg_float antid;
102 std::vector<vmg_float> intervals;
103
104 vmg_float R;
105};
106
107}
108
109#endif /* BSPLINE_HPP_ */
Note: See TracBrowser for help on using the repository browser.