-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
205 lines (130 loc) · 6.09 KB
/
main.cpp
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
203
204
205
#include "chebyshev_differentiation.h"
#include "utilities.h"
static constexpr unsigned int number_of_Chebyshev_points = 16;
static constexpr unsigned int state_dimension = 4;
static constexpr unsigned int problem_dimension = state_dimension * (number_of_Chebyshev_points-1);
typedef Eigen::Matrix<double, problem_dimension, problem_dimension> MatrixNN;
typedef Eigen::Matrix<double, problem_dimension, 1> VectorNd;
static constexpr unsigned int ne = 3;
static constexpr unsigned int na = 3;
static const auto x = ComputeChebyshevPoints<number_of_Chebyshev_points>();
Eigen::Matrix<double, ne*na, 1> qe;
Eigen::MatrixXd getQuaternionA(Eigen::VectorXd &t_qe) {
constexpr unsigned int probDimension = state_dimension*number_of_Chebyshev_points;
Eigen::Vector3d K;
Eigen::Matrix<double, state_dimension, state_dimension> A_at_chebyshev_point;
// Declare the matrix for the system Ax = b
Eigen::Matrix<double, probDimension, probDimension> A =
Eigen::Matrix<double, probDimension, probDimension>::Zero();
for(unsigned int i=0; i < number_of_Chebyshev_points; i++){
// Extract the curvature from the strain
K = Phi<na, ne>(x[i])*t_qe;
// Compute the A matrix of Q' = 1/2 A(K) Q
A_at_chebyshev_point << 0, -K(0), -K(1), -K(2),
K(0), 0, K(2), -K(1),
K(1), -K(2), 0, K(0),
K(2), K(1), -K(0), 0;
A_at_chebyshev_point = 0.5*A_at_chebyshev_point;
for (unsigned int row = 0; row < A_at_chebyshev_point.rows(); ++row) {
for (unsigned int col = 0; col < A_at_chebyshev_point.cols(); ++col) {
int row_index = row*number_of_Chebyshev_points+i;
int col_index = col*number_of_Chebyshev_points+i;
A(row_index, col_index) = A_at_chebyshev_point(row, col);
}
}
}
return A;
}
void updateA(const Eigen::Matrix<double, ne*ne, 1> &t_qe, MatrixNN &A_NN, const MatrixNN &D_NN)
{
// Define the Chebyshev points on the unit circle
const auto x = ComputeChebyshevPoints<number_of_Chebyshev_points>();
Eigen::Vector3d K;
Eigen::Matrix<double, state_dimension, state_dimension> A_at_chebyshev_point;
unsigned int left_corner_row;
unsigned int left_corner_col;
for(unsigned int i=0; i<x.size()-1; i++){
// Extract the curvature from the strain
K = Phi<na, ne>(x[i])*t_qe;
// Compute the A matrix of Q' = 1/2 A(K) Q
A_at_chebyshev_point << 0, -K(0), -K(1), -K(2),
K(0), 0, K(2), -K(1),
K(1), -K(2), 0, K(0),
K(2), K(1), -K(0), 0;
for (unsigned int row = 0; row < state_dimension; ++row) {
for (unsigned int col = 0; col < state_dimension; ++col) {
int row_index = row*(number_of_Chebyshev_points-1)+i;
int col_index = col*(number_of_Chebyshev_points-1)+i;
A_NN(row_index, col_index) = D_NN(row_index, col_index) - 0.5*A_at_chebyshev_point(row, col);
}
}
}
}
Eigen::VectorXd integrateQuaternions()
{
const Eigen::MatrixXd Dn = getDn<number_of_Chebyshev_points>();
const Eigen::MatrixXd Dn_NN = Dn.block<number_of_Chebyshev_points-1, number_of_Chebyshev_points-1>(0, 0);
const Eigen::MatrixXd Dn_IN = Dn.block<number_of_Chebyshev_points-1, 1>(0, number_of_Chebyshev_points-1);
const Eigen::MatrixXd D = Eigen::KroneckerProduct(Eigen::MatrixXd::Identity(state_dimension, state_dimension), Dn);
const MatrixNN D_NN = Eigen::KroneckerProduct(Eigen::MatrixXd::Identity(state_dimension, state_dimension), Dn_NN);
const Eigen::MatrixXd D_IN = Eigen::KroneckerProduct(Eigen::MatrixXd::Identity(state_dimension, state_dimension), Dn_IN);
MatrixNN A_NN = D_NN;
updateA(qe, A_NN, D_NN);
Eigen::VectorXd q_init(4);
q_init << 1, 0, 0, 0;
Eigen::VectorXd ivp = D_IN*q_init;
const auto b = VectorNd::Zero();
Eigen::VectorXd Q_stack = A_NN.inverse() * (b - ivp);
return Q_stack;
}
Eigen::Matrix<double, number_of_Chebyshev_points-1, 3> updatePositionb(Eigen::MatrixXd t_Q_stack) {
Eigen::Matrix<double, number_of_Chebyshev_points-1, 3> b;
Eigen::Quaterniond q;
for (unsigned int i = 0; i < number_of_Chebyshev_points-1; ++i) {
q = { t_Q_stack(i),
t_Q_stack(i + (number_of_Chebyshev_points-1)),
t_Q_stack(i + 2*(number_of_Chebyshev_points-1)),
t_Q_stack(i + 3*(number_of_Chebyshev_points-1)) };
b.block<1,3>(i, 0) = (q.toRotationMatrix()*Eigen::Vector3d(1, 0, 0)).transpose();
}
return b;
}
Eigen::MatrixXd integratePosition()
{
const auto Q_stack = integrateQuaternions();
Eigen::Matrix<double, number_of_Chebyshev_points-1, 3> b_NN;
Eigen::Vector3d r_init;
r_init << 0,
0,
0;
const Eigen::MatrixXd Dn = getDn<number_of_Chebyshev_points>();
const Eigen::MatrixXd Dn_NN = Dn.block<number_of_Chebyshev_points-1, number_of_Chebyshev_points-1>(0, 0);
const auto Dn_NN_inv = Dn_NN.inverse();
const Eigen::MatrixXd Dn_IN = Dn.block<number_of_Chebyshev_points-1, 1>(0, number_of_Chebyshev_points-1);
Eigen::Matrix<double, number_of_Chebyshev_points-1, 3> ivp;
for(unsigned int i=0; i<ivp.rows(); i++)
ivp.row(i) = Dn_IN(i, 0) * r_init.transpose();
Eigen::Matrix<double, number_of_Chebyshev_points-1, 3> r_stack;
b_NN = updatePositionb(Q_stack);
r_stack = Dn_NN_inv*(b_NN - ivp);
return r_stack;
}
int main(int argc, char *argv[])
{
// Here we give some value for the strain
// qe.setZero();
qe << 0,
0,
0,
1.2877691307032,
-1.63807499160786,
0.437406679142598,
0,
0,
0;
const auto Q_stack = integrateQuaternions();
std::cout << "Q_stack : \n" << Q_stack << std::endl;
const auto r_stack = integratePosition();
std::cout << "r_stack : \n" << r_stack << std::endl;
return 0;
}