Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_IterativePreconditionerFactory.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 // Teko includes
48 #include "Teko_IterativePreconditionerFactory.hpp"
49 
50 #include "Teko_PreconditionerInverseFactory.hpp"
51 
52 namespace Teko {
53 
56  : correctionNum_(0), precFactory_(Teuchos::null) {}
57 
62  unsigned int correctionNum, const Teuchos::RCP<Teko::InverseFactory>& precFactory)
63  : correctionNum_(correctionNum), precFactory_(precFactory) {}
64 
69  unsigned int correctionNum, const Teuchos::RCP<Teko::PreconditionerFactory>& precFactory)
70  : correctionNum_(correctionNum) {
71  precFactory_ = Teuchos::rcp(
72  new Teko::PreconditionerInverseFactory(precFactory, precFactory->getRequestHandler()));
73 }
74 
79  LinearOp& lo, PreconditionerState& state) const {
80  TEUCHOS_TEST_FOR_EXCEPTION(
81  precFactory_ == Teuchos::null, std::runtime_error,
82  "ERROR: Teko::IterativePreconditionerFactory::buildPreconditionerOperator requires that a "
83  << "preconditioner factory has been set. Currently it is null!");
84 
85  // build user specified preconditioner
86  ModifiableLinearOp& invP = state.getModifiableOp("prec");
87  if (invP == Teuchos::null)
88  invP = Teko::buildInverse(*precFactory_, lo);
89  else
90  Teko::rebuildInverse(*precFactory_, lo, invP);
91 
92  // // no repititions are required
93  // if(correctionNum_==0) return invP;
94 
95  // now build correction operator
96  LinearOp I = Thyra::identity(lo->range(), "I");
97  LinearOp AiP = multiply(lo, invP.getConst(), "AiP");
98  LinearOp correction = add(I, scale(-1.0, AiP)); // I - A * iPA
99 
100  LinearOp resMap = I; // will map r_n to r_{n+1}
101  for (unsigned int i = 0; i < correctionNum_; i++)
102  resMap = add(I, multiply(resMap, correction)); // resMap = I + resMap*(I-A*iP)
103 
104  // iP = (I-A*iP)^{correctionNum}
105  return multiply(invP.getConst(), resMap);
106 }
107 
112  const Teuchos::ParameterList& settings) {
113  correctionNum_ = 1;
114  if (settings.isParameter("Iteration Count"))
115  correctionNum_ = settings.get<int>("Iteration Count");
116 
117  TEUCHOS_TEST_FOR_EXCEPTION(
118  not settings.isParameter("Preconditioner Type"), std::runtime_error,
119  "Parameter \"Preconditioner Type\" is required by a Teko::IterativePreconditionerFactory");
120 
121  // grab library and preconditioner name
122  Teuchos::RCP<const InverseLibrary> il = getInverseLibrary();
123  std::string precName = settings.get<std::string>("Preconditioner Type");
124 
125  // build preconditioner factory
126  precFactory_ = il->getInverseFactory(precName);
127  TEUCHOS_TEST_FOR_EXCEPTION(
128  precFactory_ == Teuchos::null, std::runtime_error,
129  "ERROR: \"Preconditioner Type\" = " << precName << " could not be found");
130 }
131 
135 Teuchos::RCP<Teuchos::ParameterList> IterativePreconditionerFactory::getRequestedParameters()
136  const {
137  TEUCHOS_TEST_FOR_EXCEPTION(
138  precFactory_ == Teuchos::null, std::runtime_error,
139  "ERROR: Teko::IterativePreconditionerFactory::getRequestedParameters requires that a "
140  << "preconditioner factory has been set. Currently it is null!");
141 
142  return precFactory_->getRequestedParameters();
143 }
144 
147 bool IterativePreconditionerFactory::updateRequestedParameters(const Teuchos::ParameterList& pl) {
148  TEUCHOS_TEST_FOR_EXCEPTION(
149  precFactory_ == Teuchos::null, std::runtime_error,
150  "ERROR: Teko::IterativePreconditionerFactory::updateRequestedParameters requires that a "
151  << "preconditioner factory has been set. Currently it is null!");
152 
153  return precFactory_->updateRequestedParameters(pl);
154 }
155 
156 } // end namespace Teko
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
IterativePreconditionerFactory()
Default constructor, for use with the AutoClone class.
virtual Teuchos::RCP< Teuchos::ParameterList > getRequestedParameters() const
Request the additional parameters this preconditioner factory needs.
virtual void initializeFromParameterList(const Teuchos::ParameterList &settings)
This function builds the internals of the preconditioner factory from a parameter list...
virtual LinearOp buildPreconditionerOperator(LinearOp &lo, PreconditionerState &state) const
Function that is called to build the preconditioner for the linear operator that is passed in...
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.
Teuchos::RCP< const InverseLibrary > getInverseLibrary() const
Get the inverse library used by this preconditioner factory.
An implementation of a state object preconditioners.
virtual bool updateRequestedParameters(const Teuchos::ParameterList &pl)
Update this object with the fields from a parameter list.
virtual Teko::ModifiableLinearOp & getModifiableOp(const std::string &name)
Add a named operator to the state object.