Thyra_DefaultModelEvaluatorWithSolveFactory.hpp

Go to the documentation of this file.
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 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
00033 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
00034 #include "Teuchos_Time.hpp"
00035 
00036 namespace Thyra {
00037 
00044 template<class Scalar>
00045 class DefaultModelEvaluatorWithSolveFactory : virtual public ModelEvaluatorDelegatorBase<Scalar> {
00046 public:
00047 
00050 
00052   DefaultModelEvaluatorWithSolveFactory();
00053 
00055   DefaultModelEvaluatorWithSolveFactory(
00056     const Teuchos::RefCountPtr<ModelEvaluator<Scalar> >                 &thyraModel
00057     ,const Teuchos::RefCountPtr<LinearOpWithSolveFactoryBase<Scalar> >  &W_factory
00058     );
00059 
00061   void initialize(
00062     const Teuchos::RefCountPtr<ModelEvaluator<Scalar> >                 &thyraModel
00063     ,const Teuchos::RefCountPtr<LinearOpWithSolveFactoryBase<Scalar> >  &W_factory
00064     );
00065 
00067   void uninitialize(
00068     Teuchos::RefCountPtr<ModelEvaluator<Scalar> >                 *thyraModel = NULL
00069     ,Teuchos::RefCountPtr<LinearOpWithSolveFactoryBase<Scalar> >  *W_factory   = NULL
00070     );
00071 
00073 
00076 
00078   Teuchos::RefCountPtr<LinearOpWithSolveBase<Scalar> > create_W() const;
00080   ModelEvaluatorBase::OutArgs<Scalar> createOutArgs() const;
00082   void evalModel(
00083     const ModelEvaluatorBase::InArgs<Scalar>    &inArgs
00084     ,const ModelEvaluatorBase::OutArgs<Scalar>  &outArgs
00085     ) const;
00086 
00088 
00091 
00093   std::string description() const;
00094 
00096 
00097 private:
00098 
00099   Teuchos::RefCountPtr<LinearOpWithSolveFactoryBase<Scalar> >   W_factory_;
00100   
00101 };
00102 
00103 // /////////////////////////////////
00104 // Implementations
00105 
00106 // Constructors/initializers/accessors/utilities
00107 
00108 template<class Scalar>
00109 DefaultModelEvaluatorWithSolveFactory<Scalar>::DefaultModelEvaluatorWithSolveFactory()
00110 {}
00111 
00112 template<class Scalar>
00113 DefaultModelEvaluatorWithSolveFactory<Scalar>::DefaultModelEvaluatorWithSolveFactory(
00114   const Teuchos::RefCountPtr<ModelEvaluator<Scalar> >                 &thyraModel
00115   ,const Teuchos::RefCountPtr<LinearOpWithSolveFactoryBase<Scalar> >  &W_factory
00116   )
00117 {
00118   initialize(thyraModel,W_factory);
00119 }
00120 
00121 template<class Scalar>
00122 void DefaultModelEvaluatorWithSolveFactory<Scalar>::initialize(
00123   const Teuchos::RefCountPtr<ModelEvaluator<Scalar> >                 &thyraModel
00124   ,const Teuchos::RefCountPtr<LinearOpWithSolveFactoryBase<Scalar> >  &W_factory
00125   )
00126 {
00127   this->ModelEvaluatorDelegatorBase<Scalar>::initialize(thyraModel);
00128   W_factory_ = W_factory;
00129 }
00130 
00131 template<class Scalar>
00132 void DefaultModelEvaluatorWithSolveFactory<Scalar>::uninitialize(
00133   Teuchos::RefCountPtr<ModelEvaluator<Scalar> >                 *thyraModel
00134   ,Teuchos::RefCountPtr<LinearOpWithSolveFactoryBase<Scalar> >  *W_factory
00135   )
00136 {
00137   if(thyraModel) *thyraModel = this->getUnderlyingModel();
00138   if(W_factory) *W_factory = W_factory_;
00139   this->ModelEvaluatorDelegatorBase<Scalar>::uninitialize();
00140   W_factory_ = Teuchos::null;
00141 }
00142 
00143 // Overridden from ModelEvaulator.
00144 
00145 template<class Scalar>
00146 Teuchos::RefCountPtr<LinearOpWithSolveBase<Scalar> >
00147 DefaultModelEvaluatorWithSolveFactory<Scalar>::create_W() const
00148 {
00149   TEST_FOR_EXCEPTION(
00150     W_factory_.get()==NULL, std::logic_error
00151     ,"Thyra::DefaultModelEvaluatorWithSolveFactory<Scalar>::create_W(): "
00152     "Error, the client did not set a LinearOpWithSolveFactoryBase object for W!"
00153     );
00154   W_factory_->setOStream(this->getOStream());
00155   W_factory_->setVerbLevel(this->getVerbLevel());
00156   return W_factory_->createOp();
00157 }
00158 
00159 template<class Scalar>
00160 ModelEvaluatorBase::OutArgs<Scalar>
00161 DefaultModelEvaluatorWithSolveFactory<Scalar>::createOutArgs() const
00162 {
00163   typedef ModelEvaluatorBase MEB;
00164   const Teuchos::RefCountPtr<const ModelEvaluator<Scalar> >
00165     thyraModel = this->getUnderlyingModel();
00166   const MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
00167   MEB::OutArgsSetup<Scalar> outArgs;
00168   outArgs.setModelEvalDescription(this->description());
00169   outArgs.set_Np_Ng(wrappedOutArgs.Np(),wrappedOutArgs.Ng());
00170   outArgs.setSupports(wrappedOutArgs);
00171   outArgs.setSupports(MEB::OUT_ARG_W,wrappedOutArgs.supports(MEB::OUT_ARG_W_op)&&W_factory_.get()!=NULL);
00172   return outArgs;
00173 }
00174 
00175 template<class Scalar>
00176 void DefaultModelEvaluatorWithSolveFactory<Scalar>::evalModel(
00177   const ModelEvaluatorBase::InArgs<Scalar>     &inArgs
00178   ,const ModelEvaluatorBase::OutArgs<Scalar>   &outArgs
00179   ) const
00180 {
00181   typedef ModelEvaluatorBase MEB;
00182   using Teuchos::RefCountPtr;
00183   using Teuchos::rcp;
00184   using Teuchos::rcp_const_cast;
00185   using Teuchos::rcp_dynamic_cast;
00186   using Teuchos::OSTab;
00187 
00188   Teuchos::Time totalTimer(""), timer("");
00189   totalTimer.start(true);
00190 
00191   const Teuchos::RefCountPtr<Teuchos::FancyOStream> out       = this->getOStream();
00192   const Teuchos::EVerbosityLevel                    verbLevel = this->getVerbLevel();
00193   Teuchos::OSTab tab(out);
00194   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00195     *out << "\nEntering Thyra::DefaultModelEvaluatorWithSolveFactory<Scalar>::evalModel(...) ...\n";
00196 
00197   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_EXTREME))
00198     *out
00199       << "\ninArgs =\n" << Teuchos::describe(inArgs,verbLevel)
00200       << "\noutArgs on input =\n" << Teuchos::describe(outArgs,Teuchos::VERB_LOW);
00201   
00202   const Teuchos::RefCountPtr<const ModelEvaluator<Scalar> >
00203     thyraModel = this->getUnderlyingModel();
00204 
00205   typedef Teuchos::VerboseObjectTempState<ModelEvaluatorBase> VOTSME;
00206   VOTSME thyraModel_outputTempState(thyraModel,out,verbLevel);
00207 
00208   typedef Teuchos::VerboseObjectTempState<LinearOpWithSolveFactoryBase<Scalar> > VOTSLOWSF;
00209   VOTSLOWSF W_factory_outputTempState(W_factory_,out,verbLevel);
00210   
00211   // InArgs
00212 
00213   MEB::InArgs<Scalar> wrappedInArgs = thyraModel->createInArgs();
00214 
00215   wrappedInArgs.setArgs(inArgs,true);
00216 
00217   // OutArgs
00218 
00219   MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
00220 
00221   wrappedOutArgs.setArgs(outArgs,true);
00222   
00223   RefCountPtr<LinearOpWithSolveBase<Scalar> > W;
00224   RefCountPtr<LinearOpBase<Scalar> >          W_op;
00225   RefCountPtr<const LinearOpBase<Scalar> >    fwdW;
00226   RefCountPtr<LinearOpBase<Scalar> >          nonconst_fwdW;
00227   if( outArgs.supports(MEB::OUT_ARG_W) && (W = outArgs.get_W()).get() ) {
00228     Thyra::uninitializeOp<Scalar>(*W_factory_,&*W,&fwdW);
00229     if(fwdW.get()) {
00230       nonconst_fwdW = rcp_const_cast<LinearOpBase<Scalar> >(fwdW);
00231     }
00232     else {
00233       nonconst_fwdW = thyraModel->create_W_op();
00234       fwdW = nonconst_fwdW;
00235     }
00236   }
00237   if( outArgs.supports(MEB::OUT_ARG_W_op) && (W_op = outArgs.get_W_op()).get() ) {
00238     if( W_op.get() && !nonconst_fwdW.get() )
00239       nonconst_fwdW = rcp_const_cast<LinearOpBase<Scalar> >(fwdW);
00240   }
00241   if(nonconst_fwdW.get()) {
00242     wrappedOutArgs.set_W_op(nonconst_fwdW);
00243   }
00244 
00245   // Do the evaluation
00246   
00247   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00248     *out << "\nEvaluating the output functions on model \'" << thyraModel->description() << "\'  ...\n";
00249   timer.start(true);
00250   
00251   thyraModel->evalModel(wrappedInArgs,wrappedOutArgs);
00252   
00253   timer.stop();
00254   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00255     *OSTab(out).getOStream() << "\nTime to evaluate underlying model = "<<timer.totalElapsedTime()<<" sec\n";
00256 
00257   // Postprocess arguments
00258   
00259   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00260     *out << "\nPost processing the output objects ...\n";
00261   timer.start(true);
00262   
00263   if( W.get() ) {
00264     Thyra::initializeOp<Scalar>(*W_factory_,fwdW,&*W);
00265     W->setVerbLevel(this->getVerbLevel());
00266     W->setOStream(this->getOStream());
00267   }
00268   
00269   if( W_op.get() ) {
00270     TEST_FOR_EXCEPT(true); // Handle this case later if we need to!
00271   }
00272 
00273   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_EXTREME))
00274     *out
00275       << "\noutArgs on output =\n" << Teuchos::describe(outArgs,verbLevel);
00276 
00277   timer.stop();
00278   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00279     *OSTab(out).getOStream() << "\nTime to process output objects = "<<timer.totalElapsedTime()<<" sec\n";
00280 
00281   totalTimer.stop();
00282   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00283     *out
00284       << "\nTotal evaluation time = "<<totalTimer.totalElapsedTime()<<" sec\n"
00285       << "\nLeaving Thyra::DefaultModelEvaluatorWithSolveFactory<Scalar>::evalModel(...) ...\n";
00286   
00287 }
00288 
00289 // Public functions overridden from Teuchos::Describable
00290 
00291 template<class Scalar>
00292 std::string DefaultModelEvaluatorWithSolveFactory<Scalar>::description() const
00293 {
00294   const Teuchos::RefCountPtr<const ModelEvaluator<Scalar> >
00295     thyraModel = this->getUnderlyingModel();
00296   std::ostringstream oss;
00297   oss << "Thyra::DefaultModelEvaluatorWithSolveFactory{";
00298   oss << "thyraModel=";
00299   if(thyraModel.get())
00300     oss << "\'"<<thyraModel->description()<<"\'";
00301   else
00302     oss << "NULL";
00303   oss << ",W_factory=";
00304   if(W_factory_.get())
00305     oss << "\'"<<W_factory_->description()<<"\'";
00306   else
00307     oss << "NULL";
00308   oss << "}";
00309   return oss.str();
00310 }
00311 
00312 } // namespace Thyra
00313 
00314 #endif // THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP

Generated on Thu Sep 18 12:33:02 2008 for Thyra Package Browser (Single Doxygen Collection) by doxygen 1.3.9.1