1 #ifndef VERTEXCFD_UTILS_SMOOTHCLAMP_HPP
2 #define VERTEXCFD_UTILS_SMOOTHCLAMP_HPP
5 #include <Sacado_Fad_Exp_Expression.hpp>
6 #include <Sacado_Fad_Exp_ExpressionTraits.hpp>
17 template<
typename T,
typename ExprSpec>
24 :
public Sacado::Fad::Exp::Expr<
25 SmoothClampOp<T, Sacado::Fad::Exp::ExprSpecDefault>>
28 using ExprT =
typename std::remove_cv<T>::type;
29 using value_type =
typename ExprT::value_type;
30 using scalar_type =
typename ExprT::scalar_type;
31 using expr_spec_type = Sacado::Fad::Exp::ExprSpecDefault;
44 SACADO_INLINE_FUNCTION
int size()
const {
return x_.size(); }
46 SACADO_INLINE_FUNCTION
bool hasFastAccess()
const
48 return x_.hasFastAccess();
51 SACADO_INLINE_FUNCTION value_type val()
const
60 else if (x_ >= hi_ + tol_)
66 else if (x_ < lo_ + tol_)
72 * ((x_.val() - lo_) * (x_.val() - lo_) / tol_
77 else if (x_ > hi_ - tol_)
83 * ((x_.val() - hi_) * (x_.val() - hi_) / tol_
94 SACADO_INLINE_FUNCTION value_type dx(
int i)
const
102 else if (x_ >= hi_ + tol_)
107 else if (x_ < lo_ + tol_)
110 return 0.5 * (x_.dx(i) + (x_.val() - lo_) * x_.dx(i) / tol_);
113 else if (x_ > hi_ - tol_)
116 return 0.5 * (x_.dx(i) + (x_.val() - hi_) * x_.dx(i) / tol_);
125 SACADO_INLINE_FUNCTION value_type fastAccessDx(
int i)
const
128 if (x_ <= lo_ - tol_)
133 else if (x_ >= hi_ + tol_)
138 else if (x_ < lo_ + tol_)
142 * (x_.fastAccessDx(i)
143 + (x_.val() - lo_) * x_.fastAccessDx(i) / tol_);
146 else if (x_ > hi_ - tol_)
150 * (x_.fastAccessDx(i)
151 + (x_.val() - hi_) * x_.fastAccessDx(i) / tol_);
156 return x_.fastAccessDx(i);
169 SACADO_INLINE_FUNCTION
170 typename std::enable_if<std::is_trivial<T>::value, T>::type
171 clamp(
const T x,
const double lo,
const double hi,
const double tol)
180 else if (x >= hi + tol)
186 else if (x < lo + tol)
189 return 0.5 * (x + lo + 0.5 * ((x - lo) * (x - lo) / tol + tol));
193 else if (x > hi - tol)
196 return 0.5 * (x + hi - 0.5 * ((x - hi) * (x - hi) / tol + tol));
206 using Sacado::Fad::Exp::Expr;
210 SACADO_INLINE_FUNCTION
211 SmoothClampOp<typename Expr<T>::derived_type,
typename T::expr_spec_type>
212 clamp(
const Expr<T>& x,
double lo,
double hi,
double tol)
214 using expr_t = SmoothClampOp<typename Expr<T>::derived_type,
215 typename T::expr_spec_type>;
216 return expr_t(x.derived(), lo, hi, tol);
236 template<
typename T,
typename E>
239 static const unsigned value = ExprLevel<T>::value;
242 template<
typename T,
typename E>
245 static const unsigned value =
true;
251 template<
typename T,
typename E>
254 static const unsigned value =
true;
257 template<
typename T,
typename E>
260 using type =
typename BaseExprType<T>::type;
263 template<
typename T,
typename E>
266 static const bool value
267 = IsSimdType<typename SmoothClampOp<T, E>::scalar_type>::value;
270 template<
typename T,
typename E>
276 template<
typename T,
typename E>
284 #endif // end VERTEXCFD_UTILS_SMOOTHCLAMP_HPP