source: src/interface/interface_particles.cpp@ ab63b6

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

Ensure that interpolating splines are normalized wrt the grid.

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

  • Property mode set to 100644
File size: 7.1 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#endif
17
18#include <cmath>
19#include <cstring>
20
21#include "base/helper.hpp"
22#include "base/index.hpp"
23#include "base/linked_cell_list.hpp"
24#include "base/timer.hpp"
25#include "base/vector.hpp"
26#include "comm/comm.hpp"
27#include "grid/grid.hpp"
28#include "grid/multigrid.hpp"
29#include "grid/tempgrid.hpp"
30#include "interface/interface_particles.hpp"
31#include "mg.hpp"
32
33using namespace VMG;
34
35void InterfaceParticles::ImportRightHandSide(Multigrid& multigrid)
36{
37 Timer::Start("Particle/Grid Interpolation");
38
39 Index index_global, index_local, index;
40 Vector pos_rel, pos_abs, grid_val;
41 vmg_float length;
42
43 Factory& factory = MG::GetFactory();
44
45 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
46
47 TempGrid* temp_grid = GetTempGrid(0);
48 Grid& grid = multigrid(multigrid.GlobalMaxLevel());
49 LocalIndices local = grid.Local();
50
51 //TODO This has to be replaced for a VMG::Vector for different mesh widths on the axis
52 const vmg_float r_cut = (near_field_cells+0.5) * grid.Extent().MeshWidth().Max();
53
54 // We don't need a boundary on this grid
55 local.End() -= local.Begin();
56 local.Begin() = 0;
57 local.BoundaryBegin1() = local.BoundaryEnd1() = 0;
58 local.BoundaryBegin2() = local.BoundaryEnd2() = 0;
59
60 // Set grid size of intermediate temporary grid
61 for (int i=0; i<3; ++i) {
62
63 if (local.HasHalo1()[i]) {
64 local.HaloBegin1()[i] = 0;
65 local.HaloEnd1()[i] = local.Begin()[i] = near_field_cells+1;
66 local.End()[i] = local.Begin()[i] + local.Size()[i];
67 }
68
69 if (local.HasHalo2()[i]) {
70 local.HaloBegin2()[i] = local.End()[i];
71 local.HaloEnd2()[i] = local.HaloBegin2()[i] + near_field_cells+1;
72 }
73
74 }
75
76 local.SizeTotal() = local.Size() +
77 local.HaloEnd1() - local.HaloBegin1() +
78 local.HaloEnd2() - local.HaloBegin2();
79
80 temp_grid->SetProperties(grid.Global(), local, grid.Extent());
81 temp_grid->Clear();
82
83 /*
84 * Distribute particles to their processes
85 */
86 particles.clear();
87 MG::GetComm()->CommParticles(grid, particles);
88
89 /*
90 * Charge assignment on the grid
91 */
92 std::list<Particle::Particle>::iterator iter;
93
94#ifdef DEBUG_OUTPUT
95 vmg_float particle_charges = 0.0;
96 for (iter=particles.begin(); iter!=particles.end(); ++iter)
97 particle_charges += iter->Charge();
98 particle_charges = MG::GetComm()->GlobalSumRoot(particle_charges);
99 MG::GetComm()->PrintStringOnce("Particle list charge sum: %e", particle_charges);
100#endif
101
102 for (iter=particles.begin(); iter!=particles.end(); ++iter)
103 spl.SetSpline(*temp_grid, *iter, near_field_cells);
104
105 // Communicate charges over halo
106 MG::GetComm()->CommFromGhosts(*temp_grid);
107
108 // Assign charge values to the right hand side
109 for (index.X()=0; index.X()<grid.Local().Size().X(); ++index.X())
110 for (index.Y()=0; index.Y()<grid.Local().Size().Y(); ++index.Y())
111 for (index.Z()=0; index.Z()<grid.Local().Size().Z(); ++index.Z())
112 grid(index + grid.Local().Begin()) = temp_grid->GetVal(index + temp_grid->Local().Begin());
113
114 Timer::Stop("Particle/Grid Interpolation");
115
116#ifdef DEBUG_OUTPUT
117 Grid::iterator grid_iter;
118 vmg_float charge_sum = 0.0;
119 for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
120 charge_sum += grid.GetVal(*grid_iter);
121 charge_sum = MG::GetComm()->GlobalSum(charge_sum);
122 MG::GetComm()->PrintStringOnce("Grid charge sum: %e", charge_sum);
123#endif
124}
125
126void InterfaceParticles::ExportSolution(Grid& grid)
127{
128 Timer::Start("Grid/Particle Interpolation");
129
130 Index index;
131 vmg_float length;
132 Vector dist_vec;
133
134#ifdef DEBUG_OUTPUT
135 vmg_float e = 0.0;
136 vmg_float e_long = 0.0;
137 vmg_float e_self = 0.0;
138#endif
139
140 Factory& factory = MG::GetFactory();
141 Comm* comm = MG::GetComm();
142
143 /*
144 * Get parameters and arrays
145 */
146 const vmg_int& num_particles_local = factory.GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
147 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
148 vmg_float* p = factory.GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
149
150 const vmg_float r_cut = (near_field_cells+0.5) * grid.Extent().MeshWidth().Max();
151
152 /*
153 * Initialze potential
154 */
155 for (vmg_int i=0; i<num_particles_local; ++i)
156 p[i] = 0.0;
157
158 /*
159 * Copy potential values to a grid with sufficiently large halo size.
160 * This may be optimized in future.
161 * The parameters of this grid have been set in the import step.
162 */
163 TempGrid* temp_grid = GetTempGrid(0);
164
165 for (index.X()=0; index.X()<grid.Local().Size().X(); ++index.X())
166 for (index.Y()=0; index.Y()<grid.Local().Size().Y(); ++index.Y())
167 for (index.Z()=0; index.Z()<grid.Local().Size().Z(); ++index.Z())
168 (*temp_grid)(index + temp_grid->Local().Begin()) = grid.GetVal(index + grid.Local().Begin());
169
170 comm->CommToGhosts(*temp_grid);
171
172 /*
173 * Do potential back-interpolation
174 */
175 std::list<Particle::Particle>::iterator p_iter;
176 for (p_iter=particles.begin(); p_iter!=particles.end(); ++p_iter) {
177
178 // Interpolate long range part of potential
179 p_iter->Pot() = 0.5 * (Helper::InterpolateTrilinear(p_iter->Pos(), *temp_grid) - p_iter->Charge() * spl.GetAntiDerivativeZero());
180
181#ifdef DEBUG_OUTPUT
182 e_long += 0.5 * p_iter->Charge() * Helper::InterpolateTrilinear(p_iter->Pos(), *temp_grid);
183 e_self += 0.5 * p_iter->Charge() * p_iter->Charge() * spl.GetAntiDerivativeZero();
184#endif
185
186 }
187
188 comm->CommAddPotential(particles);
189
190 /*
191 * Compute near field correction
192 */
193 Particle::LinkedCellList lc(particles, near_field_cells, grid);
194 Particle::LinkedCellList::iterator p1, p2;
195 Grid::iterator iter;
196
197 comm->CommLCListGhosts(grid, lc);
198
199 for (iter=lc.Iterators().Local().Begin(); iter!=lc.Iterators().Local().End(); ++iter)
200 for (p1=lc(*iter).begin(); p1!=lc(*iter).end(); ++p1)
201 for (index.X()=-1*near_field_cells-1; index.X()<=near_field_cells+1; ++index.X())
202 for (index.Y()=-1*near_field_cells-1; index.Y()<=near_field_cells+1; ++index.Y())
203 for (index.Z()=-1*near_field_cells-1; index.Z()<=near_field_cells+1; ++index.Z())
204 for (p2=lc(*iter+index).begin(); p2!=lc(*iter+index).end(); ++p2)
205 if (*p1 != *p2) {
206
207 length = (p2->Pos() - p1->Pos()).Length();
208
209 //TODO Rewrite this equation more efficiently
210 if (length < r_cut)
211 p1->Pot() += 0.5 * p2->Charge() / length * (0.25*M_1_PI + spl.EvaluatePotential(length));
212
213 }
214
215 comm->CommAddPotential(lc);
216
217 Timer::Stop("Grid/Particle Interpolation");
218
219#ifdef DEBUG_OUTPUT
220 vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
221
222 for (int i=0; i<num_particles_local; ++i)
223 e += p[i] * q[i];
224
225 e = comm->GlobalSumRoot(e);
226 e_long = comm->GlobalSumRoot(e_long);
227 e_self = comm->GlobalSumRoot(e_self);
228
229 comm->PrintStringOnce("E_long: %e", e_long);
230 comm->PrintStringOnce("E_short: %e", e - e_long + e_self);
231 comm->PrintStringOnce("E_self: %e", e_self);
232 comm->PrintStringOnce("E_total: %e", e);
233
234#endif /* DEBUG_OUTPUT */
235
236}
Note: See TracBrowser for help on using the repository browser.