5#ifndef IPPL_OPERATIONS_H
6#define IPPL_OPERATIONS_H
8#include <Kokkos_MathematicalFunctions.hpp>
21 template <
typename View,
typename Coords,
size_t... Idx>
22 KOKKOS_INLINE_FUNCTION
constexpr decltype(
auto)
apply_impl(
const View& view,
24 const std::index_sequence<Idx...>&) {
25 return view(coords[Idx]...);
38 template <
typename Coords, std::enable_if_t<std::is_array_v<Coords>,
int> = 0>
39 KOKKOS_INLINE_FUNCTION
constexpr static unsigned getRank() {
40 return std::extent_v<Coords>;
48 template <
typename Coords, std::enable_if_t<std::is_
class_v<Coords>,
int> = 0>
49 KOKKOS_INLINE_FUNCTION
constexpr static unsigned getRank() {
63 template <
typename View,
typename Coords>
64 KOKKOS_INLINE_FUNCTION
constexpr decltype(
auto)
apply(
const View& view,
const Coords& coords) {
65 using Indices = std::make_index_sequence<ExtractExpressionRank::getRank<Coords>()>;
69#define DefineUnaryOperation(fun, name, op1, op2) \
70 template <typename E> \
71 struct fun : public detail::Expression<fun<E>, sizeof(E)> { \
72 constexpr static unsigned dim = E::dim; \
73 using value_type = typename E::value_type; \
79 KOKKOS_INLINE_FUNCTION auto operator[](size_t i) const { return op1; } \
81 template <typename... Args> \
82 KOKKOS_INLINE_FUNCTION auto operator()(Args... args) const { \
90 template <typename E, size_t N> \
91 KOKKOS_INLINE_FUNCTION fun<E> name(const detail::Expression<E, N>& u) { \
92 return fun<E>(*static_cast<const E*>(&u)); \
130#define DefineBinaryOperation(fun, name, op1, op2) \
131 template <typename E1, typename E2> \
132 struct fun : public detail::Expression<fun<E1, E2>, sizeof(E1) + sizeof(E2)> { \
133 constexpr static unsigned dim = std::max(E1::dim, E2::dim); \
134 using value_type = typename E1::value_type; \
137 fun(const E1& u, const E2& v) \
141 KOKKOS_INLINE_FUNCTION auto operator[](size_t i) const { return op1; } \
143 template <typename... Args> \
144 KOKKOS_INLINE_FUNCTION auto operator()(Args... args) const { \
145 static_assert(sizeof...(Args) == dim || dim == 0); \
154 template <typename E1, size_t N1, typename E2, size_t N2> \
155 KOKKOS_INLINE_FUNCTION fun<E1, E2> name(const detail::Expression<E1, N1>& u, \
156 const detail::Expression<E2, N2>& v) { \
157 return fun<E1, E2>(*static_cast<const E1*>(&u), *static_cast<const E2*>(&v)); \
160 template <typename E, size_t N, typename T, \
161 typename = std::enable_if_t<std::is_scalar<T>::value>> \
162 KOKKOS_INLINE_FUNCTION fun<E, detail::Scalar<T>> name(const detail::Expression<E, N>& u, \
164 return fun<E, detail::Scalar<T>>(*static_cast<const E*>(&u), v); \
167 template <typename E, size_t N, typename T, \
168 typename = std::enable_if_t<std::is_scalar<T>::value>> \
169 KOKKOS_INLINE_FUNCTION fun<detail::Scalar<T>, E> name(const T& u, \
170 const detail::Expression<E, N>& v) { \
171 return fun<detail::Scalar<T>, E>(u, *static_cast<const E*>(&v)); \
196 Kokkos::copysign(u_m(args...), v_m(args...)))
200 Kokkos::fmod(u_m(args...), v_m(args...)))
202 Kokkos::pow(u_m(args...), v_m(args...)))
204 Kokkos::atan2(u_m(args...), v_m(args...)))
212 template <
typename E1,
typename E2>
214 constexpr static unsigned dim = E1::dim;
215 static_assert(E1::dim == E2::dim);
226 const size_t j = (i + 1) % 3;
227 const size_t k = (i + 2) % 3;
234 template <
typename... Args>
245 template <
typename E1,
size_t N1,
typename E2,
size_t N2>
255 template <
typename E1,
typename E2>
257 constexpr static unsigned dim = E1::dim;
258 static_assert(E1::dim == E2::dim);
268 KOKKOS_INLINE_FUNCTION
auto apply()
const {
269 typename E1::value_type res = 0.0;
272 for (
size_t i = 0; i < E1::dim; ++i) {
281 template <
typename... Args>
283 return dot(
u_m(args...),
v_m(args...)).apply();
292 template <
typename E1,
size_t N1,
typename E2,
size_t N2>
303 template <
typename E>
307 sizeof(E) + sizeof(typename E::Mesh_t::vector_type[E::Mesh_t::Dimension])> {
308 constexpr static unsigned dim = E::dim;
312 meta_grad(
const E& u,
const typename E::Mesh_t::vector_type vectors[])
314 for (
unsigned d = 0; d < E::Mesh_t::Dimension; d++) {
322 template <
typename... Idx>
323 KOKKOS_INLINE_FUNCTION
auto operator()(
const Idx... args)
const {
324 using index_type = std::tuple_element_t<0, std::tuple<Idx...>>;
334 for (
unsigned d = 0; d <
dim; d++) {
335 index_type coords[
dim] = {args...};
361 template <
typename E>
365 sizeof(E) + sizeof(typename E::Mesh_t::vector_type[E::Mesh_t::Dimension])> {
366 constexpr static unsigned dim = E::dim;
369 meta_div(
const E& u,
const typename E::Mesh_t::vector_type vectors[])
371 for (
unsigned d = 0; d < E::Mesh_t::Dimension; d++) {
379 template <
typename... Idx>
380 KOKKOS_INLINE_FUNCTION
auto operator()(
const Idx... args)
const {
381 using index_type = std::tuple_element_t<0, std::tuple<Idx...>>;
389 typename E::Mesh_t::value_type res = 0;
390 for (
unsigned d = 0; d <
dim; d++) {
391 index_type coords[
dim] = {args...};
414 template <
typename E>
417 sizeof(E) + sizeof(typename E::Mesh_t::vector_type)> {
418 constexpr static unsigned dim = E::dim;
422 meta_laplace(
const E& u,
const typename E::Mesh_t::vector_type& hvector)
429 template <
typename... Idx>
430 KOKKOS_INLINE_FUNCTION
auto operator()(
const Idx... args)
const {
431 using index_type = std::tuple_element_t<0, std::tuple<Idx...>>;
432 using T =
typename E::Mesh_t::value_type;
441 for (
unsigned d = 0; d <
dim; d++) {
442 index_type coords[
dim] = {args...};
451 res +=
hvector_m[d] * (left - 2 * center + right);
469 template <
typename E>
472 sizeof(E) + 4 * sizeof(typename E::Mesh_t::vector_type)> {
473 constexpr static unsigned dim = E::dim;
476 meta_curl(
const E& u,
const typename E::Mesh_t::vector_type& xvector,
477 const typename E::Mesh_t::vector_type& yvector,
478 const typename E::Mesh_t::vector_type& zvector,
479 const typename E::Mesh_t::vector_type& hvector)
489 KOKKOS_INLINE_FUNCTION
auto operator()(
size_t i,
size_t j,
size_t k)
const {
517 template <
typename E>
521 + sizeof(typename E::Mesh_t::vector_type[E::Mesh_t::Dimension])
522 + sizeof(typename E::Mesh_t::vector_type)> {
523 constexpr static unsigned dim = E::dim;
526 meta_hess(
const E& u,
const typename E::Mesh_t::vector_type vectors[],
527 const typename E::Mesh_t::vector_type& hvector)
530 for (
unsigned d = 0; d < E::Mesh_t::Dimension; d++) {
538 template <
typename... Idx>
539 KOKKOS_INLINE_FUNCTION
auto operator()(
const Idx... args)
const {
541 computeHessian(std::make_index_sequence<dim>{}, hessian, args...);
563 template <
size_t... row,
typename... Idx>
565 const std::index_sequence<row...>& is,
matrix_type& hessian,
566 const Idx... args)
const {
570 [[maybe_unused]]
auto _ = (
hessianRow<row>(is, hessian, args...) + ...);
584 template <
size_t row,
size_t... col,
typename... Idx>
585 KOKKOS_INLINE_FUNCTION
constexpr int hessianRow(
const std::index_sequence<col...>&,
587 const Idx... args)
const {
601 template <
size_t row,
size_t col,
typename... Idx>
603 using index_type = std::tuple_element_t<0, std::tuple<Idx...>>;
604 index_type coords[
dim] = {args...};
605 if constexpr (row == col) {
616 return vectors_m[row] * (right - 2. * center + left)
634 return vectors_m[col] * (uu - du - ud + dd)
#define DefineUnaryOperation(fun, name, op1, op2)
#define DefineBinaryOperation(fun, name, op1, op2)
KOKKOS_INLINE_FUNCTION constexpr decltype(auto) apply(const View &view, const Coords &coords)
KOKKOS_INLINE_FUNCTION detail::meta_cross< E1, E2 > cross(const detail::Expression< E1, N1 > &u, const detail::Expression< E2, N2 > &v)
KOKKOS_INLINE_FUNCTION constexpr decltype(auto) apply_impl(const View &view, const Coords &coords, const std::index_sequence< Idx... > &)
KOKKOS_INLINE_FUNCTION detail::meta_dot< E1, E2 > dot(const detail::Expression< E1, N1 > &u, const detail::Expression< E2, N2 > &v)
KOKKOS_INLINE_FUNCTION static constexpr unsigned getRank()
KOKKOS_INLINE_FUNCTION auto operator()(Args... args) const
KOKKOS_INLINE_FUNCTION auto operator[](size_t i) const
KOKKOS_FUNCTION meta_cross(const E1 &u, const E2 &v)
static constexpr unsigned dim
KOKKOS_INLINE_FUNCTION auto apply() const
KOKKOS_INLINE_FUNCTION auto operator()(Args... args) const
KOKKOS_FUNCTION meta_dot(const E1 &u, const E2 &v)
static constexpr unsigned dim
typename E::value_type value_type
static constexpr unsigned dim
typename Mesh_t::vector_type vector_type
vector_type vectors_m[dim]
KOKKOS_INLINE_FUNCTION auto operator()(const Idx... args) const
typename E::Mesh_t Mesh_t
KOKKOS_FUNCTION meta_grad(const E &u, const typename E::Mesh_t::vector_type vectors[])
static constexpr unsigned dim
KOKKOS_FUNCTION meta_div(const E &u, const typename E::Mesh_t::vector_type vectors[])
vector_type vectors_m[dim]
typename Mesh_t::vector_type vector_type
typename E::Mesh_t Mesh_t
KOKKOS_INLINE_FUNCTION auto operator()(const Idx... args) const
typename E::value_type value_type
static constexpr unsigned dim
KOKKOS_FUNCTION meta_laplace(const E &u, const typename E::Mesh_t::vector_type &hvector)
const vector_type hvector_m
typename E::Mesh_t Mesh_t
KOKKOS_INLINE_FUNCTION auto operator()(const Idx... args) const
typename Mesh_t::vector_type vector_type
const vector_type zvector_m
const vector_type xvector_m
const vector_type hvector_m
typename E::Mesh_t Mesh_t
KOKKOS_FUNCTION meta_curl(const E &u, const typename E::Mesh_t::vector_type &xvector, const typename E::Mesh_t::vector_type &yvector, const typename E::Mesh_t::vector_type &zvector, const typename E::Mesh_t::vector_type &hvector)
KOKKOS_INLINE_FUNCTION auto operator()(size_t i, size_t j, size_t k) const
static constexpr unsigned dim
typename Mesh_t::vector_type vector_type
const vector_type yvector_m
KOKKOS_INLINE_FUNCTION constexpr void computeHessian(const std::index_sequence< row... > &is, matrix_type &hessian, const Idx... args) const
typename E::Mesh_t Mesh_t
vector_type vectors_m[dim]
typename Mesh_t::matrix_type matrix_type
static constexpr unsigned dim
typename Mesh_t::vector_type vector_type
KOKKOS_FUNCTION meta_hess(const E &u, const typename E::Mesh_t::vector_type vectors[], const typename E::Mesh_t::vector_type &hvector)
KOKKOS_INLINE_FUNCTION constexpr int hessianRow(const std::index_sequence< col... > &, matrix_type &hessian, const Idx... args) const
KOKKOS_INLINE_FUNCTION auto operator()(const Idx... args) const
KOKKOS_INLINE_FUNCTION constexpr vector_type hessianEntry(const Idx... args) const
const vector_type hvector_m
Vector< vector_type, Dim > matrix_type
Mesh< double, Dim >::vector_type vector_type