Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_MultPreconditionerFactory.cpp
1 /*
2 // @HEADER
3 //
4 // ***********************************************************************
5 //
6 // Teko: A package for block and physics based preconditioning
7 // Copyright 2010 Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
40 //
41 // ***********************************************************************
42 //
43 // @HEADER
44 
45 */
46 
47 #include "Teko_MultPreconditionerFactory.hpp"
48 
49 namespace Teko {
50 
51 using Teuchos::RCP;
52 
53 void MultPrecsLinearOp::implicitApply(const Teko::BlockedMultiVector &r,
54  Teko::BlockedMultiVector &y, const double /* alpha */,
55  const double /* beta */) const {
56  // Casting is a bit delicate. We basically use
57  //
58  // 1) deepcopy to copy & cast BlockedMultiVectors to MultiVectors.
59  //
60  // 2) toMultiVector to cast BlockedMultiVectors to MultiVectors.
61  //
62  Teko::MultiVector MOne_r = Teko::deepcopy(r);
63  Teko::MultiVector t = Teko::deepcopy(r);
64  Teko::MultiVector w = Teko::toMultiVector(y);
65 
66  Teko::applyOp(M1_, r, MOne_r);
67  Teko::applyOp(A_, MOne_r, t);
68  Teko::update(1., r, -1., t);
69  Teko::applyOp(M2_, t, w);
70  Teko::update(1., MOne_r, 1., w);
71 }
72 
74 MultPreconditionerFactory ::MultPreconditionerFactory(
75  const RCP<const Teko::BlockPreconditionerFactory> &FirstFactory,
76  const RCP<const Teko::BlockPreconditionerFactory> &SecondFactory)
77  : FirstFactory_(FirstFactory), SecondFactory_(SecondFactory) {}
78 
79 MultPreconditionerFactory::MultPreconditionerFactory() {}
80 
82 RCP<Teko::PreconditionerState> MultPreconditionerFactory::buildPreconditionerState() const {
83  MultPrecondState *mystate = new MultPrecondState();
84  mystate->StateOne_ = Teuchos::rcp_dynamic_cast<BlockPreconditionerState>(
85  FirstFactory_->buildPreconditionerState());
86  mystate->StateTwo_ = Teuchos::rcp_dynamic_cast<BlockPreconditionerState>(
87  SecondFactory_->buildPreconditionerState());
88  return rcp(mystate);
89 }
90 
93  Teko::BlockedLinearOp &blockOp, Teko::BlockPreconditionerState &state) const {
94  MultPrecondState *MyState = dynamic_cast<MultPrecondState *>(&state);
95 
96  TEUCHOS_ASSERT(MyState != 0);
97 
98  Teko::LinearOp M1 = FirstFactory_->buildPreconditionerOperator(blockOp, *MyState->StateOne_);
99  Teko::LinearOp M2 = SecondFactory_->buildPreconditionerOperator(blockOp, *MyState->StateTwo_);
100 
101  /*************************************************************************
102  A different way to create the same preconditioner using the funky
103  matrix representation discussed above. At the present time, there
104  appears to be some kind of bug in Thrya so this doesn't work.
105 
106  const RCP<const Thyra::LinearOpBase<double>> Mat1=
107  Thyra::block2x1(Teko::identity(Teko::rangeSpace(M1)) ,M1); const RCP<const
108  Thyra::LinearOpBase<double>> Mat3= Thyra::block1x2(M2,Teko::identity(Teko::rangeSpace(M1))); const
109  RCP<const Thyra::LinearOpBase<double>> Mat2= Thyra::block2x2(
110  Teko::identity(Teko::rangeSpace(M1)),
111  Teko::scale(-1.,Teko::toLinearOp(blockOp)),
112  Thyra::zero<double>(Teko::rangeSpace(M1),Teko::domainSpace(M1)),
113  Teko::identity(Teko::rangeSpace(M1))); Teko::LinearOp invA = Teko::multiply(Mat3,Mat2,Mat1);
114 
115  return invA;
116  *************************************************************************/
117 
118  // construct an implicit operator corresponding to multiplicative
119  // preconditioning, wrap it in an rcp pointer and return.
120 
121  return Teuchos::rcp(new MultPrecsLinearOp(blockOp, M1, M2));
122 }
123 
125 void MultPreconditionerFactory::initializeFromParameterList(const Teuchos::ParameterList &pl) {
126  RCP<const InverseLibrary> invLib = getInverseLibrary();
127 
128  // get string specifying inverse
129  std::string aStr = "", bStr = "";
130 
131  // "parse" the parameter list
132  aStr = pl.get<std::string>("Preconditioner A");
133  bStr = pl.get<std::string>("Preconditioner B");
134 
135  RCP<const Teuchos::ParameterList> aSettings = invLib->getParameterList(aStr);
136  RCP<const Teuchos::ParameterList> bSettings = invLib->getParameterList(bStr);
137 
138  // build preconditioner from the parameters
139  std::string aType = aSettings->get<std::string>("Preconditioner Type");
140  RCP<Teko::PreconditionerFactory> precA =
142  aType, aSettings->sublist("Preconditioner Settings"), invLib);
143 
144  // build preconditioner from the parameters
145  std::string bType = bSettings->get<std::string>("Preconditioner Type");
146  RCP<Teko::PreconditionerFactory> precB =
148  bType, bSettings->sublist("Preconditioner Settings"), invLib);
149 
150  // set preconditioners
151  FirstFactory_ = Teuchos::rcp_dynamic_cast<const Teko::BlockPreconditionerFactory>(precA);
152  SecondFactory_ = Teuchos::rcp_dynamic_cast<const Teko::BlockPreconditionerFactory>(precB);
153 }
154 
155 } // 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.