PlayaBlockTriangularSolverImpl.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #ifndef PLAYA_BLOCKTRIANGULARSOLVER_IMPL_HPP
00043 #define PLAYA_BLOCKTRIANGULARSOLVER_IMPL_HPP
00044 
00045 #include "PlayaDefs.hpp"
00046 #include "PlayaLinearSolverDecl.hpp" 
00047 #include "PlayaLinearCombinationImpl.hpp" 
00048 #include "PlayaSimpleZeroOpDecl.hpp" 
00049 #include "PlayaBlockTriangularSolverDecl.hpp" 
00050 
00051 
00052 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00053 #include "PlayaLinearSolverImpl.hpp" 
00054 #include "PlayaSimpleZeroOpDecl.hpp" 
00055 #endif
00056 
00057 namespace Playa
00058 {
00059 using namespace PlayaExprTemplates;
00060 
00061 template <class Scalar> inline
00062 BlockTriangularSolver<Scalar>
00063 ::BlockTriangularSolver(const LinearSolver<Scalar>& solver)
00064   : LinearSolverBase<Scalar>(ParameterList()), solvers_(tuple(solver)) {;}
00065 
00066 template <class Scalar> inline
00067 BlockTriangularSolver<Scalar>
00068 ::BlockTriangularSolver(const Array<LinearSolver<Scalar> >& solvers)
00069   : LinearSolverBase<Scalar>(ParameterList()), solvers_(solvers) {;}
00070 
00071 template <class Scalar> inline
00072 SolverState<Scalar> BlockTriangularSolver<Scalar>
00073 ::solve(const LinearOperator<Scalar>& op,
00074   const Vector<Scalar>& rhs,
00075   Vector<Scalar>& soln) const
00076 {
00077   int nRows = op.numBlockRows();
00078   int nCols = op.numBlockCols();
00079 
00080   soln = op.domain().createMember();
00081   //    bool converged = false;
00082 
00083   TEUCHOS_TEST_FOR_EXCEPTION(nRows != rhs.space().numBlocks(), std::runtime_error,
00084     "number of rows in operator " << op
00085     << " not equal to number of blocks on RHS "
00086     << rhs);
00087 
00088   TEUCHOS_TEST_FOR_EXCEPTION(nRows != nCols, std::runtime_error,
00089     "nonsquare block structure in block triangular "
00090     "solver: nRows=" << nRows << " nCols=" << nCols);
00091 
00092   bool isUpper = false;
00093   bool isLower = false;
00094 
00095   for (int r=0; r<nRows; r++)
00096   {
00097     for (int c=0; c<nCols; c++)
00098     {
00099       if (op.getBlock(r,c).ptr().get() == 0 ||
00100         dynamic_cast<const SimpleZeroOp<Scalar>* >(op.getBlock(r,c).ptr().get()))
00101       {
00102         TEUCHOS_TEST_FOR_EXCEPTION(r==c, std::runtime_error,
00103           "zero diagonal block (" << r << ", " << c 
00104           << " detected in block "
00105           "triangular solver. Operator is " << op);
00106         continue;
00107       }
00108       else
00109       {
00110         if (r < c) isUpper = true;
00111         if (c < r) isLower = true;
00112       }
00113     }
00114   }
00115 
00116   TEUCHOS_TEST_FOR_EXCEPTION(isUpper && isLower, std::runtime_error, 
00117     "block triangular solver detected non-triangular operator "
00118     << op);
00119 
00120   bool oneSolverFitsAll = false;
00121   if ((int) solvers_.size() == 1 && nRows != 1) 
00122   {
00123     oneSolverFitsAll = true;
00124   }
00125 
00126   for (int i=0; i<nRows; i++)
00127   {
00128     int r = i;
00129     if (isUpper) r = nRows - 1 - i;
00130     Vector<Scalar> rhs_r = rhs.getBlock(r);
00131     for (int j=0; j<i; j++)
00132     {
00133       int c = j;
00134       if (isUpper) c = nCols - 1 - j;
00135       if (op.getBlock(r,c).ptr().get() != 0)
00136       {
00137         rhs_r = rhs_r - op.getBlock(r,c) * soln.getBlock(c);
00138       }
00139     }
00140 
00141     SolverState<Scalar> state;
00142     Vector<Scalar> soln_r;
00143     if (oneSolverFitsAll)
00144     {
00145       state = solvers_[0].solve(op.getBlock(r,r), rhs_r, soln_r);
00146     }
00147     else
00148     {
00149       state = solvers_[r].solve(op.getBlock(r,r), rhs_r, soln_r);
00150     }
00151     if (nRows > 1) soln.setBlock(r, soln_r);
00152     else soln = soln_r;
00153     if (state.finalState() != SolveConverged)
00154     {
00155       return state;
00156     }
00157   }
00158 
00159   return SolverState<Scalar>(SolveConverged, "block solves converged",
00160     0, ScalarTraits<Scalar>::zero());
00161 }
00162   
00163 }
00164 
00165 #endif

Site Contact