10 #ifndef OPTIPACK_DIAGONAL_QUADRATIC_RESPONSE_ONLY_MODEL_EVALUATOR_DEF_HPP
11 #define OPTIPACK_DIAGONAL_QUADRATIC_RESPONSE_ONLY_MODEL_EVALUATOR_DEF_HPP
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"
35 template<
class Scalar>
40 :Np_(1), Ng_(1), comm_(comm), localDim_(localDim),
41 nonlinearTermFactor_(0.0), g_offset_(0.0)
45 using Thyra::createMember;
55 g_space_ = Thyra::locallyReplicatedDefaultSpmdVectorSpace<Scalar>(comm_, 1);
58 p_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(comm_, localDim, -1);
62 V_S(ps.ptr(), ST::zero());
67 V_S(diag.
ptr(), ST::one());
73 V_S(s_bar.
ptr(), ST::one());
77 g_offset_ = ST::zero();
82 template<
class Scalar>
86 ps_ = ps.assert_not_null();
90 template<
class Scalar>
98 template<
class Scalar>
107 template<
class Scalar>
113 using Teuchos::rcp_dynamic_cast;
114 using Thyra::createMember;
115 using Thyra::ele_wise_divide;
118 diag_bar_ = diag_bar.assert_not_null();
128 V_S( s_bar.
ptr(), ST::zero() );
129 ele_wise_divide( ST::one(), *diag_, *diag_bar_, s_bar.
ptr() );
134 sp_p_space->setScalarProd(diagonalScalarProd<Scalar>(s_bar_));
139 template<
class Scalar>
141 const Scalar &nonlinearTermFactor)
143 nonlinearTermFactor_ = nonlinearTermFactor;
147 template<
class Scalar>
149 const Scalar &g_offset)
151 g_offset_ = g_offset;
158 template<
class Scalar>
165 template<
class Scalar>
172 template<
class Scalar>
185 template<
class Scalar>
198 template<
class Scalar>
203 MEB::InArgsSetup<Scalar> inArgs;
204 inArgs.setModelEvalDescription(this->description());
213 template<
class Scalar>
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);
226 template<
class Scalar>
227 void DiagonalQuadraticResponseOnlyModelEvaluator<Scalar>::evalModelImpl(
234 using Teuchos::outArg;
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_);
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;
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_;
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));
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;
274 DgDp_grad[i] = DgDp_grad_i / s_bar[i];
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)
void setScalarOffset(const Scalar &g_offset)
Set offset scalar g_offset .
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)
RCP< const VectorSpaceBase< Scalar > > get_g_space(int j) const
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
RCP< const VectorSpaceBase< Scalar > > get_p_space(int l) 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.
ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
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 ...
Simple parallel response-only ModelEvaluator.
#define TEUCHOS_ASSERT(assertion_test)
RCP< const VectorBase< Scalar > > get_p(int l) const
Get p(l) where 0 <= l && l < this->Np().
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...