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::RefCountPtr<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::RefCountPtr<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::RefCountPtr;
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 RefCountPtr<FancyOStream> out       = this->getOStream();
00144   const Teuchos::EVerbosityLevel  verbLevel = this->getVerbLevel();
00145   Teuchos::OSTab tab(out);
00146   VOTSME modelOutputTempState(model_,out,verbLevel);
00147   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00148     *out << "\nEntering MoochoPack::NLPFirstOrderThyraModelEvaluator::calc_point(...) ...\n";
00149   //
00150   // Set the input and output arguments
00151   //
00152   MEB::InArgs<value_type>  model_inArgs  = model_->createInArgs();
00153   MEB::OutArgs<value_type> model_outArgs = model_->createOutArgs();
00154   MatrixOp            *Gc = NULL;
00155   VectorMutable       *Gf = NULL;
00156   value_type          *f  = NULL;
00157   VectorMutable       *c  = NULL;
00158   preprocessBaseInOutArgs(
00159     x,newx,zero_order_info,obj_grad_info,first_order_info
00160     ,&model_inArgs,&model_outArgs,&Gc,&Gf,&f,&c
00161     );
00162   //
00163   MatrixOpNonsing  *C_aggr;
00164   MatrixOp         *N_aggr;
00165   if( Gc && !Gc_updated_ ) {
00166     BasisSystemComposite::get_C_N( Gc, &C_aggr, &N_aggr ); // Will return NULLs if Gc is not initialized
00167     if(C_aggr) {
00168       model_outArgs.set_W(
00169         rcp_const_cast<Thyra::LinearOpWithSolveBase<value_type> >(
00170           dyn_cast<MatrixOpNonsingThyra>(*C_aggr).set_uninitialized()
00171           ).assert_not_null()
00172         );
00173       if(p_idx_ >= 0) {
00174         // ToDo: This is implemented for direct sensitivities, change this for adjoint sensitivities
00175         model_outArgs.set_DfDp(
00176           p_idx_
00177           ,DerivMV(
00178             rcp_const_cast<Thyra::MultiVectorBase<value_type> >(
00179               rcp_dynamic_cast<const Thyra::MultiVectorBase<value_type> >(
00180                 dyn_cast<MatrixOpThyra>(*N_aggr).set_uninitialized()
00181                 )
00182               ).assert_not_null()
00183             ,MEB::DERIV_MV_BY_COL
00184             )
00185           );
00186       }
00187     }
00188     else {
00189       model_outArgs.set_W(model_->create_W().assert_not_null());
00190       if(p_idx_>=0)
00191         model_outArgs.set_DfDp(p_idx_,Thyra::create_DfDp_mv(*model_,p_idx_,MEB::DERIV_MV_BY_COL));
00192     }
00193     if(model_inArgs.supports(MEB::IN_ARG_alpha)) model_inArgs.set_alpha(0.0);
00194   }
00195   //
00196   // Evaluate the model
00197   //
00198   model_->evalModel(model_inArgs,model_outArgs);
00199   //
00200   // Postprocess the output arguments
00201   //
00202   postprocessBaseOutArgs(&model_outArgs,Gf,f,c);
00203   //
00204   if( Gc && !Gc_updated_ ) {
00205     RefCountPtr<MatrixOpNonsing> C_ptr;
00206     RefCountPtr<MatrixOp>        N_ptr;
00207     if(!C_aggr) {
00208       C_ptr  = Teuchos::rcp(new MatrixOpNonsingThyra());
00209       C_aggr = &*C_ptr;
00210       if(p_idx_>=0) {
00211         N_ptr  = Teuchos::rcp(new MatrixOpThyra());
00212         N_aggr = &*N_ptr;
00213       }
00214     }
00215     RefCountPtr<Thyra::LinearOpWithSolveBase<value_type> >
00216       model_W = model_outArgs.get_W();
00217     model_W->setOStream(out);
00218     model_W->setVerbLevel(verbLevel);
00219     dyn_cast<MatrixOpNonsingThyra>(*C_aggr).initialize(model_W,BLAS_Cpp::no_trans);
00220     // ToDo: This is implemented for direct sensitivities, change this for adjoint sensitivities
00221     if(p_idx_>=0)
00222       dyn_cast<MatrixOpThyra>(*N_aggr).initialize(model_outArgs.get_DfDp(p_idx_).getDerivativeMultiVector().getMultiVector(),BLAS_Cpp::no_trans);
00223     if( C_ptr.get() ) {
00224       BasisSystemComposite::initialize_Gc(
00225         this->space_x(), basis_sys_->var_dep(), basis_sys_->var_indep()
00226         ,this->space_c()
00227         ,C_ptr, N_ptr
00228         ,Gc
00229         );
00230     }
00231     Gc_updated_ = true;
00232   }
00233   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00234     *out << "\nLeaving MoochoPack::NLPFirstOrderThyraModelEvaluator::calc_point(...) ...\n";
00235 }
00236 
00237 } // end namespace NLPInterfacePack

Generated on Thu Sep 18 12:35:19 2008 for MOOCHO (Single Doxygen Collection) by doxygen 1.3.9.1