Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_IterativePreconditionerFactory.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 // Teko includes
11 #include "Teko_IterativePreconditionerFactory.hpp"
12 
13 #include "Teko_PreconditionerInverseFactory.hpp"
14 
15 namespace Teko {
16 
19  : correctionNum_(0), precFactory_(Teuchos::null) {}
20 
25  unsigned int correctionNum, const Teuchos::RCP<Teko::InverseFactory>& precFactory)
26  : correctionNum_(correctionNum), precFactory_(precFactory) {}
27 
32  unsigned int correctionNum, const Teuchos::RCP<Teko::PreconditionerFactory>& precFactory)
33  : correctionNum_(correctionNum) {
34  precFactory_ = Teuchos::rcp(
35  new Teko::PreconditionerInverseFactory(precFactory, precFactory->getRequestHandler()));
36 }
37 
42  LinearOp& lo, PreconditionerState& state) const {
43  TEUCHOS_TEST_FOR_EXCEPTION(
44  precFactory_ == Teuchos::null, std::runtime_error,
45  "ERROR: Teko::IterativePreconditionerFactory::buildPreconditionerOperator requires that a "
46  << "preconditioner factory has been set. Currently it is null!");
47 
48  // build user specified preconditioner
49  ModifiableLinearOp& invP = state.getModifiableOp("prec");
50  if (invP == Teuchos::null)
51  invP = Teko::buildInverse(*precFactory_, lo);
52  else
53  Teko::rebuildInverse(*precFactory_, lo, invP);
54 
55  // // no repititions are required
56  // if(correctionNum_==0) return invP;
57 
58  // now build correction operator
59  LinearOp I = Thyra::identity(lo->range(), "I");
60  LinearOp AiP = multiply(lo, invP.getConst(), "AiP");
61  LinearOp correction = add(I, scale(-1.0, AiP)); // I - A * iPA
62 
63  LinearOp resMap = I; // will map r_n to r_{n+1}
64  for (unsigned int i = 0; i < correctionNum_; i++)
65  resMap = add(I, multiply(resMap, correction)); // resMap = I + resMap*(I-A*iP)
66 
67  // iP = (I-A*iP)^{correctionNum}
68  return multiply(invP.getConst(), resMap);
69 }
70 
75  const Teuchos::ParameterList& settings) {
76  correctionNum_ = 1;
77  if (settings.isParameter("Iteration Count"))
78  correctionNum_ = settings.get<int>("Iteration Count");
79 
80  TEUCHOS_TEST_FOR_EXCEPTION(
81  not settings.isParameter("Preconditioner Type"), std::runtime_error,
82  "Parameter \"Preconditioner Type\" is required by a Teko::IterativePreconditionerFactory");
83 
84  // grab library and preconditioner name
85  Teuchos::RCP<const InverseLibrary> il = getInverseLibrary();
86  std::string precName = settings.get<std::string>("Preconditioner Type");
87 
88  // build preconditioner factory
89  precFactory_ = il->getInverseFactory(precName);
90  TEUCHOS_TEST_FOR_EXCEPTION(
91  precFactory_ == Teuchos::null, std::runtime_error,
92  "ERROR: \"Preconditioner Type\" = " << precName << " could not be found");
93 }
94 
98 Teuchos::RCP<Teuchos::ParameterList> IterativePreconditionerFactory::getRequestedParameters()
99  const {
100  TEUCHOS_TEST_FOR_EXCEPTION(
101  precFactory_ == Teuchos::null, std::runtime_error,
102  "ERROR: Teko::IterativePreconditionerFactory::getRequestedParameters requires that a "
103  << "preconditioner factory has been set. Currently it is null!");
104 
105  return precFactory_->getRequestedParameters();
106 }
107 
110 bool IterativePreconditionerFactory::updateRequestedParameters(const Teuchos::ParameterList& pl) {
111  TEUCHOS_TEST_FOR_EXCEPTION(
112  precFactory_ == Teuchos::null, std::runtime_error,
113  "ERROR: Teko::IterativePreconditionerFactory::updateRequestedParameters requires that a "
114  << "preconditioner factory has been set. Currently it is null!");
115 
116  return precFactory_->updateRequestedParameters(pl);
117 }
118 
119 } // 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.