TSFEpetraMatrixMatrixSum.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 /* ***********************************************************************
00003 // 
00004 //           TSFExtended: Trilinos Solver Framework Extended
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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // **********************************************************************/
00027  /* @HEADER@ */
00028 
00029 #include "TSFEpetraMatrix.hpp"
00030 #include "TSFEpetraMatrixMatrixSum.hpp"
00031 #include "TSFEpetraVector.hpp"
00032 #include "SundanceExceptions.hpp"
00033 #include "EpetraExt_MatrixMatrix.h"
00034 
00035 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00036 #include "TSFVectorImpl.hpp"
00037 #include "TSFLinearOperatorImpl.hpp"
00038 #endif
00039 
00040 
00041 
00042 namespace TSFExtended
00043 {
00044 using namespace Teuchos;
00045 
00046 
00047 LinearOperator<double> epetraMatrixMatrixSum(
00048   const LinearOperator<double>& A,
00049   const LinearOperator<double>& B)
00050 {
00051   /* Extract the underlying Epetra matrix for A. Type checking is done
00052    * ny rcp_dynamic_cast, so we need no error check here. */
00053   RCP<const Epetra_CrsMatrix> A_crs = EpetraMatrix::getConcretePtr(A);
00054 
00055   /* Extract the underlying Epetra matrix for A. Type checking is done
00056    * ny rcp_dynamic_cast, so we need no error check here. */
00057   RCP<const Epetra_CrsMatrix> B_crs = EpetraMatrix::getConcretePtr(B);
00058   
00059   bool transA = false;
00060   bool transB = false;
00061 
00062   TEST_FOR_EXCEPTION(A.range() != B.range(), RuntimeError,
00063     "incompatible operand ranges in epetraMatrixMatrixSum()"
00064     << std::endl << "A.range()=" << A.range()
00065     << std::endl << "B.range()=" << B.range()
00066     );
00067   
00068 
00069   TEST_FOR_EXCEPTION(A.domain() != B.domain(), RuntimeError,
00070     "incompatible operand domains in epetraMatrixMatrixSum()"
00071     << std::endl << "A.domain()=" << A.domain()
00072     << std::endl << "B.domain()=" << B.domain()
00073     );
00074   
00075 
00076   /* Get the row map from A. We will need this to build the target matrix C */
00077   const Epetra_Map* rowmap 
00078     = transA ? &(A_crs->DomainMap()) : &(A_crs->RowMap());
00079 
00080   /* make the target matrix */
00081   RCP<Epetra_CrsMatrix> C = rcp(new Epetra_CrsMatrix(Copy, *rowmap, 1));
00082   Epetra_CrsMatrix* CPtr = C.get();
00083 
00084   /* Carry out the multiplication */
00085   int ierr 
00086     = EpetraExt::MatrixMatrix::Add(
00087       *A_crs, transA, 1.0, 
00088       *B_crs, transB, 1.0, CPtr);
00089   TEST_FOR_EXCEPTION(ierr != 0, RuntimeError,
00090     "EpetraExt Matrix-matrix add failed with error code ierr=" << ierr);
00091 
00092   /* Need to call FillComplete on the result */
00093   C->FillComplete();
00094 
00095   /* Prepare an operator object for the scaled matrix */
00096   RCP<const EpetraVectorSpace> range 
00097     = rcp_dynamic_cast<const EpetraVectorSpace>(A.range().ptr());
00098 
00099   RCP<const EpetraVectorSpace> domain 
00100     = rcp_dynamic_cast<const EpetraVectorSpace>(B.domain().ptr());
00101 
00102   RCP<LinearOpBase<double> > rtn 
00103     = rcp(new EpetraMatrix(C, domain, range));
00104   return rtn;
00105   
00106 }
00107 
00108 }

Site Contact