-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmdsim.cpp
More file actions
341 lines (298 loc) · 9.96 KB
/
Copy pathsmdsim.cpp
File metadata and controls
341 lines (298 loc) · 9.96 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/***************************************************************************
** **
** Spring Mass Damper vibration solution **
** Copyright (C) 2015 James Longino **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: James Longino **
** Contact: shot5114@gmail.com ** ** **
****************************************************************************/
#include <QVector>
#include <cmath>
#include <algorithm>
#include <functional>
#include "smdsim.hpp"
#include "tools/num_methods/numeric_algs.hpp"
#include "tools/num_methods/integrators.hpp"
using std::vector;
using std::sqrt;
using std::atan2;
using std::pow;
using std::exp;
using std::transform;
using std::minus;
SMDSim::SMDSim()
{
}
SMDSim::~SMDSim()
{
}
void SMDSim::smdICSol(const std::string &damp_type)
{
//array of function pointers
m_funcs.push_back(x1Dot);
m_funcs.push_back(x2Dot);
//initialize the simulation
initICSimulation(damp_type);
//Time vector
m_dt = 0.001;
double t_end = 3;
vector<double> t_vec = timeVector(t_end, m_dt);
//solution loop
vector<double> x1_sol;
vector<double> x2_sol;
vector<double> temp;
for (auto e : t_vec)
{
x1_sol.push_back(m_state_vec[m_x1_ind]);
x2_sol.push_back(m_state_vec[m_x2_ind]);
temp = rk4integrator(m_funcs, m_state_vec, m_dt, *this);
m_state_vec = temp;
}
vector<double> x1_exact_sol;
vector<double> x2_diff_sol;
exactICSol(x1_exact_sol, x2_diff_sol, t_vec, damp_type);
plotICsol(x1_sol, x2_sol, x1_exact_sol, x2_diff_sol, t_vec);
}
void SMDSim::initICSimulation(const std::string &damp_type)
{
m_state_vec = {};
//system parameters
m_mass = 1;
m_stiffness = 1000;
m_damping = 0;
//Initial conditions
double x1_0 = 0.5; //[m]
double x2_0 = 0.0; //[m/s]
m_state_vec.push_back(x1_0);
m_state_vec.push_back(x2_0);
setDamping(damp_type);
}
/**
* sets the damping value in m_sys_params to achieve the requested damping
* @brief SMDSim::determinDamping
* @param damp_type: Valid inputs: "under", "crit", "over" anything else will default to criticaly damped
* @param m_sys_params
*/
void SMDSim::setDamping(const std::string &damp_type)
{
m_exact.k = m_stiffness;
m_exact.m = m_mass;
m_exact.omega_n = std::sqrt(m_exact.k/m_exact.m);
// find c so that zeta = 1;
double c = 2 * m_exact.omega_n * m_exact.m;
if (damp_type == "under"){
m_damping = c / 10;
m_damp_type = "Under_Damped";
} else if (damp_type == "crit") {
m_damping = c;
m_damp_type = "Critically_Damped";
} else if (damp_type == "over") {
m_damping = c * 2;
m_damp_type = "Over_Damped";
} else
setDamping("crit");
m_exact.c = m_damping;
}
/**
* @brief SMDSim::plotData
* @param plot_data
*/
void SMDSim::plotData(const FigureData &plot_data, const std::string &file_name)
{
Figure* fig = new Figure();
fig->plotData(plot_data);
fig->show();
if (!file_name.empty()){
QString temp = QString::fromStdString(file_name);
fig->saveImage(temp);
}
}
/**
* @brief SMDSim::exactICSol
* @param x1_exact_sol
* @param x2_diff_sol
* @param t_vec
*/
void SMDSim::exactICSol(vector<double> &x1_exact_sol, vector<double> &x2_diff_sol,
const vector<double> &t_vec, const std::string &damp_type)
{
initICSimulation(damp_type);
double x0 = m_state_vec[m_x1_ind];
double v0 = m_state_vec[m_x2_ind];
m_exact.zeta = m_exact.c / (2*m_exact.omega_n*m_exact.m);
m_exact.omega_d = m_exact.omega_n * sqrt(1 - m_exact.zeta * m_exact.zeta);
double temp1 = x0 * m_exact.omega_d;
double temp2 = v0 +x0*m_exact.zeta*m_exact.omega_n;
double (SMDSim::*exactSol)(double);
if (m_exact.zeta<1)
{
double temp = pow(temp1, 2) + pow(temp2, 2);
m_exact.amp = sqrt(temp)/m_exact.omega_d;
m_exact.phi = atan2(temp1, temp2);
exactSol = &SMDSim::underDampedSol;
}
else if (m_exact.zeta == 1)
{
m_exact.crit_c1 = x0;
m_exact.crit_c2 = temp2;
exactSol = &SMDSim::criticallyDampedSol;
}
else
{
double temp = sqrt(pow(m_exact.zeta,2) - 1) ;
double temp3 = temp + m_exact.zeta;
temp3 = x0 * m_exact.omega_n * temp3 + v0;
double temp4 = 2 * m_exact.omega_n * temp;
m_exact.over_c1 = temp3 / temp4;
temp3 = temp - m_exact.zeta;
temp3 = x0 * m_exact.omega_n * temp3 - v0;
m_exact.over_c2 = temp3/temp4;
exactSol = &SMDSim::overDampedSol;
}
for (auto e: t_vec)
{
m_exact.exp_power = -m_exact.zeta * m_exact.omega_n * e;
double temp = (this->*exactSol)(e);
x1_exact_sol.push_back(temp);
}
x2_diff_sol = derivative(x1_exact_sol, m_dt);
}
/**
* @brief SMDSim::underDampedSol
* @param t
* @return
*/
double SMDSim::underDampedSol(double t)
{
double temp = m_exact.omega_d * t + m_exact.phi;
double x = m_exact.amp*exp(m_exact.exp_power)*sin(temp);
return x;
}
/**
* @brief SMDSim::criticallyDampedSol
* @param t
* @return
*/
double SMDSim::criticallyDampedSol(double t)
{
double temp1 = m_exact.crit_c1*exp(m_exact.exp_power);
double temp2 = m_exact.crit_c2*t*exp(m_exact.exp_power);
double x = temp1 + temp2;
return x;
}
/**
* @brief SMDSim::overDampedSol
* @param t
* @return
*/
double SMDSim::overDampedSol(double t)
{
double temp1 = exp(m_exact.exp_power);
double temp2 = m_exact.omega_n * sqrt(pow(m_exact.zeta,2) - 1) * t;
double temp3 = m_exact.over_c1 * exp(temp2);
double temp4 = m_exact.over_c2 * exp(-temp2);
double x = temp1 * (temp3 + temp4);
return x;
}
// * @param x1_exact_sol
/**
* @brief SMDSim::plotICsol
* @param x1_sol
* @param x2_sol
* @param x1_exact_sol
* @param x2_diff_sol
* @param t_vec
*/
void SMDSim::plotICsol(const vector<double>& x1_sol, const vector<double> &x2_sol,
const vector<double> &x1_exact_sol, const vector<double> &x2_diff_sol,
const vector<double> &t_vec)
{
FigureData plot_data1;
plot_data1.addPlotData(x1_sol);
plot_data1.addPlotData(x1_exact_sol);
std::vector<double> error(x1_sol.size());
transform(x1_sol.begin(), x1_sol.end(), x1_exact_sol.begin(), error.begin(), minus<double>());
plot_data1.addPlotData(error);
vector<vec_size_t> plot0_ind{0, 1};
vector<vec_size_t> plot1_ind{2};
plot_data1.addTVec(t_vec);
plot_data1.addPlotInds(plot0_ind);
plot_data1.addPlotInds(plot1_ind);
plot_data1.addXLabel("");
plot_data1.addXLabel("Time (s)");
plot_data1.addYLabel("Pos (m)");
plot_data1.addYLabel("Pos Error (m)");
plot_data1.addLegend("Sim");
plot_data1.addLegend("Exact");
std::string zetaStr = toStringWPrecision(m_exact.zeta);
std::string ttl1 = "Position Sol, \u03B6 = " + zetaStr;
plot_data1.addTitle(ttl1);
FigureData plot_data2;
plot_data2.addPlotData(x2_sol);
plot_data2.addPlotData(x2_diff_sol);
plot_data2.addTVec(t_vec);
error = std::vector<double>(x2_sol.size());
transform(x1_sol.begin(), x1_sol.end(), x1_exact_sol.begin(), error.begin(), minus<double>());
plot_data2.addPlotData(error);
plot_data2.addPlotInds(plot0_ind);
plot_data2.addPlotInds(plot1_ind);
plot_data2.addXLabel("");
plot_data2.addXLabel("Time (s)");
plot_data2.addYLabel("Vel (m/s)");
plot_data2.addYLabel("Vel Error (m/s)");
plot_data2.addLegend("Sim");
plot_data2.addLegend("Exact");
std::string ttl2 = "Velocity Sol, \u03B6 = " + zetaStr;
plot_data2.addTitle(ttl2);
std::string path = "/home/james/Documents/SpringMassDamper/doc/images";
std::string file_name1 = path + "/" + m_damp_type + "_Pos.png";
std::string file_name2 = path + "/" + m_damp_type + "_Vel.png";
plotData(plot_data1, file_name1);
plotData(plot_data2, file_name2);
}
// Non class member double tfunctions
/**
* @brief x1Dot
* @param inputdouble t
* @param state_vector
* @param sys_params
* @return
*/
double x1Dot(const vector<double> &state_vector, const SMDSim &sim_class)
{
double x2 = state_vector[sim_class.m_x2_ind];
double x1_dot = x2;
return x1_dot;
}
/**
* @brief x2Dotfriend x1Dot(const vector<double> &state_vec, const SMDSim &sim_class);
* @param input
* @param state_vector
* @param sys_params
* @return
*/
double x2Dot(const vector<double> &state_vector, const SMDSim &sim_class)
{
double x1 = state_vector[sim_class.m_x1_ind];
double x2 = state_vector[sim_class.m_x2_ind];
double m = sim_class.m_mass;
double c = sim_class.m_damping;
double k = sim_class.m_stiffness;
double F = 0;
double x2_dot = - c/m * x2 -k/m * x1 + F/m;
return x2_dot;
}