Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_PreconditionerInverseFactory.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 /*
48 // @header
49 //
50 // ***********************************************************************
51 //
52 // teko: a package for block and physics based preconditioning
53 // copyright 2010 sandia corporation
54 //
55 // under the terms of contract de-ac04-94al85000 with sandia corporation,
56 // the u.s. government retains certain rights in this software.
57 //
58 // redistribution and use in source and binary forms, with or without
59 // modification, are permitted provided that the following conditions are
60 // met:
61 //
62 // 1. redistributions of source code must retain the above copyright
63 // notice, this list of conditions and the following disclaimer.
64 //
65 // 2. redistributions in binary form must reproduce the above copyright
66 // notice, this list of conditions and the following disclaimer in the
67 // documentation and/or other materials provided with the distribution.
68 //
69 // 3. neither the name of the corporation nor the names of the
70 // contributors may be used to endorse or promote products derived from
71 // this software without specific prior written permission.
72 //
73 // this software is provided by sandia corporation "as is" and any
74 // express or implied warranties, including, but not limited to, the
75 // implied warranties of merchantability and fitness for a particular
76 // purpose are disclaimed. in no event shall sandia corporation or the
77 // contributors be liable for any direct, indirect, incidental, special,
78 // exemplary, or consequential damages (including, but not limited to,
79 // procurement of substitute goods or services; loss of use, data, or
80 // profits; or business interruption) however caused and on any theory of
81 // liability, whether in contract, strict liability, or tort (including
82 // negligence or otherwise) arising in any way out of the use of this
83 // software, even if advised of the possibility of such damage.
84 //
85 // questions? contact eric c. cyr (eccyr@sandia.gov)
86 //
87 // ***********************************************************************
88 //
89 // @header
90 */
91 
92 #include "Teko_PreconditionerInverseFactory.hpp"
93 
94 // Thyra includes
95 #include "Thyra_DefaultLinearOpSource.hpp"
96 #include "Thyra_DefaultInverseLinearOp.hpp"
97 #include "Thyra_DefaultPreconditioner.hpp"
98 
99 // Stratimikos includes
100 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
101 
102 // Teko includes
103 #include "Teko_Utilities.hpp"
104 #include "Teko_BlockPreconditionerFactory.hpp"
105 #include "Teko_Preconditioner.hpp"
106 #include "Teko_PreconditionerLinearOp.hpp"
107 #include "Teko_SolveInverseFactory.hpp"
108 
109 using Teuchos::rcp;
110 using Teuchos::RCP;
111 using Teuchos::rcp_const_cast;
112 using Teuchos::rcp_dynamic_cast;
113 
114 namespace Teko {
115 
125 PreconditionerInverseFactory::PreconditionerInverseFactory(
126  const Teuchos::RCP<Thyra::PreconditionerFactoryBase<double> >& precFactory,
127  const Teuchos::RCP<Teko::RequestHandler>& rh)
128  : precFactory_(precFactory) {
129  setRequestHandler(rh);
130 }
131 
146 PreconditionerInverseFactory::PreconditionerInverseFactory(
147  const Teuchos::RCP<Thyra::PreconditionerFactoryBase<double> >& precFactory,
148  const Teuchos::RCP<const Teuchos::ParameterList>& xtraParam,
149  const Teuchos::RCP<Teko::RequestHandler>& rh)
150  : precFactory_(precFactory) {
151  if (xtraParam != Teuchos::null)
152  extraParams_ = rcp(new Teuchos::ParameterList(*xtraParam));
153  else
154  extraParams_ = Teuchos::null; // make it explicit
155 
156  setRequestHandler(rh);
157 }
158 
160 PreconditionerInverseFactory::PreconditionerInverseFactory(
161  const PreconditionerInverseFactory& pFactory)
162  : precFactory_(pFactory.precFactory_) {
163  setRequestHandler(pFactory.getRequestHandler());
164 }
165 
175 InverseLinearOp PreconditionerInverseFactory::buildInverse(const LinearOp& linearOp) const {
176  RCP<Thyra::PreconditionerBase<double> > prec = precFactory_->createPrec();
177  precFactory_->initializePrec(Thyra::defaultLinearOpSource(linearOp), &*prec);
178 
179  RCP<Teko::PreconditionerLinearOp<double> > precOp =
181 
182  return precOp;
183 }
184 
197 InverseLinearOp PreconditionerInverseFactory::buildInverse(
198  const LinearOp& linearOp, const PreconditionerState& parentState) const {
199  Teko_DEBUG_SCOPE("PreconditionerInverseFactory::buildInverse(A,parentState)", 10);
200  RCP<Thyra::PreconditionerBase<double> > prec = precFactory_->createPrec();
201 
202  {
203  Teko_DEBUG_SCOPE("Casting to Teko::Preconditioner", 10);
204  // pass state downward if a Teko::Preconditioner object is begin used
205  RCP<Teko::Preconditioner> tekoPrec = Teuchos::rcp_dynamic_cast<Teko::Preconditioner>(prec);
206  if (tekoPrec != Teuchos::null) {
207  Teko_DEBUG_SCOPE("Merging states", 10);
208  tekoPrec->mergeStateObject(parentState);
209  }
210  }
211 
212  precFactory_->initializePrec(Thyra::defaultLinearOpSource(linearOp), &*prec);
213 
214  RCP<Teko::PreconditionerLinearOp<double> > precOp =
216 
217  return precOp;
218 }
219 
231 void PreconditionerInverseFactory::rebuildInverse(const LinearOp& source,
232  InverseLinearOp& dest) const {
233  Teko_DEBUG_MSG("BEGIN PreconditionerInverseFactory::rebuildInverse", 10);
234 
235  RCP<Thyra::PreconditionerBase<double> > prec =
236  Teuchos::rcp_dynamic_cast<Teko::PreconditionerLinearOp<double> >(dest)
237  ->getNonconstPreconditioner();
238 
239  precFactory_->initializePrec(Thyra::defaultLinearOpSource(source), &*prec);
240 
241  Teko_DEBUG_MSG("END PreconditionerInverseFactory::rebuildInverse", 10);
242 }
243 
252 Teuchos::RCP<const Teuchos::ParameterList> PreconditionerInverseFactory::getParameterList() const {
253  return precFactory_->getParameterList();
254 }
255 
270 Teuchos::RCP<Teuchos::ParameterList> PreconditionerInverseFactory::getRequestedParameters() const {
271  Teuchos::RCP<BlockPreconditionerFactory> bpf =
272  rcp_dynamic_cast<BlockPreconditionerFactory>(precFactory_);
273 
274  // request the parameters from a BPF is required
275  if (bpf != Teuchos::null) return bpf->getRequestedParameters();
276 
277  // for non block preconditioners see if there are user requested additional parameters
278  return extraParams_;
279 }
280 
294 bool PreconditionerInverseFactory::updateRequestedParameters(const Teuchos::ParameterList& pl) {
295  Teuchos::RCP<BlockPreconditionerFactory> bpf =
296  rcp_dynamic_cast<BlockPreconditionerFactory>(precFactory_);
297 
298  // update the parameters of a BPF is required
299  if (bpf != Teuchos::null) return bpf->updateRequestedParameters(pl);
300 
301  // for non block preconditioners see if there are user requested additional parameters
302  if (extraParams_ == Teuchos::null) return true;
303 
304  Teuchos::ParameterList::ConstIterator itr;
305  RCP<Teuchos::ParameterList> srcPl = precFactory_->unsetParameterList();
306 
307  // find name of settings sublist
308  std::string subName = "";
309  for (itr = srcPl->begin(); itr != srcPl->end(); ++itr) {
310  // search for std::string with "Settings" in name
311  if (itr->first.find("Settings") != std::string::npos) {
312  subName = itr->first;
313  continue;
314  }
315  }
316 
317  // update fails if no settings list was found
318  if (subName == "") {
319  precFactory_->setParameterList(srcPl);
320  return false;
321  }
322 
323  // add extra parameters to list
324  Teuchos::ParameterList& settingsList = srcPl->sublist(subName);
325  for (itr = pl.begin(); itr != pl.end(); ++itr) {
326  if (extraParams_->isParameter(itr->first)) settingsList.setEntry(itr->first, itr->second);
327  }
328 
329  // set the parameter list
330  precFactory_->setParameterList(srcPl);
331 
332  return true;
333 }
334 
335 void PreconditionerInverseFactory::setupParameterListFromRequestHandler() {
336  // for non block preconditioners see if there are user requested additional parameters
337  if (extraParams_ == Teuchos::null) return;
338 
339  Teuchos::ParameterList::ConstIterator itr;
340  RCP<Teuchos::ParameterList> srcPl = precFactory_->unsetParameterList();
341 
342  // find name of settings sublist
343  std::string subName = "";
344  for (itr = srcPl->begin(); itr != srcPl->end(); ++itr) {
345  // search for std::string with "Settings" in name
346  if (itr->first.find("Settings") != std::string::npos) {
347  subName = itr->first;
348  continue;
349  }
350  }
351 
352  // update fails if no settings list was found
353  /*
354  if(subName=="") {
355  precFactory_->setParameterList(srcPl);
356  return;
357  }*/
358 
359  Teuchos::RCP<Teko::RequestHandler> rh = getRequestHandler();
360  TEUCHOS_TEST_FOR_EXCEPTION(
361  rh == Teuchos::null, std::runtime_error,
362  "PreconditionerInverseFactory::setupParameterListFromRequestHandler: no request handler set");
363 
364  // add extra parameters to list
365  rh->preRequest<Teuchos::RCP<Teuchos::ParameterList> >(RequestMesg(extraParams_));
366  Teuchos::RCP<Teuchos::ParameterList> requestParams =
367  rh->request<Teuchos::RCP<Teuchos::ParameterList> >(RequestMesg(extraParams_));
368 
369  TEUCHOS_TEST_FOR_EXCEPTION(requestParams == Teuchos::null, std::runtime_error,
370  "User specified request not satisfied!");
371 
372  // If there is no Settings sublist, assume that the list itself contains the settings
373  if (subName == "") {
374  for (itr = requestParams->begin(); itr != requestParams->end(); ++itr)
375  srcPl->setEntry(itr->first, itr->second);
376  } else {
377  Teuchos::ParameterList& settingsList = srcPl->sublist(subName);
378  for (itr = requestParams->begin(); itr != requestParams->end(); ++itr)
379  settingsList.setEntry(itr->first, itr->second);
380  }
381 
382  // reset with updated parameter list
383  precFactory_->setParameterList(srcPl);
384 }
385 
386 } // namespace Teko
void setRequestHandler(const Teuchos::RCP< RequestHandler > &rh)
Set the request handler with pointers to the appropriate callbacks.
Class that wraps a PreconditionerBase object it makes it behave like a linear operator.
An extension of the Thyra::DefaultPreconditioner class with some specializations useful for use withi...