Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_LU2x2DiagonalStrategy.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_LU2x2DiagonalStrategy.hpp"
11 
12 #include "Teuchos_TimeMonitor.hpp"
13 
14 namespace Teko {
15 
16 using Teuchos::TimeMonitor;
17 
18 Teuchos::RCP<Teuchos::Time> LU2x2DiagonalStrategy::initTimer_;
19 Teuchos::RCP<Teuchos::Time> LU2x2DiagonalStrategy::invSTimer_;
20 Teuchos::RCP<Teuchos::Time> LU2x2DiagonalStrategy::invA00Timer_;
21 Teuchos::RCP<Teuchos::Time> LU2x2DiagonalStrategy::opsTimer_;
22 
24  if (initTimer_ == Teuchos::null)
25  initTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec");
26 
27  if (invSTimer_ == Teuchos::null)
28  invSTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec invS");
29 
30  if (invA00Timer_ == Teuchos::null)
31  invA00Timer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec invA00");
32 
33  if (opsTimer_ == Teuchos::null)
34  opsTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec buildOps");
35 }
36 
37 LU2x2DiagonalStrategy::LU2x2DiagonalStrategy() : a00InverseType_(Diagonal) { buildTimers(); }
38 
40 LU2x2DiagonalStrategy::LU2x2DiagonalStrategy(const Teuchos::RCP<InverseFactory>& invFA,
41  const Teuchos::RCP<InverseFactory>& invS)
42  : invFactoryA00_(invFA), invFactoryS_(invS), a00InverseType_(Diagonal) {
43  buildTimers();
44 }
45 
47 const Teko::LinearOp LU2x2DiagonalStrategy::getHatInvA00(const Teko::BlockedLinearOp& A,
48  BlockPreconditionerState& state) const {
49  initializeState(A, state);
50 
51  return state.getModifiableOp("invA00");
52 }
53 
55 const Teko::LinearOp LU2x2DiagonalStrategy::getTildeInvA00(const Teko::BlockedLinearOp& A,
56  BlockPreconditionerState& state) const {
57  initializeState(A, state);
58 
59  return state.getModifiableOp("invA00");
60 }
61 
64 const Teko::LinearOp LU2x2DiagonalStrategy::getInvS(const Teko::BlockedLinearOp& A,
65  BlockPreconditionerState& state) const {
66  initializeState(A, state);
67 
68  return state.getModifiableOp("invS");
69 }
70 
71 void LU2x2DiagonalStrategy::initializeState(const Teko::BlockedLinearOp& A,
72  BlockPreconditionerState& state) const {
73  Teko_DEBUG_SCOPE("LU2x2DiagonalStrategy::initializeState", 10);
74 
75  // no work to be done
76  if (state.isInitialized()) return;
77 
78  Teuchos::TimeMonitor timer(*initTimer_, true);
79 
80  // extract sub blocks
81  LinearOp A00 = Teko::getBlock(0, 0, A);
82  LinearOp A01 = Teko::getBlock(0, 1, A);
83  LinearOp A10 = Teko::getBlock(1, 0, A);
84  LinearOp A11 = Teko::getBlock(1, 1, A);
85 
86  // build the Schur complement
88  ModifiableLinearOp& S = state.getModifiableOp("S");
89  {
90  Teko_DEBUG_SCOPE("Building S", 5);
91  Teuchos::TimeMonitor timerS(*opsTimer_, true);
92 
93  LinearOp diagA00 = getInvDiagonalOp(A00, a00InverseType_);
94 
95  // build Schur-complement
96  ModifiableLinearOp& triple = state.getModifiableOp("triple");
97  triple = explicitMultiply(A10, diagA00, A01, triple);
98  S = explicitAdd(scale(-1.0, A11), triple, S);
99  }
100 
101  // build inverse S
103  {
104  Teko_DEBUG_SCOPE("Building inverse(S)", 5);
105  Teuchos::TimeMonitor timerInvS(*invSTimer_, true);
106 
107  ModifiableLinearOp& invS = state.getModifiableOp("invS");
108  if (invS == Teuchos::null)
109  invS = buildInverse(*invFactoryS_, S);
110  else
111  rebuildInverse(*invFactoryS_, S, invS);
112  }
113 
114  // build inverse A00
116  {
117  Teko_DEBUG_SCOPE("Building inverse(A00)", 5);
118  Teuchos::TimeMonitor timerInvA00(*invA00Timer_, true);
119 
120  ModifiableLinearOp& invA00 = state.getModifiableOp("invA00");
121  *getOutputStream() << "(LU2x2) invA00 pointer = " << invA00 << std::endl;
122  if (invA00 == Teuchos::null)
123  invA00 = buildInverse(*invFactoryA00_, A00);
124  else
125  rebuildInverse(*invFactoryA00_, A00, invA00);
126  }
127 
128  // mark state as initialized
129  state.setInitialized(true);
130 }
131 
143 void LU2x2DiagonalStrategy::initializeFromParameterList(const Teuchos::ParameterList& pl,
144  const InverseLibrary& invLib) {
145  Teko_DEBUG_SCOPE("LU2x2DiagonalStrategy::initializeFromParameterList", 10);
146 
147  std::string invStr = "", invA00Str = "", invSStr = "";
148 #if defined(Teko_ENABLE_Amesos)
149  invStr = "Amesos";
150 #elif defined(Teko_ENABLE_Amesos2)
151  invStr = "Amesos2";
152 #endif
153 
154  // "parse" the parameter list
155  if (pl.isParameter("Inverse Type")) invStr = pl.get<std::string>("Inverse Type");
156  if (pl.isParameter("Inverse A00 Type")) invA00Str = pl.get<std::string>("Inverse A00 Type");
157  if (pl.isParameter("Inverse Schur Type")) invSStr = pl.get<std::string>("Inverse Schur Type");
158  if (pl.isParameter("Diagonal Type")) {
159  std::string massInverseStr = pl.get<std::string>("Diagonal Type");
160 
161  // build inverse types
162  a00InverseType_ = getDiagonalType(massInverseStr);
163  }
164 
165  // set defaults as needed
166  if (invA00Str == "") invA00Str = invStr;
167  if (invSStr == "") invSStr = invStr;
168 
169  Teko_DEBUG_MSG_BEGIN(5) DEBUG_STREAM << "LU2x2 Diagonal Strategy Parameters: " << std::endl;
170  DEBUG_STREAM << " inv type = \"" << invStr << "\"" << std::endl;
171  DEBUG_STREAM << " inv A00 type = \"" << invA00Str << "\"" << std::endl;
172  DEBUG_STREAM << " inv S type = \"" << invSStr << "\"" << std::endl;
173  DEBUG_STREAM << "LU2x2 Diagonal Strategy Parameter list: " << std::endl;
174  pl.print(DEBUG_STREAM);
175  Teko_DEBUG_MSG_END()
176 
177  // build velocity inverse factory
178  invFactoryA00_ = invLib.getInverseFactory(invA00Str);
179 
180  if (invA00Str == invSStr)
181  invFactoryS_ = invFactoryA00_;
182  else
183  invFactoryS_ = invLib.getInverseFactory(invSStr);
184 }
185 } // end namespace Teko
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
virtual const Teko::LinearOp getTildeInvA00(const Teko::BlockedLinearOp &A, BlockPreconditionerState &state) const
An implementation of a state object for block preconditioners.
void initializeState(const Teko::BlockedLinearOp &A, BlockPreconditionerState &state) const
virtual const Teko::LinearOp getHatInvA00(const Teko::BlockedLinearOp &A, BlockPreconditionerState &state) const
virtual const Teko::LinearOp getInvS(const Teko::BlockedLinearOp &A, BlockPreconditionerState &state) const
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.
virtual void initializeFromParameterList(const Teuchos::ParameterList &settings, const InverseLibrary &invLib)
This function builds the internals of the state from a parameter list.
virtual void setInitialized(bool init=true)
virtual Teko::ModifiableLinearOp & getModifiableOp(const std::string &name)
Add a named operator to the state object.