MOOCHO  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
NLPInterfacePack_ExampleNLPFirstOrder.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <assert.h>
43 
44 #include <stdexcept>
45 
46 #include "NLPInterfacePack_ExampleNLPFirstOrder.hpp"
47 #include "NLPInterfacePack_ExampleBasisSystem.hpp"
48 #include "AbstractLinAlgPack_BasisSystemComposite.hpp"
49 #include "AbstractLinAlgPack_MatrixSymDiagStd.hpp"
50 #include "AbstractLinAlgPack_MatrixComposite.hpp"
51 #include "AbstractLinAlgPack_VectorStdOps.hpp"
52 #include "ReleaseResource_ref_count_ptr.hpp"
53 #include "Teuchos_dyn_cast.hpp"
54 #include "Teuchos_Assert.hpp"
55 
56 namespace NLPInterfacePack {
57 
59  const VectorSpace::space_ptr_t& vec_space
60  ,value_type xo
61  ,bool has_bounds
62  ,bool dep_bounded
63  )
64  :ExampleNLPObjGrad(vec_space,xo,has_bounds,dep_bounded)
65  ,initialized_(false)
66 {
67  basis_sys_ = Teuchos::rcp(
69  this->space_x()
70  ,this->var_dep()
71  ,this->var_indep()
72  )
73  );
74 }
75 
76 // Overridden public members from NLPFirstOrder
77 
78 void ExampleNLPFirstOrder::set_Gc(MatrixOp* Gc)
79 {
80  if(Gc) // Throw an exception if this matrix is the wrong type!
83 }
84 
87 {
88  return factory_Gc_;
89 }
90 
93 {
94  return basis_sys_;
95 }
96 
97 // Overridden public members from NLP
98 
99 void ExampleNLPFirstOrder::initialize(bool test_setup)
100 {
101  namespace rcp = MemMngPack;
102 
103  ExampleNLPObjGrad::initialize(test_setup);
104  NLPFirstOrder::initialize(test_setup);
105 
106  factory_Gc_ = BasisSystemComposite::factory_Gc();
107 
108  initialized_ = true;
109 }
110 
112 {
113  return initialized_;
114 }
115 
116 // Overridden protected members from NLPFirstOrder
117 
119  const Vector& x, bool newx, const FirstOrderInfo& first_order_info) const
120 {
121  namespace rcp = MemMngPack;
122  using Teuchos::dyn_cast;
123  using AbstractLinAlgPack::Vp_S; // Should not have to do this!
124 
125  const index_type
126  n = this->n(),
127  m = this->m();
128  const Range1D
129  var_dep = this->var_dep(),
130  var_indep = this->var_indep();
131 
132  // Get references to aggregate C and N matrices (if allocated)
133  MatrixOpNonsing
134  *C_aggr = NULL;
135  MatrixOp
136  *N_aggr = NULL;
137  BasisSystemComposite::get_C_N(
138  first_order_info.Gc, &C_aggr, &N_aggr ); // Will return NULLs if Gc is not initialized
139 
140  // Allocate C and N matrix objects if not done yet!
142  C_ptr = Teuchos::null;
144  N_ptr = Teuchos::null;
145  if( C_aggr == NULL ) {
146  const VectorSpace::space_ptr_t
147  space_x = this->space_x(),
148  space_xD = space_x->sub_space(var_dep);
149  C_ptr = Teuchos::rcp(new MatrixSymDiagStd(space_xD->create_member()));
150  N_ptr = Teuchos::rcp(new MatrixSymDiagStd(space_xD->create_member()));
151  C_aggr = C_ptr.get();
152  N_aggr = N_ptr.get();
153  }
154 
155  // Get references to concreate C and N matrices
156  MatrixSymDiagStd
157  &C = dyn_cast<MatrixSymDiagStd>(*C_aggr);
158  MatrixSymDiagStd
159  &N = dyn_cast<MatrixSymDiagStd>(*N_aggr);
160  // Get x = [ x_D' x_I ]
161  Vector::vec_ptr_t
162  x_D = x.sub_view(var_dep),
163  x_I = x.sub_view(var_indep);
164  // Set the diagonals of C and N (this is the only computation going on here)
165  C.diag() = *x_I; // C.diag = x_I - 1.0
166  Vp_S( &C.diag(), -1.0 ); // ...
167  N.diag() = *x_D; // N.diag = x_D - 10.0
168  Vp_S( &N.diag(), -10.0 ); // ...
169  // Initialize the matrix object Gc if not done so yet
170  if( C_ptr.get() != NULL ) {
171  BasisSystemComposite::initialize_Gc(
172  this->space_x(), var_dep, var_indep
173  ,this->space_c()
174  ,C_ptr, N_ptr
175  ,first_order_info.Gc
176  );
177  }
178 }
179 
180 } // end namespace NLPInterfacePack
void initialize(bool test_setup)
ExampleNLPFirstOrder(const VectorSpace::space_ptr_t &vec_space, value_type xo, bool has_bounds, bool dep_bounded)
Constructor (see ExampleNLPDirect::ExampleNLPDirect()).
T_To & dyn_cast(T_From &from)
T * get() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void imp_calc_Gc(const Vector &x, bool newx, const FirstOrderInfo &first_order_info) const
Simple example NLP subclass to illustrate how to implement the NLPObjGrad interface for a specialized...
void set_Gc(MatrixOp *Gc)
Overridden to check the concrete type of Gc.
void Vp_S(VectorMutable *v_lhs, const value_type &alpha)
const basis_sys_ptr_t basis_sys() const
Returns an ExampleBasisSystem.
Subclass of BasisSystem for example NLP.
virtual void set_Gc(MatrixOp *Gc)
const NLPFirstOrder::mat_fcty_ptr_t factory_Gc() const