40 #ifndef COMPOSEDPRECONDITIONER_HPP 41 #define COMPOSEDPRECONDITIONER_HPP 44 #include <Epetra_Operator.h> 45 #include <Epetra_MultiVector.h> 47 #include <lifev/core/LifeV.hpp> 48 #include <lifev/core/filter/GetPot.hpp> 49 #include <lifev/core/util/LifeChrono.hpp> 50 #include <lifev/core/util/Displayer.hpp> 63 template<
typename OperatorType>
64 class ComposedOperator
65 :
public Epetra_Operator
73 typedef OperatorType operator_Type;
74 typedef typename std::shared_ptr<operator_Type> operatorPtr_Type;
83 ComposedOperator (
const std::shared_ptr<Epetra_Comm>& comm );
89 ComposedOperator (
const ComposedOperator<operator_Type>& P);
91 virtual ~ComposedOperator();
105 UInt push_back (operatorPtr_Type P,
106 const bool useInverse =
false,
107 const bool useTranspose =
false,
108 const bool summed =
false);
119 UInt push_back (operator_Type* P,
120 const bool useInverse =
false,
121 const bool useTranspose =
false,
122 const bool summed =
false);
132 UInt replace (operatorPtr_Type P,
134 const bool useInverse =
false,
135 const bool useTranspose =
false);
149 void swap (
UInt first,
UInt second);
168 int SetUseTranspose (
bool UseTranspose);
175 int Apply (
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const;
178 int ApplyInverse (
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const;
185 double NormInf()
const;
191 double Condest()
const;
196 const char* Label()
const;
201 bool UseTranspose()
const;
206 bool HasNormInf()
const;
211 const Epetra_Comm& Comm()
const;
216 const std::shared_ptr<Epetra_Comm> commPtr()
const;
221 const Epetra_Map& OperatorDomainMap()
const;
226 const Epetra_Map& OperatorRangeMap()
const;
235 const std::vector<operatorPtr_Type>& Operator()
const 243 std::vector<operatorPtr_Type>& OperatorView()
const 252 bool allTranspose()
const 254 return M_allTranspose;
258 const std::vector<
bool>& transpose()
const 269 const std::vector<ID>& summed()
const 275 const std::vector<
bool>& inverse()
const 285 const double& meanIter()
const 290 int numCalled()
const 295 const Displayer& displayer()
304 void setComm (
const std::shared_ptr<Epetra_Comm> comm);
312 int Apply_DirectOperator (
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const;
314 int Apply_InverseOperator (
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const;
320 mutable std::vector<operatorPtr_Type> M_operator;
321 std::vector<
bool> M_inverse;
322 std::vector<
bool> M_transpose;
323 std::vector<ID> M_summed;
328 mutable double M_meanIter;
329 mutable int M_numCalled;
331 Displayer M_displayer;
339 template <
typename operator_Type>
340 ComposedOperator<operator_Type>::ComposedOperator (
const std::shared_ptr<Epetra_Comm>& comm)
346 M_allTranspose (
false),
354 template <
typename operator_Type>
355 ComposedOperator<operator_Type>::ComposedOperator (
const ComposedOperator<operator_Type>& P)
358 M_inverse (P.inverse() ),
359 M_transpose (P.transpose() ),
360 M_summed (P.summed() ),
361 M_allTranspose (P.allTranspose() ),
363 M_meanIter (P.meanIter() ),
364 M_numCalled (P.numCalled() ),
365 M_displayer (P.commPtr() )
367 for (
UInt i = 0; i < M_set; ++i)
369 M_operator.push_back (P.Operator() [i]);
373 template <
typename operator_Type>
374 ComposedOperator<operator_Type>::~ComposedOperator()
383 template <
typename operator_Type>
384 UInt ComposedOperator<operator_Type>::
385 push_back ( operator_Type* P,
386 const bool useInverse,
387 const bool useTranspose,
390 operatorPtr_Type prec (P);
391 return push_back (P, useInverse, useTranspose, summed);
396 template <
typename operator_Type>
397 UInt ComposedOperator<operator_Type>::
398 push_back ( operatorPtr_Type P,
399 const bool useInverse,
400 const bool useTranspose,
405 M_displayer.leaderPrint (
" CP- Precondtioner not set: ", M_set,
"\n");
417 M_operator. push_back (P);
418 M_inverse. push_back (useInverse);
419 M_transpose.push_back (useTranspose);
422 M_summed.push_back (M_set);
433 template <
typename operator_Type>
434 UInt ComposedOperator<operator_Type>::
435 replace ( operatorPtr_Type P,
437 const bool useInverse,
438 const bool useTranspose)
445 ASSERT (index <= M_set,
"ComposedOperator::replace: index too large");
447 M_displayer.leaderPrint (
" CP- Previous number of call: ", M_numCalled,
"\n");
448 M_displayer.leaderPrint (
" CP- Mean iters: ", M_meanIter,
"\n" );
454 M_operator [index] = P;
455 M_inverse [index] = useInverse;
456 M_transpose[index] = useTranspose;
458 if (useInverse && useTranspose)
468 template <
typename operator_Type>
469 void ComposedOperator<operator_Type>::reset()
471 for (
UInt q (0); q < M_set ; q++)
473 M_operator[q].reset();
478 template <
typename operator_Type>
479 void ComposedOperator<operator_Type>::swap (
UInt first,
UInt second)
481 operatorPtr_Type tmpPrec;
482 tmpPrec = M_operator[first];
483 M_operator[first] = M_operator[second];
484 M_operator[second] = tmpPrec;
491 template <
typename operator_Type>
492 int ComposedOperator<operator_Type>::SetUseTranspose (
bool useTranspose)
494 if (useTranspose != UseTranspose() )
496 M_allTranspose = useTranspose;
497 int size = M_operator.size() - 1;
499 for (
int i = 0; i < size / 2; ++i)
501 operatorPtr_Type swp;
503 M_operator[i] = M_operator[size - i];
504 M_operator[size - i] = swp;
505 M_operator[i]->SetUseTranspose (!M_operator[i]->UseTranspose() );
506 assert (M_operator[size - i]->SetUseTranspose (!M_operator[size - i]->UseTranspose() ) != -1);
510 assert (M_operator[0]->SetUseTranspose (!M_operator[0]->UseTranspose() ) != -1);
512 M_operator[ (
int) (size / 2)]->SetUseTranspose (!M_operator[ (
int) (size / 2)]->UseTranspose() );
517 template <
typename operator_Type>
518 int ComposedOperator<operator_Type>::
519 Apply (
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const 521 return Apply_DirectOperator (X, Y);
525 template <
typename operator_Type>
526 int ComposedOperator<operator_Type>::
527 ApplyInverse (
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const 529 return Apply_InverseOperator (X, Y);
532 template <
typename operator_Type>
533 double ComposedOperator<operator_Type>::NormInf()
const 536 return (EXIT_FAILURE);
539 template <
typename operator_Type>
540 double ComposedOperator<operator_Type>::Condest()
const 544 for (
UInt q (0); q < M_set ; q++)
548 cond = cond / M_operator[q]->condest();
552 cond *= M_operator[q]->condest();
558 template <
typename operator_Type>
559 const char* ComposedOperator<operator_Type>::
562 return (
"Composed operator_Type P1*P2* ... *Pn");
569 template <
typename operator_Type>
570 bool ComposedOperator<operator_Type>::UseTranspose()
const 572 return (M_allTranspose);
575 template <
typename operator_Type>
576 bool ComposedOperator<operator_Type>::
583 template <
typename operator_Type>
584 const Epetra_Comm& ComposedOperator<operator_Type>::
589 return ( *M_displayer.comm() );
592 template <
typename operator_Type>
593 const std::shared_ptr<Epetra_Comm> ComposedOperator<operator_Type>::
596 return ( M_displayer.comm() );
600 template <
typename operator_Type>
601 const Epetra_Map& ComposedOperator<operator_Type>::
602 OperatorDomainMap()
const 606 return ( M_operator[M_set - 1]->OperatorDomainMap() );
609 template <
typename operator_Type>
610 const Epetra_Map& ComposedOperator<operator_Type>::
611 OperatorRangeMap()
const 615 return ( M_operator[0]->OperatorRangeMap() );
623 template <
typename operator_Type>
void 624 ComposedOperator<operator_Type>::setComm (
const std::shared_ptr<Epetra_Comm> comm)
627 M_displayer.setCommunicator (comm);
634 template <
typename operator_Type>
635 int ComposedOperator<operator_Type>::
636 Apply_DirectOperator (
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const 641 Epetra_MultiVector Z (X);
646 for (
Int q (M_set - 1); q >= 0 ; q--)
648 if (M_summed.size() && q == (Int) M_summed[k])
656 useTranspose = M_operator[q]->UseTranspose ();
657 assert (M_operator[q]->SetUseTranspose (M_transpose[q]) != -1);
658 M_operator[q]->ApplyInverse (Z, Y);
659 M_operator[q]->SetUseTranspose (useTranspose);
663 useTranspose = M_operator[q]->UseTranspose ();
664 assert (M_operator[q]->SetUseTranspose (M_transpose[q]) != -1);
665 M_operator[q]->Apply (Z, Y);
666 M_operator[q]->SetUseTranspose (useTranspose);
672 for (UInt k = 0; k < M_summed.size(); ++k)
674 if (M_inverse[M_summed[k]])
676 M_operator[M_summed[k]]->ApplyInverse (X, Y);
680 M_operator[M_summed[k]]->Apply (X, Y);
682 Z.Update (1., Y, 1.);
686 return (EXIT_SUCCESS);
690 template <
typename operator_Type>
691 int ComposedOperator<operator_Type>::
692 Apply_InverseOperator (
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const 699 Epetra_MultiVector Z (X);
702 for (
UInt q (0); q < M_set ; q++, Z = Y)
704 if (M_summed.size() && q == M_summed[k])
712 useTranspose = M_operator[q]->UseTranspose ();
713 ASSERT (M_operator[q]->SetUseTranspose (M_transpose[q]) != -1,
"SetUseTranspose failed in the preconditioner of your choice");
714 M_operator[q]->Apply (Z, Y);
715 M_operator[q]->SetUseTranspose (useTranspose);
719 useTranspose = M_operator[q]->UseTranspose ();
721 ASSERT (!M_transpose[q] || M_operator[q]->SetUseTranspose (M_transpose[q]) == -1,
"Something went wrong in SetUseTranspose for your preconditioner.\n");
723 M_operator[q]->ApplyInverse (Z, Y);
724 M_operator[q]->SetUseTranspose (useTranspose);
730 for (UInt k = 0; k < M_summed.size(); ++k)
732 if (M_inverse[M_summed[k]])
734 useTranspose = M_operator[M_summed[k]]->UseTranspose ();
735 assert (M_operator[M_summed[k]]->SetUseTranspose (M_transpose[M_summed[k]]) != -1);
736 M_operator[M_summed[k]]->Apply (X, Y);
737 M_operator[M_summed[k]]->SetUseTranspose (useTranspose);
741 useTranspose = M_operator[M_summed[k]]->UseTranspose ();
742 assert (M_operator[M_summed[k]]->SetUseTranspose (M_transpose[M_summed[k]]) != -1);
743 M_operator[M_summed[k]]->ApplyInverse (X, Y);
744 M_operator[M_summed[k]]->SetUseTranspose (useTranspose);
746 Z.Update (1., Y, 1.);
750 return (EXIT_SUCCESS);
void assignFunction(bcBase_Type &base)
Assign the function to the base of the BCHandler.
int32_type Int
Generic integer data.
uint32_type UInt
generic unsigned integer (used mainly for addressing)