-
Notifications
You must be signed in to change notification settings - Fork 1
/
example1.html
314 lines (286 loc) · 10 KB
/
example1.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jsRK4 Example 1</title>
<script type="text/javascript">
// Ideal mass-spring-damper system characteristics
var m = 2.0; // mass
var k = 1.0; // spring constant
var c = 0.5; // damping coefficient
var gflag = false; // enable gravitational acceleration?
var omegan = Math.sqrt(k/m); // natural frequency
var zeta = c/(2.0*m*omegan); // damping ratio
// Characteristic equation: a + C1*v + C2*x = g, where:
// a = acceleration
// v = velocity
// x = displacement
// g = gravitational acceleration
// C1 = 2*zeta*omega0 = c/m
// C2 = omega0*omega0 = k/m
var g = (gflag == true) ? 9.81 : 0.0;
var C1 = (c/m);
var C2 = (k/m);
var xSS = g*(m/k); // steady-state solution to x,
// (i.e., x when v=0 and a=0)
var tmax = 50.0; // maxmimum integration time
var tinc = 1.0/50.0; // integration step size
var n = 3; // number of elements in state vector
var S = new Array(n); // state vector
var S0 = new Array(n); // initial state vector
S0 = [0.0,50.0,-10.0]; // initial conditions [t0,x0,v0]
function dotS(n,S) {
x = S[1];
v = S[2];
dS = new Array(n);
dS[0] = 1.0;
dS[1] = v;
dS[2] = g - (C2*x + C1*v);
return dS;
};
function rk4sub(h,n,S0,dS) {
var S = new Array(n);
for (var i = 0; i < n; i++) {
S[i] = S0[i] + dS[i]*h;
};
return S;
};
function rk4(h, n, S0, dotS) {
// Runge-Kutta 4th order integration method.
// h = integration step size
// n = number of elements in state vector
// S0 = state vector at t0 (i.e. [t0,x0,v0])
// dotS = function(n,state) to integrate
hh = 0.5*h;
K1 = dotS(n,S0);
K2 = dotS(n,rk4sub(hh,n,S0,K1));
K3 = dotS(n,rk4sub(hh,n,S0,K2));
K4 = dotS(n,rk4sub(h,n,S0,K3));
h6 = h/6.0;
for (var i = 0; i < n; i++) {
S[i] = S0[i] + (K1[i] + 2.0*(K2[i] + K3[i]) + K4[i])*h6;
};
return S;
};
function stateText(prefix,S) {
t = Math.round(S[0]*100.0)/100.0;
x = Math.round(S[1]*100.0)/100.0;
v = Math.round(S[2]*100.0)/100.0;
text = prefix + " t= " + t + " x= " + x + " v= " + v;
return text;
};
function displaySysProps(m,k,c,omegan,zeta,gflag) {
text = " ";
text = " mass m=" + m + ", ";
text += "spring constant k=" + k + ", ";
text += "damping coefficient c=" + c;
document.getElementById('mass-spring-damping').innerHTML=text;
wn = Math.round(omegan*10000.0)/10000.0;
z = Math.round(zeta*10000.0)/10000.0;
text = " (";
text += "natural frequency= " + wn + ", ";
text += "damping ratio = " + z + ")";
document.getElementById('omegan-zeta').innerHTML=text;
text = (gflag == true) ? "gravity enabled" : "gravity disabled";
document.getElementById('gravity-mode').innerHTML=text;
};
var canvas = null; // graph canvas
var ctx = null; // gpaph context
var xMin = 0.0; // graph x-axis minimum value
var xMax = tmax; // graph x-axis maximum value
var yMin = -60.0; // graph y-axis minimum value
var yMax = 60.0; // graph y-axis maximum value
function depvarLabel(i) {
// returns ith state vector element plot label
return (i == 1) ? "displacement (x) " : "velocity (v) ";
};
function depvarColor(i) {
// returns ith state vector element plot color
return (i == 1) ? '#0000FF' : '#FF0000';
};
function draw_dot(ctx, w, h, r, color) {
ctx.lineWidth = 1;
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(w, h, r, 0.0, 2*Math.PI, true);
ctx.fill();
};
function solveSysDynamics(ctx,width,height,tMin,tMax,yMin,yMax) {
hZero = Math.round(height/2);
tsfac = width/(tMax-tMin);
xsfac = height/(yMax-yMin);
vsfac = height/(yMax-yMin);
function woft(t) {
return Math.round(t*tsfac);
};
function hofx(x) {
return hZero - Math.round(x*xsfac);
};
function hofv(v) {
return hZero - Math.round(v*vsfac);
};
function hofS(i,S) {
return (i == 1) ? hofx(S[1]) : hofv(S[2]) ;
};
// Plot steady-state solution for x.
ctx.save();
ctx.lineWidth = 2;
ctx.strokeStyle = '#FF00FF';
ctx.beginPath();
ctx.moveTo(woft(tMin),hofx(xSS));
ctx.lineTo(woft(tMax),hofx(xSS));
ctx.stroke();
ctx.fillStyle = '#FF00FF';
ctx.textBaseline = 'bottom';
ctx.textAlign = 'center';
ctx.fillText("Steady-State Solution (x when v=0 and a=0)", width/2, height);
ctx.restore();
// Initialize state vector.
S = [S0[0], S0[1], S0[2]];
document.getElementById('rk4first').innerHTML=stateText("Initial state:",S);
// Integrate differential equations of motion and plot results.
for ( i = 1; i < n; i++ ) {
draw_dot(ctx,woft(S[0]),hofS(i,S),0.75,depvarColor(i));
};
while ( S[0] < tmax ) {
S = rk4(tinc, n, S, dotS);
for ( i = 1; i < n; i++ ) {
draw_dot(ctx,woft(S[0]),hofS(i,S),0.75,depvarColor(i));
};
};
document.getElementById('rk4final').innerHTML=stateText("Final state:",S);
};
function labelAxes(ctx,wZero,hZero,xMin,xMax,yMin,yMax,xSfac,ySfac,xdel,ydel) {
ctx.save();
ctx.strokeStyle = '#000000';
ctx.fillStyle = '#000000';
// x-axis (ommiting labels for xMin and Xmax)
ctx.textBaseline = 'top';
ctx.textAlign = 'center';
var x = xMin + xdel;
wmin = Math.round(xMin*xSfac) + wZero;
while (x < xMax) {
w = Math.round((x-xMin)*xSfac) + wmin;
ctx.beginPath();
ctx.moveTo(w, hZero );
ctx.lineTo(w, hZero+5);
ctx.stroke();
ctx.fillText(x.toString(), w, hZero+5);
x = x + xdel;
};
// y-axis (ommiting labels for yMin and yMax)
ctx.textBaseline = 'middle';
ctx.textAlign = 'start';
var y = yMin + ydel;
hmin = Math.round(yMin*ySfac) + hZero;
while (y < yMax) {
h = Math.round((y - yMin)*ySfac) + hmin;
ctx.beginPath();
ctx.moveTo(wZero, h);
ctx.lineTo(wZero+5, h);
ctx.stroke();
ytext = (y > 0.0) ? "+" + y.toString() : y.toString();
ctx.fillText(ytext, wZero+8, h);
y = y + ydel;
};
ctx.restore();
};
function drawAxes(ctx,width,height,xMin,xMax,yMin,yMax,xdel,ydel) {
wZero = 0;
hZero = Math.round(height/2);
xSfac = width/(xMax-xMin);
ySfac = height/(yMin-yMax);
ctx.save();
ctx.lineWidth = 1;
ctx.strokeStyle = '#000000';
// x-axis
ctx.beginPath();
ctx.moveTo(wZero,hZero);
ctx.lineTo(width,hZero);
ctx.stroke();
// y-axis
ctx.beginPath();
ctx.moveTo(wZero,0);
ctx.lineTo(wZero,height);
ctx.stroke();
ctx.restore();
// draw tic mark and labels for x and y axes.
labelAxes(ctx,wZero,hZero,xMin,xMax,yMin,yMax,xSfac,ySfac,xdel,ydel);
};
function draw_graph(ctx,width,height,xMin,xMax,yMin,yMax) {
drawAxes(ctx,width,height,xMin,xMax,yMin,yMax,10.0,10.0);
// Draw legend for plots of state variable solutions.
ctx.save();
ctx.fillStyle = depvarColor(1);
ctx.textBaseline = 'top';
ctx.textAlign = 'end';
ctx.fillText(depvarLabel(1),width,0);
ctx.fillStyle = depvarColor(2);
ctx.textBaseline = 'top';
ctx.textAlign = 'end';
ctx.fillText(depvarLabel(2),width,10);
ctx.restore();
};
function startSysDynamics() {
canvas = document.getElementById('example1');
ctx = canvas.getContext('2d');
width = canvas.width;
height = canvas.height;
solveSysDynamics(ctx,width,height,xMin,xMax,yMin,yMax);
return false;
};
function resetSysDynamics() {
displaySysProps(m,k,c,omegan,zeta,gflag);
// Get canvas dimensions.
canvas = document.getElementById('example1');
ctx = canvas.getContext('2d');
width = canvas.width;
height = canvas.height;
// Clear canvas and draw graph.
ctx.clearRect(0, 0, width, height);
draw_graph(ctx,width,height,xMin,xMax,yMin,yMax);
// Clear results display.
document.getElementById('rk4first').innerHTML="";
document.getElementById('rk4final').innerHTML="";
};
function initExample1() {
canvas = document.getElementById('example1');
ctx = canvas.getContext('2d');
resetSysDynamics();
};
function toggleGravity() {
gflag = (gflag == true) ? false : true;
g = (gflag == true) ? 9.81 : 0.0;
xSS = g*(m/k);
document.getElementById("g_button").value = (gflag==true) ? "Disable" : "Enable";
resetSysDynamics();
};
</script>
</head>
<body onLoad="initExample1()" style="margin: 10px;">
<div style="width: 90%">
<div style="float: right; width: 300px; height: 300px; padding-left: 10px;">
<canvas id="example1" width="300" height="300" style="border: 1px solid"></canvas>
<b> System Dynamic Response</b>
</div>
<p>
A graph, for plotting the dynamic response of a vertical mass-spring-damper
system, should appear in the canvas area to the right if this page is displayed
with an HTML5 capable and JavaScript enabled browser such as Firefox (v 3.6 or
higher) or Opera (v 10.6 or higher).
</p>
Runge-Kutta 4th order integration method applied to an ideal vertical mass-spring-damper
system defined by the 2nd order linear differential equation
<span style="white-space: nowrap;">a = g - (k/m)*x - (c/m)*v</span>
with <span id="gravity-mode"></span> and:<br><br>
<div id="mass-spring-damping"></div>
<div id="omegan-zeta"></div>
<br>
<input id="start" type="button" value="Start" onclick="startSysDynamics();"> Simulation
<input id="g_button" type="button" value="Enable" onclick="toggleGravity();"> Gravity (resets simulation)
</div>
<br>
<div id="rk4first"></div>
<div id="rk4final"></div>
</body>
</html>