source: src/interface/interface_particles.cpp@ dfed1c

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

Major vmg update.

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

  • Property mode set to 100644
File size: 8.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
104 // Compute global and local grid index of the lower left corner of the grid cell containing the particle
105 index_global = static_cast<Index>((iter->Pos() - temp_grid->Extent().Begin()) / temp_grid->Extent().MeshWidth());
106 index_local = index_global - temp_grid->Global().BeginLocal() + temp_grid->Local().Begin();
107
108 // Iterate over all grid points which lie in the support of the interpolating B-Spline
109 for (index.X()=-1*near_field_cells-1; index.X()<=near_field_cells+1; ++index.X())
110 for (index.Y()=-1*near_field_cells-1; index.Y()<=near_field_cells+1; ++index.Y())
111 for (index.Z()=-1*near_field_cells-1; index.Z()<=near_field_cells+1; ++index.Z()) {
112
113 // Compute distance from grid point to particle
114 length = ( iter->Pos() - grid.Extent().Begin() - grid.Extent().MeshWidth() * (index_global+index) ).Length();
115
116 // If grid point lies in the support of the B-Spline, do charge interpolation
117 if (length < r_cut)
118 (*temp_grid)(index_local+index) += spl.EvaluateSpline(length) * iter->Charge();
119
120 }
121
122 }
123
124 // Communicate charges over halo
125 MG::GetComm()->CommFromGhosts(*temp_grid);
126
127 // Assign charge values to the right hand side
128 for (index.X()=0; index.X()<grid.Local().Size().X(); ++index.X())
129 for (index.Y()=0; index.Y()<grid.Local().Size().Y(); ++index.Y())
130 for (index.Z()=0; index.Z()<grid.Local().Size().Z(); ++index.Z())
131 grid(index + grid.Local().Begin()) = temp_grid->GetVal(index + temp_grid->Local().Begin());
132
133 Timer::Stop("Particle/Grid Interpolation");
134
135#ifdef DEBUG_OUTPUT
136 Grid::iterator grid_iter;
137 vmg_float charge_sum = 0.0;
138 for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
139 charge_sum += grid.GetVal(*grid_iter);
140 charge_sum = MG::GetComm()->GlobalSum(charge_sum);
141 MG::GetComm()->PrintStringOnce("Grid charge sum: %e", charge_sum);
142#endif
143}
144
145void InterfaceParticles::ExportSolution(Grid& grid)
146{
147 Timer::Start("Grid/Particle Interpolation");
148
149 Index index;
150 vmg_float length;
151 Vector dist_vec;
152
153#ifdef DEBUG_OUTPUT
154 vmg_float e = 0.0;
155 vmg_float e_long = 0.0;
156 vmg_float e_self = 0.0;
157#endif
158
159 Factory& factory = MG::GetFactory();
160 Comm* comm = MG::GetComm();
161
162 /*
163 * Get parameters and arrays
164 */
165 const vmg_int& num_particles_local = factory.GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
166 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
167 vmg_float* p = factory.GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
168
169 const vmg_float r_cut = (near_field_cells+0.5) * grid.Extent().MeshWidth().Max();
170
171 /*
172 * Initialze potential
173 */
174 for (vmg_int i=0; i<num_particles_local; ++i)
175 p[i] = 0.0;
176
177 /*
178 * Copy potential values to a grid with sufficiently large halo size.
179 * This may be optimized in future.
180 * The parameters of this grid have been set in the import step.
181 */
182 TempGrid* temp_grid = GetTempGrid(0);
183
184 for (index.X()=0; index.X()<grid.Local().Size().X(); ++index.X())
185 for (index.Y()=0; index.Y()<grid.Local().Size().Y(); ++index.Y())
186 for (index.Z()=0; index.Z()<grid.Local().Size().Z(); ++index.Z())
187 (*temp_grid)(index + temp_grid->Local().Begin()) = grid.GetVal(index + grid.Local().Begin());
188
189 comm->CommToGhosts(*temp_grid);
190
191 /*
192 * Do potential back-interpolation
193 */
194 std::list<Particle::Particle>::iterator p_iter;
195 for (p_iter=particles.begin(); p_iter!=particles.end(); ++p_iter) {
196
197 // Interpolate long range part of potential
198 p_iter->Pot() = 0.5 * (Helper::InterpolateTrilinear(p_iter->Pos(), *temp_grid) - p_iter->Charge() * spl.GetAntiDerivativeZero());
199
200#ifdef DEBUG_OUTPUT
201 e_long += 0.5 * p_iter->Charge() * Helper::InterpolateTrilinear(p_iter->Pos(), *temp_grid);
202 e_self += 0.5 * p_iter->Charge() * p_iter->Charge() * spl.GetAntiDerivativeZero();
203#endif
204
205 }
206
207 comm->CommAddPotential(particles);
208
209 /*
210 * Compute near field correction
211 */
212 Particle::LinkedCellList lc(particles, near_field_cells, grid);
213 Particle::LinkedCellList::iterator p1, p2;
214 Grid::iterator iter;
215
216 comm->CommLCListGhosts(grid, lc);
217
218 for (iter=lc.Iterators().Local().Begin(); iter!=lc.Iterators().Local().End(); ++iter)
219 for (p1=lc(*iter).begin(); p1!=lc(*iter).end(); ++p1)
220 for (index.X()=-1*near_field_cells-1; index.X()<=near_field_cells+1; ++index.X())
221 for (index.Y()=-1*near_field_cells-1; index.Y()<=near_field_cells+1; ++index.Y())
222 for (index.Z()=-1*near_field_cells-1; index.Z()<=near_field_cells+1; ++index.Z())
223 for (p2=lc(*iter+index).begin(); p2!=lc(*iter+index).end(); ++p2)
224 if (*p1 != *p2) {
225
226 length = (p2->Pos() - p1->Pos()).Length();
227
228 //TODO Rewrite this equation more efficiently
229 if (length < r_cut)
230 p1->Pot() += 0.5 * p2->Charge() / length * (0.25*M_1_PI + spl.EvaluatePotential(length));
231
232 }
233
234 comm->CommAddPotential(lc);
235
236 Timer::Stop("Grid/Particle Interpolation");
237
238#ifdef DEBUG_OUTPUT
239 vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
240
241 for (int i=0; i<num_particles_local; ++i)
242 e += p[i] * q[i];
243
244 e = comm->GlobalSumRoot(e);
245 e_long = comm->GlobalSumRoot(e_long);
246 e_self = comm->GlobalSumRoot(e_self);
247
248 comm->PrintStringOnce("E_long: %e", e_long);
249 comm->PrintStringOnce("E_short: %e", e - e_long + e_self);
250 comm->PrintStringOnce("E_self: %e", e_self);
251 comm->PrintStringOnce("E_total: %e", e);
252
253#endif /* DEBUG_OUTPUT */
254
255}
Note: See TracBrowser for help on using the repository browser.