Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_JacobiPreconditionerFactory.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_JacobiPreconditionerFactory.hpp"
48 
49 using Teuchos::rcp;
50 
51 namespace Teko {
52 
53 JacobiPreconditionerFactory::JacobiPreconditionerFactory(const LinearOp & invD0,const LinearOp & invD1)
54  : invOpsStrategy_(rcp(new StaticInvDiagStrategy(invD0,invD1)))
55 { }
56 
57 JacobiPreconditionerFactory::JacobiPreconditionerFactory(const RCP<const BlockInvDiagonalStrategy> & strategy)
58  : invOpsStrategy_(strategy)
59 { }
60 
64 { }
65 
67 {
68  int rows = blo->productRange()->numBlocks();
69  int cols = blo->productDomain()->numBlocks();
70 
71  TEUCHOS_ASSERT(rows==cols);
72 
73  // get diagonal blocks
74  std::vector<LinearOp> invDiag;
75  invOpsStrategy_->getInvD(blo,state,invDiag);
76  TEUCHOS_ASSERT(rows==(int) invDiag.size());
77 
78  // create a blocked linear operator
79  BlockedLinearOp precond = createBlockedOp();
80  std::stringstream ss;
81  ss << "Jacobi Preconditioner ( ";
82 
83  // start filling the blocked operator
84  beginBlockFill(precond,rows,rows); // this is assuming the matrix is square
85 
86  // build blocked diagonal matrix
87  for(int i=0;i<rows;i++) {
88  ss << " op" << i << " = " << invDiag[i]->description() << ", ";
89  precond->setBlock(i,i,invDiag[i]);
90  }
91  ss << " )";
92 
93  endBlockFill(precond);
94  // done filling the blocked operator
95 
96  // precond->setObjectLabel(ss.str());
97  precond->setObjectLabel("Jacobi");
98 
99  return precond;
100 }
101 
103 void JacobiPreconditionerFactory::initializeFromParameterList(const Teuchos::ParameterList & pl)
104 #if 0
105 {
106  RCP<const InverseLibrary> invLib = getInverseLibrary();
107 
108  // get string specifying inverse
109  std::string invStr = pl.get<std::string>("Inverse Type");
110  if(invStr=="") invStr = "Amesos";
111 
112  // based on parameter type build a strategy
113  invOpsStrategy_ = rcp(new InvFactoryDiagStrategy(invLib->getInverseFactory(invStr)));
114 }
115 #endif
116 {
117  Teko_DEBUG_SCOPE("JacobiPreconditionerFactory::initializeFromParameterList",10);
118  Teko_DEBUG_MSG_BEGIN(9);
119  DEBUG_STREAM << "Parameter list: " << std::endl;
120  pl.print(DEBUG_STREAM);
121  Teko_DEBUG_MSG_END();
122 
123  const std::string inverse_type = "Inverse Type";
124  std::vector<RCP<InverseFactory> > inverses;
125 
126  RCP<const InverseLibrary> invLib = getInverseLibrary();
127 
128  // get string specifying default inverse
129  std::string invStr ="Amesos";
130  if(pl.isParameter(inverse_type))
131  invStr = pl.get<std::string>(inverse_type);
132 
133  Teko_DEBUG_MSG("JacobiPrecFact: Building default inverse \"" << invStr << "\"",5);
134  RCP<InverseFactory> defaultInverse = invLib->getInverseFactory(invStr);
135 
136  // now check individual solvers
137  Teuchos::ParameterList::ConstIterator itr;
138  for(itr=pl.begin();itr!=pl.end();++itr) {
139  std::string fieldName = itr->first;
140  Teko_DEBUG_MSG("JacobiPrecFact: checking fieldName = \"" << fieldName << "\"",9);
141 
142  // figure out what the integer is
143  if(fieldName.compare(0,inverse_type.length(),inverse_type)==0 && fieldName!=inverse_type) {
144  int position = -1;
145  std::string inverse,type;
146 
147  // figure out position
148  std::stringstream ss(fieldName);
149  ss >> inverse >> type >> position;
150 
151  if(position<=0) {
152  Teko_DEBUG_MSG("Jacobi \"Inverse Type\" must be a (strictly) positive integer",1);
153  }
154 
155  // inserting inverse factory into vector
156  std::string invStr = pl.get<std::string>(fieldName);
157  Teko_DEBUG_MSG("JacobiPrecFact: Building inverse " << position << " \"" << invStr << "\"",5);
158  if(position>(int) inverses.size()) {
159  inverses.resize(position,defaultInverse);
160  inverses[position-1] = invLib->getInverseFactory(invStr);
161  }
162  else
163  inverses[position-1] = invLib->getInverseFactory(invStr);
164  }
165  }
166 
167  // use default inverse
168  if(inverses.size()==0)
169  inverses.push_back(defaultInverse);
170 
171  // based on parameter type build a strategy
172  invOpsStrategy_ = rcp(new InvFactoryDiagStrategy(inverses,defaultInverse));
173 }
174 
175 } // end namspace Teko
An implementation of a state object for block preconditioners.
Teuchos::RCP< const BlockInvDiagonalStrategy > invOpsStrategy_
some members
virtual void initializeFromParameterList(const Teuchos::ParameterList &pl)
Initialize from a parameter list.
LinearOp buildPreconditionerOperator(BlockedLinearOp &blo, BlockPreconditionerState &state) const
Create the Jacobi preconditioner operator.