Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_DiagonalQuadraticResponseOnlyModelEvaluator_def.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef OPTIPACK_DIAGONAL_QUADRATIC_RESPONSE_ONLY_MODEL_EVALUATOR_DEF_HPP
11 #define OPTIPACK_DIAGONAL_QUADRATIC_RESPONSE_ONLY_MODEL_EVALUATOR_DEF_HPP
12 
13 
14 #include "Thyra_DiagonalQuadraticResponseOnlyModelEvaluator_decl.hpp"
15 #include "Thyra_DiagonalScalarProd.hpp"
16 #include "Thyra_VectorStdOps.hpp"
17 #include "Thyra_DefaultSpmdVectorSpace.hpp"
18 #include "Thyra_DetachedSpmdVectorView.hpp"
19 #include "Teuchos_DefaultComm.hpp"
20 #include "Teuchos_CommHelpers.hpp"
21 #include "Teuchos_Assert.hpp"
22 
23 
24 namespace Thyra {
25 
26 
27 //
28 // DiagonalQuadraticResponseOnlyModelEvaluator
29 //
30 
31 
32 // Constructors, Initialization, Misc.
33 
34 
35 template<class Scalar>
37  const int localDim,
38  const RCP<const Teuchos::Comm<Thyra::Ordinal> > &comm
39  )
40  :Np_(1), Ng_(1), comm_(comm), localDim_(localDim),
41  nonlinearTermFactor_(0.0), g_offset_(0.0)
42 {
43 
44  typedef ScalarTraits<Scalar> ST;
45  using Thyra::createMember;
46 
47  TEUCHOS_ASSERT( localDim > 0 );
48 
49  // Get the comm
50  if (is_null(comm_)) {
52  }
53 
54  // Locally replicated space for g
55  g_space_ = Thyra::locallyReplicatedDefaultSpmdVectorSpace<Scalar>(comm_, 1);
56 
57  // Distributed space for p
58  p_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(comm_, localDim, -1);
59 
60  // Default solution
61  const RCP<Thyra::VectorBase<Scalar> > ps = createMember<Scalar>(p_space_);
62  V_S(ps.ptr(), ST::zero());
63  ps_ = ps;
64 
65  // Default diagonal
66  const RCP<Thyra::VectorBase<Scalar> > diag = createMember<Scalar>(p_space_);
67  V_S(diag.ptr(), ST::one());
68  diag_ = diag;
69  diag_bar_ = diag;
70 
71  // Default inner product
72  const RCP<Thyra::VectorBase<Scalar> > s_bar = createMember<Scalar>(p_space_);
73  V_S(s_bar.ptr(), ST::one());
74  s_bar_ = s_bar;
75 
76  // Default response offset
77  g_offset_ = ST::zero();
78 
79 }
80 
81 
82 template<class Scalar>
84  const RCP<const Thyra::VectorBase<Scalar> > &ps)
85 {
86  ps_ = ps.assert_not_null();
87 }
88 
89 
90 template<class Scalar>
93 {
94  return ps_;
95 }
96 
97 
98 template<class Scalar>
100  const RCP<const Thyra::VectorBase<Scalar> > &diag)
101 {
102  diag_ = diag;
103  diag_bar_ = diag;
104 }
105 
106 
107 template<class Scalar>
109  const RCP<const Thyra::VectorBase<Scalar> > &diag_bar)
110 {
111 
113  using Teuchos::rcp_dynamic_cast;
114  using Thyra::createMember;
115  using Thyra::ele_wise_divide;
116  using Thyra::V_S;
117 
118  diag_bar_ = diag_bar.assert_not_null();
119 
120  // Reset the scalar product for p_space!
121 
122  RCP<Thyra::VectorBase<Scalar> > s_bar = createMember<Scalar>(p_space_->clone());
123  // NOTE: We have to clone the vector space in order to avoid creating a
124  // circular reference between the space and the vector that defines the
125  // scalar product for the vector space.
126 
127  // s_bar[i] = diag[i] / diag_bar[i]
128  V_S( s_bar.ptr(), ST::zero() );
129  ele_wise_divide( ST::one(), *diag_, *diag_bar_, s_bar.ptr() );
130  s_bar_ = s_bar;
131 
133  rcp_dynamic_cast<Thyra::ScalarProdVectorSpaceBase<Scalar> >(p_space_, true);
134  sp_p_space->setScalarProd(diagonalScalarProd<Scalar>(s_bar_));
135 
136 }
137 
138 
139 template<class Scalar>
141  const Scalar &nonlinearTermFactor)
142 {
143  nonlinearTermFactor_ = nonlinearTermFactor;
144 }
145 
146 
147 template<class Scalar>
149  const Scalar &g_offset)
150 {
151  g_offset_ = g_offset;
152 }
153 
154 
155 // Public functions overridden from ModelEvaulator
156 
157 
158 template<class Scalar>
160 {
161  return Np_;
162 }
163 
164 
165 template<class Scalar>
167 {
168  return Ng_;
169 }
170 
171 
172 template<class Scalar>
175 {
176 #ifdef TEUCHOS_DEBUG
178 #else
179  (void)l;
180 #endif
181  return p_space_;
182 }
183 
184 
185 template<class Scalar>
188 {
189 #ifdef TEUCHOS_DEBUG
191 #else
192  (void)j;
193 #endif
194  return g_space_;
195 }
196 
197 
198 template<class Scalar>
201 {
202  typedef Thyra::ModelEvaluatorBase MEB;
203  MEB::InArgsSetup<Scalar> inArgs;
204  inArgs.setModelEvalDescription(this->description());
205  inArgs.set_Np(Np_);
206  return inArgs;
207 }
208 
209 
210 // Private functions overridden from ModelEvaulatorDefaultBase
211 
212 
213 template<class Scalar>
216 {
217  typedef Thyra::ModelEvaluatorBase MEB;
218  MEB::OutArgsSetup<Scalar> outArgs;
219  outArgs.setModelEvalDescription(this->description());
220  outArgs.set_Np_Ng(Np_,Ng_);
221  outArgs.setSupports(MEB::OUT_ARG_DgDp, 0 ,0, MEB::DERIV_TRANS_MV_BY_ROW);
222  return outArgs;
223 }
224 
225 
226 template<class Scalar>
227 void DiagonalQuadraticResponseOnlyModelEvaluator<Scalar>::evalModelImpl(
230  ) const
231 {
232 
233  using Teuchos::as;
234  using Teuchos::outArg;
236  using Thyra::get_mv;
239  typedef Thyra::ModelEvaluatorBase MEB;
240 
241  const ConstDetachedSpmdVectorView<Scalar> p(inArgs.get_p(0));
242  const ConstDetachedSpmdVectorView<Scalar> ps(ps_);
243  const ConstDetachedSpmdVectorView<Scalar> diag(diag_);
244  const ConstDetachedSpmdVectorView<Scalar> s_bar(s_bar_);
245 
246  // g(p)
247  if (!is_null(outArgs.get_g(0))) {
248  Scalar g_val = ST::zero();
249  for (Ordinal i = 0; i < p.subDim(); ++i) {
250  const Scalar p_ps = p[i] - ps[i];
251  g_val += diag[i] * p_ps*p_ps;
252  if (nonlinearTermFactor_ != ST::zero()) {
253  g_val += nonlinearTermFactor_ * p_ps * p_ps * p_ps;
254  }
255  }
256  Scalar global_g_val;
257  Teuchos::reduceAll<Ordinal, Scalar>(*comm_, Teuchos::REDUCE_SUM,
258  g_val, outArg(global_g_val) );
259  DetachedSpmdVectorView<Scalar>(outArgs.get_g(0))[0] =
260  as<Scalar>(0.5) * global_g_val + g_offset_;
261  }
262 
263  // DgDp[i]
264  if (!outArgs.get_DgDp(0,0).isEmpty()) {
265  const RCP<Thyra::MultiVectorBase<Scalar> > DgDp_trans_mv =
266  get_mv<Scalar>(outArgs.get_DgDp(0,0), "DgDp^T", MEB::DERIV_TRANS_MV_BY_ROW);
267  const DetachedSpmdVectorView<Scalar> DgDp_grad(DgDp_trans_mv->col(0));
268  for (Thyra::Ordinal i = 0; i < p.subDim(); ++i) {
269  const Scalar p_ps = p[i] - ps[i];
270  Scalar DgDp_grad_i = diag[i] * p_ps;
271  if (nonlinearTermFactor_ != ST::zero()) {
272  DgDp_grad_i += as<Scalar>(1.5) * nonlinearTermFactor_ * p_ps * p_ps;
273  }
274  DgDp_grad[i] = DgDp_grad_i / s_bar[i];
275 
276  }
277  }
278 
279 }
280 
281 
282 } // namespace Thyra
283 
284 
285 #endif // OPTIPACK_DIAGONAL_QUADRATIC_RESPONSE_ONLY_MODEL_EVALUATOR_DEF_HPP
Create an explicit detached mutable (non-const) view of all of the local elements on this process of ...
bool is_null(const boost::shared_ptr< T > &p)
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
Evaluation< VectorBase< Scalar > > get_g(int j) const
Precondition: supports(OUT_ARG_g)==true..
void setSolutionVector(const RCP< const VectorBase< Scalar > > &ps)
Set the solution vector ps .
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Derivative< Scalar > get_DgDp(int j, int l) const
Precondition: supports(OUT_ARG_DgDp,j,l)==true.
DiagonalQuadraticResponseOnlyModelEvaluator(const int localDim, const RCP< const Teuchos::Comm< Ordinal > > &comm=Teuchos::null)
const RCP< const VectorBase< Scalar > > getSolutionVector() const
Get the solution vector ps .
void setNonlinearTermFactor(const Scalar &nonlinearTermFactor)
Set nonlinear term factory.
#define TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(index, lower_inclusive, upper_exclusive)
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
Ptr< T > ptr() const
Abstract interface for finite-dimensional dense vectors.
void setDiagonalVector(const RCP< const VectorBase< Scalar > > &diag)
Set the diagonal vector diag.
Base subclass for ModelEvaluator that defines some basic types.
TypeTo as(const TypeFrom &t)
void setDiagonalBarVector(const RCP< const VectorBase< Scalar > > &diag_bar)
Set the diagonal vector diag_bar.
Create an explicit detached non-mutable (const) view of all of the local elements on this process of ...
#define TEUCHOS_ASSERT(assertion_test)
RCP< const VectorBase< Scalar > > get_p(int l) const
Get p(l) where 0 &lt;= l &amp;&amp; l &lt; this-&gt;Np().
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...