MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
gradient.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbmath/gradient.h,v 1.7 2017/01/12 14:43:53 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 /*
33  AUTHOR: Reinhard Resch <r.resch@secop.com>
34  Copyright (C) 2013(-2017) all rights reserved.
35 
36  The copyright of this code is transferred
37  to Pierangelo Masarati and Paolo Mantegazza
38  for use in the software MBDyn as described
39  in the GNU Public License version 2.1
40 */
41 
42 #ifndef ___GRADIENT_H_INCLUDED___
43 #define ___GRADIENT_H_INCLUDED___
44 
45 #include <algorithm>
46 #include <cassert>
47 #include <climits>
48 #include <cmath>
49 #include <cstdlib>
50 #include <cstddef>
51 #include <cstring>
52 #include <iostream>
53 #include <iomanip>
54 #include <limits>
55 #include <map>
56 #include <new>
57 #include <stdexcept>
58 #include <typeinfo>
59 #include <vector>
60 
61 #include "myassert.h"
62 #include "except.h"
63 #include "ac/f2c.h"
64 
65 #ifndef GRADIENT_DEBUG
66  #ifdef DEBUG
67  #define GRADIENT_DEBUG 2
68  #else
69  #define GRADIENT_DEBUG 0
70  #endif
71 #endif
72 
73 #if GRADIENT_DEBUG == 0 || defined(DEBUG)
74  #define GRADIENT_ASSERT(expr) ASSERT(expr)
75 #elif GRADIENT_DEBUG > 0
76  #define GRADIENT_ASSERT(expr) assert(expr)
77 #endif
78 
79 #if GRADIENT_DEBUG >= 2
80 #define GRADIENT_TRACE(expr) \
81  static_cast<void>(std::cerr << __FILE__ << ":" \
82  << __LINE__ << ":" \
83  << __FUNCTION__ << ": " << expr)
84 #else
85 #define GRADIENT_TRACE(expr) static_cast<void>(0)
86 #endif
87 
88 #ifndef GRADIENT_MEMORY_STAT
89  #ifdef DEBUG
90  #define GRADIENT_MEMORY_STAT 1
91  #else
92  #define GRADIENT_MEMORY_STAT 0
93  #endif
94 #endif
95 
96 #if ! HAVE_COPYSIGN
98  return y > 0. ? std::abs(x) : -std::abs(x);
99 }
100 #endif
101 
102 namespace grad {
103 
105 
106 template <index_type N_SIZE>
107 class Gradient;
108 
109 /*
110  * This structure checks if the data type
111  * at the left hand side of an assignment
112  * can hold the number of derivatives
113  * provided at the right hand side
114 */
115 template <bool bSizeOK>
116 struct MaxSizeCheck {
117 private:
118  /*
119  * For an expression like
120  * Gradient<3> a = Gradient<4>(5.);
121  * one will get an compilation error here!
122  */
123  enum CheckType {};
124 };
125 
126 template <>
128  enum CheckType {};
129 };
130 
131  template <typename T>
133  {
134  // FIXME: We should use std::numeric_limits<T>::max() here!
135  // FIXME: Unfortunately that value is not accessible at compile time
136  static const T iMaxValue = (T(-1) < 0)
137  ? ~T(0) & ~(T(1) << (sizeof(T) * CHAR_BIT - 1))
138  : ~T(0);
139  };
140 
142 
143 #ifndef GRADIENT_VECTOR_REGISTER_SIZE
144  #define GRADIENT_VECTOR_REGISTER_SIZE 0
145 #endif
146 
147 template <typename T>
148 class GradientAllocator: public std::allocator<T> {
149 public:
150  typedef typename std::allocator<T>::pointer pointer;
151  typedef typename std::allocator<T>::size_type size_type;
152 
153  typedef typename std::allocator<T>::difference_type difference_type;
154  typedef typename std::allocator<T>::const_pointer const_pointer;
155  typedef typename std::allocator<T>::reference reference;
156  typedef typename std::allocator<T>::const_reference const_reference;
157  typedef typename std::allocator<T>::value_type value_type;
158 
159  template <typename U>
160  struct rebind {
162  };
163 
164  GradientAllocator() throw() { }
165 
167  :std::allocator<T>(a) {
168  }
169 
170  template <typename U>
172  :std::allocator<T>(a) {
173  }
174 
175  pointer
176  allocate(size_type n, const void* p=0) {
177 #if GRADIENT_MEMORY_STAT > 0
178  sMemUsage.Inc(n);
179 #endif
180 
181  const pointer pMem = std::allocator<T>::allocate(n, p);
182 
183 #if GRADIENT_DEBUG > 0
184  std::memset(pMem, 0xFF, n);
185 #endif
186  return pMem;
187  }
188 
189  void
191  std::allocator<T>::deallocate(p, n);
192 
193 #if GRADIENT_MEMORY_STAT > 0
194  sMemUsage.Dec(n);
195 #endif
196  }
197 
198  static T* allocate_aligned(size_type alignment, size_t n, size_type extra_bytes = 0u) {
199 #if GRADIENT_MEMORY_STAT > 0
200  sMemUsage.Inc(n);
201 #endif
202  void* p;
203 
204  const size_type byte_size = sizeof(T) * n + extra_bytes;
205 
206  GRADIENT_ASSERT(alignment % sizeof(void*) == 0);
207 
208 #if defined(HAVE_POSIX_MEMALIGN)
209  if (0 != posix_memalign(&p, alignment, byte_size)) {
210  p = 0;
211  }
212 #elif defined(HAVE_MEMALIGN)
213  p = memalign(alignment, byte_size);
214 #elif defined(HAVE_ALIGNED_MALLOC)
215  p = _aligned_malloc(byte_size, alignment);
216 #else
217  p = malloc(byte_size);
218 #endif
219  if (p == 0) {
220  throw std::bad_alloc();
221  }
222 
223 #if GRADIENT_DEBUG > 0
224  const ptrdiff_t alignment_curr = (reinterpret_cast<const char*>(p) - reinterpret_cast<const char*>(0)) % sizeof(alignment);
225 
226  if (alignment_curr != 0) {
227  silent_cout("address " << p << " has invalid alignment " << alignment_curr << std::endl);
228  }
229 
230  GRADIENT_ASSERT(alignment_curr == 0);
231 
232  std::memset(p, 0xFF, byte_size);
233 #endif
234  return reinterpret_cast<T*>(p);
235  }
236 
237 
238  static void
240 #if defined(HAVE_ALIGNED_MALLOC)
241  _aligned_free(p);
242 #else
243  free(p);
244 #endif
245 
246 #if GRADIENT_MEMORY_STAT > 0
247  sMemUsage.Dec(n);
248 #endif
249  }
250 
251 #if GRADIENT_MEMORY_STAT > 0
252  static class MemStat {
253  public:
254  MemStat()
255  :iCurrMem(0), iMaxMem(0), iNumAlloc(0), iNumDealloc(0) {
256 
257  }
258 
259  ~MemStat() {
260  silent_cerr("GradientAllocator<" << typeid(T).name()
261  << ">\n\tiMaxMem = "
262  << std::setprecision(3)
263  << iMaxMem << std::endl
264  << "\tiCurrMem=" << iCurrMem << std::endl
265  << "\tiNumAlloc=" << iNumAlloc << std::endl
266  << "\tiNumDealloc=" << iNumDealloc << std::endl);
267  }
268 
269  void Inc(size_t iSize) {
270  ++iNumAlloc;
271  iCurrMem += iSize;
272  if (iCurrMem > iMaxMem) {
273  iMaxMem = iCurrMem;
274  }
275  }
276 
277  void Dec(size_t iSize) {
278  ++iNumDealloc;
279  iCurrMem -= iSize;
280  }
281 
282  private:
283  size_t iCurrMem;
284  size_t iMaxMem;
285  size_t iNumAlloc;
286  size_t iNumDealloc;
287  } sMemUsage;
288 #endif
289 };
290 
291  struct AlignedAlloc {
292 #if USE_AUTODIFF > 0 && GRADIENT_VECTOR_REGISTER_SIZE > 0
293  void* operator new(size_t size) {
294  const size_t nBound = GRADIENT_VECTOR_REGISTER_SIZE > sizeof(void*) ? GRADIENT_VECTOR_REGISTER_SIZE : sizeof(void*);
295  return GradientAllocator<char>::allocate_aligned(nBound, 0, size);
296  }
297  void operator delete(void *p) {
298  GradientAllocator<char>::deallocate_aligned(reinterpret_cast<char*>(p), 0);
299  }
300 #endif
301 };
302 
303 #if GRADIENT_MEMORY_STAT > 0
304 template <typename T>
306 #endif
307 
308 template <typename T>
309  inline void array_fill(T* first, T* const last, const T& val) {
310  while (first < last) {
311  *first++ = val;
312  }
313  }
314 
315  template <typename T>
316  inline T* array_copy(const T* first, const T* const last, T* result) {
317  while (first < last) {
318  *result++ = *first++;
319  }
320 
321  return result;
322  }
323 
324  template <typename T>
326  static T Zero(){
327  return T();
328  }
329 
330  static T Invalid(){
331  return T(NAN);
332  }
333 };
334 
335 template <>
337  static bool Zero(){
338  return false;
339  }
340 
341  static bool Invalid(){
342  return false;
343  }
344 };
345 
346 typedef doublereal scalar_func_type; // data type for the value of a function
347 typedef doublereal scalar_deriv_type; // data type for the value of a functions derivative
348 
349 template <typename T, index_type N_SIZE=0>
351 public:
352 
353 #if GRADIENT_VECTOR_REGISTER_SIZE > 0
354  static const int iVectorSize = GRADIENT_VECTOR_REGISTER_SIZE / sizeof(T);
355  typedef T __attribute__((vector_size(iVectorSize * sizeof(T)))) vector_type;
356 #else
357  #warning "vectorization not supported for this compiler"
358  static const int iVectorSize = 1;
359  typedef T vector_type;
360 #endif
361 
362  typedef T scalar_type;
363 
365  return iStart / iVectorSize;
366  }
367 
369  return iEnd / iVectorSize + (iEnd % iVectorSize ? 1 : 0);
370  }
371 };
372 
373 typedef doublereal scalar_func_type; // data type for the value of a function
374 typedef doublereal scalar_deriv_type; // data type for the value of a functions derivative
376 
377 template <typename T, index_type N_SIZE>
378 class RangeVector: public RangeVectorBase<T, N_SIZE> {
379 public:
383 
384  static const index_type iMaxSizeVector = N_SIZE / iVectorSize + (N_SIZE % iVectorSize ? 1 : 0);
385  static const index_type iMaxSize = N_SIZE;
386 
388 #if GRADIENT_DEBUG > 0
390 #endif
391  GRADIENT_ASSERT(bIsAligned());
392 
393  iStart = 0;
394  iEnd = 0;
395  iStartVec = 0;
396  iEndVec = 0;
397 
398  GRADIENT_ASSERT(bInvariant());
399  }
400 
402  GRADIENT_ASSERT(bIsAligned());
403 
404  Copy(v);
405 
406  GRADIENT_ASSERT(bInvariant());
407  }
408 
409  template <typename T2, index_type N_SIZE2>
411  GRADIENT_ASSERT(bIsAligned());
412 
413  Copy(v);
414 
415  GRADIENT_ASSERT(bInvariant());
416  }
417 
418  explicit RangeVector(index_type iStartNew, index_type iEndNew, const scalar_type& dVal) {
419  GRADIENT_ASSERT(bIsAligned());
420 
421  Initialize(iStartNew, iEndNew, dVal);
422 
423  GRADIENT_ASSERT(bInvariant());
424  }
425 
427  GRADIENT_ASSERT(bIsAligned());
428 
429  GRADIENT_ASSERT(bInvariant());
430 
431  Copy(v);
432 
433  GRADIENT_ASSERT(bInvariant());
434 
435  return *this;
436  }
437 
438  template <typename T2, index_type N_SIZE2>
440  GRADIENT_ASSERT(bIsAligned());
441 
442  GRADIENT_ASSERT(bInvariant());
443 
444  Copy(v);
445 
446  GRADIENT_ASSERT(bInvariant());
447 
448  return *this;
449  }
450 
451  void ResizeReset(index_type iStartNew, index_type iEndNew, const T& dVal) {
452  GRADIENT_ASSERT(bIsAligned());
453 
454  GRADIENT_ASSERT(bInvariant());
455 
456  Initialize(iStartNew, iEndNew, dVal);
457 
458  GRADIENT_ASSERT(bInvariant());
459  }
460 
461  void ResizePreserve(index_type iStartNew, index_type iEndNew) {
462  GRADIENT_ASSERT(bIsAligned());
463 
464  GRADIENT_ASSERT(bInvariant());
465 
466  iStart = iStartNew;
467  iEnd = iEndNew;
468  iStartVec = iRoundStartIndexVector(iStartNew);
469  iEndVec = iRoundEndIndexVector(iEndNew);
470 
473 
474  GRADIENT_ASSERT(bInvariant());
475  }
476 
477  void Reset() {
478  GRADIENT_ASSERT(bIsAligned());
479 
480  GRADIENT_ASSERT(bInvariant());
481  // Reset the data but preserve iStart and iEnd
483 
484  GRADIENT_ASSERT(bInvariant());
485  }
486 
487  void Reserve(index_type iMaxSizeNew) {
488  GRADIENT_ASSERT(bIsAligned());
489  GRADIENT_ASSERT(bInvariant());
490  GRADIENT_ASSERT(iMaxSizeNew <= iMaxSize);
491  }
492 
493  index_type iGetStartIndex() const { return iStart; }
494  index_type iGetEndIndex() const { return iEnd; }
495  index_type iGetSize() const { return iEnd - iStart; }
496  static index_type iGetMaxSize() { return iMaxSize; }
501 
503  GRADIENT_ASSERT(i >= 0);
506  GRADIENT_ASSERT(bIsAligned());
507 
508  return rgArray[i];
509  }
510 
511  void SetValue(index_type i, const scalar_type& d) {
512  GRADIENT_ASSERT(i >= 0);
515  GRADIENT_ASSERT(bIsAligned());
516 
517  rgArray[i] = d;
518  }
519 
521  GRADIENT_ASSERT(i >= 0);
523  GRADIENT_ASSERT(bIsAligned());
524  //GRADIENT_ASSERT(i >= iStartVec && i < iEndVec || rgArrayVec[i] == RangeVectorTraits<vector_type>::Zero());
525 
526  return rgArrayVec[i];
527  }
528 
530  GRADIENT_ASSERT(i >= 0);
533  GRADIENT_ASSERT(bIsAligned());
534 
535  rgArrayVec[i] = d;
536  }
537 
538  static bool bUseDynamicMem() { return false; }
539 
540 private:
543 
544  void Initialize(index_type iStartNew, index_type iEndNew, const scalar_type& dVal) {
545  iStart = iStartNew;
546  iEnd = iEndNew;
547  iStartVec = iRoundStartIndexVector(iStartNew);
548  iEndVec = iRoundEndIndexVector(iEndNew);
549 
551 
553  {
554  array_fill(rgArray + iStart, rgArray + iEnd, dVal);
555  }
556  }
557 
558  template <typename T2, index_type N_SIZE2>
560  typedef typename MaxSizeCheck<iMaxSize >= RangeVector<T2, N_SIZE2>::iMaxSize>::CheckType check_iMaxSize;
561 
562  iStart = v.iGetStartIndex();
563  iEnd = v.iGetEndIndex();
566 
567  // This allows fast access without frequent index checking
570 
571  CopyData(v);
572  }
573 
574  template <typename T2, index_type N_SIZE2>
576  for (index_type i = iStart; i < iEnd; ++i) {
577  SetValue(i, v.GetValue(i));
578  }
579  }
580 
581  void CopyData(const RangeVector& v) {
583  }
584 
585 #if GRADIENT_DEBUG > 0
586  bool bInvariant() const {
590 
591  const index_type iStartOffset = iStart - iStartVec * iVectorSize;
592 
593  GRADIENT_ASSERT(iStartOffset >= 0);
594  GRADIENT_ASSERT(iStartOffset < iVectorSize);
595 
596  const index_type iEndOffset = iEndVec * iVectorSize - iEnd;
597 
598  GRADIENT_ASSERT(iEndOffset >= 0);
599  GRADIENT_ASSERT(iEndOffset < iVectorSize);
600 
601  if (iGetSize() > 0) {
602  for (index_type i = 0; i < iStart; ++i) {
604  }
605 
606  for (index_type i = iEnd; i < iMaxSize; ++i) {
607  GRADIENT_ASSERT(GetValue(i) == RangeVectorTraits<scalar_type>::Zero());
608  }
609  }
610 
611  return true;
612  }
613 
614  bool bIsAligned() const {
615 #ifdef __GNUC__
616  GRADIENT_ASSERT(__alignof__(rgArrayVec) >= sizeof(vector_type));
617 #endif
618  const ptrdiff_t alignment = (reinterpret_cast<const char*>(rgArrayVec) - reinterpret_cast<const char*>(0)) % sizeof(vector_type);
619 
620  if (alignment != 0) {
621  silent_cout("address " << rgArrayVec << " has invalid alignment " << alignment << std::endl);
622  }
623 
624 #if defined(WIN32) || defined(__CYGWIN__)
625  // FIXME: Stack is aligned at 16 byte boundaries on 64bit Windows systems
626  // FIXME: Therefore this test will always fail on Windows
627  return true;
628 #else
629  GRADIENT_ASSERT(alignment == 0);
630  return alignment == 0;
631 #endif
632  }
633 #endif
634 
635  union
636  {
639 };
640 
642 };
643 
644 template <typename T>
645  class RangeVector<T, 0>: public RangeVectorBase<T, 0> {
646 public:
647  static const index_type iVectorSize = RangeVectorBase<T, 0>::iVectorSize;
650 
652 
653  static const index_type iMaxSizeVector = iMaxSize / iVectorSize;
654 
656  :pData(pNullData()) {
657  GRADIENT_ASSERT(bInvariant());
658  }
659 
661  :pData(pNullData()) {
662  ReserveMem(v.iGetStartIndex(), v.iGetEndIndex(), RESIZE);
663  Copy(v);
664 
665  GRADIENT_ASSERT(bInvariant());
666  }
667 
668  template <typename T2, index_type N_SIZE2>
670  :pData(pNullData()) {
671  ReserveMem(v.iGetStartIndex(), v.iGetEndIndex(), RESIZE);
672  Copy(v);
673 
674  GRADIENT_ASSERT(bInvariant());
675  }
676 
677  explicit RangeVector(index_type iStart, index_type iEnd, const scalar_type& dVal)
678  :pData(pNullData()) {
679  ReserveMem(iStart, iEnd, RESIZE);
680  Initialize(iStart, iEnd, dVal);
681 
682  GRADIENT_ASSERT(bInvariant());
683  }
684 
686  GRADIENT_ASSERT(bInvariant());
687 
688  FreeMem();
689  }
690 
692  GRADIENT_ASSERT(bInvariant());
693 
694  ReserveMem(v.iGetStartIndex(), v.iGetEndIndex(), RESIZE);
695  Copy(v);
696 
697  GRADIENT_ASSERT(bInvariant());
698 
699  return *this;
700  }
701 
702  template <typename T2, index_type N_SIZE2>
704  GRADIENT_ASSERT(bInvariant());
705 
706  ReserveMem(v.iGetStartIndex(), v.iGetEndIndex(), RESIZE);
707  Copy(v);
708 
709  GRADIENT_ASSERT(bInvariant());
710 
711  return *this;
712  }
713 
714  void ResizeReset(index_type iStartNew, index_type iEndNew, const scalar_type& dVal) {
715  GRADIENT_ASSERT(bInvariant());
716 
717  ReserveMem(iStartNew, iEndNew, RESIZE);
718  Initialize(iStartNew, iEndNew, dVal);
719 
720  GRADIENT_ASSERT(bInvariant());
721  }
722 
723  void ResizePreserve(index_type iStartNew, index_type iEndNew) {
724  GRADIENT_ASSERT(bInvariant());
725  const index_type iStartNewVec = iRoundStartIndexVector(iStartNew);
726  const index_type iEndNewVec = iRoundEndIndexVector(iEndNew);
727 
728  if (iStartNewVec == iGetStartIndexVector() && iEndNewVec - iStartNewVec <= iGetCapacityVector()) {
729  const index_type iEndCurrVec = iGetEndIndexVector();
730 
731  ReserveMem(iStartNew, iEndNew, RESIZE);
732 
733  GRADIENT_ASSERT(iEndCurrVec <= pData->iEndVec);
734 
735  array_fill(beginVec() + iEndCurrVec, endVec(), RangeVectorTraits<vector_type>::Zero());
736  }
737  else {
738  RangeVector<T, 0> oTmpVec;
739  oTmpVec.ReserveMem(iStartNew, iEndNew, RESIZE);
740  oTmpVec.Reset();
741  const index_type iComStartVec = std::max(iGetStartIndexVector(), oTmpVec.iGetStartIndexVector());
742  const index_type iComEndVec = std::min(iGetEndIndexVector(), oTmpVec.iGetEndIndexVector());
743 
744  array_copy(beginVec() + iComStartVec - iGetStartIndexVector(),
745  beginVec() + iComEndVec - iGetStartIndexVector(),
746  oTmpVec.beginVec() + iComStartVec - oTmpVec.iGetStartIndexVector());
747 
748  std::swap(pData, oTmpVec.pData);
749  }
750 
751  GRADIENT_ASSERT(bInvariant());
752  }
753 
754  void Reset() {
755  GRADIENT_ASSERT(bInvariant());
756  // Reset the data but preserve memory and iStart
757  array_fill(beginVec(), endVec(), RangeVectorTraits<vector_type>::Zero());
758 
759  GRADIENT_ASSERT(bInvariant());
760  }
761 
762  void Reserve(index_type iMaxSize) {
763  GRADIENT_ASSERT(bInvariant());
764 
765  const index_type iCurrCap = iGetCapacity();
766 
767  if (iCurrCap >= iMaxSize) {
768  return;
769  }
770 
771  if (iGetSizeVector() > 0) {
772  RangeVector oTmpVec;
773 
774  GRADIENT_ASSERT(oTmpVec.iGetSizeVector() == 0);
775 
776  oTmpVec.Reserve(iMaxSize);
777  oTmpVec = *this;
778 
779  std::swap(pData, oTmpVec.pData);
780  } else {
781  ReserveMem(0, iMaxSize, RESERVE);
782  }
783 
784  GRADIENT_ASSERT(bInvariant());
785  }
786 
788  return pData->iStart;
789  }
790 
792  return pData->iEnd;
793  }
794 
796  return pData->iStartVec;
797  }
798 
800  return pData->iEndVec;
801  }
802 
804  return pData->iEndVec - pData->iStartVec;
805  }
806 
808  return pData->iEnd - pData->iStart;
809  }
810 
811  static index_type iGetMaxSize() { return iMaxSize; }
813 
815  GRADIENT_ASSERT(i >= 0);
817 
818  return i >= iGetStartIndex() && i < iGetEndIndex()
819  ? begin()[i - iGetStartIndexVector() * iVectorSize]
821  }
822 
823  void SetValue(index_type i, const scalar_type& d) {
824  GRADIENT_ASSERT(i >= 0);
827 
828  begin()[i - iGetStartIndexVector() * iVectorSize] = d;
829  }
830 
832  GRADIENT_ASSERT(i >= 0);
834 
835  return i >= iGetStartIndexVector() && i < iGetEndIndexVector()
836  ? beginVec()[i - iGetStartIndexVector()]
838  }
839 
841  GRADIENT_ASSERT(i >= 0);
844 
845  beginVec()[i - iGetStartIndexVector()] = d;
846  }
847 
848  static bool bUseDynamicMem() { return true; }
849 
850 private:
853 
854  struct Data
855  {
861 
862  union
863  {
866  };
867  };
868 
870  {
871  return iGetCapacityVector() * iVectorSize;
872  }
873 
875  {
876  return pData->iCapacityVec;
877  }
878 
879  template <typename T2, index_type N_SIZE2>
881  for (index_type i = iGetStartIndex(); i < iGetEndIndex(); ++i) {
882  SetValue(i, v.GetValue(i));
883  }
884  }
885 
886  void Copy(const RangeVector& v) {
887  array_copy(v.beginVec(), v.endVec(), beginVec());
888  }
889 
890  void Initialize(index_type iStart, index_type iEnd, const scalar_type& dVal) {
891  Reset();
892 
894  {
895  array_fill(begin() + iStart - iGetStartIndexVector() * iVectorSize, begin() + iEnd - iGetStartIndexVector() * iVectorSize, dVal);
896  }
897  }
898 
899  enum MemFlags
900  {
902  RESERVE
903  };
904 
906  return pData->rgArray;
907  }
908 
910  return begin() + iGetSize();
911  }
912 
913  const scalar_type* begin() const {
914  return pData->rgArray;
915  }
916 
917  const scalar_type* end() const {
918  return begin() + iGetSize();
919  }
920 
922  return pData->rgArrayVec;
923  }
924 
926  return beginVec() + iGetSizeVector();
927  }
928 
929  const vector_type* beginVec() const {
930  return pData->rgArrayVec;
931  }
932 
933  const vector_type* endVec() const {
934  return beginVec() + iGetSizeVector();
935  }
936 
937  void ReserveMem(index_type iStartNew, index_type iEndNew, MemFlags eFlags = RESIZE) {
938  const index_type iCapCurr = iGetCapacityVector();
941  const index_type iSizeVec = iEndVec - iStartVec;
942 
943  if (iSizeVec > iCapCurr) {
944  FreeMem();
945 
946  pData = GradientAllocator<Data>::allocate_aligned(sizeof(vector_type), 1u, sizeof(vector_type) * iSizeVec);
947 
948  pData->iCapacityVec = iSizeVec;
949  }
950 
951  GRADIENT_ASSERT(((char*)(pData->rgArrayVec) - (char*)0) % sizeof(vector_type) == 0);
952 
953  if (pData == pNullData()) {
954  GRADIENT_ASSERT(iSizeVec == 0);
955 
956  return;
957  }
958 
959  if (eFlags == RESERVE) {
960  iStartNew = iEndNew = iStartVec = iEndVec = 0;
961  }
962 
963  pData->iStart = iStartNew;
964  pData->iEnd = iEndNew;
965  pData->iStartVec = iStartVec;
966  pData->iEndVec = iEndVec;
967  }
968 
969  void FreeMem() {
970  if (pData != pNullData()) {
972  pData = pNullData();
973  }
974  }
975 
976  static Data* pNullData()
977  {
978  GRADIENT_ASSERT(sNullData.iStart == 0);
979  GRADIENT_ASSERT(sNullData.iEnd == 0);
980  GRADIENT_ASSERT(sNullData.iStartVec == 0);
981  GRADIENT_ASSERT(sNullData.iEndVec == 0);
982  GRADIENT_ASSERT(sNullData.iCapacityVec == 0);
983 
984  return const_cast<Data*>(&sNullData);
985  }
986 
987 #if GRADIENT_DEBUG > 0
988  bool bInvariant() const {
989  GRADIENT_ASSERT(pData != 0);
990  GRADIENT_ASSERT(pData->iStart >= 0);
991  GRADIENT_ASSERT(pData->iStart <= pData->iEnd);
992  GRADIENT_ASSERT(pData->iEnd - pData->iStart <= iVectorSize * pData->iCapacityVec);
993  GRADIENT_ASSERT(pData->iStartVec >= 0);
994  GRADIENT_ASSERT(pData->iStartVec <= pData->iEndVec);
995  GRADIENT_ASSERT(pData->iEndVec - pData->iStartVec <= pData->iCapacityVec);
996 
997  const index_type iStartOffset = pData->iStart - pData->iStartVec * iVectorSize;
998 
999  GRADIENT_ASSERT(iStartOffset >= 0);
1000  GRADIENT_ASSERT(iStartOffset < iVectorSize);
1001 
1002  const index_type iEndOffset = pData->iEndVec * iVectorSize - pData->iEnd;
1003 
1004  GRADIENT_ASSERT(iEndOffset >= 0);
1005  GRADIENT_ASSERT(iEndOffset < iVectorSize);
1006 
1007  return true;
1008  }
1009 #endif
1010 
1011  Data* pData;
1012  static const Data sNullData;
1013 };
1014 
1015 template <typename T>
1017 
1019  // FIXME: There should be a flag for the initial derivatives phase
1020  // However this information is not available for elements at the moment
1021  // The prototype for Element::AssRes and Element::AssJac should be changed like
1022  // AssRes(..., enum FunctionCall func);
1023  // AssJac(..., enum FunctionCall func);
1024  STATE_MASK = 0x0F,
1038 };
1039 
1041 public:
1042  typedef std::vector<index_type, GradientAllocator<index_type> > VectorType;
1043  typedef VectorType::size_type size_type;
1044  typedef VectorType::const_iterator LocalIterator;
1045  typedef std::map<index_type,
1046  index_type,
1047  std::less<index_type>,
1050  typedef MapType::const_iterator GlobalIterator;
1051  static const index_type INVALID_INDEX = -1;
1052 
1053  explicit LocalDofMap(index_type iMaxSize=0)
1055 
1056  if (iMaxSize > 0) {
1057  oLocalToGlobal.reserve(iMaxSize);
1058  }
1059  }
1060 
1062  GRADIENT_ASSERT(iLocal >= 0);
1063  GRADIENT_ASSERT(size_type(iLocal) < oLocalToGlobal.size());
1064 
1065  index_type iGlobal = oLocalToGlobal[iLocal];
1066 
1067  GRADIENT_ASSERT(oGlobalToLocal.find(iGlobal)->second == iLocal);
1068  GRADIENT_ASSERT(oLocalToGlobal[iLocal] == iGlobal);
1069  GRADIENT_ASSERT(oLocalToGlobal.size() == oGlobalToLocal.size());
1070 
1071  return iGlobal;
1072  }
1073 
1075  MapType::const_iterator i = oGlobalToLocal.find(iGlobal);
1076 
1077  if (i == oGlobalToLocal.end()) {
1078  return INVALID_INDEX;
1079  }
1080 
1081  index_type iLocal = i->second;
1082 
1083  GRADIENT_ASSERT(iLocal >= 0);
1084  GRADIENT_ASSERT(size_type(iLocal) < oLocalToGlobal.size());
1085  GRADIENT_ASSERT(oLocalToGlobal[iLocal] == iGlobal);
1086  GRADIENT_ASSERT(oLocalToGlobal.size() == oGlobalToLocal.size());
1087 
1088  return iLocal;
1089  }
1090 
1092  MapType::const_iterator i = oGlobalToLocal.find(iGlobal);
1093 
1094  if (i != oGlobalToLocal.end()) {
1095  GRADIENT_ASSERT(i->first == iGlobal);
1096  return i->second;
1097  }
1098 
1099  index_type iLocal = oLocalToGlobal.size();
1100  oLocalToGlobal.push_back(iGlobal);
1101  oGlobalToLocal[iGlobal] = iLocal;
1102 
1103  GRADIENT_ASSERT(oGlobalToLocal[iGlobal] == iLocal);
1104  GRADIENT_ASSERT(oLocalToGlobal[iLocal] == iGlobal);
1105  GRADIENT_ASSERT(oLocalToGlobal.size() == oGlobalToLocal.size());
1106 
1107  return iLocal;
1108  }
1109 
1111  bool bReset = (func & STATE_MASK) != (eLastCall & STATE_MASK)
1112  || func == UNKNOWN_FUNC;
1113 
1114  eLastCall = func;
1115 
1116  if (bReset) {
1117  oLocalToGlobal.clear();
1118  oGlobalToLocal.clear();
1119  }
1120  }
1121 
1122  enum FunctionCall GetLastCall() const { return eLastCall; }
1123 
1124  LocalIterator BeginLocal() const { return oLocalToGlobal.begin(); }
1125  LocalIterator EndLocal() const { return oLocalToGlobal.end(); }
1126  GlobalIterator BeginGlobal() const { return oGlobalToLocal.begin(); }
1127  GlobalIterator EndGlobal() const { return oGlobalToLocal.end(); }
1128  index_type Size() const {
1129  GRADIENT_ASSERT(oGlobalToLocal.size() == oLocalToGlobal.size());
1130  return oLocalToGlobal.size();
1131  }
1132 
1133 private:
1137 };
1138 
1139 inline std::ostream& operator<<(std::ostream& os, const LocalDofMap& dof) {
1140  for (LocalDofMap::LocalIterator i = dof.BeginLocal(); i != dof.EndLocal(); ++i) {
1141  os << i - dof.BeginLocal() << "->" << *i << std::endl;
1142  }
1143 
1144  return os;
1145 }
1146 
1148 public:
1149  enum LocalScope { LOCAL };
1151 };
1152 
1153 template <index_type N_SIZE>
1154 class MapVector: public MapVectorBase {
1155 public:
1157  static const index_type iMaxSize = RangeVectorType::iMaxSize;
1160 
1161  template <index_type N_SIZE2>
1163  :pDofMap(v.pGetDofMap()), oRange(v.GetLocalVector()) {
1164 
1165  }
1166 
1167  MapVector(LocalDofMap* pMap=0, index_type iStartLocal=0, index_type iEndLocal=0, LocalScope= LOCAL, scalar_func_type dVal=0.)
1168  :pDofMap(pMap), oRange(iStartLocal, iEndLocal, dVal) {
1169  }
1170 
1172  :pDofMap(pMap), oRange(iLocal, iLocal + 1, dVal) {
1173  }
1174 
1175  MapVector(LocalDofMap* pMap, index_type iStartGlobal, index_type iEndGlobal, GlobalScope s, scalar_func_type dVal) {
1176  ResizeReset(pMap, iStartGlobal, iEndGlobal, s, dVal);
1177  }
1178 
1180  ResizeReset(pMap, iGlobal, iGlobal + 1, s, dVal);
1181  }
1182 
1183  void ResizeReset(LocalDofMap* pMap, index_type iStartLocal, index_type iEndLocal, LocalScope, scalar_func_type dVal) {
1184  pDofMap = pMap;
1185  oRange.ResizeReset(iStartLocal, iEndLocal, dVal);
1186  }
1187 
1188  void ResizePreserve(LocalDofMap* pMap, index_type iStartLocal, index_type iEndLocal, LocalScope) {
1189  pDofMap = pMap;
1190  oRange.ResizePreserve(iStartLocal, iEndLocal);
1191  }
1192 
1193  void ResizeReset(LocalDofMap* pMap, index_type iStartGlobal, index_type iEndGlobal, GlobalScope, scalar_func_type dVal) {
1194  pDofMap = pMap;
1195 
1196  GRADIENT_ASSERT(pDofMap != 0);
1197  GRADIENT_ASSERT(iEndGlobal >= iStartGlobal);
1198 
1199  index_type iFirstIndex = std::numeric_limits<index_type>::max(), iLastIndex = 0;
1200 
1201  for (index_type iGlobal = iStartGlobal; iGlobal < iEndGlobal; ++iGlobal) {
1202  index_type iLocal = pDofMap->AllocateLocalDof(iGlobal);
1203 
1204  if (iLocal < iFirstIndex) {
1205  iFirstIndex = iLocal;
1206  }
1207 
1208  if (iLocal > iLastIndex) {
1209  iLastIndex = iLocal;
1210  }
1211  }
1212 
1213  GRADIENT_ASSERT(iLastIndex >= iFirstIndex);
1214 
1215  oRange.ResizeReset(iFirstIndex, iLastIndex + 1, dVal);
1216  }
1217 
1218  void Reset() {
1219  oRange.Reset();
1220  }
1221 
1222  void Reserve(index_type iSize) {
1223  oRange.Reserve(iSize);
1224  }
1225 
1227  GRADIENT_ASSERT(pDofMap != 0);
1228  const index_type iLocalDof = pDofMap->iGetLocalIndex(iGlobalDof);
1229 
1230  if (iLocalDof == LocalDofMap::INVALID_INDEX) {
1231  GRADIENT_ASSERT(0);
1233  }
1234 
1235  GRADIENT_ASSERT(iLocalDof >= 0);
1236  GRADIENT_ASSERT(iLocalDof < iGetMaxSize());
1237  return oRange.GetValue(iLocalDof);
1238  }
1239 
1240  void SetGlobalVector(index_type iGlobalDof, scalar_deriv_type dValue) {
1241  GRADIENT_ASSERT(pDofMap != 0);
1242  const index_type iLocalDof = pDofMap->iGetLocalIndex(iGlobalDof);
1243 
1244  if (iLocalDof == LocalDofMap::INVALID_INDEX) {
1245  GRADIENT_ASSERT(0);
1247  }
1248 
1249  GRADIENT_ASSERT(iLocalDof >= iGetStartIndexLocal());
1250  GRADIENT_ASSERT(iLocalDof < iGetEndIndexLocal());
1251  oRange.SetValue(iLocalDof, dValue);
1252  }
1253 
1255  GRADIENT_ASSERT(pDofMap != 0);
1256  GRADIENT_ASSERT(iLocalDof >= iGetStartIndexLocal());
1257  GRADIENT_ASSERT(iLocalDof < iGetEndIndexLocal());
1258  return pDofMap->iGetGlobalDof(iLocalDof);
1259  }
1260 
1261  index_type iGetSize() const { return oRange.iGetSize(); }
1267  const RangeVectorType& GetLocalVector() const { return oRange; }
1268  void SetLocalVector(index_type i, scalar_type dValue){ oRange.SetValue(i, dValue); }
1274  LocalDofMap* pGetDofMap() const { return pDofMap; }
1275  bool bUseDynamicMem() const { return oRange.bUseDynamicMem(); }
1276  static const MapVector Zero;
1277 
1278 private:
1281 };
1282 
1283 template <typename Expression>
1285 public:
1286  typedef typename Expression::GradientType GradientType;
1287  static const index_type iDimension = GradientType::iDimension;
1291 
1293  :Expression(u) {
1294 
1295  }
1296 
1297  template <typename Expr>
1298  GradientExpression(const Expr& u)
1299  :Expression(u) {
1300 
1301  }
1302 
1303  template <typename LhsExpr, typename RhsExpr>
1304  GradientExpression(const LhsExpr& u, const RhsExpr& v)
1305  :Expression(u, v) {
1306 
1307  }
1308 };
1309 
1310 
1311 template <typename LhsExpr, typename RhsExpr>
1313  static const index_type iMaxDerivatives = LhsExpr::iMaxDerivatives > RhsExpr::iMaxDerivatives
1314  ? LhsExpr::iMaxDerivatives : RhsExpr::iMaxDerivatives;
1315 };
1316 
1317 template <index_type N_SIZE1, index_type N_SIZE2>
1319  static const index_type iDimension = (N_SIZE1 == 0 || N_SIZE2 == 0) ? 0 : (N_SIZE1 > N_SIZE2 ? N_SIZE1 : N_SIZE2);
1321 };
1322 
1323 template <typename BinFunc, typename LhsExpr, typename RhsExpr>
1324 class BinaryExpr {
1325 public:
1326  static const bool bAlias = LhsExpr::bAlias || RhsExpr::bAlias;
1328  static const bool bVectorize = sizeof(typename LhsExpr::vector_deriv_type) == sizeof(typename RhsExpr::vector_deriv_type)
1329  && LhsExpr::bVectorize && RhsExpr::bVectorize && BinFunc::bVectorize;
1335 
1336  typedef LhsExpr LhsExprType;
1337  typedef RhsExpr RhsExprType;
1338 
1339  BinaryExpr(const LhsExpr& u, const RhsExpr& v)
1340  :oU(u), oV(v) {
1341 #if GRADIENT_DEBUG > 0
1342  f = df_du = df_dv = NAN;
1343 #endif
1344  }
1345 
1347  GRADIENT_ASSERT(!std::isnan(f));
1348  return f;
1349  }
1350 
1352  GRADIENT_ASSERT(!std::isnan(f));
1353  GRADIENT_ASSERT(!std::isnan(df_du));
1354  GRADIENT_ASSERT(!std::isnan(df_dv));
1355 
1356  const scalar_deriv_type du_dX = oU.dGetDerivativeLocal(iLocalDof);
1357  const scalar_deriv_type dv_dX = oV.dGetDerivativeLocal(iLocalDof);
1358 
1359  return EvalDeriv(du_dX, dv_dX);
1360  }
1361 
1363  GRADIENT_ASSERT(!std::isnan(f));
1364  GRADIENT_ASSERT(!std::isnan(df_du));
1365  GRADIENT_ASSERT(!std::isnan(df_dv));
1366 
1367  const vector_deriv_type du_dX = oU.dGetDerivativeLocalVector(iLocalVecDof);
1368  const vector_deriv_type dv_dX = oV.dGetDerivativeLocalVector(iLocalVecDof);
1369 
1370  return EvalDeriv(du_dX, dv_dX);
1371  }
1372 
1374  return std::min(oU.iGetStartIndexLocal(), oV.iGetStartIndexLocal());
1375  }
1376 
1378  return std::max(oU.iGetEndIndexLocal(), oV.iGetEndIndexLocal());
1379  }
1380 
1382  return std::min(oU.iGetStartIndexLocalVector(), oV.iGetStartIndexLocalVector());
1383  }
1384 
1386  return std::max(oU.iGetEndIndexLocalVector(), oV.iGetEndIndexLocalVector());
1387  }
1388 
1390  LocalDofMap* pDofMap = oU.pGetDofMap();
1391 
1392  if (pDofMap == 0) {
1393  pDofMap = oV.pGetDofMap();
1394  } else {
1395  GRADIENT_ASSERT(oV.pGetDofMap() == 0 || pDofMap == oV.pGetDofMap());
1396  }
1397 
1398  return pDofMap;
1399  }
1400 
1401  bool bHaveReferenceTo(const void* p) const {
1402  return oU.bHaveReferenceTo(p) || oV.bHaveReferenceTo(p);
1403  }
1404 
1406  return iMaxDerivatives;
1407  }
1408 
1409  void Compute() const {
1410  GRADIENT_ASSERT(std::isnan(f));
1411  GRADIENT_ASSERT(std::isnan(df_du));
1412  GRADIENT_ASSERT(std::isnan(df_dv));
1413 
1414  oU.Compute();
1415  oV.Compute();
1416 
1417  const scalar_func_type u = oU.dGetValue();
1418  const scalar_func_type v = oV.dGetValue();
1419 
1420  f = BinFunc::f(u, v);
1421  df_du = BinFunc::df_du(u, v);
1422  df_dv = BinFunc::df_dv(u, v);
1423  }
1424 
1425 private:
1426  template <typename T>
1427  T EvalDeriv(T du_dX, T dv_dX) const {
1428  return df_du * du_dX + df_dv * dv_dX;
1429  }
1430 
1431 private:
1432  const LhsExpr oU;
1433  const RhsExpr oV;
1436 };
1437 
1438 template <typename UnFunc, typename Expr>
1439 class UnaryExpr {
1440 public:
1441  static const bool bAlias = Expr::bAlias;
1442  static const index_type iMaxDerivatives = Expr::iMaxDerivatives;
1443  static const bool bVectorize = Expr::bVectorize && UnFunc::bVectorize;
1444  typedef typename Expr::GradientType GradientType;
1445  static const index_type iDimension = GradientType::iDimension;
1449 
1450  UnaryExpr(const Expr& u)
1451  :oU(u) {
1452 #if GRADIENT_DEBUG > 0
1453  f = df_du = NAN;
1454 #endif
1455  }
1456 
1458  GRADIENT_ASSERT(!std::isnan(f));
1459  GRADIENT_ASSERT(!std::isnan(df_du));
1460 
1461  return f;
1462  }
1463 
1465  GRADIENT_ASSERT(!std::isnan(f));
1466  GRADIENT_ASSERT(!std::isnan(df_du));
1467 
1468  const scalar_deriv_type du_dX = oU.dGetDerivativeLocal(iLocalDof);
1469 
1470  return EvalDeriv(du_dX);
1471  }
1472 
1474  GRADIENT_ASSERT(!std::isnan(f));
1475  GRADIENT_ASSERT(!std::isnan(df_du));
1476 
1477  const vector_deriv_type du_dX = oU.dGetDerivativeLocalVector(iLocalVecDof);
1478 
1479  return EvalDeriv(du_dX);
1480  }
1481 
1483  return oU.iGetStartIndexLocal();
1484  }
1485 
1487  return oU.iGetEndIndexLocal();
1488  }
1489 
1491  return oU.iGetStartIndexLocalVector();
1492  }
1493 
1495  return oU.iGetEndIndexLocalVector();
1496  }
1497 
1499  return oU.pGetDofMap();
1500  }
1501 
1502  bool bHaveReferenceTo(const void* p) const {
1503  return oU.bHaveReferenceTo(p);
1504  }
1505 
1507  return iMaxDerivatives;
1508  }
1509 
1510  void Compute() const {
1511  GRADIENT_ASSERT(std::isnan(f));
1512  GRADIENT_ASSERT(std::isnan(df_du));
1513 
1514  oU.Compute();
1515 
1516  const scalar_func_type u = oU.dGetValue();
1517 
1518  f = UnFunc::f(u);
1519  df_du = UnFunc::df_du(u);
1520  }
1521 
1522 
1523 private:
1524  template <typename T>
1525  T EvalDeriv(T du_dX) const
1526  {
1527  return df_du * du_dX;
1528  }
1529 
1530 private:
1531  const Expr oU;
1534 };
1535 
1536 template <typename T, bool ALIAS=false>
1537 class DirectExpr {
1538 public:
1539  static const bool bAlias = ALIAS;
1540  typedef typename T::GradientType GradientType;
1541  static const index_type iMaxDerivatives = GradientType::iMaxDerivatives;
1542  static const bool bVectorize = true;
1543  static const index_type iDimension = GradientType::iDimension;
1547 
1548  DirectExpr(const T& g)
1549  :oG(g) {
1550 
1551  }
1552 
1554  return oG.dGetValue();
1555  }
1556 
1558  return oG.dGetDerivativeLocal(iLocalDof);
1559  }
1560 
1562  return oG.dGetDerivativeLocalVector(iLocalVecDof);
1563  }
1564 
1566  return oG.iGetStartIndexLocal();
1567  }
1568 
1570  return oG.iGetEndIndexLocal();
1571  }
1572 
1574  return oG.iGetStartIndexLocalVector();
1575  }
1576 
1578  return oG.iGetEndIndexLocalVector();
1579  }
1580 
1582  return oG.pGetDofMap();
1583  }
1584 
1585  bool bHaveReferenceTo(const void* p) const {
1586  return p == &oG;
1587  }
1588 
1590  return oG.iGetMaxDerivatives();
1591  }
1592 
1593  void Compute() const {}
1594 
1595 private:
1597 };
1598 
1599 template <typename T>
1600 class ConstExpr {
1601 public:
1602  static const bool bAlias = false;
1603  static const index_type iMaxDerivatives = 0;
1604  static const bool bVectorize = true;
1605  typedef T GradientType;
1606  static const index_type iDimension = GradientType::iDimension;
1610 
1612  :dConst(a) {
1613 
1614  }
1615 
1617  return dConst;
1618  }
1619 
1621  return scalar_deriv_type();
1622  }
1623 
1625  return vector_deriv_type();
1626  }
1627 
1629  // Note: we must not combine two ConstExpr objects in a BinaryExpr!
1630  // If that happens, we would run out of memory!
1631  // However that would be meaningless at all.
1632  return std::numeric_limits<index_type>::max();
1633  }
1634 
1636  return 0;
1637  }
1638 
1640  return std::numeric_limits<index_type>::max();
1641  }
1642 
1644  return 0;
1645  }
1646 
1648  return 0;
1649  }
1650 
1651  bool bHaveReferenceTo(const void* p) const {
1652  return false;
1653  }
1654 
1656  return iMaxDerivatives;
1657  }
1658 
1659  void Compute() const {}
1660 
1661 private:
1663 };
1664 
1665 template <typename BoolFunc, typename LhsExpr, typename RhsExpr>
1666 class BoolExpr {
1667 public:
1668  static const bool bAlias = LhsExpr::bAlias || RhsExpr::bAlias;
1670  static const bool bVectorize = true;
1676 
1677  BoolExpr(const LhsExpr& u, const RhsExpr& v)
1678  :oU(u), oV(v) {
1679  }
1680 
1681  bool dGetValue() const {
1682  oU.Compute();
1683  oV.Compute();
1684 
1685  const scalar_func_type u = oU.dGetValue();
1686  const scalar_func_type v = oV.dGetValue();
1687 
1688  return BoolFunc::f(u, v);
1689  }
1690 
1691  operator bool() const {
1692  return dGetValue();
1693  }
1694 
1696  return scalar_deriv_type();
1697  }
1698 
1700  return vector_deriv_type();
1701  }
1702 
1704  return std::numeric_limits<index_type>::max();
1705  }
1706 
1708  return 0;
1709  }
1710 
1712  return std::numeric_limits<index_type>::max();
1713  }
1714 
1716  return 0;
1717  }
1718 
1720  LocalDofMap* pDofMap = oU.pGetDofMap();
1721 
1722  if (pDofMap == 0) {
1723  pDofMap = oV.pGetDofMap();
1724  } else {
1725  GRADIENT_ASSERT(oV.pGetDofMap() == 0 || pDofMap == oV.pGetDofMap());
1726  }
1727 
1728  return pDofMap;
1729  }
1730 
1732  return iMaxDerivatives;
1733  }
1734 
1735  void Compute() const {
1736  }
1737 
1738 private:
1739  bool bHaveReferenceTo(const void* p) const {
1740  return oU.bHaveReferenceTo(p) || oV.bHaveReferenceTo(p);
1741  }
1742 
1743 private:
1744  const LhsExpr oU;
1745  const RhsExpr oV;
1746 };
1747 
1748 class FuncPlus {
1749 public:
1750  static const bool bVectorize = true;
1751 
1753  return u + v;
1754  }
1755 
1757  return 1.;
1758  }
1759 
1761  return 1.;
1762  }
1763 };
1764 
1765 class FuncMinus {
1766 public:
1767  static const bool bVectorize = true;
1768 
1770  return u - v;
1771  }
1772 
1774  return 1.;
1775  }
1776 
1778  return -1.;
1779  }
1780 };
1781 
1782 class FuncMult {
1783 public:
1784  static const bool bVectorize = true;
1785 
1787  return u * v;
1788  }
1789 
1791  return v;
1792  }
1793 
1795  return u;
1796  }
1797 };
1798 
1799 class FuncDiv {
1800 public:
1801  static const bool bVectorize = true;
1802 
1804  return u / v;
1805  }
1806 
1808  return 1. / v;
1809  }
1810 
1812  return -u / (v * v);
1813  }
1814 };
1815 
1816 class FuncPow {
1817 public:
1818  static const bool bVectorize = false;
1819 
1821  return pow(u, v);
1822  }
1823 
1825  return v * pow(u, v - 1.);
1826  }
1827 
1829  return pow(u, v) * log(u);
1830  }
1831 };
1832 
1833 class FuncAtan2 {
1834 public:
1835  static const bool bVectorize = false;
1836 
1838  return atan2(u, v);
1839  }
1840 
1842  return v / (v * v + u * u);
1843  }
1844 
1846  return -u / (v * v + u * u);
1847  }
1848 };
1849 
1851 public:
1852  static const bool bVectorize = false;
1853 
1855  return copysign(u, v);
1856  }
1857 
1859  return copysign(1., u) * copysign(1., v);
1860  }
1861 
1863  return 0.;
1864  }
1865 };
1866 
1867 class FuncFabs {
1868 public:
1869  static const bool bVectorize = false;
1870 
1872  return fabs(u);
1873  }
1874 
1876  return copysign(1., u);
1877  }
1878 };
1879 
1880 class FuncSqrt {
1881 public:
1882  static const bool bVectorize = false;
1883 
1885  return sqrt(u);
1886  }
1887 
1889  return 1. / (2. * sqrt(u));
1890  }
1891 };
1892 
1893 class FuncExp {
1894 public:
1895  static const bool bVectorize = false;
1896 
1898  return exp(u);
1899  }
1900 
1902  return exp(u);
1903  }
1904 };
1905 
1906 class FuncLog {
1907 public:
1908  static const bool bVectorize = false;
1909 
1911  return log(u);
1912  }
1913 
1915  return 1. / u;
1916  }
1917 };
1918 
1919 class FuncSin {
1920 public:
1921  static const bool bVectorize = false;
1922 
1924  return sin(u);
1925  }
1926 
1928  return cos(u);
1929  }
1930 };
1931 
1932 class FuncCos {
1933 public:
1934  static const bool bVectorize = false;
1935 
1937  return cos(u);
1938  }
1939 
1941  return -sin(u);
1942  }
1943 };
1944 
1945 class FuncTan {
1946 public:
1947  static const bool bVectorize = false;
1948 
1950  return tan(u);
1951  }
1952 
1954  const scalar_deriv_type tan_u = tan(u);
1955  return 1. + tan_u * tan_u;
1956  }
1957 };
1958 
1959 class FuncSinh {
1960 public:
1961  static const bool bVectorize = false;
1962 
1964  return sinh(u);
1965  }
1966 
1968  return cosh(u);
1969  }
1970 };
1971 
1972 class FuncCosh {
1973 public:
1974  static const bool bVectorize = false;
1975 
1977  return cosh(u);
1978  }
1979 
1981  return sinh(u);
1982  }
1983 };
1984 
1985 class FuncTanh {
1986 public:
1987  static const bool bVectorize = false;
1988 
1990  return tanh(u);
1991  }
1992 
1994  const scalar_deriv_type tanh_u = tanh(u);
1995  return 1. - tanh_u * tanh_u;
1996  }
1997 };
1998 
1999 class FuncAsin {
2000 public:
2001  static const bool bVectorize = false;
2002 
2004  return asin(u);
2005  }
2006 
2008  return 1. / sqrt(1 - u * u);
2009  }
2010 };
2011 
2012 class FuncAcos {
2013 public:
2014  static const bool bVectorize = false;
2015 
2017  return acos(u);
2018  }
2019 
2021  return -1. / sqrt(1 - u * u);
2022  }
2023 };
2024 
2025 class FuncAtan {
2026 public:
2027  static const bool bVectorize = false;
2028 
2030  return atan(u);
2031  }
2032 
2034  return 1. / (1. + u * u);
2035  }
2036 };
2037 
2038 #if HAVE_ASINH
2039 class FuncAsinh {
2040 public:
2041  static const bool bVectorize = false;
2042 
2043  static scalar_func_type f(scalar_func_type u) {
2044  return asinh(u);
2045  }
2046 
2047  static scalar_deriv_type df_du(scalar_func_type u) {
2048  return 1. / sqrt(1. + u * u);
2049  }
2050 };
2051 #endif
2052 
2053 #if HAVE_ACOSH
2054 class FuncAcosh {
2055 public:
2056  static const bool bVectorize = false;
2057 
2058  static scalar_func_type f(scalar_func_type u) {
2059  return acosh(u);
2060  }
2061 
2062  static scalar_deriv_type df_du(scalar_func_type u) {
2063  return 1. / sqrt(u * u - 1.);
2064  }
2065 };
2066 #endif
2067 
2068 #if HAVE_ATANH
2069 class FuncAtanh {
2070 public:
2071  static const bool bVectorize = false;
2072 
2073  static scalar_func_type f(scalar_func_type u) {
2074  return atanh(u);
2075  }
2076 
2077  static scalar_deriv_type df_du(scalar_func_type u) {
2078  return 1. / (1. - u * u);
2079  }
2080 };
2081 #endif
2082 
2084 public:
2085  static const bool bVectorize = true;
2086 
2088  return -u;
2089  }
2090 
2092  return -1.;
2093  }
2094 };
2095 
2097 public:
2098  static const bool bVectorize = true;
2099 
2101  return u < v;
2102  }
2103 };
2104 
2106 public:
2107  static const bool bVectorize = true;
2108 
2110  return u <= v;
2111  }
2112 };
2113 
2115 public:
2116  static const bool bVectorize = true;
2117 
2119  return u > v;
2120  }
2121 };
2122 
2124 public:
2125  static const bool bVectorize = true;
2126 
2128  return u >= v;
2129  }
2130 };
2131 
2133 public:
2134  static const bool bVectorize = true;
2135 
2137  return u == v;
2138  }
2139 };
2140 
2142 public:
2143  static const bool bVectorize = true;
2144 
2146  return u != v;
2147  }
2148 };
2149 
2150 template <bool bVectorize>
2152 
2153 template <>
2155  template <typename MapVectorType, typename Expression>
2156  static void ApplyDerivative(MapVectorType& ad, const GradientExpression<Expression>& f) {
2157  for (index_type i = ad.iGetStartIndexLocal(); i < ad.iGetEndIndexLocal(); ++i) {
2158  ad.SetLocalVector(i, f.dGetDerivativeLocal(i));
2159  }
2160  }
2161 
2162  template <typename MapVectorType, typename Expression>
2163  static void ApplyBinaryFunction1(MapVectorType& ad,
2165  const index_type iStartLocal,
2166  const index_type iEndLocal,
2167  const scalar_deriv_type df_du,
2168  const scalar_deriv_type df_dv) {
2169  for (index_type i = iStartLocal; i < iEndLocal; ++i) {
2170  const scalar_deriv_type ud = ad.dGetLocalVector(i);
2171  const scalar_deriv_type vd = f.dGetDerivativeLocal(i);
2172  ad.SetLocalVector(i, df_du * ud + df_dv * vd);
2173  }
2174  }
2175 };
2176 
2177 template <>
2179  template <typename MapVectorType, typename Expression>
2180  static void ApplyDerivative(MapVectorType& ad, const GradientExpression<Expression>& f) {
2181  for (index_type i = ad.iGetStartIndexLocalVector(); i < ad.iGetEndIndexLocalVector(); ++i) {
2182  ad.SetLocalVectorVector(i, f.dGetDerivativeLocalVector(i));
2183  }
2184  }
2185 
2186  template <typename MapVectorType, typename Expression>
2187  static void ApplyBinaryFunction1(MapVectorType& ad,
2189  const index_type iStartLocal,
2190  const index_type iEndLocal,
2195  const index_type iStartLocalVec = RangeVectorType::iRoundStartIndexVector(iStartLocal);
2196  const index_type iEndLocalVec = RangeVectorType::iRoundEndIndexVector(iEndLocal);
2197 
2198  for (index_type i = iStartLocalVec; i < iEndLocalVec; ++i) {
2199  const vector_deriv_type ud = ad.dGetLocalVectorVector(i);
2200  const vector_deriv_type vd = f.dGetDerivativeLocalVector(i);
2201  ad.SetLocalVectorVector(i, df_du * ud + df_dv * vd);
2202  }
2203  }
2204 };
2205 
2206 template <bool ALIAS>
2208 
2209 template <>
2211  template <typename GradientType, typename Expression>
2212  static inline void ApplyExpression(GradientType& g, const GradientExpression<Expression>& f) {
2213  g.ApplyNoAlias(f);
2214  }
2215 
2216  template <typename BinFunc, typename GradientType, typename Expression>
2217  static void ApplyBinaryFunction(GradientType& g, const GradientExpression<Expression>& f, const BinFunc& bfunc) {
2218  g.ApplyBinaryFunctionNoAlias(f, bfunc);
2219  }
2220 };
2221 
2222 template <>
2224  template <typename GradientType, typename Expression>
2225  static inline void ApplyExpression(GradientType& g, const GradientExpression<Expression>& f) {
2226  g.ApplyWithAlias(f);
2227  }
2228 
2229  template <typename BinFunc, typename GradientType, typename Expression>
2230  static void ApplyBinaryFunction(GradientType& g, const GradientExpression<Expression>& f, const BinFunc& bfunc) {
2231  g.ApplyBinaryFunctionWithAlias(f, bfunc);
2232  }
2233 };
2234 
2235 template <index_type N_SIZE>
2236 class Gradient {
2238 
2239 public:
2241  static const index_type iDimension = N_SIZE;
2242  static const index_type iMaxDerivatives = MapVectorType::iMaxSize;
2244  typedef typename MapVectorType::scalar_type scalar_deriv_type;
2245  typedef typename MapVectorType::vector_type vector_deriv_type;
2246 
2247  explicit Gradient(scalar_func_type a = 0., LocalDofMap* pDofMap = 0)
2248  :a(a), ad(pDofMap){
2249  }
2250 
2252  :a(a), ad(da) {
2253 
2254  }
2255 
2256  Gradient(const Gradient& g)
2257  :a(g.a), ad(g.ad) {
2258 
2259  }
2260 
2261  template <index_type N_SIZE2>
2263  :a(g.a), ad(g.ad) {
2264 
2265  }
2266 
2267  template <index_type N_SIZE2>
2269 #if GRADIENT_DEBUG > 0
2270  a = NAN;
2271 #endif
2272  Copy(g, pDofMap);
2273  }
2274 
2275  template <typename Expression>
2277  :ad(f.pGetDofMap(), f.iGetStartIndexLocal(), f.iGetEndIndexLocal(), MapVector<N_SIZE>::LOCAL, 0.) {
2278 
2279  // No aliases are possible because the object did not exist before
2280  GRADIENT_ASSERT(!f.bHaveReferenceTo(this));
2281 
2282  f.Compute();
2283  a = f.dGetValue();
2284 
2285  ApplyDerivative(f);
2286  }
2287 
2288  template <index_type N_SIZE2>
2289  void Copy(const Gradient<N_SIZE2>& g, LocalDofMap* pDofMap) {
2290  LocalDofMap* pDofMap2 = g.pGetDofMap();
2291 
2292  index_type iFirstLocal = std::numeric_limits<index_type>::max();
2293  index_type iLastLocal = 0;
2294 
2295  GRADIENT_TRACE("g=" << g << std::endl);
2296  GRADIENT_TRACE("*pDofMap=" << *pDofMap << std::endl);
2297  GRADIENT_TRACE("*pDofMap2=" << *pDofMap2 << std::endl);
2298 
2299  for (index_type i = g.iGetStartIndexLocal(); i < g.iGetEndIndexLocal(); ++i) {
2300  if (g.dGetDerivativeLocal(i) == 0.) {
2301  // FIXME: Checking for zero could cause problems with the KLU linear solver
2302  // because the matrix structure could change.
2303  // But we have to omit zeros here, otherwise we could allocate
2304  // too much local degrees of freedom and cause a buffer overflow
2305  // in case of stack based Gradients.
2306  continue;
2307  }
2308 
2309  const index_type iGlobal = pDofMap2->iGetGlobalDof(i);
2310  const index_type iLocal = pDofMap->AllocateLocalDof(iGlobal);
2311 
2312  if (iLocal < iFirstLocal) {
2313  iFirstLocal = iLocal;
2314  }
2315 
2316  if (iLocal > iLastLocal) {
2317  iLastLocal = iLocal;
2318  }
2319  }
2320 
2321  ++iLastLocal;
2322 
2323  if (iLastLocal <= iFirstLocal) {
2324  iFirstLocal = iLastLocal = 0;
2325  }
2326 
2327  GRADIENT_TRACE("*pDofMap=" << *pDofMap << std::endl);
2328 
2329  a = g.dGetValue();
2330  ad.ResizeReset(pDofMap, iFirstLocal, iLastLocal, MapVectorBase::LOCAL, 0.);
2331 
2332  GRADIENT_TRACE("*pDofMap2=" << *pDofMap2 << std::endl);
2333  GRADIENT_TRACE("*pDofMap=" << *pDofMap << std::endl);
2334 
2335  for (index_type i = iFirstLocal; i < iLastLocal; ++i) {
2336  const index_type iGlobal = pDofMap->iGetGlobalDof(i);
2337  const index_type iLocal2 = pDofMap2->iGetLocalIndex(iGlobal);
2338 
2339  if (iLocal2 == LocalDofMap::INVALID_INDEX) {
2340  // Note: It can happen that there is no
2341  // corresponding entry in pDofMap2.
2342  // However it should be safe to ignore such entries.
2343  continue;
2344  }
2345 
2346  ad.SetLocalVector(i, g.dGetDerivativeLocal(iLocal2));
2347  }
2348 
2349 #if GRADIENT_DEBUG > 0
2350  for (index_type i = iFirstLocal; i < iLastLocal; ++i) {
2351  const index_type iGlobal = ad.iGetGlobalDof(i);
2352  const index_type iLocal2 = g.pGetDofMap()->iGetLocalIndex(iGlobal);
2353  if (iLocal2 != LocalDofMap::INVALID_INDEX) {
2354  GRADIENT_ASSERT(g.dGetDerivativeGlobal(iGlobal) == ad.dGetLocalVector(i));
2355  }
2356  }
2357 
2358  for (index_type i = g.iGetStartIndexLocal(); i < g.iGetEndIndexLocal(); ++i) {
2359  if (g.dGetDerivativeLocal(i) != 0.) {
2360  const index_type iGlobal = g.iGetGlobalDof(i);
2361  GRADIENT_ASSERT(ad.dGetGlobalVector(iGlobal) == g.dGetDerivativeLocal(i));
2362  }
2363  }
2364 #endif
2365  }
2366 
2367  void Reset() {
2368  a = 0.;
2369  ad.Reset();
2370  }
2371 
2372  void Reserve(index_type iSize) {
2373  ad.Reserve(iSize);
2374  }
2375 
2377  // This operator is needed for matrix/vector expressions with different base types
2378  SetValue(d);
2379  return *this;
2380  }
2381 
2382  template <typename Expression>
2384  ApplyAliasHelper<GradientExpression<Expression>::bAlias>::ApplyExpression(*this, f);
2385 
2386  return *this;
2387  }
2388 
2389  template <index_type N_SIZE2>
2391  a = g.dGetValue();
2392  ad = g.GetDerivativeLocal();
2393 
2394  return *this;
2395  }
2396 
2398  if (&g != this) {
2399  a = g.a;
2400  ad = g.ad;
2401  }
2402 
2403  return *this;
2404  }
2405 
2406  inline Gradient& operator+=(const Gradient& g) {
2407  ApplyBinaryFunction<FuncPlus>(GradientExpression<DirectExpr<Gradient> >(g));
2408  return *this;
2409  }
2410 
2411  inline Gradient& operator-=(const Gradient& g) {
2412  ApplyBinaryFunction<FuncMinus>(GradientExpression<DirectExpr<Gradient> >(g));
2413  return *this;
2414  }
2415 
2416  inline Gradient& operator*=(const Gradient& g) {
2417  ApplyBinaryFunction<FuncMult>(GradientExpression<DirectExpr<Gradient> >(g));
2418  return *this;
2419  }
2420 
2421  inline Gradient& operator/=(const Gradient& g) {
2422  ApplyBinaryFunction<FuncDiv>(GradientExpression<DirectExpr<Gradient> >(g));
2423  return *this;
2424  }
2425 
2426  inline Gradient& operator++() {
2427  ++a;
2428  return *this;
2429  }
2430 
2432  --a;
2433  return *this;
2434  }
2435 
2436  inline Gradient operator++(int){
2437  const Gradient<N_SIZE> tmp(*this);
2438  a++;
2439  return tmp;
2440  }
2441 
2442  inline Gradient operator--(int) {
2443  const Gradient<N_SIZE> tmp(*this);
2444  a--;
2445  return tmp;
2446  }
2447 
2448  template <typename Expression>
2450  ApplyBinaryFunction<FuncPlus>(f);
2451  return *this;
2452  }
2453 
2454  template <typename Expression>
2456  ApplyBinaryFunction<FuncMinus>(f);
2457  return *this;
2458  }
2459 
2460  template <typename Expression>
2462  ApplyBinaryFunction<FuncMult>(f);
2463  return *this;
2464  }
2465 
2466  template <typename Expression>
2468  ApplyBinaryFunction<FuncDiv>(f);
2469  return *this;
2470  }
2471 
2473  a += d;
2474  return *this;
2475  }
2476 
2478  a -= d;
2479  return *this;
2480  }
2481 
2483  a *= d;
2484 
2485  for (index_type i = iGetStartIndexLocal(); i < iGetEndIndexLocal(); ++i) {
2486  ad.SetLocalVector(i, ad.dGetLocalVector(i) * d);
2487  }
2488 
2489  return *this;
2490  }
2491 
2493  a /= d;
2494 
2495  for (index_type i = iGetStartIndexLocal(); i < iGetEndIndexLocal(); ++i) {
2496  ad.SetLocalVector(i, ad.dGetLocalVector(i) / d);
2497  }
2498 
2499  return *this;
2500  }
2501 
2503  return a;
2504  }
2505 
2507  a = dVal;
2508  ad.ResizeReset(0, 0, 0, MapVector<N_SIZE>::LOCAL, 0.);
2509  }
2510 
2512  // Keep the same derivatives - needed for the Node class for example
2513  a = dVal;
2514  }
2515 
2517  GRADIENT_ASSERT(iLocalDof >= 0);
2518  GRADIENT_ASSERT(iLocalDof < iGetMaxDerivatives());
2519  return ad.dGetLocalVector(iLocalDof);
2520  }
2521 
2523  GRADIENT_ASSERT(iLocalVecDof >= 0);
2524  GRADIENT_ASSERT(iLocalVecDof < iGetMaxDerivativesVector());
2525  return ad.dGetLocalVectorVector(iLocalVecDof);
2526  }
2527 
2529  return ad;
2530  }
2531 
2533  GRADIENT_ASSERT(ad.pGetDofMap()->iGetLocalIndex(iGlobalDof) >= 0);
2534  GRADIENT_ASSERT(ad.pGetDofMap()->iGetLocalIndex(iGlobalDof) < iGetMaxDerivatives());
2535  return ad.dGetGlobalVector(iGlobalDof);
2536  }
2537 
2539  ad.ResizeReset(pMap, iStartGlobal, iEndGlobal, s, dVal);
2540  }
2541 
2543  ad.ResizeReset(pMap, iStartLocal, iEndLocal, s, dVal);
2544  }
2545 
2547  DerivativeResizeReset(pMap, iGlobal, iGlobal + 1, s, dVal);
2548  }
2549 
2551  DerivativeResizeReset(pMap, iLocal, iLocal + 1, s, dVal);
2552  }
2553 
2555  GRADIENT_ASSERT(iLocalDof >= iGetStartIndexLocal());
2556  GRADIENT_ASSERT(iLocalDof < iGetEndIndexLocal());
2557  ad.SetLocalVector(iLocalDof, dCoef);
2558  }
2559 
2561  GRADIENT_ASSERT(iLocalVecDof >= iGetStartIndexLocalVector());
2562  GRADIENT_ASSERT(iLocalVecDof < iGetEndIndexLocalVector());
2563  ad.SetLocalVectorVector(iLocalVecDof, dVec);
2564  }
2565 
2567  GRADIENT_ASSERT(ad.pGetDofMap()->iGetLocalIndex(iGlobalDof) >= iGetStartIndexLocal());
2568  GRADIENT_ASSERT(ad.pGetDofMap()->iGetLocalIndex(iGlobalDof) < iGetEndIndexLocal());
2569  ad.SetGlobalVector(iGlobalDof, dCoef);
2570  }
2571 
2573  return ad.iGetGlobalDof(iLocalDof);
2574  }
2575 
2577  return ad.iGetStartIndexLocal();
2578  }
2579 
2581  return ad.iGetEndIndexLocal();
2582  }
2583 
2585  return ad.iGetStartIndexLocalVector();
2586  }
2587 
2589  return ad.iGetEndIndexLocalVector();
2590  }
2591 
2594  }
2595 
2597  return ad.iGetMaxSize();
2598  }
2599 
2601  return ad.iGetMaxSizeVector();
2602  }
2603 
2604  bool bIsEqual(const Gradient& g) const {
2605  if (dGetValue() != g.dGetValue()) {
2606  return false;
2607  }
2608 
2609  const index_type iStart = std::min(iGetStartIndexLocal(), g.iGetStartIndexLocal());
2610  const index_type iEnd = std::max(iGetEndIndexLocal(), g.iGetEndIndexLocal());
2611 
2612  for (index_type i = iStart; i < iEnd; ++i) {
2613  if (dGetDerivativeLocal(i) != g.dGetDerivativeLocal(i)) {
2614  return false;
2615  }
2616  }
2617 
2618  return true;
2619  }
2620 
2621  template <typename Expression>
2623  return bIsEqual(Gradient(g));
2624  }
2625 
2627  return ad.pGetDofMap();
2628  }
2629 
2630  bool bHaveReferenceTo(const void* p) const {
2631  return this == p;
2632  }
2633 
2634 private:
2635  friend struct ApplyAliasHelper<true>;
2636  friend struct ApplyAliasHelper<false>;
2637 
2638  template <typename Expression>
2640  GRADIENT_ASSERT(f.iGetMaxDerivatives() <= iGetMaxDerivatives());
2641  GRADIENT_ASSERT(!f.bHaveReferenceTo(this));
2642 
2643  f.Compute();
2644 
2645  a = f.dGetValue();
2646 
2647  ad.ResizeReset(f.pGetDofMap(), f.iGetStartIndexLocal(), f.iGetEndIndexLocal(), MapVector<N_SIZE>::LOCAL, 0.);
2648 
2649  ApplyDerivative(f);
2650  }
2651 
2652  template <typename Expression>
2654  // Attention: If we have an expression like a = (a + x)
2655  // we must not overwrite the contents of a
2656  // until the expression (a + x) has been evaluated
2657  *this = Gradient(f);
2658  }
2659 
2660  template <typename Expression>
2662  // compile time check for the maximum number of derivatives
2663  typedef typename MaxSizeCheck<iMaxDerivatives >= Expression::iMaxDerivatives>::CheckType check_iMaxDerivatives;
2664  const bool bVectorize = sizeof(vector_deriv_type) == sizeof(typename GradientExpression<Expression>::vector_deriv_type)
2666  GRADIENT_ASSERT(f.iGetMaxDerivatives() <= iGetMaxDerivatives());
2667 
2669  }
2670 
2671  template <typename BinFunc, typename Expression>
2674  }
2675 
2676  template <typename BinFunc, typename Expression>
2678  typedef typename MaxSizeCheck<iMaxDerivatives >= Expression::iMaxDerivatives>::CheckType check_iMaxDerivatives;
2679  const bool bVectorize = sizeof(vector_deriv_type) == sizeof(typename GradientExpression<Expression>::vector_deriv_type)
2681 
2682  GRADIENT_ASSERT(!f.bHaveReferenceTo(this));
2683  LocalDofMap* pDofMap = pGetDofMap();
2684  LocalDofMap* pDofMap2 = f.pGetDofMap();
2685 
2686  if (pDofMap2 == 0) {
2687  pDofMap2 = pDofMap;
2688  }
2689 
2690  if (pDofMap == 0) {
2691  pDofMap = pDofMap2;
2692  }
2693 
2694  f.Compute();
2695 
2696  const scalar_func_type u = a;
2697  const scalar_func_type v = f.dGetValue();
2698 
2699  a = BinFunc::f(u, v);
2700  const scalar_deriv_type df_du = BinFunc::df_du(u, v);
2701  const scalar_deriv_type df_dv = BinFunc::df_dv(u, v);
2702 
2703  const index_type iStartFunc = f.iGetStartIndexLocal();
2704  const index_type iEndFunc = f.iGetEndIndexLocal();
2705 
2706  if (pDofMap == pDofMap2) {
2707  index_type iStartLocal = std::min(iGetStartIndexLocal(), iStartFunc);
2708  index_type iEndLocal = std::max(iGetEndIndexLocal(), iEndFunc);
2709 
2710  ad.ResizePreserve(pDofMap, iStartLocal, iEndLocal, MapVector<N_SIZE>::LOCAL);
2711 
2712  if (df_du == 1.) {
2713  // Optimize for operator+=() and operator-=()
2714  iStartLocal = iStartFunc;
2715  iEndLocal = iEndFunc;
2716  }
2717 
2718  ApplyDerivativeHelper<bVectorize>::ApplyBinaryFunction1(ad, f, iStartLocal, iEndLocal, df_du, df_dv);
2719  } else {
2720  index_type iFirstLocal = std::numeric_limits<index_type>::max();
2721  index_type iLastLocal = 0;
2722 
2723  for (index_type i = iStartFunc; i < iEndFunc; ++i) {
2724  if (f.dGetDerivativeLocal(i) == 0.) {
2725  // FIXME: In case of complex expressions, this extra function call might be expensive
2726 
2727  // FIXME: Checking for zero could cause problems with the KLU linear solver
2728  // because the matrix structure could change.
2729  // But we have to omit zeros here, otherwise we could allocate
2730  // too much local degrees of freedom and cause a buffer overflow
2731  // in case of stack based Gradients.
2732  continue;
2733  }
2734 
2735  const index_type iGlobal = pDofMap2->iGetGlobalDof(i);
2736  const index_type iLocal = pDofMap->AllocateLocalDof(iGlobal);
2737 
2738  if (iLocal < iFirstLocal) {
2739  iFirstLocal = iLocal;
2740  }
2741 
2742  if (iLocal > iLastLocal) {
2743  iLastLocal = iLocal;
2744  }
2745  }
2746 
2747  ++iLastLocal;
2748 
2749  if (iLastLocal <= iFirstLocal) {
2750  // empty vector
2751  iFirstLocal = std::numeric_limits<index_type>::max();
2752  iLastLocal = 0;
2753  }
2754 
2755  GRADIENT_TRACE("iFirstLocal=" << iFirstLocal << std::endl);
2756  GRADIENT_TRACE("iLastLocal=" << iLastLocal << std::endl);
2757  GRADIENT_TRACE("*pDofMap=" << *pDofMap << std::endl);
2758 
2759  ad.ResizePreserve(pDofMap,
2760  std::min(ad.iGetStartIndexLocal(), iFirstLocal),
2761  std::max(ad.iGetEndIndexLocal(), iLastLocal),
2763 
2764  GRADIENT_TRACE("*pDofMap2=" << *pDofMap2 << std::endl);
2765  GRADIENT_TRACE("*pDofMap=" << *pDofMap << std::endl);
2766  GRADIENT_TRACE("ad=" << ad.iGetStartIndexLocal() << ":" << ad.iGetEndIndexLocal() << std::endl);
2767 
2768  if (df_du == 1.) {
2769  // optimized loop for operator+= and operator-=
2770  for (index_type i = iStartFunc; i < iEndFunc; ++i) {
2771  const scalar_deriv_type vd = f.dGetDerivativeLocal(i);
2772 
2773  if (vd == 0.) {
2774  // no effect for the current index
2775  continue;
2776  }
2777 
2778  const index_type iGlobal = pDofMap2->iGetGlobalDof(i);
2779  const index_type iLocal = pDofMap->iGetLocalIndex(iGlobal);
2780 
2781  GRADIENT_ASSERT(iLocal != LocalDofMap::INVALID_INDEX); // because vd != 0
2782 
2783  const scalar_deriv_type ud = ad.dGetLocalVector(iLocal);
2784 
2785  GRADIENT_TRACE("i=" << i << " iLocal=" << iLocal << " ud=" << ud << " vd=" << vd << std::endl);
2786 
2787  ad.SetLocalVector(iLocal, ud + df_dv * vd);
2788  }
2789  } else {
2790  // generic loop for operator*= and operator/=
2791  for (index_type i = ad.iGetStartIndexLocal(); i < ad.iGetEndIndexLocal(); ++i) {
2792  const scalar_deriv_type ud = ad.dGetLocalVector(i);
2793  const index_type iGlobal = pDofMap->iGetGlobalDof(i);
2794  const index_type iLocal2 = pDofMap2->iGetLocalIndex(iGlobal);
2795  scalar_deriv_type vd;
2796 
2797  if (iLocal2 == LocalDofMap::INVALID_INDEX) {
2798  // Note: It can happen that there is no
2799  // corresponding entry in pDofMap2.
2800  // However it should be safe to ignore such entries.
2801  vd = 0.;
2802  } else {
2803  vd = f.dGetDerivativeLocal(iLocal2);
2804  }
2805 
2806  GRADIENT_TRACE("i=" << i << " iLocal2=" << iLocal2 << " ud=" << ud << " vd=" << vd << std::endl);
2807 
2808  ad.SetLocalVector(i, df_du * ud + df_dv * vd);
2809  }
2810  }
2811  }
2812  }
2813 
2814  template <typename BinFunc, typename Expression>
2816  typedef typename MaxSizeCheck<iMaxDerivatives >= Expression::iMaxDerivatives>::CheckType check_iMaxDerivatives;
2817 
2818  *this = Gradient(GradientExpression<BinaryExpr<BinFunc, DirectExpr<Gradient>, Expression> >(*this, f));
2819  }
2820 
2821 private:
2824 };
2825 
2826 // helper functions needed for templates (e.g. Matrix<T>, Vector<T>)
2827 inline void Copy(scalar_func_type& d1, const scalar_func_type& d2, LocalDofMap*) {
2828  d1 = d2;
2829 }
2830 
2831 template <index_type N_SIZE1, index_type N_SIZE2>
2832 inline void Copy(Gradient<N_SIZE1>& g1, const Gradient<N_SIZE2>& g2, LocalDofMap* pDofMap) {
2833  g1.Copy(g2, pDofMap);
2834 }
2835 
2836 inline void Reset(scalar_func_type& d) {
2837  d = 0.;
2838 }
2839 
2840 template <index_type N_SIZE>
2841 inline void Reset(Gradient<N_SIZE>& g) {
2842  g.Reset();
2843 }
2844 
2846  return d;
2847 }
2848 
2849 template <index_type N_SIZE>
2851  return g.dGetValue();
2852 }
2853 
2854 template <index_type N_SIZE>
2855 inline void Convert(scalar_func_type& d, const Gradient<N_SIZE>& g) {
2856  // Attention: This operation must be explicit!
2857  d = g.dGetValue();
2858 }
2859 
2860 template <typename T1, typename T2>
2861 inline void Convert(T1& g1, const T2& g2) {
2862  g1 = g2;
2863 }
2864 
2865 template <index_type N_SIZE>
2867 {
2869 }
2870 
2872 {
2873  return d;
2874 }
2875 
2876 template <index_type N_SIZE>
2877 inline std::ostream& operator<<(std::ostream& os, const Gradient<N_SIZE>& f) {
2878  os << std::setw(12) << f.dGetValue();
2879  const LocalDofMap* pDofMap = f.pGetDofMap();
2880 
2881  os << " [" << f.iGetStartIndexLocal() << "->" << f.iGetEndIndexLocal() << "]";
2882 
2883  if (pDofMap) {
2884  os << " (";
2885 
2886  for (LocalDofMap::GlobalIterator i = pDofMap->BeginGlobal(); i != pDofMap->EndGlobal(); ++i) {
2887  const index_type iGlobal = i->first;
2888  const index_type iLocal = i->second;
2889  os << std::setw(3) << iGlobal << ":" << std::setw(12) << f.dGetDerivativeLocal(iLocal) << " ";
2890  }
2891 
2892  os << ")";
2893 
2894  }
2895 
2896  return os;
2897 }
2898 
2899 #define GRADIENT_DEFINE_BINARY_FUNCTION(ExpressionName, FunctionName, FunctionClass) \
2900  template <typename LhsExpr, typename RhsExpr> \
2901  inline GradientExpression<ExpressionName<FunctionClass, LhsExpr, RhsExpr> > \
2902  FunctionName(const GradientExpression<LhsExpr>& u, const GradientExpression<RhsExpr>& v) { \
2903  return GradientExpression<ExpressionName<FunctionClass, LhsExpr, RhsExpr> >(u, v); \
2904  } \
2905  \
2906  template <index_type N_SIZE, typename LhsExpr> \
2907  inline GradientExpression<ExpressionName<FunctionClass, LhsExpr, DirectExpr<Gradient<N_SIZE> > > > \
2908  FunctionName(const GradientExpression<LhsExpr>& u, const Gradient<N_SIZE>& v) { \
2909  return GradientExpression<ExpressionName<FunctionClass, LhsExpr, DirectExpr<Gradient<N_SIZE> > > >(u, v); \
2910  } \
2911  \
2912  template <typename LhsExpr> \
2913  inline GradientExpression<ExpressionName<FunctionClass, LhsExpr, ConstExpr<typename LhsExpr::GradientType> > > \
2914  FunctionName(const GradientExpression<LhsExpr>& u, scalar_func_type v) { \
2915  return GradientExpression<ExpressionName<FunctionClass, LhsExpr, ConstExpr<typename LhsExpr::GradientType> > >(u, v); \
2916  } \
2917  template <index_type N_SIZE, typename RhsExpr> \
2918  inline GradientExpression<ExpressionName<FunctionClass, DirectExpr<Gradient<N_SIZE> >, RhsExpr> > \
2919  FunctionName(const Gradient<N_SIZE>& u, const GradientExpression<RhsExpr>& v) { \
2920  return GradientExpression<ExpressionName<FunctionClass, DirectExpr<Gradient<N_SIZE> >, RhsExpr> >(u, v); \
2921  } \
2922  template <typename RhsExpr> \
2923  inline GradientExpression<ExpressionName<FunctionClass, ConstExpr<typename RhsExpr::GradientType>, RhsExpr> > \
2924  FunctionName(scalar_func_type u, const GradientExpression<RhsExpr>& v) { \
2925  return GradientExpression<ExpressionName<FunctionClass, ConstExpr<typename RhsExpr::GradientType>, RhsExpr> >(u, v); \
2926  } \
2927  template <index_type N_SIZE> \
2928  inline GradientExpression<ExpressionName<FunctionClass, DirectExpr<Gradient<N_SIZE> >, DirectExpr<Gradient<N_SIZE> > > > \
2929  FunctionName(const Gradient<N_SIZE>& u, const Gradient<N_SIZE>& v) { \
2930  return GradientExpression<ExpressionName<FunctionClass, DirectExpr<Gradient<N_SIZE> >, DirectExpr<Gradient<N_SIZE> > > > (u, v); \
2931  } \
2932  template <index_type N_SIZE> \
2933  inline GradientExpression<ExpressionName<FunctionClass, DirectExpr<Gradient<N_SIZE> >, ConstExpr<Gradient<N_SIZE> > > > \
2934  FunctionName(const Gradient<N_SIZE>& u, scalar_func_type v) { \
2935  return GradientExpression<ExpressionName<FunctionClass, DirectExpr<Gradient<N_SIZE> >, ConstExpr<Gradient<N_SIZE> > > >(u, v); \
2936  } \
2937  \
2938  template <index_type N_SIZE> \
2939  inline GradientExpression<ExpressionName<FunctionClass, ConstExpr<Gradient<N_SIZE> >, DirectExpr<Gradient<N_SIZE> > > > \
2940  FunctionName(scalar_func_type u, const Gradient<N_SIZE>& v) { \
2941  return GradientExpression<ExpressionName<FunctionClass, ConstExpr<Gradient<N_SIZE> >, DirectExpr<Gradient<N_SIZE> > > >(u, v); \
2942  }
2943 
2944 #define GRADIENT_DEFINE_UNARY_FUNCTION(FunctionName, FunctionClass) \
2945  template <typename Expr> \
2946  inline GradientExpression<UnaryExpr<FunctionClass, Expr> > \
2947  FunctionName(const GradientExpression<Expr>& u) { \
2948  return GradientExpression<UnaryExpr<FunctionClass, Expr> >(u); \
2949  } \
2950  \
2951  template <index_type N_SIZE> \
2952  inline GradientExpression<UnaryExpr<FunctionClass, DirectExpr<Gradient<N_SIZE> > > > \
2953  FunctionName(const Gradient<N_SIZE>& u) { \
2954  return GradientExpression<UnaryExpr<FunctionClass, DirectExpr<Gradient<N_SIZE> > > >(u); \
2955  }
2956 
2964 
2971 
2986 
2987 #if HAVE_ASINH
2988 GRADIENT_DEFINE_UNARY_FUNCTION(asinh, FuncAsinh)
2989 #endif
2990 
2991 #if HAVE_ACOSH
2992 GRADIENT_DEFINE_UNARY_FUNCTION(acosh, FuncAcosh)
2993 #endif
2994 
2995 #if HAVE_ATANH
2996 GRADIENT_DEFINE_UNARY_FUNCTION(atanh, FuncAtanh)
2997 #endif
2998 
2999 #undef GRADIENT_DEFINE_BINARY_FUNCTION
3000 #undef GRADIENT_DEFINE_UNARY_FUNCTION
3001 
3002 }
3003 #endif
GradientExpression< UnaryExpr< FuncTanh, Expr > > tanh(const GradientExpression< Expr > &u)
Definition: gradient.h:2982
void SetVectorValue(index_type i, const vector_type &d)
Definition: gradient.h:529
GradientExpression< UnaryExpr< FuncExp, Expr > > exp(const GradientExpression< Expr > &u)
Definition: gradient.h:2975
index_type iGetEndIndexLocalVector() const
Definition: gradient.h:2588
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1914
void SetDerivativeLocal(index_type iLocalDof, scalar_deriv_type dCoef)
Definition: gradient.h:2554
scalar_deriv_type dGetDerivativeGlobal(index_type iGlobalDof) const
Definition: gradient.h:2532
LocalDofMap * pDofMap
Definition: gradient.h:1279
RangeVector< scalar_deriv_type, N_SIZE > RangeVectorType
Definition: gradient.h:1156
void ResizeReset(index_type iStartNew, index_type iEndNew, const scalar_type &dVal)
Definition: gradient.h:714
bool bHaveReferenceTo(const void *p) const
Definition: gradient.h:2630
const scalar_func_type dConst
Definition: gradient.h:1662
void SetValue(scalar_func_type dVal)
Definition: gradient.h:2506
bool dGetValue() const
Definition: gradient.h:1681
bool bIsEqual(const Gradient &g) const
Definition: gradient.h:2604
index_type iGetEndIndex() const
Definition: gradient.h:791
scalar_func_type dGetValue(scalar_func_type d)
Definition: gradient.h:2845
static const bool bVectorize
Definition: gradient.h:1443
Gradient & operator=(const Gradient< N_SIZE2 > &g)
Definition: gradient.h:2390
GradientType::vector_deriv_type vector_deriv_type
Definition: gradient.h:1334
index_type iGetCapacityVector() const
Definition: gradient.h:874
MapVector(LocalDofMap *pMap, index_type iLocal, LocalScope, scalar_func_type dVal)
Definition: gradient.h:1171
index_type iEnd
Definition: gradient.h:641
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1888
void ResizePreserve(index_type iStartNew, index_type iEndNew)
Definition: gradient.h:723
Gradient & operator+=(scalar_func_type d)
Definition: gradient.h:2472
index_type iGetStartIndexLocal() const
Definition: gradient.h:2576
static const bool bVectorize
Definition: gradient.h:1818
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1980
Gradient & operator/=(const GradientExpression< Expression > &f)
Definition: gradient.h:2467
bool bHaveReferenceTo(const void *p) const
Definition: gradient.h:1651
scalar_deriv_type dGetDerivativeLocal(index_type iLocalDof) const
Definition: gradient.h:1620
const RhsExpr oV
Definition: gradient.h:1433
static index_type iGetMaxDerivatives()
Definition: gradient.h:1731
index_type iGetSize() const
Definition: gradient.h:807
void Initialize(index_type iStartNew, index_type iEndNew, const scalar_type &dVal)
Definition: gradient.h:544
GradientExpression< UnaryExpr< FuncAsin, Expr > > asin(const GradientExpression< Expr > &u)
Definition: gradient.h:2983
static scalar_deriv_type df_du(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1824
std::allocator< T >::const_reference const_reference
Definition: gradient.h:156
UnaryExpr(const Expr &u)
Definition: gradient.h:1450
static scalar_deriv_type df_du(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1790
Expression::scalar_func_type scalar_func_type
Definition: gradient.h:1288
index_type Size() const
Definition: gradient.h:1128
static index_type iGetMaxSizeVector()
Definition: gradient.h:500
index_type iGetStartIndexLocalVector() const
Definition: gradient.h:1639
index_type iGetStartIndex() const
Definition: gradient.h:787
MapType oGlobalToLocal
Definition: gradient.h:1135
scalar_type rgArray[iMaxSize]
Definition: gradient.h:637
scalar_deriv_type dGetGlobalVector(index_type iGlobalDof) const
Definition: gradient.h:1226
MapVectorType ad
Definition: gradient.h:2823
scalar_func_type a
Definition: gradient.h:2822
GradientType::vector_deriv_type vector_deriv_type
Definition: gradient.h:1448
static const bool bVectorize
Definition: gradient.h:1934
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1901
GradientExpression< BinaryExpr< FuncPow, LhsExpr, RhsExpr > > pow(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
Definition: gradient.h:2961
MapVector(LocalDofMap *pMap, index_type iGlobal, GlobalScope s, scalar_func_type dVal)
Definition: gradient.h:1179
std::allocator< T >::size_type size_type
Definition: gradient.h:151
static const index_type iMaxSizeVector
Definition: gradient.h:384
static index_type iGetMaxDerivatives()
Definition: gradient.h:1506
void Reserve(index_type iMaxSizeNew)
Definition: gradient.h:487
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
RangeVector & operator=(const RangeVector &v)
Definition: gradient.h:691
static const index_type DYNAMIC_SIZE
Definition: gradient.h:141
static scalar_deriv_type df_dv(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1862
GradientExpression(const Expression &u)
Definition: gradient.h:1292
GradientExpression< UnaryExpr< FuncSin, Expr > > sin(const GradientExpression< Expr > &u)
Definition: gradient.h:2977
index_type iGetEndIndexLocal() const
Definition: gradient.h:1377
void SetDerivativeLocalVector(index_type iLocalVecDof, vector_deriv_type dVec)
Definition: gradient.h:2560
static const bool bVectorize
Definition: gradient.h:2098
static const index_type iMaxDerivatives
Definition: gradient.h:1603
static void ApplyBinaryFunction(GradientType &g, const GradientExpression< Expression > &f, const BinFunc &bfunc)
Definition: gradient.h:2230
static const index_type iVectorSize
Definition: gradient.h:380
static const index_type iDimension
Definition: gradient.h:1287
void ApplyBinaryFunctionWithAlias(const GradientExpression< Expression > &f, const BinFunc &)
Definition: gradient.h:2815
index_type iGetMaxSizeVector() const
Definition: gradient.h:1264
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1989
Gradient GradientType
Definition: gradient.h:2240
GradientType::scalar_deriv_type scalar_deriv_type
Definition: gradient.h:1674
static const index_type iDimension
Definition: gradient.h:1606
static const index_type iDimension
Definition: gradient.h:2241
static const bool bAlias
Definition: gradient.h:1668
index_type iGetEndIndexLocal() const
Definition: gradient.h:1271
static const bool bVectorize
Definition: gradient.h:1921
RangeVectorBase< T, 0 >::vector_type vector_type
Definition: gradient.h:649
static const index_type iMaxDerivatives
Definition: gradient.h:1327
GradientAllocator< U > other
Definition: gradient.h:161
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1910
const RangeVectorType & GetLocalVector() const
Definition: gradient.h:1267
void Reserve(index_type iMaxSize)
Definition: gradient.h:762
GradientType::vector_deriv_type vector_deriv_type
Definition: gradient.h:1546
static const bool bAlias
Definition: gradient.h:1539
static void deallocate_aligned(pointer p, size_type n)
Definition: gradient.h:239
GradientType::scalar_func_type scalar_func_type
Definition: gradient.h:1332
void SetGlobalVector(index_type iGlobalDof, scalar_deriv_type dValue)
Definition: gradient.h:1240
index_type iGetMaxDerivatives() const
Definition: gradient.h:1589
static const bool bVectorize
Definition: gradient.h:1947
void Compute() const
Definition: gradient.h:1735
static void ApplyExpression(GradientType &g, const GradientExpression< Expression > &f)
Definition: gradient.h:2212
index_type iGetStartIndexLocalVector() const
Definition: gradient.h:2584
Gradient & operator++()
Definition: gradient.h:2426
static const index_type iDimension
Definition: gradient.h:1543
static scalar_deriv_type df_du(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1858
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:2003
static void ApplyDerivative(MapVectorType &ad, const GradientExpression< Expression > &f)
Definition: gradient.h:2156
bool bHaveReferenceTo(const void *p) const
Definition: gradient.h:1401
void SetVectorValue(index_type i, const vector_type &d)
Definition: gradient.h:840
index_type iGetSizeVector() const
Definition: gradient.h:1262
grad::scalar_func_type scalar_func_type
Definition: gradient.h:2243
int bool
Definition: mbdyn.h:74
#define GRADIENT_VECTOR_REGISTER_SIZE
Definition: gradient.h:144
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1871
scalar_deriv_type df_du
Definition: gradient.h:1435
index_type iGetSizeVector() const
Definition: gradient.h:803
const Expr oU
Definition: gradient.h:1531
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:2007
void Copy(const Gradient< N_SIZE2 > &g, LocalDofMap *pDofMap)
Definition: gradient.h:2289
Gradient operator--(int)
Definition: gradient.h:2442
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1967
index_type iGetEndIndexLocalVector() const
Definition: gradient.h:1494
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1963
VectorType oLocalToGlobal
Definition: gradient.h:1134
#define GRADIENT_DEFINE_BINARY_FUNCTION(ExpressionName, FunctionName, FunctionClass)
Definition: gradient.h:2899
index_type iGetEndIndexLocalVector() const
Definition: gradient.h:1385
static const bool bVectorize
Definition: gradient.h:1852
index_type iGetStartIndexLocalVector() const
Definition: gradient.h:1490
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:2016
static const MapVector Zero
Definition: gradient.h:1276
static const index_type iMaxDerivatives
Definition: gradient.h:1669
bool bHaveReferenceTo(const void *p) const
Definition: gradient.h:1585
static const index_type iMaxDerivatives
Definition: gradient.h:1442
vector_type GetVectorValue(index_type i) const
Definition: gradient.h:831
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1940
vector_type dGetLocalVectorVector(index_type i) const
Definition: gradient.h:1266
scalar_deriv_type dGetDerivativeLocal(index_type iLocalDof) const
Definition: gradient.h:1695
Expression::GradientType GradientType
Definition: gradient.h:1286
void ReserveMem(index_type iStartNew, index_type iEndNew, MemFlags eFlags=RESIZE)
Definition: gradient.h:937
const vector_type * endVec() const
Definition: gradient.h:933
GradientType::scalar_deriv_type scalar_deriv_type
Definition: gradient.h:1545
scalar_func_type dGetValue() const
Definition: gradient.h:1616
integer index_type
Definition: gradient.h:104
MapVector(LocalDofMap *pMap, index_type iStartGlobal, index_type iEndGlobal, GlobalScope s, scalar_func_type dVal)
Definition: gradient.h:1175
static const bool bVectorize
Definition: gradient.h:1328
void ResizeReset(LocalDofMap *pMap, index_type iStartLocal, index_type iEndLocal, LocalScope, scalar_func_type dVal)
Definition: gradient.h:1183
index_type iGetEndIndexLocalVector() const
Definition: gradient.h:1577
static const bool bVectorize
Definition: gradient.h:1835
scalar_deriv_type dGetDerivativeLocal(index_type iLocalDof) const
Definition: gradient.h:1464
LocalDofMap(index_type iMaxSize=0)
Definition: gradient.h:1053
RangeVector(index_type iStartNew, index_type iEndNew, const scalar_type &dVal)
Definition: gradient.h:418
index_type iEndVec
Definition: gradient.h:641
static const bool bVectorize
Definition: gradient.h:2001
static const bool bVectorize
Definition: gradient.h:2085
index_type iGetEndIndexLocal() const
Definition: gradient.h:1569
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:2087
BoolExpr(const LhsExpr &u, const RhsExpr &v)
Definition: gradient.h:1677
void SetValue(index_type i, const scalar_type &d)
Definition: gradient.h:823
GradientType::scalar_func_type scalar_func_type
Definition: gradient.h:1544
VectorType::const_iterator LocalIterator
Definition: gradient.h:1044
Gradient(const GradientExpression< Expression > &f)
Definition: gradient.h:2276
index_type iGetStartIndexLocal() const
Definition: gradient.h:1270
RangeVectorBase< T, N_SIZE >::vector_type vector_type
Definition: gradient.h:382
static bool bUseDynamicMem()
Definition: gradient.h:538
Gradient & operator/=(scalar_func_type d)
Definition: gradient.h:2492
RangeVector(index_type iStart, index_type iEnd, const scalar_type &dVal)
Definition: gradient.h:677
static const bool bVectorize
Definition: gradient.h:1895
static scalar_deriv_type df_dv(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1811
index_type iGetEndIndexLocalVector() const
Definition: gradient.h:1715
index_type iGetEndIndexVector() const
Definition: gradient.h:799
Gradient & operator*=(const Gradient &g)
Definition: gradient.h:2416
scalar_deriv_type dGetDerivativeLocal(index_type iLocalDof) const
Definition: gradient.h:1557
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1936
static const bool bVectorize
Definition: gradient.h:1869
static const bool bVectorize
Definition: gradient.h:1801
GradientExpression< UnaryExpr< FuncFabs, Expr > > fabs(const GradientExpression< Expr > &u)
Definition: gradient.h:2973
Expression::vector_deriv_type vector_deriv_type
Definition: gradient.h:1290
index_type iGetMaxDerivativesVector() const
Definition: gradient.h:2600
T::GradientType GradientType
Definition: gradient.h:1540
index_type iGetEndIndexLocal() const
Definition: gradient.h:1486
GradientType::scalar_deriv_type scalar_deriv_type
Definition: gradient.h:1333
void ResizeReset(index_type iStartNew, index_type iEndNew, const T &dVal)
Definition: gradient.h:451
Gradient(const Gradient &g)
Definition: gradient.h:2256
void deallocate(pointer p, size_type n)
Definition: gradient.h:190
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:2020
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:2029
#define GRADIENT_TRACE(expr)
Definition: gradient.h:85
static scalar_deriv_type df_dv(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1845
static scalar_deriv_type df_du(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1773
GradientType::vector_deriv_type vector_deriv_type
Definition: gradient.h:1609
Gradient & operator+=(const Gradient &g)
Definition: gradient.h:2406
static index_type iGetMaxSizeVector()
Definition: gradient.h:812
pointer allocate(size_type n, const void *p=0)
Definition: gradient.h:176
LocalDofMap * pGetDofMap() const
Definition: gradient.h:1647
static scalar_func_type f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1837
static index_type iRoundEndIndexVector(index_type iEnd)
Definition: gradient.h:368
index_type iGetSize() const
Definition: gradient.h:1261
Gradient(scalar_func_type a=0., LocalDofMap *pDofMap=0)
Definition: gradient.h:2247
index_type iGetStartIndexLocal() const
Definition: gradient.h:1565
static const index_type iMaxDerivatives
Definition: gradient.h:2242
index_type iGetStartIndexVector() const
Definition: gradient.h:497
index_type iGetStartIndexLocalVector() const
Definition: gradient.h:1573
Gradient & operator*=(const GradientExpression< Expression > &f)
Definition: gradient.h:2461
Expr::GradientType GradientType
Definition: gradient.h:1444
static index_type iGetMaxSize()
Definition: gradient.h:496
static index_type iRoundStartIndexVector(index_type iStart)
Definition: gradient.h:364
void func(const T &u, const T &v, const T &w, doublereal e, T &f)
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1927
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1953
std::allocator< T >::reference reference
Definition: gradient.h:155
void SetDerivativeGlobal(index_type iGlobalDof, scalar_deriv_type dCoef)
Definition: gradient.h:2566
GradientExpression< UnaryExpr< FuncCosh, Expr > > cosh(const GradientExpression< Expr > &u)
Definition: gradient.h:2981
GlobalIterator BeginGlobal() const
Definition: gradient.h:1126
GradientExpression< UnaryExpr< FuncSinh, Expr > > sinh(const GradientExpression< Expr > &u)
Definition: gradient.h:2980
LocalDofMap * pGetDofMap() const
Definition: gradient.h:1498
bool bHaveReferenceTo(const void *p) const
Definition: gradient.h:1502
MapVectorType::scalar_type scalar_deriv_type
Definition: gradient.h:2244
DirectExpr(const T &g)
Definition: gradient.h:1548
VectorType::size_type size_type
Definition: gradient.h:1043
void DerivativeResizeReset(LocalDofMap *pMap, index_type iGlobal, MapVectorBase::GlobalScope s, scalar_deriv_type dVal)
Definition: gradient.h:2546
const MapVector< N_SIZE > & GetDerivativeLocal() const
Definition: gradient.h:2528
Gradient< iDimension > GradientType
Definition: gradient.h:1320
index_type iGetEndIndexVector() const
Definition: gradient.h:498
index_type iGetSizeVector() const
Definition: gradient.h:499
enum FunctionCall GetLastCall() const
Definition: gradient.h:1122
static scalar_func_type f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1786
index_type iGetLocalSize() const
Definition: gradient.h:2592
LocalIterator EndLocal() const
Definition: gradient.h:1125
static const index_type iDimension
Definition: gradient.h:1445
scalar_type GetValue(index_type i) const
Definition: gradient.h:814
GradientType::scalar_func_type scalar_func_type
Definition: gradient.h:1446
index_type iGetStartIndexLocal() const
Definition: gradient.h:1482
LocalDofMap * pGetDofMap() const
Definition: gradient.h:2626
const RhsExpr oV
Definition: gradient.h:1745
RangeVectorType::scalar_type scalar_type
Definition: gradient.h:1158
scalar_type * begin()
Definition: gradient.h:905
LocalDofMap * pGetDofMap() const
Definition: gradient.h:1719
MapVector(LocalDofMap *pMap=0, index_type iStartLocal=0, index_type iEndLocal=0, LocalScope=LOCAL, scalar_func_type dVal=0.)
Definition: gradient.h:1167
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1884
static void ApplyDerivative(MapVectorType &ad, const GradientExpression< Expression > &f)
Definition: gradient.h:2180
static const bool bVectorize
Definition: gradient.h:1882
GradientType::scalar_deriv_type scalar_deriv_type
Definition: gradient.h:1608
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1923
vector_type * beginVec()
Definition: gradient.h:921
void SetLocalVector(index_type i, scalar_type dValue)
Definition: gradient.h:1268
GradientExpression< UnaryExpr< FuncLog, Expr > > log(const GradientExpression< Expr > &u)
Definition: gradient.h:2976
void CopyData(const RangeVector< T2, N_SIZE2 > &v)
Definition: gradient.h:575
ConstExpr(scalar_func_type a)
Definition: gradient.h:1611
void ApplyBinaryFunction(const GradientExpression< Expression > &f)
Definition: gradient.h:2672
static void ApplyExpression(GradientType &g, const GradientExpression< Expression > &f)
Definition: gradient.h:2225
scalar_func_type f
Definition: gradient.h:1532
enum FunctionCall eLastCall
Definition: gradient.h:1136
void Reset(scalar_func_type &d)
Definition: gradient.h:2836
static const bool bVectorize
Definition: gradient.h:2014
static scalar_func_type f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1769
Definition: mbdyn.h:76
void Compute() const
Definition: gradient.h:1409
static const char * dof[]
Definition: drvdisp.cc:195
Gradient & operator=(const Gradient &g)
Definition: gradient.h:2397
void ApplyNoAlias(const GradientExpression< Expression > &f)
Definition: gradient.h:2639
GradientSizeHelper< LhsExpr::iDimension, RhsExpr::iDimension >::GradientType GradientType
Definition: gradient.h:1330
GradientAllocator(const GradientAllocator< U > &a)
Definition: gradient.h:171
void ApplyDerivative(const GradientExpression< Expression > &f)
Definition: gradient.h:2661
#define GRADIENT_DEFINE_UNARY_FUNCTION(FunctionName, FunctionClass)
Definition: gradient.h:2944
doublereal copysign(doublereal x, doublereal y)
Definition: gradient.h:97
scalar_func_type dGetValue() const
Definition: gradient.h:1457
RangeVector & operator=(const RangeVector &v)
Definition: gradient.h:426
FunctionCall
Definition: gradient.h:1018
static void ApplyBinaryFunction1(MapVectorType &ad, const GradientExpression< Expression > &f, const index_type iStartLocal, const index_type iEndLocal, const typename GradientExpression< Expression >::scalar_deriv_type df_du, const typename GradientExpression< Expression >::scalar_deriv_type df_dv)
Definition: gradient.h:2187
void SetValuePreserve(scalar_func_type dVal)
Definition: gradient.h:2511
void Reserve(index_type iSize)
Definition: gradient.h:1222
void Compute() const
Definition: gradient.h:1510
static void ApplyBinaryFunction1(MapVectorType &ad, const GradientExpression< Expression > &f, const index_type iStartLocal, const index_type iEndLocal, const scalar_deriv_type df_du, const scalar_deriv_type df_dv)
Definition: gradient.h:2163
static T * allocate_aligned(size_type alignment, size_t n, size_type extra_bytes=0u)
Definition: gradient.h:198
GradientSizeHelper< LhsExpr::iDimension, RhsExpr::iDimension >::GradientType GradientType
Definition: gradient.h:1671
void ApplyBinaryFunctionNoAlias(const GradientExpression< Expression > &f, const BinFunc &)
Definition: gradient.h:2677
index_type iStart
Definition: gradient.h:641
LocalIterator BeginLocal() const
Definition: gradient.h:1124
LocalDofMap * pGetDofMap() const
Definition: gradient.h:1274
index_type iGetGlobalDof(index_type iLocal) const
Definition: gradient.h:1061
index_type iGetEndIndexLocal() const
Definition: gradient.h:1707
std::allocator< T >::pointer pointer
Definition: gradient.h:150
static scalar_deriv_type df_du(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1756
static const bool bVectorize
Definition: gradient.h:2116
Definition: mbdyn.h:77
void DerivativeResizeReset(LocalDofMap *pMap, index_type iStartLocal, index_type iEndLocal, MapVectorBase::LocalScope s, scalar_deriv_type dVal)
Definition: gradient.h:2542
void ApplyWithAlias(const GradientExpression< Expression > &f)
Definition: gradient.h:2653
std::allocator< T >::const_pointer const_pointer
Definition: gradient.h:154
void DerivativeResizeReset(LocalDofMap *pMap, index_type iLocal, MapVectorBase::LocalScope s, scalar_deriv_type dVal)
Definition: gradient.h:2550
const scalar_type * begin() const
Definition: gradient.h:913
const scalar_type * end() const
Definition: gradient.h:917
static const index_type iDimension
Definition: gradient.h:1319
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1976
void Copy(scalar_func_type &d1, const scalar_func_type &d2, LocalDofMap *)
Definition: gradient.h:2827
void Reset(enum FunctionCall func=UNKNOWN_FUNC)
Definition: gradient.h:1110
vector_deriv_type dGetDerivativeLocalVector(index_type iLocalVecDof) const
Definition: gradient.h:1561
RangeVectorBase< T, N_SIZE >::scalar_type scalar_type
Definition: gradient.h:381
scalar_func_type dGetValue() const
Definition: gradient.h:1553
LhsExpr LhsExprType
Definition: gradient.h:1336
Gradient(scalar_func_type a, const MapVectorType &da)
Definition: gradient.h:2251
std::map< index_type, index_type, std::less< index_type >, GradientAllocator< std::pair< index_type, index_type > > > MapType
Definition: gradient.h:1049
index_type iGetStartIndexLocalVector() const
Definition: gradient.h:1381
index_type iGetStartIndexLocal() const
Definition: gradient.h:1628
Gradient & operator*=(scalar_func_type d)
Definition: gradient.h:2482
Gradient & operator+=(const GradientExpression< Expression > &f)
Definition: gradient.h:2449
Gradient & operator/=(const Gradient &g)
Definition: gradient.h:2421
void SetLocalVectorVector(index_type i, vector_type dVector)
Definition: gradient.h:1269
GradientType::scalar_deriv_type scalar_deriv_type
Definition: gradient.h:1447
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1949
void Initialize(index_type iStart, index_type iEnd, const scalar_type &dVal)
Definition: gradient.h:890
RhsExpr RhsExprType
Definition: gradient.h:1337
index_type iGetEndIndexLocalVector() const
Definition: gradient.h:1643
static const index_type iMaxDerivatives
Definition: gradient.h:1313
static bool bUseDynamicMem()
Definition: gradient.h:848
static const index_type iDimension
Definition: gradient.h:1672
const vector_type * beginVec() const
Definition: gradient.h:929
static scalar_func_type f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1820
void CopyData(const RangeVector &v)
Definition: gradient.h:581
GradientExpression< UnaryExpr< FuncSqrt, Expr > > sqrt(const GradientExpression< Expr > &u)
Definition: gradient.h:2974
RangeVector(const RangeVector &v)
Definition: gradient.h:401
vector_deriv_type dGetDerivativeLocalVector(index_type iLocalVecDof) const
Definition: gradient.h:1624
Gradient & operator-=(const Gradient &g)
Definition: gradient.h:2411
MapVector(const MapVector< N_SIZE2 > &v)
Definition: gradient.h:1162
const LhsExpr oU
Definition: gradient.h:1744
void Copy(const RangeVector< T2, N_SIZE2 > &v)
Definition: gradient.h:559
std::vector< index_type, GradientAllocator< index_type > > VectorType
Definition: gradient.h:1042
vector_deriv_type dGetDerivativeLocalVector(index_type iLocalVecDof) const
Definition: gradient.h:1473
RangeVector(const RangeVector< T2, N_SIZE2 > &v)
Definition: gradient.h:410
void DerivativeResizeReset(LocalDofMap *pMap, index_type iStartGlobal, index_type iEndGlobal, MapVectorBase::GlobalScope s, scalar_deriv_type dVal)
Definition: gradient.h:2538
static scalar_deriv_type df_dv(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1777
vector_type rgArrayVec[iMaxSizeVector]
Definition: gradient.h:638
doublereal scalar_deriv_type
Definition: gradient.h:347
static bool f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:2109
std::allocator< T >::value_type value_type
Definition: gradient.h:157
GradientAllocator(const GradientAllocator &a)
Definition: gradient.h:166
static index_type iGetMaxSize()
Definition: gradient.h:811
index_type iGetEndIndexLocalVector() const
Definition: gradient.h:1273
static const bool bAlias
Definition: gradient.h:1326
static bool f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:2136
index_type iGetGlobalDof(index_type iLocalDof) const
Definition: gradient.h:2572
scalar_func_type f
Definition: gradient.h:1434
bool bIsEqual(const GradientExpression< Expression > &g) const
Definition: gradient.h:2622
static const index_type iMaxSize
Definition: gradient.h:385
static const bool bVectorize
Definition: gradient.h:1767
scalar_deriv_type df_du
Definition: gradient.h:1533
static const bool bVectorize
Definition: gradient.h:2134
vector_type * endVec()
Definition: gradient.h:925
Gradient(const Gradient< N_SIZE2 > &g)
Definition: gradient.h:2262
static index_type iGetMaxDerivatives()
Definition: gradient.h:1405
index_type iGetStartIndexVector() const
Definition: gradient.h:795
index_type iGetMaxSize() const
Definition: gradient.h:1263
vector_deriv_type dGetDerivativeLocalVector(index_type iLocalVecDof) const
Definition: gradient.h:1362
static bool f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:2118
scalar_deriv_type dGetDerivativeLocal(index_type iLocalDof) const
Definition: gradient.h:1351
GlobalIterator EndGlobal() const
Definition: gradient.h:1127
T EvalDeriv(T du_dX, T dv_dX) const
Definition: gradient.h:1427
void ResizeReset(LocalDofMap *pMap, index_type iStartGlobal, index_type iEndGlobal, GlobalScope, scalar_func_type dVal)
Definition: gradient.h:1193
GradientExpression< DirectExpr< Gradient< N_SIZE >, true > > Alias(const Gradient< N_SIZE > &g)
Definition: gradient.h:2866
index_type iGetEndIndexLocal() const
Definition: gradient.h:1635
MapType::const_iterator GlobalIterator
Definition: gradient.h:1050
static const bool bVectorize
Definition: gradient.h:1784
index_type iGetStartIndex() const
Definition: gradient.h:493
static scalar_func_type f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1803
LocalDofMap * pGetDofMap() const
Definition: gradient.h:1389
index_type iStartVec
Definition: gradient.h:641
static const bool bVectorize
Definition: gradient.h:1908
Gradient & operator--()
Definition: gradient.h:2431
void Reset()
Definition: gradient.h:2367
GradientType::scalar_func_type scalar_func_type
Definition: gradient.h:1673
static const bool bVectorize
Definition: gradient.h:1961
static scalar_deriv_type df_du(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1807
vector_type GetVectorValue(index_type i) const
Definition: gradient.h:520
Gradient & operator=(const GradientExpression< Expression > &f)
Definition: gradient.h:2383
static const Data sNullData
Definition: gradient.h:1012
Expression::scalar_deriv_type scalar_deriv_type
Definition: gradient.h:1289
Gradient & operator-=(scalar_func_type d)
Definition: gradient.h:2477
static const bool bAlias
Definition: gradient.h:1441
GradientExpression(const Expr &u)
Definition: gradient.h:1298
index_type AllocateLocalDof(index_type iGlobal)
Definition: gradient.h:1091
RangeVector & operator=(const RangeVector< T2, N_SIZE2 > &v)
Definition: gradient.h:439
static index_type iGetMaxDerivatives()
Definition: gradient.h:1655
index_type iGetStartIndexLocalVector() const
Definition: gradient.h:1272
static const bool bAlias
Definition: gradient.h:1602
Gradient & operator=(scalar_func_type d)
Definition: gradient.h:2376
std::ostream & operator<<(std::ostream &os, const LocalDofMap &dof)
Definition: gradient.h:1139
RangeVector & operator=(const RangeVector< T2, N_SIZE2 > &v)
Definition: gradient.h:703
GradientExpression< UnaryExpr< FuncAcos, Expr > > acos(const GradientExpression< Expr > &u)
Definition: gradient.h:2984
static const bool bVectorize
Definition: gradient.h:1542
bool bHaveReferenceTo(const void *p) const
Definition: gradient.h:1739
MapVector< N_SIZE > MapVectorType
Definition: gradient.h:2237
index_type iGetLocalIndex(index_type iGlobal) const
Definition: gradient.h:1074
static const index_type INVALID_INDEX
Definition: gradient.h:1051
void Reserve(index_type iSize)
Definition: gradient.h:2372
RangeVectorBase< scalar_func_type >::vector_type vector_deriv_type
Definition: gradient.h:375
BinaryExpr(const LhsExpr &u, const RhsExpr &v)
Definition: gradient.h:1339
static void ApplyBinaryFunction(GradientType &g, const GradientExpression< Expression > &f, const BinFunc &bfunc)
Definition: gradient.h:2217
scalar_type dGetLocalVector(index_type i) const
Definition: gradient.h:1265
Gradient & operator-=(const GradientExpression< Expression > &f)
Definition: gradient.h:2455
bool bUseDynamicMem() const
Definition: gradient.h:1275
index_type iGetCapacity() const
Definition: gradient.h:869
GradientType::vector_deriv_type vector_deriv_type
Definition: gradient.h:1675
index_type iGetSize() const
Definition: gradient.h:495
Gradient operator++(int)
Definition: gradient.h:2436
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:2091
scalar_deriv_type df_dv
Definition: gradient.h:1435
void Copy(const RangeVector< T2, N_SIZE2 > &v)
Definition: gradient.h:880
T EvalDeriv(T du_dX) const
Definition: gradient.h:1525
Gradient(const Gradient< N_SIZE2 > &g, LocalDofMap *pDofMap)
Definition: gradient.h:2268
static scalar_func_type f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1752
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:2033
void Compute() const
Definition: gradient.h:1593
GradientExpression< UnaryExpr< FuncCos, Expr > > cos(const GradientExpression< Expr > &u)
Definition: gradient.h:2978
static bool f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:2127
const LhsExpr oU
Definition: gradient.h:1432
static const index_type iMaxDerivatives
Definition: gradient.h:1541
static scalar_deriv_type df_dv(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1828
GradientExpression(const LhsExpr &u, const RhsExpr &v)
Definition: gradient.h:1304
scalar_func_type dGetValue() const
Definition: gradient.h:2502
static const doublereal a
Definition: hfluid_.h:289
static scalar_deriv_type df_du(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1841
index_type iGetGlobalDof(index_type iLocalDof) const
Definition: gradient.h:1254
static const int iVectorSize
Definition: gradient.h:358
index_type iGetStartIndexLocal() const
Definition: gradient.h:1703
index_type iGetStartIndexLocal() const
Definition: gradient.h:1373
static const bool bVectorize
Definition: gradient.h:2143
scalar_type GetValue(index_type i) const
Definition: gradient.h:502
GradientExpression< UnaryExpr< FuncAtan, Expr > > atan(const GradientExpression< Expr > &u)
Definition: gradient.h:2985
index_type iGetStartIndexLocalVector() const
Definition: gradient.h:1711
RangeVectorType::vector_type vector_type
Definition: gradient.h:1159
void ResizePreserve(LocalDofMap *pMap, index_type iStartLocal, index_type iEndLocal, LocalScope)
Definition: gradient.h:1188
static const bool bVectorize
Definition: gradient.h:2125
GradientExpression< BinaryExpr< FuncCopysign, LhsExpr, RhsExpr > > copysign(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
Definition: gradient.h:2963
static bool f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:2145
static const bool bVectorize
Definition: gradient.h:2027
doublereal scalar_func_type
Definition: gradient.h:346
static scalar_func_type f(scalar_func_type u)
Definition: gradient.h:1897
scalar_deriv_type dGetDerivativeLocal(index_type iLocalDof) const
Definition: gradient.h:2516
MapVectorType::vector_type vector_deriv_type
Definition: gradient.h:2245
static const bool bVectorize
Definition: gradient.h:1987
void Convert(scalar_func_type &d, const Gradient< N_SIZE > &g)
Definition: gradient.h:2855
RangeVector(const RangeVector &v)
Definition: gradient.h:660
GradientExpression< BinaryExpr< FuncAtan2, LhsExpr, RhsExpr > > atan2(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
Definition: gradient.h:2962
static const bool bVectorize
Definition: gradient.h:1974
double doublereal
Definition: colamd.c:52
static const bool bVectorize
Definition: gradient.h:1604
T * array_copy(const T *first, const T *const last, T *result)
Definition: gradient.h:316
RangeVector(const RangeVector< T2, N_SIZE2 > &v)
Definition: gradient.h:669
RangeVectorType oRange
Definition: gradient.h:1280
long int integer
Definition: colamd.c:51
static bool f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:2100
index_type iGetEndIndexLocal() const
Definition: gradient.h:2580
GradientExpression< UnaryExpr< FuncTan, Expr > > tan(const GradientExpression< Expr > &u)
Definition: gradient.h:2979
GradientType::scalar_func_type scalar_func_type
Definition: gradient.h:1607
#define GRADIENT_ASSERT(expr)
Definition: gradient.h:74
static scalar_deriv_type df_dv(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1760
static const index_type iDimension
Definition: gradient.h:1331
static const bool bVectorize
Definition: gradient.h:1670
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1993
RangeVectorBase< T, 0 >::scalar_type scalar_type
Definition: gradient.h:648
static const bool bVectorize
Definition: gradient.h:1750
void Compute() const
Definition: gradient.h:1659
void SetValue(index_type i, const scalar_type &d)
Definition: gradient.h:511
vector_deriv_type dGetDerivativeLocalVector(index_type iLocalDof) const
Definition: gradient.h:1699
static const bool bVectorize
Definition: gradient.h:2107
index_type iGetMaxDerivatives() const
Definition: gradient.h:2596
vector_deriv_type dGetDerivativeLocalVector(index_type iLocalVecDof) const
Definition: gradient.h:2522
scalar_type * end()
Definition: gradient.h:909
std::allocator< T >::difference_type difference_type
Definition: gradient.h:153
index_type iGetEndIndex() const
Definition: gradient.h:494
void array_fill(T *first, T *const last, const T &val)
Definition: gradient.h:309
static const T iMaxValue
Definition: gradient.h:136
LocalDofMap * pGetDofMap() const
Definition: gradient.h:1581
static scalar_deriv_type df_dv(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1794
static Data * pNullData()
Definition: gradient.h:976
const GradientType & oG
Definition: gradient.h:1596
void ResizePreserve(index_type iStartNew, index_type iEndNew)
Definition: gradient.h:461
void Copy(const RangeVector &v)
Definition: gradient.h:886
static scalar_deriv_type df_du(scalar_func_type u)
Definition: gradient.h:1875
static scalar_func_type f(scalar_func_type u, scalar_func_type v)
Definition: gradient.h:1854
scalar_func_type dGetValue() const
Definition: gradient.h:1346