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 {
62  if(initTimer_==Teuchos::null)
63  initTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec");
64 
65  if(invSTimer_==Teuchos::null)
66  invSTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec invS");
67 
68  if(invA00Timer_==Teuchos::null)
69  invA00Timer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec invA00");
70 
71  if(opsTimer_==Teuchos::null)
72  opsTimer_ = TimeMonitor::getNewTimer("LU2x2DiagonalStrategy::initializePrec buildOps");
73 }
74 
76  : a00InverseType_(Diagonal)
77 {
78  buildTimers();
79 }
80 
82 LU2x2DiagonalStrategy::LU2x2DiagonalStrategy(const Teuchos::RCP<InverseFactory> & invFA,
83  const Teuchos::RCP<InverseFactory> & invS)
84  : invFactoryA00_(invFA), invFactoryS_(invS), a00InverseType_(Diagonal)
85 {
86  buildTimers();
87 }
88 
90 const Teko::LinearOp
91 LU2x2DiagonalStrategy::getHatInvA00(const Teko::BlockedLinearOp & A,BlockPreconditionerState & state) const
92 {
93  initializeState(A,state);
94 
95  return state.getModifiableOp("invA00");
96 }
97 
99 const Teko::LinearOp
100 LU2x2DiagonalStrategy::getTildeInvA00(const Teko::BlockedLinearOp & A,BlockPreconditionerState & state) const
101 {
102  initializeState(A,state);
103 
104  return state.getModifiableOp("invA00");
105 }
106 
108 const Teko::LinearOp
109 LU2x2DiagonalStrategy::getInvS(const Teko::BlockedLinearOp & A,BlockPreconditionerState & state) const
110 {
111  initializeState(A,state);
112 
113  return state.getModifiableOp("invS");
114 }
115 
116 void LU2x2DiagonalStrategy::initializeState(const Teko::BlockedLinearOp & A,BlockPreconditionerState & state) const
117 {
118  Teko_DEBUG_SCOPE("LU2x2DiagonalStrategy::initializeState",10);
119 
120  // no work to be done
121  if(state.isInitialized())
122  return;
123 
124  Teuchos::TimeMonitor timer(*initTimer_,true);
125 
126  // extract sub blocks
127  LinearOp A00 = Teko::getBlock(0,0,A);
128  LinearOp A01 = Teko::getBlock(0,1,A);
129  LinearOp A10 = Teko::getBlock(1,0,A);
130  LinearOp A11 = Teko::getBlock(1,1,A);
131 
132  // build the Schur complement
134  ModifiableLinearOp & S = state.getModifiableOp("S");
135  {
136  Teko_DEBUG_SCOPE("Building S",5);
137  Teuchos::TimeMonitor timer(*opsTimer_,true);
138 
139  LinearOp diagA00 = getInvDiagonalOp(A00,a00InverseType_);
140 
141  // build Schur-complement
142  ModifiableLinearOp & triple = state.getModifiableOp("triple");
143  triple = explicitMultiply(A10,diagA00,A01,triple);
144  S = explicitAdd(scale(-1.0,A11),triple,S);
145  }
146 
147  // build inverse S
149  {
150  Teko_DEBUG_SCOPE("Building inverse(S)",5);
151  Teuchos::TimeMonitor timer(*invSTimer_,true);
152 
153  ModifiableLinearOp & invS = state.getModifiableOp("invS");
154  if(invS==Teuchos::null)
155  invS = buildInverse(*invFactoryS_,S);
156  else
157  rebuildInverse(*invFactoryS_,S,invS);
158  }
159 
160  // build inverse A00
162  {
163  Teko_DEBUG_SCOPE("Building inverse(A00)",5);
164  Teuchos::TimeMonitor timer(*invA00Timer_,true);
165 
166  ModifiableLinearOp & invA00 = state.getModifiableOp("invA00");
167  *getOutputStream() << "(LU2x2) invA00 pointer = " << invA00 << std::endl;
168  if(invA00==Teuchos::null)
169  invA00 = buildInverse(*invFactoryA00_,A00);
170  else
171  rebuildInverse(*invFactoryA00_,A00,invA00);
172  }
173 
174  // mark state as initialized
175  state.setInitialized(true);
176 }
177 
189 void LU2x2DiagonalStrategy::initializeFromParameterList(const Teuchos::ParameterList & pl,
190  const InverseLibrary & invLib)
191 {
192  Teko_DEBUG_SCOPE("LU2x2DiagonalStrategy::initializeFromParameterList",10);
193 
194  std::string invStr="Amesos", invA00Str="", invSStr="";
195 
196  // "parse" the parameter list
197  if(pl.isParameter("Inverse Type"))
198  invStr = pl.get<std::string>("Inverse Type");
199  if(pl.isParameter("Inverse A00 Type"))
200  invA00Str = pl.get<std::string>("Inverse A00 Type");
201  if(pl.isParameter("Inverse Schur Type"))
202  invSStr = pl.get<std::string>("Inverse Schur Type");
203  if(pl.isParameter("Diagonal Type")) {
204  std::string massInverseStr = pl.get<std::string>("Diagonal Type");
205 
206  // build inverse types
207  a00InverseType_ = getDiagonalType(massInverseStr);
208  }
209 
210  // set defaults as needed
211  if(invA00Str=="") invA00Str = invStr;
212  if(invSStr=="") invSStr = invStr;
213 
214  Teko_DEBUG_MSG_BEGIN(5)
215  DEBUG_STREAM << "LU2x2 Diagonal Strategy Parameters: " << std::endl;
216  DEBUG_STREAM << " inv type = \"" << invStr << "\"" << std::endl;
217  DEBUG_STREAM << " inv A00 type = \"" << invA00Str << "\"" << std::endl;
218  DEBUG_STREAM << " inv S type = \"" << invSStr << "\"" << std::endl;
219  DEBUG_STREAM << "LU2x2 Diagonal Strategy Parameter list: " << std::endl;
220  pl.print(DEBUG_STREAM);
221  Teko_DEBUG_MSG_END()
222 
223  // build velocity inverse factory
224  invFactoryA00_ = invLib.getInverseFactory(invA00Str);
225 
226  if(invA00Str==invSStr)
227  invFactoryS_ = invFactoryA00_;
228  else
229  invFactoryS_ = invLib.getInverseFactory(invSStr);
230 }
231 } // 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.