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, Teko::BlockedMultiVector & y,
54  const double /* alpha */, const double /* beta */) const
55 {
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
75  ::MultPreconditionerFactory(const RCP<const Teko::BlockPreconditionerFactory> & FirstFactory,
76  const RCP<const Teko::BlockPreconditionerFactory> & SecondFactory)
77  : FirstFactory_(FirstFactory), SecondFactory_(SecondFactory)
78 { }
79 
80 MultPreconditionerFactory::MultPreconditionerFactory()
81 { }
82 
84 RCP<Teko::PreconditionerState> MultPreconditionerFactory::buildPreconditionerState() const
85 {
86  MultPrecondState* mystate = new MultPrecondState();
87  mystate->StateOne_ = Teuchos::rcp_dynamic_cast<BlockPreconditionerState>(FirstFactory_->buildPreconditionerState());
88  mystate->StateTwo_ = Teuchos::rcp_dynamic_cast<BlockPreconditionerState>(SecondFactory_->buildPreconditionerState());
89  return rcp(mystate);
90 }
91 
92 
94 Teko::LinearOp MultPreconditionerFactory
95  ::buildPreconditionerOperator(Teko::BlockedLinearOp & blockOp,
96  Teko::BlockPreconditionerState & state) const
97 {
98 
99  MultPrecondState *MyState = dynamic_cast<MultPrecondState *> (&state);
100 
101  TEUCHOS_ASSERT(MyState != 0);
102 
103  Teko::LinearOp M1 = FirstFactory_->buildPreconditionerOperator(blockOp, *MyState->StateOne_);
104  Teko::LinearOp M2 = SecondFactory_->buildPreconditionerOperator(blockOp, *MyState->StateTwo_);
105 
106 
107  /*************************************************************************
108  A different way to create the same preconditioner using the funky
109  matrix representation discussed above. At the present time, there
110  appears to be some kind of bug in Thrya so this doesn't work.
111 
112  const RCP<const Thyra::LinearOpBase<double>> Mat1= Thyra::block2x1(Teko::identity(Teko::rangeSpace(M1)) ,M1);
113  const RCP<const Thyra::LinearOpBase<double>> Mat3= Thyra::block1x2(M2,Teko::identity(Teko::rangeSpace(M1)));
114  const RCP<const Thyra::LinearOpBase<double>> Mat2= Thyra::block2x2(
115  Teko::identity(Teko::rangeSpace(M1)), Teko::scale(-1.,Teko::toLinearOp(blockOp)),
116  Thyra::zero<double>(Teko::rangeSpace(M1),Teko::domainSpace(M1)), Teko::identity(Teko::rangeSpace(M1)));
117  Teko::LinearOp invA = Teko::multiply(Mat3,Mat2,Mat1);
118 
119  return invA;
120  *************************************************************************/
121 
122  // construct an implicit operator corresponding to multiplicative
123  // preconditioning, wrap it in an rcp pointer and return.
124 
125  return Teuchos::rcp(new MultPrecsLinearOp(blockOp,M1,M2));
126 }
127 
129 void MultPreconditionerFactory::initializeFromParameterList(const Teuchos::ParameterList & pl)
130 {
131  RCP<const InverseLibrary> invLib = getInverseLibrary();
132 
133  // get string specifying inverse
134  std::string aStr="", bStr="";
135 
136  // "parse" the parameter list
137  aStr = pl.get<std::string>("Preconditioner A");
138  bStr = pl.get<std::string>("Preconditioner B");
139 
140  RCP<const Teuchos::ParameterList> aSettings = invLib->getParameterList(aStr);
141  RCP<const Teuchos::ParameterList> bSettings = invLib->getParameterList(bStr);
142 
143  // build preconditioner from the parameters
144  std::string aType = aSettings->get<std::string>("Preconditioner Type");
145  RCP<Teko::PreconditionerFactory> precA
146  = Teko::BlockPreconditionerFactory::buildPreconditionerFactory(aType,aSettings->sublist("Preconditioner Settings"),invLib);
147 
148  // build preconditioner from the parameters
149  std::string bType = bSettings->get<std::string>("Preconditioner Type");
150  RCP<Teko::PreconditionerFactory> precB
151  = Teko::BlockPreconditionerFactory::buildPreconditionerFactory(bType,bSettings->sublist("Preconditioner Settings"),invLib);
152 
153  // set preconditioners
154  FirstFactory_ = Teuchos::rcp_dynamic_cast<const Teko::BlockPreconditionerFactory>(precA);
155  SecondFactory_ = Teuchos::rcp_dynamic_cast<const Teko::BlockPreconditionerFactory>(precB);
156 }
157 
158 } // 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.