codac 2.1.2
Loading...
Searching...
No Matches
codac2_CtcDeriv.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <numeric>
13#include "codac2_Ctc.h"
14#include "codac2_TimePropag.h"
16#include "codac2_TDomain.h"
17
18namespace codac2
19{
20 template<class T>
21 class Slice;
22
23 template<typename T>
24 class SlicedTube;
25
26 class CtcDeriv
27 {
28 public:
29
30 CtcDeriv(const TimePropag& time_propag = TimePropag::FWD_BWD, bool fast_mode = true);
31
32 void restrict_tdomain(const Interval& tdomain)
33 {
34 _tdomain = tdomain;
35 }
36
37 template<typename T>
38 inline void contract(Slice<T>& x, const Slice<T>& v, const std::vector<Index>& ctc_indices = {}) const
39 requires std::is_same_v<T,Interval> || std::is_same_v<T,IntervalVector>
40 {
41 assert(x.size() == v.size());
42 assert(x.t0_tf() == v.t0_tf());
43
44 T input = x.input_gate();
45 T output = x.output_gate();
46 T envelope = x.codomain();
47
48 if constexpr(std::is_same_v<T,Interval>)
49 contract(x.t0_tf(), envelope, input, output, v.codomain(), _time_propag, _fast_mode);
50
51 else if constexpr(std::is_same_v<T,IntervalVector>)
52 {
53 std::vector<Index> ctc_indices_(ctc_indices);
54 if(ctc_indices_.empty())
55 {
56 // Then all the dimensions will be contracted
57 ctc_indices_ = std::vector<Index>(x.size());
58 std::iota(ctc_indices_.begin(), ctc_indices_.end(), 0);
59 }
60
61 for(auto i : ctc_indices_)
62 contract(x.t0_tf(), envelope[i], input[i], output[i], v.codomain()[i], _time_propag, _fast_mode);
63 }
64
65 auto x_next = x.next_slice();
66 if(x_next && x_next->is_gate())
67 x_next->set(output, false);
68
69 auto x_prev = x.prev_slice();
70 if(x_prev && x_prev->is_gate())
71 x_prev->set(input, false);
72
73 x.set(envelope, false);
74 }
75
76 template<typename T>
77 inline void contract(SlicedTube<T>& x, const SlicedTube<T>& v, const std::vector<Index>& ctc_indices = {}) const
78 requires std::is_same_v<T,Interval> || std::is_same_v<T,IntervalVector>
79 {
80 assert_release(TDomain::are_same(x.tdomain(), v.tdomain()));
81 Interval t = x.tdomain()->t0_tf() & _tdomain;
82 if(t.is_empty() || t.is_degenerated())
83 return;
84
85 // Refine only when the restricted domain cuts through a slice.
86 // No explicit gates are created.
87 if(t.lb() > x.tdomain()->t0_tf().lb())
88 x.tdomain()->sample(t.lb(), false);
89 if(t.ub() < x.tdomain()->t0_tf().ub())
90 x.tdomain()->sample(t.ub(), false);
91
92 // Keep boundary values locally to propagate contractions across slices.
93 // Only gates already present in the TDomain are updated afterwards.
94 std::vector<std::shared_ptr<Slice<T>>> sx;
95 std::vector<std::shared_ptr<const Slice<T>>> sv;
96 for(auto it = x.tdomain()->begin(); it != x.tdomain()->end(); ++it)
97 if(!it->is_gate() && it->is_subset(t))
98 {
99 sx.push_back(x.slice(it));
100 sv.push_back(v.slice(it));
101 }
102
103 if(sx.empty())
104 return;
105
106 std::vector<T> gates;
107 gates.reserve(sx.size()+1);
108 gates.push_back(sx.front()->input_gate());
109 for(const auto& s : sx)
110 gates.push_back(s->output_gate());
111
112 auto contract_slice = [&](std::size_t i)
113 {
114 T envelope = sx[i]->codomain();
115
116 if constexpr(std::is_same_v<T,Interval>)
117 contract(sx[i]->t0_tf(), envelope, gates[i], gates[i+1],
118 sv[i]->codomain(), _time_propag, _fast_mode);
119
120 else if constexpr(std::is_same_v<T,IntervalVector>)
121 {
122 std::vector<Index> ctc_indices_(ctc_indices);
123 if(ctc_indices_.empty())
124 {
125 ctc_indices_.resize(sx[i]->size());
126 std::iota(ctc_indices_.begin(), ctc_indices_.end(), 0);
127 }
128
129 for(auto j : ctc_indices_)
130 contract(sx[i]->t0_tf(), envelope[j], gates[i][j], gates[i+1][j],
131 sv[i]->codomain()[j], _time_propag, _fast_mode);
132 }
133
134 sx[i]->set(envelope, false);
135 };
136
137 if((_time_propag & TimePropag::FWD) == TimePropag::FWD)
138 for(std::size_t i = 0 ; i < sx.size() ; i++)
139 contract_slice(i);
140
141 if((_time_propag & TimePropag::BWD) == TimePropag::BWD)
142 for(std::size_t i = sx.size() ; i-- > 0 ; )
143 contract_slice(i);
144
145 // Update only gates explicitly present in the TDomain.
146 // Temporary boundary values are discarded after contraction.
147 for(std::size_t i = 0 ; i < sx.size() ; i++)
148 {
149 auto prev = sx[i]->prev_slice();
150 if(prev && prev->is_gate())
151 prev->set(gates[i], false);
152
153 auto next = sx[i]->next_slice();
154 if(next && next->is_gate())
155 next->set(gates[i+1], false);
156 }
157 }
158
159 static ConvexPolygon polygon_slice(
160 const Interval& t, const Interval& envelope,
161 const Interval& input, const Interval& proj_input,
162 const Interval& output, const Interval& proj_output,
163 const Interval& v);
164
165 protected:
166
167 static void contract(
168 const Interval& t, Interval& envelope,
169 Interval& input, Interval& output,
170 const Interval& v, const TimePropag& time_propag, bool fast_mode);
171
172 const TimePropag _time_propag;
173 const bool _fast_mode;
174 Interval _tdomain;
175 };
176}
Codomain of a sliced tube over one temporal slice.
Definition codac2_Slice.h:42
Tube represented over a sliced temporal domain.
Definition codac2_SlicedTube.h:38
static bool are_same(const std::shared_ptr< const TDomain > &tdom1, const std::shared_ptr< const TDomain > &tdom2)
Tests whether two temporal domains share the same temporal partition.
Definition codac2_OctaSym.h:21
TimePropag
Enumeration specifying the temporal propagation way (forward or backward in time).
Definition codac2_TimePropag.h:23