|
Thyra Package Browser (Single Doxygen Collection) Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Thyra: Interfaces and Support for Abstract Numerical Algorithms 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #ifndef THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP 00030 #define THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP 00031 00032 00033 #include "Thyra_ModelEvaluatorDelegatorBase.hpp" 00034 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" 00035 #include "Teuchos_Time.hpp" 00036 00037 00038 namespace Thyra { 00039 00040 00049 template<class Scalar> 00050 class DefaultModelEvaluatorWithSolveFactory 00051 : virtual public ModelEvaluatorDelegatorBase<Scalar> 00052 { 00053 public: 00054 00057 00059 DefaultModelEvaluatorWithSolveFactory(); 00060 00062 DefaultModelEvaluatorWithSolveFactory( 00063 const RCP<ModelEvaluator<Scalar> > &thyraModel, 00064 const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory 00065 ); 00066 00068 void initialize( 00069 const RCP<ModelEvaluator<Scalar> > &thyraModel, 00070 const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory 00071 ); 00072 00074 void uninitialize( 00075 RCP<ModelEvaluator<Scalar> > *thyraModel = NULL, 00076 RCP<LinearOpWithSolveFactoryBase<Scalar> > *W_factory = NULL 00077 ); 00078 00080 00083 00085 std::string description() const; 00086 00088 00091 00093 RCP<LinearOpWithSolveBase<Scalar> > create_W() const; 00094 00096 00097 private: 00098 00101 00103 ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const; 00105 void evalModelImpl( 00106 const ModelEvaluatorBase::InArgs<Scalar> &inArgs, 00107 const ModelEvaluatorBase::OutArgs<Scalar> &outArgs 00108 ) const; 00109 00111 00112 private: 00113 00114 RCP<LinearOpWithSolveFactoryBase<Scalar> > W_factory_; 00115 00116 }; 00117 00118 00119 // ///////////////////////////////// 00120 // Implementations 00121 00122 00123 // Constructors/initializers/accessors/utilities 00124 00125 00126 template<class Scalar> 00127 DefaultModelEvaluatorWithSolveFactory<Scalar>::DefaultModelEvaluatorWithSolveFactory() 00128 {} 00129 00130 00131 template<class Scalar> 00132 DefaultModelEvaluatorWithSolveFactory<Scalar>::DefaultModelEvaluatorWithSolveFactory( 00133 const RCP<ModelEvaluator<Scalar> > &thyraModel, 00134 const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory 00135 ) 00136 { 00137 initialize(thyraModel,W_factory); 00138 } 00139 00140 00141 template<class Scalar> 00142 void DefaultModelEvaluatorWithSolveFactory<Scalar>::initialize( 00143 const RCP<ModelEvaluator<Scalar> > &thyraModel, 00144 const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory 00145 ) 00146 { 00147 this->ModelEvaluatorDelegatorBase<Scalar>::initialize(thyraModel); 00148 W_factory_ = W_factory; 00149 } 00150 00151 00152 template<class Scalar> 00153 void DefaultModelEvaluatorWithSolveFactory<Scalar>::uninitialize( 00154 RCP<ModelEvaluator<Scalar> > *thyraModel, 00155 RCP<LinearOpWithSolveFactoryBase<Scalar> > *W_factory 00156 ) 00157 { 00158 if(thyraModel) *thyraModel = this->getUnderlyingModel(); 00159 if(W_factory) *W_factory = W_factory_; 00160 this->ModelEvaluatorDelegatorBase<Scalar>::uninitialize(); 00161 W_factory_ = Teuchos::null; 00162 } 00163 00164 00165 // Public functions overridden from Teuchos::Describable 00166 00167 00168 template<class Scalar> 00169 std::string DefaultModelEvaluatorWithSolveFactory<Scalar>::description() const 00170 { 00171 const RCP<const ModelEvaluator<Scalar> > 00172 thyraModel = this->getUnderlyingModel(); 00173 std::ostringstream oss; 00174 oss << "Thyra::DefaultModelEvaluatorWithSolveFactory{"; 00175 oss << "thyraModel="; 00176 if(thyraModel.get()) 00177 oss << "\'"<<thyraModel->description()<<"\'"; 00178 else 00179 oss << "NULL"; 00180 oss << ",W_factory="; 00181 if(W_factory_.get()) 00182 oss << "\'"<<W_factory_->description()<<"\'"; 00183 else 00184 oss << "NULL"; 00185 oss << "}"; 00186 return oss.str(); 00187 } 00188 00189 00190 // Overridden from ModelEvaulator. 00191 00192 00193 template<class Scalar> 00194 RCP<LinearOpWithSolveBase<Scalar> > 00195 DefaultModelEvaluatorWithSolveFactory<Scalar>::create_W() const 00196 { 00197 TEST_FOR_EXCEPTION( 00198 W_factory_.get()==NULL, std::logic_error 00199 ,"Thyra::DefaultModelEvaluatorWithSolveFactory<Scalar>::create_W(): " 00200 "Error, the client did not set a LinearOpWithSolveFactoryBase object for W!" 00201 ); 00202 W_factory_->setOStream(this->getOStream()); 00203 W_factory_->setVerbLevel(this->getVerbLevel()); 00204 return W_factory_->createOp(); 00205 } 00206 00207 00208 // Private functions overridden from ModelEvaulatorDefaultBase. 00209 00210 00211 template<class Scalar> 00212 ModelEvaluatorBase::OutArgs<Scalar> 00213 DefaultModelEvaluatorWithSolveFactory<Scalar>::createOutArgsImpl() const 00214 { 00215 typedef ModelEvaluatorBase MEB; 00216 const RCP<const ModelEvaluator<Scalar> > 00217 thyraModel = this->getUnderlyingModel(); 00218 const MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs(); 00219 MEB::OutArgsSetup<Scalar> outArgs; 00220 outArgs.setModelEvalDescription(this->description()); 00221 outArgs.set_Np_Ng(wrappedOutArgs.Np(),wrappedOutArgs.Ng()); 00222 outArgs.setSupports(wrappedOutArgs); 00223 outArgs.setSupports(MEB::OUT_ARG_W, 00224 wrappedOutArgs.supports(MEB::OUT_ARG_W_op)&&W_factory_.get()!=NULL); 00225 return outArgs; 00226 } 00227 00228 00229 template<class Scalar> 00230 void DefaultModelEvaluatorWithSolveFactory<Scalar>::evalModelImpl( 00231 const ModelEvaluatorBase::InArgs<Scalar> &inArgs, 00232 const ModelEvaluatorBase::OutArgs<Scalar> &outArgs 00233 ) const 00234 { 00235 typedef ModelEvaluatorBase MEB; 00236 using Teuchos::rcp; 00237 using Teuchos::rcp_const_cast; 00238 using Teuchos::rcp_dynamic_cast; 00239 using Teuchos::OSTab; 00240 00241 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN( 00242 "Thyra::DefaultModelEvaluatorWithSolveFactory",inArgs,outArgs 00243 ); 00244 00245 Teuchos::Time timer(""); 00246 00247 typedef Teuchos::VerboseObjectTempState<LinearOpWithSolveFactoryBase<Scalar> > 00248 VOTSLOWSF; 00249 VOTSLOWSF W_factory_outputTempState(W_factory_,out,verbLevel); 00250 00251 // InArgs 00252 00253 MEB::InArgs<Scalar> wrappedInArgs = thyraModel->createInArgs(); 00254 00255 wrappedInArgs.setArgs(inArgs,true); 00256 00257 // OutArgs 00258 00259 MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs(); 00260 00261 wrappedOutArgs.setArgs(outArgs,true); 00262 00263 RCP<LinearOpWithSolveBase<Scalar> > W; 00264 RCP<LinearOpBase<Scalar> > W_op; 00265 RCP<const LinearOpBase<Scalar> > fwdW; 00266 RCP<LinearOpBase<Scalar> > nonconst_fwdW; 00267 if( outArgs.supports(MEB::OUT_ARG_W) && (W = outArgs.get_W()).get() ) { 00268 Thyra::uninitializeOp<Scalar>(*W_factory_,&*W,&fwdW); 00269 if(fwdW.get()) { 00270 nonconst_fwdW = rcp_const_cast<LinearOpBase<Scalar> >(fwdW); 00271 } 00272 else { 00273 nonconst_fwdW = thyraModel->create_W_op(); 00274 fwdW = nonconst_fwdW; 00275 } 00276 } 00277 if( outArgs.supports(MEB::OUT_ARG_W_op) && (W_op = outArgs.get_W_op()).get() ) { 00278 if( W_op.get() && !nonconst_fwdW.get() ) 00279 nonconst_fwdW = rcp_const_cast<LinearOpBase<Scalar> >(fwdW); 00280 } 00281 if(nonconst_fwdW.get()) { 00282 wrappedOutArgs.set_W_op(nonconst_fwdW); 00283 } 00284 00285 // Do the evaluation 00286 00287 if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW)) 00288 *out << "\nEvaluating the output functions on model \'" 00289 << thyraModel->description() << "\' ...\n"; 00290 timer.start(true); 00291 00292 thyraModel->evalModel(wrappedInArgs,wrappedOutArgs); 00293 00294 timer.stop(); 00295 if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW)) 00296 OSTab(out).o() << "\nTime to evaluate underlying model = " 00297 << timer.totalElapsedTime()<<" sec\n"; 00298 00299 // Postprocess arguments 00300 00301 if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW)) 00302 *out << "\nPost processing the output objects ...\n"; 00303 timer.start(true); 00304 00305 if( W.get() ) { 00306 Thyra::initializeOp<Scalar>(*W_factory_,fwdW,&*W); 00307 W->setVerbLevel(this->getVerbLevel()); 00308 W->setOStream(this->getOStream()); 00309 } 00310 00311 if( W_op.get() ) { 00312 TEST_FOR_EXCEPT(true); // Handle this case later if we need to! 00313 } 00314 00315 timer.stop(); 00316 if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW)) 00317 OSTab(out).o() << "\nTime to process output objects = " 00318 << timer.totalElapsedTime()<<" sec\n"; 00319 00320 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END(); 00321 00322 } 00323 00324 00325 } // namespace Thyra 00326 00327 00328 #endif // THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
1.7.4