Tensorium
Loading...
Searching...
No Matches
BSSNAutoDiff.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "../Metric.hpp"
5#include "BSSNDerivatives.hpp"
6
7namespace tensorium {
8template <typename T> struct TensorTraits;
9
10template <typename T, size_t Rank> struct TensorTraits<Tensor<T, Rank>> {
11 static constexpr size_t rank = Rank;
12 using value_type = T;
13};
14
15template <typename T> struct TensorTraits {
16 static constexpr size_t rank = 0;
17 using value_type = T;
18};
19
20template <typename T> struct TensorTraits<Vector<T>> {
21 static constexpr size_t rank = 1;
22 using value_type = T;
23};
24} // namespace tensorium
25
26namespace tensorium_RG {
27
28enum class DiffMode { PARTIAL, COV, COV2, SPEC };
29
30template <typename T, typename ScalarFunc>
32 ScalarFunc &&func,
33 const tensorium::Tensor<T, 3> &christoffel) {
34 tensorium::Vector<T> grad = partial_scalar(X, dx, dy, dz, std::forward<ScalarFunc>(func));
35 return grad;
36}
37
38template <typename T, typename ScalarFunc>
40 ScalarFunc &&func,
41 const tensorium::Tensor<T, 3> &christoffel) {
42 return covariant_scalar_second(X, dx, dy, dz, std::forward<ScalarFunc>(func), christoffel);
43}
44
45template <typename T, typename ScalarFunc>
47 ScalarFunc &&func, DiffMode mode,
48 const tensorium::Tensor<T, 3> &christoffel) {
49 using namespace tensorium;
50 if (mode == DiffMode::PARTIAL) {
51 return partial_scalar(X, dx, dy, dz, std::forward<ScalarFunc>(func));
52 } else if (mode == DiffMode::COV) {
53 return covariant_scalar(X, dx, dy, dz, std::forward<ScalarFunc>(func), christoffel);
54 } else {
55 throw std::invalid_argument("autodiff_rank0: PARTIAL ou COV uniquement. Utiliser "
56 "`autodiff_scalar_second` pour COV2.");
57 }
58}
59
60template <typename T, typename VectorFunc>
64 using namespace tensorium;
65 if (mode == DiffMode::PARTIAL) {
66 return partial_vector(X, dx, dy, dz, std::forward<VectorFunc>(func));
67 } else if (mode == DiffMode::COV) {
68 return covariant_vector(X, dx, dy, dz, std::forward<VectorFunc>(func), christoffel);
69 } else {
70 throw std::invalid_argument(
71 "autodiff_rank1: uniquement PARTIAL ou COV autorisés pour un champ vectoriel");
72 }
73}
74
75template <typename T, typename TensorFunc>
79 using namespace tensorium;
80 if (mode == DiffMode::PARTIAL) {
81 return partial_tensor2(X, dx, dy, dz, std::forward<TensorFunc>(func));
82 } else if (mode == DiffMode::COV) {
83 return covariant_tensor2(X, dx, dy, dz, std::forward<TensorFunc>(func), christoffel);
84 } else if (mode == DiffMode::SPEC) {
85 return spectral_partial_tensor2<T>(X, dx, dy, dz, std::forward<TensorFunc>(func), 64, 64,
86 64);
87 } else {
88 throw std::invalid_argument(
89 "autodiff_rank2_first: uniquement PARTIAL ou COV autorisés pour un tenseur d'ordre 2");
90 }
91}
92
93template <typename T, typename FieldFunc>
96 using namespace tensorium;
97 auto sample = func(X);
98
99 constexpr size_t rank = TensorTraits<std::decay_t<decltype(sample)>>::rank;
100
101 if constexpr (rank == 0) {
102 if (mode == DiffMode::COV2)
103 return autodiff_scalar_second(X, dx, dy, dz, std::forward<FieldFunc>(func),
104 christoffel); // <- nouveau chemin
105 else
106 return autodiff_rank0(X, dx, dy, dz, std::forward<FieldFunc>(func), mode, christoffel);
107 } else if constexpr (rank == 1) {
108 return autodiff_rank1(X, dx, dy, dz, std::forward<FieldFunc>(func), mode, christoffel);
109 } else if constexpr (rank == 2) {
110 return autodiff_rank2_first(X, dx, dy, dz, std::forward<FieldFunc>(func), mode,
112 } else {
113 static_assert(
114 rank <= 2,
115 "autodiff: seuls les rangs 0 (scalaire), 1 (vecteur) et 2 (tenseur2) sont supportés.");
116 }
117}
118
119} // namespace tensorium_RG
static FrontendPluginRegistry::Add< TensoriumPluginAction > X("tensorium-dispatch", "Handle #pragma tensorium directives")
Register the plugin under the name "tensorium-dispatch".
Multi-dimensional tensor class with fixed rank and SIMD support.
Definition Tensor.hpp:25
Aligned, SIMD-optimized mathematical vector class for scientific computing.
Definition Vector.hpp:26
int rank()
Definition MPI_init.hpp:71
Definition BSSNAtildeTensor.hpp:10
tensorium::Tensor< T, 3 > autodiff_rank2_first(const tensorium::Vector< T > &X, T dx, T dy, T dz, TensorFunc &&func, DiffMode mode, const tensorium::Tensor< T, 3 > &christoffel)
Definition BSSNAutoDiff.hpp:76
tensorium::Tensor< T, 2 > autodiff_scalar_second(const tensorium::Vector< T > &X, T dx, T dy, T dz, ScalarFunc &&func, const tensorium::Tensor< T, 3 > &christoffel)
Definition BSSNAutoDiff.hpp:39
tensorium::Tensor< T, 2 > partial_vector(const tensorium::Vector< T > &X, T dx, T dy, T dz, VectorFunc &&func)
Definition BSSNDerivatives.hpp:169
DiffMode
Definition BSSNAutoDiff.hpp:28
tensorium::Tensor< T, 2 > covariant_vector(const tensorium::Vector< T > &X, T dx, T dy, T dz, VectorFunc &&func, const tensorium::Tensor< T, 3 > &Gamma)
Definition BSSNDerivatives.hpp:628
auto autodiff(const tensorium::Vector< T > &X, T dx, T dy, T dz, FieldFunc &&func, DiffMode mode, const tensorium::Tensor< T, 3 > &christoffel={})
Definition BSSNAutoDiff.hpp:94
tensorium::Vector< T > covariant_scalar(const tensorium::Vector< T > &X, T dx, T dy, T dz, ScalarFunc &&func, const tensorium::Tensor< T, 3 > &christoffel)
Definition BSSNAutoDiff.hpp:31
tensorium::Tensor< T, 2 > covariant_scalar_second(const tensorium::Vector< T > &X, T dx, T dy, T dz, ScalarFunc &&func, const tensorium::Tensor< T, 3 > &christoffel)
Definition BSSNDerivatives.hpp:513
tensorium::Tensor< T, 3 > partial_tensor2(const tensorium::Vector< T > &X, T dx, T dy, T dz, TensorFunc &&func)
Definition BSSNDerivatives.hpp:663
tensorium::Vector< T > partial_scalar(const tensorium::Vector< T > &X, T dx, T dy, T dz, ScalarFunc &&func)
Definition BSSNDerivatives.hpp:119
tensorium::Tensor< T, 2 > autodiff_rank1(const tensorium::Vector< T > &X, T dx, T dy, T dz, VectorFunc &&func, DiffMode mode, const tensorium::Tensor< T, 3 > &christoffel)
Definition BSSNAutoDiff.hpp:61
tensorium::Tensor< T, 3 > covariant_tensor2(const tensorium::Vector< T > &X, T dx, T dy, T dz, TensorFunc &&func, const tensorium::Tensor< T, 3 > &Gamma)
Definition BSSNDerivatives.hpp:645
tensorium::Vector< T > autodiff_rank0(const tensorium::Vector< T > &X, T dx, T dy, T dz, ScalarFunc &&func, DiffMode mode, const tensorium::Tensor< T, 3 > &christoffel)
Definition BSSNAutoDiff.hpp:46
Definition Derivate.hpp:24
Definition BSSNAutoDiff.hpp:15
static constexpr size_t rank
Definition BSSNAutoDiff.hpp:16