source: src/interface/interface_particles.cpp@ 32ff22

Last change on this file since 32ff22 was 894a5f, checked in by Julian Iseringhausen <isering@…>, 14 years ago

Parallel performance update.

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1314 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[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"
[48b662]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
32using namespace VMG;
33
34void InterfaceParticles::ImportRightHandSide(Multigrid& multigrid)
35{
[dfed1c]36 Index index_global, index_local, index;
37 Vector pos_rel, pos_abs, grid_val;
[48b662]38
[dfed1c]39 Factory& factory = MG::GetFactory();
[894a5f]40 Comm& comm = *MG::GetComm();
[48b662]41
[dfed1c]42 const int& near_field_cells = factory.GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
[48b662]43
44 Grid& grid = multigrid(multigrid.GlobalMaxLevel());
[894a5f]45 Grid& particle_grid = comm.GetParticleGrid();
[dfed1c]46
[894a5f]47 particle_grid.Clear();
[48b662]48
[dfed1c]49 /*
50 * Distribute particles to their processes
51 */
52 particles.clear();
[894a5f]53 comm.CommParticles(grid, particles);
[dfed1c]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);
[894a5f]65 comm.PrintStringOnce("Particle list charge sum: %e", particle_charges);
[dfed1c]66#endif
[48b662]67
[ab63b6]68 for (iter=particles.begin(); iter!=particles.end(); ++iter)
[894a5f]69 spl.SetSpline(particle_grid, *iter, near_field_cells);
[48b662]70
[dfed1c]71 // Communicate charges over halo
[894a5f]72 comm.CommFromGhosts(particle_grid);
[48b662]73
[dfed1c]74 // Assign charge values to the right hand side
[48b662]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())
[894a5f]78 grid(index + grid.Local().Begin()) = particle_grid.GetVal(index + particle_grid.Local().Begin());
[dfed1c]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);
[894a5f]86 comm.PrintStringOnce("Grid charge sum: %e", charge_sum);
[dfed1c]87#endif
[48b662]88}
89
90void InterfaceParticles::ExportSolution(Grid& grid)
91{
[dfed1c]92 Index index;
93 vmg_float length;
94 Vector dist_vec;
[48b662]95
[dfed1c]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
[48b662]101
[dfed1c]102 Factory& factory = MG::GetFactory();
[894a5f]103 Comm& comm = *MG::GetComm();
[dfed1c]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");
[01be70]111 vmg_float* f = factory.GetObjectStorageArray<vmg_float>("PARTICLE_FORCE_ARRAY");
[dfed1c]112
113 const vmg_float r_cut = (near_field_cells+0.5) * grid.Extent().MeshWidth().Max();
114
115 /*
[01be70]116 * Initialize potential
[dfed1c]117 */
[01be70]118 for (vmg_int i=0; i<num_particles_local; ++i) {
[dfed1c]119 p[i] = 0.0;
[01be70]120 for (vmg_int j=0;j<3;++j)
121 f[3*i+j] = 0.;
122 }
[dfed1c]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 */
[894a5f]129 Grid& particle_grid = comm.GetParticleGrid();
[48b662]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())
[894a5f]134 particle_grid(index + particle_grid.Local().Begin()) = grid.GetVal(index + grid.Local().Begin());
[48b662]135
[894a5f]136 comm.CommToGhosts(particle_grid);
[48b662]137
[dfed1c]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) {
[48b662]143
[dfed1c]144 // Interpolate long range part of potential
[894a5f]145 p_iter->Pot() = 0.5 * (Helper::InterpolateTrilinear(p_iter->Pos(), particle_grid) - p_iter->Charge() * spl.GetAntiDerivativeZero());
[48b662]146
[dfed1c]147#ifdef DEBUG_OUTPUT
[894a5f]148 e_long += 0.5 * p_iter->Charge() * Helper::InterpolateTrilinear(p_iter->Pos(), particle_grid);
[dfed1c]149 e_self += 0.5 * p_iter->Charge() * p_iter->Charge() * spl.GetAntiDerivativeZero();
150#endif
[48b662]151
[dfed1c]152 }
[48b662]153
[894a5f]154 comm.CommAddPotential(particles);
[48b662]155
[dfed1c]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;
[48b662]162
[894a5f]163 comm.CommLCListGhosts(grid, lc);
[48b662]164
[dfed1c]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) {
[48b662]172
[dfed1c]173 length = (p2->Pos() - p1->Pos()).Length();
[48b662]174
[dfed1c]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));
[48b662]178
[dfed1c]179 }
[48b662]180
[894a5f]181 comm.CommAddPotential(lc);
[48b662]182
[dfed1c]183#ifdef DEBUG_OUTPUT
184 vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
[48b662]185
[dfed1c]186 for (int i=0; i<num_particles_local; ++i)
187 e += p[i] * q[i];
[48b662]188
[894a5f]189 e = comm.GlobalSumRoot(e);
190 e_long = comm.GlobalSumRoot(e_long);
191 e_self = comm.GlobalSumRoot(e_self);
[48b662]192
[894a5f]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);
[48b662]197
[dfed1c]198#endif /* DEBUG_OUTPUT */
199
200}
Note: See TracBrowser for help on using the repository browser.