source: src/interface/interface_particles.cpp@ 01be70

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

Merge remote branch 'heber/Bugfixes_Tremolo_implementation'

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

  • Property mode set to 100644
File size: 7.0 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
42 Factory& factory = MG::GetFactory();
43
44 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
45
46 TempGrid* temp_grid = GetTempGrid(0);
47 Grid& grid = multigrid(multigrid.GlobalMaxLevel());
48 LocalIndices local = grid.Local();
49
50 // We don't need a boundary on this grid
51 local.End() -= local.Begin();
52 local.Begin() = 0;
53 local.BoundaryBegin1() = local.BoundaryEnd1() = 0;
54 local.BoundaryBegin2() = local.BoundaryEnd2() = 0;
55
56 // Set grid size of intermediate temporary grid
57 for (int i=0; i<3; ++i) {
58
59 if (local.HasHalo1()[i]) {
60 local.HaloBegin1()[i] = 0;
61 local.HaloEnd1()[i] = local.Begin()[i] = near_field_cells+1;
62 local.End()[i] = local.Begin()[i] + local.Size()[i];
63 }
64
65 if (local.HasHalo2()[i]) {
66 local.HaloBegin2()[i] = local.End()[i];
67 local.HaloEnd2()[i] = local.HaloBegin2()[i] + near_field_cells+1;
68 }
69
70 }
71
72 local.SizeTotal() = local.Size() +
73 local.HaloEnd1() - local.HaloBegin1() +
74 local.HaloEnd2() - local.HaloBegin2();
75
76 temp_grid->SetProperties(grid.Global(), local, grid.Extent());
77 temp_grid->Clear();
78
79 /*
80 * Distribute particles to their processes
81 */
82 particles.clear();
83 MG::GetComm()->CommParticles(grid, particles);
84
85 /*
86 * Charge assignment on the grid
87 */
88 std::list<Particle::Particle>::iterator iter;
89
90#ifdef DEBUG_OUTPUT
91 vmg_float particle_charges = 0.0;
92 for (iter=particles.begin(); iter!=particles.end(); ++iter)
93 particle_charges += iter->Charge();
94 particle_charges = MG::GetComm()->GlobalSumRoot(particle_charges);
95 MG::GetComm()->PrintStringOnce("Particle list charge sum: %e", particle_charges);
96#endif
97
98 for (iter=particles.begin(); iter!=particles.end(); ++iter)
99 spl.SetSpline(*temp_grid, *iter, near_field_cells);
100
101 // Communicate charges over halo
102 MG::GetComm()->CommFromGhosts(*temp_grid);
103
104 // Assign charge values to the right hand side
105 for (index.X()=0; index.X()<grid.Local().Size().X(); ++index.X())
106 for (index.Y()=0; index.Y()<grid.Local().Size().Y(); ++index.Y())
107 for (index.Z()=0; index.Z()<grid.Local().Size().Z(); ++index.Z())
108 grid(index + grid.Local().Begin()) = temp_grid->GetVal(index + temp_grid->Local().Begin());
109
110 Timer::Stop("Particle/Grid Interpolation");
111
112#ifdef DEBUG_OUTPUT
113 Grid::iterator grid_iter;
114 vmg_float charge_sum = 0.0;
115 for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
116 charge_sum += grid.GetVal(*grid_iter);
117 charge_sum = MG::GetComm()->GlobalSum(charge_sum);
118 MG::GetComm()->PrintStringOnce("Grid charge sum: %e", charge_sum);
119#endif
120}
121
122void InterfaceParticles::ExportSolution(Grid& grid)
123{
124 Timer::Start("Grid/Particle Interpolation");
125
126 Index index;
127 vmg_float length;
128 Vector dist_vec;
129
130#ifdef DEBUG_OUTPUT
131 vmg_float e = 0.0;
132 vmg_float e_long = 0.0;
133 vmg_float e_self = 0.0;
134#endif
135
136 Factory& factory = MG::GetFactory();
137 Comm* comm = MG::GetComm();
138
139 /*
140 * Get parameters and arrays
141 */
142 const vmg_int& num_particles_local = factory.GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
143 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
144 vmg_float* p = factory.GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
145 vmg_float* f = factory.GetObjectStorageArray<vmg_float>("PARTICLE_FORCE_ARRAY");
146
147 const vmg_float r_cut = (near_field_cells+0.5) * grid.Extent().MeshWidth().Max();
148
149 /*
150 * Initialize potential
151 */
152 for (vmg_int i=0; i<num_particles_local; ++i) {
153 p[i] = 0.0;
154 for (vmg_int j=0;j<3;++j)
155 f[3*i+j] = 0.;
156 }
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.