source: src/units/particle/interface_particles.cpp@ d13e27

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

vmg: Work on output verbosity.

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

  • Property mode set to 100644
File size: 8.4 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 interface_particles.cpp
21 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
22 * @date Mon Apr 18 12:56:48 2011
23 *
24 * @brief VMG::InterfaceParticles
25 *
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32#ifdef HAVE_MPI
33#include <mpi.h>
34#ifdef HAVE_MARMOT
35#include <enhancempicalls.h>
36#include <sourceinfompicalls.h>
37#endif
38#endif
39
40#include <algorithm>
41#include <cmath>
42#include <cstring>
43
44#include "base/helper.hpp"
45#include "base/index.hpp"
46#include "base/math.hpp"
47#include "base/vector.hpp"
48#include "comm/comm.hpp"
49#include "grid/grid.hpp"
50#include "grid/multigrid.hpp"
51#include "grid/tempgrid.hpp"
52#include "units/particle/comm_mpi_particle.hpp"
53#include "units/particle/interface_particles.hpp"
54#include "units/particle/interpolation.hpp"
55#include "units/particle/linked_cell_list.hpp"
56#include "mg.hpp"
57
58using namespace VMG;
59
60void InterfaceParticles::ImportRightHandSide(Multigrid& multigrid)
61{
62 Index index_global, index_local, index;
63 Vector pos_rel, pos_abs, grid_val;
64
65 Factory& factory = MG::GetFactory();
66 Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
67
68 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
69
70 Grid& grid = multigrid(multigrid.MaxLevel());
71 Grid& particle_grid = comm.GetParticleGrid();
72
73 particle_grid.Clear();
74
75 assert(particle_grid.Global().LocalSize().IsComponentwiseGreater(near_field_cells));
76
77 /*
78 * Distribute particles to their processes
79 */
80 particles.clear();
81 comm.CommParticles(grid, particles);
82
83 /*
84 * Charge assignment on the grid
85 */
86 std::list<Particle::Particle>::iterator iter;
87
88#ifdef OUTPUT_DEBUG
89 vmg_float particle_charges = 0.0;
90 for (iter=particles.begin(); iter!=particles.end(); ++iter)
91 particle_charges += iter->Charge();
92 particle_charges = MG::GetComm()->GlobalSumRoot(particle_charges);
93 comm.PrintOnce(Debug, "Particle list charge sum: %e", particle_charges);
94 comm.Print(Debug, "Local number of particles: %d", particles.size());
95#endif
96
97 for (iter=particles.begin(); iter!=particles.end(); ++iter)
98 spl.SetSpline(particle_grid, *iter);
99
100 // Communicate charges over halo
101 comm.CommFromGhosts(particle_grid);
102
103 // Assign charge values to the right hand side
104 for (int i=0; i<grid.Local().Size().X(); ++i)
105 for (int j=0; j<grid.Local().Size().Y(); ++j)
106 for (int k=0; k<grid.Local().Size().Z(); ++k)
107 grid(grid.Local().Begin().X() + i,
108 grid.Local().Begin().Y() + j,
109 grid.Local().Begin().Z() + k) = 4.0 * Math::pi *
110 particle_grid.GetVal(particle_grid.Local().Begin().X() + i,
111 particle_grid.Local().Begin().Y() + j,
112 particle_grid.Local().Begin().Z() + k);
113
114#ifdef OUTPUT_DEBUG
115 Grid::iterator grid_iter;
116 vmg_float charge_sum = 0.0;
117 for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
118 charge_sum += grid.GetVal(*grid_iter);
119 charge_sum = MG::GetComm()->GlobalSum(charge_sum);
120 comm.PrintOnce(Debug, "Grid charge sum: %e", charge_sum);
121#endif
122}
123
124void InterfaceParticles::ExportSolution(Grid& grid)
125{
126 Index i;
127
128#ifdef OUTPUT_DEBUG
129 vmg_float e = 0.0;
130 vmg_float e_long = 0.0;
131 vmg_float e_self = 0.0;
132 vmg_float e_short_peak = 0.0;
133 vmg_float e_short_spline = 0.0;
134#endif
135
136 Factory& factory = MG::GetFactory();
137 Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
138
139 /*
140 * Get parameters and arrays
141 */
142 const vmg_int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
143 const vmg_int& interpolation_degree = factory.GetObjectStorageVal<int>("PARTICLE_INTERPOLATION_DEGREE");
144
145 Particle::Interpolation ip(interpolation_degree);
146
147 const vmg_float r_cut = near_field_cells * grid.Extent().MeshWidth().Max();
148
149 /*
150 * Copy potential values to a grid with sufficiently large halo size.
151 * This may be optimized in future.
152 * The parameters of this grid have been set in the import step.
153 */
154 Grid& particle_grid = comm.GetParticleGrid();
155
156 for (i.X()=0; i.X()<grid.Local().Size().X(); ++i.X())
157 for (i.Y()=0; i.Y()<grid.Local().Size().Y(); ++i.Y())
158 for (i.Z()=0; i.Z()<grid.Local().Size().Z(); ++i.Z())
159 particle_grid(i + particle_grid.Local().Begin()) = grid.GetVal(i + grid.Local().Begin());
160
161 comm.CommToGhosts(particle_grid);
162
163 /*
164 * Compute potentials
165 */
166 Particle::LinkedCellList lc(particles, near_field_cells, grid);
167 Particle::LinkedCellList::iterator p1, p2;
168 Grid::iterator iter;
169
170 comm.CommLCListToGhosts(lc);
171
172 for (int i=lc.Local().Begin().X(); i<lc.Local().End().X(); ++i)
173 for (int j=lc.Local().Begin().Y(); j<lc.Local().End().Y(); ++j)
174 for (int k=lc.Local().Begin().Z(); k<lc.Local().End().Z(); ++k) {
175
176 if (lc(i,j,k).size() > 0)
177 ip.ComputeCoefficients(particle_grid, Index(i,j,k) - lc.Local().Begin() + particle_grid.Local().Begin());
178
179 for (p1=lc(i,j,k).begin(); p1!=lc(i,j,k).end(); ++p1) {
180
181 // Interpolate long-range part of potential and electric field
182 ip.Evaluate(**p1);
183
184 // Subtract self-induced potential
185 (*p1)->Pot() -= (*p1)->Charge() * spl.GetAntiDerivativeAtZero();
186
187#ifdef OUTPUT_DEBUG
188 e_long += 0.5 * (*p1)->Charge() * ip.EvaluatePotentialLR(**p1);
189 e_self += 0.5 * (*p1)->Charge() * (*p1)->Charge() * spl.GetAntiDerivativeAtZero();
190#endif
191
192 for (int dx=-1*near_field_cells; dx<=near_field_cells; ++dx)
193 for (int dy=-1*near_field_cells; dy<=near_field_cells; ++dy)
194 for (int dz=-1*near_field_cells; dz<=near_field_cells; ++dz) {
195
196 for (p2=lc(i+dx,j+dy,k+dz).begin(); p2!=lc(i+dx,j+dy,k+dz).end(); ++p2)
197
198 if (*p1 != *p2) {
199
200 const Vector dir = (*p1)->Pos() - (*p2)->Pos();
201 const vmg_float length = dir.Length();
202
203 if (length < r_cut) {
204
205 (*p1)->Pot() += (*p2)->Charge() / length * (1.0 + spl.EvaluatePotential(length));
206 (*p1)->Field() += (*p2)->Charge() * dir * spl.EvaluateField(length);
207
208#ifdef OUTPUT_DEBUG
209 e_short_peak += 0.5 * (*p1)->Charge() * (*p2)->Charge() / length;
210 e_short_spline += 0.5 * (*p1)->Charge() * (*p2)->Charge() / length * spl.EvaluatePotential(length);
211#endif
212 }
213 }
214 }
215 }
216 }
217
218 /* Remove average force term */
219 Vector average_force = 0.0;
220 for (std::list<Particle::Particle>::const_iterator iter=particles.begin(); iter!=particles.end(); ++iter)
221 average_force += iter->Charge() * iter->Field();
222 const vmg_int& npl = MG::GetFactory().GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
223 const vmg_int num_particles_global = comm.GlobalSum(npl);
224 average_force /= num_particles_global;
225 comm.GlobalSumArray(average_force.vec(), 3);
226 for (std::list<Particle::Particle>::iterator iter=particles.begin(); iter!=particles.end(); ++iter)
227 iter->Field() -= average_force / iter->Charge();
228
229 comm.CommParticlesBack(particles);
230
231#ifdef OUTPUT_DEBUG
232 vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
233 const vmg_int& num_particles_local = factory.GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
234 const vmg_float* p = factory.GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
235 const vmg_float* f = factory.GetObjectStorageArray<vmg_float>("PARTICLE_FIELD_ARRAY");
236
237
238 e_long = comm.GlobalSumRoot(e_long);
239 e_short_peak = comm.GlobalSumRoot(e_short_peak);
240 e_short_spline = comm.GlobalSumRoot(e_short_spline);
241 e_self = comm.GlobalSumRoot(e_self);
242
243 for (int j=0; j<num_particles_local; ++j)
244 e += 0.5 * p[j] * q[j];
245 e = comm.GlobalSumRoot(e);
246
247 comm.PrintOnce(Debug, "E_long: %e", e_long);
248 comm.PrintOnce(Debug, "E_short_peak: %e", e_short_peak);
249 comm.PrintOnce(Debug, "E_short_spline: %e", e_short_spline);
250 comm.PrintOnce(Debug, "E_self: %e", e_self);
251 comm.PrintOnce(Debug, "E_total: %e", e);
252 comm.PrintOnce(Debug, "E_total*: %e", e_long + e_short_peak + e_short_spline - e_self);
253#endif
254}
Note: See TracBrowser for help on using the repository browser.