47 #include "NLPInterfacePack_NLPThyraModelEvaluatorBase.hpp"
48 #include "AbstractLinAlgPack_LinAlgOpPack.hpp"
49 #include "AbstractLinAlgPack_VectorOut.hpp"
50 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
51 #include "AbstractLinAlgPack_VectorSpaceThyra.hpp"
52 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
53 #include "AbstractLinAlgPack_MatrixOpNonsingThyra.hpp"
54 #include "AbstractLinAlgPack_BasisSystemComposite.hpp"
55 #include "AbstractLinAlgPack_VectorSpaceBlocked.hpp"
56 #include "AbstractLinAlgPack_VectorAuxiliaryOps.hpp"
57 #include "AbstractLinAlgPack_MatrixSymPosDefCholFactor.hpp"
58 #include "Thyra_DetachedVectorView.hpp"
59 #include "Thyra_VectorStdOps.hpp"
60 #include "Teuchos_AbstractFactoryStd.hpp"
61 #include "Teuchos_Assert.hpp"
62 #include "Teuchos_dyn_cast.hpp"
64 namespace NLPInterfacePack {
70 x_guess_bounds_updated_ =
false;
101 return num_bounded_x_;
111 return force_xinit_in_bounds_;
166 ,
const Vector* lambda
175 MEB::InArgs<value_type> model_finalPoint = model_->createInArgs();
176 if( basis_sys_.
get() ) {
178 var_dep = basis_sys_->var_dep(),
179 var_indep = basis_sys_->var_indep();
180 RCP<const Vector> xD = x.sub_view(var_dep), xI;
181 if(p_idx_>=0) xI = x.sub_view(var_indep);
182 model_finalPoint.set_x(dyn_cast<const VectorMutableThyra>(*xD).thyra_vec().assert_not_null());
184 model_finalPoint.set_p(p_idx_,dyn_cast<const VectorMutableThyra>(*xI).thyra_vec().assert_not_null());
185 else if( model_finalPoint.Np() >= 1 )
186 model_finalPoint.set_p(0,model_->getNominalValues().get_p(0));
190 model_finalPoint.set_p(p_idx_,dyn_cast<const VectorMutableThyra>(x).thyra_vec().assert_not_null());
192 model_->reportFinalPoint(model_finalPoint,optimal);
199 NLPObjGrad::set_Gf(Gf);
206 const Vector& x,
bool newx
214 const Vector& x,
bool newx
224 const Vector& x,
bool newx
234 :showModelEvaluatorTrace_(false),initialized_(false)
235 ,obj_scale_(1.0),has_bounds_(false)
236 ,force_xinit_in_bounds_(true),num_bounded_x_(0)
237 ,x_guess_bounds_updated_(false)
251 typedef ::Thyra::ModelEvaluatorBase MEB;
253 initialized_ =
false;
254 x_guess_bounds_updated_ =
false;
255 model_g_updated_ = model_Dg_updated_ = f_updated_ = c_updated_ = Gf_updated_ = Gc_updated_ =
false;
257 const char msg_err[] =
"NLPThyraModelEvaluatorBase::initialize(...): Errror!";
261 TEUCHOS_TEST_FOR_EXCEPTION( g_idx >= 0 && ( g_idx > model_outArgs.
Ng()-1 ), std::invalid_argument, msg_err );
264 const bool no_model_x = (model_space_x.get() == NULL);
266 const bool no_model_f = (model_space_f.get() == NULL);
269 TEUCHOS_TEST_FOR_EXCEPTION( !model_outArgs.
supports(MEB::OUT_ARG_W), std::invalid_argument, msg_err );
270 MEB::DerivativeProperties model_W_properties = model_outArgs.
get_W_properties();
271 TEUCHOS_TEST_FOR_EXCEPTION( model_W_properties.supportsAdjoint==
false, std::invalid_argument, msg_err );
272 TEUCHOS_TEST_FOR_EXCEPTION( model_W_properties.rank==MEB::DERIV_RANK_DEFICIENT, std::invalid_argument, msg_err );
291 DfDp_supports_op_ = model_outArgs.
supports(MEB::OUT_ARG_DfDp,p_idx).supports(MEB::DERIV_LINEAR_OP);
292 DfDp_supports_mv_ = model_outArgs.
supports(MEB::OUT_ARG_DfDp,p_idx).supports(MEB::DERIV_MV_BY_COL);
295 DfDp_supports_op_ =
false;
296 DfDp_supports_mv_ =
false;
299 VectorSpace::space_ptr_t space_xI;
301 space_xI =
Teuchos::rcp(
new VectorSpaceThyra(model_->get_p_space(p_idx)));
302 VectorSpace::space_ptr_t space_xD;
305 space_xD =
Teuchos::rcp(
new VectorSpaceThyra(model_space_x));
307 VectorSpace::space_ptr_t spaces_xD_xI[] = { space_xD, space_xI };
308 space_x_ =
Teuchos::rcp(
new VectorSpaceBlocked(spaces_xD_xI,2));
320 space_c_ =
Teuchos::rcp(
new VectorSpaceThyra(model_space_f));
322 space_c_ = Teuchos::null;
324 xinit_ = space_x_->create_member(); *xinit_ = 0.0;
330 factory_Gc_ = BasisSystemComposite::factory_Gc();
333 new BasisSystemComposite(
345 factory_Gc_ = Teuchos::null;
347 basis_sys_ = Teuchos::null;
352 model_g_ = createMember(model_->get_g_space(g_idx));
364 typedef ::Thyra::ModelEvaluatorBase MEB;
366 if (x_guess_bounds_updated_)
370 model_outArgs = model_->createOutArgs();
372 model_space_x = model_->get_x_space();
374 no_model_x = (model_space_x.
get() == NULL);
376 model_space_f = model_->get_f_space();
378 no_model_f = (model_space_f.
get() == NULL);
381 model_initialGuess = model_->getNominalValues(),
382 model_lowerBounds = model_->getLowerBounds(),
383 model_upperBounds = model_->getUpperBounds();
386 VectorSpace::vec_mut_ptr_t xinit_D = xinit_->sub_view(basis_sys_->var_dep());
388 VectorSpace::vec_mut_ptr_t xl_D = xl_->sub_view(basis_sys_->var_dep());
390 VectorSpace::vec_mut_ptr_t xu_D = xu_->sub_view(basis_sys_->var_dep());
396 VectorSpace::vec_mut_ptr_t xinit_I = xinit_->sub_view(var_indep);
398 VectorSpace::vec_mut_ptr_t xl_I = xl_->sub_view(var_indep);
400 VectorSpace::vec_mut_ptr_t xu_I = xu_->sub_view(var_indep);
404 x_guess_bounds_updated_ =
true;
412 ,
"NLPThyraModelEvaluatorBase::assert_is_initialized() : Error, "
413 "NLPThyraModelEvaluatorBase::initialize() has not been called yet."
436 using Teuchos::rcp_const_cast;
437 using Teuchos::rcp_dynamic_cast;
443 MEB::InArgs<value_type> &model_inArgs = *model_inArgs_inout;
444 if( basis_sys_.
get() ) {
446 var_dep = basis_sys_->var_dep(),
447 var_indep = basis_sys_->var_indep();
448 RCP<const Vector> xD = x.sub_view(var_dep), xI;
449 if(p_idx_>=0) xI = x.sub_view(var_indep);
450 model_inArgs.set_x(dyn_cast<const VectorMutableThyra>(*xD).thyra_vec().assert_not_null());
452 model_inArgs.set_p(p_idx_,dyn_cast<const VectorMutableThyra>(*xI).thyra_vec().assert_not_null());
456 model_inArgs.set_p(p_idx_,dyn_cast<const VectorMutableThyra>(x).thyra_vec().assert_not_null());
469 ,VectorMutable* *Gf_out
471 ,VectorMutable* *c_out
476 using Teuchos::rcp_const_cast;
477 using Teuchos::rcp_dynamic_cast;
482 typedef MEB::DerivativeMultiVector<value_type> DerivMV;
483 typedef MEB::Derivative<value_type> Deriv;
485 if(newx) model_g_updated_ = model_Dg_updated_ = f_updated_ = c_updated_ = Gf_updated_ = Gc_updated_ =
false;
489 MEB::InArgs<value_type> &model_inArgs = *model_inArgs_inout;
490 set_x(x,&model_inArgs);
495 VectorMutable *Gf = NULL;
496 value_type *
f = NULL;
497 VectorMutable *
c = NULL;
499 if(zero_order_info) {
500 f = zero_order_info->
f;
501 c = zero_order_info->
c;
503 else if(obj_grad_info) {
504 Gf = obj_grad_info->
Gf;
505 f = obj_grad_info->
f;
506 c = obj_grad_info->
c;
508 else if(first_order_info) {
509 Gc = first_order_info->
Gc;
510 Gf = first_order_info->
Gf;
511 f = first_order_info->
f;
512 c = first_order_info->
c;
518 MEB::OutArgs<value_type> &model_outArgs = *model_outArgs_inout;
519 if( f && (g_idx_>=0) && !f_updated_ ) {
522 if( c && !c_updated_ ) {
524 get_thyra_vector(*space_c_,c,&thyra_c);
527 if( Gf && !Gf_updated_ ) {
532 var_indep = ( basis_sys_.
get() ? basis_sys_->var_indep() :
Range1D() );
533 if( var_dep.
size() ) {
534 model_outArgs.set_DgDx(
538 dyn_cast<VectorMutableThyra>(*Gf->sub_view(var_dep)).thyra_vec()
540 ,MEB::DERIV_TRANS_MV_BY_ROW
544 model_outArgs.set_DgDp(
548 dyn_cast<VectorMutableThyra>(*Gf->sub_view(var_indep)).thyra_vec()
550 ,MEB::DERIV_TRANS_MV_BY_ROW
556 if(Gc_out) *Gc_out = Gc;
557 if(Gf_out) *Gf_out = Gf;
558 if(f_out) *f_out =
f;
559 if(c_out) *c_out =
c;
570 MEB::OutArgs<value_type> &model_outArgs = *model_outArgs_inout;
571 if( f && !f_updated_ ) {
573 *f = obj_scale_ * ::Thyra::get_ele(*model_g_,0);
580 if( c && !c_updated_ ) {
582 thyra_c = model_outArgs.get_f();
583 commit_thyra_vector(*space_c_,c,&thyra_c);
584 model_outArgs.set_f(Teuchos::null);
587 if( Gf && !Gf_updated_ ) {
591 var_indep = ( basis_sys_.
get() ? basis_sys_->var_indep() :
Range1D() );
592 if (obj_scale_ != 1.0 )
593 Vt_S( Gf, obj_scale_ );
595 Gf->sub_view(var_dep)->has_changed();
597 Gf->sub_view(var_indep)->has_changed();
620 out = this->getOStream();
622 verbLevel = ( showModelEvaluatorTrace() ? this->getVerbLevel() :
Teuchos::VERB_NONE );
625 VOTSME modelOutputTempState(model_,out,verbLevel);
627 *out <<
"\nEntering MoochoPack::NLPThyraModelEvaluatorBase::evalModel(...) ...\n";
631 MEB::InArgs<value_type> model_inArgs = model_->createInArgs();
632 MEB::OutArgs<value_type> model_outArgs = model_->createOutArgs();
633 VectorMutable *Gf = NULL;
634 value_type *
f = NULL;
635 VectorMutable *
c = NULL;
637 x,newx,zero_order_info,obj_grad_info,NULL
638 ,&model_inArgs,&model_outArgs,NULL,&Gf,&f,&c
643 model_->evalModel(model_inArgs,model_outArgs);
650 *out <<
"\nLeaving MoochoPack::NLPThyraModelEvaluatorBase::evalModel(...) ...\n";
vec_space_ptr_t space_c() const
value_type max_var_bounds_viol() const
void postprocessBaseOutArgs(Thyra::ModelEvaluatorBase::OutArgs< value_type > *model_outArgs_inout, VectorMutable *Gf, value_type *f, VectorMutable *c) const
void set_g(int j, const Evaluation< VectorBase< Scalar > > &g_j)
void Vt_S(VectorMutable *v_lhs, const value_type &alpha)
const Vector & xinit() const
VectorSpace adapter subclass for Thyra::VectorSpaceBase<value_type> .
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void initializeBase(const Teuchos::RCP< Thyra::ModelEvaluator< value_type > > &model, const int p_idx, const int g_idx)
Initialize given a Thyra::ModelEvaluator and a description of how to interpret it.
T_To & dyn_cast(T_From &from)
bool supports(EOutArgsMembers arg) const
void initialize(bool test_setup)
value_type scale_f() const
void report_final_solution(const Vector &x, const Vector *lambda, const Vector *nu, bool optimal)
bool is_initialized() const
virtual void unset_quantities()
void imp_calc_f(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info) const
const Vector & xu() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void imp_calc_Gf(const Vector &x, bool newx, const ObjGradInfo &obj_grad_info) const
const Vector & xl() const
void copy_from_model_p(const Thyra::VectorBase< value_type > *model_p, VectorMutable *x_I) const
void copy_from_model_x(const Thyra::VectorBase< value_type > *model_x, VectorMutable *x_D) const
VectorMutable adapter subclass for Thyra::VectorBase.
virtual void set_c(VectorMutable *c)
void preprocessBaseInOutArgs(const Vector &x, bool newx, const ZeroOrderInfo *zero_order_info, const ObjGradInfo *obj_grad_info, const NLPFirstOrder::FirstOrderInfo *first_order_info, Thyra::ModelEvaluatorBase::InArgs< value_type > *model_inArgs_inout, Thyra::ModelEvaluatorBase::OutArgs< value_type > *model_outArgs_inout, MatrixOp **Gc_out, VectorMutable **Gf_out, value_type **f_out, VectorMutable **c_out) const
static const Range1D Invalid
size_type num_bounded(const Vector &xl, const Vector &xu, value_type inf_bound)
void set_f(value_type *f)
const RCP< T > & assert_not_null() const
MatrixOp adapter subclass for Thyra::LinearOpBase.
MatrixOpNonsing adapter subclass for Thyra::Nonlin::LinearOpWithSolve.
NLPThyraModelEvaluatorBase()
vec_space_ptr_t space_x() const
void set_x(const Vector &x, Thyra::ModelEvaluatorBase::InArgs< value_type > *model_inArgs_inout) const
void set_c(VectorMutable *c)
void assert_is_initialized() const
void updateInitialGuessAndBounds() const
Update the initial guess and bounds .
void set_Gf(VectorMutable *Gf)
void imp_calc_c(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info) const
void evalModel(const Vector &x, bool newx, const ZeroOrderInfo *zero_order_info, const ObjGradInfo *obj_grad_info) const
virtual void initialize(bool test_setup=false)
static value_type infinite_bound()
size_type num_bounded_x() const
RCP< const VectorBase< Scalar > > get_x() const
virtual void set_f(value_type *f)
bool force_xinit_in_bounds() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
RCP< const VectorBase< Scalar > > get_p(int l) const
DerivativeProperties get_W_properties() const
virtual VectorMutable & c()