| 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   solver_singular.cpp
 | 
|---|
| 21 |  * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
 | 
|---|
| 22 |  * @date   Mon Apr 18 13:12:02 2011
 | 
|---|
| 23 |  *
 | 
|---|
| 24 |  * @brief  VMG::SolverSingular
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  */
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 29 | #include <libvmg_config.h>
 | 
|---|
| 30 | #endif
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #include <cmath>
 | 
|---|
| 33 | #include <cassert>
 | 
|---|
| 34 | #include <iostream>
 | 
|---|
| 35 | #include <limits>
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "base/discretization.hpp"
 | 
|---|
| 38 | #include "base/stencil.hpp"
 | 
|---|
| 39 | #include "comm/comm.hpp"
 | 
|---|
| 40 | #include "grid/multigrid.hpp"
 | 
|---|
| 41 | #include "solver/solver_singular.hpp"
 | 
|---|
| 42 | #include "mg.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | using namespace VMG;
 | 
|---|
| 45 | 
 | 
|---|
| 46 | //TODO: Implement global MPI communication here
 | 
|---|
| 47 | 
 | 
|---|
| 48 | void SolverSingular::AssembleMatrix(const Grid& rhs)
 | 
|---|
| 49 | {
 | 
|---|
| 50 |   Grid::iterator grid_iter;
 | 
|---|
| 51 |   Stencil::iterator stencil_iter;
 | 
|---|
| 52 |   Index i;
 | 
|---|
| 53 |   int index, index2;
 | 
|---|
| 54 |   vmg_float row_sum;
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   Comm* comm = MG::GetComm();
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   const vmg_float prefactor = MG::GetDiscretization()->OperatorPrefactor(rhs);
 | 
|---|
| 59 |   const Stencil& A = MG::GetDiscretization()->GetStencil();
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   // Make sure that arrays are big enough to hold expanded system of equations
 | 
|---|
| 62 |   this->Realloc(rhs.Global().GlobalSize().Product() + 1);
 | 
|---|
| 63 | 
 | 
|---|
| 64 |   for (grid_iter = rhs.Iterators().Local().Begin(); grid_iter != rhs.Iterators().Local().End(); ++grid_iter) {
 | 
|---|
| 65 | 
 | 
|---|
| 66 |     // Compute 1-dimensional index from 3-dimensional grid
 | 
|---|
| 67 |     index = rhs.GlobalLinearIndex(*grid_iter - rhs.Local().Begin() + rhs.Global().LocalBegin());
 | 
|---|
| 68 | 
 | 
|---|
| 69 |     // Check if we computed the index correctly
 | 
|---|
| 70 |     assert(index >= 0 && index < this->Size()-1);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |     // Set solution and right hand side vectors
 | 
|---|
| 73 |     this->Sol(index) = 0.0;
 | 
|---|
| 74 |     this->Rhs(index) = rhs.GetVal(*grid_iter);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     // Initialize matrix with zeros and then set entries according to the stencil
 | 
|---|
| 77 |     for (int l=0; l<this->Size(); l++)
 | 
|---|
| 78 |       this->Mat(index,l) = 0.0;
 | 
|---|
| 79 | 
 | 
|---|
| 80 |     this->Mat(index,index) = prefactor * A.GetDiag();
 | 
|---|
| 81 | 
 | 
|---|
| 82 |     for (stencil_iter = A.begin(); stencil_iter != A.end(); ++stencil_iter) {
 | 
|---|
| 83 | 
 | 
|---|
| 84 |       i = *grid_iter - rhs.Local().Begin() + rhs.Global().LocalBegin() + stencil_iter->Disp();
 | 
|---|
| 85 | 
 | 
|---|
| 86 |       for (int j=0; j<3; ++j)
 | 
|---|
| 87 |         if (comm->BoundaryConditions()[j] == Periodic) {
 | 
|---|
| 88 |           if (i[j] < rhs.Global().GlobalBegin()[j])
 | 
|---|
| 89 |             i[j] += rhs.Global().GlobalSize()[j];
 | 
|---|
| 90 |           else if (i[j] >= rhs.Global().GlobalEnd()[j])
 | 
|---|
| 91 |             i[j] -= rhs.Global().GlobalSize()[j];
 | 
|---|
| 92 |         }
 | 
|---|
| 93 | 
 | 
|---|
| 94 |       // Compute global 1-dimensional index
 | 
|---|
| 95 |       index2 = rhs.GlobalLinearIndex(i);
 | 
|---|
| 96 | 
 | 
|---|
| 97 |       // Set matrix entry
 | 
|---|
| 98 |       this->Mat(index,index2) += prefactor * stencil_iter->Val();
 | 
|---|
| 99 |     }
 | 
|---|
| 100 |   }
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   // Check if matrix has zero row sum (i.e. (1,1,...,1) is an Eigenvector to the Eigenvalue 0)
 | 
|---|
| 103 |   row_sum = A.GetDiag();
 | 
|---|
| 104 |   for (Stencil::iterator iter=A.begin(); iter!=A.end(); iter++)
 | 
|---|
| 105 |     row_sum += iter->Val();
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   if (std::abs(row_sum) <= (A.size()+1) * std::numeric_limits<vmg_float>::epsilon()) {
 | 
|---|
| 108 | 
 | 
|---|
| 109 |     // Expand equation system in order to make the system regular.
 | 
|---|
| 110 |     // The last entry of the solution vector will hold a correction to the right hand side,
 | 
|---|
| 111 |     // ensuring that the discrete compatibility condition holds. (Compare Trottenberg)
 | 
|---|
| 112 |     for (int i=0; i<this->Size()-1; i++)
 | 
|---|
| 113 |       this->Mat(this->Size()-1, i) = this->Mat(i, this->Size()-1) = 1.0;
 | 
|---|
| 114 | 
 | 
|---|
| 115 |     this->Mat(this->Size()-1, this->Size()-1) = 0.0;
 | 
|---|
| 116 |     this->Sol(this->Size()-1) = 0.0;
 | 
|---|
| 117 |     this->Rhs(this->Size()-1) = 0.0;
 | 
|---|
| 118 | 
 | 
|---|
| 119 |   }else {
 | 
|---|
| 120 |     //TODO: Implement this
 | 
|---|
| 121 |     assert(0 == "At the first glance your stencil does not seem to be singular. Try SolverRegular instead.");
 | 
|---|
| 122 |   }
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | void SolverSingular::ExportSol(Grid& sol, Grid& rhs)
 | 
|---|
| 126 | {
 | 
|---|
| 127 |   int index;
 | 
|---|
| 128 |   const vmg_float correction = this->Sol(this->Size()-1);
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   for (int i=0; i<sol.Local().Size().X(); i++)
 | 
|---|
| 131 |     for (int j=0; j<sol.Local().Size().Y(); j++)
 | 
|---|
| 132 |       for (int k=0; k<sol.Local().Size().Z(); k++) {
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         // Compute global 1-dimensional index
 | 
|---|
| 135 |         index = sol.GlobalLinearIndex(sol.Global().LocalBegin().X() + i,
 | 
|---|
| 136 |                                       sol.Global().LocalBegin().Y() + j,
 | 
|---|
| 137 |                                       sol.Global().LocalBegin().Z() + k);
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         assert(index >= 0 && index < sol.Global().GlobalSize().Product());
 | 
|---|
| 140 | 
 | 
|---|
| 141 |         // Set solution
 | 
|---|
| 142 |         sol(sol.Local().Begin().X()+i, sol.Local().Begin().Y()+j, sol.Local().Begin().Z()+k) = this->Sol(index) - correction;
 | 
|---|
| 143 | 
 | 
|---|
| 144 |       }
 | 
|---|
| 145 | }
 | 
|---|