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