Tatooine
lagrangian_Q_field.h
Go to the documentation of this file.
1#ifndef TATOOINE_LAGRANGIAN_Q_FIELD_H
2#define TATOOINE_LAGRANGIAN_Q_FIELD_H
3//==============================================================================
4#include <tatooine/field.h>
5#include <tatooine/line.h>
7#include <tatooine/Q_field.h>
8//==============================================================================
9namespace tatooine {
10//==============================================================================
11template <typename V, size_t N>
13 : public scalarfield<lagrangian_Q_field<V, N>, typename V::real_type, N> {
14 //============================================================================
15 // typedefs
16 //============================================================================
17 public:
18 using real_type = typename V::real_type;
21 field<this_type, real_type, V::num_dimensions()>;
23 using typename parent_type::pos_type;
24 using typename parent_type::tensor_type;
26 //============================================================================
27 // fields
28 //============================================================================
29 private:
30 V const& m_v;
32
33 //============================================================================
34 // ctor
35 //============================================================================
36 public:
37 template <typename Real>
39 arithmetic auto const btau, arithmetic auto const ftau)
40 : m_v{v.as_derived()},
41 m_btau{static_cast<real_type>(btau)},
42 m_ftau{static_cast<real_type>(ftau)} {}
43
44 //============================================================================
45 // methods
46 //============================================================================
47 public:
48 constexpr tensor_type evaluate(const pos_type& x, real_type t) const {
49 parameterized_line<real_type, num_dimensions(), interpolation::linear> pathline;
50 auto const Qf = Q(m_v);
51 ode_solver_t ode_solver;
52 auto evaluator = [this, &Qf](auto const& y, auto const t) ->
53 typename ode_solver_t::maybe_vec {
54 return m_v(y, t);
55 };
56
57 real_type const eps = 1e-6;
58 auto& pathline_Q_prop = pathline.template add_vertex_property<real_type>("Q");
59 if (m_ftau > 0) {
60 ode_solver.solve(evaluator, x, t, m_ftau,
61 [&pathline, &pathline_Q_prop, &Qf, eps](
62 const pos_type& y, real_type t) {
63 auto const Q = Qf(y, t);
64
65 if (pathline.empty()) {
66 pathline.push_back(y, t, false);
67 pathline_Q_prop.back() = Q;
68 }
69 if (distance(pathline.back_vertex(), y) > eps) {
70 pathline.push_back(y, t, false);
71 pathline_Q_prop.back() = Q;
72 }
73 });
74 }
75 if (m_btau < 0) {
76 ode_solver.solve(evaluator, x, t, m_btau,
77 [&pathline, &pathline_Q_prop, &Qf, eps](const vec<double, 3>& y, double t) {
78 auto const Q = Qf(y, t);
79 if (pathline.empty()) {
80 pathline.push_front(y, t, false);
81 pathline_Q_prop.front() = Q;
82 }
83 if (distance(pathline.front_vertex(), y) > eps) {
84 pathline.push_front(y, t, false);
85 pathline_Q_prop.front() = Q;
86 }
87 });
88 }
89
90 auto const t_range = m_ftau - m_btau;
91 auto Q_time = [&](double const threshold) {
92 double Q_time = 0;
93 for (size_t i = 0; i < pathline.vertices().size() - 1; ++i) {
94 typename decltype(pathline)::vertex_idx vi{i};
95 typename decltype(pathline)::vertex_idx vj{i + 1};
96 auto const& t0 = pathline.parameterization_at(i);
97 auto const& t1 = pathline.parameterization_at(i + 1);
98 auto const& Q0 = pathline_Q_prop[vi];
99 auto const& Q1 = pathline_Q_prop[vj];
100 if (Q0 >= threshold && Q1 >= threshold) {
101 Q_time += t1 - t0;
102 } else if (Q0 >= threshold && Q1 < threshold) {
103 auto const t_root =
104 ((t1 - t0) * threshold - Q0 * t1 + Q1 * t0) / (Q1 - Q0);
105 Q_time += t_root - t0;
106 } else if (Q0 < threshold && Q1 >= threshold) {
107 auto const t_root =
108 ((t1 - t0) * threshold - Q0 * t1 + Q1 * t0) / (Q1 - Q0);
109 Q_time += t1 - t_root;
110 }
111 }
112 return Q_time / t_range;
113 };
114 return Q_time(0);
115 }
116 //----------------------------------------------------------------------------
117 constexpr bool in_domain(const pos_type& x, real_type t) const {
118 const real_type eps = 1e-6;
119 return m_v.in_domain(x + vec{eps, 0, 0}, t) &&
120 m_v.in_domain(x - vec{eps, 0, 0}, t) &&
121 m_v.in_domain(x + vec{0, eps, 0}, t) &&
122 m_v.in_domain(x - vec{0, eps, 0}, t) &&
123 m_v.in_domain(x + vec{0, 0, eps}, t) &&
124 m_v.in_domain(x - vec{0, 0, eps}, t);
125 }
126};
127//==============================================================================
128template <typename V, typename Real, size_t N>
129auto lagrangian_Q(const field<V, Real, N, N>& vf, arithmetic auto const btau,
130 arithmetic auto const ftau) {
131 return lagrangian_Q_field<V, N>{vf, btau, ftau};
132}
133//==============================================================================
134} // namespace tatooine
135//==============================================================================
136#endif
Definition: lagrangian_Q_field.h:13
lagrangian_Q_field< V, N > this_type
Definition: lagrangian_Q_field.h:19
typename V::real_type real_type
Definition: lagrangian_Q_field.h:18
constexpr bool in_domain(const pos_type &x, real_type t) const
Definition: lagrangian_Q_field.h:117
lagrangian_Q_field(const vectorfield< V, Real, N > &v, arithmetic auto const btau, arithmetic auto const ftau)
Definition: lagrangian_Q_field.h:38
real_type m_btau
Definition: lagrangian_Q_field.h:31
V const & m_v
Definition: lagrangian_Q_field.h:30
constexpr tensor_type evaluate(const pos_type &x, real_type t) const
Definition: lagrangian_Q_field.h:48
real_type m_ftau
Definition: lagrangian_Q_field.h:31
Definition: concepts.h:33
Definition: algorithm.h:6
constexpr auto distance(Iter const &it0, Iter const &it1)
Definition: iterator_facade.h:372
auto lagrangian_Q(const field< V, Real, N, N > &vf, arithmetic auto const btau, arithmetic auto const ftau)
Definition: lagrangian_Q_field.h:129
auto Q(V &&v)
Definition: Q_field.h:57
Definition: field.h:134
vec< real_type, NumDimensions > pos_type
Definition: field.h:20
Tensor tensor_type
Definition: field.h:18
auto as_derived() -> auto &
Definition: field.h:161
auto num_dimensions() const -> std::size_t
Definition: hdf5.h:764
Definition: interpolation.h:16
Definition: rungekuttafehlberg78.h:28
constexpr void solve(Evaluator &&evaluator, vec< Y0Real, N > const &y0, arithmetic auto const t0, arithmetic auto tau, StepperCallback &&callback) const
Definition: solver.h:47
static constexpr auto num_dimensions() -> std::size_t
Definition: field.h:38
constexpr auto in_domain(const pos_type &, double) const
Definition: symbolic_field.h:50
Definition: vec.h:12