| [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 |
|
|---|
| 58 | using namespace VMG;
|
|---|
| 59 |
|
|---|
| 60 | void 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 |
|
|---|
| [894a5f] | 73 | particle_grid.Clear();
|
|---|
| [48b662] | 74 |
|
|---|
| [ac6d04] | 75 | assert(particle_grid.Global().LocalSize().IsComponentwiseGreater(near_field_cells));
|
|---|
| 76 |
|
|---|
| [dfed1c] | 77 | /*
|
|---|
| 78 | * Distribute particles to their processes
|
|---|
| 79 | */
|
|---|
| 80 | particles.clear();
|
|---|
| [894a5f] | 81 | comm.CommParticles(grid, particles);
|
|---|
| [dfed1c] | 82 |
|
|---|
| 83 | /*
|
|---|
| 84 | * Charge assignment on the grid
|
|---|
| 85 | */
|
|---|
| 86 | std::list<Particle::Particle>::iterator iter;
|
|---|
| 87 |
|
|---|
| 88 | #ifdef DEBUG_OUTPUT
|
|---|
| 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);
|
|---|
| [894a5f] | 93 | comm.PrintStringOnce("Particle list charge sum: %e", particle_charges);
|
|---|
| [ac6d04] | 94 | comm.PrintString("Local number of particles: %d", particles.size());
|
|---|
| [dfed1c] | 95 | #endif
|
|---|
| [48b662] | 96 |
|
|---|
| [ab63b6] | 97 | for (iter=particles.begin(); iter!=particles.end(); ++iter)
|
|---|
| [36d56c] | 98 | spl.SetSpline(particle_grid, *iter);
|
|---|
| [48b662] | 99 |
|
|---|
| [dfed1c] | 100 | // Communicate charges over halo
|
|---|
| [894a5f] | 101 | comm.CommFromGhosts(particle_grid);
|
|---|
| [48b662] | 102 |
|
|---|
| [dfed1c] | 103 | // Assign charge values to the right hand side
|
|---|
| [06e153] | 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);
|
|---|
| [dfed1c] | 113 |
|
|---|
| 114 | #ifdef DEBUG_OUTPUT
|
|---|
| 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);
|
|---|
| [894a5f] | 120 | comm.PrintStringOnce("Grid charge sum: %e", charge_sum);
|
|---|
| [dfed1c] | 121 | #endif
|
|---|
| [48b662] | 122 | }
|
|---|
| 123 |
|
|---|
| 124 | void InterfaceParticles::ExportSolution(Grid& grid)
|
|---|
| 125 | {
|
|---|
| [4571da] | 126 | Index i;
|
|---|
| [48b662] | 127 |
|
|---|
| [dfed1c] | 128 | #ifdef DEBUG_OUTPUT
|
|---|
| 129 | vmg_float e = 0.0;
|
|---|
| 130 | vmg_float e_long = 0.0;
|
|---|
| 131 | vmg_float e_self = 0.0;
|
|---|
| [4571da] | 132 | vmg_float e_short_peak = 0.0;
|
|---|
| 133 | vmg_float e_short_spline = 0.0;
|
|---|
| [dfed1c] | 134 | #endif
|
|---|
| [48b662] | 135 |
|
|---|
| [dfed1c] | 136 | Factory& factory = MG::GetFactory();
|
|---|
| [f003a9] | 137 | Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
|
|---|
| [dfed1c] | 138 |
|
|---|
| 139 | /*
|
|---|
| 140 | * Get parameters and arrays
|
|---|
| 141 | */
|
|---|
| [a150d0] | 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");
|
|---|
| [dfed1c] | 144 |
|
|---|
| [f003a9] | 145 | Particle::Interpolation ip(interpolation_degree);
|
|---|
| [a150d0] | 146 |
|
|---|
| [4571da] | 147 | const vmg_float r_cut = near_field_cells * grid.Extent().MeshWidth().Max();
|
|---|
| [dfed1c] | 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 | */
|
|---|
| [894a5f] | 154 | Grid& particle_grid = comm.GetParticleGrid();
|
|---|
| [48b662] | 155 |
|
|---|
| [4571da] | 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());
|
|---|
| [48b662] | 160 |
|
|---|
| [894a5f] | 161 | comm.CommToGhosts(particle_grid);
|
|---|
| [48b662] | 162 |
|
|---|
| [dfed1c] | 163 | /*
|
|---|
| [b2154a3] | 164 | * Compute potentials
|
|---|
| [dfed1c] | 165 | */
|
|---|
| 166 | Particle::LinkedCellList lc(particles, near_field_cells, grid);
|
|---|
| 167 | Particle::LinkedCellList::iterator p1, p2;
|
|---|
| 168 | Grid::iterator iter;
|
|---|
| [48b662] | 169 |
|
|---|
| [716da7] | 170 | comm.CommLCListToGhosts(lc);
|
|---|
| [48b662] | 171 |
|
|---|
| [4e8206] | 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) {
|
|---|
| [b2154a3] | 175 |
|
|---|
| [4e8206] | 176 | if (lc(i,j,k).size() > 0)
|
|---|
| 177 | ip.ComputeCoefficients(particle_grid, Index(i,j,k) - lc.Local().Begin() + particle_grid.Local().Begin());
|
|---|
| [b2154a3] | 178 |
|
|---|
| [4e8206] | 179 | for (p1=lc(i,j,k).begin(); p1!=lc(i,j,k).end(); ++p1) {
|
|---|
| [b2154a3] | 180 |
|
|---|
| [ef94e7] | 181 | // Interpolate long-range part of potential and electric field
|
|---|
| [f003a9] | 182 | ip.Evaluate(**p1);
|
|---|
| [ef94e7] | 183 |
|
|---|
| 184 | // Subtract self-induced potential
|
|---|
| [4e8206] | 185 | (*p1)->Pot() -= (*p1)->Charge() * spl.GetAntiDerivativeAtZero();
|
|---|
| [b2154a3] | 186 |
|
|---|
| 187 | #ifdef DEBUG_OUTPUT
|
|---|
| [f003a9] | 188 | e_long += 0.5 * (*p1)->Charge() * ip.EvaluatePotentialLR(**p1);
|
|---|
| [4e8206] | 189 | e_self += 0.5 * (*p1)->Charge() * (*p1)->Charge() * spl.GetAntiDerivativeAtZero();
|
|---|
| [b2154a3] | 190 | #endif
|
|---|
| 191 |
|
|---|
| [4e8206] | 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) {
|
|---|
| [716da7] | 195 |
|
|---|
| [4e8206] | 196 | for (p2=lc(i+dx,j+dy,k+dz).begin(); p2!=lc(i+dx,j+dy,k+dz).end(); ++p2)
|
|---|
| [716da7] | 197 |
|
|---|
| [4e8206] | 198 | if (*p1 != *p2) {
|
|---|
| [48b662] | 199 |
|
|---|
| [4e8206] | 200 | const Vector dir = (*p1)->Pos() - (*p2)->Pos();
|
|---|
| 201 | const vmg_float length = dir.Length();
|
|---|
| [48b662] | 202 |
|
|---|
| [4e8206] | 203 | if (length < r_cut) {
|
|---|
| [1a92cf] | 204 |
|
|---|
| [4e8206] | 205 | (*p1)->Pot() += (*p2)->Charge() / length * (1.0 + spl.EvaluatePotential(length));
|
|---|
| 206 | (*p1)->Field() += (*p2)->Charge() * dir * spl.EvaluateField(length);
|
|---|
| [48b662] | 207 |
|
|---|
| [ac6d04] | 208 | #ifdef DEBUG_OUTPUT
|
|---|
| [4e8206] | 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);
|
|---|
| [ac6d04] | 211 | #endif
|
|---|
| [4e8206] | 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| [48b662] | 217 |
|
|---|
| [2d3854] | 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 |
|
|---|
| [ac6d04] | 229 | comm.CommParticlesBack(particles);
|
|---|
| [48b662] | 230 |
|
|---|
| [dfed1c] | 231 | #ifdef DEBUG_OUTPUT
|
|---|
| 232 | vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
|
|---|
| [cd0fed] | 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 |
|
|---|
| [48b662] | 237 |
|
|---|
| [894a5f] | 238 | e_long = comm.GlobalSumRoot(e_long);
|
|---|
| [4571da] | 239 | e_short_peak = comm.GlobalSumRoot(e_short_peak);
|
|---|
| 240 | e_short_spline = comm.GlobalSumRoot(e_short_spline);
|
|---|
| [894a5f] | 241 | e_self = comm.GlobalSumRoot(e_self);
|
|---|
| [4571da] | 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.PrintStringOnce("E_long: %e", e_long);
|
|---|
| 248 | comm.PrintStringOnce("E_short_peak: %e", e_short_peak);
|
|---|
| 249 | comm.PrintStringOnce("E_short_spline: %e", e_short_spline);
|
|---|
| 250 | comm.PrintStringOnce("E_self: %e", e_self);
|
|---|
| 251 | comm.PrintStringOnce("E_total: %e", e);
|
|---|
| 252 | comm.PrintStringOnce("E_total*: %e", e_long + e_short_peak + e_short_spline - e_self);
|
|---|
| [48b662] | 253 |
|
|---|
| [dfed1c] | 254 | #endif /* DEBUG_OUTPUT */
|
|---|
| 255 |
|
|---|
| 256 | }
|
|---|