Thyra Version of the Day
Thyra_DefaultMultiVectorLinearOpWithSolve_def.hpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
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 Roscoe A. Bartlett (bartlettra@ornl.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef THYRA_MULTI_VECTOR_LINEAR_OP_WITH_SOLVE_HPP
00043 #define THYRA_MULTI_VECTOR_LINEAR_OP_WITH_SOLVE_HPP
00044 
00045 #include "Thyra_DefaultMultiVectorLinearOpWithSolve_decl.hpp"
00046 #include "Thyra_DefaultDiagonalLinearOp.hpp"
00047 #include "Thyra_LinearOpWithSolveBase.hpp"
00048 #include "Thyra_DefaultMultiVectorProductVectorSpace.hpp"
00049 #include "Thyra_DefaultMultiVectorProductVector.hpp"
00050 #include "Thyra_AssertOp.hpp"
00051 #include "Teuchos_dyn_cast.hpp"
00052 
00053 
00054 namespace Thyra {
00055 
00056 
00057 // Constructors/initializers/accessors
00058 
00059 
00060 template<class Scalar>
00061 DefaultMultiVectorLinearOpWithSolve<Scalar>::DefaultMultiVectorLinearOpWithSolve()
00062 {}
00063 
00064 
00065 template<class Scalar>
00066 void DefaultMultiVectorLinearOpWithSolve<Scalar>::nonconstInitialize(
00067   const RCP<LinearOpWithSolveBase<Scalar> > &lows,
00068   const RCP<const DefaultMultiVectorProductVectorSpace<Scalar> > &multiVecRange,
00069   const RCP<const DefaultMultiVectorProductVectorSpace<Scalar> > &multiVecDomain
00070   )
00071 {
00072   validateInitialize(lows,multiVecRange,multiVecDomain);
00073   lows_ = lows;
00074   multiVecRange_ = multiVecRange;
00075   multiVecDomain_ = multiVecDomain;
00076 }
00077 
00078 
00079 template<class Scalar>
00080 void DefaultMultiVectorLinearOpWithSolve<Scalar>::initialize(
00081   const RCP<const LinearOpWithSolveBase<Scalar> > &lows,
00082   const RCP<const DefaultMultiVectorProductVectorSpace<Scalar> > &multiVecRange,
00083   const RCP<const DefaultMultiVectorProductVectorSpace<Scalar> > &multiVecDomain
00084   )
00085 {
00086   validateInitialize(lows,multiVecRange,multiVecDomain);
00087   lows_ = lows;
00088   multiVecRange_ = multiVecRange;
00089   multiVecDomain_ = multiVecDomain;
00090 }
00091 
00092 
00093 template<class Scalar>
00094 RCP<LinearOpWithSolveBase<Scalar> >
00095 DefaultMultiVectorLinearOpWithSolve<Scalar>::getNonconstLinearOpWithSolve()
00096 {
00097   return lows_.getNonconstObj();
00098 }
00099 
00100 
00101 template<class Scalar>
00102 RCP<const LinearOpWithSolveBase<Scalar> >
00103 DefaultMultiVectorLinearOpWithSolve<Scalar>::getLinearOpWithSolve() const
00104 {
00105   return lows_.getConstObj();
00106 }
00107 
00108 
00109 template<class Scalar>
00110 void DefaultMultiVectorLinearOpWithSolve<Scalar>::uninitialize()
00111 {
00112   lows_.uninitialize();
00113   multiVecRange_ = Teuchos::null;
00114   multiVecDomain_ = Teuchos::null;
00115 }
00116 
00117 
00118 // Overridden from LinearOpBase
00119 
00120 
00121 template<class Scalar>
00122 RCP< const VectorSpaceBase<Scalar> >
00123 DefaultMultiVectorLinearOpWithSolve<Scalar>::range() const
00124 {
00125   return multiVecRange_;
00126 }
00127 
00128 
00129 template<class Scalar>
00130 RCP< const VectorSpaceBase<Scalar> >
00131 DefaultMultiVectorLinearOpWithSolve<Scalar>::domain() const
00132 {
00133   return multiVecDomain_;
00134 }
00135 
00136 
00137 template<class Scalar>
00138 RCP<const LinearOpBase<Scalar> >
00139 DefaultMultiVectorLinearOpWithSolve<Scalar>::clone() const
00140 {
00141   return Teuchos::null; // ToDo: Implement if needed ???
00142 }
00143 
00144 
00145 // protected
00146 
00147 
00148 // Overridden from LinearOpBase
00149 
00150 
00151 template<class Scalar>
00152 bool DefaultMultiVectorLinearOpWithSolve<Scalar>::opSupportedImpl(
00153   EOpTransp M_trans
00154   ) const
00155 {
00156   return Thyra::opSupported(*lows_.getConstObj(),M_trans);
00157 }
00158 
00159 
00160 template<class Scalar>
00161 void DefaultMultiVectorLinearOpWithSolve<Scalar>::applyImpl(
00162   const EOpTransp M_trans,
00163   const MultiVectorBase<Scalar> &XX,
00164   const Ptr<MultiVectorBase<Scalar> > &YY,
00165   const Scalar alpha,
00166   const Scalar beta
00167   ) const
00168 {
00169 
00170   using Teuchos::dyn_cast;
00171   typedef DefaultMultiVectorProductVector<Scalar> MVPV;
00172 
00173   const Ordinal numCols = XX.domain()->dim();
00174 
00175   for (Ordinal col_j = 0; col_j < numCols; ++col_j) {
00176 
00177     const RCP<const VectorBase<Scalar> > x = XX.col(col_j);
00178     const RCP<VectorBase<Scalar> > y = YY->col(col_j);
00179  
00180     RCP<const MultiVectorBase<Scalar> >
00181       X = dyn_cast<const MVPV>(*x).getMultiVector().assert_not_null();
00182     RCP<MultiVectorBase<Scalar> >
00183       Y = dyn_cast<MVPV>(*y).getNonconstMultiVector().assert_not_null();
00184     
00185     Thyra::apply( *lows_.getConstObj(), M_trans, *X, Y.ptr(), alpha, beta );
00186 
00187   }
00188 
00189 }
00190 
00191 
00192 // Overridden from LinearOpWithSolveBase
00193 
00194 
00195 template<class Scalar>
00196 bool
00197 DefaultMultiVectorLinearOpWithSolve<Scalar>::solveSupportsImpl(
00198   EOpTransp M_trans
00199   ) const
00200 {
00201   return Thyra::solveSupports(*lows_.getConstObj(),M_trans);
00202 }
00203 
00204 
00205 template<class Scalar>
00206 bool
00207 DefaultMultiVectorLinearOpWithSolve<Scalar>::solveSupportsSolveMeasureTypeImpl(
00208   EOpTransp M_trans, const SolveMeasureType& solveMeasureType
00209   ) const
00210 {
00211   return Thyra::solveSupportsSolveMeasureType(
00212     *lows_.getConstObj(),M_trans,solveMeasureType);
00213 }
00214 
00215 
00216 template<class Scalar>
00217 SolveStatus<Scalar>
00218 DefaultMultiVectorLinearOpWithSolve<Scalar>::solveImpl(
00219   const EOpTransp transp,
00220   const MultiVectorBase<Scalar> &BB,
00221   const Ptr<MultiVectorBase<Scalar> > &XX,
00222   const Ptr<const SolveCriteria<Scalar> > solveCriteria
00223   ) const
00224 {
00225 
00226   using Teuchos::dyn_cast;
00227   using Teuchos::outArg;
00228   using Teuchos::inOutArg;
00229   typedef DefaultMultiVectorProductVector<Scalar> MVPV;
00230 
00231   const Ordinal numCols = BB.domain()->dim();
00232 
00233   SolveStatus<Scalar> overallSolveStatus;
00234   accumulateSolveStatusInit(outArg(overallSolveStatus));
00235   
00236   for (Ordinal col_j = 0; col_j < numCols; ++col_j) {
00237 
00238     const RCP<const VectorBase<Scalar> > b = BB.col(col_j);
00239     const RCP<VectorBase<Scalar> > x = XX->col(col_j);
00240 
00241     RCP<const MultiVectorBase<Scalar> >
00242       B = dyn_cast<const MVPV>(*b).getMultiVector().assert_not_null();
00243     RCP<MultiVectorBase<Scalar> >
00244       X = dyn_cast<MVPV>(*x).getNonconstMultiVector().assert_not_null();
00245 
00246     const SolveStatus<Scalar> solveStatus =
00247       Thyra::solve(*lows_.getConstObj(), transp, *B, X.ptr(), solveCriteria);
00248 
00249     accumulateSolveStatus(
00250       SolveCriteria<Scalar>(), // Never used
00251       solveStatus, inOutArg(overallSolveStatus) );
00252 
00253   }
00254   
00255   return overallSolveStatus;
00256 
00257 }
00258 
00259 
00260 // private
00261 
00262 
00263 template<class Scalar>
00264 void DefaultMultiVectorLinearOpWithSolve<Scalar>::validateInitialize(
00265   const RCP<const LinearOpWithSolveBase<Scalar> > &lows,
00266   const RCP<const DefaultMultiVectorProductVectorSpace<Scalar> > &multiVecRange,
00267   const RCP<const DefaultMultiVectorProductVectorSpace<Scalar> > &multiVecDomain
00268   )
00269 {
00270 #ifdef TEUCHOS_DEBUG
00271   TEUCHOS_TEST_FOR_EXCEPT(is_null(lows));
00272   TEUCHOS_TEST_FOR_EXCEPT(is_null(multiVecRange));
00273   TEUCHOS_TEST_FOR_EXCEPT(is_null(multiVecDomain));
00274   TEUCHOS_TEST_FOR_EXCEPT( multiVecRange->numBlocks() != multiVecDomain->numBlocks() );
00275   THYRA_ASSERT_VEC_SPACES(
00276     "DefaultMultiVectorLinearOpWithSolve<Scalar>::initialize(lows,multiVecRange,multiVecDomain)",
00277     *lows->range(), *multiVecRange->getBlock(0) );
00278   THYRA_ASSERT_VEC_SPACES(
00279     "DefaultMultiVectorLinearOpWithSolve<Scalar>::initialize(lows,multiVecRange,multiVecDomain)",
00280     *lows->domain(), *multiVecDomain->getBlock(0) );
00281 #endif
00282 }
00283 
00284 
00285 } // end namespace Thyra
00286 
00287 
00288 #endif  // THYRA_MULTI_VECTOR_LINEAR_OP_WITH_SOLVE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines