44 #include "Teuchos_implicit_cast.hpp"
45 #include "Epetra_RowMatrix.h"
67 const std::string fwdScalingVecName =
"fwdScalingVec";
71 void assertModelVarScalings(
76 TEUCHOS_TEST_FOR_EXCEPTION(
77 (varScalings.
supports(EME::IN_ARG_x) && varScalings.
supports(EME::IN_ARG_x_dot))
80 "Error, if scaling for x is given and x_dot is supported, then\n"
81 "the scaling for x_dot must also be set and must be the same scaling\n"
90 template<
class InArgsVectorGetterSetter>
92 InArgsVectorGetterSetter vecGetterSetter,
96 Teuchos::FancyOStream *out,
97 Teuchos::EVerbosityLevel verbLevel
105 using Teuchos::rcp_const_cast;
109 TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
112 RCP<const Epetra_Vector>
113 orig_vec = vecGetterSetter.getVector(origVars);
114 if ( !is_null(orig_vec) ) {
115 RCP<const Epetra_Vector>
116 inv_s_vec = vecGetterSetter.getVector(varScalings);
117 if ( !is_null(inv_s_vec) ) {
119 scaled_vec = rcp_const_cast<Epetra_Vector>(
120 vecGetterSetter.getVector(*scaledVars) );
121 if ( is_null(scaled_vec) )
122 scaled_vec = rcp(
new Epetra_Vector(orig_vec->Map()));
124 Ptr<const RCP<const Epetra_Vector> > fwd_s_vec =
125 Teuchos::getOptionalEmbeddedObj<Epetra_Vector, RCP<const Epetra_Vector> >(
131 if ( !is_null(fwd_s_vec) ) {
133 scaled_vec->Multiply( 1.0, **fwd_s_vec, *orig_vec, 0.0 );
138 *orig_vec, *inv_s_vec, &*scaled_vec );
140 vecGetterSetter.setVector( scaled_vec, scaledVars );
143 vecGetterSetter.setVector( orig_vec, scaledVars );
147 vecGetterSetter.setVector( null, scaledVars );
155 template<
class InArgsVectorGetterSetter>
156 void scaleModelBound(
157 InArgsVectorGetterSetter vecGetterSetter,
164 Teuchos::FancyOStream *out,
165 Teuchos::EVerbosityLevel verbLevel
172 using Teuchos::rcp_const_cast;
175 TEUCHOS_TEST_FOR_EXCEPT(!scaledLowerBounds);
176 TEUCHOS_TEST_FOR_EXCEPT(!scaledUpperBounds);
179 RCP<const Epetra_Vector>
180 orig_lower_vec = vecGetterSetter.getVector(origLowerBounds);
181 if ( !is_null(orig_lower_vec) ) {
182 RCP<const Epetra_Vector>
183 inv_s_vec = vecGetterSetter.getVector(varScalings);
184 if ( !is_null(inv_s_vec) ) {
185 TEUCHOS_TEST_FOR_EXCEPT(
"Can't handle scaling bounds yet!");
188 vecGetterSetter.setVector( orig_lower_vec, scaledLowerBounds );
192 vecGetterSetter.setVector( null, scaledLowerBounds );
195 RCP<const Epetra_Vector>
196 orig_upper_vec = vecGetterSetter.getVector(origUpperBounds);
197 if ( !is_null(orig_upper_vec) ) {
198 RCP<const Epetra_Vector>
199 inv_s_vec = vecGetterSetter.getVector(varScalings);
200 if ( !is_null(inv_s_vec) ) {
201 TEUCHOS_TEST_FOR_EXCEPT(
"Can't handle scaling bounds yet!");
204 vecGetterSetter.setVector( orig_upper_vec, scaledUpperBounds );
208 vecGetterSetter.setVector( null, scaledUpperBounds );
216 template<
class InArgsVectorGetterSetter>
217 void unscaleModelVar(
218 InArgsVectorGetterSetter vecGetterSetter,
222 Teuchos::FancyOStream *out,
223 Teuchos::EVerbosityLevel verbLevel
230 using Teuchos::rcp_const_cast;
231 using Teuchos::includesVerbLevel;
235 TEUCHOS_TEST_FOR_EXCEPT(!origVars);
238 RCP<const Epetra_Vector>
239 scaled_vec = vecGetterSetter.getVector(scaledVars);
240 if ( !is_null(scaled_vec) ) {
241 RCP<const Epetra_Vector>
242 inv_s_vec = vecGetterSetter.getVector(varScalings);
243 if ( !is_null(inv_s_vec) ) {
245 orig_vec = rcp_const_cast<Epetra_Vector>(
246 vecGetterSetter.getVector(*origVars) );
247 if ( is_null(orig_vec) )
248 orig_vec = rcp(
new Epetra_Vector(scaled_vec->Map()));
250 *scaled_vec, *inv_s_vec, &*orig_vec );
251 if (out && includesVerbLevel(verbLevel,Teuchos::VERB_HIGH)) {
252 *out <<
"\nUnscaled vector "<<vecGetterSetter.getName()<<
":\n";
253 Teuchos::OSTab tab(*out);
254 orig_vec->Print(*out);
256 vecGetterSetter.setVector( orig_vec, origVars );
259 vecGetterSetter.setVector( scaled_vec, origVars );
263 vecGetterSetter.setVector( null, origVars );
271 template<
class OutArgsVectorGetterSetter>
273 OutArgsVectorGetterSetter vecGetterSetter,
277 Teuchos::FancyOStream *out,
278 Teuchos::EVerbosityLevel verbLevel
281 TEUCHOS_TEST_FOR_EXCEPT(0==scaledFuncs);
282 Teuchos::RCP<Epetra_Vector>
283 func = vecGetterSetter.getVector(origFuncs);
284 if (!is_null(func) ) {
285 Teuchos::RCP<const Epetra_Vector>
286 funcScaling = vecGetterSetter.getVector(funcScalings);
287 if (!is_null(funcScaling) ) {
291 vecGetterSetter.setVector( func, scaledFuncs );
304 using Teuchos::implicit_cast;
308 TEUCHOS_TEST_FOR_EXCEPT(!nominalValues);
313 if(nominalValues->
supports(EME::IN_ARG_x)) {
317 if(nominalValues->
supports(EME::IN_ARG_x_dot)) {
321 for(
int l = 0; l < nominalValues->
Np(); ++l ) {
325 if(nominalValues->
supports(EME::IN_ARG_t)) {
339 using Teuchos::implicit_cast;
343 TEUCHOS_TEST_FOR_EXCEPT(!lowerBounds);
344 TEUCHOS_TEST_FOR_EXCEPT(!upperBounds);
350 if(lowerBounds->
supports(EME::IN_ARG_x)) {
355 for(
int l = 0; l < lowerBounds->
Np(); ++l ) {
360 if(lowerBounds->
supports(EME::IN_ARG_t)) {
372 Teuchos::FancyOStream *out,
373 Teuchos::EVerbosityLevel verbLevel
379 TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
380 assertModelVarScalings(varScalings);
383 if (origVars.
supports(EME::IN_ARG_x)) {
388 if (origVars.
supports(EME::IN_ARG_x_dot)) {
393 const int Np = origVars.
Np();
394 for (
int l = 0; l < Np; ++l ) {
399 if (origVars.
supports(EME::IN_ARG_x_poly)) {
400 TEUCHOS_TEST_FOR_EXCEPTION(
401 !is_null(varScalings.
get_x()), std::logic_error,
402 "Error, can't hanlde scaling of x_poly yet!"
407 if (origVars.
supports(EME::IN_ARG_x_dot_poly)) {
408 TEUCHOS_TEST_FOR_EXCEPTION(
409 !is_null(varScalings.
get_x()), std::logic_error,
410 "Error, can't hanlde scaling of x_dot_poly yet!"
415 if (origVars.
supports(EME::IN_ARG_t)) {
416 TEUCHOS_TEST_FOR_EXCEPTION(
417 varScalings.
get_t() > 0.0, std::logic_error,
418 "Error, can't hanlde scaling of t yet!"
423 if (origVars.
supports(EME::IN_ARG_alpha)) {
424 TEUCHOS_TEST_FOR_EXCEPTION(
425 varScalings.
get_alpha() > 0.0, std::logic_error,
426 "Error, can't hanlde scaling of alpha yet!"
431 if (origVars.
supports(EME::IN_ARG_beta)) {
432 TEUCHOS_TEST_FOR_EXCEPTION(
433 varScalings.
get_beta() > 0.0, std::logic_error,
434 "Error, can't hanlde scaling of beta yet!"
451 Teuchos::FancyOStream *out,
452 Teuchos::EVerbosityLevel verbLevel
459 TEUCHOS_TEST_FOR_EXCEPT(!scaledLowerBounds);
460 TEUCHOS_TEST_FOR_EXCEPT(!scaledUpperBounds);
461 assertModelVarScalings(varScalings);
464 if (origLowerBounds.
supports(EME::IN_ARG_x)) {
467 varScalings, scaledLowerBounds, scaledUpperBounds,
471 if (origLowerBounds.
supports(EME::IN_ARG_x_dot)) {
474 varScalings, scaledLowerBounds, scaledUpperBounds,
478 const int Np = origLowerBounds.
Np();
479 for (
int l = 0; l < Np; ++l ) {
482 varScalings, scaledLowerBounds, scaledUpperBounds,
495 Teuchos::FancyOStream *out,
496 Teuchos::EVerbosityLevel verbLevel
501 using Teuchos::includesVerbLevel;
505 TEUCHOS_TEST_FOR_EXCEPT(!origVars);
506 assertModelVarScalings(varScalings);
511 if (out && includesVerbLevel(verbLevel,Teuchos::VERB_HIGH)) {
512 RCP<const Epetra_Vector> inv_s_x;
513 if ( scaledVars.
supports(EME::IN_ARG_x) &&
514 !is_null(inv_s_x=varScalings.
get_x()) )
516 *out <<
"\nState inverse scaling vector inv_s_x:\n";
517 Teuchos::OSTab tab(*out);
518 inv_s_x->Print(*out);
524 if (scaledVars.
supports(EME::IN_ARG_x_dot)) {
529 if (scaledVars.
supports(EME::IN_ARG_x)) {
534 const int Np = scaledVars.
Np();
535 for (
int l = 0; l < Np; ++l ) {
540 if (scaledVars.
supports(EME::IN_ARG_x_dot_poly)) {
541 TEUCHOS_TEST_FOR_EXCEPTION(
542 !is_null(varScalings.
get_x()), std::logic_error,
543 "Error, can't hanlde unscaling of x_dot_poly yet!"
548 if (scaledVars.
supports(EME::IN_ARG_x_poly)) {
549 TEUCHOS_TEST_FOR_EXCEPTION(
550 !is_null(varScalings.
get_x()), std::logic_error,
551 "Error, can't hanlde unscaling of x_poly yet!"
556 if (scaledVars.
supports(EME::IN_ARG_t)) {
557 TEUCHOS_TEST_FOR_EXCEPTION(
558 varScalings.
get_t() > 0.0, std::logic_error,
559 "Error, can't hanlde unscaling of t yet!"
564 if (scaledVars.
supports(EME::IN_ARG_alpha)) {
565 TEUCHOS_TEST_FOR_EXCEPTION(
566 varScalings.
get_alpha() > 0.0, std::logic_error,
567 "Error, can't hanlde unscaling of alpha yet!"
572 if (scaledVars.
supports(EME::IN_ARG_beta)) {
573 TEUCHOS_TEST_FOR_EXCEPTION(
574 varScalings.
get_beta() > 0.0, std::logic_error,
575 "Error, can't hanlde unscaling of beta yet!"
588 bool *allFuncsWhereScaled,
589 Teuchos::FancyOStream *out,
590 Teuchos::EVerbosityLevel verbLevel
597 TEUCHOS_TEST_FOR_EXCEPT(0==allFuncsWhereScaled);
599 *allFuncsWhereScaled =
true;
601 const int Np = origFuncs.
Np();
602 const int Ng = origFuncs.
Ng();
605 if ( origFuncs.
supports(EME::OUT_ARG_f) && !is_null(origFuncs.
get_f()) ) {
612 origFuncs.
supports(EME::OUT_ARG_f_poly)
616 TEUCHOS_TEST_FOR_EXCEPTION(
617 !is_null(funcScalings.
get_f()), std::logic_error,
618 "Error, we can't handle scaling of f_poly yet!"
624 for (
int j = 0; j < Ng; ++j ) {
630 RCP<Epetra_Operator> W;
631 if ( origFuncs.
supports(EME::OUT_ARG_W) && !is_null(W=origFuncs.
get_W()) ) {
632 bool didScaling =
false;
634 varScalings.
get_x().get(), funcScalings.
get_f().get(),
638 scaledFuncs->
set_W(W);
640 *allFuncsWhereScaled =
false;
644 for (
int l = 0; l < Np; ++l ) {
645 EME::Derivative orig_DfDp_l;
647 !origFuncs.
supports(EME::OUT_ARG_DfDp,l).none()
648 && !(orig_DfDp_l=origFuncs.
get_DfDp(l)).isEmpty()
651 EME::Derivative scaled_DfDp_l;
652 bool didScaling =
false;
654 orig_DfDp_l, varScalings.
get_p(l).get(), funcScalings.
get_f().get(),
655 &scaled_DfDp_l, &didScaling
658 scaledFuncs->
set_DfDp(l,scaled_DfDp_l);
660 *allFuncsWhereScaled =
false;
666 for (
int j = 0; j < Ng; ++j ) {
668 EME::Derivative orig_DgDx_dot_j;
670 !origFuncs.
supports(EME::OUT_ARG_DgDx_dot,j).none()
671 && !(orig_DgDx_dot_j=origFuncs.
get_DgDx_dot(j)).isEmpty()
674 EME::Derivative scaled_DgDx_dot_j;
675 bool didScaling =
false;
677 orig_DgDx_dot_j, varScalings.
get_x().get(), funcScalings.
get_g(j).get(),
678 &scaled_DgDx_dot_j, &didScaling
683 *allFuncsWhereScaled =
false;
686 EME::Derivative orig_DgDx_j;
688 !origFuncs.
supports(EME::OUT_ARG_DgDx,j).none()
689 && !(orig_DgDx_j=origFuncs.
get_DgDx(j)).isEmpty()
692 EME::Derivative scaled_DgDx_j;
693 bool didScaling =
false;
695 orig_DgDx_j, varScalings.
get_x().get(), funcScalings.
get_g(j).get(),
696 &scaled_DgDx_j, &didScaling
699 scaledFuncs->
set_DgDx(j,scaled_DgDx_j);
701 *allFuncsWhereScaled =
false;
704 for (
int l = 0; l < Np; ++l ) {
705 EME::Derivative orig_DgDp_j_l;
707 !origFuncs.
supports(EME::OUT_ARG_DgDp,j,l).none()
708 && !(orig_DgDp_j_l=origFuncs.
get_DgDp(j,l)).isEmpty()
711 EME::Derivative scaled_DgDp_j_l;
712 bool didScaling =
false;
714 orig_DgDp_j_l, varScalings.
get_p(l).get(), funcScalings.
get_g(j).get(),
715 &scaled_DgDp_j_l, &didScaling
718 scaledFuncs->
set_DgDp(j,l,scaled_DgDp_j_l);
720 *allFuncsWhereScaled =
false;
728 Teuchos::RCP<const Epetra_Vector>
730 Teuchos::RCP<const Epetra_Vector>
const& scalingVector
733 Teuchos::RCP<Epetra_Vector> invScalingVector =
734 Teuchos::rcpWithEmbeddedObj(
735 new Epetra_Vector(scalingVector->Map()),
738 invScalingVector->Reciprocal(*scalingVector);
739 return invScalingVector;
747 const Epetra_Vector &origVars,
748 const Epetra_Vector &invVarScaling,
749 Epetra_Vector *scaledVars
753 TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
754 TEUCHOS_TEST_FOR_EXCEPT(!origVars.Map().SameAs(invVarScaling.Map()));
755 TEUCHOS_TEST_FOR_EXCEPT(!origVars.Map().SameAs(scaledVars->Map()));
757 const int localDim = origVars.Map().NumMyElements();
758 for (
int i = 0; i < localDim; ++i )
759 (*scaledVars)[i] = origVars[i] / invVarScaling[i];
764 const Epetra_Vector &origLowerBounds,
765 const Epetra_Vector &origUpperBounds,
767 const Epetra_Vector &invVarScaling,
768 Epetra_Vector *scaledLowerBounds,
769 Epetra_Vector *scaledUpperBounds
772 TEUCHOS_TEST_FOR_EXCEPT(
"ToDo: Implement!");
777 const Epetra_Vector &origVars,
778 const Epetra_Vector &invVarScaling,
779 Epetra_Vector *scaledVars
782 TEUCHOS_TEST_FOR_EXCEPT(0==scaledVars);
783 scaledVars->Multiply( 1.0, invVarScaling, origVars, 0.0 );
788 const Epetra_Vector &fwdFuncScaling,
792 TEUCHOS_TEST_FOR_EXCEPT(0==funcs);
793 funcs->Multiply( 1.0, fwdFuncScaling, *funcs, 0.0 );
800 const Epetra_Vector *invVarScaling,
801 const Epetra_Vector *fwdFuncScaling,
802 Epetra_Operator *funcDerivOp,
806 TEUCHOS_TEST_FOR_EXCEPT(0==funcDerivOp);
807 TEUCHOS_TEST_FOR_EXCEPT(0==didScaling);
809 Epetra_RowMatrix *funcDerivRowMatrix
810 =
dynamic_cast<Epetra_RowMatrix*
>(funcDerivOp);
811 if (funcDerivRowMatrix) {
813 funcDerivRowMatrix->LeftScale(*fwdFuncScaling);
815 funcDerivRowMatrix->RightScale(*invVarScaling);
825 const Epetra_Vector *invVarScaling,
826 const Epetra_Vector *fwdFuncScaling,
833 TEUCHOS_TEST_FOR_EXCEPT(0==scaledFuncDeriv);
834 TEUCHOS_TEST_FOR_EXCEPT(0==didScaling);
836 const RCP<Epetra_MultiVector>
838 const EME::EDerivativeMultiVectorOrientation
840 if(!is_null(funcDerivMv)) {
841 if ( funcDerivMv_orientation == EME::DERIV_MV_BY_COL )
843 if (fwdFuncScaling) {
844 funcDerivMv->Multiply(1.0, *fwdFuncScaling, *funcDerivMv, 0.0);
847 TEUCHOS_TEST_FOR_EXCEPT(
"ToDo: Scale rows!");
851 else if ( funcDerivMv_orientation == EME::DERIV_TRANS_MV_BY_ROW )
854 funcDerivMv->Multiply(1.0, *invVarScaling, *funcDerivMv, 0.0);
856 if (fwdFuncScaling) {
857 TEUCHOS_TEST_FOR_EXCEPT(
"ToDo: Scale rows!");
862 TEUCHOS_TEST_FOR_EXCEPT(
"Should not get here!");
864 *scaledFuncDeriv = EME::Derivative(funcDerivMv,funcDerivMv_orientation);
870 TEUCHOS_TEST_FOR_EXCEPT(is_null(funcDerivOp));
872 &*funcDerivOp, didScaling );
874 *scaledFuncDeriv = EME::Derivative(funcDerivOp);
Class that gets and sets p(l) in an InArgs object.
bool supports(EOutArgsMembers arg) const
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_x_lower_bounds() const
void set_x_dot_poly(const Teuchos::RefCountPtr< const Teuchos::Polynomial< Epetra_Vector > > &x_dot_poly)
Set time derivative vector Taylor polynomial.
void set_W(const Teuchos::RefCountPtr< Epetra_Operator > &W)
virtual InArgs createInArgs() const =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::RefCountPtr< const Epetra_Vector > get_x_dot() const
Teuchos::RefCountPtr< Epetra_MultiVector > getMultiVector() const
void set_beta(double beta)
Teuchos::RefCountPtr< Teuchos::Polynomial< Epetra_Vector > > get_f_poly() const
Get residual vector Taylor polynomial.
virtual double get_t_upper_bound() const
void set_DfDp(int l, const Derivative &DfDp_l)
Derivative get_DgDx_dot(int j) const
Teuchos::RefCountPtr< const Teuchos::Polynomial< Epetra_Vector > > get_x_poly() const
Get solution vector Taylor polynomial.
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
void set_x_dot(const Teuchos::RefCountPtr< const Epetra_Vector > &x_dot)
void set_DgDx_dot(int j, const Derivative &DgDx_dot_j)
Teuchos::RefCountPtr< const Epetra_Vector > get_p(int l) const
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_p_init(int l) const
Teuchos::RefCountPtr< const Epetra_Vector > get_x() const
Set solution vector Taylor polynomial.
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_p_upper_bounds(int l) const
Derivative get_DgDp(int j, int l) const
Class that gets and sets f in an OutArgs object.
bool supports(EInArgsMembers arg) const
void set_x(const Teuchos::RefCountPtr< const Epetra_Vector > &x)
void set_f_poly(const Teuchos::RefCountPtr< Teuchos::Polynomial< Epetra_Vector > > &f_poly)
Set residual vector Taylor polynomial.
Class that gets and sets g(j) in an OutArgs object.
void set_DgDp(int j, int l, const Derivative &DgDp_j_l)
Teuchos::RefCountPtr< Epetra_Operator > get_W() const
EDerivativeMultiVectorOrientation getMultiVectorOrientation() const
virtual double get_t_lower_bound() const
void set_alpha(double alpha)
Class that gets and sets x in an InArgs object.
virtual double get_t_init() const
Teuchos::RefCountPtr< Epetra_Operator > getLinearOp() const
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_p_lower_bounds(int l) const
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_x_dot_init() const
void set_p(int l, const Teuchos::RefCountPtr< const Epetra_Vector > &p_l)
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_x_upper_bounds() const
Teuchos::RefCountPtr< const Teuchos::Polynomial< Epetra_Vector > > get_x_dot_poly() const
Get time derivative vector Taylor polynomial.
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_x_init() const
Evaluation< Epetra_Vector > get_f() const
Base interface for evaluating a stateless "model".
void set_x_poly(const Teuchos::RefCountPtr< const Teuchos::Polynomial< Epetra_Vector > > &x_poly)