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

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

vmg: Added license files and headers (GPLv3).

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

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 * vmg - a versatile multigrid solver
3 * Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
4 *
5 * vmg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * vmg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * @file bspline.hpp
21 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
22 * @date Mon Nov 21 13:27:22 2011
23 *
24 * @brief B-Splines for molecular dynamics.
25 *
26 */
27
28#ifndef BSPLINE_HPP_
29#define BSPLINE_HPP_
30
31#include "base/helper.hpp"
32#include "base/index.hpp"
33#include "base/polynomial.hpp"
34#include "base/vector.hpp"
35#include "grid/grid.hpp"
36#include "units/particle/particle.hpp"
37
38namespace VMG
39{
40
41namespace Particle
42{
43
44class BSpline
45{
46public:
47 BSpline(const vmg_float& width);
48
49 vmg_float EvaluateSpline(const vmg_float& val) const
50 {
51 for (unsigned int i=0; i<intervals.size(); ++i)
52 if (val < intervals[i])
53 return spline_nom[i](val) / spline_denom[i](val);
54 return 0.0;
55 }
56
57 void SetSpline(Grid& grid, const Particle& p, const int& near_field_cells) const
58 {
59 assert(p.Pos().X() >= grid.Extent().Begin().X() && p.Pos().X() < grid.Extent().End().X());
60 assert(p.Pos().Y() >= grid.Extent().Begin().Y() && p.Pos().Y() < grid.Extent().End().Y());
61 assert(p.Pos().Z() >= grid.Extent().Begin().Z() && p.Pos().Z() < grid.Extent().End().Z());
62
63 std::vector<vmg_float> vals(Helper::intpow(2*near_field_cells+1,3));
64 vmg_float temp_val;
65 vmg_float int_val = 0.0;
66 int c = 0;
67
68 const int index_global_x = (p.Pos().X() - grid.Extent().Begin().X()) / grid.Extent().MeshWidth().X();
69 const int index_global_y = (p.Pos().Y() - grid.Extent().Begin().Y()) / grid.Extent().MeshWidth().Y();
70 const int index_global_z = (p.Pos().Z() - grid.Extent().Begin().Z()) / grid.Extent().MeshWidth().Z();
71
72 assert(index_global_x >= grid.Global().LocalBegin().X() && index_global_x < grid.Global().LocalEnd().X());
73 assert(index_global_y >= grid.Global().LocalBegin().Y() && index_global_y < grid.Global().LocalEnd().Y());
74 assert(index_global_z >= grid.Global().LocalBegin().Z() && index_global_z < grid.Global().LocalEnd().Z());
75
76 const int index_local_x = index_global_x - grid.Global().LocalBegin().X() + grid.Local().Begin().X();
77 const int index_local_y = index_global_y - grid.Global().LocalBegin().Y() + grid.Local().Begin().Y();
78 const int index_local_z = index_global_z - grid.Global().LocalBegin().Z() + grid.Local().Begin().Z();
79
80 assert(index_local_x >= grid.Local().Begin().X() && index_local_x < grid.Local().End().X());
81 assert(index_local_y >= grid.Local().Begin().Y() && index_local_y < grid.Local().End().Y());
82 assert(index_local_z >= grid.Local().Begin().Z() && index_local_z < grid.Local().End().Z());
83
84 const vmg_float pos_beg_x = p.Pos().X() - grid.Extent().Begin().X() - grid.Extent().MeshWidth().X() * (index_global_x - near_field_cells);
85 const vmg_float pos_beg_y = p.Pos().Y() - grid.Extent().Begin().Y() - grid.Extent().MeshWidth().Y() * (index_global_y - near_field_cells);
86 const vmg_float pos_beg_z = p.Pos().Z() - grid.Extent().Begin().Z() - grid.Extent().MeshWidth().Z() * (index_global_z - near_field_cells);
87
88 const vmg_float h_x = grid.Extent().MeshWidth().X();
89 const vmg_float h_y = grid.Extent().MeshWidth().Y();
90 const vmg_float h_z = grid.Extent().MeshWidth().Z();
91
92 // Iterate over all grid points which lie in the support of the interpolating B-Spline
93
94 vmg_float dir_x, dir_y, dir_z;
95 dir_x = pos_beg_x;
96 for (int i=-1*near_field_cells; i<=near_field_cells; ++i) {
97 dir_y = pos_beg_y;
98 for (int j=-1*near_field_cells; j<=near_field_cells; ++j) {
99 dir_z = pos_beg_z;
100 for (int k=-1*near_field_cells; k<=near_field_cells; ++k) {
101
102 // Compute distance from grid point to particle
103 temp_val = EvaluateSpline(std::sqrt(dir_x*dir_x+dir_y*dir_y+dir_z*dir_z));
104 vals[c++] = temp_val * p.Charge();
105 int_val += temp_val;
106
107 dir_z -= h_z;
108 }
109 dir_y -= h_y;
110 }
111 dir_x -= h_x;
112 }
113
114 // Reciprocal value of the numerically integrated spline
115 int_val = 1.0 / (int_val * h_x * h_y * h_z);
116
117 c = 0;
118 for (int i=-1*near_field_cells; i<=near_field_cells; ++i)
119 for (int j=-1*near_field_cells; j<=near_field_cells; ++j)
120 for (int k=-1*near_field_cells; k<=near_field_cells; ++k)
121 grid(index_local_x + i,
122 index_local_y + j,
123 index_local_z + k) += vals[c++] * int_val;
124
125 }
126
127 vmg_float EvaluatePotential(const vmg_float& val) const
128 {
129 for (unsigned int i=0; i<intervals.size(); ++i)
130 if (val < intervals[i])
131 return potential_nom[i](val) / potential_denom[i](val);
132 return potential_nom.back()(val) / potential_denom.back()(val);
133 }
134
135 vmg_float EvaluateField(const vmg_float& val) const
136 {
137 for (unsigned int i=0; i<intervals.size(); ++i)
138 if (val < intervals[i])
139 return field_nom[i](val) / field_denom[i](val);
140 return 0.0;
141 }
142
143 const vmg_float& GetAntiDerivativeAtZero() const
144 {
145 return antid;
146 }
147
148private:
149 std::vector<Polynomial> spline_nom, spline_denom;
150 std::vector<Polynomial> potential_nom, potential_denom;
151 std::vector<Polynomial> field_nom, field_denom;
152 vmg_float antid;
153 std::vector<vmg_float> intervals;
154
155 vmg_float R;
156};
157
158}
159
160}
161
162#endif /* BSPLINE_HPP_ */
Note: See TracBrowser for help on using the repository browser.