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
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"
[dfed1c]15#include "base/polynomial.hpp"
16#include "base/vector.hpp"
[ab63b6]17#include "grid/grid.hpp"
[f003a9]18#include "units/particle/particle.hpp"
[dfed1c]19
20namespace VMG
21{
22
[f003a9]23namespace Particle
24{
25
[dfed1c]26class BSpline
27{
28public:
29 BSpline(const vmg_float& width);
30
[4571da]31 vmg_float EvaluateSpline(const vmg_float& val) const
[dfed1c]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
[f003a9]39 void SetSpline(Grid& grid, const Particle& p, const int& near_field_cells) const
[ab63b6]40 {
[ac6d04]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
[cd0fed]45 std::vector<vmg_float> vals(Helper::intpow(2*near_field_cells+1,3));
[39a6d9]46 vmg_float temp_val;
[ab63b6]47 vmg_float int_val = 0.0;
[39a6d9]48 int c = 0;
49
50 Index i;
51 Vector dir;
[ab63b6]52
[39a6d9]53 const Index index_global = static_cast<Index>((p.Pos() - grid.Extent().Begin()) / grid.Extent().MeshWidth());
[ac6d04]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
[39a6d9]58 const Index index_local = index_global - grid.Global().LocalBegin() + grid.Local().Begin();
[ac6d04]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());
[ab63b6]62
[39a6d9]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
[ab63b6]66 // Iterate over all grid points which lie in the support of the interpolating B-Spline
[39a6d9]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];
[ac6d04]72 for (i.Z()=-1*near_field_cells; i.Z()<=near_field_cells; ++i.Z()) {
[ab63b6]73
74 // Compute distance from grid point to particle
[39a6d9]75 temp_val = EvaluateSpline(dir.Length());
76 vals[c++] = temp_val * p.Charge();
[ab63b6]77 int_val += temp_val;
[ac6d04]78
[39a6d9]79 dir[2] -= h[2];
[ab63b6]80 }
[39a6d9]81 dir[1] -= h[1];
82 }
83 dir[0] -= h[0];
84 }
[ab63b6]85
[ac6d04]86 // Reciprocal value of the numerically integrated spline
87 int_val = 1.0 / (int_val * grid.Extent().MeshWidth().Product());
[ab63b6]88
[39a6d9]89 c = 0;
[ac6d04]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())
[39a6d9]93 grid(index_local+i) += vals[c++] * int_val;
[ab63b6]94
95 }
96
[4571da]97 vmg_float EvaluatePotential(const vmg_float& val) const
[dfed1c]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
[1a92cf]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
[ac6d04]113 const vmg_float& GetAntiDerivativeAtZero() const
[dfed1c]114 {
115 return antid;
116 }
117
118private:
119 std::vector<Polynomial> spline_nom, spline_denom;
120 std::vector<Polynomial> potential_nom, potential_denom;
[1a92cf]121 std::vector<Polynomial> field_nom, field_denom;
[dfed1c]122 vmg_float antid;
123 std::vector<vmg_float> intervals;
124
125 vmg_float R;
126};
127
128}
129
[f003a9]130}
131
[dfed1c]132#endif /* BSPLINE_HPP_ */
Note: See TracBrowser for help on using the repository browser.