EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_ProductOperator.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 
43 #include "Epetra_Vector.h"
44 #include "Epetra_Map.h"
45 
46 namespace EpetraExt {
47 
48 // Constructors / initializers / accessors
49 
51 {}
52 
54  const int num_Op
55  ,const Teuchos::RefCountPtr<const Epetra_Operator> Op[]
56  ,const Teuchos::ETransp Op_trans[]
57  ,const EApplyMode Op_inverse[]
58  )
59 {
60  initialize(num_Op,Op,Op_trans,Op_inverse);
61 }
62 
64  const int num_Op
65  ,const Teuchos::RefCountPtr<const Epetra_Operator> Op[]
66  ,const Teuchos::ETransp Op_trans[]
67  ,const EApplyMode Op_inverse[]
68  )
69 {
70 #ifdef _DEBUG
71  TEUCHOS_TEST_FOR_EXCEPTION(
72  num_Op < 1, std::invalid_argument
73  ,"ProductOperator::initialize(...): Error!"
74  );
75  // ToDo: Validate maps for operators!
76 #endif // _DEBUG
77  Op_.resize(num_Op);
78  Op_trans_.resize(num_Op);
79  Op_inverse_.resize(num_Op);
80  std::copy( Op, Op + num_Op, Op_.begin() );
81  std::copy( Op_trans, Op_trans + num_Op, Op_trans_.begin() );
82  std::copy( Op_inverse, Op_inverse + num_Op, Op_inverse_.begin() );
83  UseTranspose_ = false;
84  // Wipe cache vectors so that they will be reset just to be safe
85  range_vecs_.resize(0);
86  domain_vecs_.resize(0);
87 }
88 
90  int *num_Op
91  ,Teuchos::RefCountPtr<const Epetra_Operator> Op[]
92  ,Teuchos::ETransp Op_trans[]
93  ,EApplyMode Op_inverse[]
94  )
95 {
96 #ifdef _DEBUG
97  TEUCHOS_TEST_FOR_EXCEPTION(
98  (Op!=NULL || Op_trans!=NULL || Op_inverse!=NULL) && num_Op==NULL
99  ,std::invalid_argument
100  ,"ProductOperator::uninitialize(...): Error!"
101  );
102 #endif // _DEBUG
103  if(num_Op) {
104  *num_Op = Op_.size();
105  if(Op) std::copy( Op_.begin(), Op_.end(), Op );
106  if(Op_trans) std::copy( Op_trans_.begin(), Op_trans_.end(), Op_trans );
107  if(Op_inverse) std::copy( Op_inverse_.begin(), Op_inverse_.end(), Op_inverse );
108  }
109  UseTranspose_ = false;
110  Op_.resize(0);
111  Op_trans_.resize(0);
112  Op_inverse_.resize(0);
113  range_vecs_.resize(0);
114  domain_vecs_.resize(0);
115 }
116 
118  const int k
119  ,Teuchos::ETransp Op_trans
120  ,EApplyMode Op_inverse
121  ,const Epetra_MultiVector &X_k
122  ,Epetra_MultiVector *Y_k
123  ) const
124 {
125  Epetra_Operator &Op_k = const_cast<Epetra_Operator&>(*Op_[k]); // Okay since we put back UseTranspose!
126  bool oldUseTranspose = Op_k.UseTranspose();
127  Op_k.SetUseTranspose((Op_trans==Teuchos::NO_TRANS)!=(Op_trans_[k]==Teuchos::NO_TRANS));
128  const bool applyInverse_k = (Op_inverse==APPLY_MODE_APPLY)!=(Op_inverse_[k]==APPLY_MODE_APPLY);
129  const int err = !applyInverse_k ? Op_[k]->Apply(X_k,*Y_k) : Op_[k]->ApplyInverse(X_k,*Y_k);
130  Op_k.SetUseTranspose(oldUseTranspose);
131  TEUCHOS_TEST_FOR_EXCEPTION(
132  err!=0, std::runtime_error
133  ,"ProductOperator::applyConstituent(...): Error, Op["<<k<<"]."
134  <<(!applyInverse_k?"Apply":"ApplyInverse")<<"(...) returned "
135  "err = "<<err<<" with Op["<<k<<"].UseTranspose() = "<<Op_[k]->UseTranspose()<<"!"
136  );
137 }
138 
139 // Overridden from Epetra_Operator
140 
141 int ProductOperator::SetUseTranspose(bool UseTranspose)
142 {
143  assertInitialized();
144  UseTranspose_ = UseTranspose;
145  return 0;
146 }
147 
148 int ProductOperator::Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
149 {
150  assertInitialized();
151  const int num_Op = this->num_Op();
152  // Setup the temporary vectors
153  initializeTempVecs(false);
154  // Apply the constituent operators one at a time!
155  if( !UseTranspose_ ) {
156  //
157  // Forward Mat-vec: Y = M * X (See main documenation)
158  //
159  for( int k = num_Op-1; k >= 0; --k ) {
160  const Epetra_MultiVector &X_k = ( k==num_Op-1 ? X : *range_vecs_[k] );
161  Epetra_MultiVector &Y_k = ( k==0 ? Y : *range_vecs_[k-1] );
162  applyConstituent(k,Teuchos::NO_TRANS,APPLY_MODE_APPLY,X_k,&Y_k);
163  }
164  }
165  else if( UseTranspose_ ) {
166  //
167  // Adjoint Mat-vec: Y = M' * X (See main documentation)
168  //
169  for( int k = 0; k <= num_Op-1; ++k ) {
170  const Epetra_MultiVector &X_k = ( k==0 ? X : *domain_vecs_[k-1] );
171  Epetra_MultiVector &Y_k = ( k==num_Op-1 ? Y : *domain_vecs_[k] );
172  applyConstituent(k,Teuchos::TRANS,APPLY_MODE_APPLY,X_k,&Y_k);
173  }
174  }
175  return 0;
176 }
177 
178 int ProductOperator::ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
179 {
180  assertInitialized();
181  const int num_Op = this->num_Op();
182  // Setup the temporary vectors
183  initializeTempVecs(true);
184  // Apply the constituent operators one at a time!
185  if( !UseTranspose_ ) {
186  //
187  // Forward Inverse Mat-vec: Y = inv(M) * X (See main documenation)
188  //
189  for( int k = 0; k <= num_Op-1; ++k ) {
190  const Epetra_MultiVector &X_k = ( k==0 ? X : *domain_vecs_[k-1] );
191  Epetra_MultiVector &Y_k = ( k==num_Op-1 ? Y : *domain_vecs_[k] );
192  applyConstituent(k,Teuchos::NO_TRANS,APPLY_MODE_APPLY_INVERSE,X_k,&Y_k);
193  }
194  }
195  else if( UseTranspose_ ) {
196  //
197  // Adjoint Invese Mat-vec: Y = inv(M') * X (See main documentation)
198  //
199  for( int k = num_Op-1; k >= 0; --k ) {
200  const Epetra_MultiVector &X_k = ( k==num_Op-1 ? X : *range_vecs_[k] );
201  Epetra_MultiVector &Y_k = ( k==0 ? Y : *range_vecs_[k-1] );
202  applyConstituent(k,Teuchos::TRANS,APPLY_MODE_APPLY_INVERSE,X_k,&Y_k);
203  }
204  }
205  return 0;
206 }
207 
209 {
210  assertInitialized();
211  return -1.0;
212 }
213 
214 const char* ProductOperator::Label() const
215 {
216  assertInitialized();
217  return NULL;
218 }
219 
221 {
222  assertInitialized();
223  return UseTranspose_;
224 }
225 
227 {
228  assertInitialized();
229  return false;
230 }
231 
232 const Epetra_Comm&
234 {
235  assertInitialized();
236  return Op_.front()->OperatorRangeMap().Comm();
237 }
238 
239 const Epetra_Map&
241 {
242  assertInitialized();
243  return ( Op_trans_.back()==Teuchos::NO_TRANS
244  ? Op_.back()->OperatorDomainMap()
245  : Op_.back()->OperatorRangeMap()
246  );
247 }
248 
249 const Epetra_Map&
251 {
252  assertInitialized();
253  return ( Op_trans_.front()==Teuchos::NO_TRANS
254  ? Op_.front()->OperatorRangeMap()
255  : Op_.front()->OperatorDomainMap()
256  );
257 }
258 
259 // private
260 
261 void ProductOperator::initializeTempVecs(bool applyInverse) const
262 {
263  const int num_Op = this->num_Op();
264  if( num_Op > 0 ) {
265  if( ( !UseTranspose_ && !applyInverse ) || ( UseTranspose_ && applyInverse )
266  && range_vecs_.size()==0
267  )
268  {
269  //
270  // Forward Mat-vec
271  //
272  // We need to create storage to hold:
273  //
274  // T[k-1] = M[k]*T[k]
275  //
276  // for k = num_Op-1...1
277  //
278  // where: T[num_Op-1] = X (input vector)
279  //
280  range_vecs_.resize(num_Op-1);
281  for( int k = num_Op-1; k >= 1; --k ) {
282  range_vecs_[k-1] = Teuchos::rcp(
283  new Epetra_Vector(
284  (Op_trans_[k]==Teuchos::NO_TRANS) == (Op_inverse_[k]==APPLY_MODE_APPLY)
285  ? Op_[k]->OperatorRangeMap()
286  : Op_[k]->OperatorDomainMap()
287  )
288  );
289  }
290  }
291  else if( ( UseTranspose_ && !applyInverse ) || ( !UseTranspose_ && applyInverse )
292  && domain_vecs_.size()==0
293  )
294  {
295  //
296  // Adjoint Mat-vec
297  //
298  // We need to create storage to hold:
299  //
300  // T[k] = M[k]'*T[k-1]
301  //
302  // for k = 0...num_Op-2
303  //
304  // where: T[-1] = X (input vector)
305  //
306  domain_vecs_.resize(num_Op-1);
307  for( int k = 0; k <= num_Op-2; ++k ) {
308  domain_vecs_[k] = Teuchos::rcp(
309  new Epetra_Vector(
310  (Op_trans_[k]==Teuchos::NO_TRANS) == (Op_inverse_[k]==APPLY_MODE_APPLY)
311  ? Op_[k]->OperatorDomainMap()
312  : Op_[k]->OperatorRangeMap()
313  )
314  );
315  }
316  }
317  }
318 }
319 
320 } // namespace EpetraExt
void initialize(const int num_Op, const Teuchos::RefCountPtr< const Epetra_Operator > Op[], const Teuchos::ETransp Op_trans[], const EApplyMode Op_inverse[])
Setup with constituent operators.
const Epetra_Map & OperatorDomainMap() const
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
ProductOperator()
Construct to uninitialized.
const Epetra_Comm & Comm() const
Teuchos::RefCountPtr< const Epetra_Operator > Op(int k) const
Access the kth operator (zero-based).
int num_Op() const
Return the number of aggregate opeators.
const Epetra_Map & OperatorRangeMap() const
Teuchos::ETransp Op_trans(int k) const
Access the transpose mode of the kth operator (zero-based).
int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
EApplyMode Op_inverse(int k) const
Access the inverse mode of the kth operator (zero-based).
void uninitialize(int *num_Op, Teuchos::RefCountPtr< const Epetra_Operator > Op[], Teuchos::ETransp Op_trans[], EApplyMode p_inverse[])
Set to an uninitialized state and wipe out memory.
void applyConstituent(const int k, Teuchos::ETransp Op_trans, EApplyMode Op_inverse, const Epetra_MultiVector &X_k, Epetra_MultiVector *Y_k) const
Apply the kth aggregate operator M[k] correctly.