Rythmos_StepperAsModelEvaluator.hpp

00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //                           Rythmos Package
00005 //                 Copyright (2006) 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 Todd S. Coffey (tscoffe@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 //@HEADER
00028 
00029 
00030 #ifndef RYTHMOS_STEPPER_AS_MODEL_EVALUATOR_HPP
00031 #define RYTHMOS_STEPPER_AS_MODEL_EVALUATOR_HPP
00032 
00033 
00034 #include "Thyra_ResponseOnlyModelEvaluatorBase.hpp"
00035 
00036 #include "Rythmos_StepperBase.hpp"
00037 #include "Rythmos_IntegratorBase.hpp"
00038 #include "Teuchos_StandardMemberCompositionMacros.hpp"
00039 #include "Teuchos_Assert.hpp"
00040 
00041 
00042 namespace Rythmos {
00043 
00044 
00051 template<class Scalar>
00052 class StepperAsModelEvaluator
00053   : virtual public Thyra::ResponseOnlyModelEvaluatorBase<Scalar>
00054 {
00055 public:
00056 
00059 
00061   StepperAsModelEvaluator();
00062 
00064   void initialize(
00065     const RCP<StepperBase<Scalar> > &stepper,
00066     const RCP<IntegratorBase<Scalar> > &integrator,
00067     const Thyra::ModelEvaluatorBase::InArgs<Scalar> &initialCondition
00068     );
00069 
00071 
00074 
00076   int Np() const;
00078   int Ng() const;
00080   RCP<const Thyra::VectorSpaceBase<Scalar> >
00081   get_p_space(int l) const;
00083   RCP<const Thyra::VectorSpaceBase<Scalar> >
00084   get_g_space(int j) const;
00086   Thyra::ModelEvaluatorBase::InArgs<Scalar> createInArgs() const;
00087 
00089 
00090 private:
00091 
00094 
00096   Thyra::ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const;
00098   void evalModelImpl(
00099     const Thyra::ModelEvaluatorBase::InArgs<Scalar>& inArgs,
00100     const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs
00101     ) const;
00102 
00104 
00105 private:
00106 
00107   // //////////////////////
00108   // Private types
00109 
00110   typedef Array<RCP<const Thyra::VectorSpaceBase<Scalar> > > SpaceArray_t;
00111   
00112 
00113   // //////////////////////
00114   // Private data members
00115 
00116   RCP<StepperBase<Scalar> > stepper_;
00117   RCP<IntegratorBase<Scalar> > integrator_;
00118   Thyra::ModelEvaluatorBase::InArgs<Scalar> initialCondition_;
00119 
00120   int Np_;
00121   int Ng_;
00122 
00123   SpaceArray_t g_space_;
00124   SpaceArray_t p_space_;
00125 
00126   mutable  Thyra::ModelEvaluatorBase::InArgs<Scalar> currentInitialCondition_;
00127 
00128 };
00129 
00130 
00132 template<class Scalar>
00133 RCP<StepperAsModelEvaluator<Scalar> >
00134 stepperAsModelEvaluator(
00135   const RCP<StepperBase<Scalar> > &stepper,
00136   const RCP<IntegratorBase<Scalar> > &integrator,
00137   const Thyra::ModelEvaluatorBase::InArgs<Scalar> &initialCondition
00138   )
00139 {
00140   using Teuchos::rcp;
00141   RCP<StepperAsModelEvaluator<Scalar> >
00142     stepperAsModelEvaluator = rcp(new StepperAsModelEvaluator<Scalar>());
00143   stepperAsModelEvaluator->initialize(stepper,integrator,initialCondition);
00144   return stepperAsModelEvaluator;
00145 }
00146 
00147 
00148 // ////////////////////////
00149 // Definitions
00150 
00151 
00152 // Constructors, Initialization, Misc.
00153 
00154 
00155 template<class Scalar>
00156 StepperAsModelEvaluator<Scalar>::StepperAsModelEvaluator()
00157   : Np_(0), Ng_(0)
00158 {}
00159 
00160 
00161 template<class Scalar>
00162 void StepperAsModelEvaluator<Scalar>::initialize(
00163   const RCP<StepperBase<Scalar> > &stepper,
00164   const RCP<IntegratorBase<Scalar> > &integrator,
00165   const Thyra::ModelEvaluatorBase::InArgs<Scalar> &initialCondition
00166   )
00167 {
00168 
00169 #ifdef TEUCHOS_DEBUG
00170   TEST_FOR_EXCEPT(is_null(stepper));
00171   TEST_FOR_EXCEPT(is_null(stepper->getModel()));
00172   TEST_FOR_EXCEPT(is_null(integrator));
00173 #endif
00174   stepper_ = stepper;
00175   integrator_ = integrator;
00176   initialCondition_ = initialCondition;
00177   currentInitialCondition_ = initialCondition;
00178 
00179   const RCP<const Thyra::ModelEvaluator<Scalar> >
00180     stepperModel = stepper_->getModel();
00181 
00182   Np_ = stepperModel->Np();
00183   p_space_.clear();
00184   for ( int l = 0; l < Np_; ++l ) {
00185     p_space_.push_back(stepperModel->get_p_space(l));
00186   }
00187 
00188   Ng_ = 1;
00189   g_space_.clear();
00190   g_space_.push_back(stepper_->getModel()->get_x_space());
00191 
00192 }
00193 
00194 
00195 // Public functions overridden from ModelEvaulator
00196 
00197 
00198 template<class Scalar>
00199 int StepperAsModelEvaluator<Scalar>::Np() const
00200 {
00201   return Np_;
00202 }
00203 
00204 
00205 template<class Scalar>
00206 int StepperAsModelEvaluator<Scalar>::Ng() const
00207 {
00208   return Ng_;
00209 }
00210 
00211 
00212 template<class Scalar>
00213 RCP<const Thyra::VectorSpaceBase<Scalar> >
00214 StepperAsModelEvaluator<Scalar>::get_p_space(int l) const
00215 {
00216 #ifdef TEUCHOS_DEBUG
00217   TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( l, 0, Np_ );
00218 #endif
00219   return p_space_[l];
00220 }
00221 
00222 
00223 template<class Scalar>
00224 RCP<const Thyra::VectorSpaceBase<Scalar> >
00225 StepperAsModelEvaluator<Scalar>::get_g_space(int j) const
00226 {
00227 #ifdef TEUCHOS_DEBUG
00228   TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( j, 0, Ng_ );
00229 #endif
00230   return g_space_[j];
00231 }
00232 
00233 
00234 template<class Scalar>
00235 Thyra::ModelEvaluatorBase::InArgs<Scalar>
00236 StepperAsModelEvaluator<Scalar>::createInArgs() const
00237 {
00238   typedef Thyra::ModelEvaluatorBase MEB;
00239   MEB::InArgsSetup<Scalar> inArgs;
00240   inArgs.setModelEvalDescription(this->description());
00241   inArgs.set_Np(Np_);
00242   inArgs.setSupports(MEB::IN_ARG_t);
00243   return inArgs;
00244 }
00245 
00246 
00247 // Private functions overridden from ModelEvaulatorDefaultBase
00248 
00249 
00250 template<class Scalar>
00251 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
00252 StepperAsModelEvaluator<Scalar>::createOutArgsImpl() const
00253 {
00254   typedef Thyra::ModelEvaluatorBase MEB;
00255   MEB::OutArgsSetup<Scalar> outArgs;
00256   outArgs.setModelEvalDescription(this->description());
00257   outArgs.set_Np_Ng(Np_,Ng_);
00258   return outArgs;
00259 }
00260 
00261 
00262 template<class Scalar>
00263 void StepperAsModelEvaluator<Scalar>::evalModelImpl(
00264   const Thyra::ModelEvaluatorBase::InArgs<Scalar>& inArgs,
00265   const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs
00266   ) const
00267 {
00268 
00269   using Teuchos::as;
00270   using Teuchos::describe;
00271   typedef Teuchos::ScalarTraits<Scalar> ST;
00272   typedef Teuchos::VerboseObjectTempState<InterpolationBufferBase<Scalar> > VOTSSB;
00273 
00274   THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_GEN_BEGIN(
00275     "Rythmos::StepperAsModelEvaluator", inArgs, outArgs, Teuchos::null
00276     );
00277   VOTSSB integrator_outputTempState(integrator_,out,incrVerbLevel(verbLevel,-1));
00278   //VOTSSB stepper_outputTempState(stepper_,out,incrVerbLevel(verbLevel,-1));
00279 
00280   // InArgs
00281 
00282   const Scalar finalTime = inArgs.get_t();
00283   for ( int l = 0; l < Np_; ++l ) {
00284     currentInitialCondition_.set_p(l,inArgs.get_p(l));
00285   }
00286 
00287   // OutArgs
00288 
00289   RCP<Thyra::VectorBase<Scalar> >
00290     g_out = outArgs.get_g(0);
00291 
00292   TEST_FOR_EXCEPT(
00293     is_null(g_out) && "You must ask for g(0) when you call this function!"
00294     );
00295 
00296 #ifdef TEUCHOS_DEBUG
00297 
00298   THYRA_ASSERT_VEC_SPACES(
00299     "StepperAsModelEvaluator<Scalar>::evalModel(...)",
00300     *g_out->space(), *stepper_->get_x_space() );
00301 
00302 #endif
00303   
00304   // Set up the integrator
00305 
00306   stepper_->setInitialCondition(currentInitialCondition_);
00307   integrator_->setStepper(stepper_,finalTime);
00308 
00309   // Compute the desired response
00310 
00311   if (!is_null(g_out)) {
00312 
00313     // Get x and xdot at the end time
00314     Array<Scalar> time_vec = Teuchos::tuple<Scalar>(finalTime);
00315     Array<RCP<const Thyra::VectorBase<Scalar> > > x_vec, xdot_vec;
00316     integrator_->getFwdPoints( time_vec, &x_vec, &xdot_vec, 0 );
00317     
00318     Thyra::V_V( &*g_out, *x_vec[0] );
00319 
00320   }
00321 
00322   THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
00323 
00324 }
00325 
00326 
00327 } // namespace Rythmos
00328 
00329 
00330 #endif // RYTHMOS_STEPPER_AS_MODEL_EVALUATOR_HPP

Generated on Tue Oct 20 12:46:08 2009 for Rythmos - Transient Integration for Differential Equations by doxygen 1.4.7