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