42 #ifndef THYRA_DEFAULT_MULTI_PERIOD_MODEL_EVALUATOR_HPP
43 #define THYRA_DEFAULT_MULTI_PERIOD_MODEL_EVALUATOR_HPP
46 #include "Thyra_ModelEvaluatorDefaultBase.hpp"
47 #include "Thyra_DefaultProductVectorSpace.hpp"
48 #include "Thyra_DefaultBlockedTriangularLinearOpWithSolveFactory.hpp"
49 #include "Thyra_DefaultBlockedLinearOp.hpp"
50 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
51 #include "Thyra_ProductVectorBase.hpp"
52 #include "Teuchos_implicit_cast.hpp"
53 #include "Teuchos_AbstractFactory.hpp"
54 #include "Teuchos_AbstractFactoryStd.hpp"
130 template<
class Scalar>
335 void set_z_indexes_and_create_period_l_map(
const Array<int> &z_indexes );
337 void wrapNominalValuesAndBounds();
347 int period_l(
const int l)
const
350 return period_l_map_[l];
353 int numPeriodZs()
const {
return z_indexes_.
size(); }
355 int N()
const {
return x_bar_space_->numBlocks(); }
367 template<
class Scalar>
369 :g_index_(-1), Np_(-1), Ng_(-1)
373 template<
class Scalar>
385 :g_index_(-1), Np_(-1), Ng_(-1)
388 N, periodModels, z_indexes, z, g_index, g_weights,
389 x_bar_space, f_bar_space, W_bar_factory
394 template<
class Scalar>
414 MEB::InArgs<Scalar> periodInArgs = periodModels[0]->createInArgs();
415 MEB::OutArgs<Scalar> periodOutArgs = periodModels[0]->createOutArgs();
416 for (
int k = 0; k < implicit_cast<int>(z_indexes.
size()); ++k ) {
424 Np_ = periodInArgs.Np() - z_indexes.
size();
428 set_z_indexes_and_create_period_l_map(z_indexes);
430 periodModel_ = periodModels[0];
432 periodModels_ = periodModels;
438 g_weights_ = g_weights;
440 if ( periodInArgs.supports(MEB::IN_ARG_x) ) {
444 x_bar_space_ = x_bar_space;
447 x_bar_space_ = productVectorSpace(periodModel_->get_x_space(),N);
452 if ( periodOutArgs.supports(MEB::OUT_ARG_f) ) {
456 f_bar_space_ = f_bar_space;
459 f_bar_space_ = productVectorSpace(periodModel_->get_f_space(),N);
464 if ( periodOutArgs.supports(MEB::OUT_ARG_W) ) {
465 if ( !
is_null(W_bar_factory) ) {
467 W_bar_factory_ = W_bar_factory;
471 defaultBlockedTriangularLinearOpWithSolveFactory<Scalar>(
472 periodModel_->get_W_factory() );
476 wrapNominalValuesAndBounds();
481 template<
class Scalar>
489 const int N = z_.size();
496 for(
int i = 0; i < N; ++i ) {
510 template<
class Scalar>
517 template<
class Scalar>
524 template<
class Scalar>
532 template<
class Scalar>
540 template<
class Scalar>
544 return periodModel_->get_p_space(period_l(l));
548 template<
class Scalar>
552 return periodModel_->get_p_names(period_l(l));
556 template<
class Scalar>
561 return periodModel_->get_g_space(g_index_);
565 template<
class Scalar>
569 return periodModel_->get_g_names(j);
573 template<
class Scalar>
577 return nominalValues_;
581 template<
class Scalar>
589 template<
class Scalar>
597 template<
class Scalar>
603 W_op_bar = defaultBlockedLinearOp<Scalar>();
604 W_op_bar->beginBlockFill(f_bar_space_,x_bar_space_);
605 const int N = x_bar_space_->numBlocks();
606 for (
int i = 0; i < N; ++i ) {
607 W_op_bar->setNonconstBlock( i, i, periodModel_->create_W_op() );
609 W_op_bar->endBlockFill();
614 template<
class Scalar>
618 return Teuchos::null;
622 template<
class Scalar>
626 return W_bar_factory_;
630 template<
class Scalar>
635 MEB::InArgs<Scalar> periodInArgs = periodModel_->createInArgs();
636 MEB::InArgsSetup<Scalar> inArgs;
637 inArgs.setModelEvalDescription(this->description());
639 inArgs.setSupports( MEB::IN_ARG_x, periodInArgs.supports(MEB::IN_ARG_x) );
644 template<
class Scalar>
647 ,
const bool wasSolved
662 template<
class Scalar>
667 return Teuchos::null;
671 template<
class Scalar>
672 RCP<LinearOpBase<Scalar> >
673 DefaultMultiPeriodModelEvaluator<Scalar>::create_DgDx_dot_op_impl(
int j)
const
676 return Teuchos::null;
680 template<
class Scalar>
681 RCP<LinearOpBase<Scalar> >
682 DefaultMultiPeriodModelEvaluator<Scalar>::create_DgDx_op_impl(
int j)
const
685 return Teuchos::null;
689 template<
class Scalar>
690 RCP<LinearOpBase<Scalar> >
691 DefaultMultiPeriodModelEvaluator<Scalar>::create_DgDp_op_impl(
int j,
int l)
const
694 return Teuchos::null;
698 template<
class Scalar>
699 ModelEvaluatorBase::OutArgs<Scalar>
700 DefaultMultiPeriodModelEvaluator<Scalar>::createOutArgsImpl()
const
703 typedef ModelEvaluatorBase MEB;
705 MEB::OutArgs<Scalar> periodOutArgs = periodModel_->createOutArgs();
706 MEB::OutArgsSetup<Scalar> outArgs;
708 outArgs.setModelEvalDescription(this->description());
710 outArgs.set_Np_Ng(Np_,Ng_);
713 if (periodOutArgs.supports(MEB::OUT_ARG_f) ) {
714 outArgs.setSupports(MEB::OUT_ARG_f);
718 if (periodOutArgs.supports(MEB::OUT_ARG_W_op) ) {
719 outArgs.setSupports(MEB::OUT_ARG_W_op);
720 outArgs.set_W_properties(periodOutArgs.get_W_properties());
726 for (
int l = 0; l < Np_; ++l ) {
727 const int period_l = this->period_l(l);
728 const MEB::DerivativeSupport period_DfDp_l_support
729 = periodOutArgs.supports(MEB::OUT_ARG_DfDp,period_l);
730 if (!period_DfDp_l_support.none()) {
731 outArgs.setSupports( MEB::OUT_ARG_DfDp, l, period_DfDp_l_support );
732 outArgs.set_DfDp_properties(
733 l, periodOutArgs.get_DfDp_properties(period_l) );
738 const MEB::DerivativeSupport
739 period_DgDx_dot_support = periodOutArgs.supports(MEB::OUT_ARG_DgDx_dot,g_index_);
740 if (!period_DgDx_dot_support.none()) {
741 outArgs.setSupports( MEB::OUT_ARG_DgDx_dot, 0, period_DgDx_dot_support );
742 outArgs.set_DgDx_dot_properties(
743 0, periodOutArgs.get_DgDx_dot_properties(g_index_) );
747 const MEB::DerivativeSupport
748 period_DgDx_support = periodOutArgs.supports(MEB::OUT_ARG_DgDx,g_index_);
749 if (!period_DgDx_support.none()) {
750 outArgs.setSupports( MEB::OUT_ARG_DgDx, 0, period_DgDx_support );
751 outArgs.set_DgDx_properties(
752 0, periodOutArgs.get_DgDx_properties(g_index_) );
756 for (
int l = 0; l < Np_; ++l ) {
757 const int period_l = this->period_l(l);
758 const MEB::DerivativeSupport period_DgDp_l_support
759 = periodOutArgs.supports(MEB::OUT_ARG_DgDp, g_index_, period_l);
760 if (!period_DgDp_l_support.none()) {
761 outArgs.setSupports( MEB::OUT_ARG_DgDp, 0, l, period_DgDp_l_support );
762 outArgs.set_DgDp_properties(
763 0, l, periodOutArgs.get_DgDp_properties(g_index_,period_l) );
772 template<
class Scalar>
773 void DefaultMultiPeriodModelEvaluator<Scalar>::evalModelImpl(
774 const ModelEvaluatorBase::InArgs<Scalar> &inArgs,
775 const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
779 using Teuchos::rcp_dynamic_cast;
781 typedef ModelEvaluatorBase MEB;
784 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_GEN_BEGIN(
785 "DefaultMultiPeriodModelEvaluator",inArgs,outArgs,periodModel_ );
789 const int N = x_bar_space_->numBlocks();
790 const int Np = this->Np_;
797 RCP<const ProductVectorBase<Scalar> > x_bar;
798 if (inArgs.supports(MEB::IN_ARG_x)) {
799 x_bar = rcp_dynamic_cast<
const ProductVectorBase<Scalar> >(
800 inArgs.get_x(), true );
802 is_null(x_bar), std::logic_error,
803 "Error, if x is supported, it must be set!"
811 RCP<ProductVectorBase<Scalar> > f_bar;
812 if (outArgs.supports(MEB::OUT_ARG_f)) {
813 f_bar = rcp_dynamic_cast<ProductVectorBase<Scalar> >(
814 outArgs.get_f(), true );
817 Array<MEB::Derivative<Scalar> > DfDp_bar(Np);
818 Array<RCP<ProductMultiVectorBase<Scalar> > > DfDp_bar_mv(Np);
819 for (
int l = 0; l < Np; ++l ) {
820 if (!outArgs.supports(MEB::OUT_ARG_DfDp,l).none()) {
821 MEB::Derivative<Scalar>
822 DfDp_bar_l = outArgs.get_DfDp(l);
823 DfDp_bar[l] = DfDp_bar_l;
824 DfDp_bar_mv[l] = rcp_dynamic_cast<ProductMultiVectorBase<Scalar> >(
825 DfDp_bar_l.getMultiVector(), true );
828 !DfDp_bar_l.isEmpty()
833 DfDp_bar_l.getMultiVectorOrientation() != MEB::DERIV_MV_BY_COL
837 "Error, we currently can only handle DfDp as an column-based multi-vector!"
842 RCP<BlockedLinearOpBase<Scalar> > W_op_bar;
843 if (outArgs.supports(MEB::OUT_ARG_W_op)) {
844 W_op_bar = rcp_dynamic_cast<BlockedLinearOpBase<Scalar> >(
845 outArgs.get_W_op(), true
849 RCP<VectorBase<Scalar> >
850 g_bar = outArgs.get_g(0);
852 MEB::Derivative<Scalar> DgDx_dot_bar;
853 RCP<ProductMultiVectorBase<Scalar> > DgDx_dot_bar_mv;
854 if (!outArgs.supports(MEB::OUT_ARG_DgDx_dot,0).none()) {
855 DgDx_dot_bar = outArgs.get_DgDx_dot(0);
856 DgDx_dot_bar_mv = rcp_dynamic_cast<ProductMultiVectorBase<Scalar> >(
857 DgDx_dot_bar.getMultiVector(), true );
860 !DgDx_dot_bar.isEmpty()
865 DgDx_dot_bar.getMultiVectorOrientation() != MEB::DERIV_TRANS_MV_BY_ROW
869 "Error, we currently can only handle DgDx_dot as an row-based multi-vector!"
873 MEB::Derivative<Scalar> DgDx_bar;
874 RCP<ProductMultiVectorBase<Scalar> > DgDx_bar_mv;
875 if (!outArgs.supports(MEB::OUT_ARG_DgDx,0).none()) {
876 DgDx_bar = outArgs.get_DgDx(0);
877 DgDx_bar_mv = rcp_dynamic_cast<ProductMultiVectorBase<Scalar> >(
878 DgDx_bar.getMultiVector(), true );
886 DgDx_bar.getMultiVectorOrientation() != MEB::DERIV_TRANS_MV_BY_ROW
890 "Error, we currently can only handle DgDx as an row-based multi-vector!"
894 Array<MEB::Derivative<Scalar> > DgDp_bar(Np);
895 Array<RCP<MultiVectorBase<Scalar> > > DgDp_bar_mv(Np);
896 for (
int l = 0; l < Np; ++l ) {
897 if (!outArgs.supports(MEB::OUT_ARG_DgDp,0,l).none()) {
898 MEB::Derivative<Scalar>
899 DgDp_bar_l = outArgs.get_DgDp(0,l);
900 DgDp_bar[l] = DgDp_bar_l;
901 DgDp_bar_mv[l] = DgDp_bar_l.getMultiVector();
903 !DgDp_bar_l.isEmpty() &&
is_null(DgDp_bar_mv[l]),
905 "Error, we currently can only handle DgDp as some type of multi-vector!"
917 periodInArgs = periodModel_->createInArgs();
922 for (
int l = 0; l < Np; ++l )
923 periodInArgs.set_p( period_l(l), inArgs.get_p(l) );
926 periodOutArgs = periodModel_->createOutArgs();
932 g_index_, createMember<Scalar>( periodModel_->get_g_space(g_index_) ) );
936 assign( g_bar.ptr(), ST::zero() );
940 for (
int l = 0; l < Np; ++l ) {
941 if ( !
is_null(DgDp_bar_mv[l]) ) {
942 assign(DgDp_bar_mv[l].ptr(), ST::zero());
943 periodOutArgs.set_DgDp(
944 g_index_, period_l(l),
946 *periodModel_, g_index_, period_l(l),
947 DgDp_bar[l].getMultiVectorOrientation()
955 for (
int i = 0; i < N; ++i ) {
957 VOTSME thyraModel_outputTempState(periodModels_[i],out,verbLevel);
961 for (
int k = 0; k < numPeriodZs(); ++k )
962 periodInArgs.set_p( z_indexes_[k], z_[i][k] );
965 periodInArgs.set_x(x_bar->getVectorBlock(i));
968 periodOutArgs.set_f(f_bar->getNonconstVectorBlock(i));
971 periodOutArgs.set_W_op(W_op_bar->getNonconstBlock(i,i));
973 for (
int l = 0; l < Np; ++l ) {
974 if ( !
is_null(DfDp_bar_mv[l]) ) {
975 periodOutArgs.set_DfDp(
977 MEB::Derivative<Scalar>(
978 DfDp_bar_mv[l]->getNonconstMultiVectorBlock(i),
985 if ( !
is_null(DgDx_dot_bar_mv) ) {
986 periodOutArgs.set_DgDx_dot(
988 MEB::Derivative<Scalar>(
989 DgDx_dot_bar_mv->getNonconstMultiVectorBlock(i),
990 MEB::DERIV_TRANS_MV_BY_ROW
996 periodOutArgs.set_DgDx(
998 MEB::Derivative<Scalar>(
999 DgDx_bar_mv->getNonconstMultiVectorBlock(i),
1000 MEB::DERIV_TRANS_MV_BY_ROW
1007 periodModels_[i]->evalModel( periodInArgs, periodOutArgs );
1013 Vp_StV( g_bar.ptr(), g_weights_[i], *periodOutArgs.get_g(g_index_) );
1017 for (
int l = 0; l < Np; ++l ) {
1018 if ( !
is_null(DgDp_bar_mv[l]) ) {
1021 *periodOutArgs.get_DgDp(g_index_,period_l(l)).getMultiVector(),
1022 DgDp_bar_mv[l].ptr()
1028 if ( !
is_null(DgDx_dot_bar_mv) ) {
1029 scale( g_weights_[i],
1030 DgDx_dot_bar_mv->getNonconstMultiVectorBlock(i).ptr() );
1034 if ( !
is_null(DgDx_bar_mv) ) {
1035 scale( g_weights_[i],
1036 DgDx_bar_mv->getNonconstMultiVectorBlock(i).ptr() );
1048 THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
1056 template<
class Scalar>
1057 void DefaultMultiPeriodModelEvaluator<Scalar>::set_z_indexes_and_create_period_l_map(
1058 const Array<int> &z_indexes
1061 #ifdef TEUCHOS_DEBUG
1064 z_indexes_ = z_indexes;
1065 period_l_map_.resize(0);
1066 const int numTotalParams = Np_ + z_indexes_.size();
1068 z_indexes_itr = z_indexes_.begin(),
1069 z_indexes_end = z_indexes_.end();
1070 int last_z_index = -1;
1071 for (
int k = 0; k < numTotalParams; ++k ) {
1072 if ( z_indexes_itr == z_indexes_end || k < *z_indexes_itr ) {
1074 period_l_map_.push_back(k);
1079 #ifdef TEUCHOS_DEBUG
1082 const int tmp_last_z_index = *z_indexes_itr;
1084 if ( z_indexes_itr != z_indexes_end ) {
1085 #ifdef TEUCHOS_DEBUG
1086 if ( last_z_index >= 0 ) {
1088 *z_indexes_itr <= last_z_index, std::logic_error,
1089 "Error, the z_indexes array = " <<
toString(z_indexes_)
1090 <<
" is not sorted or contains duplicate entries!"
1094 last_z_index = tmp_last_z_index;
1101 template<
class Scalar>
1102 void DefaultMultiPeriodModelEvaluator<Scalar>::wrapNominalValuesAndBounds()
1105 using Teuchos::rcp_dynamic_cast;
1106 typedef ModelEvaluatorBase MEB;
1108 nominalValues_ = this->createInArgs();
1109 lowerBounds_ = this->createInArgs();
1110 upperBounds_ = this->createInArgs();
1112 const MEB::InArgs<Scalar>
1113 periodNominalValues = periodModel_->getNominalValues(),
1114 periodLowerBounds = periodModel_->getLowerBounds(),
1115 periodUpperBounds = periodModel_->getUpperBounds();
1117 if (periodNominalValues.supports(MEB::IN_ARG_x)) {
1119 if( !
is_null(periodNominalValues.get_x()) ) {
1122 RCP<Thyra::ProductVectorBase<Scalar> >
1123 x_bar_init = createProductVector(x_bar_space_);
1124 const int N = this->N();
1125 for (
int i = 0; i < N; ++i ) {
1127 x_bar_init->getNonconstVectorBlock(i).ptr(),
1128 *periodModels_[i]->getNominalValues().get_x()
1131 nominalValues_.set_x(x_bar_init);
1134 if( !
is_null(periodLowerBounds.get_x()) ) {
1137 RCP<Thyra::ProductVectorBase<Scalar> >
1138 x_bar_l = createProductVector(x_bar_space_);
1139 const int N = this->N();
1140 for (
int i = 0; i < N; ++i ) {
1142 x_bar_l->getNonconstVectorBlock(i).ptr(),
1143 *periodModels_[i]->getLowerBounds().get_x()
1146 lowerBounds_.set_x(x_bar_l);
1149 if( !
is_null(periodUpperBounds.get_x()) ) {
1152 RCP<Thyra::ProductVectorBase<Scalar> >
1153 x_bar_u = createProductVector(x_bar_space_);
1154 const int N = this->N();
1155 for (
int i = 0; i < N; ++i ) {
1157 x_bar_u->getNonconstVectorBlock(i).ptr(),
1158 *periodModels_[i]->getUpperBounds().get_x()
1161 upperBounds_.set_x(x_bar_u);
1168 for (
int l = 0; l < Np_; ++l ) {
1169 const int period_l = this->period_l(l);
1170 nominalValues_.set_p(l,periodNominalValues.get_p(period_l));
1171 lowerBounds_.set_p(l,periodLowerBounds.get_p(period_l));
1172 upperBounds_.set_p(l,periodUpperBounds.get_p(period_l));
1178 template<
class Scalar>
1179 RCP<ProductVectorBase<Scalar> >
1180 DefaultMultiPeriodModelEvaluator<Scalar>::createProductVector(
1181 const RCP<
const ProductVectorSpaceBase<Scalar> > &prodVecSpc
1184 return Teuchos::rcp_dynamic_cast<ProductVectorBase<Scalar> >(
1185 Thyra::createMember<Scalar>(prodVecSpc),
true );
1192 #endif // THYRA_DEFAULT_MULTI_PERIOD_MODEL_EVALUATOR_HPP
Pure abstract base interface for evaluating a stateless "model" that can be mapped into a number of d...
Default base class for concrete model evaluators.
bool is_null(const boost::shared_ptr< T > &p)
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
ModelEvaluatorBase::InArgs< Scalar > getLowerBounds() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
RCP< const Array< std::string > > get_p_names(int l) const
ArrayView< const std::string > get_g_names(int j) const
void reset_z(const Array< Array< RCP< const VectorBase< Scalar > > > > &z)
Reset z.
RCP< const VectorSpaceBase< Scalar > > get_p_space(int l) const
Composite subclass that takes a single ModelEvaluator object and represents it as a single aggregate ...
void initialize(const int N, const Array< RCP< ModelEvaluator< Scalar > > > &periodModels, const Array< int > &z_indexes, const Array< Array< RCP< const VectorBase< Scalar > > > > &z, const int g_index, const Array< Scalar > g_weights, const RCP< const ProductVectorSpaceBase< Scalar > > &x_bar_space=Teuchos::null, const RCP< const ProductVectorSpaceBase< Scalar > > &f_bar_space=Teuchos::null, const RCP< LinearOpWithSolveFactoryBase< Scalar > > &W_bar_factory=Teuchos::null)
Initialize.
ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
Factory interface for creating LinearOpWithSolveBase objects from compatible LinearOpBase objects...
ModelEvaluatorBase::InArgs< Scalar > getUpperBounds() const
ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
RCP< const LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
TypeTo implicit_cast(const TypeFrom &t)
Abstract interface for finite-dimensional dense vectors.
TEUCHOSCORE_LIB_DLL_EXPORT std::string toString(const EVerbosityLevel verbLevel)
RCP< const VectorSpaceBase< Scalar > > get_g_space(int j) const
void reportFinalPoint(const ModelEvaluatorBase::InArgs< Scalar > &finalPoint, const bool wasSolved)
Ignores the final point.
RCP< const VectorSpaceBase< Scalar > > get_f_space() const
std::vector< int >::const_iterator const_iterator
Base subclass for ModelEvaluator that defines some basic types.
RCP< LinearOpBase< Scalar > > create_W_op() const
RCP< const VectorSpaceBase< Scalar > > get_x_space() const
RCP< PreconditionerBase< Scalar > > create_W_prec() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
DefaultMultiPeriodModelEvaluator()
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...