44 #ifndef OPTIPACK_DIAGONAL_QUADRATIC_RESPONSE_ONLY_MODEL_EVALUATOR_DEF_HPP
45 #define OPTIPACK_DIAGONAL_QUADRATIC_RESPONSE_ONLY_MODEL_EVALUATOR_DEF_HPP
48 #include "Thyra_DiagonalQuadraticResponseOnlyModelEvaluator_decl.hpp"
49 #include "Thyra_DiagonalScalarProd.hpp"
50 #include "Thyra_VectorStdOps.hpp"
51 #include "Thyra_DefaultSpmdVectorSpace.hpp"
52 #include "Thyra_DetachedSpmdVectorView.hpp"
53 #include "Teuchos_DefaultComm.hpp"
54 #include "Teuchos_CommHelpers.hpp"
55 #include "Teuchos_Assert.hpp"
69 template<
class Scalar>
74 :Np_(1), Ng_(1), comm_(comm), localDim_(localDim),
75 nonlinearTermFactor_(0.0), g_offset_(0.0)
79 using Thyra::createMember;
89 g_space_ = Thyra::locallyReplicatedDefaultSpmdVectorSpace<Scalar>(comm_, 1);
92 p_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(comm_, localDim, -1);
96 V_S(ps.ptr(), ST::zero());
101 V_S(diag.
ptr(), ST::one());
107 V_S(s_bar.
ptr(), ST::one());
111 g_offset_ = ST::zero();
116 template<
class Scalar>
120 ps_ = ps.assert_not_null();
124 template<
class Scalar>
132 template<
class Scalar>
141 template<
class Scalar>
147 using Teuchos::rcp_dynamic_cast;
148 using Thyra::createMember;
149 using Thyra::ele_wise_divide;
152 diag_bar_ = diag_bar.assert_not_null();
162 V_S( s_bar.
ptr(), ST::zero() );
163 ele_wise_divide( ST::one(), *diag_, *diag_bar_, s_bar.
ptr() );
168 sp_p_space->setScalarProd(diagonalScalarProd<Scalar>(s_bar_));
173 template<
class Scalar>
175 const Scalar &nonlinearTermFactor)
177 nonlinearTermFactor_ = nonlinearTermFactor;
181 template<
class Scalar>
183 const Scalar &g_offset)
185 g_offset_ = g_offset;
192 template<
class Scalar>
199 template<
class Scalar>
206 template<
class Scalar>
219 template<
class Scalar>
232 template<
class Scalar>
237 MEB::InArgsSetup<Scalar> inArgs;
238 inArgs.setModelEvalDescription(this->description());
247 template<
class Scalar>
252 MEB::OutArgsSetup<Scalar> outArgs;
253 outArgs.setModelEvalDescription(this->description());
254 outArgs.set_Np_Ng(Np_,Ng_);
255 outArgs.setSupports(MEB::OUT_ARG_DgDp, 0 ,0, MEB::DERIV_TRANS_MV_BY_ROW);
260 template<
class Scalar>
261 void DiagonalQuadraticResponseOnlyModelEvaluator<Scalar>::evalModelImpl(
268 using Teuchos::outArg;
275 const ConstDetachedSpmdVectorView<Scalar> p(inArgs.
get_p(0));
276 const ConstDetachedSpmdVectorView<Scalar> ps(ps_);
277 const ConstDetachedSpmdVectorView<Scalar> diag(diag_);
278 const ConstDetachedSpmdVectorView<Scalar> s_bar(s_bar_);
282 Scalar g_val = ST::zero();
283 for (
Ordinal i = 0; i < p.subDim(); ++i) {
284 const Scalar p_ps = p[i] - ps[i];
285 g_val += diag[i] * p_ps*p_ps;
286 if (nonlinearTermFactor_ != ST::zero()) {
287 g_val += nonlinearTermFactor_ * p_ps * p_ps * p_ps;
292 g_val, outArg(global_g_val) );
293 DetachedSpmdVectorView<Scalar>(outArgs.
get_g(0))[0] =
294 as<Scalar>(0.5) * global_g_val + g_offset_;
298 if (!outArgs.
get_DgDp(0,0).isEmpty()) {
299 const RCP<Thyra::MultiVectorBase<Scalar> > DgDp_trans_mv =
300 get_mv<Scalar>(outArgs.
get_DgDp(0,0),
"DgDp^T", MEB::DERIV_TRANS_MV_BY_ROW);
301 const DetachedSpmdVectorView<Scalar> DgDp_grad(DgDp_trans_mv->col(0));
303 const Scalar p_ps = p[i] - ps[i];
304 Scalar DgDp_grad_i = diag[i] * p_ps;
305 if (nonlinearTermFactor_ != ST::zero()) {
306 DgDp_grad_i += as<Scalar>(1.5) * nonlinearTermFactor_ * p_ps * p_ps;
308 DgDp_grad[i] = DgDp_grad_i / s_bar[i];
319 #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...