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 
54  const LinearOp& invD1)
55  : invOpsStrategy_(rcp(new StaticInvDiagStrategy(invD0, invD1))) {}
56 
58  const RCP<const BlockInvDiagonalStrategy>& strategy)
59  : invOpsStrategy_(strategy) {}
60 
64 
66  BlockedLinearOp& blo, BlockPreconditionerState& state) const {
67  int rows = blo->productRange()->numBlocks();
68  int cols = blo->productDomain()->numBlocks();
69 
70  TEUCHOS_ASSERT(rows == cols);
71 
72  // get diagonal blocks
73  std::vector<LinearOp> invDiag;
74  invOpsStrategy_->getInvD(blo, state, invDiag);
75  TEUCHOS_ASSERT(rows == (int)invDiag.size());
76 
77  // create a blocked linear operator
78  BlockedLinearOp precond = createBlockedOp();
79  std::stringstream ss;
80  ss << "Jacobi Preconditioner ( ";
81 
82  // start filling the blocked operator
83  beginBlockFill(precond, rows, rows); // this is assuming the matrix is square
84 
85  // build blocked diagonal matrix
86  for (int i = 0; i < rows; i++) {
87  ss << " op" << i << " = " << invDiag[i]->description() << ", ";
88  precond->setBlock(i, i, invDiag[i]);
89  }
90  ss << " )";
91 
92  endBlockFill(precond);
93  // done filling the blocked operator
94 
95  // precond->setObjectLabel(ss.str());
96  precond->setObjectLabel("Jacobi");
97 
98  return precond;
99 }
100 
102 void JacobiPreconditionerFactory::initializeFromParameterList(const Teuchos::ParameterList& pl)
103 #if 0
104 {
105  RCP<const InverseLibrary> invLib = getInverseLibrary();
106 
107  // get string specifying inverse
108  std::string invStr = pl.get<std::string>("Inverse Type");
109 #if defined(Teko_ENABLE_Amesos)
110  if(invStr=="") invStr = "Amesos";
111 #elif defined(Teko_ENABLE_Amesos2)
112  if(invStr=="") invStr= "Amesos2";
113 #endif
114 
115  // based on parameter type build a strategy
116  invOpsStrategy_ = rcp(new InvFactoryDiagStrategy(invLib->getInverseFactory(invStr)));
117 }
118 #endif
119 {
120  Teko_DEBUG_SCOPE("JacobiPreconditionerFactory::initializeFromParameterList", 10);
121  Teko_DEBUG_MSG_BEGIN(9);
122  DEBUG_STREAM << "Parameter list: " << std::endl;
123  pl.print(DEBUG_STREAM);
124  Teko_DEBUG_MSG_END();
125 
126  const std::string inverse_type = "Inverse Type";
127  const std::string preconditioner_type = "Preconditioner Type";
128  std::vector<RCP<InverseFactory> > inverses;
129  std::vector<RCP<InverseFactory> > preconditioners;
130 
131  RCP<const InverseLibrary> invLib = getInverseLibrary();
132 
133  // get string specifying default inverse
134  std::string invStr = "";
135 #if defined(Teko_ENABLE_Amesos)
136  invStr = "Amesos";
137 #elif defined(Teko_ENABLE_Amesos2)
138  invStr = "Amesos2";
139 #endif
140 
141  std::string precStr = "None";
142  if (pl.isParameter(inverse_type)) invStr = pl.get<std::string>(inverse_type);
143  RCP<InverseFactory> defaultPrec;
144  if (precStr != "None") defaultPrec = invLib->getInverseFactory(precStr);
145 
146  Teko_DEBUG_MSG("JacobiPrecFact: Building default inverse \"" << invStr << "\"", 5);
147  RCP<InverseFactory> defaultInverse = invLib->getInverseFactory(invStr);
148 
149  // now check individual solvers
150  Teuchos::ParameterList::ConstIterator itr;
151  for (itr = pl.begin(); itr != pl.end(); ++itr) {
152  std::string fieldName = itr->first;
153  Teko_DEBUG_MSG("JacobiPrecFact: checking fieldName = \"" << fieldName << "\"", 9);
154 
155  // figure out what the integer is
156  if (fieldName.compare(0, inverse_type.length(), inverse_type) == 0 &&
157  fieldName != inverse_type) {
158  int position = -1;
159  std::string inverse, type;
160 
161  // figure out position
162  std::stringstream ss(fieldName);
163  ss >> inverse >> type >> position;
164 
165  if (position <= 0) {
166  Teko_DEBUG_MSG("Jacobi \"Inverse Type\" must be a (strictly) positive integer", 1);
167  }
168 
169  // inserting inverse factory into vector
170  std::string invStr2 = pl.get<std::string>(fieldName);
171  Teko_DEBUG_MSG("JacobiPrecFact: Building inverse " << position << " \"" << invStr2 << "\"",
172  5);
173  if (position > (int)inverses.size()) {
174  inverses.resize(position, defaultInverse);
175  inverses[position - 1] = invLib->getInverseFactory(invStr2);
176  } else
177  inverses[position - 1] = invLib->getInverseFactory(invStr2);
178  } else if (fieldName.compare(0, preconditioner_type.length(), preconditioner_type) == 0 &&
179  fieldName != preconditioner_type) {
180  int position = -1;
181  std::string preconditioner, type;
182 
183  // figure out position
184  std::stringstream ss(fieldName);
185  ss >> preconditioner >> type >> position;
186 
187  if (position <= 0) {
188  Teko_DEBUG_MSG("Jacobi \"Preconditioner Type\" must be a (strictly) positive integer", 1);
189  }
190 
191  // inserting preconditioner factory into vector
192  std::string precStr2 = pl.get<std::string>(fieldName);
193  Teko_DEBUG_MSG(
194  "JacobiPrecFact: Building preconditioner " << position << " \"" << precStr2 << "\"", 5);
195  if (position > (int)preconditioners.size()) {
196  preconditioners.resize(position, defaultPrec);
197  preconditioners[position - 1] = invLib->getInverseFactory(precStr2);
198  } else
199  preconditioners[position - 1] = invLib->getInverseFactory(precStr2);
200  }
201  }
202 
203  // use default inverse
204  if (inverses.size() == 0) inverses.push_back(defaultInverse);
205 
206  // based on parameter type build a strategy
207  invOpsStrategy_ =
208  rcp(new InvFactoryDiagStrategy(inverses, preconditioners, defaultInverse, defaultPrec));
209 }
210 
211 } // namespace 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.