-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHybSys.cpp
More file actions
203 lines (164 loc) · 5.58 KB
/
Copy pathHybSys.cpp
File metadata and controls
203 lines (164 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (C) 2020 Paul Gustafson
* License: MIT (see the file LICENSE)
*/
#include "HybSys.h"
#include <Eigen/Dense>
using namespace std;
Eigen::VectorXd concatE(Eigen::VectorXd a, Eigen::VectorXd b) {
Eigen::VectorXd vec_joined(a.size() + b.size());
vec_joined << a, b;
return vec_joined;
}
template <typename T> vector<T> concat(vector<T> a, vector<T> b) {
vector<T> ret = vector<T>();
copy(a.begin(), a.end(), back_inserter(ret));
copy(b.begin(), b.end(), back_inserter(ret));
return ret;
}
Mode::Mode(int dim, function<Eigen::VectorXd(Eigen::VectorXd)> vectorField) {
this->dim = dim;
this->vectorField = vectorField;
}
Mode Mode::parallel(Mode M, Mode N) {
function<Eigen::VectorXd(Eigen::VectorXd)> vectorField = [&M, &N](Eigen::VectorXd coords) {
Eigen::VectorXd mCoords = Eigen::VectorXd(0, M.dim);
Eigen::VectorXd nCoords = Eigen::VectorXd(M.dim, M.dim + N.dim);
Eigen::VectorXd mAns = M.vectorField(mCoords);
Eigen::VectorXd nAns = N.vectorField(nCoords);
return concatE(mAns, nAns);
};
return Mode(M.dim + N.dim, vectorField);
}
Reset::Reset(function<bool(Eigen::VectorXd)> guard,
function<Eigen::VectorXd(Eigen::VectorXd)> reset) {
this->guard = guard;
this->reset = reset;
}
HybSys::HybSys(vector<Mode> modes, vector<vector<Reset>> resets) {
this->modes = modes;
this->resets = resets;
}
Semiconjugacy::Semiconjugacy(HybSys dom, HybSys cod, vector<int> nodeMap, vector<function<Eigen::VectorXd(Eigen::VectorXd)>> manifoldMap) {
this->dom = dom;
this->cod = cod;
this->modeMap = modeMap;
this->manifoldMap = manifoldMap;
}
TAPair::TAPair(HybSys anchor, HybSys temp, Semiconjugacy asymptoticPhase) {
this->anchor = anchor;
this->temp = temp;
this->asymptoticPhase = asymptoticPhase;
}
HybSys HybSys::parallel(HybSys H, HybSys K) {
vector<Mode> modes (H.modes.size() * K.modes.size());
vector<vector<Reset>> resets (H.modes.size() * K.modes.size());
int n = K.modes.size();
for (int i = 0; i < H.modes.size(); i++) {
for (int j = 0; j < K.modes.size(); j++) {
modes[i*n + j] = Mode::parallel(H.modes[i], K.modes[j]);
}
}
for (int i = 0; i < H.modes.size(); i++) {
for (int j = 0; j < K.modes.size(); j++) {
resets[i*n + j] = vector<Reset> (H.modes.size() * K.modes.size());
for (int k = 0; k < H.modes.size(); k++) {
for (int l = 0; l < K.modes.size(); l++) {
int offset = H.modes[i].dim;
Reset rh = H.resets[i][k];
Reset rk = K.resets[j][l];
function<bool(Eigen::VectorXd)> guard1, guard2, guard3;
function<Eigen::VectorXd(Eigen::VectorXd)> reset1, reset2, reset3;
guard1 = [&offset, &rh, &rk](Eigen::VectorXd x) {
Eigen::VectorXd xh = Eigen::VectorXd(0, offset);
Eigen::VectorXd xk = Eigen::VectorXd(offset, x.size());
return rh.guard(xh) && !rk.guard(xk);
};
reset1 = [&offset, &rh, &rk](Eigen::VectorXd x) {
Eigen::VectorXd xh = Eigen::VectorXd(0, offset);
Eigen::VectorXd xk = Eigen::VectorXd(offset, x.size());
return concatE(rh.reset(xh), xk);
};
resets[i*n + j][k*n + j] = Reset(guard1, reset1);
guard2 = [&offset, &rh, &rk](Eigen::VectorXd x) {
Eigen::VectorXd xh = Eigen::VectorXd(0, offset);
Eigen::VectorXd xk = Eigen::VectorXd(offset, x.size());
return !rh.guard(xh) && rk.guard(xk);
};
reset2 = [&offset, &rh, &rk](Eigen::VectorXd x){
Eigen::VectorXd xh = Eigen::VectorXd(0, offset);
Eigen::VectorXd xk = Eigen::VectorXd(offset, x.size());
return concatE(xh, rk.reset(xk));
};
resets[i*n + j][i*n +l] = Reset(guard2, reset2);
guard3 = [&offset, &rh, &rk](Eigen::VectorXd x){
Eigen::VectorXd xh = Eigen::VectorXd(0, offset);
Eigen::VectorXd xk = Eigen::VectorXd(offset, x.size());
return rh.guard(xh) && rk.guard(xk);
};
reset3 = [&offset, &rh, &rk](Eigen::VectorXd x){
Eigen::VectorXd xh = Eigen::VectorXd(0, offset);
Eigen::VectorXd xk = Eigen::VectorXd(offset, x.size());
return concatE(rh.reset(xh), rk.reset(xk));
};
resets[i*n + j][k*n + l] = Reset(guard3, reset3);
}
}
}
}
return HybSys(modes, resets);
}
/*
* Replace the final mode of H with the first mode of K
*/
HybSys HybSys::sequential(HybSys H, HybSys K) {
H.modes.pop_back();
vector<Mode> modes = concat(H.modes, K.modes);
vector<vector<Reset>> resets (H.modes.size() + K.modes.size() - 1);
for (int i = 0; i < resets.size(); i++) {
resets[i] = vector<Reset> (H.modes.size() + K.modes.size() - 1);
if (i < H.modes.size() - 1) {
resets[i] = H.resets[i];
}
else {
resets[i + H.modes.size() - 1] = K.resets[i - H.modes.size() + 1];
}
}
return HybSys(modes, resets);
}
Reset Reset::either(Reset r1, Reset r2) {
function<bool(Eigen::VectorXd)> guard = [r1, r2](Eigen::VectorXd x) {
return r1.guard(x) || r2.guard(x);
};
function<Eigen::VectorXd(Eigen::VectorXd)> reset = [r1, r2](Eigen::VectorXd x) {
if (r1.guard(x)) {
return r1.reset(x);
}
return r2.reset(x);
};
return Reset(guard, reset);
}
/*
* Identify the final mode and the first mode. Use the first mode's guards/resets
*/
HybSys HybSys::loop(HybSys H) {
H.modes.pop_back();
H.resets.pop_back();
int last = H.modes.size() - 1;
// If you would reset into the final mode, reset into the first mode instead
for (int i = 0; i < H.modes.size(); i++) {
H.resets[i][0] = Reset::either(H.resets[i][0], H.resets[i][last]);
H.resets[i].pop_back();
}
return H;
}
Mode::Mode() {
}
Reset::Reset() {
}
HybSys::HybSys() {
}
Semiconjugacy::Semiconjugacy() {
}
TAPair::TAPair() {
}