source: src/samples/bspline.hpp@ 1a92cf

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

vmg: Prepared force calculation.

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

  • Property mode set to 100644
File size: 3.8 KB
Line 
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
20namespace VMG
21{
22
23class BSpline
24{
25public:
26 BSpline(const vmg_float& width);
27
28 vmg_float EvaluateSpline(const vmg_float& val) const
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) const
37 {
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
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());
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());
58
59 // Iterate over all grid points which lie in the support of the interpolating B-Spline
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()) {
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;
70
71 }
72
73 // Reciprocal value of the numerically integrated spline
74 int_val = 1.0 / (int_val * grid.Extent().MeshWidth().Product());
75
76 iter = vals.begin();
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())
80 grid(index_local+i) += *iter++ * int_val;
81
82 assert(iter == vals.end());
83 }
84
85 vmg_float EvaluatePotential(const vmg_float& val) const
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
93 vmg_float EvaluateField(const vmg_float& val) const
94 {
95 for (unsigned int i=0; i<intervals.size(); ++i)
96 if (val < intervals[i])
97 return field_nom[i](val) / field_denom[i](val);
98 return 0.0;
99 }
100
101 const vmg_float& GetAntiDerivativeAtZero() const
102 {
103 return antid;
104 }
105
106private:
107 std::vector<Polynomial> spline_nom, spline_denom;
108 std::vector<Polynomial> potential_nom, potential_denom;
109 std::vector<Polynomial> field_nom, field_denom;
110 vmg_float antid;
111 std::vector<vmg_float> intervals;
112
113 vmg_float R;
114};
115
116}
117
118#endif /* BSPLINE_HPP_ */
Note: See TracBrowser for help on using the repository browser.