source: src/units/particle/interface_particles.cpp@ 5c22be

Last change on this file since 5c22be was 5c22be, checked in by Frederik Heber <heber@…>, 10 years ago

tempcommit: Small changes to interface_particles.

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