Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_Exp_GeneralFad.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef SACADO_FAD_EXP_GENERALFAD_HPP
31 #define SACADO_FAD_EXP_GENERALFAD_HPP
32 
37 
38 namespace Sacado {
39 
41  namespace Fad {
42  namespace Exp {
43 
45 
50  template <typename Storage>
51  class GeneralFad :
52  //public Storage, // Brings in value, derivative storage
53  public Expr< GeneralFad<Storage> >, // Brings in expression interface
54  public Extender<Storage> // Brings in interface extensions
55  {
56  public:
57 
59  typedef Storage StorageType;
60 
63 
66 
68  using typename StorageType::value_type;
69 
72 
74  template <typename T>
75  struct apply {
76  typedef typename Storage::template apply<T>::type S;
78  };
79 
81  template <int N>
82  struct apply_N {
83  typedef typename Storage::template apply_N<N>::type S;
85  };
86 
91 
93  GeneralFad() = default;
94 
96  template <typename S>
99  ExtenderType(x) {}
100 
102 
106  GeneralFad(const int sz, const value_type & x,
107  const DerivInit zero_out = InitDerivArray) :
108  ExtenderType(sz, x, zero_out) {}
109 
111 
117  GeneralFad(const int sz, const int i, const value_type & x) :
118  ExtenderType(sz, x, InitDerivArray) {
119  this->fastAccessDx(i)=1.;
120  }
121 
124  GeneralFad(const Storage& s) : ExtenderType(s) {}
125 
127  GeneralFad(const GeneralFad& x) = default;
128 
130  template <typename S>
133  ExtenderType(x.derived().size(), value_type(0.), NoInitDerivArray)
134  {
136  }
137 
139  //using Storage::Storage;
140  using ExtenderType::ExtenderType;
141 
143  ~GeneralFad() = default;
144 
146 
153  void diff(const int ith, const int n) {
154  if (this->size() != n)
155  this->resize(n);
156 
157  this->zero();
158  this->fastAccessDx(ith) = value_type(1.);
159  }
160 
162  template <typename S>
164  SACADO_EXP_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr<S>& xx) const {
165  typedef typename Expr<S>::derived_type expr_type;
166  const expr_type& x = xx.derived();
167 
168  typedef IsEqual<value_type> IE;
169  if (x.size() != this->size()) return false;
170  bool eq = IE::eval(x.val(), this->val());
171  const int sz = this->size();
173  eq = eq && IE::eval(x.dx(i), this->dx(i));
174  return eq;
175  }
176 
178 
183 
189  int availableSize() const { return this->length(); }
190 
193  bool hasFastAccess() const { return this->size()!=0; }
194 
197  void setIsConstant(bool is_const) {
198  if (is_const && this->size()!=0)
199  this->resize(0);
200  }
201 
203 
208 
210  template <typename S>
212  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator=(const S& v) {
213  this->val() = v;
214  if (this->size()) this->resize(0);
215  return *this;
216  }
217 
219  GeneralFad&
220  operator=(const GeneralFad& x) = default;
221 
223  template <typename S>
225  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator=(const Expr<S>& x) {
226  ExprAssign<GeneralFad>::assign_equal(*this, x.derived());
227  return *this;
228  }
229 
231 
236 
238  template <typename S>
240  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator += (const S& v) {
241  this->val() += v;
242  return *this;
243  }
244 
246  template <typename S>
248  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator -= (const S& v) {
249  this->val() -= v;
250  return *this;
251  }
252 
254  template <typename S>
256  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator *= (const S& v) {
257  const int sz = this->size();
258  this->val() *= v;
260  this->fastAccessDx(i) *= v;
261  return *this;
262  }
263 
265  template <typename S>
267  SACADO_ENABLE_VALUE_FUNC(GeneralFad&) operator /= (const S& v) {
268  const int sz = this->size();
269  this->val() /= v;
271  this->fastAccessDx(i) /= v;
272  return *this;
273  }
274 
277  GeneralFad& operator += (const GeneralFad& x) {
279  return *this;
280  }
281 
284  GeneralFad& operator -= (const GeneralFad& x) {
286  return *this;
287  }
288 
291  GeneralFad& operator *= (const GeneralFad& x) {
293  return *this;
294  }
295 
298  GeneralFad& operator /= (const GeneralFad& x) {
300  return *this;
301  }
302 
304  template <typename S>
306  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator += (const Expr<S>& x) {
307  ExprAssign<GeneralFad>::assign_plus_equal(*this, x.derived());
308  return *this;
309  }
310 
312  template <typename S>
314  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator -= (const Expr<S>& x) {
315  ExprAssign<GeneralFad>::assign_minus_equal(*this, x.derived());
316  return *this;
317  }
318 
320  template <typename S>
322  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator *= (const Expr<S>& x) {
323  ExprAssign<GeneralFad>::assign_times_equal(*this, x.derived());
324  return *this;
325  }
326 
328  template <typename S>
330  SACADO_EXP_ENABLE_EXPR_FUNC(GeneralFad&) operator /= (const Expr<S>& x) {
332  return *this;
333  }
334 
336 
337  }; // class GeneralFad
338 
339  template <typename S>
340  struct ExprLevel< GeneralFad<S> > {
341  static constexpr unsigned value =
343  };
344 
345  template <typename S>
346  struct IsFadExpr< GeneralFad<S> > {
347  static constexpr bool value = true;
348  };
349 
350  } // namespace Exp
351  } // namespace Fad
352 
353  template <typename S>
354  struct IsFad< Fad::Exp::GeneralFad<S> > {
355  static constexpr bool value = true;
356  };
357 
358  template <typename S>
359  struct IsExpr< Fad::Exp::GeneralFad<S> > {
360  static constexpr bool value = true;
361  };
362 
363  template <typename S>
364  struct BaseExprType< Fad::Exp::GeneralFad<S> > {
366  };
367 
368 } // namespace Sacado
369 
370 #include "Sacado_Fad_Exp_Ops.hpp"
371 
372 #endif // SACADO_FAD_EXP_GENERALFAD_HPP
Wrapper for a generic expression template.
KOKKOS_INLINE_FUNCTION SACADO_ENABLE_VALUE_FUNC(GeneralFad &) operator
Assignment operator with constant right-hand-side.
expr expr dx(i)
static KOKKOS_INLINE_FUNCTION void assign_times_equal(DstType &dst, const SrcType &x)
Implementation of dst *= x.
#define SACADO_ENABLE_VALUE_CTOR_DECL
Base template specification for whether a type is a Fad type.
KOKKOS_INLINE_FUNCTION GeneralFad(const Expr< S > &x, SACADO_EXP_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
KOKKOS_INLINE_FUNCTION GeneralFad(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with value.
KOKKOS_INLINE_FUNCTION const derived_type & derived() const
Return derived object.
Turn GeneralFad into a meta-function class usable with mpl::apply.
#define SACADO_EXP_ENABLE_EXPR_CTOR_DECL
Determine whether a given type is an expression.
Wrapper for a generic expression template.
static KOKKOS_INLINE_FUNCTION void assign_equal(DstType &dst, const SrcType &x)
Implementation of dst = x.
~GeneralFad()=default
Destructor.
Replace static derivative length.
Is a type an expression.
KOKKOS_INLINE_FUNCTION GeneralFad(const int sz, const value_type &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
expr val()
#define KOKKOS_INLINE_FUNCTION
static KOKKOS_INLINE_FUNCTION void assign_divide_equal(DstType &dst, const SrcType &x)
Implementation of dst /= x.
static KOKKOS_INLINE_FUNCTION void assign_plus_equal(DstType &dst, const SrcType &x)
Implementation of dst += x.
GeneralFad()=default
Default constructor.
Storage::template apply_N< N >::type S
KOKKOS_INLINE_FUNCTION GeneralFad(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
Base template specification for testing equivalence.
Forward-mode AD class templated on the storage for the derivative array.
#define SACADO_FAD_DERIV_LOOP(I, SZ)
KOKKOS_INLINE_FUNCTION bool hasFastAccess() const
Returns true if derivative array is not empty.
Get the base Fad type from a view/expression.
Extender< Storage > ExtenderType
Extender type.
T derived_type
Typename of derived object, returned by derived()
Do not initialize the derivative array.
Storage::template apply< T >::type S
ScalarType< value_type >::type scalar_type
Typename of scalar&#39;s (which may be different from T)
Meta-function for determining nesting with an expression.
KOKKOS_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
Expr< GeneralFad< Storage > > ExprType
Expression type.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
static KOKKOS_INLINE_FUNCTION void assign_minus_equal(DstType &dst, const SrcType &x)
Implementation of dst -= x.
KOKKOS_INLINE_FUNCTION void diff(const int ith, const int n)
Set GeneralFad object as the ith independent variable.
KOKKOS_INLINE_FUNCTION SACADO_EXP_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr< S > &xx) const
Returns whether two Fad objects have the same values.
KOKKOS_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.
Initialize the derivative array.
Forward-mode AD class templated on the storage for the derivative array.
KOKKOS_INLINE_FUNCTION GeneralFad(const Storage &s)
Constructor with supplied storage s.
Extension class for extending interface of its argument.