SimiLie
Loading...
Searching...
No Matches
physics/concepts.hpp
1// SPDX-FileCopyrightText: 2026 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3// AI-GENERATED
4
5#pragma once
6
7#include <array>
8#include <concepts>
9#include <cstddef>
10#include <type_traits>
11
12#include <similie/exterior/form.hpp>
13
14#include <Kokkos_Core.hpp>
15
16namespace similie::physics {
17
18namespace detail {
19
20struct ElementConcept
21{
22};
23
24template <class Dimension>
25struct GridConcept
26{
27 using continuous_dimension_type = Dimension;
28};
29
30struct ParameterConcept
31{
32 template <class Element, class Component>
33 constexpr double operator()(Element, Component) const
34 {
35 return 1;
36 }
37};
38
39// A complete state keeps coupled components available to every operation.
40struct HamiltonianPhysicsConcept
41{
42 static constexpr std::size_t N = 1;
43 static constexpr bool IS_LINEAR = true;
44 struct State
45 {
48 };
49
50 template <class Element>
51 KOKKOS_FUNCTION constexpr sil::exterior::Form<double> hamiltonian(State const& state, Element)
52 const
53 {
55 (state.potential[0] * state.potential[0] + state.moments[0] * state.moments[0])
56 / 2);
57 }
58 template <class Index, class Element>
59 KOKKOS_FUNCTION constexpr double dhamiltonian_dmoments(State const& state, Element) const
60 {
61 return state.moments[Index::value];
62 }
63 template <class Index, class Element>
64 KOKKOS_FUNCTION constexpr double dhamiltonian_dpotential(State const& state, Element) const
65 {
66 return state.potential[Index::value];
67 }
68 template <class Row, class Column, class Element>
69 KOKKOS_FUNCTION constexpr double jacobian(State const&, Element) const
70 {
71 return Row::value == Column::value ? 1 : 0;
72 }
73 template <class Index, class Element>
74 KOKKOS_FUNCTION constexpr double dhamiltonian_dmoments_value(State const&, Element) const
75 {
76 return 1;
77 }
78 template <class Index, class Element>
79 KOKKOS_FUNCTION constexpr double dhamiltonian_dpotential_value(State const&, Element) const
80 {
81 return 1;
82 }
83};
84
85// Namespace-level concepts avoid nvcc rejecting dependent member-template calls
86// in requires-expressions declared inside a class template.
87template <class Hamiltonian, class Index, class Element>
88concept HasMomentCoefficientConcept = requires(
89 Hamiltonian const& h,
90 typename Hamiltonian::State const& state,
91 Element element) { h.template dhamiltonian_dmoments_value<Index>(state, element); };
92
93template <class Hamiltonian, class Index, class Element>
94concept HasPotentialCoefficientConcept = requires(
95 Hamiltonian const& h,
96 typename Hamiltonian::State const& state,
97 Element element) { h.template dhamiltonian_dpotential_value<Index>(state, element); };
98
99} // namespace detail
100
101// Physics states, arguments, and results contain pointwise differential-form
102// coefficients. Numerical callers own cochain reduction and reconstruction.
103// Physics models share state and compile-time metadata. Their equations are
104// specified by more specialized concepts, without prescribing the state layout.
105template <class Physics>
106concept PhysicsConcept = requires {
107 typename Physics::State;
108 requires std::same_as<std::remove_cv_t<decltype(Physics::N)>, std::size_t>;
109 requires std::same_as<std::remove_cv_t<decltype(Physics::IS_LINEAR)>, bool>;
110 typename std::integral_constant<std::size_t, Physics::N>;
111 typename std::bool_constant<Physics::IS_LINEAR>;
112};
113
114template <class Physics, class Element>
117 && requires(
118 Physics const& physics,
119 typename Physics::State const& state,
120 Element element) {
121 {
122 physics.hamiltonian(state, element)
123 } -> std::same_as<sil::exterior::Form<double>>;
124 {
125 physics.template dhamiltonian_dmoments<
126 std::integral_constant<std::size_t, 0>>(state, element)
127 } -> std::same_as<double>;
128 {
129 physics.template dhamiltonian_dpotential<
130 std::integral_constant<std::size_t, 0>>(state, element)
131 } -> std::same_as<double>;
132 {
133 physics.template jacobian<
134 std::integral_constant<std::size_t, 0>,
135 std::integral_constant<std::size_t, 0>>(state, element)
136 } -> std::same_as<double>;
137 };
138
139template <class Equations, class Element>
140concept HamiltonEquationsConcept = requires(
141 Equations const& equations,
142 typename Equations::State const& state,
143 Element element) {
144 {
145 equations.template dpotential_dt<std::integral_constant<std::size_t, 0>>(state, element)
146 } -> std::same_as<double>;
147 {
148 equations.template dmoments_dt<std::integral_constant<std::size_t, 0>>(state, element)
149 } -> std::same_as<double>;
150 {
151 equations.template jacobian<
152 std::integral_constant<std::size_t, 0>,
153 std::integral_constant<std::size_t, 0>>(state, element)
154 } -> std::same_as<double>;
155};
156
157template <class Equations, class Element>
159 Equations const& equations,
160 typename Equations::State const& state,
161 Element element) {
162 {
163 equations.template potential_grad<std::integral_constant<std::size_t, 0>>(state, element)
164 } -> std::same_as<double>;
165 {
166 equations.template moments_div<std::integral_constant<std::size_t, 0>>(state, element)
167 } -> std::same_as<double>;
168};
169
170// Scalar and vector laws use arrays of the same form, with N components.
171template <class Law, class Element>
173 = requires(Law const& law, typename Law::State const& state, Element element) {
174 typename std::integral_constant<std::size_t, Law::N>;
175 {
176 law(state, element)
177 } -> std::same_as<sil::exterior::Form<std::array<double, Law::N>>>;
178 { law.value(state, element) } -> std::same_as<std::array<double, Law::N * Law::N>>;
179 { law.jacobian(state, element) } -> std::same_as<std::array<double, Law::N * Law::N>>;
180 {
181 law.inverse(state, element)
182 } -> std::same_as<sil::exterior::Form<std::array<double, Law::N>>>;
183 {
184 law.inverse_value(state, element)
185 } -> std::same_as<std::array<double, Law::N * Law::N>>;
186 };
187
188} // namespace similie::physics
Differential-form coefficients, pointwise or sampled over a spatial domain.
Definition form.hpp:32