29 #ifndef RYTHMOS_FORWARD_SENSITIVITY_EXPLICIT_MODEL_EVALUATOR_HPP
30 #define RYTHMOS_FORWARD_SENSITIVITY_EXPLICIT_MODEL_EVALUATOR_HPP
33 #include "Rythmos_IntegratorBase.hpp"
34 #include "Rythmos_ForwardSensitivityModelEvaluatorBase.hpp"
35 #include "Thyra_ModelEvaluator.hpp"
36 #include "Thyra_StateFuncModelEvaluatorBase.hpp"
37 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
38 #include "Thyra_DefaultMultiVectorProductVectorSpace.hpp"
39 #include "Thyra_DefaultMultiVectorProductVector.hpp"
40 #include "Thyra_DefaultMultiVectorLinearOpWithSolve.hpp"
41 #include "Thyra_VectorStdOps.hpp"
42 #include "Thyra_MultiVectorStdOps.hpp"
125 template<
class Scalar>
144 const RCP<
const Thyra::ModelEvaluator<Scalar> > &stateModel,
150 const RCP<
const Thyra::ModelEvaluator<Scalar> >& stateModel,
151 const RCP<
const Thyra::VectorSpaceBase<Scalar> >& p_space
155 RCP<const Thyra::ModelEvaluator<Scalar> >
159 RCP<Thyra::ModelEvaluator<Scalar> >
166 RCP<const Thyra::DefaultMultiVectorProductVectorSpace<Scalar> >
185 RCP<const Thyra::VectorSpaceBase<Scalar> >
get_x_space()
const;
187 RCP<const Thyra::VectorSpaceBase<Scalar> >
get_f_space()
const;
191 RCP<Thyra::LinearOpWithSolveBase<Scalar> >
create_W()
const;
193 Thyra::ModelEvaluatorBase::InArgs<Scalar>
createInArgs()
const;
203 Thyra::ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl()
const;
206 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs,
207 const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs
217 RCP<const Thyra::ModelEvaluator<Scalar> > stateModel_;
221 RCP<const Thyra::DefaultMultiVectorProductVectorSpace<Scalar> > s_bar_space_;
222 RCP<const Thyra::DefaultMultiVectorProductVectorSpace<Scalar> > f_sens_space_;
224 Thyra::ModelEvaluatorBase::InArgs<Scalar> nominalValues_;
226 mutable Thyra::ModelEvaluatorBase::InArgs<Scalar> stateBasePoint_;
228 mutable RCP<Thyra::LinearOpBase<Scalar> > DfDx_;
229 mutable RCP<Thyra::MultiVectorBase<Scalar> > DfDp_;
231 mutable RCP<Thyra::LinearOpBase<Scalar> > DfDx_compute_;
232 mutable RCP<Thyra::MultiVectorBase<Scalar> > DfDp_compute_;
237 void wrapNominalValuesAndBounds();
239 void computeDerivativeMatrices(
240 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &point
253 template<
class Scalar>
254 RCP<ForwardSensitivityExplicitModelEvaluator<Scalar> >
255 forwardSensitivityExplicitModelEvaluator()
257 RCP<ForwardSensitivityExplicitModelEvaluator<Scalar> > fseme =
263 template<
class Scalar>
265 : p_index_(0), np_(-1)
273 template<
class Scalar>
275 const RCP<
const Thyra::ModelEvaluator<Scalar> > &stateModel,
280 typedef Thyra::ModelEvaluatorBase MEB;
286 TEUCHOS_TEST_FOR_EXCEPT( is_null(stateModel) );
287 TEUCHOS_TEST_FOR_EXCEPTION(
288 !( 0 <= p_index && p_index < stateModel->Np() ), std::logic_error,
289 "Error, p_index does not fall in the range [0,"<<(stateModel->Np()-1)<<
"]!" );
296 stateModel_ = stateModel;
298 np_ = stateModel_->get_p_space(p_index)->dim();
304 s_bar_space_ = Thyra::multiVectorProductVectorSpace(
305 stateModel_->get_x_space(), np_
308 f_sens_space_ = Thyra::multiVectorProductVectorSpace(
309 stateModel_->get_f_space(), np_
312 nominalValues_ = this->createInArgs();
314 this->wrapNominalValuesAndBounds();
320 stateBasePoint_ = MEB::InArgs<Scalar>();
321 DfDx_ = Teuchos::null;
322 DfDp_ = Teuchos::null;
323 DfDx_compute_ = Teuchos::null;
324 DfDp_compute_ = Teuchos::null;
329 template<
class Scalar>
331 const RCP<
const Thyra::ModelEvaluator<Scalar> >& ,
332 const RCP<
const Thyra::VectorSpaceBase<Scalar> >&
335 TEUCHOS_TEST_FOR_EXCEPT_MSG(
true,
"ToDo: Implement initializeStructureInitCondOnly()!" );
339 template<
class Scalar>
340 RCP<const Thyra::ModelEvaluator<Scalar> >
347 template<
class Scalar>
348 RCP<Thyra::ModelEvaluator<Scalar> >
351 return Teuchos::null;
355 template<
class Scalar>
362 template<
class Scalar>
363 RCP<const Thyra::DefaultMultiVectorProductVectorSpace<Scalar> >
370 template<
class Scalar>
371 RCP<const Thyra::VectorSpaceBase<Scalar> >
374 return stateModel_->get_p_space(p_index_);
378 template<
class Scalar>
384 TEUCHOS_ASSERT( Teuchos::nonnull(stateStepper) );
385 #ifdef HAVE_RYTHMOS_DEBUG
386 TEUCHOS_TEST_FOR_EXCEPTION(
387 is_null(stateModel_), std::logic_error,
388 "Error, you must call intializeStructure(...) before you call initializePointState(...)"
390 #endif // HAVE_RYTHMOS_DEBUG
392 Scalar curr_t = stateStepper->getStepStatus().time;
393 RCP<const Thyra::VectorBase<Scalar> > x;
394 x = get_x(*stateStepper,curr_t);
395 #ifdef HAVE_RYTHMOS_DEBUG
396 TEUCHOS_TEST_FOR_EXCEPT( Teuchos::is_null(x) );
397 #endif // HAVE_RYTHMOS_DEBUG
399 stateBasePoint_ = stateStepper->getInitialCondition();
400 stateBasePoint_.set_x( x );
401 stateBasePoint_.set_t( curr_t );
407 wrapNominalValuesAndBounds();
415 template<
class Scalar>
416 RCP<const Thyra::VectorSpaceBase<Scalar> >
423 template<
class Scalar>
424 RCP<const Thyra::VectorSpaceBase<Scalar> >
427 return f_sens_space_;
431 template<
class Scalar>
432 Thyra::ModelEvaluatorBase::InArgs<Scalar>
435 return nominalValues_;
439 template<
class Scalar>
440 RCP<Thyra::LinearOpWithSolveBase<Scalar> >
443 return Thyra::multiVectorLinearOpWithSolve<Scalar>();
447 template<
class Scalar>
448 Thyra::ModelEvaluatorBase::InArgs<Scalar>
451 TEUCHOS_ASSERT( !is_null(stateModel_) );
452 typedef Thyra::ModelEvaluatorBase MEB;
453 MEB::InArgs<Scalar> stateModelInArgs = stateModel_->createInArgs();
454 MEB::InArgsSetup<Scalar> inArgs;
455 inArgs.setModelEvalDescription(this->description());
456 inArgs.setSupports( MEB::IN_ARG_x );
457 inArgs.setSupports( MEB::IN_ARG_t );
458 inArgs.setSupports( MEB::IN_ARG_beta,
459 stateModelInArgs.supports(MEB::IN_ARG_beta) );
467 template<
class Scalar>
468 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
471 TEUCHOS_ASSERT( !is_null(stateModel_) );
472 typedef Thyra::ModelEvaluatorBase MEB;
474 MEB::OutArgs<Scalar> stateModelOutArgs = stateModel_->createOutArgs();
475 MEB::OutArgsSetup<Scalar> outArgs;
477 outArgs.setModelEvalDescription(this->description());
479 outArgs.setSupports(MEB::OUT_ARG_f);
486 template<
class Scalar>
487 void ForwardSensitivityExplicitModelEvaluator<Scalar>::evalModelImpl(
488 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs,
489 const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs
493 using Teuchos::rcp_dynamic_cast;
494 typedef Teuchos::ScalarTraits<Scalar> ST;
496 typedef Teuchos::VerboseObjectTempState<Thyra::ModelEvaluatorBase> VOTSME;
498 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_GEN_BEGIN(
499 "ForwardSensitivityExplicitModelEvaluator", inArgs, outArgs, Teuchos::null );
507 RYTHMOS_FUNC_TIME_MONITOR_DIFF(
508 "Rythmos:ForwardSensitivityExplicitModelEvaluator::evalModel: computeMatrices",
511 computeDerivativeMatrices(inArgs);
518 RCP<const Thyra::DefaultMultiVectorProductVector<Scalar> >
519 s_bar = rcp_dynamic_cast<
const Thyra::DefaultMultiVectorProductVector<Scalar> >(
520 inArgs.get_x().assert_not_null(), true
522 RCP<const Thyra::MultiVectorBase<Scalar> >
523 S = s_bar->getMultiVector();
529 RCP<Thyra::DefaultMultiVectorProductVector<Scalar> >
530 f_sens = rcp_dynamic_cast<Thyra::DefaultMultiVectorProductVector<Scalar> >(
531 outArgs.get_f(), true
534 RCP<Thyra::MultiVectorBase<Scalar> >
535 F_sens = f_sens->getNonconstMultiVector().assert_not_null();
541 if(!is_null(F_sens)) {
543 RYTHMOS_FUNC_TIME_MONITOR_DIFF(
544 "Rythmos:ForwardSensitivityExplicitModelEvaluator::evalModel: computeSens",
551 *DfDx_, Thyra::NOTRANS,
553 ST::one(), ST::zero()
556 Vp_V( F_sens.ptr(), *DfDp_ );
559 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
567 template<
class Scalar>
568 void ForwardSensitivityExplicitModelEvaluator<Scalar>::wrapNominalValuesAndBounds()
570 TEUCHOS_ASSERT( !is_null(stateModel_) );
574 typedef Teuchos::ScalarTraits<Scalar> ST;
578 nominalValues_.set_t(stateModel_->getNominalValues().get_t());
588 RCP<VectorBase<Scalar> > s_bar_ic = Thyra::createMember(this->get_x_space());
589 Thyra::V_S(s_bar_ic.ptr(),ST::zero());
590 nominalValues_.set_x(s_bar_ic);
594 template<
class Scalar>
595 void ForwardSensitivityExplicitModelEvaluator<Scalar>::computeDerivativeMatrices(
596 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &
599 TEUCHOS_ASSERT( !is_null(stateModel_) );
601 typedef Thyra::ModelEvaluatorBase MEB;
602 typedef Teuchos::VerboseObjectTempState<MEB> VOTSME;
604 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
605 Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
607 MEB::InArgs<Scalar> inArgs = stateBasePoint_;
608 MEB::OutArgs<Scalar> outArgs = stateModel_->createOutArgs();
610 if (is_null(DfDx_)) {
611 DfDx_ = stateModel_->create_W_op();
613 if (inArgs.supports(MEB::IN_ARG_beta)) {
614 inArgs.set_beta(1.0);
616 outArgs.set_W_op(DfDx_);
618 if (is_null(DfDp_)) {
619 DfDp_ = Thyra::create_DfDp_mv(
620 *stateModel_,p_index_,
626 MEB::Derivative<Scalar>(DfDp_,MEB::DERIV_MV_BY_COL)
629 VOTSME stateModel_outputTempState(stateModel_,out,verbLevel);
630 stateModel_->evalModel(inArgs,outArgs);
639 #endif // RYTHMOS_FORWARD_SENSITIVITY_EXPLICIT_MODEL_EVALUATOR_HPP
Base class for defining stepper functionality.
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
RCP< const Thyra::DefaultMultiVectorProductVectorSpace< Scalar > > get_s_bar_space() const
RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
void initializePointState(Ptr< StepperBase< Scalar > > stateStepper, bool forceUpToDateW)
Initialize full state for a single point in time.
void initializeStructure(const RCP< const Thyra::ModelEvaluator< Scalar > > &stateModel, const int p_index)
RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
RCP< Thyra::LinearOpWithSolveBase< Scalar > > create_W() const
RCP< Thyra::ModelEvaluator< Scalar > > getNonconstStateModel() const
void initializeStructureInitCondOnly(const RCP< const Thyra::ModelEvaluator< Scalar > > &stateModel, const RCP< const Thyra::VectorSpaceBase< Scalar > > &p_space)
Explicit forward sensitivity transient ModelEvaluator subclass.
Forward sensitivity transient ModelEvaluator node interface class.
RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_sens_space() const
RCP< const Thyra::ModelEvaluator< Scalar > > getStateModel() const
ForwardSensitivityExplicitModelEvaluator()