Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_LU2x2DiagonalStrategy.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_LU2x2DiagonalStrategy.hpp"
48 
49 #include "Teuchos_TimeMonitor.hpp"
50 
51 namespace Teko {
52 
53 using Teuchos::TimeMonitor;
54 
55 Teuchos::RCP<Teuchos::Time> LU2x2DiagonalStrategy::initTimer_;
56 Teuchos::RCP<Teuchos::Time> LU2x2DiagonalStrategy::invSTimer_;
57 Teuchos::RCP<Teuchos::Time> LU2x2DiagonalStrategy::invA00Timer_;
58 Teuchos::RCP<Teuchos::Time> LU2x2DiagonalStrategy::opsTimer_;
59 
61  if (initTimer_ == Teuchos::null)
62  initTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec");
63 
64  if (invSTimer_ == Teuchos::null)
65  invSTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec invS");
66 
67  if (invA00Timer_ == Teuchos::null)
68  invA00Timer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec invA00");
69 
70  if (opsTimer_ == Teuchos::null)
71  opsTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec buildOps");
72 }
73 
74 LU2x2DiagonalStrategy::LU2x2DiagonalStrategy() : a00InverseType_(Diagonal) { buildTimers(); }
75 
77 LU2x2DiagonalStrategy::LU2x2DiagonalStrategy(const Teuchos::RCP<InverseFactory>& invFA,
78  const Teuchos::RCP<InverseFactory>& invS)
79  : invFactoryA00_(invFA), invFactoryS_(invS), a00InverseType_(Diagonal) {
80  buildTimers();
81 }
82 
84 const Teko::LinearOp LU2x2DiagonalStrategy::getHatInvA00(const Teko::BlockedLinearOp& A,
85  BlockPreconditionerState& state) const {
86  initializeState(A, state);
87 
88  return state.getModifiableOp("invA00");
89 }
90 
92 const Teko::LinearOp LU2x2DiagonalStrategy::getTildeInvA00(const Teko::BlockedLinearOp& A,
93  BlockPreconditionerState& state) const {
94  initializeState(A, state);
95 
96  return state.getModifiableOp("invA00");
97 }
98 
101 const Teko::LinearOp LU2x2DiagonalStrategy::getInvS(const Teko::BlockedLinearOp& A,
102  BlockPreconditionerState& state) const {
103  initializeState(A, state);
104 
105  return state.getModifiableOp("invS");
106 }
107 
108 void LU2x2DiagonalStrategy::initializeState(const Teko::BlockedLinearOp& A,
109  BlockPreconditionerState& state) const {
110  Teko_DEBUG_SCOPE("LU2x2DiagonalStrategy::initializeState", 10);
111 
112  // no work to be done
113  if (state.isInitialized()) return;
114 
115  Teuchos::TimeMonitor timer(*initTimer_, true);
116 
117  // extract sub blocks
118  LinearOp A00 = Teko::getBlock(0, 0, A);
119  LinearOp A01 = Teko::getBlock(0, 1, A);
120  LinearOp A10 = Teko::getBlock(1, 0, A);
121  LinearOp A11 = Teko::getBlock(1, 1, A);
122 
123  // build the Schur complement
125  ModifiableLinearOp& S = state.getModifiableOp("S");
126  {
127  Teko_DEBUG_SCOPE("Building S", 5);
128  Teuchos::TimeMonitor timerS(*opsTimer_, true);
129 
130  LinearOp diagA00 = getInvDiagonalOp(A00, a00InverseType_);
131 
132  // build Schur-complement
133  ModifiableLinearOp& triple = state.getModifiableOp("triple");
134  triple = explicitMultiply(A10, diagA00, A01, triple);
135  S = explicitAdd(scale(-1.0, A11), triple, S);
136  }
137 
138  // build inverse S
140  {
141  Teko_DEBUG_SCOPE("Building inverse(S)", 5);
142  Teuchos::TimeMonitor timerInvS(*invSTimer_, true);
143 
144  ModifiableLinearOp& invS = state.getModifiableOp("invS");
145  if (invS == Teuchos::null)
146  invS = buildInverse(*invFactoryS_, S);
147  else
148  rebuildInverse(*invFactoryS_, S, invS);
149  }
150 
151  // build inverse A00
153  {
154  Teko_DEBUG_SCOPE("Building inverse(A00)", 5);
155  Teuchos::TimeMonitor timerInvA00(*invA00Timer_, true);
156 
157  ModifiableLinearOp& invA00 = state.getModifiableOp("invA00");
158  *getOutputStream() << "(LU2x2) invA00 pointer = " << invA00 << std::endl;
159  if (invA00 == Teuchos::null)
160  invA00 = buildInverse(*invFactoryA00_, A00);
161  else
162  rebuildInverse(*invFactoryA00_, A00, invA00);
163  }
164 
165  // mark state as initialized
166  state.setInitialized(true);
167 }
168 
180 void LU2x2DiagonalStrategy::initializeFromParameterList(const Teuchos::ParameterList& pl,
181  const InverseLibrary& invLib) {
182  Teko_DEBUG_SCOPE("LU2x2DiagonalStrategy::initializeFromParameterList", 10);
183 
184  std::string invStr = "", invA00Str = "", invSStr = "";
185 #if defined(Teko_ENABLE_Amesos)
186  invStr = "Amesos";
187 #elif defined(Teko_ENABLE_Amesos2)
188  invStr = "Amesos2";
189 #endif
190 
191  // "parse" the parameter list
192  if (pl.isParameter("Inverse Type")) invStr = pl.get<std::string>("Inverse Type");
193  if (pl.isParameter("Inverse A00 Type")) invA00Str = pl.get<std::string>("Inverse A00 Type");
194  if (pl.isParameter("Inverse Schur Type")) invSStr = pl.get<std::string>("Inverse Schur Type");
195  if (pl.isParameter("Diagonal Type")) {
196  std::string massInverseStr = pl.get<std::string>("Diagonal Type");
197 
198  // build inverse types
199  a00InverseType_ = getDiagonalType(massInverseStr);
200  }
201 
202  // set defaults as needed
203  if (invA00Str == "") invA00Str = invStr;
204  if (invSStr == "") invSStr = invStr;
205 
206  Teko_DEBUG_MSG_BEGIN(5) DEBUG_STREAM << "LU2x2 Diagonal Strategy Parameters: " << std::endl;
207  DEBUG_STREAM << " inv type = \"" << invStr << "\"" << std::endl;
208  DEBUG_STREAM << " inv A00 type = \"" << invA00Str << "\"" << std::endl;
209  DEBUG_STREAM << " inv S type = \"" << invSStr << "\"" << std::endl;
210  DEBUG_STREAM << "LU2x2 Diagonal Strategy Parameter list: " << std::endl;
211  pl.print(DEBUG_STREAM);
212  Teko_DEBUG_MSG_END()
213 
214  // build velocity inverse factory
215  invFactoryA00_ = invLib.getInverseFactory(invA00Str);
216 
217  if (invA00Str == invSStr)
218  invFactoryS_ = invFactoryA00_;
219  else
220  invFactoryS_ = invLib.getInverseFactory(invSStr);
221 }
222 } // 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.