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