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