| 1 | /* | 
|---|
| 2 | *    vmg - a versatile multigrid solver | 
|---|
| 3 | *    Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn | 
|---|
| 4 | * | 
|---|
| 5 | *  vmg is free software: you can redistribute it and/or modify | 
|---|
| 6 | *  it under the terms of the GNU General Public License as published by | 
|---|
| 7 | *  the Free Software Foundation, either version 3 of the License, or | 
|---|
| 8 | *  (at your option) any later version. | 
|---|
| 9 | * | 
|---|
| 10 | *  vmg is distributed in the hope that it will be useful, | 
|---|
| 11 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 13 | *  GNU General Public License for more details. | 
|---|
| 14 | * | 
|---|
| 15 | *  You should have received a copy of the GNU General Public License | 
|---|
| 16 | *  along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 | /** | 
|---|
| 20 | * @file   linked_cell_list.cpp | 
|---|
| 21 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de> | 
|---|
| 22 | * @date   Mon Nov 21 13:27:22 2011 | 
|---|
| 23 | * | 
|---|
| 24 | * @brief  A linked cell list implementation. | 
|---|
| 25 | * | 
|---|
| 26 | */ | 
|---|
| 27 |  | 
|---|
| 28 | #ifdef HAVE_CONFIG_H | 
|---|
| 29 | #include <libvmg_config.h> | 
|---|
| 30 | #endif | 
|---|
| 31 |  | 
|---|
| 32 | #include "comm/comm.hpp" | 
|---|
| 33 | #include "units/particle/linked_cell_list.hpp" | 
|---|
| 34 | #include "mg.hpp" | 
|---|
| 35 |  | 
|---|
| 36 | using namespace VMG; | 
|---|
| 37 |  | 
|---|
| 38 | Particle::LinkedCellList::LinkedCellList(std::list<Particle>& particles, | 
|---|
| 39 | const int& near_field_cells, const Grid& grid) | 
|---|
| 40 | { | 
|---|
| 41 | std::list<Particle>::iterator iter; | 
|---|
| 42 | Index index_global, index_local; | 
|---|
| 43 | LocalIndices local = grid.Local(); | 
|---|
| 44 |  | 
|---|
| 45 | local.BoundaryBegin1() = 0; | 
|---|
| 46 | local.BoundaryEnd1() = 0; | 
|---|
| 47 | local.BoundarySize1() = 0; | 
|---|
| 48 |  | 
|---|
| 49 | local.BoundaryBegin2() = 0; | 
|---|
| 50 | local.BoundaryEnd2() = 0; | 
|---|
| 51 | local.BoundarySize2() = 0; | 
|---|
| 52 |  | 
|---|
| 53 | for (int i=0; i<3; ++i) { | 
|---|
| 54 |  | 
|---|
| 55 | if (local.HaloSize1()[i] > 0) { | 
|---|
| 56 | local.HaloBegin1()[i] = 0; | 
|---|
| 57 | local.HaloEnd1()[i] = near_field_cells+1; | 
|---|
| 58 | local.HaloSize1()[i] = near_field_cells+1; | 
|---|
| 59 | local.Begin()[i] = near_field_cells+1; | 
|---|
| 60 | local.End()[i] = local.Begin()[i] + local.Size()[i]; | 
|---|
| 61 | local.SizeTotal()[i] = local.Size()[i] + | 
|---|
| 62 | local.HaloEnd1()[i] - local.HaloBegin1()[i] + | 
|---|
| 63 | local.HaloEnd2()[i] - local.HaloBegin2()[i]; | 
|---|
| 64 | }else { | 
|---|
| 65 | local.Begin()[i] = 0; | 
|---|
| 66 | local.End()[i] = local.Size()[i]; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | if (local.HaloSize2()[i] > 0) { | 
|---|
| 70 | local.HaloBegin2()[i] = local.End()[i]; | 
|---|
| 71 | local.HaloEnd2()[i] = local.HaloBegin2()[i] + near_field_cells+1; | 
|---|
| 72 | local.HaloSize2()[i] = near_field_cells+1; | 
|---|
| 73 | local.SizeTotal()[i] = local.Size()[i] + | 
|---|
| 74 | local.HaloEnd1()[i] - local.HaloBegin1()[i] + | 
|---|
| 75 | local.HaloEnd2()[i] - local.HaloBegin2()[i]; | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | SetGridSize(grid.Global(), local, grid.Extent()); | 
|---|
| 80 |  | 
|---|
| 81 | for (iter=particles.begin(); iter!=particles.end(); ++iter) | 
|---|
| 82 | AddParticle(&(*iter)); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | Particle::LinkedCellList::~LinkedCellList() | 
|---|
| 86 | { | 
|---|
| 87 | ClearHalo(); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | void Particle::LinkedCellList::AddParticle(Particle* p) | 
|---|
| 91 | { | 
|---|
| 92 | const Index global_index = global.GlobalBegin() + ((p->Pos() - Extent().Begin()) / Extent().MeshWidth()).Floor(); | 
|---|
| 93 | const Index local_index = global_index - Global().LocalBegin() + Local().Begin(); | 
|---|
| 94 |  | 
|---|
| 95 | assert(local_index.IsInBounds(Local().Begin(), Local().End())); | 
|---|
| 96 | (*this)(local_index).push_back(p); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void Particle::LinkedCellList::AddParticleToHalo(const vmg_float* x, const vmg_float& q) | 
|---|
| 100 | { | 
|---|
| 101 | const Index global_index = Global().GlobalBegin() + ((Vector(x) - Extent().Begin()) / Extent().MeshWidth()).Floor(); | 
|---|
| 102 | const Index local_index = global_index - Global().LocalBegin() + Local().Begin(); | 
|---|
| 103 |  | 
|---|
| 104 | assert(local_index.IsInBounds(0, Local().SizeTotal())); | 
|---|
| 105 | assert((local_index[0] >= Local().HaloBegin1()[0] && local_index[0] < Local().HaloEnd1()[0]) || | 
|---|
| 106 | (local_index[1] >= Local().HaloBegin1()[1] && local_index[1] < Local().HaloEnd1()[1]) || | 
|---|
| 107 | (local_index[2] >= Local().HaloBegin1()[2] && local_index[2] < Local().HaloEnd1()[2]) || | 
|---|
| 108 | (local_index[0] >= Local().HaloBegin2()[0] && local_index[0] < Local().HaloEnd2()[0]) || | 
|---|
| 109 | (local_index[1] >= Local().HaloBegin2()[1] && local_index[1] < Local().HaloEnd2()[1]) || | 
|---|
| 110 | (local_index[2] >= Local().HaloBegin2()[2] && local_index[2] < Local().HaloEnd2()[2])); | 
|---|
| 111 |  | 
|---|
| 112 | (*this)(local_index).push_back(new Particle(x, q)); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | void Particle::LinkedCellList::ClearHalo() | 
|---|
| 116 | { | 
|---|
| 117 | Grid::iterator g_iter; | 
|---|
| 118 | std::list<Particle*>::iterator p_iter; | 
|---|
| 119 |  | 
|---|
| 120 | for (int i=0; i<3; ++i) { | 
|---|
| 121 |  | 
|---|
| 122 | for (g_iter = Iterators().Halo1()[i].Begin(); g_iter != Iterators().Halo1()[i].End(); ++g_iter) | 
|---|
| 123 | for (p_iter = (*this)(*g_iter).begin(); p_iter!=(*this)(*g_iter).end(); ++p_iter) | 
|---|
| 124 | delete *p_iter; | 
|---|
| 125 |  | 
|---|
| 126 | for (g_iter = Iterators().Halo2()[i].Begin(); g_iter != Iterators().Halo2()[i].End(); ++g_iter) | 
|---|
| 127 | for (p_iter = (*this)(*g_iter).begin(); p_iter!=(*this)(*g_iter).end(); ++p_iter) | 
|---|
| 128 | delete *p_iter; | 
|---|
| 129 |  | 
|---|
| 130 | } | 
|---|
| 131 | } | 
|---|