44 #include "Teuchos_implicit_cast.hpp"
45 #include "Epetra_RowMatrix.h"
67 const std::string fwdScalingVecName =
"fwdScalingVec";
75 void assertModelVarScalings(
80 TEUCHOS_TEST_FOR_EXCEPTION(
81 (varScalings.
supports(EME::IN_ARG_x) && varScalings.
supports(EME::IN_ARG_x_dot))
84 "Error, if scaling for x is given and x_dot is supported, then\n"
85 "the scaling for x_dot must also be set and must be the same scaling\n"
88 TEUCHOS_TEST_FOR_EXCEPTION(
89 (varScalings.
supports(EME::IN_ARG_x) && varScalings.
supports(EME::IN_ARG_x_dotdot))
92 "Error, if scaling for x is given and x_dotdot is supported, then\n"
93 "the scaling for x_dotdot must also be set and must be the same scaling\n"
98 #endif // TEUCHOS_DEBUG
103 template<
class InArgsVectorGetterSetter>
105 InArgsVectorGetterSetter vecGetterSetter,
109 Teuchos::FancyOStream * ,
110 Teuchos::EVerbosityLevel
118 using Teuchos::rcp_const_cast;
122 TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
125 RCP<const Epetra_Vector>
126 orig_vec = vecGetterSetter.getVector(origVars);
127 if ( !is_null(orig_vec) ) {
128 RCP<const Epetra_Vector>
129 inv_s_vec = vecGetterSetter.getVector(varScalings);
130 if ( !is_null(inv_s_vec) ) {
133 vecGetterSetter.getVector(*scaledVars) );
134 if ( is_null(scaled_vec) )
137 Ptr<const RCP<const Epetra_Vector> > fwd_s_vec =
138 Teuchos::getOptionalEmbeddedObj<Epetra_Vector, RCP<const Epetra_Vector> >(
144 if ( !is_null(fwd_s_vec) ) {
146 scaled_vec->Multiply( 1.0, **fwd_s_vec, *orig_vec, 0.0 );
151 *orig_vec, *inv_s_vec, &*scaled_vec );
153 vecGetterSetter.setVector( scaled_vec, scaledVars );
156 vecGetterSetter.setVector( orig_vec, scaledVars );
160 vecGetterSetter.setVector( null, scaledVars );
168 template<
class InArgsVectorGetterSetter>
169 void scaleModelBound(
170 InArgsVectorGetterSetter vecGetterSetter,
177 Teuchos::FancyOStream * ,
178 Teuchos::EVerbosityLevel
185 using Teuchos::rcp_const_cast;
188 TEUCHOS_TEST_FOR_EXCEPT(!scaledLowerBounds);
189 TEUCHOS_TEST_FOR_EXCEPT(!scaledUpperBounds);
192 RCP<const Epetra_Vector>
193 orig_lower_vec = vecGetterSetter.getVector(origLowerBounds);
194 if ( !is_null(orig_lower_vec) ) {
195 RCP<const Epetra_Vector>
196 inv_s_vec = vecGetterSetter.getVector(varScalings);
197 if ( !is_null(inv_s_vec) ) {
198 TEUCHOS_TEST_FOR_EXCEPT(
"Can't handle scaling bounds yet!");
201 vecGetterSetter.setVector( orig_lower_vec, scaledLowerBounds );
205 vecGetterSetter.setVector( null, scaledLowerBounds );
208 RCP<const Epetra_Vector>
209 orig_upper_vec = vecGetterSetter.getVector(origUpperBounds);
210 if ( !is_null(orig_upper_vec) ) {
211 RCP<const Epetra_Vector>
212 inv_s_vec = vecGetterSetter.getVector(varScalings);
213 if ( !is_null(inv_s_vec) ) {
214 TEUCHOS_TEST_FOR_EXCEPT(
"Can't handle scaling bounds yet!");
217 vecGetterSetter.setVector( orig_upper_vec, scaledUpperBounds );
221 vecGetterSetter.setVector( null, scaledUpperBounds );
229 template<
class InArgsVectorGetterSetter>
230 void unscaleModelVar(
231 InArgsVectorGetterSetter vecGetterSetter,
235 Teuchos::FancyOStream *out,
236 Teuchos::EVerbosityLevel verbLevel
243 using Teuchos::rcp_const_cast;
244 using Teuchos::includesVerbLevel;
248 TEUCHOS_TEST_FOR_EXCEPT(!origVars);
251 RCP<const Epetra_Vector>
252 scaled_vec = vecGetterSetter.getVector(scaledVars);
253 if ( !is_null(scaled_vec) ) {
254 RCP<const Epetra_Vector>
255 inv_s_vec = vecGetterSetter.getVector(varScalings);
256 if ( !is_null(inv_s_vec) ) {
259 vecGetterSetter.getVector(*origVars) );
260 if ( is_null(orig_vec) )
263 *scaled_vec, *inv_s_vec, &*orig_vec );
264 if (out && includesVerbLevel(verbLevel,Teuchos::VERB_HIGH)) {
265 *out <<
"\nUnscaled vector "<<vecGetterSetter.getName()<<
":\n";
266 Teuchos::OSTab tab(*out);
267 orig_vec->Print(*out);
269 vecGetterSetter.setVector( orig_vec, origVars );
272 vecGetterSetter.setVector( scaled_vec, origVars );
276 vecGetterSetter.setVector( null, origVars );
284 template<
class OutArgsVectorGetterSetter>
286 OutArgsVectorGetterSetter vecGetterSetter,
290 Teuchos::FancyOStream * ,
291 Teuchos::EVerbosityLevel
294 TEUCHOS_TEST_FOR_EXCEPT(0==scaledFuncs);
295 Teuchos::RCP<Epetra_Vector>
296 func = vecGetterSetter.getVector(origFuncs);
297 if (!is_null(func) ) {
298 Teuchos::RCP<const Epetra_Vector>
299 funcScaling = vecGetterSetter.getVector(funcScalings);
300 if (!is_null(funcScaling) ) {
304 vecGetterSetter.setVector( func, scaledFuncs );
317 using Teuchos::implicit_cast;
321 TEUCHOS_TEST_FOR_EXCEPT(!nominalValues);
326 if(nominalValues->
supports(EME::IN_ARG_x)) {
330 if(nominalValues->
supports(EME::IN_ARG_x_dot)) {
334 if(nominalValues->
supports(EME::IN_ARG_x_dotdot)) {
338 for(
int l = 0; l < nominalValues->
Np(); ++l ) {
342 if(nominalValues->
supports(EME::IN_ARG_t)) {
356 using Teuchos::implicit_cast;
360 TEUCHOS_TEST_FOR_EXCEPT(!lowerBounds);
361 TEUCHOS_TEST_FOR_EXCEPT(!upperBounds);
367 if(lowerBounds->
supports(EME::IN_ARG_x)) {
372 for(
int l = 0; l < lowerBounds->
Np(); ++l ) {
377 if(lowerBounds->
supports(EME::IN_ARG_t)) {
389 Teuchos::FancyOStream *out,
390 Teuchos::EVerbosityLevel verbLevel
396 TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
397 assertModelVarScalings(varScalings);
400 if (origVars.
supports(EME::IN_ARG_x)) {
405 if (origVars.
supports(EME::IN_ARG_x_dot)) {
410 if (origVars.
supports(EME::IN_ARG_x_dotdot)) {
415 const int Np = origVars.
Np();
416 for (
int l = 0; l < Np; ++l ) {
421 if (origVars.
supports(EME::IN_ARG_x_poly)) {
422 TEUCHOS_TEST_FOR_EXCEPTION(
423 !is_null(varScalings.
get_x()), std::logic_error,
424 "Error, can't hanlde scaling of x_poly yet!"
429 if (origVars.
supports(EME::IN_ARG_x_dot_poly)) {
430 TEUCHOS_TEST_FOR_EXCEPTION(
431 !is_null(varScalings.
get_x()), std::logic_error,
432 "Error, can't hanlde scaling of x_dot_poly yet!"
437 if (origVars.
supports(EME::IN_ARG_x_dotdot_poly)) {
438 TEUCHOS_TEST_FOR_EXCEPTION(
439 !is_null(varScalings.
get_x()), std::logic_error,
440 "Error, can't hanlde scaling of x_dotdot_poly yet!"
445 if (origVars.
supports(EME::IN_ARG_t)) {
446 TEUCHOS_TEST_FOR_EXCEPTION(
447 varScalings.
get_t() > 0.0, std::logic_error,
448 "Error, can't hanlde scaling of t yet!"
453 if (origVars.
supports(EME::IN_ARG_alpha)) {
454 TEUCHOS_TEST_FOR_EXCEPTION(
455 varScalings.
get_alpha() > 0.0, std::logic_error,
456 "Error, can't hanlde scaling of alpha yet!"
461 if (origVars.
supports(EME::IN_ARG_beta)) {
462 TEUCHOS_TEST_FOR_EXCEPTION(
463 varScalings.
get_beta() > 0.0, std::logic_error,
464 "Error, can't hanlde scaling of beta yet!"
481 Teuchos::FancyOStream *out,
482 Teuchos::EVerbosityLevel verbLevel
489 TEUCHOS_TEST_FOR_EXCEPT(!scaledLowerBounds);
490 TEUCHOS_TEST_FOR_EXCEPT(!scaledUpperBounds);
491 assertModelVarScalings(varScalings);
494 if (origLowerBounds.
supports(EME::IN_ARG_x)) {
497 varScalings, scaledLowerBounds, scaledUpperBounds,
501 if (origLowerBounds.
supports(EME::IN_ARG_x_dot)) {
504 varScalings, scaledLowerBounds, scaledUpperBounds,
508 if (origLowerBounds.
supports(EME::IN_ARG_x_dotdot)) {
511 varScalings, scaledLowerBounds, scaledUpperBounds,
515 const int Np = origLowerBounds.
Np();
516 for (
int l = 0; l < Np; ++l ) {
519 varScalings, scaledLowerBounds, scaledUpperBounds,
532 Teuchos::FancyOStream *out,
533 Teuchos::EVerbosityLevel verbLevel
538 using Teuchos::includesVerbLevel;
542 TEUCHOS_TEST_FOR_EXCEPT(!origVars);
543 assertModelVarScalings(varScalings);
548 if (out && includesVerbLevel(verbLevel,Teuchos::VERB_HIGH)) {
549 RCP<const Epetra_Vector> inv_s_x;
550 if ( scaledVars.
supports(EME::IN_ARG_x) &&
551 !is_null(inv_s_x=varScalings.
get_x()) )
553 *out <<
"\nState inverse scaling vector inv_s_x:\n";
554 Teuchos::OSTab tab(*out);
555 inv_s_x->Print(*out);
561 if (scaledVars.
supports(EME::IN_ARG_x_dot)) {
566 if (scaledVars.
supports(EME::IN_ARG_x_dotdot)) {
571 if (scaledVars.
supports(EME::IN_ARG_x)) {
576 const int Np = scaledVars.
Np();
577 for (
int l = 0; l < Np; ++l ) {
582 if (scaledVars.
supports(EME::IN_ARG_x_dot_poly)) {
583 TEUCHOS_TEST_FOR_EXCEPTION(
584 !is_null(varScalings.
get_x()), std::logic_error,
585 "Error, can't hanlde unscaling of x_dot_poly yet!"
590 if (scaledVars.
supports(EME::IN_ARG_x_dotdot_poly)) {
591 TEUCHOS_TEST_FOR_EXCEPTION(
592 !is_null(varScalings.
get_x()), std::logic_error,
593 "Error, can't hanlde unscaling of x_dotdot_poly yet!"
598 if (scaledVars.
supports(EME::IN_ARG_x_poly)) {
599 TEUCHOS_TEST_FOR_EXCEPTION(
600 !is_null(varScalings.
get_x()), std::logic_error,
601 "Error, can't hanlde unscaling of x_poly yet!"
606 if (scaledVars.
supports(EME::IN_ARG_t)) {
607 TEUCHOS_TEST_FOR_EXCEPTION(
608 varScalings.
get_t() > 0.0, std::logic_error,
609 "Error, can't hanlde unscaling of t yet!"
614 if (scaledVars.
supports(EME::IN_ARG_alpha)) {
615 TEUCHOS_TEST_FOR_EXCEPTION(
616 varScalings.
get_alpha() > 0.0, std::logic_error,
617 "Error, can't hanlde unscaling of alpha yet!"
622 if (scaledVars.
supports(EME::IN_ARG_beta)) {
623 TEUCHOS_TEST_FOR_EXCEPTION(
624 varScalings.
get_beta() > 0.0, std::logic_error,
625 "Error, can't hanlde unscaling of beta yet!"
638 bool *allFuncsWhereScaled,
639 Teuchos::FancyOStream *out,
640 Teuchos::EVerbosityLevel verbLevel
647 TEUCHOS_TEST_FOR_EXCEPT(0==allFuncsWhereScaled);
649 *allFuncsWhereScaled =
true;
651 const int Np = origFuncs.
Np();
652 const int Ng = origFuncs.
Ng();
655 if ( origFuncs.
supports(EME::OUT_ARG_f) && !is_null(origFuncs.
get_f()) ) {
662 origFuncs.
supports(EME::OUT_ARG_f_poly)
666 TEUCHOS_TEST_FOR_EXCEPTION(
667 !is_null(funcScalings.
get_f()), std::logic_error,
668 "Error, we can't handle scaling of f_poly yet!"
674 for (
int j = 0; j < Ng; ++j ) {
680 RCP<Epetra_Operator> W;
681 if ( origFuncs.
supports(EME::OUT_ARG_W) && !is_null(W=origFuncs.
get_W()) ) {
682 bool didScaling =
false;
684 varScalings.
get_x().get(), funcScalings.
get_f().get(),
688 scaledFuncs->
set_W(W);
690 *allFuncsWhereScaled =
false;
694 for (
int l = 0; l < Np; ++l ) {
695 EME::Derivative orig_DfDp_l;
697 !origFuncs.
supports(EME::OUT_ARG_DfDp,l).none()
698 && !(orig_DfDp_l=origFuncs.
get_DfDp(l)).isEmpty()
701 EME::Derivative scaled_DfDp_l;
702 bool didScaling =
false;
704 orig_DfDp_l, varScalings.
get_p(l).get(), funcScalings.
get_f().get(),
705 &scaled_DfDp_l, &didScaling
708 scaledFuncs->
set_DfDp(l,scaled_DfDp_l);
710 *allFuncsWhereScaled =
false;
716 for (
int j = 0; j < Ng; ++j ) {
718 EME::Derivative orig_DgDx_dot_j;
720 !origFuncs.
supports(EME::OUT_ARG_DgDx_dot,j).none()
721 && !(orig_DgDx_dot_j=origFuncs.
get_DgDx_dot(j)).isEmpty()
724 EME::Derivative scaled_DgDx_dot_j;
725 bool didScaling =
false;
727 orig_DgDx_dot_j, varScalings.
get_x().get(), funcScalings.
get_g(j).get(),
728 &scaled_DgDx_dot_j, &didScaling
733 *allFuncsWhereScaled =
false;
736 EME::Derivative orig_DgDx_dotdot_j;
738 !origFuncs.
supports(EME::OUT_ARG_DgDx_dotdot,j).none()
742 EME::Derivative scaled_DgDx_dotdot_j;
743 bool didScaling =
false;
745 orig_DgDx_dotdot_j, varScalings.
get_x().get(), funcScalings.
get_g(j).get(),
746 &scaled_DgDx_dotdot_j, &didScaling
751 *allFuncsWhereScaled =
false;
754 EME::Derivative orig_DgDx_j;
756 !origFuncs.
supports(EME::OUT_ARG_DgDx,j).none()
757 && !(orig_DgDx_j=origFuncs.
get_DgDx(j)).isEmpty()
760 EME::Derivative scaled_DgDx_j;
761 bool didScaling =
false;
763 orig_DgDx_j, varScalings.
get_x().get(), funcScalings.
get_g(j).get(),
764 &scaled_DgDx_j, &didScaling
767 scaledFuncs->
set_DgDx(j,scaled_DgDx_j);
769 *allFuncsWhereScaled =
false;
772 for (
int l = 0; l < Np; ++l ) {
773 EME::Derivative orig_DgDp_j_l;
775 !origFuncs.
supports(EME::OUT_ARG_DgDp,j,l).none()
776 && !(orig_DgDp_j_l=origFuncs.
get_DgDp(j,l)).isEmpty()
779 EME::Derivative scaled_DgDp_j_l;
780 bool didScaling =
false;
782 orig_DgDp_j_l, varScalings.
get_p(l).get(), funcScalings.
get_g(j).get(),
783 &scaled_DgDp_j_l, &didScaling
786 scaledFuncs->
set_DgDp(j,l,scaled_DgDp_j_l);
788 *allFuncsWhereScaled =
false;
796 Teuchos::RCP<const Epetra_Vector>
798 Teuchos::RCP<const Epetra_Vector>
const& scalingVector
801 Teuchos::RCP<Epetra_Vector> invScalingVector =
802 Teuchos::rcpWithEmbeddedObj(
806 invScalingVector->Reciprocal(*scalingVector);
807 return invScalingVector;
821 TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
822 TEUCHOS_TEST_FOR_EXCEPT(!origVars.Map().SameAs(invVarScaling.Map()));
823 TEUCHOS_TEST_FOR_EXCEPT(!origVars.Map().SameAs(scaledVars->Map()));
825 const int localDim = origVars.Map().NumMyElements();
826 for (
int i = 0; i < localDim; ++i )
827 (*scaledVars)[i] = origVars[i] / invVarScaling[i];
840 TEUCHOS_TEST_FOR_EXCEPT(
"ToDo: Implement!");
850 TEUCHOS_TEST_FOR_EXCEPT(0==scaledVars);
851 scaledVars->Multiply( 1.0, invVarScaling, origVars, 0.0 );
860 TEUCHOS_TEST_FOR_EXCEPT(0==funcs);
861 funcs->Multiply( 1.0, fwdFuncScaling, *funcs, 0.0 );
874 TEUCHOS_TEST_FOR_EXCEPT(0==funcDerivOp);
875 TEUCHOS_TEST_FOR_EXCEPT(0==didScaling);
879 if (funcDerivRowMatrix) {
881 funcDerivRowMatrix->
LeftScale(*fwdFuncScaling);
883 funcDerivRowMatrix->
RightScale(*invVarScaling);
901 TEUCHOS_TEST_FOR_EXCEPT(0==scaledFuncDeriv);
902 TEUCHOS_TEST_FOR_EXCEPT(0==didScaling);
904 const RCP<Epetra_MultiVector>
906 const EME::EDerivativeMultiVectorOrientation
908 if(!is_null(funcDerivMv)) {
909 if ( funcDerivMv_orientation == EME::DERIV_MV_BY_COL )
911 if (fwdFuncScaling) {
912 funcDerivMv->Multiply(1.0, *fwdFuncScaling, *funcDerivMv, 0.0);
915 TEUCHOS_TEST_FOR_EXCEPT(
"ToDo: Scale rows!");
919 else if ( funcDerivMv_orientation == EME::DERIV_TRANS_MV_BY_ROW )
922 funcDerivMv->Multiply(1.0, *invVarScaling, *funcDerivMv, 0.0);
924 if (fwdFuncScaling) {
925 TEUCHOS_TEST_FOR_EXCEPT(
"ToDo: Scale rows!");
930 TEUCHOS_TEST_FOR_EXCEPT(
"Should not get here!");
932 *scaledFuncDeriv = EME::Derivative(funcDerivMv,funcDerivMv_orientation);
938 TEUCHOS_TEST_FOR_EXCEPT(is_null(funcDerivOp));
940 &*funcDerivOp, didScaling );
942 *scaledFuncDeriv = EME::Derivative(funcDerivOp);
Class that gets and sets p(l) in an InArgs object.
Teuchos::RCP< const Teuchos::Polynomial< Epetra_Vector > > get_x_dot_poly() const
Get time derivative vector Taylor polynomial.
bool supports(EOutArgsMembers arg) const
virtual InArgs createInArgs() const =0
virtual int RightScale(const Epetra_Vector &x)=0
Evaluation< Epetra_Vector > get_g(int j) const
Get g(j) where 0 <= j && j < this->Ng().
void set_DgDx(int j, const Derivative &DgDx_j)
Derivative get_DfDp(int l) const
Teuchos::RCP< const Epetra_Vector > get_x_dotdot() const
void set_x_dotdot_poly(const Teuchos::RCP< const Teuchos::Polynomial< Epetra_Vector > > &x_dotdot_poly)
virtual Teuchos::RCP< const Epetra_Vector > get_p_init(int l) const
void set_beta(double beta)
Teuchos::RCP< Teuchos::Polynomial< Epetra_Vector > > get_f_poly() const
Get residual vector Taylor polynomial.
void set_x(const Teuchos::RCP< const Epetra_Vector > &x)
void set_x_dot(const Teuchos::RCP< const Epetra_Vector > &x_dot)
virtual Teuchos::RCP< const Epetra_Vector > get_p_upper_bounds(int l) const
virtual double get_t_upper_bound() const
Teuchos::RCP< Epetra_Operator > get_W() const
void set_DfDp(int l, const Derivative &DfDp_l)
void set_DgDx_dotdot(int j, const Derivative &DgDx_dotdot_j)
Derivative get_DgDx_dot(int j) const
Class that gets and sets x_dotdot in an InArgs object.
Teuchos::RCP< const Teuchos::Polynomial< Epetra_Vector > > get_x_poly() const
Get solution vector Taylor polynomial.
Teuchos::RCP< Epetra_Operator > getLinearOp() const
Teuchos::RCP< const Epetra_Vector > get_p(int l) const
virtual int LeftScale(const Epetra_Vector &x)=0
Simple aggregate class that stores a derivative object as a general linear operator or as a multi-vec...
Class that gets and sets x_dot in an InArgs object.
Derivative get_DgDx(int j) const
virtual Teuchos::RCP< const Epetra_Vector > get_x_upper_bounds() const
void set_x_dotdot(const Teuchos::RCP< const Epetra_Vector > &x_dotdot)
void set_DgDx_dot(int j, const Derivative &DgDx_dot_j)
Teuchos::RCP< Epetra_MultiVector > getMultiVector() const
virtual Teuchos::RCP< const Epetra_Vector > get_x_lower_bounds() const
Teuchos::RCP< const Teuchos::Polynomial< Epetra_Vector > > get_x_dotdot_poly() const
virtual Teuchos::RCP< const Epetra_Vector > get_x_dotdot_init() const
void set_p(int l, const Teuchos::RCP< const Epetra_Vector > &p_l)
Derivative get_DgDp(int j, int l) const
Class that gets and sets f in an OutArgs object.
Teuchos::RCP< const Epetra_Vector > get_x_dot() const
bool supports(EInArgsMembers arg) const
void set_W(const Teuchos::RCP< Epetra_Operator > &W)
virtual Teuchos::RCP< const Epetra_Vector > get_x_dot_init() const
Class that gets and sets g(j) in an OutArgs object.
Derivative get_DgDx_dotdot(int j) const
void set_DgDp(int j, int l, const Derivative &DgDp_j_l)
void set_f_poly(const Teuchos::RCP< Teuchos::Polynomial< Epetra_Vector > > &f_poly)
Set residual vector Taylor polynomial.
EDerivativeMultiVectorOrientation getMultiVectorOrientation() const
void set_x_poly(const Teuchos::RCP< const Teuchos::Polynomial< Epetra_Vector > > &x_poly)
virtual Teuchos::RCP< const Epetra_Vector > get_p_lower_bounds(int l) const
virtual double get_t_lower_bound() const
void set_alpha(double alpha)
Class that gets and sets x in an InArgs object.
virtual Teuchos::RCP< const Epetra_Vector > get_x_init() const
virtual double get_t_init() const
void set_x_dot_poly(const Teuchos::RCP< const Teuchos::Polynomial< Epetra_Vector > > &x_dot_poly)
Set time derivative vector Taylor polynomial.
Evaluation< Epetra_Vector > get_f() const
Teuchos::RCP< const Epetra_Vector > get_x() const
Set solution vector Taylor polynomial.
Base interface for evaluating a stateless "model".