55 #include "Thyra_ModelEvaluatorHelpers.hpp"
56 #include "Thyra_DetachedVectorView.hpp"
57 #include "Thyra_VectorStdOps.hpp"
58 #include "Thyra_MultiVectorStdOps.hpp"
63 namespace NLPInterfacePack {
66 :DfDp_is_const_(false)
70 const Teuchos::RCP<Thyra::ModelEvaluator<value_type> > &model
73 ,
const objDirecFiniteDiffCalculator_ptr_t objDirecFiniteDiffCalculator
74 ,
const conDirecFiniteDiffCalculator_ptr_t conDirecFiniteDiffCalculator
76 :DfDp_is_const_(false)
80 ,objDirecFiniteDiffCalculator,conDirecFiniteDiffCalculator
85 const Teuchos::RCP<Thyra::ModelEvaluator<value_type> > &model
88 ,
const objDirecFiniteDiffCalculator_ptr_t objDirecFiniteDiffCalculator
89 ,
const conDirecFiniteDiffCalculator_ptr_t conDirecFiniteDiffCalculator
92 typedef Thyra::ModelEvaluatorBase MEB;
93 if(objDirecFiniteDiffCalculator.get())
94 this->set_objDirecFiniteDiffCalculator(objDirecFiniteDiffCalculator);
95 if(conDirecFiniteDiffCalculator.get())
96 this->set_conDirecFiniteDiffCalculator(conDirecFiniteDiffCalculator);
98 Thyra::ModelEvaluatorBase::OutArgs<double> model_outArgs = model->createOutArgs();
99 MEB::DerivativeProperties model_W_properties = model_outArgs.get_W_properties();
102 (conDirecFiniteDiffCalculator_.get()==0 && !model_outArgs.supports(MEB::OUT_ARG_DfDp,p_idx).supports(MEB::DERIV_MV_BY_COL))
103 ,std::invalid_argument
104 ,
"Error, model must support computing DfDp("<<p_idx<<
") as a"
105 " column-oriented multi-vector if not using finite differences!"
108 DfDp_is_const_ =
false;
132 if(objDirecFiniteDiffCalculator_.get())
141 return ( objDirecFiniteDiffCalculator_.get() != NULL );
149 typedef Thyra::ModelEvaluatorBase MEB;
150 MEB::InArgs<value_type> basePoint =
model_->createInArgs();
151 MEB::InArgs<value_type> directions =
model_->createInArgs();
152 MEB::OutArgs<value_type> baseFunc =
model_->createOutArgs();
153 MEB::OutArgs<value_type> variations =
model_->createOutArgs();
155 set_x(d,&directions);
157 objDirecFiniteDiffCalculator_->calcVariations(
158 *
model_,basePoint,directions,baseFunc,variations
160 return Thyra::get_ele(*variations.get_g(
g_idx_),0);
205 using Teuchos::rcp_const_cast;
206 using Teuchos::rcp_dynamic_cast;
212 typedef Thyra::ModelEvaluatorBase MEB;
214 typedef MEB::DerivativeMultiVector<value_type> DerivMV;
215 typedef MEB::Derivative<value_type> Deriv;
220 out = this->getOStream();
225 VOTSME modelOutputTempState(
model_,out,verbLevel);
227 VOTSDFDC objDirecFiniteDiffCalculatorOutputTempState(objDirecFiniteDiffCalculator_,out,verbLevel);
228 VOTSDFDC conDirecFiniteDiffCalculatorOutputTempState(conDirecFiniteDiffCalculator_,out,verbLevel);
229 const bool trace =
static_cast<int>(verbLevel) >= static_cast<int>(
Teuchos::VERB_LOW);
230 if(out.
get() && trace)
231 *out <<
"\nEntering MoochoPack::NLPDirectThyraModelEvaluator::calc_point(...) ...\n";
238 objDirecFiniteDiffCalculator_.get()!=NULL && Gf!=NULL, std::logic_error
239 ,
"Error, can not compute full gradient vector Gf when using directional finite differences!"
249 MEB::InArgs<value_type> model_inArgs =
model_->createInArgs();
250 MEB::OutArgs<value_type> model_outArgs =
model_->createOutArgs();
252 obj_grad_info.
Gf =
Gf;
254 if(recalc_c) obj_grad_info.
c =
c;
256 x,
true,NULL,&obj_grad_info,NULL
257 ,&model_inArgs,&model_outArgs,NULL,NULL,NULL,NULL
259 if( py || rGf || D ) {
264 bool new_thyra_N =
false;
271 !conDirecFiniteDiffCalculator_.get()
274 || model_outArgs.get_DfDp_properties(
p_idx_).linearity!=MEB::DERIV_LINEARITY_CONST )
280 if ( !
is_null(model_outArgs.get_W()) || !
is_null(model_outArgs.get_W_op()) ) {
281 if (model_inArgs.supports(MEB::IN_ARG_alpha))
282 model_inArgs.set_alpha(0.0);
283 if (model_inArgs.supports(MEB::IN_ARG_beta))
284 model_inArgs.set_beta(1.0);
289 model_->evalModel(model_inArgs,model_outArgs);
295 const VectorSpaceThyra *
space_c;
296 const VectorSpaceThyra *space_xD;
299 RCP<MatrixOp> D_used =
rcp(D,
false);
300 RCP<Thyra::MultiVectorBase<value_type> > thyra_D;
302 if( py || ( ( rGf || D ) && conDirecFiniteDiffCalculator_.get() ) ) {
304 space_xD = &dyn_cast<const VectorSpaceThyra>(py->
space());
312 rcp_const_cast<Thyra::MultiVectorBase<value_type> >(
313 rcp_dynamic_cast<
const Thyra::MultiVectorBase<value_type> >(
314 dyn_cast<MultiVectorMutableThyra>(*D_used).thyra_multi_vec()
322 !
is_null(conDirecFiniteDiffCalculator_)
324 ( new_thyra_N || !DfDp_is_const() )
327 if(out.
get() && trace)
328 *out <<
"\nComputing thyra_N using directional finite differences ...\n";
331 model_outArgs.set_f(rcp_const_cast<Thyra::VectorBase<value_type> >(thyra_c));
332 typedef Thyra::DirectionalFiniteDiffCalculator<value_type>::SelectedDerivatives SelectedDerivatives;
333 MEB::OutArgs<value_type>
334 model_fdOutArgs = conDirecFiniteDiffCalculator_->createOutArgs(
335 *
model_,SelectedDerivatives().supports(MEB::OUT_ARG_DfDp,0)
338 conDirecFiniteDiffCalculator_->calcDerivatives(
346 if( ( D || rGf ) && py ) {
348 if(out.
get() && trace)
349 *out <<
"\nSolving C*[py,D] = -[c,N] simultaneously ...\n";
350 const int nind =
thyra_N_->domain()->dim();
351 RCP<Thyra::MultiVectorBase<value_type> >
352 thyra_cN = Thyra::createMembers(
thyra_N_->range(),nind+1);
355 RCP<Thyra::MultiVectorBase<value_type> >
356 thyra_pyD = Thyra::createMembers(thyra_D->range(),nind+1);
358 Thyra::SolveStatus<value_type>
359 solveStatus = Thyra::solve(*
thyra_C_, Thyra::NOTRANS, *thyra_cN, thyra_pyD.
ptr());
360 if(out.
get() && trace) {
362 <<
"\nsolve status:\n";
363 OSTab(out).o() << solveStatus;
365 Thyra::scale(-1.0, thyra_pyD.ptr());
372 if(out.
get() && trace)
373 *out <<
"\nSolving C*py = -c ...\n";
375 Thyra::SolveStatus<value_type> solveStatus =
376 Thyra::solve<value_type>(*
thyra_C_, Thyra::NOTRANS, *thyra_c, thyra_py.
ptr());
378 *out <<
"\nsolve status:\n";
379 OSTab(out).o() << solveStatus;
384 if(out.
get() && trace)
385 *out <<
"\nSolving C*D = -N ...\n";
387 Thyra::SolveStatus<value_type>
389 if(out.
get() && trace) {
391 <<
"\nsolve status:\n";
392 OSTab(out).o() << solveStatus;
394 Thyra::scale(-1.0, thyra_D.ptr());
407 if(objDirecFiniteDiffCalculator_.get()) {
408 if(out.
get() && trace)
409 *out <<
"\nComputing rGf using directional finite differences ...\n";
416 TVecPtr e_i = createMember(
model_->get_p_space(
p_idx_));
418 MEB::InArgs<value_type> dir =
model_->createInArgs();
420 MEB::OutArgs<value_type> bfunc =
model_->createOutArgs();
423 Thyra::set_ele(0, *f, bfunc.get_g(
g_idx_).ptr());
425 MEB::OutArgs<value_type> var =
model_->createOutArgs();
428 for(
int i = 0; i < np; ++i ) {
429 set_ele(i, 1.0, e_i.ptr());
430 dir.set_x(thyra_D->col(i));
431 objDirecFiniteDiffCalculator_->calcVariations(
432 *
model_,model_inArgs,dir,bfunc,var
435 Thyra::set_ele(i, 0.0, e_i.ptr());
436 d_rGf()[i] = Thyra::get_ele(*var.get_g(
g_idx_),0);
447 if(out.
get() && trace)
448 *out <<
"\nLeaving MoochoPack::NLPDirectThyraModelEvaluator::calc_point(...) ...\n";
void unset_quantities()
Call to unset all storage quantities (both in this class and all subclasses).
virtual value_type & f()
Returns non-const *this->get_f().
virtual vec_mut_ptr_t sub_view(const Range1D &rng)
Create a mutable abstract view of a vector object.
vec_space_ptr_t space_c() const
Struct for gradient (objective), objective and constriants (pointers)
virtual const VectorSpace & space() const =0
Return the vector space that this vector belongs to.
bool is_null(const boost::shared_ptr< T > &p)
Abstract interface for immutable, finite dimensional, coordinate vectors {abstract}.
VectorMutable * c
Pointer to constraints residual c (may be NULL if not set)
void postprocessBaseOutArgs(Thyra::ModelEvaluatorBase::OutArgs< value_type > *model_outArgs_inout, VectorMutable *Gf, value_type *f, VectorMutable *c) const
Teuchos::RCP< Thyra::MultiVectorBase< value_type > > thyra_N_
void Vt_S(VectorMutable *v_lhs, const value_type &alpha)
v_lhs *= alpha
VectorMutable * Gf
Pointer to gradient of objective function Gf (may be NULL if not set)
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.
const mat_sym_nonsing_fcty_ptr_t factory_S() const
void assign(VectorMutable *v_lhs, const V &V_rhs)
v_lhs = V_rhs.
const mat_fcty_ptr_t factory_D() const
const ObjGradInfo obj_grad_info() const
Return objective gradient and zero order information.
void initialize(bool test_setup)
basic_OSTab< char > OSTab
value_type * f
Pointer to objective function f (may be NULL if not set)
void get_thyra_vector(const VectorSpaceThyra &thyra_vec_spc, const Vector &vec, Teuchos::RCP< const Thyra::VectorBase< value_type > > *thyra_vec)
Teuchos::RCP< Thyra::LinearOpWithSolveBase< value_type > > thyra_C_
void initialize(const Teuchos::RCP< Thyra::ModelEvaluator< value_type > > &model, const int p_idx, const int g_idx, const objDirecFiniteDiffCalculator_ptr_t objDirecFiniteDiffCalculator=Teuchos::null, const conDirecFiniteDiffCalculator_ptr_t conDirecFiniteDiffCalculator=Teuchos::null)
.Initialize given a Thyra::ModelEvaluator and a description of how to interpret it.
void commit_thyra_vector(const VectorSpaceThyra &thyra_vec_spc, VectorMutable *vec, Teuchos::RCP< Thyra::VectorBase< value_type > > *thyra_vec)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void initialize(bool test_setup)
Initialize the NLP for its first use.
T_To & dyn_cast(T_From &from)
VectorMutable adapter subclass for Thyra::VectorBase.
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
value_type calc_Gf_prod(const Vector &x, const Vector &d, bool newx) const
const RCP< T > & assert_not_null() const
MatrixOp adapter subclass for Thyra::LinearOpBase.
MatrixOpNonsing adapter subclass for Thyra::Nonlin::LinearOpWithSolve.
bool supports_Gf_prod() const
NLPDirectThyraModelEvaluator()
Initialize to uninitialized.
void calc_point(const Vector &x, value_type *f, VectorMutable *c, bool recalc_c, VectorMutable *Gf, VectorMutable *py, VectorMutable *rGf, MatrixOp *GcU, MatrixOp *D, MatrixOp *Uz) const
bool nonnull(const boost::shared_ptr< T > &p)
basic_FancyOStream< char > FancyOStream
Teuchos::RCP< Thyra::ModelEvaluator< value_type > > model_
MultiVectorMutable adapter subclass for Thyra::MultiVectorBase.
void V_MtV(VectorMutable *v_lhs, const MatrixOp &M_rhs1, BLAS_Cpp::Transp trans_rhs1, const V &V_rhs2)
v_lhs = op(M_rhs1) * V_rhs2.
AbstractLinAlgPack::value_type value_type
void set_x(const Vector &x, Thyra::ModelEvaluatorBase::InArgs< value_type > *model_inArgs_inout) const
Abstract interface for mutable coordinate vectors {abstract}.
virtual obj_ptr_t create() const =0
NLPFirstOrder::basis_sys_ptr_t basis_sys_
void calc_semi_newton_step(const Vector &x, VectorMutable *c, bool recalc_c, VectorMutable *py) const
void free_thyra_vector(const VectorSpaceThyra &thyra_vec_spc, const Vector &vec, Teuchos::RCP< const Thyra::VectorBase< value_type > > *thyra_vec)
Extract a non-const DenseLinAlgPack::DVectorSlice view of a VectorMutable object. ...
virtual VectorMutable & Gf()
Returns non-const *this->get_Gf().
Range1D var_indep() const
virtual EVerbosityLevel getVerbLevel() const
RangePack::Range1D Range1D
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
virtual VectorMutable & c()
Returns non-const *this->get_c().
void Vp_V(VectorMutable *v_lhs, const V &V_rhs)
v_lhs += V_rhs.