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