Thyra Version of the Day
Thyra_DefaultModelEvaluatorWithSolveFactory.hpp
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
00043 #define THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
00044 
00045 
00046 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
00047 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
00048 #include "Teuchos_Time.hpp"
00049 
00050 
00051 namespace Thyra {
00052 
00053 
00062 template<class Scalar>
00063 class DefaultModelEvaluatorWithSolveFactory
00064   : virtual public ModelEvaluatorDelegatorBase<Scalar>
00065 {
00066 public:
00067 
00070 
00072   DefaultModelEvaluatorWithSolveFactory();
00073 
00075   DefaultModelEvaluatorWithSolveFactory(
00076     const RCP<ModelEvaluator<Scalar> > &thyraModel,
00077     const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
00078     );
00079 
00081   void initialize(
00082     const RCP<ModelEvaluator<Scalar> > &thyraModel,
00083     const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
00084     );
00085 
00087   void uninitialize(
00088     RCP<ModelEvaluator<Scalar> > *thyraModel = NULL,
00089     RCP<LinearOpWithSolveFactoryBase<Scalar> > *W_factory = NULL
00090     );
00091 
00093 
00096 
00098   std::string description() const;
00099 
00101 
00104 
00106   RCP<LinearOpWithSolveBase<Scalar> > create_W() const;
00107 
00109 
00110 private:
00111 
00114 
00116   ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const;
00118   void evalModelImpl(
00119     const ModelEvaluatorBase::InArgs<Scalar> &inArgs,
00120     const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
00121     ) const;
00122 
00124 
00125 private:
00126 
00127   RCP<LinearOpWithSolveFactoryBase<Scalar> > W_factory_;
00128  
00129 };
00130 
00131 
00132 // /////////////////////////////////
00133 // Implementations
00134 
00135 
00136 // Constructors/initializers/accessors/utilities
00137 
00138 
00139 template<class Scalar>
00140 DefaultModelEvaluatorWithSolveFactory<Scalar>::DefaultModelEvaluatorWithSolveFactory()
00141 {}
00142 
00143 
00144 template<class Scalar>
00145 DefaultModelEvaluatorWithSolveFactory<Scalar>::DefaultModelEvaluatorWithSolveFactory(
00146   const RCP<ModelEvaluator<Scalar> > &thyraModel,
00147   const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
00148   )
00149 {
00150   initialize(thyraModel,W_factory);
00151 }
00152 
00153 
00154 template<class Scalar>
00155 void DefaultModelEvaluatorWithSolveFactory<Scalar>::initialize(
00156   const RCP<ModelEvaluator<Scalar> > &thyraModel,
00157   const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
00158   )
00159 {
00160   this->ModelEvaluatorDelegatorBase<Scalar>::initialize(thyraModel);
00161   W_factory_ = W_factory;
00162 }
00163 
00164 
00165 template<class Scalar>
00166 void DefaultModelEvaluatorWithSolveFactory<Scalar>::uninitialize(
00167   RCP<ModelEvaluator<Scalar> > *thyraModel,
00168   RCP<LinearOpWithSolveFactoryBase<Scalar> > *W_factory
00169   )
00170 {
00171   if(thyraModel) *thyraModel = this->getUnderlyingModel();
00172   if(W_factory) *W_factory = W_factory_;
00173   this->ModelEvaluatorDelegatorBase<Scalar>::uninitialize();
00174   W_factory_ = Teuchos::null;
00175 }
00176 
00177 
00178 // Public functions overridden from Teuchos::Describable
00179 
00180 
00181 template<class Scalar>
00182 std::string DefaultModelEvaluatorWithSolveFactory<Scalar>::description() const
00183 {
00184   const RCP<const ModelEvaluator<Scalar> >
00185     thyraModel = this->getUnderlyingModel();
00186   std::ostringstream oss;
00187   oss << "Thyra::DefaultModelEvaluatorWithSolveFactory{";
00188   oss << "thyraModel=";
00189   if(thyraModel.get())
00190     oss << "\'"<<thyraModel->description()<<"\'";
00191   else
00192     oss << "NULL";
00193   oss << ",W_factory=";
00194   if(W_factory_.get())
00195     oss << "\'"<<W_factory_->description()<<"\'";
00196   else
00197     oss << "NULL";
00198   oss << "}";
00199   return oss.str();
00200 }
00201 
00202 
00203 // Overridden from ModelEvaulator.
00204 
00205 
00206 template<class Scalar>
00207 RCP<LinearOpWithSolveBase<Scalar> >
00208 DefaultModelEvaluatorWithSolveFactory<Scalar>::create_W() const
00209 {
00210   TEUCHOS_TEST_FOR_EXCEPTION(
00211     W_factory_.get()==NULL, std::logic_error
00212     ,"Thyra::DefaultModelEvaluatorWithSolveFactory<Scalar>::create_W(): "
00213     "Error, the client did not set a LinearOpWithSolveFactoryBase object for W!"
00214     );
00215   W_factory_->setOStream(this->getOStream());
00216   W_factory_->setVerbLevel(this->getVerbLevel());
00217   return W_factory_->createOp();
00218 }
00219 
00220 
00221 // Private functions overridden from ModelEvaulatorDefaultBase.
00222 
00223 
00224 template<class Scalar>
00225 ModelEvaluatorBase::OutArgs<Scalar>
00226 DefaultModelEvaluatorWithSolveFactory<Scalar>::createOutArgsImpl() const
00227 {
00228   typedef ModelEvaluatorBase MEB;
00229   const RCP<const ModelEvaluator<Scalar> >
00230     thyraModel = this->getUnderlyingModel();
00231   const MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
00232   MEB::OutArgsSetup<Scalar> outArgs;
00233   outArgs.setModelEvalDescription(this->description());
00234   outArgs.set_Np_Ng(wrappedOutArgs.Np(),wrappedOutArgs.Ng());
00235   outArgs.setSupports(wrappedOutArgs);
00236   outArgs.setSupports(MEB::OUT_ARG_W,
00237     wrappedOutArgs.supports(MEB::OUT_ARG_W_op)&&W_factory_.get()!=NULL);
00238   return outArgs;
00239 }
00240 
00241 
00242 template<class Scalar>
00243 void DefaultModelEvaluatorWithSolveFactory<Scalar>::evalModelImpl(
00244   const ModelEvaluatorBase::InArgs<Scalar> &inArgs,
00245   const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
00246   ) const
00247 {
00248   typedef ModelEvaluatorBase MEB;
00249   using Teuchos::rcp;
00250   using Teuchos::rcp_const_cast;
00251   using Teuchos::rcp_dynamic_cast;
00252   using Teuchos::OSTab;
00253 
00254   THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
00255     "Thyra::DefaultModelEvaluatorWithSolveFactory",inArgs,outArgs
00256     );
00257 
00258   Teuchos::Time timer("");
00259 
00260   typedef Teuchos::VerboseObjectTempState<LinearOpWithSolveFactoryBase<Scalar> >
00261     VOTSLOWSF;
00262   VOTSLOWSF W_factory_outputTempState(W_factory_,out,verbLevel);
00263  
00264   // InArgs
00265 
00266   MEB::InArgs<Scalar> wrappedInArgs = thyraModel->createInArgs();
00267 
00268   wrappedInArgs.setArgs(inArgs,true);
00269 
00270   // OutArgs
00271 
00272   MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
00273 
00274   wrappedOutArgs.setArgs(outArgs,true);
00275  
00276   RCP<LinearOpWithSolveBase<Scalar> > W;
00277   RCP<LinearOpBase<Scalar> > W_op;
00278   RCP<const LinearOpBase<Scalar> > fwdW;
00279   RCP<LinearOpBase<Scalar> > nonconst_fwdW;
00280   if( outArgs.supports(MEB::OUT_ARG_W) && (W = outArgs.get_W()).get() ) {
00281     Thyra::uninitializeOp<Scalar>(*W_factory_,&*W,&fwdW);
00282     if(fwdW.get()) {
00283       nonconst_fwdW = rcp_const_cast<LinearOpBase<Scalar> >(fwdW);
00284     }
00285     else {
00286       nonconst_fwdW = thyraModel->create_W_op();
00287       fwdW = nonconst_fwdW;
00288     }
00289   }
00290   if( outArgs.supports(MEB::OUT_ARG_W_op) && (W_op = outArgs.get_W_op()).get() ) {
00291     if( W_op.get() && !nonconst_fwdW.get() )
00292       nonconst_fwdW = rcp_const_cast<LinearOpBase<Scalar> >(fwdW);
00293   }
00294   if(nonconst_fwdW.get()) {
00295     wrappedOutArgs.set_W_op(nonconst_fwdW);
00296   }
00297 
00298   // Do the evaluation
00299  
00300   if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
00301     *out << "\nEvaluating the output functions on model \'"
00302          << thyraModel->description() << "\' ...\n";
00303   timer.start(true);
00304  
00305   thyraModel->evalModel(wrappedInArgs,wrappedOutArgs);
00306  
00307   timer.stop();
00308   if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
00309     OSTab(out).o() << "\nTime to evaluate underlying model = "
00310                    << timer.totalElapsedTime()<<" sec\n";
00311 
00312   // Postprocess arguments
00313  
00314   if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
00315     *out << "\nPost processing the output objects ...\n";
00316   timer.start(true);
00317  
00318   if( W.get() ) {
00319     Thyra::initializeOp<Scalar>(*W_factory_,fwdW,&*W);
00320     W->setVerbLevel(this->getVerbLevel());
00321     W->setOStream(this->getOStream());
00322   }
00323  
00324   if( W_op.get() ) {
00325     TEUCHOS_TEST_FOR_EXCEPT(true); // Handle this case later if we need to!
00326   }
00327 
00328   timer.stop();
00329   if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
00330     OSTab(out).o() << "\nTime to process output objects = "
00331                    << timer.totalElapsedTime()<<" sec\n";
00332 
00333   THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
00334  
00335 }
00336 
00337 
00338 } // namespace Thyra
00339 
00340 
00341 #endif // THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines