source: src/units/particle/bspline.hpp@ cd0fed

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

Fixed some warnings.

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

  • Property mode set to 100644
File size: 4.0 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/polynomial.hpp"
16#include "base/vector.hpp"
17#include "grid/grid.hpp"
18#include "units/particle/particle.hpp"
19
20namespace VMG
21{
22
23namespace Particle
24{
25
26class BSpline
27{
28public:
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 Index i;
51 Vector dir;
52
53 const Index index_global = static_cast<Index>((p.Pos() - grid.Extent().Begin()) / grid.Extent().MeshWidth());
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 Index index_local = index_global - grid.Global().LocalBegin() + grid.Local().Begin();
59 assert(index_local.X() >= grid.Local().Begin().X() && index_local.X() < grid.Local().End().X());
60 assert(index_local.Y() >= grid.Local().Begin().Y() && index_local.Y() < grid.Local().End().Y());
61 assert(index_local.Z() >= grid.Local().Begin().Z() && index_local.Z() < grid.Local().End().Z());
62
63 const Vector pos_beg = p.Pos() - grid.Extent().Begin() - grid.Extent().MeshWidth() * (index_global - near_field_cells);
64 const Vector& h = grid.Extent().MeshWidth();
65
66 // Iterate over all grid points which lie in the support of the interpolating B-Spline
67 dir[0] = pos_beg[0];
68 for (i.X()=-1*near_field_cells; i.X()<=near_field_cells; ++i.X()) {
69 dir[1] = pos_beg[1];
70 for (i.Y()=-1*near_field_cells; i.Y()<=near_field_cells; ++i.Y()) {
71 dir[2] = pos_beg[2];
72 for (i.Z()=-1*near_field_cells; i.Z()<=near_field_cells; ++i.Z()) {
73
74 // Compute distance from grid point to particle
75 temp_val = EvaluateSpline(dir.Length());
76 vals[c++] = temp_val * p.Charge();
77 int_val += temp_val;
78
79 dir[2] -= h[2];
80 }
81 dir[1] -= h[1];
82 }
83 dir[0] -= h[0];
84 }
85
86 // Reciprocal value of the numerically integrated spline
87 int_val = 1.0 / (int_val * grid.Extent().MeshWidth().Product());
88
89 c = 0;
90 for (i.X()=-1*near_field_cells; i.X()<=near_field_cells; ++i.X())
91 for (i.Y()=-1*near_field_cells; i.Y()<=near_field_cells; ++i.Y())
92 for (i.Z()=-1*near_field_cells; i.Z()<=near_field_cells; ++i.Z())
93 grid(index_local+i) += vals[c++] * int_val;
94
95 }
96
97 vmg_float EvaluatePotential(const vmg_float& val) const
98 {
99 for (unsigned int i=0; i<intervals.size(); ++i)
100 if (val < intervals[i])
101 return potential_nom[i](val) / potential_denom[i](val);
102 return potential_nom.back()(val) / potential_denom.back()(val);
103 }
104
105 vmg_float EvaluateField(const vmg_float& val) const
106 {
107 for (unsigned int i=0; i<intervals.size(); ++i)
108 if (val < intervals[i])
109 return field_nom[i](val) / field_denom[i](val);
110 return 0.0;
111 }
112
113 const vmg_float& GetAntiDerivativeAtZero() const
114 {
115 return antid;
116 }
117
118private:
119 std::vector<Polynomial> spline_nom, spline_denom;
120 std::vector<Polynomial> potential_nom, potential_denom;
121 std::vector<Polynomial> field_nom, field_denom;
122 vmg_float antid;
123 std::vector<vmg_float> intervals;
124
125 vmg_float R;
126};
127
128}
129
130}
131
132#endif /* BSPLINE_HPP_ */
Note: See TracBrowser for help on using the repository browser.