30 #ifndef RYTHMOS_IMPLICITRK_MODEL_EVALUATOR_HPP
31 #define RYTHMOS_IMPLICITRK_MODEL_EVALUATOR_HPP
34 #include "Rythmos_Types.hpp"
35 #include "Rythmos_RKButcherTableauHelpers.hpp"
36 #include "Thyra_StateFuncModelEvaluatorBase.hpp"
37 #include "Thyra_ModelEvaluatorHelpers.hpp"
38 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
39 #include "Thyra_DefaultProductVectorSpace.hpp"
40 #include "Thyra_DefaultBlockedLinearOp.hpp"
41 #include "Thyra_VectorStdOps.hpp"
42 #include "Thyra_TestingTools.hpp"
43 #include "Teuchos_as.hpp"
44 #include "Teuchos_SerialDenseMatrix.hpp"
45 #include "Teuchos_SerialDenseVector.hpp"
46 #include "Teuchos_Assert.hpp"
53 template<
class Scalar>
66 const RCP<
const Thyra::ModelEvaluator<Scalar> >& daeModel,
67 const Thyra::ModelEvaluatorBase::InArgs<Scalar>& basePoint,
68 const RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >& irk_W_factory,
69 const RCP<
const RKButcherTableauBase<Scalar> >& irkButcherTableau
74 const RCP<
const Thyra::VectorBase<Scalar> > &x_old,
85 RCP<const Thyra::VectorSpaceBase<Scalar> >
get_x_space()
const;
87 RCP<const Thyra::VectorSpaceBase<Scalar> >
get_f_space()
const;
89 RCP<Thyra::LinearOpBase<Scalar> >
create_W_op()
const;
91 RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> >
get_W_factory()
const;
95 Thyra::ModelEvaluatorBase::InArgs<Scalar>
createInArgs()
const;
105 Thyra::ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl()
const;
108 const Thyra::ModelEvaluatorBase::InArgs<Scalar>& inArgs,
109 const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs
117 RCP<const Thyra::ModelEvaluator<Scalar> > daeModel_;
118 Thyra::ModelEvaluatorBase::InArgs<Scalar> basePoint_;
119 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > irk_W_factory_;
120 RCP<const RKButcherTableauBase<Scalar> > irkButcherTableau_;
122 Thyra::ModelEvaluatorBase::InArgs<Scalar> inArgs_;
123 Thyra::ModelEvaluatorBase::OutArgs<Scalar> outArgs_;
124 Thyra::ModelEvaluatorBase::InArgs<Scalar> nominalValues_;
126 RCP<const Thyra::ProductVectorSpaceBase<Scalar> > x_bar_space_;
127 RCP<const Thyra::ProductVectorSpaceBase<Scalar> > f_bar_space_;
128 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > W_bar_factory_;
130 bool setTimeStepPointCalled_;
131 RCP<const Thyra::VectorBase<Scalar> > x_old_;
144 template<
class Scalar>
145 RCP<ImplicitRKModelEvaluator<Scalar> >
147 const RCP<
const Thyra::ModelEvaluator<Scalar> >& daeModel,
148 const Thyra::ModelEvaluatorBase::InArgs<Scalar>& basePoint,
149 const RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >& irk_W_factory,
150 const RCP<
const RKButcherTableauBase<Scalar> >& irkButcherTableau
153 RCP<ImplicitRKModelEvaluator<Scalar> >
155 irkModel->initializeIRKModel(daeModel,basePoint,irk_W_factory,irkButcherTableau);
167 template<
class Scalar>
170 setTimeStepPointCalled_ =
false;
171 isInitialized_ =
false;
178 template<
class Scalar>
180 const RCP<
const Thyra::ModelEvaluator<Scalar> >& daeModel,
181 const Thyra::ModelEvaluatorBase::InArgs<Scalar>& basePoint,
182 const RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >& irk_W_factory,
183 const RCP<
const RKButcherTableauBase<Scalar> >& irkButcherTableau
188 TEUCHOS_TEST_FOR_EXCEPTION(
189 is_null(basePoint.get_x()),
191 "Error! The basepoint x vector is null!"
193 TEUCHOS_TEST_FOR_EXCEPTION(
196 "Error! The model evaluator pointer is null!"
198 TEUCHOS_TEST_FOR_EXCEPTION(
199 !daeModel->get_x_space()->isCompatible(*(basePoint.get_x()->space())),
201 "Error! The basepoint input arguments are incompatible with the model evaluator vector space!"
203 TEUCHOS_TEST_FOR_EXCEPT(is_null(irk_W_factory));
205 daeModel_ = daeModel;
206 basePoint_ = basePoint;
207 irk_W_factory_ = irk_W_factory;
208 irkButcherTableau_ = irkButcherTableau;
210 const int numStages = irkButcherTableau_->numStages();
212 x_bar_space_ = productVectorSpace(daeModel_->get_x_space(),numStages);
213 f_bar_space_ = productVectorSpace(daeModel_->get_f_space(),numStages);
216 if (irk_W_factory_->acceptsPreconditionerFactory())
217 irk_W_factory_->unsetPreconditionerFactory();
224 typedef Thyra::ModelEvaluatorBase MEB;
225 MEB::InArgsSetup<Scalar> inArgs;
226 inArgs.setModelEvalDescription(this->description());
227 inArgs.setSupports(MEB::IN_ARG_x);
232 typedef Thyra::ModelEvaluatorBase MEB;
233 MEB::OutArgsSetup<Scalar> outArgs;
234 outArgs.setModelEvalDescription(this->description());
235 outArgs.setSupports(MEB::OUT_ARG_f);
236 outArgs.setSupports(MEB::OUT_ARG_W_op);
240 nominalValues_ = inArgs_;
242 isInitialized_ =
true;
246 template<
class Scalar>
248 const RCP<
const Thyra::VectorBase<Scalar> > &x_old,
250 const Scalar &delta_t
253 typedef ScalarTraits<Scalar> ST;
254 TEUCHOS_TEST_FOR_EXCEPT( is_null(x_old) );
255 TEUCHOS_TEST_FOR_EXCEPT( delta_t <= ST::zero() );
257 TEUCHOS_TEST_FOR_EXCEPTION(!daeModel_->get_x_space()->isCompatible(*(x_old->space())), std::logic_error,
258 "Error! The incoming VectorBase object is not compatible with the DAE model that was provided at initialization!");
262 setTimeStepPointCalled_ =
true;
269 template<
class Scalar>
270 RCP<const Thyra::VectorSpaceBase<Scalar> >
273 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
274 "Error, initializeIRKModel must be called first!\n"
280 template<
class Scalar>
281 RCP<const Thyra::VectorSpaceBase<Scalar> >
284 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
285 "Error, initializeIRKModel must be called first!\n"
291 template<
class Scalar>
292 RCP<Thyra::LinearOpBase<Scalar> >
295 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
296 "Error, initializeIRKModel must be called first!\n"
299 const int numStages = irkButcherTableau_->numStages();
300 RCP<Thyra::PhysicallyBlockedLinearOpBase<Scalar> >
301 W_op_bar = Thyra::defaultBlockedLinearOp<Scalar>();
302 W_op_bar->beginBlockFill( f_bar_space_, x_bar_space_ );
303 for (
int i = 0; i < numStages; ++i )
304 for (
int j = 0; j < numStages; ++j )
305 W_op_bar->setNonconstBlock( i, j, daeModel_->create_W_op() );
306 W_op_bar->endBlockFill();
311 template<
class Scalar>
312 RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> >
315 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
316 "Error, initializeIRKModel must be called first!\n"
318 return irk_W_factory_;
322 template<
class Scalar>
323 Thyra::ModelEvaluatorBase::InArgs<Scalar>
326 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
327 "Error, initializeIRKModel must be called first!\n"
329 return nominalValues_;
333 template<
class Scalar>
334 Thyra::ModelEvaluatorBase::InArgs<Scalar>
337 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
338 "Error, initializeIRKModel must be called first!\n"
347 template<
class Scalar>
348 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
351 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
352 "Error, initializeIRKModel must be called first!\n"
358 template<
class Scalar>
359 void ImplicitRKModelEvaluator<Scalar>::evalModelImpl(
360 const Thyra::ModelEvaluatorBase::InArgs<Scalar>& inArgs_bar,
361 const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs_bar
365 using Teuchos::rcp_dynamic_cast;
366 typedef ScalarTraits<Scalar> ST;
367 typedef Thyra::ModelEvaluatorBase MEB;
368 typedef Thyra::VectorBase<Scalar> VB;
369 typedef Thyra::ProductVectorBase<Scalar> PVB;
370 typedef Thyra::BlockedLinearOpBase<Scalar> BLWB;
372 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
373 "Error! initializeIRKModel must be called before evalModel\n"
376 TEUCHOS_TEST_FOR_EXCEPTION( !setTimeStepPointCalled_, std::logic_error,
377 "Error! setTimeStepPoint must be called before evalModel"
380 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_GEN_BEGIN(
381 "Rythmos::ImplicitRKModelEvaluator",inArgs_bar,outArgs_bar,daeModel_
388 const RCP<const PVB> x_bar = rcp_dynamic_cast<
const PVB>(inArgs_bar.get_x(),
true);
389 const RCP<PVB> f_bar = rcp_dynamic_cast<PVB>(outArgs_bar.get_f(),
true);
390 const RCP<BLWB> W_op_bar = rcp_dynamic_cast<BLWB>(outArgs_bar.get_W_op(),
true);
396 MEB::InArgs<Scalar> daeInArgs = daeModel_->createInArgs();
397 MEB::OutArgs<Scalar> daeOutArgs = daeModel_->createOutArgs();
398 const RCP<VB> x_i = createMember(daeModel_->get_x_space());
399 daeInArgs.setArgs(basePoint_);
401 const int numStages = irkButcherTableau_->numStages();
403 for (
int i = 0; i < numStages; ++i ) {
406 assembleIRKState( i, irkButcherTableau_->A(), delta_t_, *x_old_, *x_bar, outArg(*x_i) );
407 daeInArgs.set_x( x_i );
408 daeInArgs.set_x_dot( x_bar->getVectorBlock(i) );
409 daeInArgs.set_t( t_old_ + irkButcherTableau_->c()(i) * delta_t_ );
410 Scalar alpha = ST::zero();
416 Scalar beta = delta_t_ * irkButcherTableau_->A()(i,0);
417 daeInArgs.set_alpha( alpha );
418 daeInArgs.set_beta( beta );
422 daeOutArgs.set_f( f_bar->getNonconstVectorBlock(i) );
423 if (!is_null(W_op_bar)) {
424 daeOutArgs.set_W_op(W_op_bar->getNonconstBlock(i,0));
428 daeModel_->evalModel( daeInArgs, daeOutArgs );
429 daeOutArgs.set_f(Teuchos::null);
430 daeOutArgs.set_W_op(Teuchos::null);
433 if (!is_null(W_op_bar)) {
434 for (
int j = 1; j < numStages; ++j ) {
441 beta = delta_t_ * irkButcherTableau_->A()(i,j);
442 daeInArgs.set_alpha( alpha );
443 daeInArgs.set_beta( beta );
444 daeOutArgs.set_W_op(W_op_bar->getNonconstBlock(i,j));
445 daeModel_->evalModel( daeInArgs, daeOutArgs );
446 daeOutArgs.set_W_op(Teuchos::null);
452 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
460 #endif // RYTHMOS_IMPLICITRK_MODEL_EVALUATOR_HPP
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
void initializeIRKModel(const RCP< const Thyra::ModelEvaluator< Scalar > > &daeModel, const Thyra::ModelEvaluatorBase::InArgs< Scalar > &basePoint, const RCP< Thyra::LinearOpWithSolveFactoryBase< Scalar > > &irk_W_factory, const RCP< const RKButcherTableauBase< Scalar > > &irkButcherTableau)
RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
void setTimeStepPoint(const RCP< const Thyra::VectorBase< Scalar > > &x_old, const Scalar &t_old, const Scalar &delta_t)
ImplicitRKModelEvaluator()
RCP< ImplicitRKModelEvaluator< Scalar > > implicitRKModelEvaluator(const RCP< const Thyra::ModelEvaluator< Scalar > > &daeModel, const Thyra::ModelEvaluatorBase::InArgs< Scalar > &basePoint, const RCP< Thyra::LinearOpWithSolveFactoryBase< Scalar > > &irk_W_factory, const RCP< const RKButcherTableauBase< Scalar > > &irkButcherTableau)
Nonmember constructor function.