00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 #include "Teko_PreconditionerInverseFactory.hpp"
00093
00094
00095 #include "Thyra_DefaultLinearOpSource.hpp"
00096 #include "Thyra_DefaultInverseLinearOp.hpp"
00097 #include "Thyra_DefaultPreconditioner.hpp"
00098
00099
00100 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
00101
00102
00103 #include "Teko_Utilities.hpp"
00104 #include "Teko_BlockPreconditionerFactory.hpp"
00105 #include "Teko_Preconditioner.hpp"
00106 #include "Teko_PreconditionerLinearOp.hpp"
00107 #include "Teko_SolveInverseFactory.hpp"
00108
00109 using Teuchos::rcp;
00110 using Teuchos::rcp_const_cast;
00111 using Teuchos::rcp_dynamic_cast;
00112 using Teuchos::RCP;
00113
00114 namespace Teko {
00115
00125 PreconditionerInverseFactory::PreconditionerInverseFactory(
00126 const Teuchos::RCP<Thyra::PreconditionerFactoryBase<double> > & precFactory,
00127 const Teuchos::RCP<Teko::RequestHandler> & rh)
00128 : precFactory_(precFactory)
00129 {
00130 setRequestHandler(rh);
00131 }
00132
00147 PreconditionerInverseFactory::PreconditionerInverseFactory(
00148 const Teuchos::RCP<Thyra::PreconditionerFactoryBase<double> > & precFactory,
00149 const Teuchos::RCP<const Teuchos::ParameterList> & xtraParam,
00150 const Teuchos::RCP<Teko::RequestHandler> & rh)
00151 : precFactory_(precFactory)
00152 {
00153 if(xtraParam!=Teuchos::null)
00154 extraParams_ = rcp(new Teuchos::ParameterList(*xtraParam));
00155 else
00156 extraParams_ = Teuchos::null;
00157
00158 setRequestHandler(rh);
00159 }
00160
00162 PreconditionerInverseFactory::PreconditionerInverseFactory(const PreconditionerInverseFactory & pFactory)
00163 : precFactory_(pFactory.precFactory_)
00164 {
00165 setRequestHandler(pFactory.getRequestHandler());
00166 }
00167
00177 InverseLinearOp PreconditionerInverseFactory::buildInverse(const LinearOp & linearOp) const
00178 {
00179 RCP<Thyra::PreconditionerBase<double> > prec = precFactory_->createPrec();
00180 precFactory_->initializePrec(Thyra::defaultLinearOpSource(linearOp),&*prec);
00181
00182 RCP<Teko::PreconditionerLinearOp<double> > precOp
00183 = rcp(new Teko::PreconditionerLinearOp<double>(prec));
00184
00185 return precOp;
00186 }
00187
00200 InverseLinearOp PreconditionerInverseFactory::buildInverse(const LinearOp & linearOp, const PreconditionerState & parentState) const
00201 {
00202 Teko_DEBUG_SCOPE("PreconditionerInverseFactory::buildInverse(A,parentState)",10);
00203 RCP<Thyra::PreconditionerBase<double> > prec = precFactory_->createPrec();
00204
00205 {
00206 Teko_DEBUG_SCOPE("Casting to Teko::Preconditioner",10);
00207
00208 RCP<Teko::Preconditioner> tekoPrec = Teuchos::rcp_dynamic_cast<Teko::Preconditioner>(prec);
00209 if(tekoPrec!=Teuchos::null) {
00210 Teko_DEBUG_SCOPE("Merging states",10);
00211 tekoPrec->mergeStateObject(parentState);
00212 }
00213 }
00214
00215 precFactory_->initializePrec(Thyra::defaultLinearOpSource(linearOp),&*prec);
00216
00217 RCP<Teko::PreconditionerLinearOp<double> > precOp
00218 = rcp(new Teko::PreconditionerLinearOp<double>(prec));
00219
00220 return precOp;
00221 }
00222
00234 void PreconditionerInverseFactory::rebuildInverse(const LinearOp & source,InverseLinearOp & dest) const
00235 {
00236 Teko_DEBUG_MSG("BEGIN PreconditionerInverseFactory::rebuildInverse",10);
00237
00238 RCP<Thyra::PreconditionerBase<double> > prec
00239 = Teuchos::rcp_dynamic_cast<Teko::PreconditionerLinearOp<double> >(dest)->getNonconstPreconditioner();
00240
00241 precFactory_->initializePrec(Thyra::defaultLinearOpSource(source),&*prec);
00242
00243 Teko_DEBUG_MSG("END PreconditionerInverseFactory::rebuildInverse",10);
00244 }
00245
00254 Teuchos::RCP<const Teuchos::ParameterList> PreconditionerInverseFactory::getParameterList() const
00255 {
00256 return precFactory_->getParameterList();
00257 }
00258
00273 Teuchos::RCP<Teuchos::ParameterList> PreconditionerInverseFactory::getRequestedParameters() const
00274 {
00275 Teuchos::RCP<BlockPreconditionerFactory> bpf = rcp_dynamic_cast<BlockPreconditionerFactory>(precFactory_);
00276
00277
00278 if(bpf!=Teuchos::null)
00279 return bpf->getRequestedParameters();
00280
00281
00282 return extraParams_;
00283 }
00284
00298 bool PreconditionerInverseFactory::updateRequestedParameters(const Teuchos::ParameterList & pl)
00299 {
00300 Teuchos::RCP<BlockPreconditionerFactory> bpf = rcp_dynamic_cast<BlockPreconditionerFactory>(precFactory_);
00301
00302
00303 if(bpf!=Teuchos::null)
00304 return bpf->updateRequestedParameters(pl);
00305
00306
00307 if(extraParams_==Teuchos::null)
00308 return true;
00309
00310 Teuchos::ParameterList::ConstIterator itr;
00311 RCP<Teuchos::ParameterList> srcPl = precFactory_->unsetParameterList();
00312
00313
00314 std::string subName = "";
00315 for(itr=srcPl->begin();itr!=srcPl->end();++itr) {
00316
00317 if(itr->first.find("Settings")!=std::string::npos) {
00318 subName = itr->first;
00319 continue;
00320 }
00321 }
00322
00323
00324 if(subName=="") {
00325 precFactory_->setParameterList(srcPl);
00326 return false;
00327 }
00328
00329
00330 Teuchos::ParameterList & settingsList = srcPl->sublist(subName);
00331 for(itr=pl.begin();itr!=pl.end();++itr) {
00332 if(extraParams_->isParameter(itr->first))
00333 settingsList.setEntry(itr->first,itr->second);
00334 }
00335
00336
00337 precFactory_->setParameterList(srcPl);
00338
00339 return true;
00340 }
00341
00342 void PreconditionerInverseFactory::setupParameterListFromRequestHandler()
00343 {
00344
00345 if(extraParams_==Teuchos::null) return;
00346
00347 Teuchos::ParameterList::ConstIterator itr;
00348 RCP<Teuchos::ParameterList> srcPl = precFactory_->unsetParameterList();
00349
00350
00351 std::string subName = "";
00352 for(itr=srcPl->begin();itr!=srcPl->end();++itr) {
00353
00354 if(itr->first.find("Settings")!=std::string::npos) {
00355 subName = itr->first;
00356 continue;
00357 }
00358 }
00359
00360
00361 if(subName=="") {
00362 precFactory_->setParameterList(srcPl);
00363 return;
00364 }
00365
00366 Teuchos::RCP<Teko::RequestHandler> rh = getRequestHandler();
00367 TEUCHOS_TEST_FOR_EXCEPTION(rh==Teuchos::null,std::runtime_error,
00368 "PreconditionerInverseFactory::setupParameterListFromRequestHandler: no request handler set");
00369
00370
00371 Teuchos::ParameterList & settingsList = srcPl->sublist(subName);
00372 rh->preRequest<Teuchos::RCP<Teuchos::ParameterList> >(RequestMesg(extraParams_));
00373 Teuchos::RCP<Teuchos::ParameterList> requestParams =
00374 rh->request<Teuchos::RCP<Teuchos::ParameterList> >(RequestMesg(extraParams_));
00375
00376 TEUCHOS_TEST_FOR_EXCEPTION(requestParams==Teuchos::null,std::runtime_error,"User specified request not satisfied!");
00377 for(itr=requestParams->begin();itr!=requestParams->end();++itr)
00378 settingsList.setEntry(itr->first,itr->second);
00379
00380
00381 precFactory_->setParameterList(srcPl);
00382 }
00383
00384 }