52 #ifndef SACADO_FAD_GENERALFAD_MP_VECTOR_HPP
53 #define SACADO_FAD_GENERALFAD_MP_VECTOR_HPP
55 #include "Sacado_Fad_GeneralFad.hpp"
59 template <
typename Ord,
typename Val,
int Num,
typename Dev>
60 class StaticFixedStorage;
66 template <
typename S>
class Vector;
71 template <
typename Ord,
typename Val,
int VecNum,
typename Dev,
73 struct ExprSpec< GeneralFad< Sacado::
MP::Vector< Stokhos::StaticFixedStorage<Ord,Val,VecNum,Dev> >,
Storage > > {
83 template <
typename Ord,
typename Val,
int VecNum,
typename Dev,
105 KOKKOS_INLINE_FUNCTION
112 template <
typename S>
113 KOKKOS_INLINE_FUNCTION
121 KOKKOS_INLINE_FUNCTION
122 GeneralFad(
const int sz,
const T & x,
const DerivInit zero_out = InitDerivArray) :
131 KOKKOS_INLINE_FUNCTION
138 KOKKOS_INLINE_FUNCTION
142 KOKKOS_INLINE_FUNCTION
147 template <
typename S>
148 KOKKOS_INLINE_FUNCTION
152 const int sz = x.
size();
155 if (x.hasFastAccess())
156 for(
int i=0; i<sz; ++i)
157 for (
int j=0;
j<VecNum; ++
j)
160 for(
int i=0; i<sz; ++i)
161 for (
int j=0;
j<VecNum; ++
j)
165 for (
int j=0;
j<VecNum; ++
j)
170 KOKKOS_INLINE_FUNCTION
180 KOKKOS_INLINE_FUNCTION
181 void diff(
const int ith,
const int n) {
182 if (this->size() != n)
190 KOKKOS_INLINE_FUNCTION
194 KOKKOS_INLINE_FUNCTION
198 template <
typename S>
199 KOKKOS_INLINE_FUNCTION
201 typedef IsEqual<value_type> IE;
202 if (x.size() != this->size())
return false;
203 bool eq = IE::eval(x.val(), this->
val());
204 for (
int i=0; i<this->size(); i++)
205 eq = eq && IE::eval(x.dx(i), this->
dx(i));
217 KOKKOS_INLINE_FUNCTION
221 KOKKOS_INLINE_FUNCTION
225 KOKKOS_INLINE_FUNCTION
229 KOKKOS_INLINE_FUNCTION
243 KOKKOS_INLINE_FUNCTION
247 KOKKOS_INLINE_FUNCTION
251 KOKKOS_INLINE_FUNCTION
255 KOKKOS_INLINE_FUNCTION
257 if (is_const && this->size()!=0)
262 KOKKOS_INLINE_FUNCTION
263 const T*
dx()
const {
return this->dx_; }
266 KOKKOS_INLINE_FUNCTION
267 T dx(
int i)
const {
return this->size() ? this->dx_[i] :
T(0.); }
270 KOKKOS_INLINE_FUNCTION
274 KOKKOS_INLINE_FUNCTION
278 KOKKOS_INLINE_FUNCTION
279 val_type dx(
int i,
int j)
const {
return this->size() ? this->dx_[i].fastAccessCoeff(j) :
val_type(0.0); }
282 KOKKOS_INLINE_FUNCTION
284 return this->dx_[i].fastAccessCoeff(j);
288 KOKKOS_INLINE_FUNCTION
290 return this->dx_[i].fastAccessCoeff(j);
301 template <
typename S>
302 KOKKOS_INLINE_FUNCTION
303 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator=(const S& v) {
305 if (this->size()) this->resize(0);
310 KOKKOS_INLINE_FUNCTION
312 operator=(
const GeneralFad& x) {
314 Storage::operator=(x);
319 template <
typename S>
320 KOKKOS_INLINE_FUNCTION
321 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator=(const Expr<S>& x) {
322 const int xsz = x.size();
324 if (xsz != this->size())
325 this->resizeAndZero(xsz);
327 const int sz = this->size();
335 if (x.hasFastAccess())
336 for(
int i=0; i<sz; ++i)
337 for (
int j=0;
j<VecNum; ++
j)
340 for(
int i=0; i<sz; ++i)
341 for (
int j=0;
j<VecNum; ++
j)
345 for (
int j=0;
j<VecNum; ++
j)
359 template <
typename S>
360 KOKKOS_INLINE_FUNCTION
361 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator += (const S& v) {
367 template <
typename S>
368 KOKKOS_INLINE_FUNCTION
369 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator -= (const S& v) {
375 template <
typename S>
376 KOKKOS_INLINE_FUNCTION
377 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator *= (const S& v) {
378 const int sz = this->size();
380 for (
int i=0; i<sz; ++i)
386 template <
typename S>
387 KOKKOS_INLINE_FUNCTION
388 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator /= (const S& v) {
389 const int sz = this->size();
391 for (
int i=0; i<sz; ++i)
397 KOKKOS_INLINE_FUNCTION
398 GeneralFad& operator += (
const GeneralFad& x) {
399 const int xsz = x.size(), sz = this->size();
401 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
402 if ((xsz != sz) && (xsz != 0) && (sz != 0))
403 throw "Fad Error: Attempt to assign with incompatible sizes";
408 for (
int i=0; i<sz; ++i)
412 this->resizeAndZero(xsz);
413 for (
int i=0; i<xsz; ++i)
418 this->
val() += x.val();
423 KOKKOS_INLINE_FUNCTION
424 GeneralFad& operator -= (
const GeneralFad& x) {
425 const int xsz = x.size(), sz = this->size();
427 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
428 if ((xsz != sz) && (xsz != 0) && (sz != 0))
429 throw "Fad Error: Attempt to assign with incompatible sizes";
434 for(
int i=0; i<sz; ++i)
438 this->resizeAndZero(xsz);
439 for(
int i=0; i<xsz; ++i)
444 this->
val() -= x.val();
450 KOKKOS_INLINE_FUNCTION
451 GeneralFad& operator *= (
const GeneralFad& x) {
452 const int xsz = x.size(), sz = this->size();
456 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
457 if ((xsz != sz) && (xsz != 0) && (sz != 0))
458 throw "Fad Error: Attempt to assign with incompatible sizes";
463 for(
int i=0; i<sz; ++i)
467 this->resizeAndZero(xsz);
468 for(
int i=0; i<xsz; ++i)
474 for (
int i=0; i<sz; ++i)
485 KOKKOS_INLINE_FUNCTION
486 GeneralFad& operator /= (
const GeneralFad& x) {
487 const int xsz = x.size(), sz = this->size();
491 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
492 if ((xsz != sz) && (xsz != 0) && (sz != 0))
493 throw "Fad Error: Attempt to assign with incompatible sizes";
498 for(
int i=0; i<sz; ++i)
500 ( this->
fastAccessDx(i)*xval - v*x.fastAccessDx(i) )/ (xval*xval);
503 this->resizeAndZero(xsz);
504 for(
int i=0; i<xsz; ++i)
505 this->
fastAccessDx(i) = - v*x.fastAccessDx(i) / (xval*xval);
510 for (
int i=0; i<sz; ++i)
521 template <
typename S>
522 KOKKOS_INLINE_FUNCTION
523 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator += (const Expr<S>& x) {
524 const int xsz = x.size();
525 int sz = this->size();
527 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
528 if ((xsz != sz) && (xsz != 0) && (sz != 0))
529 throw "Fad Error: Attempt to assign with incompatible sizes";
533 this->resizeAndZero(xsz);
538 if (x.hasFastAccess())
539 for(
int i=0; i<sz; ++i)
540 for (
int j=0;
j<VecNum; ++
j)
543 for(
int i=0; i<sz; ++i)
544 for (
int j=0;
j<VecNum; ++
j)
548 for (
int j=0;
j<VecNum; ++
j)
555 template <
typename S>
556 KOKKOS_INLINE_FUNCTION
557 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator -= (const Expr<S>& x) {
558 const int xsz = x.size(), sz = this->size();
560 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
561 if ((xsz != sz) && (xsz != 0) && (sz != 0))
562 throw "Fad Error: Attempt to assign with incompatible sizes";
567 if (x.hasFastAccess())
568 for(
int i=0; i<sz; ++i)
569 for (
int j=0;
j<VecNum; ++
j)
572 for (
int i=0; i<sz; ++i)
573 for (
int j=0;
j<VecNum; ++
j)
577 this->resizeAndZero(xsz);
578 if (x.hasFastAccess())
579 for(
int i=0; i<xsz; ++i)
580 for (
int j=0;
j<VecNum; ++
j)
583 for (
int i=0; i<xsz; ++i)
584 for (
int j=0;
j<VecNum; ++
j)
589 for (
int j=0;
j<VecNum; ++
j)
596 template <
typename S>
597 KOKKOS_INLINE_FUNCTION
598 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator *= (const Expr<S>& x) {
599 const int xsz = x.size(), sz = this->size();
603 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
604 if ((xsz != sz) && (xsz != 0) && (sz != 0))
605 throw "Fad Error: Attempt to assign with incompatible sizes";
610 if (x.hasFastAccess())
611 for(
int i=0; i<sz; ++i)
612 for (
int j=0;
j<VecNum; ++
j)
615 for (
int i=0; i<sz; ++i)
616 for (
int j=0;
j<VecNum; ++
j)
620 this->resizeAndZero(xsz);
621 if (x.hasFastAccess())
622 for(
int i=0; i<xsz; ++i)
623 for (
int j=0;
j<VecNum; ++
j)
626 for (
int i=0; i<xsz; ++i)
627 for (
int j=0;
j<VecNum; ++
j)
633 for (
int i=0; i<sz; ++i)
634 for (
int j=0;
j<VecNum; ++
j)
639 for (
int j=0;
j<VecNum; ++
j)
640 val(
j) *= xval.fastAccessCoeff(
j);
646 template <
typename S>
647 KOKKOS_INLINE_FUNCTION
648 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator /= (const Expr<S>& x) {
649 const int xsz = x.size(), sz = this->size();
653 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
654 if ((xsz != sz) && (xsz != 0) && (sz != 0))
655 throw "Fad Error: Attempt to assign with incompatible sizes";
661 if (x.hasFastAccess())
662 for(
int i=0; i<sz; ++i)
663 for (
int j=0;
j<VecNum; ++
j)
666 for (
int i=0; i<sz; ++i)
667 for (
int j=0;
j<VecNum; ++
j)
671 this->resizeAndZero(xsz);
672 if (x.hasFastAccess())
673 for(
int i=0; i<xsz; ++i)
674 for (
int j=0;
j<VecNum; ++
j)
675 fastAccessDx(i,
j) = - v.fastAccessCoeff(
j)*x.fastAccessDx(i,
j) / (xval2.fastAccessCoeff(
j));
677 for (
int i=0; i<xsz; ++i)
678 for (
int j=0;
j<VecNum; ++
j)
679 fastAccessDx(i,
j) = -v.fastAccessCoeff(
j)*x.dx(i,
j) / (xval2.fastAccessCoeff(
j));
684 for (
int i=0; i<sz; ++i)
685 for (
int j=0;
j<VecNum; ++
j)
690 for (
int j=0;
j<VecNum; ++
j)
691 val(
j) /= xval.fastAccessCoeff(
j);
700 template <
typename S>
701 KOKKOS_INLINE_FUNCTION
703 fastAssign(
const Expr<S>& x) {
704 const int sz = this->size();
705 for(
int i=0; i<sz; ++i)
706 for (
int j=0;
j<VecNum; ++
j)
709 for (
int j=0;
j<VecNum; ++
j)
713 template <
typename S>
714 KOKKOS_INLINE_FUNCTION
716 slowAssign(
const Expr<S>& x) {
717 const int sz = this->size();
718 for(
int i=0; i<sz; ++i)
719 for (
int j=0;
j<VecNum; ++
j)
722 for (
int j=0;
j<VecNum; ++
j)
732 #endif // SACADO_FAD_GENERALFAD_HPP
Sacado::MP::Vector< Stokhos::StaticFixedStorage< Ord, Val, VecNum, Dev > > T
KOKKOS_INLINE_FUNCTION SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr< S > &x) const
Returns whether two Fad objects have the same values.
KOKKOS_INLINE_FUNCTION GeneralFad(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
KOKKOS_INLINE_FUNCTION T dx(int i) const
Returns derivative component i with bounds checking.
RemoveConst< T >::type value_type
Typename of values.
ScalarType< value_type >::type scalar_type
Typename of scalar's (which may be different from T)
KOKKOS_INLINE_FUNCTION val_type & fastAccessDx(int i, int j)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
KOKKOS_INLINE_FUNCTION const val_type & fastAccessDx(int i, int j) const
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
KOKKOS_INLINE_FUNCTION GeneralFad()
Default constructor.
KOKKOS_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
KOKKOS_INLINE_FUNCTION GeneralFad(const int sz, const int i, const T &x)
Constructor with size sz, index i, and value x.
KOKKOS_INLINE_FUNCTION void diff(const int ith, const int n)
Set GeneralFad object as the ith independent variable.
KOKKOS_INLINE_FUNCTION ~GeneralFad()
Destructor.
KOKKOS_INLINE_FUNCTION const T * dx() const
Returns derivative array.
KOKKOS_INLINE_FUNCTION bool isPassive() const
Returns true if derivative array is empty.
KOKKOS_INLINE_FUNCTION val_type & val(int j)
Returns value.
KOKKOS_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION GeneralFad(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
KOKKOS_INLINE_FUNCTION const val_type & val(int j) const
Returns value.
KOKKOS_INLINE_FUNCTION val_type dx(int i, int j) const
Returns derivative component i with bounds checking.
KOKKOS_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.
KOKKOS_INLINE_FUNCTION GeneralFad(const GeneralFad &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION GeneralFad(const Storage &s)
Constructor with supplied storage s.
KOKKOS_INLINE_FUNCTION GeneralFad(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
KOKKOS_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
KOKKOS_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
value_type::value_type val_type
ordinal_type size() const
Return size.
KOKKOS_INLINE_FUNCTION bool hasFastAccess() const
Returns true if derivative array is not empty.