MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NLPInterfacePack_NLPFirstOrderThyraModelEvaluator.cpp
Go to the documentation of this file.
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 <algorithm>
45 
57 #include "Thyra_ModelEvaluatorHelpers.hpp"
58 #include "Thyra_DetachedVectorView.hpp"
60 #include "Teuchos_Assert.hpp"
61 #include "Teuchos_dyn_cast.hpp"
62 
63 namespace NLPInterfacePack {
64 
66 {}
67 
69  const Teuchos::RCP<Thyra::ModelEvaluator<value_type> > &model
70  ,const int p_idx
71  ,const int g_idx
72  )
73 {
74  initialize(model,p_idx,g_idx);
75 }
76 
78  const Teuchos::RCP<Thyra::ModelEvaluator<value_type> > &model
79  ,const int p_idx
80  ,const int g_idx
81  )
82 {
83  initializeBase(model,p_idx,g_idx);
84 }
85 
86 // Overridden public members from NLP
87 
89 {
90  if(initialized_) {
91  NLPFirstOrder::initialize(test_setup);
92  return;
93  }
95  NLPFirstOrder::initialize(test_setup);
96 }
97 
99 {
101 }
102 
103 // Overridden public members from NLPFirstOrder
104 
106 {
108  Gc_updated_ = false;
109 }
110 
113 {
114  return factory_Gc_;
115 }
116 
119 {
120  return basis_sys_;
121 }
122 
123 // Overridden protected members from NLPFirstOrder
124 
125 void NLPFirstOrderThyraModelEvaluator::imp_calc_Gc(const Vector& x, bool newx, const FirstOrderInfo& first_order_info) const
126 {
127  evalModel(x,newx,NULL,NULL,&first_order_info);
128 }
129 
130 // private
131 
133  const Vector &x
134  ,bool newx
135  ,const ZeroOrderInfo *zero_order_info
136  ,const ObjGradInfo *obj_grad_info
137  ,const FirstOrderInfo *first_order_info
138  ) const
139 {
140  using Teuchos::FancyOStream;
141  using Teuchos::OSTab;
142  using Teuchos::dyn_cast;
143  using Teuchos::RCP;
144  using Teuchos::rcp_const_cast;
145  using Teuchos::rcp_dynamic_cast;
149  typedef Thyra::ModelEvaluatorBase MEB;
151  typedef MEB::DerivativeMultiVector<value_type> DerivMV;
152  typedef MEB::Derivative<value_type> Deriv;
153  //
154  // Get output and verbosity
155  //
157  out = this->getOStream();
159  verbLevel = ( showModelEvaluatorTrace() ? this->getVerbLevel() : Teuchos::VERB_NONE );
160  Teuchos::OSTab tab(out);
161  VOTSME modelOutputTempState(model_,out,verbLevel);
162  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
163  *out << "\nEntering MoochoPack::NLPFirstOrderThyraModelEvaluator::calc_point(...) ...\n";
164  //
165  // Set the input and output arguments
166  //
167  MEB::InArgs<value_type> model_inArgs = model_->createInArgs();
168  MEB::OutArgs<value_type> model_outArgs = model_->createOutArgs();
169  MatrixOp *Gc = NULL;
170  VectorMutable *Gf = NULL;
171  value_type *f = NULL;
172  VectorMutable *c = NULL;
174  x,newx,zero_order_info,obj_grad_info,first_order_info
175  ,&model_inArgs,&model_outArgs,&Gc,&Gf,&f,&c
176  );
177  //
178  MatrixOpNonsing *C_aggr;
179  MatrixOp *N_aggr;
180  if( Gc && !Gc_updated_ ) {
181  BasisSystemComposite::get_C_N( Gc, &C_aggr, &N_aggr ); // Will return NULLs if Gc is not initialized
182  if(C_aggr) {
183  model_outArgs.set_W(
184  rcp_const_cast<Thyra::LinearOpWithSolveBase<value_type> >(
185  dyn_cast<MatrixOpNonsingThyra>(*C_aggr).set_uninitialized()
186  ).assert_not_null()
187  );
188  if(p_idx_ >= 0) {
189  // ToDo: This is implemented for direct sensitivities, change this for adjoint sensitivities
190  model_outArgs.set_DfDp(
191  p_idx_
192  ,DerivMV(
193  rcp_const_cast<Thyra::MultiVectorBase<value_type> >(
194  rcp_dynamic_cast<const Thyra::MultiVectorBase<value_type> >(
195  dyn_cast<MatrixOpThyra>(*N_aggr).set_uninitialized()
196  )
197  ).assert_not_null()
198  ,MEB::DERIV_MV_BY_COL
199  )
200  );
201  }
202  }
203  else {
204  model_outArgs.set_W(model_->create_W().assert_not_null());
205  if(p_idx_>=0)
206  model_outArgs.set_DfDp(p_idx_,Thyra::create_DfDp_mv(*model_,p_idx_,MEB::DERIV_MV_BY_COL));
207  }
208  if (model_inArgs.supports(MEB::IN_ARG_alpha))
209  model_inArgs.set_alpha(0.0);
210  if (model_inArgs.supports(MEB::IN_ARG_beta))
211  model_inArgs.set_beta(1.0);
212  }
213  //
214  // Evaluate the model
215  //
216  model_->evalModel(model_inArgs,model_outArgs);
217  //
218  // Postprocess the output arguments
219  //
220  postprocessBaseOutArgs(&model_outArgs,Gf,f,c);
221  //
222  if( Gc && !Gc_updated_ ) {
223  RCP<MatrixOpNonsing> C_ptr;
224  RCP<MatrixOp> N_ptr;
225  if(!C_aggr) {
226  C_ptr = Teuchos::rcp(new MatrixOpNonsingThyra());
227  C_aggr = &*C_ptr;
228  if(p_idx_>=0) {
229  N_ptr = Teuchos::rcp(new MatrixOpThyra());
230  N_aggr = &*N_ptr;
231  }
232  }
233  RCP<Thyra::LinearOpWithSolveBase<value_type> >
234  model_W = model_outArgs.get_W();
235  model_W->setOStream(out);
236  if(showModelEvaluatorTrace())
237  model_W->setVerbLevel(verbLevel);
238  dyn_cast<MatrixOpNonsingThyra>(*C_aggr).initialize(model_W,BLAS_Cpp::no_trans);
239  // ToDo: This is implemented for direct sensitivities, change this for adjoint sensitivities
240  if(p_idx_>=0)
241  dyn_cast<MatrixOpThyra>(*N_aggr).initialize(model_outArgs.get_DfDp(p_idx_).getDerivativeMultiVector().getMultiVector(),BLAS_Cpp::no_trans);
242  if( C_ptr.get() ) {
243  BasisSystemComposite::initialize_Gc(
244  this->space_x(), basis_sys_->var_dep(), basis_sys_->var_indep()
245  ,this->space_c()
246  ,C_ptr, N_ptr
247  ,Gc
248  );
249  }
250  Gc_updated_ = true;
251  }
252  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
253  *out << "\nLeaving MoochoPack::NLPFirstOrderThyraModelEvaluator::calc_point(...) ...\n";
254 }
255 
256 } // end namespace NLPInterfacePack
virtual value_type & f()
Returns non-const *this->get_f().
Struct for gradient (objective), objective and constriants (pointers)
void initialize(bool test_setup)
Initialize the NLP for its first use.
Abstract interface for immutable, finite dimensional, coordinate vectors {abstract}.
void postprocessBaseOutArgs(Thyra::ModelEvaluatorBase::OutArgs< value_type > *model_outArgs_inout, VectorMutable *Gf, value_type *f, VectorMutable *c) const
void evalModel(const Vector &x, bool newx, const ZeroOrderInfo *zero_order_info, const ObjGradInfo *obj_grad_info, const FirstOrderInfo *first_order_info) const
void initializeBase(const Teuchos::RCP< Thyra::ModelEvaluator< value_type > > &model, const int p_idx, const int g_idx)
Initialize given a Thyra::ModelEvaluator and a description of how to interpret it.
void initialize(const Teuchos::RCP< Thyra::ModelEvaluator< value_type > > &model, const int p_idx, const int g_idx)
.Initialize given a Thyra::ModelEvaluator and a description of how to interpret it.
T * get() const
basic_OSTab< char > OSTab
Not transposed.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void unset_quantities()
Call to unset all storage quantities (both in this class and all subclasses).
T_To & dyn_cast(T_From &from)
const basis_sys_ptr_t basis_sys() const
Returns an ExampleBasisSystem.
VectorMutable adapter subclass for Thyra::VectorBase.
std::ostream * out
Struct for zero and first order quantities (pointers)
void preprocessBaseInOutArgs(const Vector &x, bool newx, const ZeroOrderInfo *zero_order_info, const ObjGradInfo *obj_grad_info, const NLPFirstOrder::FirstOrderInfo *first_order_info, Thyra::ModelEvaluatorBase::InArgs< value_type > *model_inArgs_inout, Thyra::ModelEvaluatorBase::OutArgs< value_type > *model_outArgs_inout, MatrixOp **Gc_out, VectorMutable **Gf_out, value_type **f_out, VectorMutable **c_out) const
Struct for objective and constriants (pointer).
const RCP< T > & assert_not_null() const
MatrixOp adapter subclass for Thyra::LinearOpBase.
MatrixOpNonsing adapter subclass for Thyra::Nonlin::LinearOpWithSolve.
void set_Gc(MatrixOp *Gc)
Overridden to check the concrete type of Gc.
basic_FancyOStream< char > FancyOStream
virtual MatrixOp & Gc()
Returns non-const *this->get_Gc().
Teuchos::RCP< Thyra::ModelEvaluator< value_type > > model_
AbstractLinAlgPack::value_type value_type
Abstract interface for mutable coordinate vectors {abstract}.
void imp_calc_Gc(const Vector &x, bool newx, const FirstOrderInfo &first_order_info) const
virtual VectorMutable & Gf()
Returns non-const *this->get_Gf().
virtual EVerbosityLevel getVerbLevel() const
virtual void set_Gc(MatrixOp *Gc)
Set a pointer to a matrix object to be updated when this->calc_Gc() is called.
virtual VectorMutable & c()
Returns non-const *this->get_c().