Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_EpetraExtDiagScaledMatProdTransformer.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
43 #include "Thyra_EpetraExtDiagScaledMatProdTransformer.hpp"
44 #include "Thyra_MultipliedLinearOpBase.hpp"
45 #include "Thyra_DiagonalLinearOpBase.hpp"
46 #include "Thyra_ScaledAdjointLinearOpBase.hpp"
47 #include "Thyra_EpetraLinearOp.hpp"
48 #include "Thyra_get_Epetra_Operator.hpp"
50 #include "Epetra_Map.h"
51 #include "Epetra_LocalMap.h"
52 #include "Epetra_SerialComm.h"
53 #include "Epetra_CrsMatrix.h"
54 #include "EpetraExt_MatrixMatrix.h"
55 #include "Teuchos_Assert.hpp"
56 
57 
58 namespace Thyra {
59 
60 
61 // Overridden from LinearOpTransformerBase
62 
63 
65  const LinearOpBase<double> &/* op_in */) const
66 {
69 }
70 
71 
74 {
75  return nonconstEpetraLinearOp();
76 }
77 
78 
80  const LinearOpBase<double> &op_in,
81  const Ptr<LinearOpBase<double> > &op_inout) const
82 {
83  using Thyra::unwrap;
84  using EpetraExt::MatrixMatrix;
85  using Teuchos::rcp;
86  using Teuchos::rcp_dynamic_cast;
87  using Teuchos::dyn_cast;
88 
89  //
90  // A) Get the component Thyra objects for M = op(B) * D * G
91  //
92 
93  const MultipliedLinearOpBase<double> &multi_op =
95 
96  bool haveDiagScaling = (multi_op.numOps()==3);
97 
98  // get properties of first operator: Transpose, scaler multiply...etc
99  const RCP<const LinearOpBase<double> > op_B = multi_op.getOp(0);
100  double B_scalar = 0.0;
101  EOpTransp B_transp = NOTRANS;
103  unwrap( op_B, &B_scalar, &B_transp, &B );
104  TEUCHOS_ASSERT(B_transp==NOTRANS || B_transp==CONJTRANS); // sanity check
105 
106  // get diagonal scaling
108  double D_scalar = 1.0;
109  if(haveDiagScaling) {
110  const RCP<const LinearOpBase<double> > op_D = multi_op.getOp(1);
111  EOpTransp D_transp = NOTRANS;
113  unwrap( op_D, &D_scalar, &D_transp, &D );
114  d = rcp_dynamic_cast<const DiagonalLinearOpBase<double> >(D, true)->getDiag();
115  }
116 
117  // get properties of third operator: Transpose, scaler multiply...etc
118  const RCP<const LinearOpBase<double> > op_G = multi_op.getOp(haveDiagScaling ? 2 : 1);
119  double G_scalar = 0.0;
120  EOpTransp G_transp = NOTRANS;
122  unwrap( op_G, &G_scalar, &G_transp, &G );
123  TEUCHOS_ASSERT(G_transp==NOTRANS || G_transp==CONJTRANS); // sanity check
124 
125  //
126  // B) Extract out the Epetra_CrsMatrix objects and the vector
127  //
128 
129  // convert second operator to an Epetra_CrsMatrix
130  const RCP<const Epetra_CrsMatrix> epetra_B =
131  rcp_dynamic_cast<const Epetra_CrsMatrix>(get_Epetra_Operator(*B), true);
132  // TEUCHOS_ASSERT( B_transp == NOTRANS ); // ToDo: Handle the transpose
133 
134  // extract dagonal
135  RCP<const Epetra_Vector> epetra_d;
136  if(haveDiagScaling) {
137  epetra_d = (B_transp==CONJTRANS ? get_Epetra_Vector(epetra_B->OperatorRangeMap(), d)
138  : get_Epetra_Vector(epetra_B->OperatorDomainMap(), d));
139  }
140 
141  // convert third operator to an Epetra_CrsMatrix
142  const RCP<const Epetra_CrsMatrix> epetra_G =
143  rcp_dynamic_cast<const Epetra_CrsMatrix>(get_Epetra_Operator(*G), true);
144 
145  // determine row map for final operator
146  const Epetra_Map op_inout_row_map
147  = (B_transp==CONJTRANS ? epetra_B->ColMap() : epetra_B->RowMap());
148  const Epetra_Map op_inout_col_map
149  = (G_transp==CONJTRANS ? epetra_B->RowMap() : epetra_B->ColMap());
150 
151  //
152  // C) Do the explicit multiplication
153  //
154 
155  // allocate space for final product: 3 steps
156  // 1. Get destination EpetraLinearOp
157  // 2. Extract RCP to destination Epetra_CrsMatrix
158  // 3. If neccessary, allocate new Epetra_CrsMatrix
159  EpetraLinearOp &thyra_epetra_op_inout = dyn_cast<EpetraLinearOp>(*op_inout);
160  RCP<Epetra_CrsMatrix> epetra_op =
161  rcp_dynamic_cast<Epetra_CrsMatrix>(thyra_epetra_op_inout.epetra_op());
162  if(is_null(epetra_op)) {
163  epetra_op = Teuchos::rcp(
164  new Epetra_CrsMatrix(::Copy, op_inout_row_map, 0));
165  }
166 
167  // if necessary scale B by diagonal vector
168  RCP<const Epetra_CrsMatrix> epetra_BD;
169  if(haveDiagScaling) {
170  // create a temporary to get around const issue
171  RCP<Epetra_CrsMatrix> epetra_BD_temp = rcp(new Epetra_CrsMatrix(*epetra_B));
172 
173  // scale matrix depending on properties of B
174  if(B_transp==CONJTRANS)
175  epetra_BD_temp->LeftScale(*epetra_d);
176  else
177  epetra_BD_temp->RightScale(*epetra_d);
178 
179  epetra_BD = epetra_BD_temp;
180  }
181  else
182  epetra_BD = epetra_B;
183 
184  // perform multiply
185  int mm_error = MatrixMatrix::Multiply( *epetra_BD, B_transp==CONJTRANS,
186  *epetra_G, G_transp==CONJTRANS, *epetra_op);
187  TEUCHOS_TEST_FOR_EXCEPTION(mm_error!=0,std::invalid_argument,
188  "EpetraExt::MatrixMatrix::Multiply failed returning error code " << mm_error << ".");
189 
190  // scale the whole thing if neccessary
191  if(B_scalar*G_scalar*D_scalar!=1.0)
192  epetra_op->Scale(B_scalar*G_scalar*D_scalar);
193 
194  // set output operator to use newly create epetra_op
195  thyra_epetra_op_inout.initialize(epetra_op);
196 }
197 
198 
199 } // namespace Thyra
Concrete LinearOpBase adapter subclass for Epetra_Operator object.
bool is_null(const boost::shared_ptr< T > &p)
EOpTransp
Enumeration for determining how a linear operator is applied. `*.
void unwrap(const LinearOpBase< Scalar > &Op, Scalar *scalar, EOpTransp *transp, const LinearOpBase< Scalar > **origOp)
Extract the overallScalar, overallTransp and const origOp from a const LinearOpBase object...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Use the non-transposed operator.
T_To & dyn_cast(T_From &from)
Use the transposed operator with complex-conjugate clements (same as TRANS for real scalar types)...
const Epetra_Map & ColMap() const
const Epetra_Map & OperatorDomainMap() const
virtual int numOps() const =0
Returns the number of constituent operators.
void initialize(const RCP< Epetra_Operator > &op, EOpTransp opTrans=NOTRANS, EApplyEpetraOpAs applyAs=EPETRA_OP_APPLY_APPLY, EAdjointEpetraOp adjointSupport=EPETRA_OP_ADJOINT_SUPPORTED, const RCP< const VectorSpaceBase< double > > &range=Teuchos::null, const RCP< const VectorSpaceBase< double > > &domain=Teuchos::null)
Fully initialize.
const Epetra_Map & RowMap() const
Interface class for implicitly multiplied linear operators.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< Epetra_Vector > get_Epetra_Vector(const Epetra_Map &map, const RCP< VectorBase< double > > &v)
Get a non-const Epetra_Vector view from a non-const VectorBase object if possible.
virtual void transform(const LinearOpBase< double > &op_in, const Ptr< LinearOpBase< double > > &op_inout) const
int LeftScale(const Epetra_Vector &x)
const Epetra_Map & OperatorRangeMap() const
#define TEUCHOS_UNREACHABLE_RETURN(dummyReturnVal)
Teuchos::RCP< Epetra_Operator > get_Epetra_Operator(LinearOpBase< Scalar > &)
Get smart pointer to non-const Epetra_Operator object from reference to a non-const EpetraLinearOp ac...
virtual bool isCompatible(const LinearOpBase< double > &op_in) const
#define TEUCHOS_ASSERT(assertion_test)
int Scale(double ScalarConstant)
virtual Teuchos::RCP< const LinearOpBase< Scalar > > getOp(const int k) const =0
Return the kth constant constituent operator.
int RightScale(const Epetra_Vector &x)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Interface class for for diagonal linear operators.
RCP< Epetra_Operator > epetra_op()