Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_GaussSeidelPreconditionerFactory.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 #include "Teko_GaussSeidelPreconditionerFactory.hpp"
11 
12 #include "Teko_BlockUpperTriInverseOp.hpp"
13 #include "Teko_BlockLowerTriInverseOp.hpp"
14 
15 using Teuchos::rcp;
16 using Teuchos::RCP;
17 
18 namespace Teko {
19 
21  const LinearOp& invD0,
22  const LinearOp& invD1)
23  : invOpsStrategy_(rcp(new StaticInvDiagStrategy(invD0, invD1))), solveType_(solveType) {}
24 
26  TriSolveType solveType, const RCP<const BlockInvDiagonalStrategy>& strategy)
27  : invOpsStrategy_(strategy), solveType_(solveType) {}
28 
30  : solveType_(GS_UseLowerTriangle) {}
31 
33  BlockedLinearOp& blo, BlockPreconditionerState& state) const {
34  int rows = blockRowCount(blo);
35  int cols = blockColCount(blo);
36 
37  TEUCHOS_ASSERT(rows == cols);
38 
39  // get diagonal blocks
40  std::vector<LinearOp> invDiag;
41  invOpsStrategy_->getInvD(blo, state, invDiag);
42  TEUCHOS_ASSERT(rows == (int)invDiag.size());
43 
44  if (solveType_ == GS_UseUpperTriangle) {
45  // create a blocked linear operator
46  BlockedLinearOp U = getUpperTriBlocks(blo);
47 
48  return createBlockUpperTriInverseOp(U, invDiag, "Gauss Seidel");
49  } else if (solveType_ == GS_UseLowerTriangle) {
50  // create a blocked linear operator
51  BlockedLinearOp L = getLowerTriBlocks(blo);
52 
53  return createBlockLowerTriInverseOp(L, invDiag, "Gauss Seidel");
54  }
55 
56  TEUCHOS_ASSERT(false); // we should never make it here!
57 }
58 
61  const Teuchos::ParameterList& pl) {
62  Teko_DEBUG_SCOPE("GaussSeidelPreconditionerFactory::initializeFromParameterList", 10);
63  Teko_DEBUG_MSG_BEGIN(9);
64  DEBUG_STREAM << "Parameter list: " << std::endl;
65  pl.print(DEBUG_STREAM);
66  Teko_DEBUG_MSG_END();
67 
68  const std::string inverse_type = "Inverse Type";
69  const std::string preconditioner_type = "Preconditioner Type";
70  std::vector<RCP<InverseFactory> > inverses;
71  std::vector<RCP<InverseFactory> > preconditioners;
72 
73  RCP<const InverseLibrary> invLib = getInverseLibrary();
74 
75  // get string specifying default inverse
76  std::string invStr = "";
77 #if defined(Teko_ENABLE_Amesos)
78  invStr = "Amesos";
79 #elif defined(Teko_ENABLE_Amesos2)
80  invStr = "Amesos2";
81 #endif
82  std::string precStr = "None";
83  if (pl.isParameter(inverse_type)) invStr = pl.get<std::string>(inverse_type);
84  if (pl.isParameter(preconditioner_type)) precStr = pl.get<std::string>(preconditioner_type);
85  if (pl.isParameter("Use Upper Triangle"))
86  solveType_ = pl.get<bool>("Use Upper Triangle") ? GS_UseUpperTriangle : GS_UseLowerTriangle;
87 
88  Teko_DEBUG_MSG("GSPrecFact: Building default inverse \"" << invStr << "\"", 5);
89  RCP<InverseFactory> defaultInverse = invLib->getInverseFactory(invStr);
90  RCP<InverseFactory> defaultPrec;
91  if (precStr != "None") defaultPrec = invLib->getInverseFactory(precStr);
92 
93  // now check individual solvers
94  Teuchos::ParameterList::ConstIterator itr;
95  for (itr = pl.begin(); itr != pl.end(); ++itr) {
96  std::string fieldName = itr->first;
97  Teko_DEBUG_MSG("GSPrecFact: checking fieldName = \"" << fieldName << "\"", 9);
98 
99  // figure out what the integer is
100  if (fieldName.compare(0, inverse_type.length(), inverse_type) == 0 &&
101  fieldName != inverse_type) {
102  int position = -1;
103  std::string inverse, type;
104 
105  // figure out position
106  std::stringstream ss(fieldName);
107  ss >> inverse >> type >> position;
108 
109  if (position <= 0) {
110  Teko_DEBUG_MSG("Gauss-Seidel \"Inverse Type\" must be a (strictly) positive integer", 1);
111  }
112 
113  // inserting inverse factory into vector
114  std::string invStr2 = pl.get<std::string>(fieldName);
115  Teko_DEBUG_MSG("GSPrecFact: Building inverse " << position << " \"" << invStr2 << "\"", 5);
116  if (position > (int)inverses.size()) {
117  inverses.resize(position, defaultInverse);
118  inverses[position - 1] = invLib->getInverseFactory(invStr2);
119  } else
120  inverses[position - 1] = invLib->getInverseFactory(invStr2);
121  } else if (fieldName.compare(0, preconditioner_type.length(), preconditioner_type) == 0 &&
122  fieldName != preconditioner_type) {
123  int position = -1;
124  std::string preconditioner, type;
125 
126  // figure out position
127  std::stringstream ss(fieldName);
128  ss >> preconditioner >> type >> position;
129 
130  if (position <= 0) {
131  Teko_DEBUG_MSG("Gauss-Seidel \"Preconditioner Type\" must be a (strictly) positive integer",
132  1);
133  }
134 
135  // inserting preconditioner factory into vector
136  std::string precStr2 = pl.get<std::string>(fieldName);
137  Teko_DEBUG_MSG(
138  "GSPrecFact: Building preconditioner " << position << " \"" << precStr2 << "\"", 5);
139  if (position > (int)preconditioners.size()) {
140  preconditioners.resize(position, defaultPrec);
141  preconditioners[position - 1] = invLib->getInverseFactory(precStr2);
142  } else
143  preconditioners[position - 1] = invLib->getInverseFactory(precStr2);
144  }
145  }
146 
147  // use default inverse
148  if (inverses.size() == 0) inverses.push_back(defaultInverse);
149 
150  // based on parameter type build a strategy
152  rcp(new InvFactoryDiagStrategy(inverses, preconditioners, defaultInverse, defaultPrec));
153 }
154 
155 } // namespace Teko
An implementation of a state object for block preconditioners.
Teuchos::RCP< const BlockInvDiagonalStrategy > invOpsStrategy_
some members
LinearOp buildPreconditionerOperator(BlockedLinearOp &blo, BlockPreconditionerState &state) const
Create the Gauss-Seidel preconditioner operator.
Teuchos::RCP< const InverseLibrary > getInverseLibrary() const
Get the inverse library used by this preconditioner factory.
virtual void initializeFromParameterList(const Teuchos::ParameterList &pl)
Initialize from a parameter list.