source: src/units/particle/bspline.hpp@ 06e153

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

vmg: Improved serial performance.

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

  • Property mode set to 100644
File size: 4.9 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
[06e153]50 const int index_global_x = (p.Pos().X() - grid.Extent().Begin().X()) / grid.Extent().MeshWidth().X();
51 const int index_global_y = (p.Pos().Y() - grid.Extent().Begin().Y()) / grid.Extent().MeshWidth().Y();
52 const int index_global_z = (p.Pos().Z() - grid.Extent().Begin().Z()) / grid.Extent().MeshWidth().Z();
[ab63b6]53
[06e153]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());
[ac6d04]57
[06e153]58 const int index_local_x = index_global_x - grid.Global().LocalBegin().X() + grid.Local().Begin().X();
59 const int index_local_y = index_global_y - grid.Global().LocalBegin().Y() + grid.Local().Begin().Y();
60 const int index_local_z = index_global_z - grid.Global().LocalBegin().Z() + grid.Local().Begin().Z();
[ab63b6]61
[06e153]62 assert(index_local_x >= grid.Local().Begin().X() && index_local_x < grid.Local().End().X());
63 assert(index_local_y >= grid.Local().Begin().Y() && index_local_y < grid.Local().End().Y());
64 assert(index_local_z >= grid.Local().Begin().Z() && index_local_z < grid.Local().End().Z());
65
66 const vmg_float pos_beg_x = p.Pos().X() - grid.Extent().Begin().X() - grid.Extent().MeshWidth().X() * (index_global_x - near_field_cells);
67 const vmg_float pos_beg_y = p.Pos().Y() - grid.Extent().Begin().Y() - grid.Extent().MeshWidth().Y() * (index_global_y - near_field_cells);
68 const vmg_float pos_beg_z = p.Pos().Z() - grid.Extent().Begin().Z() - grid.Extent().MeshWidth().Z() * (index_global_z - near_field_cells);
69
70 const vmg_float h_x = grid.Extent().MeshWidth().X();
71 const vmg_float h_y = grid.Extent().MeshWidth().Y();
72 const vmg_float h_z = grid.Extent().MeshWidth().Z();
[39a6d9]73
[ab63b6]74 // Iterate over all grid points which lie in the support of the interpolating B-Spline
[06e153]75
76 vmg_float dir_x, dir_y, dir_z;
77 dir_x = pos_beg_x;
78 for (int i=-1*near_field_cells; i<=near_field_cells; ++i) {
79 dir_y = pos_beg_y;
80 for (int j=-1*near_field_cells; j<=near_field_cells; ++j) {
81 dir_z = pos_beg_z;
82 for (int k=-1*near_field_cells; k<=near_field_cells; ++k) {
[ab63b6]83
84 // Compute distance from grid point to particle
[06e153]85 temp_val = EvaluateSpline(std::sqrt(dir_x*dir_x+dir_y*dir_y+dir_z*dir_z));
[39a6d9]86 vals[c++] = temp_val * p.Charge();
[ab63b6]87 int_val += temp_val;
[ac6d04]88
[06e153]89 dir_z -= h_z;
[ab63b6]90 }
[06e153]91 dir_y -= h_y;
[39a6d9]92 }
[06e153]93 dir_x -= h_x;
[39a6d9]94 }
[ab63b6]95
[ac6d04]96 // Reciprocal value of the numerically integrated spline
[06e153]97 int_val = 1.0 / (int_val * h_x * h_y * h_z);
[ab63b6]98
[39a6d9]99 c = 0;
[06e153]100 for (int i=-1*near_field_cells; i<=near_field_cells; ++i)
101 for (int j=-1*near_field_cells; j<=near_field_cells; ++j)
102 for (int k=-1*near_field_cells; k<=near_field_cells; ++k)
103 grid(index_local_x + i,
104 index_local_y + j,
105 index_local_z + k) += vals[c++] * int_val;
[ab63b6]106
107 }
108
[4571da]109 vmg_float EvaluatePotential(const vmg_float& val) const
[dfed1c]110 {
111 for (unsigned int i=0; i<intervals.size(); ++i)
112 if (val < intervals[i])
113 return potential_nom[i](val) / potential_denom[i](val);
114 return potential_nom.back()(val) / potential_denom.back()(val);
115 }
116
[1a92cf]117 vmg_float EvaluateField(const vmg_float& val) const
118 {
119 for (unsigned int i=0; i<intervals.size(); ++i)
120 if (val < intervals[i])
121 return field_nom[i](val) / field_denom[i](val);
122 return 0.0;
123 }
124
[ac6d04]125 const vmg_float& GetAntiDerivativeAtZero() const
[dfed1c]126 {
127 return antid;
128 }
129
130private:
131 std::vector<Polynomial> spline_nom, spline_denom;
132 std::vector<Polynomial> potential_nom, potential_denom;
[1a92cf]133 std::vector<Polynomial> field_nom, field_denom;
[dfed1c]134 vmg_float antid;
135 std::vector<vmg_float> intervals;
136
137 vmg_float R;
138};
139
140}
141
[f003a9]142}
143
[dfed1c]144#endif /* BSPLINE_HPP_ */
Note: See TracBrowser for help on using the repository browser.