| 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   comm.hpp
 | 
|---|
| 21 |  * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
 | 
|---|
| 22 |  * @date   Wed Jun 16 13:21:06 2010
 | 
|---|
| 23 |  *
 | 
|---|
| 24 |  * @brief  Base class for communication.
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  */
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 29 | #include <libvmg_config.h>
 | 
|---|
| 30 | #endif
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #ifdef HAVE_BOOST_FILESYSTEM_PATH_HPP
 | 
|---|
| 33 | #include <boost/filesystem.hpp>
 | 
|---|
| 34 | namespace fs = boost::filesystem;
 | 
|---|
| 35 | #endif
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "base/discretization.hpp"
 | 
|---|
| 38 | #include "base/helper.hpp"
 | 
|---|
| 39 | #include "base/stencil.hpp"
 | 
|---|
| 40 | #include "comm/comm.hpp"
 | 
|---|
| 41 | #include "comm/domain_decomposition.hpp"
 | 
|---|
| 42 | #include "grid/grid.hpp"
 | 
|---|
| 43 | #include "mg.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | using namespace VMG;
 | 
|---|
| 46 | 
 | 
|---|
| 47 | vmg_float Comm::ComputeResidualNorm(Multigrid& sol_mg, Multigrid& rhs_mg)
 | 
