Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_MultPreconditionerFactory.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Teko: A package for block and physics based preconditioning
4 //
5 // Copyright 2010 NTESS and the Teko contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teko_MultPreconditionerFactory.hpp"
11 
12 namespace Teko {
13 
14 using Teuchos::RCP;
15 
16 void MultPrecsLinearOp::implicitApply(const Teko::BlockedMultiVector &r,
17  Teko::BlockedMultiVector &y, const double /* alpha */,
18  const double /* beta */) const {
19  // Casting is a bit delicate. We basically use
20  //
21  // 1) deepcopy to copy & cast BlockedMultiVectors to MultiVectors.
22  //
23  // 2) toMultiVector to cast BlockedMultiVectors to MultiVectors.
24  //
25  Teko::MultiVector MOne_r = Teko::deepcopy(r);
26  Teko::MultiVector t = Teko::deepcopy(r);
27  Teko::MultiVector w = Teko::toMultiVector(y);
28 
29  Teko::applyOp(M1_, r, MOne_r);
30  Teko::applyOp(A_, MOne_r, t);
31  Teko::update(1., r, -1., t);
32  Teko::applyOp(M2_, t, w);
33  Teko::update(1., MOne_r, 1., w);
34 }
35 
37 MultPreconditionerFactory ::MultPreconditionerFactory(
38  const RCP<const Teko::BlockPreconditionerFactory> &FirstFactory,
39  const RCP<const Teko::BlockPreconditionerFactory> &SecondFactory)
40  : FirstFactory_(FirstFactory), SecondFactory_(SecondFactory) {}
41 
42 MultPreconditionerFactory::MultPreconditionerFactory() {}
43 
45 RCP<Teko::PreconditionerState> MultPreconditionerFactory::buildPreconditionerState() const {
46  MultPrecondState *mystate = new MultPrecondState();
47  mystate->StateOne_ = Teuchos::rcp_dynamic_cast<BlockPreconditionerState>(
48  FirstFactory_->buildPreconditionerState());
49  mystate->StateTwo_ = Teuchos::rcp_dynamic_cast<BlockPreconditionerState>(
50  SecondFactory_->buildPreconditionerState());
51  return rcp(mystate);
52 }
53 
56  Teko::BlockedLinearOp &blockOp, Teko::BlockPreconditionerState &state) const {
57  MultPrecondState *MyState = dynamic_cast<MultPrecondState *>(&state);
58 
59  TEUCHOS_ASSERT(MyState != 0);
60 
61  Teko::LinearOp M1 = FirstFactory_->buildPreconditionerOperator(blockOp, *MyState->StateOne_);
62  Teko::LinearOp M2 = SecondFactory_->buildPreconditionerOperator(blockOp, *MyState->StateTwo_);
63 
64  /*************************************************************************
65  A different way to create the same preconditioner using the funky
66  matrix representation discussed above. At the present time, there
67  appears to be some kind of bug in Thrya so this doesn't work.
68 
69  const RCP<const Thyra::LinearOpBase<double>> Mat1=
70  Thyra::block2x1(Teko::identity(Teko::rangeSpace(M1)) ,M1); const RCP<const
71  Thyra::LinearOpBase<double>> Mat3= Thyra::block1x2(M2,Teko::identity(Teko::rangeSpace(M1))); const
72  RCP<const Thyra::LinearOpBase<double>> Mat2= Thyra::block2x2(
73  Teko::identity(Teko::rangeSpace(M1)),
74  Teko::scale(-1.,Teko::toLinearOp(blockOp)),
75  Thyra::zero<double>(Teko::rangeSpace(M1),Teko::domainSpace(M1)),
76  Teko::identity(Teko::rangeSpace(M1))); Teko::LinearOp invA = Teko::multiply(Mat3,Mat2,Mat1);
77 
78  return invA;
79  *************************************************************************/
80 
81  // construct an implicit operator corresponding to multiplicative
82  // preconditioning, wrap it in an rcp pointer and return.
83 
84  return Teuchos::rcp(new MultPrecsLinearOp(blockOp, M1, M2));
85 }
86 
88 void MultPreconditionerFactory::initializeFromParameterList(const Teuchos::ParameterList &pl) {
89  RCP<const InverseLibrary> invLib = getInverseLibrary();
90 
91  // get string specifying inverse
92  std::string aStr = "", bStr = "";
93 
94  // "parse" the parameter list
95  aStr = pl.get<std::string>("Preconditioner A");
96  bStr = pl.get<std::string>("Preconditioner B");
97 
98  RCP<const Teuchos::ParameterList> aSettings = invLib->getParameterList(aStr);
99  RCP<const Teuchos::ParameterList> bSettings = invLib->getParameterList(bStr);
100 
101  // build preconditioner from the parameters
102  std::string aType = aSettings->get<std::string>("Preconditioner Type");
103  RCP<Teko::PreconditionerFactory> precA =
105  aType, aSettings->sublist("Preconditioner Settings"), invLib);
106 
107  // build preconditioner from the parameters
108  std::string bType = bSettings->get<std::string>("Preconditioner Type");
109  RCP<Teko::PreconditionerFactory> precB =
111  bType, bSettings->sublist("Preconditioner Settings"), invLib);
112 
113  // set preconditioners
114  FirstFactory_ = Teuchos::rcp_dynamic_cast<const Teko::BlockPreconditionerFactory>(precA);
115  SecondFactory_ = Teuchos::rcp_dynamic_cast<const Teko::BlockPreconditionerFactory>(precB);
116 }
117 
118 } // end namespace Teko
virtual Teuchos::RCP< Teko::PreconditionerState > buildPreconditionerState() const
Build the MultPrecondState object.
virtual void initializeFromParameterList(const Teuchos::ParameterList &pl)
Initialize from a parameter list.
Abstract class which block preconditioner factories in Teko should be based on.
An implementation of a state object for block preconditioners.
Teuchos::RCP< const InverseLibrary > getInverseLibrary() const
Get the inverse library used by this preconditioner factory.
Teko::LinearOp buildPreconditionerOperator(Teko::BlockedLinearOp &blo, Teko::BlockPreconditionerState &state) const
Function inherited from Teko::BlockPreconditionerFactory.
static Teuchos::RCP< PreconditionerFactory > buildPreconditionerFactory(const std::string &name, const Teuchos::ParameterList &settings, const Teuchos::RCP< const InverseLibrary > &invLib=Teuchos::null)
Builder function for creating preconditioner factories (yes this is a factory factory).
virtual void implicitApply(const Teko::BlockedMultiVector &r, Teko::BlockedMultiVector &y, const double alpha=1.0, const double beta=0.0) const
Perform a matrix vector multiply with this implicitly defined blocked operator.