[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
constraintfromobjective_simopt.hpp
1 #ifndef __PHILIP_CONSTRAINTFROMOBJECTIVE_SIMOPT_H__
2 #define __PHILIP_CONSTRAINTFROMOBJECTIVE_SIMOPT_H__
3 #include "ROL_Constraint_SimOpt.hpp"
4 #include "ROL_SingletonVector.hpp"
5 
6 namespace PHiLiP {
7 
9 
19 template<class Real>
20 class ConstraintFromObjective_SimOpt : public ROL::Constraint_SimOpt<Real> {
21 
23  using V = ROL::Vector<Real>;
24 
25 private:
26 
28  const ROL::Ptr<ROL::Objective_SimOpt<Real> > obj_;
30  ROL::Ptr<V> dualVector_1_;
32  ROL::Ptr<V> dualVector_2_;
34  const Real offset_;
39 
41  Real getValue( const V& x ) {
42  return dynamic_cast<const ROL::SingletonVector<Real>&>(x).getValue();
43  }
44 
46  void setValue( V& x, Real val ) {
47  dynamic_cast<ROL::SingletonVector<Real>&>(x).setValue(val);
48  }
49 
50 public:
51  using ROL::Constraint_SimOpt<double>::value;
52  using ROL::Constraint_SimOpt<double>::update;
53  using ROL::Constraint_SimOpt<double>::applyAdjointJacobian_1;
54  using ROL::Constraint_SimOpt<double>::applyAdjointJacobian_2;
55 
58  const ROL::Ptr<ROL::Objective_SimOpt<Real> > &obj,
59  const Real offset = 0 )
60  : obj_(obj)
61  , dualVector_1_(ROL::nullPtr)
62  , dualVector_2_(ROL::nullPtr)
63  , offset_(offset)
64  , isDual1Initialized_(false)
65  , isDual2Initialized_(false) {}
66 
68  const ROL::Ptr<ROL::Objective_SimOpt<Real> > getObjective(void) const { return obj_; }
69 
70 
72  void setParameter( const std::vector<Real> &param ) override
73  {
74  obj_->setParameter(param);
75  ROL::Constraint<Real>::setParameter(param);
76  }
77 
78 
80  void update( const V& des_var_sim, const V& des_var_ctl, bool flag = true, int iter = -1 ) override
81  {
82  obj_->update(des_var_sim, des_var_ctl, flag, iter);
83  }
84 
85  //void update_1( const V& des_var_sim, bool flag, int iter )
86  //{
87  //}
88 
89  //void update_2( const V& des_var_ctl, bool flag, int iter )
90  //{
91  //}
92 
94  void value( V& c, const V& des_var_sim, const V& des_var_ctl, Real& tol ) override
95  {
96  setValue(c, obj_->value(des_var_sim, des_var_ctl, tol) - offset_ );
97  }
98 
100  void applyJacobian_1( V& jacobian_vector, const V& v, const V& des_var_sim, const V& des_var_ctl, Real& tol ) override
101  {
102  if ( !isDual1Initialized_ ) {
103  dualVector_1_ = des_var_sim.dual().clone();
104  isDual1Initialized_ = true;
105  }
106  obj_->gradient_1(*dualVector_1_, des_var_sim, des_var_ctl, tol);
107  setValue(jacobian_vector, v.dot(dualVector_1_->dual()));
108  }
110  void applyJacobian_2( V& jacobian_vector, const V& v, const V& des_var_sim, const V& des_var_ctl, Real& tol ) override
111  {
112  if ( !isDual2Initialized_ ) {
113  dualVector_2_ = des_var_ctl.dual().clone();
114  isDual2Initialized_ = true;
115  }
116  obj_->gradient_2(*dualVector_2_, des_var_sim, des_var_ctl, tol);
117  setValue(jacobian_vector, v.dot(dualVector_2_->dual()));
118  }
119 
121  void applyAdjointJacobian_1( V& ajv, const V& v, const V& des_var_sim, const V& des_var_ctl, Real& tol ) override
122  {
123  obj_->gradient_1(ajv, des_var_sim, des_var_ctl, tol);
124  ajv.scale(getValue(v));
125  }
126 
128  void applyAdjointJacobian_2( V& ajv, const V& v, const V& des_var_sim, const V& des_var_ctl, Real& tol ) override
129  {
130  obj_->gradient_2(ajv, des_var_sim, des_var_ctl, tol);
131  ajv.scale(getValue(v));
132  }
133 
135  void applyAdjointHessian_11 ( ROL::Vector<double> &output_vector,
136  const ROL::Vector<double> &dual,
137  const ROL::Vector<double> &input_vector,
138  const ROL::Vector<double> &des_var_sim,
139  const ROL::Vector<double> &des_var_ctl,
140  double &tol)
141  {
142  obj_->hessVec_11(output_vector, input_vector, des_var_sim, des_var_ctl, tol);
143  output_vector.scale(getValue(dual));
144  }
145 
147  void applyAdjointHessian_12 ( ROL::Vector<double> &output_vector,
148  const ROL::Vector<double> &dual,
149  const ROL::Vector<double> &input_vector,
150  const ROL::Vector<double> &des_var_sim,
151  const ROL::Vector<double> &des_var_ctl,
152  double &tol)
153  {
154  obj_->hessVec_21(output_vector, input_vector, des_var_sim, des_var_ctl, tol);
155  output_vector.scale(getValue(dual));
156  }
157 
159  void applyAdjointHessian_21 ( ROL::Vector<double> &output_vector,
160  const ROL::Vector<double> &dual,
161  const ROL::Vector<double> &input_vector,
162  const ROL::Vector<double> &des_var_sim,
163  const ROL::Vector<double> &des_var_ctl,
164  double &tol)
165  {
166  obj_->hessVec_12(output_vector, input_vector, des_var_sim, des_var_ctl, tol);
167  output_vector.scale(getValue(dual));
168  }
169 
171  void applyAdjointHessian_22 ( ROL::Vector<double> &output_vector,
172  const ROL::Vector<double> &dual,
173  const ROL::Vector<double> &input_vector,
174  const ROL::Vector<double> &des_var_sim,
175  const ROL::Vector<double> &des_var_ctl,
176  double &tol)
177  {
178  obj_->hessVec_22(output_vector, input_vector, des_var_sim, des_var_ctl, tol);
179  output_vector.scale(getValue(dual));
180  }
181 
182 }; // ConstraintFromObjective_SimOpt
183 
184 } // namespace PHiLiP
185 #endif
const ROL::Ptr< ROL::Objective_SimOpt< Real > > obj_
Underlying objective function.
void applyAdjointHessian_21(ROL::Vector< double > &output_vector, const ROL::Vector< double > &dual, const ROL::Vector< double > &input_vector, const ROL::Vector< double > &des_var_sim, const ROL::Vector< double > &des_var_ctl, double &tol)
Returns dual*dIdX1dX2*input_vector where dual should be a scalar, and input_vector has size X2...
void applyAdjointJacobian_1(V &ajv, const V &v, const V &des_var_sim, const V &des_var_ctl, Real &tol) override
Returns v*dIdX1 where v should be a scalar.
void applyAdjointHessian_11(ROL::Vector< double > &output_vector, const ROL::Vector< double > &dual, const ROL::Vector< double > &input_vector, const ROL::Vector< double > &des_var_sim, const ROL::Vector< double > &des_var_ctl, double &tol)
Returns dual*dIdX1dX1*input_vector where dual should be a scalar, and input_vector has size X1...
Files for the baseline physics.
Definition: ADTypes.hpp:10
const ROL::Ptr< ROL::Objective_SimOpt< Real > > getObjective(void) const
Get underlying objective function.
void applyJacobian_1(V &jacobian_vector, const V &v, const V &des_var_sim, const V &des_var_ctl, Real &tol) override
Returns dIdX1.dot(v) where v has the size of X1.
void applyAdjointHessian_12(ROL::Vector< double > &output_vector, const ROL::Vector< double > &dual, const ROL::Vector< double > &input_vector, const ROL::Vector< double > &des_var_sim, const ROL::Vector< double > &des_var_ctl, double &tol)
Returns dual*dIdX2dX1*input_vector where dual should be a scalar, and input_vector has size X1...
ROL::Ptr< V > dualVector_2_
Vector of size n_ctl to store dIdX2.
ROL::Vector< Real > V
Shorthand for ROL::Vector.
ConstraintFromObjective_SimOpt(const ROL::Ptr< ROL::Objective_SimOpt< Real > > &obj, const Real offset=0)
Constructor.
bool isDual1Initialized_
Check whether dualVector_1_ has been initialized.
ROL::Ptr< V > dualVector_1_
Vector of size n_sim to store dIdX1.
void applyJacobian_2(V &jacobian_vector, const V &v, const V &des_var_sim, const V &des_var_ctl, Real &tol) override
Returns dIdX2.dot(v) where v has the size of X2.
const Real offset_
Offset value defining the constraint on the objective function.
void setParameter(const std::vector< Real > &param) override
Set parameter on constraint.
void update(const V &des_var_sim, const V &des_var_ctl, bool flag=true, int iter=-1) override
Update constraint (underlying objective)&#39;s design varibles.
void applyAdjointJacobian_2(V &ajv, const V &v, const V &des_var_sim, const V &des_var_ctl, Real &tol) override
Returns v*dIdX2 where v should be a scalar.
void applyAdjointHessian_22(ROL::Vector< double > &output_vector, const ROL::Vector< double > &dual, const ROL::Vector< double > &input_vector, const ROL::Vector< double > &des_var_sim, const ROL::Vector< double > &des_var_ctl, double &tol)
Returns dual*dIdX2dX2*input_vector where dual should be a scalar, and input_vector has size X2...
void value(V &c, const V &des_var_sim, const V &des_var_ctl, Real &tol) override
Returns the constraint value equal to the objective value minus the offset.
void setValue(V &x, Real val)
Set a value on the vector, where the vector is a SingletonVector.
bool isDual2Initialized_
Check whether dualVector_2_ has been initialized.
Real getValue(const V &x)
Get a vector&#39;s entry value, where the vector is a SingletonVector.
Creates a constraint from an objective function and a offset value.