source: src/interface/interface_particles.cpp@ 716da7

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

Fix energy calculation.

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

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/**
2 * @file interface_particles.cpp
3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
4 * @date Mon Apr 18 12:56:48 2011
5 *
6 * @brief VMG::InterfaceParticles
7 *
8 */
9
10#ifdef HAVE_CONFIG_H
11#include <config.h>
12#endif
13
14#ifdef HAVE_MPI
15#include <mpi.h>
16#ifdef HAVE_MARMOT
17#include <enhancempicalls.h>
18#include <sourceinfompicalls.h>
19#endif
20#endif
21
22#include <cmath>
23#include <cstring>
24
25#include "base/helper.hpp"
26#include "base/index.hpp"
27#include "base/interpolate_polynomial.hpp"
28#include "base/linked_cell_list.hpp"
29#include "base/math.hpp"
30#include "base/vector.hpp"
31#include "comm/comm.hpp"
32#include "grid/grid.hpp"
33#include "grid/multigrid.hpp"
34#include "grid/tempgrid.hpp"
35#include "interface/interface_particles.hpp"
36#include "mg.hpp"
37
38using namespace VMG;
39
40void InterfaceParticles::ImportRightHandSide(Multigrid& multigrid)
41{
42 Index index_global, index_local, index;
43 Vector pos_rel, pos_abs, grid_val;
44
45 Factory& factory = MG::GetFactory();
46 Comm& comm = *MG::GetComm();
47
48 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
49
50 Grid& grid = multigrid(multigrid.MaxLevel());
51 Grid& particle_grid = comm.GetParticleGrid();
52
53 particle_grid.Clear();
54
55 assert(particle_grid.Global().LocalSize().IsComponentwiseGreater(near_field_cells));
56
57 /*
58 * Distribute particles to their processes
59 */
60 particles.clear();
61 comm.CommParticles(grid, particles);
62
63 /*
64 * Charge assignment on the grid
65 */
66 std::list<Particle::Particle>::iterator iter;
67
68#ifdef DEBUG_OUTPUT
69 vmg_float particle_charges = 0.0;
70 for (iter=particles.begin(); iter!=particles.end(); ++iter)
71 particle_charges += iter->Charge();
72 particle_charges = MG::GetComm()->GlobalSumRoot(particle_charges);
73 comm.PrintStringOnce("Particle list charge sum: %e", particle_charges);
74 comm.PrintString("Local number of particles: %d", particles.size());
75#endif
76
77 for (iter=particles.begin(); iter!=particles.end(); ++iter)
78 spl.SetSpline(particle_grid, *iter, near_field_cells);
79
80 // Communicate charges over halo
81 comm.CommFromGhosts(particle_grid);
82
83 // Assign charge values to the right hand side
84 for (index.X()=0; index.X()<grid.Local().Size().X(); ++index.X())
85 for (index.Y()=0; index.Y()<grid.Local().Size().Y(); ++index.Y())
86 for (index.Z()=0; index.Z()<grid.Local().Size().Z(); ++index.Z())
87 grid(index + grid.Local().Begin()) = 4.0 * Math::pi * particle_grid.GetVal(index + particle_grid.Local().Begin());
88
89#ifdef DEBUG_OUTPUT
90 Grid::iterator grid_iter;
91 vmg_float charge_sum = 0.0;
92 for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
93 charge_sum += grid.GetVal(*grid_iter);
94 charge_sum = MG::GetComm()->GlobalSum(charge_sum);
95 comm.PrintStringOnce("Grid charge sum: %e", charge_sum);
96#endif
97}
98
99void InterfaceParticles::ExportSolution(Grid& grid)
100{
101 Index index;
102 vmg_float length;
103 Vector dist_vec;
104 InterpolatePolynomial ip(8);
105
106#ifdef DEBUG_OUTPUT
107 vmg_float e = 0.0;
108 vmg_float e_long = 0.0;
109 vmg_float e_self = 0.0;
110 vmg_float e_short = 0.0;
111 vmg_float e_shortcorr = 0.0;
112#endif
113
114 Factory& factory = MG::GetFactory();
115 Comm& comm = *MG::GetComm();
116
117 /*
118 * Get parameters and arrays
119 */
120 const vmg_int& num_particles_local = factory.GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
121 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
122 vmg_float* p = factory.GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
123 vmg_float* f = factory.GetObjectStorageArray<vmg_float>("PARTICLE_FIELD_ARRAY");
124
125 const vmg_float r_cut = (near_field_cells+0.5) * grid.Extent().MeshWidth().Max();
126
127 /*
128 * Initialize potential
129 */
130 for (vmg_int i=0; i<num_particles_local; ++i) {
131 p[i] = 0.0;
132 for (vmg_int j=0;j<3;++j)
133 f[3*i+j] = 0.;
134 }
135
136 /*
137 * Copy potential values to a grid with sufficiently large halo size.
138 * This may be optimized in future.
139 * The parameters of this grid have been set in the import step.
140 */
141 Grid& particle_grid = comm.GetParticleGrid();
142
143 for (index.X()=0; index.X()<grid.Local().Size().X(); ++index.X())
144 for (index.Y()=0; index.Y()<grid.Local().Size().Y(); ++index.Y())
145 for (index.Z()=0; index.Z()<grid.Local().Size().Z(); ++index.Z())
146 particle_grid(index + particle_grid.Local().Begin()) = grid.GetVal(index + grid.Local().Begin());
147
148 comm.CommToGhosts(particle_grid);
149
150 /*
151 * Do potential back-interpolation
152 */
153 std::list<Particle::Particle>::iterator p_iter;
154 for (p_iter=particles.begin(); p_iter!=particles.end(); ++p_iter) {
155
156 const Index global_index = (p_iter->Pos() - particle_grid.Extent().Begin()) / particle_grid.Extent().MeshWidth();
157 const Index local_index = global_index - particle_grid.Global().LocalBegin() + particle_grid.Local().Begin();
158
159 ip.ComputeCoefficients(particle_grid, local_index);
160
161 // Interpolate long range part of potential minus self-interaction
162 p_iter->Pot() = ip.Evaluate(p_iter->Pos()) - p_iter->Charge() * spl.GetAntiDerivativeAtZero();
163 p_iter->Field() = ip.EvaluateGradient(p_iter->Pos());
164
165#ifdef DEBUG_OUTPUT
166 // TODO The scaling with 1/2 may not be correct. Check that.
167 e_long += 0.5 * p_iter->Charge() * Helper::InterpolateTrilinear(p_iter->Pos(), particle_grid);
168 e_self += 0.5 * p_iter->Charge() * p_iter->Charge() * spl.GetAntiDerivativeAtZero();
169#endif
170
171 }
172
173 /*
174 * Compute near field correction
175 */
176 Particle::LinkedCellList lc(particles, near_field_cells, grid);
177 Particle::LinkedCellList::iterator p1, p2;
178 Grid::iterator iter;
179
180 comm.CommLCListToGhosts(lc);
181
182 for (iter=lc.Iterators().Local().Begin(); iter!=lc.Iterators().Local().End(); ++iter) {
183 std::list<Particle::Particle*>& lc_1 = lc(*iter);
184 for (p1=lc_1.begin(); p1!=lc_1.end(); ++p1)
185 for (index.X()=-1*near_field_cells-1; index.X()<=near_field_cells+1; ++index.X())
186 for (index.Y()=-1*near_field_cells-1; index.Y()<=near_field_cells+1; ++index.Y())
187 for (index.Z()=-1*near_field_cells-1; index.Z()<=near_field_cells+1; ++index.Z()) {
188
189 std::list<Particle::Particle*>& lc_2 = lc(*iter+index);
190
191 for (p2=lc_2.begin(); p2!=lc_2.end(); ++p2)
192 if (*p1 != *p2) {
193
194 length = ((*p2)->Pos() - (*p1)->Pos()).Length();
195
196 //TODO Rewrite this equation more efficiently
197 if (length < r_cut) {
198 (*p1)->Pot() += (*p2)->Charge() / length * (1.0 + spl.EvaluatePotential(length));
199
200#ifdef DEBUG_OUTPUT
201 e_short += 0.5 * (*p1)->Charge() * (*p2)->Charge() / length;
202 e_shortcorr -= 0.5 * (*p1)->Charge() * (*p2)->Charge() / length * spl.EvaluatePotential(length);
203#endif
204 }
205 }
206 }
207 }
208
209 comm.CommParticlesBack(particles);
210
211#ifdef DEBUG_OUTPUT
212 vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
213
214 for (int i=0; i<num_particles_local; ++i)
215 e += 0.5 * p[i] * q[i];
216
217 e = comm.GlobalSumRoot(e);
218 e_long = comm.GlobalSumRoot(e_long);
219 e_self = comm.GlobalSumRoot(e_self);
220 e_short = comm.GlobalSumRoot(e_short);
221 e_shortcorr = comm.GlobalSumRoot(e_shortcorr);
222
223 comm.PrintStringOnce("E_long: %e", e_long);
224 comm.PrintStringOnce("E_short: %e", e_short);
225 comm.PrintStringOnce("E_shortcorr: %e", e_shortcorr);
226 comm.PrintStringOnce("E_short*: %e", e - e_long + e_self);
227 comm.PrintStringOnce("E_self: %e", e_self);
228 comm.PrintStringOnce("E_total: %e", e);
229
230#endif /* DEBUG_OUTPUT */
231
232}
Note: See TracBrowser for help on using the repository browser.