|---|
| 48 | {
 | 
|---|
| 49 |   Stencil::iterator stencil_iter;
 | 
|---|
| 50 |   vmg_float norm = 0.0;
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   const vmg_float prefactor = MG::GetDiscretization()->OperatorPrefactor(sol_mg());
 | 
|---|
| 53 |   const Stencil& A = MG::GetDiscretization()->GetStencil();
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   this->CommToGhosts(sol_mg());
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   if (sol_mg().Global().BoundaryType() == LocallyRefined)
 | 
|---|
| 58 |     MG::GetDiscretization()->SetInnerBoundary(sol_mg(), rhs_mg(), sol_mg(sol_mg.Level()-1));
 | 
|---|
| 59 | 
 | 
|---|
| 60 |   const Grid& sol = sol_mg();
 | 
|---|
| 61 |   const Grid& rhs = rhs_mg();
 | 
|---|
| 62 | 
 | 
|---|
| 63 | #ifdef DEBUG_MATRIX_CHECKS
 | 
|---|
| 64 |   sol.IsCompatible(rhs);
 | 
|---|
| 65 |   sol.IsConsistent();
 | 
|---|
| 66 |   rhs.IsConsistent();
 | 
|---|
| 67 | #endif
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   for (int i=rhs.Local().Begin().X(); i<rhs.Local().End().X(); ++i)
 | 
|---|
| 70 |     for (int j=rhs.Local().Begin().Y(); j<rhs.Local().End().Y(); ++j)
 | 
|---|
| 71 |       for (int k=rhs.Local().Begin().Z(); k<rhs.Local().End().Z(); ++k) {
 | 
|---|
| 72 |         vmg_float val = rhs.GetVal(i,j,k) - prefactor * A.GetDiag() * sol.GetVal(i,j,k);
 | 
|---|
| 73 |         for (stencil_iter=A.begin(); stencil_iter!=A.end(); ++stencil_iter)
 | 
|---|
| 74 |           val -= prefactor * stencil_iter->Val() * sol.GetVal(i + stencil_iter->Disp().X(),
 | 
|---|
| 75 |                                                               j + stencil_iter->Disp().Y(),
 | 
|---|
| 76 |                                                               k + stencil_iter->Disp().Z());
 | 
|---|
| 77 |         norm += val*val;
 | 
|---|
| 78 |       }
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   norm = GlobalSum(norm);
 | 
|---|
| 81 |   norm = std::sqrt(sol.Extent().MeshWidth().Product() * norm);
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   return norm;
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | Grid& Comm::GetParticleGrid()
 | 
|---|
| 87 | {
 | 
|---|
| 88 |   if (particle_grid != NULL)
 | 
|---|
| 89 |     return *particle_grid;
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   const Grid& grid = (*MG::GetRhs())(MG::GetRhs()->MaxLevel());
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   LocalIndices local = grid.Local();
 | 
|---|
| 94 |   GlobalIndices global = grid.Global();
 | 
|---|
| 95 |   SpatialExtent extent = grid.Extent();
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   const int& near_field_cells = MG::GetFactory().GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS");
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   for (int i=0; i<3; ++i) {
 | 
|---|
| 100 | 
 | 
|---|
| 101 |     if (BoundaryConditions()[i] == Open || BoundaryConditions()[i] == Dirichlet) {
 | 
|---|
| 102 | 
 | 
|---|
| 103 |       if (global.LocalBegin()[i] == global.GlobalBegin()[i])
 | 
|---|
| 104 |         global.LocalBegin()[i] += 1;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |       if (global.LocalEnd()[i] == global.GlobalEnd()[i])
 | 
|---|
| 107 |         global.LocalEnd()[i] -= 1;
 | 
|---|
| 108 | 
 | 
|---|
| 109 |       global.LocalSize()[i] = global.LocalEnd()[i] - global.LocalBegin()[i];
 | 
|---|
| 110 | 
 | 
|---|
| 111 |       global.GlobalBegin()[i] += 1;
 | 
|---|
| 112 |       global.GlobalEnd()[i] -= 1;
 | 
|---|
| 113 |       global.GlobalSize()[i] = global.GlobalEnd()[i] - global.GlobalBegin()[i];
 | 
|---|
| 114 | 
 | 
|---|
| 115 |       extent.Begin()[i] += extent.MeshWidth()[i];
 | 
|---|
| 116 |       extent.End()[i] -= extent.MeshWidth()[i];
 | 
|---|
| 117 |       extent.Size()[i] = extent.End()[i] - extent.Begin()[i];
 | 
|---|
| 118 | 
 | 
|---|
| 119 |     }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   }
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   local.BoundaryBegin1() = local.BoundaryEnd1() = local.BoundarySize1() = 0;
 | 
|---|
| 124 |   local.BoundaryBegin2() = local.BoundaryEnd2() = local.BoundarySize2() = 0;
 | 
|---|
| 125 | 
 | 
|---|
| 126 |   local.End() -= local.Begin();
 | 
|---|
| 127 |   local.Begin() = 0;
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   // Set grid size of intermediate temporary grid
 | 
|---|
| 130 |   for (int i=0; i<3; ++i) {
 | 
|---|
| 131 | 
 | 
|---|
| 132 |     if (local.HaloSize1()[i] > 0) {
 | 
|---|
| 133 |       local.HaloBegin1()[i] = 0;
 | 
|---|
| 134 |       local.HaloEnd1()[i] = near_field_cells;
 | 
|---|
| 135 |       local.HaloSize1()[i] = near_field_cells;
 | 
|---|
| 136 |       local.Begin()[i] = near_field_cells;
 | 
|---|
| 137 |       local.End()[i] = local.Begin()[i] + local.Size()[i];
 | 
|---|
| 138 |     }
 | 
|---|
| 139 | 
 | 
|---|
| 140 |     if (local.HaloSize2()[i] > 0) {
 | 
|---|
| 141 |       local.HaloBegin2()[i] = local.End()[i];
 | 
|---|
| 142 |       local.HaloEnd2()[i] = local.HaloBegin2()[i] + near_field_cells;
 | 
|---|
| 143 |       local.HaloSize2()[i] = near_field_cells;
 | 
|---|
| 144 |     }
 | 
|---|
| 145 | 
 | 
|---|
| 146 |   }
 | 
|---|
| 147 | 
 | 
|---|
| 148 |   local.SizeTotal() = local.Size() + local.HaloSize1() + local.HaloSize2();
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   particle_grid = new Grid(global, local, extent);
 | 
|---|
| 151 | 
 | 
|---|
| 152 |   return *particle_grid;
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | Comm::~Comm()
 | 
|---|
| 156 | {
 | 
|---|
| 157 |   delete dd;
 | 
|---|
| 158 |   delete particle_grid;
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | const std::string& Comm::OutputPath()
 | 
|---|
| 162 | {
 | 
|---|
| 163 |   if (!output_directory_is_created) {
 | 
|---|
| 164 |     output_path_str = CreateOutputDirectory();
 | 
|---|
| 165 |     output_directory_is_created = true;
 | 
|---|
| 166 |   }
 | 
|---|
| 167 | 
 | 
|---|
| 168 |   return output_path_str;
 | 
|---|
| 169 | }
 | 
|---|