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