| [48b662] | 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 |
|
|---|
| [dfed1c] | 14 | #ifdef HAVE_MPI
|
|---|
| 15 | #include <mpi.h>
|
|---|
| [ac6d04] | 16 | #ifdef HAVE_MARMOT
|
|---|
| 17 | #include <enhancempicalls.h>
|
|---|
| 18 | #include <sourceinfompicalls.h>
|
|---|
| 19 | #endif
|
|---|
| [dfed1c] | 20 | #endif
|
|---|
| 21 |
|
|---|
| [4571da] | 22 | #include <algorithm>
|
|---|
| [48b662] | 23 | #include <cmath>
|
|---|
| [dfed1c] | 24 | #include <cstring>
|
|---|
| [48b662] | 25 |
|
|---|
| 26 | #include "base/helper.hpp"
|
|---|
| 27 | #include "base/index.hpp"
|
|---|
| [ac6d04] | 28 | #include "base/math.hpp"
|
|---|
| [48b662] | 29 | #include "base/vector.hpp"
|
|---|
| 30 | #include "comm/comm.hpp"
|
|---|
| 31 | #include "grid/grid.hpp"
|
|---|
| 32 | #include "grid/multigrid.hpp"
|
|---|
| 33 | #include "grid/tempgrid.hpp"
|
|---|
| [f003a9] | 34 | #include "units/particle/comm_mpi_particle.hpp"
|
|---|
| 35 | #include "units/particle/interface_particles.hpp"
|
|---|
| 36 | #include "units/particle/interpolation.hpp"
|
|---|
| 37 | #include "units/particle/linked_cell_list.hpp"
|
|---|
| [48b662] | 38 | #include "mg.hpp"
|
|---|
| 39 |
|
|---|
| 40 | using namespace VMG;
|
|---|
| 41 |
|
|---|
| 42 | void InterfaceParticles::ImportRightHandSide(Multigrid& multigrid)
|
|---|
| 43 | {
|
|---|
| [dfed1c] | 44 | Index index_global, index_local, index;
|
|---|
| 45 | Vector pos_rel, pos_abs, grid_val;
|
|---|
| [48b662] | 46 |
|
|---|
| [dfed1c] | 47 | Factory& factory = MG::GetFactory();
|
|---|
| [f003a9] | 48 | Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
|
|---|
| [48b662] | 49 |
|
|---|
| [dfed1c] | 50 | const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
|
|---|
| [48b662] | 51 |
|
|---|
| [ac6d04] | 52 | Grid& grid = multigrid(multigrid.MaxLevel());
|
|---|
| [894a5f] | 53 | Grid& particle_grid = comm.GetParticleGrid();
|
|---|
| [dfed1c] | 54 |
|
|---|
| [894a5f] | 55 | particle_grid.Clear();
|
|---|
| [48b662] | 56 |
|
|---|
| [ac6d04] | 57 | assert(particle_grid.Global().LocalSize().IsComponentwiseGreater(near_field_cells));
|
|---|
| 58 |
|
|---|
| [dfed1c] | 59 | /*
|
|---|
| 60 | * Distribute particles to their processes
|
|---|
| 61 | */
|
|---|
| 62 | particles.clear();
|
|---|
| [894a5f] | 63 | comm.CommParticles(grid, particles);
|
|---|
| [dfed1c] | 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | * Charge assignment on the grid
|
|---|
| 67 | */
|
|---|
| 68 | std::list<Particle::Particle>::iterator iter;
|
|---|
| 69 |
|
|---|
| 70 | #ifdef DEBUG_OUTPUT
|
|---|
| 71 | vmg_float particle_charges = 0.0;
|
|---|
| 72 | for (iter=particles.begin(); iter!=particles.end(); ++iter)
|
|---|
| 73 | particle_charges += iter->Charge();
|
|---|
| 74 | particle_charges = MG::GetComm()->GlobalSumRoot(particle_charges);
|
|---|
| [894a5f] | 75 | comm.PrintStringOnce("Particle list charge sum: %e", particle_charges);
|
|---|
| [ac6d04] | 76 | comm.PrintString("Local number of particles: %d", particles.size());
|
|---|
| [dfed1c] | 77 | #endif
|
|---|
| [48b662] | 78 |
|
|---|
| [ab63b6] | 79 | for (iter=particles.begin(); iter!=particles.end(); ++iter)
|
|---|
| [894a5f] | 80 | spl.SetSpline(particle_grid, *iter, near_field_cells);
|
|---|
| [48b662] | 81 |
|
|---|
| [dfed1c] | 82 | // Communicate charges over halo
|
|---|
| [894a5f] | 83 | comm.CommFromGhosts(particle_grid);
|
|---|
| [48b662] | 84 |
|
|---|
| [dfed1c] | 85 | // Assign charge values to the right hand side
|
|---|
| [06e153] | 86 | for (int i=0; i<grid.Local().Size().X(); ++i)
|
|---|
| 87 | for (int j=0; j<grid.Local().Size().Y(); ++j)
|
|---|
| 88 | for (int k=0; k<grid.Local().Size().Z(); ++k)
|
|---|
| 89 | grid(grid.Local().Begin().X() + i,
|
|---|
| 90 | grid.Local().Begin().Y() + j,
|
|---|
| 91 | grid.Local().Begin().Z() + k) = 4.0 * Math::pi *
|
|---|
| 92 | particle_grid.GetVal(particle_grid.Local().Begin().X() + i,
|
|---|
| 93 | particle_grid.Local().Begin().Y() + j,
|
|---|
| 94 | particle_grid.Local().Begin().Z() + k);
|
|---|
| [dfed1c] | 95 |
|
|---|
| 96 | #ifdef DEBUG_OUTPUT
|
|---|
| 97 | Grid::iterator grid_iter;
|
|---|
| 98 | vmg_float charge_sum = 0.0;
|
|---|
| 99 | for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
|
|---|
| 100 | charge_sum += grid.GetVal(*grid_iter);
|
|---|
| 101 | charge_sum = MG::GetComm()->GlobalSum(charge_sum);
|
|---|
| [894a5f] | 102 | comm.PrintStringOnce("Grid charge sum: %e", charge_sum);
|
|---|
| [dfed1c] | 103 | #endif
|
|---|
| [48b662] | 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void InterfaceParticles::ExportSolution(Grid& grid)
|
|---|
| 107 | {
|
|---|
| [4571da] | 108 | Index i;
|
|---|
| [48b662] | 109 |
|
|---|
| [dfed1c] | 110 | #ifdef DEBUG_OUTPUT
|
|---|
| 111 | vmg_float e = 0.0;
|
|---|
| 112 | vmg_float e_long = 0.0;
|
|---|
| 113 | vmg_float e_self = 0.0;
|
|---|
| [4571da] | 114 | vmg_float e_short_peak = 0.0;
|
|---|
| 115 | vmg_float e_short_spline = 0.0;
|
|---|
| [dfed1c] | 116 | #endif
|
|---|
| [48b662] | 117 |
|
|---|
| [dfed1c] | 118 | Factory& factory = MG::GetFactory();
|
|---|
| [f003a9] | 119 | Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
|
|---|
| [dfed1c] | 120 |
|
|---|
| 121 | /*
|
|---|
| 122 | * Get parameters and arrays
|
|---|
| 123 | */
|
|---|
| [a150d0] | 124 | const vmg_int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
|
|---|
| 125 | const vmg_int& interpolation_degree = factory.GetObjectStorageVal<int>("PARTICLE_INTERPOLATION_DEGREE");
|
|---|
| [dfed1c] | 126 |
|
|---|
| [f003a9] | 127 | Particle::Interpolation ip(interpolation_degree);
|
|---|
| [a150d0] | 128 |
|
|---|
| [4571da] | 129 | const vmg_float r_cut = near_field_cells * grid.Extent().MeshWidth().Max();
|
|---|
| [dfed1c] | 130 |
|
|---|
| 131 | /*
|
|---|
| 132 | * Copy potential values to a grid with sufficiently large halo size.
|
|---|
| 133 | * This may be optimized in future.
|
|---|
| 134 | * The parameters of this grid have been set in the import step.
|
|---|
| 135 | */
|
|---|
| [894a5f] | 136 | Grid& particle_grid = comm.GetParticleGrid();
|
|---|
| [48b662] | 137 |
|
|---|
| [4571da] | 138 | for (i.X()=0; i.X()<grid.Local().Size().X(); ++i.X())
|
|---|
| 139 | for (i.Y()=0; i.Y()<grid.Local().Size().Y(); ++i.Y())
|
|---|
| 140 | for (i.Z()=0; i.Z()<grid.Local().Size().Z(); ++i.Z())
|
|---|
| 141 | particle_grid(i + particle_grid.Local().Begin()) = grid.GetVal(i + grid.Local().Begin());
|
|---|
| [48b662] | 142 |
|
|---|
| [894a5f] | 143 | comm.CommToGhosts(particle_grid);
|
|---|
| [48b662] | 144 |
|
|---|
| [dfed1c] | 145 | /*
|
|---|
| [b2154a3] | 146 | * Compute potentials
|
|---|
| [dfed1c] | 147 | */
|
|---|
| 148 | Particle::LinkedCellList lc(particles, near_field_cells, grid);
|
|---|
| 149 | Particle::LinkedCellList::iterator p1, p2;
|
|---|
| 150 | Grid::iterator iter;
|
|---|
| [48b662] | 151 |
|
|---|
| [716da7] | 152 | comm.CommLCListToGhosts(lc);
|
|---|
| [48b662] | 153 |
|
|---|
| [4e8206] | 154 | for (int i=lc.Local().Begin().X(); i<lc.Local().End().X(); ++i)
|
|---|
| 155 | for (int j=lc.Local().Begin().Y(); j<lc.Local().End().Y(); ++j)
|
|---|
| 156 | for (int k=lc.Local().Begin().Z(); k<lc.Local().End().Z(); ++k) {
|
|---|
| [b2154a3] | 157 |
|
|---|
| [4e8206] | 158 | if (lc(i,j,k).size() > 0)
|
|---|
| 159 | ip.ComputeCoefficients(particle_grid, Index(i,j,k) - lc.Local().Begin() + particle_grid.Local().Begin());
|
|---|
| [b2154a3] | 160 |
|
|---|
| [4e8206] | 161 | for (p1=lc(i,j,k).begin(); p1!=lc(i,j,k).end(); ++p1) {
|
|---|
| [b2154a3] | 162 |
|
|---|
| [f003a9] | 163 | ip.Evaluate(**p1);
|
|---|
| [4e8206] | 164 | (*p1)->Pot() -= (*p1)->Charge() * spl.GetAntiDerivativeAtZero();
|
|---|
| [f003a9] | 165 | (*p1)->Field() *= 0.5;
|
|---|
| [b2154a3] | 166 |
|
|---|
| 167 | #ifdef DEBUG_OUTPUT
|
|---|
| [f003a9] | 168 | e_long += 0.5 * (*p1)->Charge() * ip.EvaluatePotentialLR(**p1);
|
|---|
| [4e8206] | 169 | e_self += 0.5 * (*p1)->Charge() * (*p1)->Charge() * spl.GetAntiDerivativeAtZero();
|
|---|
| [b2154a3] | 170 | #endif
|
|---|
| 171 |
|
|---|
| [4e8206] | 172 | for (int dx=-1*near_field_cells; dx<=near_field_cells; ++dx)
|
|---|
| 173 | for (int dy=-1*near_field_cells; dy<=near_field_cells; ++dy)
|
|---|
| 174 | for (int dz=-1*near_field_cells; dz<=near_field_cells; ++dz) {
|
|---|
| [716da7] | 175 |
|
|---|
| [4e8206] | 176 | for (p2=lc(i+dx,j+dy,k+dz).begin(); p2!=lc(i+dx,j+dy,k+dz).end(); ++p2)
|
|---|
| [716da7] | 177 |
|
|---|
| [4e8206] | 178 | if (*p1 != *p2) {
|
|---|
| [48b662] | 179 |
|
|---|
| [4e8206] | 180 | const Vector dir = (*p1)->Pos() - (*p2)->Pos();
|
|---|
| 181 | const vmg_float length = dir.Length();
|
|---|
| [48b662] | 182 |
|
|---|
| [4e8206] | 183 | if (length < r_cut) {
|
|---|
| [1a92cf] | 184 |
|
|---|
| [4e8206] | 185 | (*p1)->Pot() += (*p2)->Charge() / length * (1.0 + spl.EvaluatePotential(length));
|
|---|
| 186 | (*p1)->Field() += (*p2)->Charge() * dir * spl.EvaluateField(length);
|
|---|
| [48b662] | 187 |
|
|---|
| [ac6d04] | 188 | #ifdef DEBUG_OUTPUT
|
|---|
| [4e8206] | 189 | e_short_peak += 0.5 * (*p1)->Charge() * (*p2)->Charge() / length;
|
|---|
| 190 | e_short_spline += 0.5 * (*p1)->Charge() * (*p2)->Charge() / length * spl.EvaluatePotential(length);
|
|---|
| [ac6d04] | 191 | #endif
|
|---|
| [4e8206] | 192 | }
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| [48b662] | 197 |
|
|---|
| [ac6d04] | 198 | comm.CommParticlesBack(particles);
|
|---|
| [48b662] | 199 |
|
|---|
| [dfed1c] | 200 | #ifdef DEBUG_OUTPUT
|
|---|
| 201 | vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
|
|---|
| [cd0fed] | 202 | const vmg_int& num_particles_local = factory.GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
|
|---|
| 203 | const vmg_float* p = factory.GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
|
|---|
| 204 | const vmg_float* f = factory.GetObjectStorageArray<vmg_float>("PARTICLE_FIELD_ARRAY");
|
|---|
| 205 |
|
|---|
| [48b662] | 206 |
|
|---|
| [894a5f] | 207 | e_long = comm.GlobalSumRoot(e_long);
|
|---|
| [4571da] | 208 | e_short_peak = comm.GlobalSumRoot(e_short_peak);
|
|---|
| 209 | e_short_spline = comm.GlobalSumRoot(e_short_spline);
|
|---|
| [894a5f] | 210 | e_self = comm.GlobalSumRoot(e_self);
|
|---|
| [4571da] | 211 |
|
|---|
| 212 | for (int j=0; j<num_particles_local; ++j)
|
|---|
| 213 | e += 0.5 * p[j] * q[j];
|
|---|
| 214 | e = comm.GlobalSumRoot(e);
|
|---|
| 215 |
|
|---|
| 216 | comm.PrintStringOnce("E_long: %e", e_long);
|
|---|
| 217 | comm.PrintStringOnce("E_short_peak: %e", e_short_peak);
|
|---|
| 218 | comm.PrintStringOnce("E_short_spline: %e", e_short_spline);
|
|---|
| 219 | comm.PrintStringOnce("E_self: %e", e_self);
|
|---|
| 220 | comm.PrintStringOnce("E_total: %e", e);
|
|---|
| 221 | comm.PrintStringOnce("E_total*: %e", e_long + e_short_peak + e_short_spline - e_self);
|
|---|
| [48b662] | 222 |
|
|---|
| [dfed1c] | 223 | #endif /* DEBUG_OUTPUT */
|
|---|
| 224 |
|
|---|
| 225 | }
|
|---|