NLPInterfacePack_NLPThyraModelEvaluatorBase.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 //
00030 // Note: I am using a BasisSystem here just to make it easy to generate
00031 // var_dep() and var_indep() and for not much else.
00032 //
00033 
00034 #include "NLPInterfacePack_NLPThyraModelEvaluatorBase.hpp"
00035 #include "AbstractLinAlgPack_LinAlgOpPack.hpp"
00036 #include "AbstractLinAlgPack_VectorOut.hpp"
00037 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
00038 #include "AbstractLinAlgPack_VectorSpaceThyra.hpp"
00039 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
00040 #include "AbstractLinAlgPack_MatrixOpNonsingThyra.hpp"
00041 #include "AbstractLinAlgPack_BasisSystemComposite.hpp"
00042 #include "AbstractLinAlgPack_VectorSpaceBlocked.hpp"
00043 #include "AbstractLinAlgPack_VectorAuxiliaryOps.hpp"
00044 #include "AbstractLinAlgPack_MatrixSymPosDefCholFactor.hpp"
00045 #include "Thyra_DetachedVectorView.hpp"
00046 #include "Thyra_VectorStdOps.hpp"
00047 #include "Teuchos_AbstractFactoryStd.hpp"
00048 #include "Teuchos_TestForException.hpp"
00049 #include "Teuchos_dyn_cast.hpp"
00050 
00051 namespace NLPInterfacePack {
00052   
00053 // Overridden public members from NLP
00054 
00055 void NLPThyraModelEvaluatorBase::initialize(bool test_setup)
00056 {
00057   x_guess_bounds_updated_ = false;
00058   updateInitialGuessAndBounds();
00059   if(initialized_) {
00060     NLPObjGrad::initialize(test_setup);
00061     return;
00062   }
00063   //TEST_FOR_EXCEPT(true); // Todo: push the variables in bounds!
00064   num_bounded_x_ = AbstractLinAlgPack::num_bounded(*xl_,*xu_,NLP::infinite_bound());
00065   NLPObjGrad::initialize(test_setup);
00066   initialized_ = true;
00067 }
00068 
00069 bool NLPThyraModelEvaluatorBase::is_initialized() const
00070 {
00071   return initialized_;
00072 }
00073 
00074 NLP::vec_space_ptr_t
00075 NLPThyraModelEvaluatorBase::space_x() const
00076 {
00077   return space_x_;
00078 }
00079 
00080 NLP::vec_space_ptr_t
00081 NLPThyraModelEvaluatorBase::space_c() const
00082 {
00083   return space_c_;
00084 }
00085 
00086 size_type NLPThyraModelEvaluatorBase::num_bounded_x() const
00087 {
00088   return num_bounded_x_;
00089 }
00090 
00091 void NLPThyraModelEvaluatorBase::force_xinit_in_bounds(bool force_xinit_in_bounds)
00092 {
00093   force_xinit_in_bounds_ = force_xinit_in_bounds;
00094 }
00095 
00096 bool NLPThyraModelEvaluatorBase::force_xinit_in_bounds() const
00097 {
00098   return force_xinit_in_bounds_;
00099 }
00100 
00101 const Vector& NLPThyraModelEvaluatorBase::xinit() const
00102 {
00103   updateInitialGuessAndBounds();
00104   return *xinit_;
00105 }
00106 
00107 const Vector& NLPThyraModelEvaluatorBase::xl() const
00108 {
00109   updateInitialGuessAndBounds();
00110   return *xl_;
00111 }
00112 
00113 const Vector& NLPThyraModelEvaluatorBase::xu() const
00114 {
00115   updateInitialGuessAndBounds();
00116   return *xu_;
00117 }
00118 
00119 value_type NLPThyraModelEvaluatorBase::max_var_bounds_viol() const
00120 {
00121   return 1e-5; // I have no idea?
00122 }
00123 
00124 void NLPThyraModelEvaluatorBase::set_f(value_type* f)
00125 {
00126   NLP::set_f(f);
00127   f_updated_ = false;
00128 }
00129 
00130 void NLPThyraModelEvaluatorBase::set_c(VectorMutable* c)
00131 {
00132   NLP::set_c(c);
00133   c_updated_ = false;
00134 }
00135 
00136 void NLPThyraModelEvaluatorBase::unset_quantities()
00137 {
00138   NLP::unset_quantities();
00139 }
00140 
00141 void NLPThyraModelEvaluatorBase::scale_f( value_type scale_f )
00142 {
00143   obj_scale_ = scale_f;
00144 }
00145 
00146 value_type NLPThyraModelEvaluatorBase::scale_f() const
00147 {
00148   return obj_scale_;
00149 }
00150 
00151 void NLPThyraModelEvaluatorBase::report_final_solution(
00152   const Vector&    x
00153   ,const Vector*   lambda
00154   ,const Vector*   nu
00155   ,bool            optimal
00156   )
00157 {
00158   using Teuchos::dyn_cast;
00159   using Teuchos::RCP;
00160   typedef Thyra::ModelEvaluatorBase MEB;
00161   using AbstractLinAlgPack::VectorMutableThyra;
00162   MEB::InArgs<value_type> model_finalPoint = model_->createInArgs();
00163   if( basis_sys_.get() ) {
00164     const Range1D
00165       var_dep   = basis_sys_->var_dep(),
00166       var_indep = basis_sys_->var_indep();
00167     RCP<const Vector> xD = x.sub_view(var_dep), xI;
00168     if(p_idx_>=0) xI = x.sub_view(var_indep);
00169     model_finalPoint.set_x(dyn_cast<const VectorMutableThyra>(*xD).thyra_vec().assert_not_null());
00170     if(p_idx_ >= 0)
00171       model_finalPoint.set_p(p_idx_,dyn_cast<const VectorMutableThyra>(*xI).thyra_vec().assert_not_null());
00172     else if( model_finalPoint.Np() >= 1 )
00173       model_finalPoint.set_p(0,model_->getNominalValues().get_p(0)); // Assume we will find in it zero!
00174   }
00175   else { // no dependent vars
00176     TEST_FOR_EXCEPT(p_idx_<0);
00177     model_finalPoint.set_p(p_idx_,dyn_cast<const VectorMutableThyra>(x).thyra_vec().assert_not_null());
00178   }
00179   model_->reportFinalPoint(model_finalPoint,optimal);
00180 }
00181 
00182 // Overridden public members from NLPObjGrad
00183 
00184 void NLPThyraModelEvaluatorBase::set_Gf(VectorMutable* Gf)
00185 {
00186   NLPObjGrad::set_Gf(Gf);
00187   Gf_updated_ = false;
00188 }
00189 
00190 // Overridden protected members from NLP
00191 
00192 void NLPThyraModelEvaluatorBase::imp_calc_f(
00193   const Vector& x, bool newx
00194   ,const ZeroOrderInfo& zero_order_info
00195   ) const
00196 {
00197   evalModel(x,newx,&zero_order_info,NULL);
00198 }
00199 
00200 void NLPThyraModelEvaluatorBase::imp_calc_c(
00201   const Vector& x, bool newx
00202   ,const ZeroOrderInfo& zero_order_info
00203   ) const
00204 {
00205   evalModel(x,newx,&zero_order_info,NULL);
00206 }
00207 
00208 // Overridden protected members from NLPObjGrad
00209 
00210 void NLPThyraModelEvaluatorBase::imp_calc_Gf(
00211   const Vector& x, bool newx
00212   ,const ObjGradInfo& obj_grad_info
00213   ) const
00214 {
00215   evalModel(x,newx,NULL,&obj_grad_info);
00216 }
00217 
00218 // Protected functions to be used by subclasses
00219 
00220 NLPThyraModelEvaluatorBase::NLPThyraModelEvaluatorBase()
00221   :showModelEvaluatorTrace_(false),initialized_(false)
00222   ,obj_scale_(1.0),has_bounds_(false)
00223   ,force_xinit_in_bounds_(true),num_bounded_x_(0)
00224   ,x_guess_bounds_updated_(false)
00225 {}
00226 
00227 void NLPThyraModelEvaluatorBase::initializeBase(
00228   const Teuchos::RCP<Thyra::ModelEvaluator<value_type> >  &model,
00229   const int p_idx,
00230   const int g_idx
00231   )
00232 {
00233 
00234   using Teuchos::dyn_cast;
00235   using AbstractLinAlgPack::VectorSpaceThyra;
00236   using AbstractLinAlgPack::VectorMutableThyra;
00237   using AbstractLinAlgPack::MatrixOpNonsingThyra;
00238   typedef ::Thyra::ModelEvaluatorBase MEB;
00239 
00240   initialized_ = false;
00241   x_guess_bounds_updated_ = false;
00242   model_g_updated_ = model_Dg_updated_ = f_updated_ = c_updated_ = Gf_updated_ = Gc_updated_ = false;
00243 
00244   const char msg_err[] = "NLPThyraModelEvaluatorBase::initialize(...): Errror!";
00245   Thyra::ModelEvaluatorBase::OutArgs<value_type> model_outArgs = model->createOutArgs();
00246   TEST_FOR_EXCEPTION( model.get() == NULL, std::invalid_argument, msg_err );
00247   TEST_FOR_EXCEPTION( p_idx >= 0 && ( p_idx > model_outArgs.Np()-1 ), std::invalid_argument, msg_err );
00248   TEST_FOR_EXCEPTION( g_idx >= 0 && ( g_idx > model_outArgs.Ng()-1 ), std::invalid_argument, msg_err );
00249   //
00250   Teuchos::RCP<const Thyra::VectorSpaceBase<value_type> > model_space_x(model->get_x_space());
00251   const bool no_model_x = (model_space_x.get() == NULL);
00252   Teuchos::RCP<const Thyra::VectorSpaceBase<value_type> > model_space_f(model->get_f_space());
00253   const bool no_model_f = (model_space_f.get() == NULL);
00254   //
00255   if( !no_model_f ) {
00256     TEST_FOR_EXCEPTION( !model_outArgs.supports(MEB::OUT_ARG_W), std::invalid_argument, msg_err );
00257     MEB::DerivativeProperties model_W_properties = model_outArgs.get_W_properties();
00258     TEST_FOR_EXCEPTION( model_W_properties.supportsAdjoint==false, std::invalid_argument, msg_err );
00259     TEST_FOR_EXCEPTION( model_W_properties.rank==MEB::DERIV_RANK_DEFICIENT, std::invalid_argument, msg_err );
00260     /*
00261     if(p_idx >= 0 ) {
00262       TEST_FOR_EXCEPTION( model_outArgs.supports(MEB::OUT_ARG_DfDp,p_idx).none(), std::invalid_argument, msg_err );
00263       if(g_idx >= 0) {
00264         TEST_FOR_EXCEPTION( model_outArgs.supports(MEB::OUT_ARG_DgDp,g_idx,p_idx).none(), std::invalid_argument, msg_err );
00265       }
00266     }
00267     if(g_idx >= 0) {
00268       TEST_FOR_EXCEPTION( model_outArgs.supports(MEB::OUT_ARG_DgDx,g_idx).none(), std::invalid_argument, msg_err );
00269     }
00270     */
00271   }
00272 
00273   model_ = model;
00274   p_idx_ = p_idx;
00275   g_idx_ = g_idx;
00276 
00277   if(p_idx >= 0 ) {
00278     DfDp_supports_op_ = model_outArgs.supports(MEB::OUT_ARG_DfDp,p_idx).supports(MEB::DERIV_LINEAR_OP);
00279     DfDp_supports_mv_ = model_outArgs.supports(MEB::OUT_ARG_DfDp,p_idx).supports(MEB::DERIV_MV_BY_COL);
00280   }
00281   else {
00282     DfDp_supports_op_ = false;
00283     DfDp_supports_mv_ = false;
00284   }
00285 
00286   VectorSpace::space_ptr_t space_xI;
00287   if(p_idx >= 0)
00288     space_xI = Teuchos::rcp(new VectorSpaceThyra(model_->get_p_space(p_idx)));
00289   VectorSpace::space_ptr_t space_xD;
00290   //
00291   if(!no_model_x) {
00292     space_xD = Teuchos::rcp(new VectorSpaceThyra(model_space_x));
00293     if (p_idx >= 0)  {
00294       VectorSpace::space_ptr_t spaces_xD_xI[] = { space_xD, space_xI };
00295       space_x_ = Teuchos::rcp(new VectorSpaceBlocked(spaces_xD_xI,2));
00296     }
00297     else {
00298       space_x_ = space_xD;
00299     }
00300   }
00301   else {
00302     space_x_ = space_xI;
00303   } 
00304   TEST_FOR_EXCEPT(!space_x_.get());
00305 
00306   if(!no_model_f)
00307     space_c_ = Teuchos::rcp(new VectorSpaceThyra(model_space_f));
00308   else
00309     space_c_ = Teuchos::null;
00310 
00311   xinit_ = space_x_->create_member();  *xinit_ = 0.0;
00312   xl_    = space_x_->create_member();  *xl_    = -NLP::infinite_bound();
00313   xu_    = space_x_->create_member();  *xu_    = +NLP::infinite_bound();
00314 
00315   if(!no_model_f) {
00316 
00317     factory_Gc_ = BasisSystemComposite::factory_Gc();
00318 
00319     basis_sys_ = Teuchos::rcp(
00320       new BasisSystemComposite(
00321         space_x_
00322         ,space_c_
00323         ,Teuchos::rcp(new Teuchos::AbstractFactoryStd<MatrixOpNonsing,MatrixOpNonsingThyra>())          // factory_C
00324         ,Teuchos::rcp(new Teuchos::AbstractFactoryStd<MatrixSymOp,MatrixSymPosDefCholFactor>())         // factory_transDtD
00325         ,Teuchos::rcp(new Teuchos::AbstractFactoryStd<MatrixSymOpNonsing,MatrixSymPosDefCholFactor>())  // factory_S
00326         )
00327       );
00328 
00329   }
00330   else {
00331 
00332     factory_Gc_ = Teuchos::null;
00333 
00334     basis_sys_ = Teuchos::null;
00335 
00336   }
00337   
00338   if(g_idx >= 0) {
00339     model_g_ = createMember(model_->get_g_space(g_idx));
00340   }
00341 
00342 }
00343 
00344 void NLPThyraModelEvaluatorBase::updateInitialGuessAndBounds() const
00345 {
00346 
00347   using Teuchos::dyn_cast;
00348   using AbstractLinAlgPack::VectorSpaceThyra;
00349   using AbstractLinAlgPack::VectorMutableThyra;
00350   using AbstractLinAlgPack::MatrixOpNonsingThyra;
00351   typedef ::Thyra::ModelEvaluatorBase MEB;
00352 
00353   if (x_guess_bounds_updated_)
00354     return;
00355 
00356   Thyra::ModelEvaluatorBase::OutArgs<value_type>
00357     model_outArgs = model_->createOutArgs();
00358   Teuchos::RCP<const Thyra::VectorSpaceBase<value_type> >
00359     model_space_x = model_->get_x_space();
00360   const bool
00361     no_model_x = (model_space_x.get() == NULL);
00362   Teuchos::RCP<const Thyra::VectorSpaceBase<value_type> >
00363     model_space_f = model_->get_f_space();
00364   const bool
00365     no_model_f = (model_space_f.get() == NULL);
00366 
00367   Thyra::ModelEvaluatorBase::InArgs<value_type>
00368     model_initialGuess = model_->getNominalValues(),
00369     model_lowerBounds = model_->getLowerBounds(),
00370     model_upperBounds = model_->getUpperBounds();
00371 
00372   if(!no_model_x) {
00373     VectorSpace::vec_mut_ptr_t xinit_D = xinit_->sub_view(basis_sys_->var_dep());
00374     copy_from_model_x( model_initialGuess.get_x().get(), &*xinit_D );
00375     VectorSpace::vec_mut_ptr_t xl_D = xl_->sub_view(basis_sys_->var_dep());
00376     copy_from_model_x( model_lowerBounds.get_x().get(), &*xl_D );
00377     VectorSpace::vec_mut_ptr_t xu_D = xu_->sub_view(basis_sys_->var_dep());
00378     copy_from_model_x( model_upperBounds.get_x().get(), &*xu_D );
00379   }
00380 
00381   if(p_idx_ >= 0) {
00382     Range1D var_indep = ( basis_sys_.get() ? basis_sys_->var_indep() : Range1D() );
00383     VectorSpace::vec_mut_ptr_t xinit_I = xinit_->sub_view(var_indep);
00384     copy_from_model_p( model_initialGuess.get_p(p_idx_).get(), &*xinit_I );
00385     VectorSpace::vec_mut_ptr_t xl_I = xl_->sub_view(var_indep);
00386     copy_from_model_p( model_lowerBounds.get_p(p_idx_).get(), &*xl_I );
00387     VectorSpace::vec_mut_ptr_t xu_I = xu_->sub_view(var_indep);
00388     copy_from_model_p( model_upperBounds.get_p(p_idx_).get(), &*xu_I );
00389   }
00390 
00391   x_guess_bounds_updated_ = true;
00392 
00393 }
00394 
00395 void NLPThyraModelEvaluatorBase::assert_is_initialized() const
00396 {
00397   TEST_FOR_EXCEPTION(
00398     !is_initialized(), NLP::UnInitialized
00399     ,"NLPThyraModelEvaluatorBase::assert_is_initialized() : Error, "
00400     "NLPThyraModelEvaluatorBase::initialize() has not been called yet."
00401     );
00402 }
00403 
00404 void NLPThyraModelEvaluatorBase::copy_from_model_x( const Thyra::VectorBase<value_type>* model_x, VectorMutable* x_D ) const
00405 {
00406   if(!model_x) return;
00407   *x_D = AbstractLinAlgPack::VectorMutableThyra(Teuchos::rcp(const_cast<Thyra::VectorBase<value_type>*>(model_x),false));
00408 }
00409 
00410 void NLPThyraModelEvaluatorBase::copy_from_model_p( const Thyra::VectorBase<value_type> *model_p, VectorMutable* x_I ) const
00411 {
00412   if(!model_p) return;
00413   *x_I = AbstractLinAlgPack::VectorMutableThyra(Teuchos::rcp(const_cast<Thyra::VectorBase<value_type>*>(model_p),false));
00414 }
00415 
00416 void NLPThyraModelEvaluatorBase::set_x(
00417   const Vector                                      &x
00418   ,Thyra::ModelEvaluatorBase::InArgs<value_type>    *model_inArgs_inout
00419   ) const
00420 {
00421   using Teuchos::dyn_cast;
00422   using Teuchos::RCP;
00423   using Teuchos::rcp_const_cast;
00424   using Teuchos::rcp_dynamic_cast;
00425   using AbstractLinAlgPack::VectorMutableThyra;
00426   typedef Thyra::ModelEvaluatorBase MEB;
00427   //
00428   // Set the input arguments
00429   //
00430   MEB::InArgs<value_type> &model_inArgs = *model_inArgs_inout;
00431   if( basis_sys_.get() ) {
00432     const Range1D
00433       var_dep   = basis_sys_->var_dep(),
00434       var_indep = basis_sys_->var_indep();
00435     RCP<const Vector> xD = x.sub_view(var_dep), xI;
00436     if(p_idx_>=0) xI = x.sub_view(var_indep);
00437     model_inArgs.set_x(dyn_cast<const VectorMutableThyra>(*xD).thyra_vec().assert_not_null());
00438     if(p_idx_ >= 0)
00439       model_inArgs.set_p(p_idx_,dyn_cast<const VectorMutableThyra>(*xI).thyra_vec().assert_not_null());
00440   }
00441   else { // no dependent vars
00442     TEST_FOR_EXCEPT(p_idx_<0);
00443     model_inArgs.set_p(p_idx_,dyn_cast<const VectorMutableThyra>(x).thyra_vec().assert_not_null());
00444   }
00445 }
00446 
00447 void NLPThyraModelEvaluatorBase::preprocessBaseInOutArgs(
00448   const Vector                                      &x
00449   ,bool                                             newx
00450   ,const ZeroOrderInfo                              *zero_order_info
00451   ,const ObjGradInfo                                *obj_grad_info
00452   ,const NLPFirstOrder::FirstOrderInfo              *first_order_info
00453   ,Thyra::ModelEvaluatorBase::InArgs<value_type>    *model_inArgs_inout
00454   ,Thyra::ModelEvaluatorBase::OutArgs<value_type>   *model_outArgs_inout
00455   ,MatrixOp*                                        *Gc_out
00456   ,VectorMutable*                                   *Gf_out
00457   ,value_type*                                      *f_out
00458   ,VectorMutable*                                   *c_out
00459   ) const
00460 {
00461   using Teuchos::dyn_cast;
00462   using Teuchos::RCP;
00463   using Teuchos::rcp_const_cast;
00464   using Teuchos::rcp_dynamic_cast;
00465   using AbstractLinAlgPack::VectorMutableThyra;
00466   using AbstractLinAlgPack::MatrixOpThyra;
00467   using AbstractLinAlgPack::MatrixOpNonsingThyra;
00468   typedef Thyra::ModelEvaluatorBase MEB;
00469   typedef MEB::DerivativeMultiVector<value_type> DerivMV;
00470   typedef MEB::Derivative<value_type> Deriv;
00471   //
00472   if(newx) model_g_updated_ = model_Dg_updated_ = f_updated_ = c_updated_ = Gf_updated_ = Gc_updated_ = false;
00473   //
00474   // Set the input arguments
00475   //
00476   MEB::InArgs<value_type> &model_inArgs = *model_inArgs_inout;
00477   set_x(x,&model_inArgs);
00478   //
00479   // Set the output arguments
00480   //
00481   MatrixOp            *Gc = NULL;
00482   VectorMutable       *Gf = NULL;
00483   value_type          *f  = NULL;
00484   VectorMutable       *c  = NULL;
00485   //
00486   if(zero_order_info) {
00487     f = zero_order_info->f;
00488     c = zero_order_info->c;
00489   }
00490   else if(obj_grad_info) {
00491     Gf = obj_grad_info->Gf;
00492     f  = obj_grad_info->f;
00493     c  = obj_grad_info->c;
00494   }
00495   else if(first_order_info) {
00496     Gc = first_order_info->Gc;
00497     Gf = first_order_info->Gf;
00498     f  = first_order_info->f;
00499     c  = first_order_info->c;
00500   }
00501   else {
00502     TEST_FOR_EXCEPT(true); // Should never be called!
00503   }
00504   //
00505   MEB::OutArgs<value_type> &model_outArgs = *model_outArgs_inout;
00506   if( f && (g_idx_>=0) && !f_updated_ ) {
00507     model_outArgs.set_g(g_idx_,model_g_.assert_not_null()); // ToDo: Make more general!
00508   }
00509   if( c && !c_updated_ ) {
00510     Teuchos::RCP<Thyra::VectorBase<value_type> > thyra_c;
00511     get_thyra_vector(*space_c_,c,&thyra_c);
00512     model_outArgs.set_f(thyra_c.assert_not_null());
00513   }
00514   if( Gf && !Gf_updated_ ) {
00515     if(g_idx_>=0) {
00516       if(p_idx_>=0) {
00517         const Range1D
00518           var_dep   = ( basis_sys_.get() ? basis_sys_->var_dep() : Range1D::Invalid ),
00519           var_indep = ( basis_sys_.get() ? basis_sys_->var_indep() : Range1D() );
00520         if( var_dep.size() ) {
00521           model_outArgs.set_DgDx(
00522             g_idx_
00523             ,DerivMV(
00524               rcp_const_cast<Thyra::VectorBase<value_type> >(
00525                 dyn_cast<VectorMutableThyra>(*Gf->sub_view(var_dep)).thyra_vec()
00526                 ).assert_not_null()
00527               ,MEB::DERIV_TRANS_MV_BY_ROW
00528               )
00529             );
00530         }
00531         model_outArgs.set_DgDp(
00532           g_idx_,p_idx_
00533           ,DerivMV(
00534             rcp_const_cast<Thyra::VectorBase<value_type> >(
00535               dyn_cast<VectorMutableThyra>(*Gf->sub_view(var_indep)).thyra_vec()
00536               ).assert_not_null()
00537             ,MEB::DERIV_TRANS_MV_BY_ROW
00538             )
00539           );
00540       }
00541     }
00542   }
00543   if(Gc_out) *Gc_out = Gc;
00544   if(Gf_out) *Gf_out = Gf;
00545   if(f_out)  *f_out  = f;
00546   if(c_out)  *c_out  = c;
00547 }
00548 
00549 void NLPThyraModelEvaluatorBase::postprocessBaseOutArgs(
00550   Thyra::ModelEvaluatorBase::OutArgs<value_type>        *model_outArgs_inout
00551   ,VectorMutable                                        *Gf
00552   ,value_type                                           *f
00553   ,VectorMutable                                        *c
00554   ) const
00555 {
00556   typedef Thyra::ModelEvaluatorBase MEB;
00557   MEB::OutArgs<value_type> &model_outArgs = *model_outArgs_inout;
00558   if( f && !f_updated_ ) {
00559     if(g_idx_>=0) {
00560       *f = obj_scale_ * ::Thyra::get_ele(*model_g_,0);
00561     }
00562     else {
00563       *f = 0.0;
00564     }
00565     f_updated_ = true;
00566   }
00567   if( c && !c_updated_ ) {
00568     Teuchos::RCP<Thyra::VectorBase<value_type> >
00569       thyra_c = model_outArgs.get_f();
00570     commit_thyra_vector(*space_c_,c,&thyra_c);
00571     model_outArgs.set_f(Teuchos::null);
00572     c_updated_ = true;
00573   }
00574   if( Gf && !Gf_updated_ ) {
00575     if(g_idx_>=0) {
00576       const Range1D
00577         var_dep   = ( basis_sys_.get() ? basis_sys_->var_dep() : Range1D::Invalid ),
00578         var_indep = ( basis_sys_.get() ? basis_sys_->var_indep() : Range1D() );
00579       if (obj_scale_ != 1.0 )
00580         Vt_S( Gf, obj_scale_ );
00581       if(var_dep.size())
00582         Gf->sub_view(var_dep)->has_changed();
00583       if(var_indep.size())
00584         Gf->sub_view(var_indep)->has_changed();
00585     }
00586     else {
00587       *Gf = 0.0;
00588     }
00589     Gf_updated_ = true;
00590   }
00591 }
00592 
00593 // private
00594 
00595 void NLPThyraModelEvaluatorBase::evalModel( 
00596   const Vector            &x
00597   ,bool                   newx
00598   ,const ZeroOrderInfo    *zero_order_info
00599   ,const ObjGradInfo      *obj_grad_info
00600   ) const
00601 {
00602   typedef Thyra::ModelEvaluatorBase MEB;
00603   //
00604   // Get output and verbosity
00605   //
00606   const Teuchos::RCP<Teuchos::FancyOStream>
00607     out = this->getOStream();
00608   const Teuchos::EVerbosityLevel
00609     verbLevel = ( showModelEvaluatorTrace() ? this->getVerbLevel() : Teuchos::VERB_NONE );
00610   Teuchos::OSTab tab(out);
00611   typedef Teuchos::VerboseObjectTempState<MEB> VOTSME;
00612   VOTSME modelOutputTempState(model_,out,verbLevel);
00613   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00614     *out << "\nEntering MoochoPack::NLPThyraModelEvaluatorBase::evalModel(...) ...\n";
00615   //
00616   // Set the input and output arguments
00617   //
00618   MEB::InArgs<value_type>  model_inArgs  = model_->createInArgs();
00619   MEB::OutArgs<value_type> model_outArgs = model_->createOutArgs();
00620   VectorMutable       *Gf = NULL;
00621   value_type          *f  = NULL;
00622   VectorMutable       *c  = NULL;
00623   preprocessBaseInOutArgs(
00624     x,newx,zero_order_info,obj_grad_info,NULL
00625     ,&model_inArgs,&model_outArgs,NULL,&Gf,&f,&c
00626     );
00627   //
00628   // Evaluate the model
00629   //
00630   model_->evalModel(model_inArgs,model_outArgs);
00631   //
00632   // Postprocess the output arguments
00633   //
00634   postprocessBaseOutArgs(&model_outArgs,Gf,f,c);
00635   //
00636   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00637     *out << "\nLeaving MoochoPack::NLPThyraModelEvaluatorBase::evalModel(...) ...\n";
00638 }
00639 
00640 } // end namespace NLPInterfacePack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:11:01 2011 for MOOCHO (Single Doxygen Collection) by  doxygen 1.6.3