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