Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_AddPreconditionerFactory.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_AddPreconditionerFactory.hpp"
48 
49 namespace Teko {
50 
51 using Teuchos::RCP;
52 
53 AddPreconditionerFactory::AddPreconditionerFactory(
54  const RCP<const BlockPreconditionerFactory> & FirstFactory,
55  const RCP<const BlockPreconditionerFactory> & SecondFactory)
56  : FirstFactory_(FirstFactory), SecondFactory_(SecondFactory)
57 {}
58 
59 AddPreconditionerFactory::AddPreconditionerFactory()
60 {}
61 
63 RCP<PreconditionerState> AddPreconditionerFactory::buildPreconditionerState() const
64 {
65  AddPrecondState* mystate = new AddPrecondState();
66  mystate->StateOne_ = Teuchos::rcp_dynamic_cast<BlockPreconditionerState>(FirstFactory_->buildPreconditionerState());
67  mystate->StateTwo_ = Teuchos::rcp_dynamic_cast<BlockPreconditionerState>(SecondFactory_->buildPreconditionerState());
68  return rcp(mystate);
69 }
70 
71 // Use the factory to build the preconditioner (this is where the work goes)
72 LinearOp AddPreconditionerFactory
73  ::buildPreconditionerOperator(BlockedLinearOp & blockOp,
74  BlockPreconditionerState & state) const
75 {
76  // The main tricky thing here is that we have to take the 'state' object
77  // associated with AddPreconditionerFactory(), pull out the states for
78  // the individual preconditioners, and pass these on to
79  // buildPreconditionerOperator() for each subpreconditioner.
80 
81  AddPrecondState *MyState = dynamic_cast<AddPrecondState *> (&state);
82  TEUCHOS_ASSERT(MyState != 0);
83 
84  LinearOp M1 = FirstFactory_->buildPreconditionerOperator(blockOp, *MyState->StateOne_);
85  LinearOp M2 = SecondFactory_->buildPreconditionerOperator(blockOp, *MyState->StateTwo_);
86 
87  LinearOp invA = add(M1, M2);
88 
89  // return fully constructed preconditioner
90  return invA;
91 }
92 
94 void AddPreconditionerFactory::initializeFromParameterList(const Teuchos::ParameterList & pl)
95 {
96  RCP<const InverseLibrary> invLib = getInverseLibrary();
97 
98  // get string specifying inverse
99  std::string aStr="", bStr="";
100 
101  // "parse" the parameter list
102  aStr = pl.get<std::string>("Preconditioner A");
103  bStr = pl.get<std::string>("Preconditioner B");
104 
105  RCP<const Teuchos::ParameterList> aSettings = invLib->getParameterList(aStr);
106  RCP<const Teuchos::ParameterList> bSettings = invLib->getParameterList(bStr);
107 
108  // build preconditioner from the parameters
109  std::string aType = aSettings->get<std::string>("Preconditioner Type");
110  RCP<Teko::PreconditionerFactory> precA
111  = Teko::PreconditionerFactory::buildPreconditionerFactory(aType,aSettings->sublist("Preconditioner Settings"),invLib);
112 
113  // build preconditioner from the parameters
114  std::string bType = bSettings->get<std::string>("Preconditioner Type");
115  RCP<Teko::PreconditionerFactory> precB
116  = Teko::PreconditionerFactory::buildPreconditionerFactory(bType,bSettings->sublist("Preconditioner Settings"),invLib);
117 
118  // set preconditioners
119  FirstFactory_ = Teuchos::rcp_dynamic_cast<const Teko::BlockPreconditionerFactory>(precA);
120  SecondFactory_ = Teuchos::rcp_dynamic_cast<const Teko::BlockPreconditionerFactory>(precB);
121 }
122 
123 } // end namespace Teko
Abstract class which block preconditioner factories in Teko should be based on.
An implementation of a state object for block preconditioners.
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).