32 #ifndef SACADO_FAD_GENERALFAD_MP_VECTOR_HPP
33 #define SACADO_FAD_GENERALFAD_MP_VECTOR_HPP
35 #include "Sacado_Fad_GeneralFad.hpp"
39 template <
typename Ord,
typename Val,
int Num,
typename Dev>
40 class StaticFixedStorage;
46 template <
typename S>
class Vector;
51 template <
typename Ord,
typename Val,
int VecNum,
typename Dev,
53 struct ExprSpec< GeneralFad< Sacado::
MP::Vector< Stokhos::StaticFixedStorage<Ord,Val,VecNum,Dev> >,
Storage > > {
63 template <
typename Ord,
typename Val,
int VecNum,
typename Dev,
85 KOKKOS_INLINE_FUNCTION
93 KOKKOS_INLINE_FUNCTION
101 KOKKOS_INLINE_FUNCTION
102 GeneralFad(
const int sz,
const T & x,
const DerivInit zero_out = InitDerivArray) :
111 KOKKOS_INLINE_FUNCTION
118 KOKKOS_INLINE_FUNCTION
122 KOKKOS_INLINE_FUNCTION
127 template <
typename S>
128 KOKKOS_INLINE_FUNCTION
132 const int sz = x.
size();
135 if (x.hasFastAccess())
136 for(
int i=0; i<sz; ++i)
137 for (
int j=0;
j<VecNum; ++
j)
140 for(
int i=0; i<sz; ++i)
141 for (
int j=0;
j<VecNum; ++
j)
145 for (
int j=0;
j<VecNum; ++
j)
150 KOKKOS_INLINE_FUNCTION
160 KOKKOS_INLINE_FUNCTION
161 void diff(
const int ith,
const int n) {
162 if (this->size() != n)
170 KOKKOS_INLINE_FUNCTION
174 KOKKOS_INLINE_FUNCTION
178 template <
typename S>
179 KOKKOS_INLINE_FUNCTION
181 typedef IsEqual<value_type> IE;
182 if (x.size() != this->size())
return false;
183 bool eq = IE::eval(x.val(), this->
val());
184 for (
int i=0; i<this->size(); i++)
185 eq = eq && IE::eval(x.dx(i), this->
dx(i));
197 KOKKOS_INLINE_FUNCTION
201 KOKKOS_INLINE_FUNCTION
205 KOKKOS_INLINE_FUNCTION
209 KOKKOS_INLINE_FUNCTION
223 KOKKOS_INLINE_FUNCTION
227 KOKKOS_INLINE_FUNCTION
231 KOKKOS_INLINE_FUNCTION
235 KOKKOS_INLINE_FUNCTION
237 if (is_const && this->size()!=0)
242 KOKKOS_INLINE_FUNCTION
243 const T*
dx()
const {
return this->dx_; }
246 KOKKOS_INLINE_FUNCTION
247 T dx(
int i)
const {
return this->size() ? this->dx_[i] :
T(0.); }
250 KOKKOS_INLINE_FUNCTION
254 KOKKOS_INLINE_FUNCTION
258 KOKKOS_INLINE_FUNCTION
259 val_type dx(
int i,
int j)
const {
return this->size() ? this->dx_[i].fastAccessCoeff(j) :
val_type(0.0); }
262 KOKKOS_INLINE_FUNCTION
264 return this->dx_[i].fastAccessCoeff(j);
268 KOKKOS_INLINE_FUNCTION
270 return this->dx_[i].fastAccessCoeff(j);
281 template <
typename S>
282 KOKKOS_INLINE_FUNCTION
283 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator=(const S& v) {
285 if (this->size()) this->resize(0);
290 KOKKOS_INLINE_FUNCTION
292 operator=(
const GeneralFad& x) {
294 Storage::operator=(x);
299 template <
typename S>
300 KOKKOS_INLINE_FUNCTION
301 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator=(const Expr<S>& x) {
302 const int xsz = x.size();
304 if (xsz != this->size())
305 this->resizeAndZero(xsz);
307 const int sz = this->size();
315 if (x.hasFastAccess())
316 for(
int i=0; i<sz; ++i)
317 for (
int j=0;
j<VecNum; ++
j)
320 for(
int i=0; i<sz; ++i)
321 for (
int j=0;
j<VecNum; ++
j)
325 for (
int j=0;
j<VecNum; ++
j)
339 template <
typename S>
340 KOKKOS_INLINE_FUNCTION
341 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator += (const S& v) {
347 template <
typename S>
348 KOKKOS_INLINE_FUNCTION
349 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator -= (const S& v) {
355 template <
typename S>
356 KOKKOS_INLINE_FUNCTION
357 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator *= (const S& v) {
358 const int sz = this->size();
360 for (
int i=0; i<sz; ++i)
366 template <
typename S>
367 KOKKOS_INLINE_FUNCTION
368 SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator /= (const S& v) {
369 const int sz = this->size();
371 for (
int i=0; i<sz; ++i)
377 KOKKOS_INLINE_FUNCTION
378 GeneralFad& operator += (
const GeneralFad& x) {
379 const int xsz = x.size(), sz = this->size();
381 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
382 if ((xsz != sz) && (xsz != 0) && (sz != 0))
383 throw "Fad Error: Attempt to assign with incompatible sizes";
388 for (
int i=0; i<sz; ++i)
392 this->resizeAndZero(xsz);
393 for (
int i=0; i<xsz; ++i)
398 this->
val() += x.val();
403 KOKKOS_INLINE_FUNCTION
404 GeneralFad& operator -= (
const GeneralFad& x) {
405 const int xsz = x.size(), sz = this->size();
407 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
408 if ((xsz != sz) && (xsz != 0) && (sz != 0))
409 throw "Fad Error: Attempt to assign with incompatible sizes";
414 for(
int i=0; i<sz; ++i)
418 this->resizeAndZero(xsz);
419 for(
int i=0; i<xsz; ++i)
424 this->
val() -= x.val();
430 KOKKOS_INLINE_FUNCTION
431 GeneralFad& operator *= (
const GeneralFad& x) {
432 const int xsz = x.size(), sz = this->size();
436 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
437 if ((xsz != sz) && (xsz != 0) && (sz != 0))
438 throw "Fad Error: Attempt to assign with incompatible sizes";
443 for(
int i=0; i<sz; ++i)
447 this->resizeAndZero(xsz);
448 for(
int i=0; i<xsz; ++i)
454 for (
int i=0; i<sz; ++i)
465 KOKKOS_INLINE_FUNCTION
466 GeneralFad& operator /= (
const GeneralFad& x) {
467 const int xsz = x.size(), sz = this->size();
471 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
472 if ((xsz != sz) && (xsz != 0) && (sz != 0))
473 throw "Fad Error: Attempt to assign with incompatible sizes";
478 for(
int i=0; i<sz; ++i)
480 ( this->
fastAccessDx(i)*xval - v*x.fastAccessDx(i) )/ (xval*xval);
483 this->resizeAndZero(xsz);
484 for(
int i=0; i<xsz; ++i)
485 this->
fastAccessDx(i) = - v*x.fastAccessDx(i) / (xval*xval);
490 for (
int i=0; i<sz; ++i)
501 template <
typename S>
502 KOKKOS_INLINE_FUNCTION
503 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator += (const Expr<S>& x) {
504 const int xsz = x.size();
505 int sz = this->size();
507 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
508 if ((xsz != sz) && (xsz != 0) && (sz != 0))
509 throw "Fad Error: Attempt to assign with incompatible sizes";
513 this->resizeAndZero(xsz);
518 if (x.hasFastAccess())
519 for(
int i=0; i<sz; ++i)
520 for (
int j=0;
j<VecNum; ++
j)
523 for(
int i=0; i<sz; ++i)
524 for (
int j=0;
j<VecNum; ++
j)
528 for (
int j=0;
j<VecNum; ++
j)
535 template <
typename S>
536 KOKKOS_INLINE_FUNCTION
537 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator -= (const Expr<S>& x) {
538 const int xsz = x.size(), sz = this->size();
540 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
541 if ((xsz != sz) && (xsz != 0) && (sz != 0))
542 throw "Fad Error: Attempt to assign with incompatible sizes";
547 if (x.hasFastAccess())
548 for(
int i=0; i<sz; ++i)
549 for (
int j=0;
j<VecNum; ++
j)
552 for (
int i=0; i<sz; ++i)
553 for (
int j=0;
j<VecNum; ++
j)
557 this->resizeAndZero(xsz);
558 if (x.hasFastAccess())
559 for(
int i=0; i<xsz; ++i)
560 for (
int j=0;
j<VecNum; ++
j)
563 for (
int i=0; i<xsz; ++i)
564 for (
int j=0;
j<VecNum; ++
j)
569 for (
int j=0;
j<VecNum; ++
j)
576 template <
typename S>
577 KOKKOS_INLINE_FUNCTION
578 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator *= (const Expr<S>& x) {
579 const int xsz = x.size(), sz = this->size();
583 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
584 if ((xsz != sz) && (xsz != 0) && (sz != 0))
585 throw "Fad Error: Attempt to assign with incompatible sizes";
590 if (x.hasFastAccess())
591 for(
int i=0; i<sz; ++i)
592 for (
int j=0;
j<VecNum; ++
j)
595 for (
int i=0; i<sz; ++i)
596 for (
int j=0;
j<VecNum; ++
j)
600 this->resizeAndZero(xsz);
601 if (x.hasFastAccess())
602 for(
int i=0; i<xsz; ++i)
603 for (
int j=0;
j<VecNum; ++
j)
606 for (
int i=0; i<xsz; ++i)
607 for (
int j=0;
j<VecNum; ++
j)
613 for (
int i=0; i<sz; ++i)
614 for (
int j=0;
j<VecNum; ++
j)
619 for (
int j=0;
j<VecNum; ++
j)
620 val(
j) *= xval.fastAccessCoeff(
j);
626 template <
typename S>
627 KOKKOS_INLINE_FUNCTION
628 SACADO_ENABLE_EXPR_FUNC(GeneralFad&) operator /= (const Expr<S>& x) {
629 const int xsz = x.size(), sz = this->size();
633 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
634 if ((xsz != sz) && (xsz != 0) && (sz != 0))
635 throw "Fad Error: Attempt to assign with incompatible sizes";
641 if (x.hasFastAccess())
642 for(
int i=0; i<sz; ++i)
643 for (
int j=0;
j<VecNum; ++
j)
646 for (
int i=0; i<sz; ++i)
647 for (
int j=0;
j<VecNum; ++
j)
651 this->resizeAndZero(xsz);
652 if (x.hasFastAccess())
653 for(
int i=0; i<xsz; ++i)
654 for (
int j=0;
j<VecNum; ++
j)
655 fastAccessDx(i,
j) = - v.fastAccessCoeff(
j)*x.fastAccessDx(i,
j) / (xval2.fastAccessCoeff(
j));
657 for (
int i=0; i<xsz; ++i)
658 for (
int j=0;
j<VecNum; ++
j)
659 fastAccessDx(i,
j) = -v.fastAccessCoeff(
j)*x.dx(i,
j) / (xval2.fastAccessCoeff(
j));
664 for (
int i=0; i<sz; ++i)
665 for (
int j=0;
j<VecNum; ++
j)
670 for (
int j=0;
j<VecNum; ++
j)
671 val(
j) /= xval.fastAccessCoeff(
j);
680 template <
typename S>
681 KOKKOS_INLINE_FUNCTION
683 fastAssign(
const Expr<S>& x) {
684 const int sz = this->size();
685 for(
int i=0; i<sz; ++i)
686 for (
int j=0;
j<VecNum; ++
j)
689 for (
int j=0;
j<VecNum; ++
j)
693 template <
typename S>
694 KOKKOS_INLINE_FUNCTION
696 slowAssign(
const Expr<S>& x) {
697 const int sz = this->size();
698 for(
int i=0; i<sz; ++i)
699 for (
int j=0;
j<VecNum; ++
j)
702 for (
int j=0;
j<VecNum; ++
j)
712 #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.