-
Notifications
You must be signed in to change notification settings - Fork 0
/
stan_test.cpp
186 lines (135 loc) · 4.85 KB
/
stan_test.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
#include <stan/math.hpp>
#include <iostream>
#include <cmath>
#include "util.hpp"
#include "polybasis.hpp"
#include "mechanics.hpp"
#include "stan_mech.hpp"
const double X = 0.007753,
Y = 0.009057,
Z = 0.013199;
const double density = 4401.695921;
int P = 10;
int N = 30;
ArrayXd data(N, 1);
using namespace Eigen;
using namespace stan::math;
double tol = 1e-3;
double fdtol = 1e-3;
int main() {
data << 109.076, 136.503, 144.899, 184.926, 188.476, 195.562,
199.246, 208.46 , 231.22 , 232.63 , 239.057, 241.684,
242.159, 249.891, 266.285, 272.672, 285.217, 285.67 ,
288.796, 296.976, 301.101, 303.024, 305.115, 305.827,
306.939, 310.428, 318. , 319.457, 322.249, 323.464;
Matrix<double, Dynamic, 1> lookup = rus_namespace::mech_init(P, X, Y, Z, density, NULL);
Matrix<var, Dynamic, 1> vec(3);
Matrix<var, Dynamic, Dynamic> C(6, 6);
var c11 = 1.6,
a = 1.0,
c44 = 0.446;
var c12 = -(c44 * 2.0 / a - c11);
C << c11, c12, c12, 0.0, 0.0, 0.0,
c12, c11, c12, 0.0, 0.0, 0.0,
c12, c12, c11, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, c44, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, c44, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, c44;
double delta = 0.0001;
auto v1 = rus_namespace::mech_rus(N, lookup, C, NULL);
VectorXd ref(N);
ref << 108.47023141, 135.00692667, 143.37988477, 183.83960174,
186.95853441, 194.85994545, 198.32368438, 207.20001131,
229.56350053, 231.65836965, 237.96801498, 241.04923862,
241.28543212, 248.26841215, 266.09547338, 271.28146467,
283.99770118, 284.43249363, 287.6396657, 295.07029835,
298.86837277, 301.45603222, 301.74432735, 303.1395348,
303.78519859, 307.95540376, 316.03191991, 316.92235217,
319.99504521, 321.69228994;
bool failed = false;
for(int i = 0; i < N; i++) {
std::cout << "Computed: " << v1(i).val() << ", reference: " << ref(i) << std::endl;
if(std::abs(v1(i).val() - ref(i)) / ref(i) > tol) {
failed = true;
break;
}
}
if(failed)
std::cout << "Failed freq check" << std::endl;
else
std::cout << "Passed freq check" << std::endl;
/*int ij = 0;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < i + 1; j++) {
Matrix<var, Dynamic, Dynamic> Ct = stan::math::value_of(C);
Ct(i, j) = Ct(i, j) + delta;
Ct(j, i) = Ct(i, j);
auto v2 = rus_namespace::mech_rus(N, lookup, Ct, NULL);
failed = false;
for(int n = 0; n < N; n++) {
set_zero_all_adjoints();
v2(n).grad();
var rel = std::abs((Ct(i, j).adj() - (v2(n).val() - v1(n).val()) / delta) / Ct(i, j).adj());
if(rel > fdtol) {
failed = true;
std::cout << Ct(i, j).adj() << " " << (v2(n) - v1(n)) / delta << std::endl;
std::cout << rel << " " << fdtol << std::endl;
break;
}
}
if(failed)
std::cout << "Failed fd gradient check (" << i << ", " << j << ")" << std::endl;
else
std::cout << "Passed fd gradient check (" << i << ", " << j << ")" << std::endl;
//std::cout << "========" << std::endl;
ij++;
}
}*/
failed = false;
VectorXd refc11(N);
refc11 << 1.58096611e-02, 5.92606098e+00, 6.52767552e+00,
1.33702240e+00, 4.99569554e+00, 1.65364703e-01,
1.85331828e+00, 2.86127299e+00, 1.53803563e+01,
1.26443548e+00, 3.89031218e+00, 4.88030577e+00,
4.73043451e+00, 9.66712421e+00, 2.58175197e+00,
3.32103660e+00, 1.18496950e+00, 3.98949689e+00,
6.95709878e+00, 8.84615784e+00, 2.03038423e+01,
9.48756016e+00, 2.89992732e+01, 1.07677790e+01,
1.91150986e+01, 1.53958549e+01, 4.79186147e+00,
2.31886719e+01, 1.34852308e+01, 5.27677204e+00;
for(int i = 0; i < N; i++) {
set_zero_all_adjoints();
v1(i).grad();
std::cout << c11.adj() << ", " << refc11(i) << std::endl;
if(std::abs((c11.adj() - refc11(i)) / c11.adj()) > tol) {
failed = true;
break;
}
}
if(failed)
std::cout << "Failed c11 reference gradient check" << std::endl;
else
std::cout << "Passed c11 reference gradient check" << std::endl;
failed = false;
VectorXd refc12(N);
refc12 << -0.503395, -48.0169, -49.5532, -7.44497,
-95.8234, -5.91993, -17.295, -24.7766, -59.6339,
-22.8485, -120.672, -101.35, -5.10607, -67.289,
-144.439, -63.3056, -143.012, -91.2816, -10.6521,
-79.2997, -66.982, -40.6559, -91.408, -46.5387,
-84.0838, -62.4809, -154.166, -127.356, -105.808,
-51.7811;
for(int i = 0; i < N; i++) {
set_zero_all_adjoints();
v1(i).grad();
std::cout << c12.adj() << ", " << refc12(i) << std::endl;
if(std::abs((c12.adj() - refc12(i)) / c12.adj()) > tol) {
failed = true;
break;
}
}
if(failed)
std::cout << "Failed c12 reference gradient check" << std::endl;
else
std::cout << "Passed c12 reference gradient check" << std::endl;
}