NLPInterfacePack_ExampleNLPFirstOrder.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 // 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 <stdexcept>
00032 
00033 #include "NLPInterfacePack_ExampleNLPFirstOrder.hpp"
00034 #include "NLPInterfacePack_ExampleBasisSystem.hpp"
00035 #include "AbstractLinAlgPack_BasisSystemComposite.hpp"
00036 #include "AbstractLinAlgPack_MatrixSymDiagStd.hpp"
00037 #include "AbstractLinAlgPack_MatrixComposite.hpp"
00038 #include "AbstractLinAlgPack_VectorStdOps.hpp"
00039 #include "ReleaseResource_ref_count_ptr.hpp"
00040 #include "Teuchos_dyn_cast.hpp"
00041 #include "Teuchos_TestForException.hpp"
00042 
00043 namespace NLPInterfacePack {
00044 
00045 ExampleNLPFirstOrder::ExampleNLPFirstOrder(
00046   const VectorSpace::space_ptr_t&  vec_space
00047   ,value_type                      xo
00048   ,bool                            has_bounds
00049   ,bool                            dep_bounded
00050   )
00051   :ExampleNLPObjGrad(vec_space,xo,has_bounds,dep_bounded)
00052   ,initialized_(false)
00053 {
00054   basis_sys_ = Teuchos::rcp(
00055     new ExampleBasisSystem(
00056       this->space_x()
00057       ,this->var_dep()
00058       ,this->var_indep()
00059       )
00060     );
00061 }
00062 
00063 // Overridden public members from NLPFirstOrder
00064 
00065 void ExampleNLPFirstOrder::set_Gc(MatrixOp* Gc)
00066 {
00067   if(Gc) // Throw an exception if this matrix is the wrong type!
00068     Teuchos::dyn_cast<AbstractLinAlgPack::MatrixComposite>(*Gc);
00069   NLPFirstOrder::set_Gc(Gc);
00070 }
00071 
00072 const NLPFirstOrder::mat_fcty_ptr_t
00073 ExampleNLPFirstOrder::factory_Gc() const
00074 {
00075   return factory_Gc_;
00076 }
00077 
00078 const NLPFirstOrder::basis_sys_ptr_t
00079 ExampleNLPFirstOrder::basis_sys() const
00080 {
00081   return basis_sys_;
00082 }
00083 
00084 // Overridden public members from NLP
00085 
00086 void ExampleNLPFirstOrder::initialize(bool test_setup)
00087 {
00088   namespace rcp = MemMngPack;
00089 
00090   ExampleNLPObjGrad::initialize(test_setup);
00091   NLPFirstOrder::initialize(test_setup);
00092 
00093   factory_Gc_ = BasisSystemComposite::factory_Gc();
00094   
00095   initialized_ = true;
00096 }
00097 
00098 bool ExampleNLPFirstOrder::is_initialized() const
00099 {
00100   return initialized_;
00101 }
00102 
00103 // Overridden protected members from NLPFirstOrder
00104 
00105 void ExampleNLPFirstOrder::imp_calc_Gc(
00106   const Vector& x, bool newx, const FirstOrderInfo& first_order_info) const
00107 {
00108   namespace rcp = MemMngPack;
00109   using Teuchos::dyn_cast;
00110   using AbstractLinAlgPack::Vp_S; // Should not have to do this!
00111 
00112   const index_type
00113     n = this->n(),
00114     m = this->m();
00115   const Range1D
00116     var_dep   = this->var_dep(),
00117     var_indep = this->var_indep();
00118 
00119   // Get references to aggregate C and N matrices (if allocated)
00120   MatrixOpNonsing
00121     *C_aggr = NULL;
00122   MatrixOp
00123     *N_aggr = NULL;
00124   BasisSystemComposite::get_C_N(
00125     first_order_info.Gc, &C_aggr, &N_aggr ); // Will return NULLs if Gc is not initialized
00126 
00127   // Allocate C and N matrix objects if not done yet!
00128   Teuchos::RCP<MatrixOpNonsing>
00129     C_ptr = Teuchos::null;
00130   Teuchos::RCP<MatrixOp>
00131     N_ptr = Teuchos::null;
00132   if( C_aggr == NULL ) {
00133     const VectorSpace::space_ptr_t
00134       space_x  = this->space_x(),
00135       space_xD = space_x->sub_space(var_dep);
00136     C_ptr  = Teuchos::rcp(new MatrixSymDiagStd(space_xD->create_member()));
00137     N_ptr  = Teuchos::rcp(new MatrixSymDiagStd(space_xD->create_member()));
00138     C_aggr = C_ptr.get();
00139     N_aggr = N_ptr.get();
00140   }
00141 
00142   // Get references to concreate C and N matrices
00143   MatrixSymDiagStd
00144     &C = dyn_cast<MatrixSymDiagStd>(*C_aggr);
00145   MatrixSymDiagStd
00146     &N = dyn_cast<MatrixSymDiagStd>(*N_aggr);
00147   // Get x = [ x_D' x_I ]
00148   Vector::vec_ptr_t
00149     x_D = x.sub_view(var_dep),
00150     x_I = x.sub_view(var_indep);
00151   // Set the diagonals of C and N (this is the only computation going on here)
00152   C.diag() = *x_I;          // C.diag = x_I - 1.0
00153   Vp_S( &C.diag(),  -1.0 ); // ...
00154   N.diag() = *x_D;          // N.diag = x_D - 10.0
00155   Vp_S( &N.diag(), -10.0 ); // ...
00156   // Initialize the matrix object Gc if not done so yet
00157   if( C_ptr.get() != NULL ) {
00158     BasisSystemComposite::initialize_Gc(
00159       this->space_x(), var_dep, var_indep
00160       ,this->space_c()
00161       ,C_ptr, N_ptr
00162       ,first_order_info.Gc
00163       );
00164   }
00165 }
00166 
00167 } // end namespace NLPInterfacePack

Generated on Tue Jul 13 09:36:22 2010 for MOOCHO by  doxygen 1.4.7