Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_EpetraExtDiagScalingTransformer.cpp
Go to the documentation of this file.
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 
44 #include "Thyra_MultipliedLinearOpBase.hpp"
45 #include "Thyra_DiagonalLinearOpBase.hpp"
46 #include "Thyra_ScaledAdjointLinearOpBase.hpp"
47 #include "Thyra_EpetraLinearOp.hpp"
50 #include "Epetra_Map.h"
51 #include "Epetra_LocalMap.h"
52 #include "Epetra_SerialComm.h"
53 #include "Epetra_CrsMatrix.h"
54 #include "Teuchos_Assert.hpp"
55 #include "Teuchos_RCP.hpp"
56 
57 
58 namespace Thyra {
59 
60 
61 // Overridden from LinearOpTransformerBase
62 
63 
65  const LinearOpBase<double> &op_in) const
66 {
67  using Thyra::unwrap;
68  using Teuchos::rcp;
69  using Teuchos::rcp_dynamic_cast;
70  using Teuchos::dyn_cast;
71 
72  const MultipliedLinearOpBase<double> &multi_op =
73  dyn_cast<const MultipliedLinearOpBase<double> >(op_in);
74 
75  // this operation must only have two operands
76  if(multi_op.numOps()!=2)
77  return false;
78 
79  double scalar = 0.0;
80  EOpTransp transp = NOTRANS;
81 
82  // get properties of first operator: Transpose, scaler multiply...etc
84  unwrap( multi_op.getOp(0), &scalar, &transp, &A );
85  if(transp!=NOTRANS) return false;
86 
87  // get properties of second operator: Transpose, scaler multiply...etc
89  unwrap( multi_op.getOp(1), &scalar, &transp, &B );
90  if(transp!=NOTRANS) return false;
91 
92  // see if exactly one operator is a diagonal linear op
94  = rcp_dynamic_cast<const DiagonalLinearOpBase<double> >(A);
96  = rcp_dynamic_cast<const DiagonalLinearOpBase<double> >(B);
97 
98  if(dA==Teuchos::null && dB!=Teuchos::null)
99  return true;
100 
101  if(dA!=Teuchos::null && dB==Teuchos::null)
102  return true;
103 
104  return false;
105 }
106 
107 
110 {
111  return nonconstEpetraLinearOp();
112 }
113 
114 
116  const LinearOpBase<double> &op_in,
117  const Ptr<LinearOpBase<double> > &op_inout) const
118 {
119  using Thyra::unwrap;
120  using Teuchos::rcp;
121  using Teuchos::rcp_dynamic_cast;
122  using Teuchos::dyn_cast;
123 
124  //
125  // A) Get the component Thyra objects for M = op(A) * op(B)
126  //
127 
128  const MultipliedLinearOpBase<double> &multi_op =
129  dyn_cast<const MultipliedLinearOpBase<double> >(op_in);
130 
131  TEUCHOS_ASSERT(multi_op.numOps()==2);
132 
133  // get properties of first operator: Transpose, scaler multiply...etc
134  const RCP<const LinearOpBase<double> > op_A = multi_op.getOp(0);
135  double A_scalar = 0.0;
136  EOpTransp A_transp = NOTRANS;
138  unwrap( op_A, &A_scalar, &A_transp, &A );
139  TEUCHOS_ASSERT(A_transp==NOTRANS || A_transp==CONJTRANS); // sanity check
140 
141  // get properties of third operator: Transpose, scaler multiply...etc
142  const RCP<const LinearOpBase<double> > op_B = multi_op.getOp(1);
143  double B_scalar = 0.0;
144  EOpTransp B_transp = NOTRANS;
146  unwrap( op_B, &B_scalar, &B_transp, &B );
147  TEUCHOS_ASSERT(B_transp==NOTRANS || B_transp==CONJTRANS); // sanity check
148 
149  //
150  // B) Extract out the CrsMatrix and Diagonal objects
151  //
152 
153  // see if exactly one operator is a diagonal linear op
155  = rcp_dynamic_cast<const DiagonalLinearOpBase<double> >(A);
157  = rcp_dynamic_cast<const DiagonalLinearOpBase<double> >(B);
158 
161  if(dA==Teuchos::null) {
162  bool exactly_one_op_must_be_diagonal__dB_neq_null = dB!=Teuchos::null;
163  TEUCHOS_ASSERT(exactly_one_op_must_be_diagonal__dB_neq_null);
164 
165  // convert second operator to an Epetra_CrsMatrix
166  epetra_A = rcp_dynamic_cast<const Epetra_CrsMatrix>(get_Epetra_Operator(*A), true);
167  }
168  else if(dB==Teuchos::null) {
169  bool exactly_one_op_must_be_diagonal__dA_neq_null = dA!=Teuchos::null;
170  TEUCHOS_ASSERT(exactly_one_op_must_be_diagonal__dA_neq_null);
171 
172  // convert second operator to an Epetra_CrsMatrix
173  epetra_B = rcp_dynamic_cast<const Epetra_CrsMatrix>(get_Epetra_Operator(*B), true);
174  }
175  else {
176  bool exactly_one_op_must_be_diagonal=false;
177  TEUCHOS_ASSERT(exactly_one_op_must_be_diagonal);
178  }
179 
180  TEUCHOS_ASSERT( A_transp == NOTRANS ); // ToDo: Handle the transpose
181  TEUCHOS_ASSERT( B_transp == NOTRANS ); // ToDo: Handle the transpose
182 
183 
184  // get or allocate an object to use as the destination
185  EpetraLinearOp & thyra_epetra_op_inout = dyn_cast<EpetraLinearOp>(*op_inout);
186  RCP<Epetra_CrsMatrix> epetra_op =
187  rcp_dynamic_cast<Epetra_CrsMatrix>(thyra_epetra_op_inout.epetra_op());
188 
189  bool rightScale = dB!=Teuchos::null;
190 
191  if(rightScale) {
192  RCP<const Epetra_Vector> v = get_Epetra_Vector(epetra_A->OperatorDomainMap(),dB->getDiag());
193 
194  // if needed allocate a new operator, otherwise use old one assuming
195  // constant sparsity
196  if(epetra_op==Teuchos::null)
197  epetra_op = Teuchos::rcp(new Epetra_CrsMatrix(*epetra_A));
198  else
199  *epetra_op = *epetra_A;
200  epetra_op->RightScale(*v);
201  }
202  else {
203  RCP<const Epetra_Vector> v = get_Epetra_Vector(epetra_B->OperatorRangeMap(),dA->getDiag());
204 
205  // if needed allocate a new operator, otherwise use old one assuming
206  // constant sparsity
207  if(epetra_op==Teuchos::null)
208  epetra_op = Teuchos::rcp(new Epetra_CrsMatrix(*epetra_B));
209  else
210  *epetra_op = *epetra_B;
211  epetra_op->LeftScale(*v);
212  }
213 
214  epetra_op->Scale(A_scalar*B_scalar);
215 
216  // set output operator to use newly create epetra_op
217  thyra_epetra_op_inout.initialize(epetra_op);
218 }
219 
220 
221 } // namespace Thyra
Concrete LinearOpBase adapter subclass for Epetra_Operator object.
virtual bool isCompatible(const LinearOpBase< double > &op_in) const
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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
T_To & dyn_cast(T_From &from)
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 RCP< LinearOpBase< double > > createOutputOp() const
Teuchos::RCP< Epetra_Operator > get_Epetra_Operator(LinearOpBase< double > &op)
Full specialization for Scalar=double.
virtual void transform(const LinearOpBase< double > &op_in, const Ptr< LinearOpBase< double > > &op_inout) const
#define TEUCHOS_ASSERT(assertion_test)
RCP< Epetra_Operator > epetra_op()