MOOCHO/Thyra Adapter Software Version of the Day
NLPInterfacePack_NLPFirstOrderThyraModelEvaluator.cpp
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <assert.h>
00043 
00044 #include <algorithm>
00045 
00046 #include "NLPInterfacePack_NLPFirstOrderThyraModelEvaluator.hpp"
00047 #include "AbstractLinAlgPack_LinAlgOpPack.hpp"
00048 #include "AbstractLinAlgPack_VectorOut.hpp"
00049 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
00050 #include "AbstractLinAlgPack_VectorSpaceThyra.hpp"
00051 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
00052 #include "AbstractLinAlgPack_MatrixOpNonsingThyra.hpp"
00053 #include "AbstractLinAlgPack_BasisSystemComposite.hpp"
00054 #include "AbstractLinAlgPack_VectorSpaceBlocked.hpp"
00055 #include "AbstractLinAlgPack_VectorAuxiliaryOps.hpp"
00056 #include "AbstractLinAlgPack_MatrixSymPosDefCholFactor.hpp"
00057 #include "Thyra_ModelEvaluatorHelpers.hpp"
00058 #include "Thyra_DetachedVectorView.hpp"
00059 #include "Teuchos_AbstractFactoryStd.hpp"
00060 #include "Teuchos_Assert.hpp"
00061 #include "Teuchos_dyn_cast.hpp"
00062 
00063 namespace NLPInterfacePack {
00064 
00065 NLPFirstOrderThyraModelEvaluator::NLPFirstOrderThyraModelEvaluator()
00066 {}
00067 
00068 NLPFirstOrderThyraModelEvaluator::NLPFirstOrderThyraModelEvaluator(
00069   const Teuchos::RCP<Thyra::ModelEvaluator<value_type> >  &model   
00070   ,const int                                                      p_idx 
00071   ,const int                                                      g_idx 
00072   )
00073 {
00074   initialize(model,p_idx,g_idx);
00075 }
00076 
00077 void NLPFirstOrderThyraModelEvaluator::initialize(
00078   const Teuchos::RCP<Thyra::ModelEvaluator<value_type> >  &model
00079   ,const int                                                      p_idx
00080   ,const int                                                      g_idx
00081   )
00082 {
00083   initializeBase(model,p_idx,g_idx);
00084 }
00085   
00086 // Overridden public members from NLP
00087 
00088 void NLPFirstOrderThyraModelEvaluator::initialize(bool test_setup)
00089 {
00090   if(initialized_) {
00091     NLPFirstOrder::initialize(test_setup);
00092     return;
00093   }
00094   NLPThyraModelEvaluatorBase::initialize(test_setup);
00095   NLPFirstOrder::initialize(test_setup);
00096 }
00097 
00098 void NLPFirstOrderThyraModelEvaluator::unset_quantities()
00099 {
00100   NLPFirstOrder::unset_quantities();
00101 }
00102 
00103 // Overridden public members from NLPFirstOrder
00104 
00105 void NLPFirstOrderThyraModelEvaluator::set_Gc(MatrixOp* Gc)
00106 {
00107   NLPFirstOrder::set_Gc(Gc);
00108   Gc_updated_ = false;
00109 }
00110 
00111 const NLPFirstOrder::mat_fcty_ptr_t
00112 NLPFirstOrderThyraModelEvaluator::factory_Gc() const
00113 {
00114   return factory_Gc_;
00115 }
00116 
00117 const NLPFirstOrder::basis_sys_ptr_t
00118 NLPFirstOrderThyraModelEvaluator::basis_sys() const
00119 {
00120   return basis_sys_;
00121 }
00122 
00123 // Overridden protected members from NLPFirstOrder
00124 
00125 void NLPFirstOrderThyraModelEvaluator::imp_calc_Gc(const Vector& x, bool newx, const FirstOrderInfo& first_order_info) const
00126 {
00127   evalModel(x,newx,NULL,NULL,&first_order_info);
00128 }
00129 
00130 // private
00131 
00132 void NLPFirstOrderThyraModelEvaluator::evalModel( 
00133   const Vector            &x
00134   ,bool                   newx
00135   ,const ZeroOrderInfo    *zero_order_info
00136   ,const ObjGradInfo      *obj_grad_info
00137   ,const FirstOrderInfo   *first_order_info
00138   ) const
00139 {
00140   using Teuchos::FancyOStream;
00141   using Teuchos::OSTab;
00142   using Teuchos::dyn_cast;
00143   using Teuchos::RCP;
00144   using Teuchos::rcp_const_cast;
00145   using Teuchos::rcp_dynamic_cast;
00146   using AbstractLinAlgPack::VectorMutableThyra;
00147   using AbstractLinAlgPack::MatrixOpThyra;
00148   using AbstractLinAlgPack::MatrixOpNonsingThyra;
00149   typedef Thyra::ModelEvaluatorBase MEB;
00150   typedef Teuchos::VerboseObjectTempState<MEB> VOTSME;
00151   typedef MEB::DerivativeMultiVector<value_type> DerivMV;
00152   typedef MEB::Derivative<value_type> Deriv;
00153   //
00154   // Get output and verbosity
00155   //
00156   const Teuchos::RCP<Teuchos::FancyOStream>
00157     out = this->getOStream();
00158   const Teuchos::EVerbosityLevel
00159     verbLevel = ( showModelEvaluatorTrace() ? this->getVerbLevel() : Teuchos::VERB_NONE );
00160   Teuchos::OSTab tab(out);
00161   VOTSME modelOutputTempState(model_,out,verbLevel);
00162   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00163     *out << "\nEntering MoochoPack::NLPFirstOrderThyraModelEvaluator::calc_point(...) ...\n";
00164   //
00165   // Set the input and output arguments
00166   //
00167   MEB::InArgs<value_type>  model_inArgs  = model_->createInArgs();
00168   MEB::OutArgs<value_type> model_outArgs = model_->createOutArgs();
00169   MatrixOp            *Gc = NULL;
00170   VectorMutable       *Gf = NULL;
00171   value_type          *f  = NULL;
00172   VectorMutable       *c  = NULL;
00173   preprocessBaseInOutArgs(
00174     x,newx,zero_order_info,obj_grad_info,first_order_info
00175     ,&model_inArgs,&model_outArgs,&Gc,&Gf,&f,&c
00176     );
00177   //
00178   MatrixOpNonsing  *C_aggr;
00179   MatrixOp         *N_aggr;
00180   if( Gc && !Gc_updated_ ) {
00181     BasisSystemComposite::get_C_N( Gc, &C_aggr, &N_aggr ); // Will return NULLs if Gc is not initialized
00182     if(C_aggr) {
00183       model_outArgs.set_W(
00184         rcp_const_cast<Thyra::LinearOpWithSolveBase<value_type> >(
00185           dyn_cast<MatrixOpNonsingThyra>(*C_aggr).set_uninitialized()
00186           ).assert_not_null()
00187         );
00188       if(p_idx_ >= 0) {
00189         // ToDo: This is implemented for direct sensitivities, change this for adjoint sensitivities
00190         model_outArgs.set_DfDp(
00191           p_idx_
00192           ,DerivMV(
00193             rcp_const_cast<Thyra::MultiVectorBase<value_type> >(
00194               rcp_dynamic_cast<const Thyra::MultiVectorBase<value_type> >(
00195                 dyn_cast<MatrixOpThyra>(*N_aggr).set_uninitialized()
00196                 )
00197               ).assert_not_null()
00198             ,MEB::DERIV_MV_BY_COL
00199             )
00200           );
00201       }
00202     }
00203     else {
00204       model_outArgs.set_W(model_->create_W().assert_not_null());
00205       if(p_idx_>=0)
00206         model_outArgs.set_DfDp(p_idx_,Thyra::create_DfDp_mv(*model_,p_idx_,MEB::DERIV_MV_BY_COL));
00207     }
00208     if (model_inArgs.supports(MEB::IN_ARG_alpha))
00209       model_inArgs.set_alpha(0.0);
00210     if (model_inArgs.supports(MEB::IN_ARG_beta))
00211       model_inArgs.set_beta(1.0);
00212   }
00213   //
00214   // Evaluate the model
00215   //
00216   model_->evalModel(model_inArgs,model_outArgs);
00217   //
00218   // Postprocess the output arguments
00219   //
00220   postprocessBaseOutArgs(&model_outArgs,Gf,f,c);
00221   //
00222   if( Gc && !Gc_updated_ ) {
00223     RCP<MatrixOpNonsing> C_ptr;
00224     RCP<MatrixOp>        N_ptr;
00225     if(!C_aggr) {
00226       C_ptr  = Teuchos::rcp(new MatrixOpNonsingThyra());
00227       C_aggr = &*C_ptr;
00228       if(p_idx_>=0) {
00229         N_ptr  = Teuchos::rcp(new MatrixOpThyra());
00230         N_aggr = &*N_ptr;
00231       }
00232     }
00233     RCP<Thyra::LinearOpWithSolveBase<value_type> >
00234       model_W = model_outArgs.get_W();
00235     model_W->setOStream(out);
00236     if(showModelEvaluatorTrace())
00237       model_W->setVerbLevel(verbLevel);
00238     dyn_cast<MatrixOpNonsingThyra>(*C_aggr).initialize(model_W,BLAS_Cpp::no_trans);
00239     // ToDo: This is implemented for direct sensitivities, change this for adjoint sensitivities
00240     if(p_idx_>=0)
00241       dyn_cast<MatrixOpThyra>(*N_aggr).initialize(model_outArgs.get_DfDp(p_idx_).getDerivativeMultiVector().getMultiVector(),BLAS_Cpp::no_trans);
00242     if( C_ptr.get() ) {
00243       BasisSystemComposite::initialize_Gc(
00244         this->space_x(), basis_sys_->var_dep(), basis_sys_->var_indep()
00245         ,this->space_c()
00246         ,C_ptr, N_ptr
00247         ,Gc
00248         );
00249     }
00250     Gc_updated_ = true;
00251   }
00252   if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
00253     *out << "\nLeaving MoochoPack::NLPFirstOrderThyraModelEvaluator::calc_point(...) ...\n";
00254 }
00255 
00256 } // end namespace NLPInterfacePack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends