MOOCHO (Single Doxygen Collection) Version of the Day
NLPInterfacePack_NLPFirstOrderThyraModelEvaluator.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 #include <assert.h>
00030 
00031 #include <algorithm>
00032 
00033 #include "NLPInterfacePack_NLPFirstOrderThyraModelEvaluator.hpp"
00034 #include "AbstractLinAlgPack_LinAlgOpPack.hpp"
00035 #include "AbstractLinAlgPack_VectorOut.hpp"
00036 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
00037 #include "AbstractLinAlgPack_VectorSpaceThyra.hpp"
00038 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
00039 #include "AbstractLinAlgPack_MatrixOpNonsingThyra.hpp"
00040 #include "AbstractLinAlgPack_BasisSystemComposite.hpp"
00041 #include "AbstractLinAlgPack_VectorSpaceBlocked.hpp"
00042 #include "AbstractLinAlgPack_VectorAuxiliaryOps.hpp"
00043 #include "AbstractLinAlgPack_MatrixSymPosDefCholFactor.hpp"
00044 #include "Thyra_ModelEvaluatorHelpers.hpp"
00045 #include "Thyra_DetachedVectorView.hpp"
00046 #include "Teuchos_AbstractFactoryStd.hpp"
00047 #include "Teuchos_TestForException.hpp"
00048 #include "Teuchos_dyn_cast.hpp"
00049 
00050 namespace NLPInterfacePack {
00051 
00052 NLPFirstOrderThyraModelEvaluator::NLPFirstOrderThyraModelEvaluator()
00053 {}
00054 
00055 NLPFirstOrderThyraModelEvaluator::NLPFirstOrderThyraModelEvaluator(
00056   const Teuchos::RCP<Thyra::ModelEvaluator<value_type> >  &model   
00057   ,const int                                                      p_idx 
00058   ,const int                                                      g_idx 
00059   )
00060 {
00061   initialize(model,p_idx,g_idx);
00062 }
00063 
00064 void NLPFirstOrderThyraModelEvaluator::initialize(
00065   const Teuchos::RCP<Thyra::ModelEvaluator<value_type> >  &model
00066   ,const int                                                      p_idx
00067   ,const int                                                      g_idx
00068   )
00069 {
00070   initializeBase(model,p_idx,g_idx);
00071 }
00072   
00073 // Overridden public members from NLP
00074 
00075 void NLPFirstOrderThyraModelEvaluator::initialize(bool test_setup)
00076 {
00077   if(initialized_) {
00078     NLPFirstOrder::initialize(test_setup);
00079     return;
00080   }
00081   NLPThyraModelEvaluatorBase::initialize(test_setup);
00082   NLPFirstOrder::initialize(test_setup);
00083 }
00084 
00085 void NLPFirstOrderThyraModelEvaluator::unset_quantities()
00086 {
00087   NLPFirstOrder::unset_quantities();
00088 }
00089 
00090 // Overridden public members from NLPFirstOrder
00091 
00092 void NLPFirstOrderThyraModelEvaluator::set_Gc(MatrixOp* Gc)
00093 {
00094   NLPFirstOrder::set_Gc(Gc);
00095   Gc_updated_ = false;
00096 }
00097 
00098 const NLPFirstOrder::mat_fcty_ptr_t
00099 NLPFirstOrderThyraModelEvaluator::factory_Gc() const
00100 {
00101   return factory_Gc_;
00102 }
00103 
00104 const NLPFirstOrder::basis_sys_ptr_t
00105 NLPFirstOrderThyraModelEvaluator::basis_sys() const
00106 {
00107   return basis_sys_;
00108 }
00109 
00110 // Overridden protected members from NLPFirstOrder
00111 
00112 void NLPFirstOrderThyraModelEvaluator::imp_calc_Gc(const Vector& x, bool newx, const FirstOrderInfo& first_order_info) const
00113 {
00114   evalModel(x,newx,NULL,NULL,&first_order_info);
00115 }
00116 
00117 // private
00118 
00119 void NLPFirstOrderThyraModelEvaluator::evalModel( 
00120   const Vector            &x
00121   ,bool                   newx
00122   ,const ZeroOrderInfo    *zero_order_info
00123   ,const ObjGradInfo      *obj_grad_info
00124   ,const FirstOrderInfo   *first_order_info
00125   ) const
00126 {
00127   using Teuchos::FancyOStream;
00128   using Teuchos::OSTab;
00129   using Teuchos::dyn_cast;
00130   using Teuchos::RCP;
00131   using Teuchos::rcp_const_cast;
00132   using Teuchos::rcp_dynamic_cast;
00133   using AbstractLinAlgPack::VectorMutableThyra;
00134   using AbstractLinAlgPack::MatrixOpThyra;
00135   using AbstractLinAlgPack::MatrixOpNonsingThyra;
00136   typedef Thyra::ModelEvaluatorBase MEB;
00137   typedef Teuchos::VerboseObjectTempState<MEB> VOTSME;
00138   typedef MEB::DerivativeMultiVector<value_type> DerivMV;
00139   typedef MEB::Derivative<value_type> Deriv;
00140   //
00141   // Get output and verbosity
00142   //
00143   const Teuchos::RCP<Teuchos::FancyOStream>
00144     out = this->getOStream();
00145   const Teuchos::EVerbosityLevel
00146     verbLevel = ( showModelEvaluatorTrace() ? this->getVerbLevel() : Teuchos::VERB_NONE );
00147   Teuchos::OSTab tab(out);
00148   VOTSME modelOutputTempState(model_,out,verbLevel);
00149   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00150     *out << "\nEntering MoochoPack::NLPFirstOrderThyraModelEvaluator::calc_point(...) ...\n";
00151   //
00152   // Set the input and output arguments
00153   //
00154   MEB::InArgs<value_type>  model_inArgs  = model_->createInArgs();
00155   MEB::OutArgs<value_type> model_outArgs = model_->createOutArgs();
00156   MatrixOp            *Gc = NULL;
00157   VectorMutable       *Gf = NULL;
00158   value_type          *f  = NULL;
00159   VectorMutable       *c  = NULL;
00160   preprocessBaseInOutArgs(
00161     x,newx,zero_order_info,obj_grad_info,first_order_info
00162     ,&model_inArgs,&model_outArgs,&Gc,&Gf,&f,&c
00163     );
00164   //
00165   MatrixOpNonsing  *C_aggr;
00166   MatrixOp         *N_aggr;
00167   if( Gc && !Gc_updated_ ) {
00168     BasisSystemComposite::get_C_N( Gc, &C_aggr, &N_aggr ); // Will return NULLs if Gc is not initialized
00169     if(C_aggr) {
00170       model_outArgs.set_W(
00171         rcp_const_cast<Thyra::LinearOpWithSolveBase<value_type> >(
00172           dyn_cast<MatrixOpNonsingThyra>(*C_aggr).set_uninitialized()
00173           ).assert_not_null()
00174         );
00175       if(p_idx_ >= 0) {
00176         // ToDo: This is implemented for direct sensitivities, change this for adjoint sensitivities
00177         model_outArgs.set_DfDp(
00178           p_idx_
00179           ,DerivMV(
00180             rcp_const_cast<Thyra::MultiVectorBase<value_type> >(
00181               rcp_dynamic_cast<const Thyra::MultiVectorBase<value_type> >(
00182                 dyn_cast<MatrixOpThyra>(*N_aggr).set_uninitialized()
00183                 )
00184               ).assert_not_null()
00185             ,MEB::DERIV_MV_BY_COL
00186             )
00187           );
00188       }
00189     }
00190     else {
00191       model_outArgs.set_W(model_->create_W().assert_not_null());
00192       if(p_idx_>=0)
00193         model_outArgs.set_DfDp(p_idx_,Thyra::create_DfDp_mv(*model_,p_idx_,MEB::DERIV_MV_BY_COL));
00194     }
00195     if (model_inArgs.supports(MEB::IN_ARG_alpha))
00196       model_inArgs.set_alpha(0.0);
00197     if (model_inArgs.supports(MEB::IN_ARG_beta))
00198       model_inArgs.set_beta(1.0);
00199   }
00200   //
00201   // Evaluate the model
00202   //
00203   model_->evalModel(model_inArgs,model_outArgs);
00204   //
00205   // Postprocess the output arguments
00206   //
00207   postprocessBaseOutArgs(&model_outArgs,Gf,f,c);
00208   //
00209   if( Gc && !Gc_updated_ ) {
00210     RCP<MatrixOpNonsing> C_ptr;
00211     RCP<MatrixOp>        N_ptr;
00212     if(!C_aggr) {
00213       C_ptr  = Teuchos::rcp(new MatrixOpNonsingThyra());
00214       C_aggr = &*C_ptr;
00215       if(p_idx_>=0) {
00216         N_ptr  = Teuchos::rcp(new MatrixOpThyra());
00217         N_aggr = &*N_ptr;
00218       }
00219     }
00220     RCP<Thyra::LinearOpWithSolveBase<value_type> >
00221       model_W = model_outArgs.get_W();
00222     model_W->setOStream(out);
00223     if(showModelEvaluatorTrace())
00224       model_W->setVerbLevel(verbLevel);
00225     dyn_cast<MatrixOpNonsingThyra>(*C_aggr).initialize(model_W,BLAS_Cpp::no_trans);
00226     // ToDo: This is implemented for direct sensitivities, change this for adjoint sensitivities
00227     if(p_idx_>=0)
00228       dyn_cast<MatrixOpThyra>(*N_aggr).initialize(model_outArgs.get_DfDp(p_idx_).getDerivativeMultiVector().getMultiVector(),BLAS_Cpp::no_trans);
00229     if( C_ptr.get() ) {
00230       BasisSystemComposite::initialize_Gc(
00231         this->space_x(), basis_sys_->var_dep(), basis_sys_->var_indep()
00232         ,this->space_c()
00233         ,C_ptr, N_ptr
00234         ,Gc
00235         );
00236     }
00237     Gc_updated_ = true;
00238   }
00239   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00240     *out << "\nLeaving MoochoPack::NLPFirstOrderThyraModelEvaluator::calc_point(...) ...\n";
00241 }
00242 
00243 } // end namespace NLPInterfacePack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines