www.mooseframework.org
MathUtils.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "Moose.h"
13 #include "libmesh/libmesh.h"
14 #include "libmesh/utility.h"
15 #include "libmesh/numeric_vector.h"
16 #include "libmesh/compare_types.h"
17 
18 namespace MathUtils
19 {
20 
21 inline Real
22 round(Real x)
23 {
24  return ::round(x); // use round from math.h
25 }
26 
27 inline Real
28 sign(Real x)
29 {
30  return x >= 0.0 ? 1.0 : -1.0;
31 }
32 
33 Real poly1Log(Real x, Real tol, int deriv);
34 Real poly2Log(Real x, Real tol, int deriv);
35 Real poly3Log(Real x, Real tol, int order);
36 Real poly4Log(Real x, Real tol, int order);
37 Real taylorLog(Real x);
38 
39 Real pow(Real x, int e);
40 Real poly(std::vector<Real> c, const Real x, const bool deriv);
41 
42 inline Real
43 heavyside(Real x)
44 {
45  return x < 0.0 ? 0.0 : 1.0;
46 }
47 inline Real
49 {
50  return x > 0.0 ? x : 0.0;
51 }
52 inline Real
54 {
55  return x < 0.0 ? x : 0.0;
56 }
57 
58 template <typename T,
59  typename T2,
60  typename T3,
61  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
62  ScalarTraits<T3>::value,
63  int>::type = 0>
64 void
65 addScaled(const T & a, const T2 & b, T3 & result)
66 {
67  result += a * b;
68 }
69 
70 template <typename T,
71  typename T2,
72  typename T3,
73  typename std::enable_if<ScalarTraits<T>::value, int>::type = 0>
74 void
75 addScaled(const T & scalar, const NumericVector<T2> & numeric_vector, NumericVector<T3> & result)
76 {
77  result.add(scalar, numeric_vector);
78 }
79 
80 template <
81  typename T,
82  typename T2,
83  template <typename> class W,
84  template <typename> class W2,
85  typename std::enable_if<std::is_same<typename W<T>::index_type, unsigned int>::value &&
86  std::is_same<typename W2<T2>::index_type, unsigned int>::value,
87  int>::type = 0>
88 typename CompareTypes<T, T2>::supertype
89 dotProduct(const W<T> & a, const W2<T2> & b)
90 {
91  return a * b;
92 }
93 
94 template <typename T,
95  typename T2,
96  template <typename> class W,
97  template <typename> class W2,
98  typename std::enable_if<std::is_same<typename W<T>::index_type,
99  std::tuple<unsigned int, unsigned int>>::value &&
100  std::is_same<typename W2<T2>::index_type,
101  std::tuple<unsigned int, unsigned int>>::value,
102  int>::type = 0>
103 typename CompareTypes<T, T2>::supertype
104 dotProduct(const W<T> & a, const W2<T2> & b)
105 {
106  return a.contract(b);
107 }
108 
109 template <typename T>
110 T
111 poly(std::vector<Real> c, const T x, const bool derivative)
112 {
113  const unsigned int size = c.size();
114  if (size == 0)
115  return 0.0;
116 
117  T value = c[0];
118  if (derivative)
119  {
120  value *= size - 1;
121  for (unsigned int i = 1; i < size - 1; i++)
122  value = value * x + c[i] * (size - i - 1);
123  }
124  else
125  {
126  for (unsigned int i = 1; i < size; i++)
127  value = value * x + c[i];
128  }
129 
130  return value;
131 }
132 
133 template <typename T>
134 T
135 clamp(const T & x, Real lowerlimit, Real upperlimit)
136 {
137  if (x < lowerlimit)
138  return lowerlimit;
139  if (x > upperlimit)
140  return upperlimit;
141  return x;
142 }
143 
144 template <typename T>
145 T
146 smootherStep(T x, T start, T end, bool derivative = false)
147 {
148  if (end == start)
149  return 0.0;
150  x = clamp((x - start) / (end - start), 0.0, 1.0);
151  if (x == 0.0)
152  return 0.0;
153  if (derivative)
154  {
155  if (x == 1.0)
156  return 0.0;
157  return 30.0 * Utility::pow<2>(x) * (x * (x - 2.0) + 1.0) / (end - start);
158  }
159  if (x == 1.0)
160  return 1.0;
161  return Utility::pow<3>(x) * (x * (x * 6.0 - 15.0) + 10.0);
162 }
163 
164 } // namespace MathUtils
Real heavyside(Real x)
Definition: MathUtils.h:43
void addScaled(const T &a, const T2 &b, T3 &result)
Definition: MathUtils.h:65
Real sign(Real x)
Definition: MathUtils.h:28
Real round(Real x)
Definition: MathUtils.h:22
Real poly1Log(Real x, Real tol, int deriv)
Definition: MathUtils.C:17
static PetscErrorCode Vec x
Real poly3Log(Real x, Real tol, int order)
Definition: MathUtils.C:98
T smootherStep(T x, T start, T end, bool derivative=false)
Definition: MathUtils.h:146
Real pow(Real x, int e)
Definition: MathUtils.C:211
Real poly2Log(Real x, Real tol, int deriv)
Definition: MathUtils.C:57
Real poly4Log(Real x, Real tol, int order)
Definition: MathUtils.C:139
Real taylorLog(Real x)
Definition: MathUtils.C:196
T clamp(const T &x, Real lowerlimit, Real upperlimit)
Definition: MathUtils.h:135
MatType type
Real negativePart(Real x)
Definition: MathUtils.h:53
Real positivePart(Real x)
Definition: MathUtils.h:48
CompareTypes< T, T2 >::supertype dotProduct(const W< T > &a, const W2< T2 > &b)
Definition: MathUtils.h:89
Real poly(std::vector< Real > c, const Real x, const bool deriv)