SimiLie
Loading...
Searching...
No Matches
form.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3// AI-GENERATED
4
5#pragma once
6
7#include <array>
8#include <cassert>
9#include <initializer_list>
10#include <utility>
11
12#include <similie/tensor/owning_tensor.hpp>
13
14namespace sil::exterior {
15
16namespace detail {
17template <class T>
18struct FormArrayStorage : std::false_type
19{
20};
21template <class T, std::size_t N>
22struct FormArrayStorage<std::array<T, N>> : std::true_type
23{
24};
25} // namespace detail
26
30template <class Coefficients>
31class Form : public Coefficients
32{
33 static_assert(
36 || detail::FormArrayStorage<Coefficients>::value);
37
38public:
39 using coefficients_type = Coefficients;
40
41 KOKKOS_DEFAULTED_FUNCTION constexpr Form() = default;
42 KOKKOS_DEFAULTED_FUNCTION constexpr Form(Form const&) = default;
43 KOKKOS_DEFAULTED_FUNCTION constexpr Form(Form&&) = default;
44 KOKKOS_DEFAULTED_FUNCTION constexpr Form& operator=(Form const&) = default;
45 KOKKOS_DEFAULTED_FUNCTION constexpr Form& operator=(Form&&) = default;
46
47 KOKKOS_FUNCTION constexpr explicit Form(Coefficients const& coefficients)
48 : Coefficients(coefficients)
49 {
50 }
51
52 KOKKOS_FUNCTION constexpr Form(std::initializer_list<typename Coefficients::value_type> values)
53 requires(detail::FormArrayStorage<Coefficients>::value)
54 : Coefficients {}
55 {
56 if (values.size() > this->size())
57 Kokkos::abort("Too many differential-form coefficients");
58 std::size_t i = 0;
59 for (typename Coefficients::value_type const& value : values)
60 Coefficients::operator[](i++) = value;
61 }
62
63 KOKKOS_FUNCTION constexpr decltype(auto) coefficients() const
64 {
66 return static_cast<typename Coefficients::tensor_type const&>(*this);
67 else
68 return static_cast<Coefficients const&>(*this);
69 }
70 KOKKOS_FUNCTION constexpr decltype(auto) coefficients()
71 {
73 return static_cast<typename Coefficients::tensor_type&>(*this);
74 else
75 return static_cast<Coefficients&>(*this);
76 }
77
78 KOKKOS_FUNCTION Form& operator+=(Form const& other)
79 requires requires(Coefficients& left, Coefficients const& right) { left += right; }
80 {
81 coefficients() += other.coefficients();
82 return *this;
83 }
84
85 template <class Scalar>
86 KOKKOS_FUNCTION Form& operator*=(Scalar scalar)
87 requires requires(Coefficients& coefficients) { coefficients *= scalar; }
88 {
89 coefficients() *= scalar;
90 return *this;
91 }
92
93 template <class Element>
94 KOKKOS_FUNCTION constexpr decltype(auto) operator[](Element element) const
95 {
96 if constexpr (!detail::FormArrayStorage<Coefficients>::value)
97 return ::sil::exterior::Form(Coefficients::operator[](element));
98 else
99 return Coefficients::operator[](element);
100 }
101
102 template <class Element>
103 KOKKOS_FUNCTION constexpr decltype(auto) operator[](Element element)
104 {
105 if constexpr (!detail::FormArrayStorage<Coefficients>::value)
106 return ::sil::exterior::Form(Coefficients::operator[](element));
107 else
108 return Coefficients::operator[](element);
109 }
110};
111
112// A scalar physical field is a zero-form. Arithmetic reads its pointwise
113// coefficient; constructing one from an unclassified number remains explicit.
114template <>
115class Form<double>
116{
117 double m_value = 0.0;
118
119public:
120 using coefficients_type = double;
121 KOKKOS_DEFAULTED_FUNCTION constexpr Form() = default;
122 KOKKOS_FUNCTION constexpr explicit Form(double value) : m_value(value) {}
123 KOKKOS_FUNCTION constexpr operator double() const
124 {
125 return m_value;
126 }
127 KOKKOS_FUNCTION constexpr double value() const
128 {
129 return m_value;
130 }
131 KOKKOS_FUNCTION constexpr double const& coefficients() const
132 {
133 return m_value;
134 }
135 KOKKOS_FUNCTION constexpr double& coefficients()
136 {
137 return m_value;
138 }
139 KOKKOS_FUNCTION constexpr Form& operator=(double value)
140 {
141 m_value = value;
142 return *this;
143 }
144 KOKKOS_FUNCTION constexpr Form& operator+=(double value)
145 {
146 m_value += value;
147 return *this;
148 }
149 KOKKOS_FUNCTION constexpr Form& operator-=(double value)
150 {
151 m_value -= value;
152 return *this;
153 }
154 KOKKOS_FUNCTION constexpr Form& operator*=(double value)
155 {
156 m_value *= value;
157 return *this;
158 }
159 KOKKOS_FUNCTION constexpr Form& operator/=(double value)
160 {
161 m_value /= value;
162 return *this;
163 }
164};
165
166template <class Coefficients>
167Form(Coefficients const&) -> Form<Coefficients>;
168
169template <class T>
171
172template <class T>
173concept TensorFormType = FormType<T> && requires { typename T::indices_domain_t; };
174
175namespace detail {
176template <class Domain>
177struct FormDegree;
178template <class... Index>
179struct FormDegree<ddc::DiscreteDomain<Index...>>
180 : std::integral_constant<std::size_t, (Index::rank() + ... + 0)>
181{
182};
183} // namespace detail
184
185template <TensorFormType T>
186inline constexpr std::size_t form_degree_v
187 = detail::FormDegree<typename T::indices_domain_t>::value;
188
189} // namespace sil::exterior
KOKKOS_FUNCTION constexpr Form & operator/=(double value)
Definition form.hpp:159
KOKKOS_FUNCTION constexpr Form & operator*=(double value)
Definition form.hpp:154
KOKKOS_DEFAULTED_FUNCTION constexpr Form()=default
KOKKOS_FUNCTION constexpr Form & operator=(double value)
Definition form.hpp:139
KOKKOS_FUNCTION constexpr Form & operator-=(double value)
Definition form.hpp:149
KOKKOS_FUNCTION constexpr double const & coefficients() const
Definition form.hpp:131
KOKKOS_FUNCTION constexpr double value() const
Definition form.hpp:127
KOKKOS_FUNCTION constexpr double & coefficients()
Definition form.hpp:135
KOKKOS_FUNCTION constexpr Form & operator+=(double value)
Definition form.hpp:144
KOKKOS_FUNCTION constexpr Form(double value)
Definition form.hpp:122
Differential-form coefficients, pointwise or sampled over a spatial domain.
Definition form.hpp:32
KOKKOS_DEFAULTED_FUNCTION constexpr Form & operator=(Form const &)=default
KOKKOS_DEFAULTED_FUNCTION constexpr Form()=default
KOKKOS_FUNCTION constexpr Form(Coefficients const &coefficients)
Definition form.hpp:47
KOKKOS_DEFAULTED_FUNCTION constexpr Form & operator=(Form &&)=default
KOKKOS_DEFAULTED_FUNCTION constexpr Form(Form const &)=default
KOKKOS_FUNCTION constexpr decltype(auto) coefficients() const
Definition form.hpp:63
Coefficients coefficients_type
Definition form.hpp:39
KOKKOS_FUNCTION constexpr Form(std::initializer_list< typename Coefficients::value_type > values)
Definition form.hpp:52
KOKKOS_FUNCTION constexpr decltype(auto) coefficients()
Definition form.hpp:70
KOKKOS_FUNCTION Form & operator*=(Scalar scalar)
Definition form.hpp:86
KOKKOS_DEFAULTED_FUNCTION constexpr Form(Form &&)=default
KOKKOS_FUNCTION Form & operator+=(Form const &other)
Definition form.hpp:78
Form(Coefficients const &) -> Form< Coefficients >
constexpr std::size_t form_degree_v
Definition form.hpp:187