-
Notifications
You must be signed in to change notification settings - Fork 6
/
pendulum.py
299 lines (262 loc) · 8.76 KB
/
pendulum.py
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
###############################################################################
#
# CSCI 4446 - Chaotic Dynamics
#
# File: pendulum.py
# Author: Ken Sheedlo
#
# Pendulum problems for Problem Set 4.
#
###############################################################################
from __future__ import division
import getopt
import matplotlib.pyplot
import numpy
import rungekutta
import sys
from utils import split_dict, ufunc
PHASE_POINTS = (
(3.0, 0.1),
(2.6, 0.1),
(0.1, 0.0),
(0.2, 0.0),
(-2*numpy.pi, 30.0),
(-2*numpy.pi, 22.0),
(2*numpy.pi, -30.0),
(2*numpy.pi, -22.0),
(numpy.pi/2, 0.0),
)
def pendulum(mass, length, damping, ampl=0.0, freq=0.0):
'''
Creates a pendulum function.
Parameters (all types are 64-bit floating point unless specified otherwise):
mass The mass of the simulated pendulum.
length The pendulum's length.
damping The damping coefficient.
ampl The drive amplitude (defaults to 0.0).
freq The drive frequency.
Returns: a callable pfunc(t, x) that returns the value of the derivative at
t, x.
'''
def _pendulum(t, xvec):
'''
Pendulum callable function.
'''
theta = xvec[0]
omega = xvec[1]
omega_dot = (ampl*numpy.cos(freq*t) - damping*length*omega -
mass*9.8*numpy.sin(theta)) / (mass*length)
return numpy.array([omega, omega_dot], dtype=numpy.float64)
return _pendulum
def mod2pi(theta):
modulus = int(numpy.floor(theta / (2*numpy.pi)))
return theta - (modulus*2*numpy.pi)
def render_plot(ts, xs, *args, **kwargs):
'''
Renders a plot to the screen or to a file.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict((
'xlabel', 'ylabel', 'mod2pi', 'title', 'file_prefix'
), kwargs)
title = opts.get('title', '$x(t)$')
file_prefix = opts.get('file_prefix')
modulo = opts.get('mod2pi', False)
axes.plot(ts, xs, *args, **plot_args)
axes.set_xlabel(opts.get('xlabel', 't'))
axes.set_ylabel(opts.get('ylabel', 'x'))
axes.set_title(title)
if modulo:
axes.set_xbound(0, 2*numpy.pi)
axes.set_xticks((
0,
numpy.pi/2,
numpy.pi,
3*numpy.pi/2,
2*numpy.pi
))
axes.set_xticklabels((
'0',
r'$\frac{\pi}{2}$',
r'$\pi$',
r'$\frac{3\pi}{2}$',
r'$2\pi$'
))
if file_prefix is None:
figure.show()
else:
figure.savefig('{0}.png'.format(file_prefix), dpi=220)
def make_phase_portrait(pfunc, *args, **kwargs):
'''
Constructs a phase portrait of the system.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict(('title', 'file_prefix'), kwargs)
file_prefix = opts.get('file_prefix')
title = opts.get('title')
for (theta, omega) in PHASE_POINTS:
_, xs = rungekutta.rk4(
pfunc,
0.0,
numpy.array([theta, omega], dtype=numpy.float64),
0.005,
2000
)
axes.plot(xs[0,:], xs[1,:], *args, **plot_args)
axes.set_xlabel(r'$\theta$')
axes.set_ylabel(r'$\omega$')
axes.set_xbound(-3*numpy.pi/2, 3*numpy.pi/2)
axes.set_xticks((
-3*numpy.pi/2,
-numpy.pi,
-numpy.pi/2,
0,
numpy.pi/2,
numpy.pi,
3*numpy.pi/2
))
axes.set_xticklabels((
r'$-\frac{3\pi}{2}$',
r'$-\pi$',
r'$-\frac{\pi}{2}$',
'0',
r'$\frac{\pi}{2}$',
r'$\pi$',
r'$\frac{3\pi}{2}$'
))
axes.set_title('Phase Portrait' if title is None else title)
if file_prefix is None:
figure.show()
else:
figure.savefig('{0}.png'.format(file_prefix), dpi=220)
def make_phase_portrait_mod2pi(pfunc, *args, **kwargs):
'''
Makes a phase portrait modulo 2*pi.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict(('title', 'file_prefix'), kwargs)
file_prefix = opts.get('file_prefix')
title = opts.get('title')
for (theta, omega) in PHASE_POINTS:
_, xs = rungekutta.rk4(
pfunc,
0.0,
numpy.array([theta, omega], dtype=numpy.float64),
0.005,
2000
)
xs[0,:] = numpy.array([
mod2pi(theta) for theta in xs[0,:]
], dtype=numpy.float64)
axes.plot(xs[0,:], xs[1,:], *args, **plot_args)
axes.set_xlabel(r'$\theta$')
axes.set_ylabel(r'$\omega$')
axes.set_xbound(0, 2*numpy.pi)
axes.set_xticks((
0,
numpy.pi/2,
numpy.pi,
3*numpy.pi/2,
2*numpy.pi
))
axes.set_xticklabels((
'0',
r'$\frac{\pi}{2}$',
r'$\pi$',
r'$\frac{3\pi}{2}$',
r'$2\pi$'
))
axes.set_title('Phase Portrait' if title is None else title)
if file_prefix is None:
figure.show()
else:
figure.savefig('{0}.png'.format(file_prefix), dpi=220)
def plot_pfunc(pfunc, *args, **kwargs):
'''
Convenience function for experimenting with pendulum functions.
'''
opts, plot_args = split_dict(('tstep', 'theta0', 'omega0', 'nsteps'), kwargs)
theta0 = opts.get('theta0', 3.0)
omega0 = opts.get('omega0', 0.1)
tstep = opts.get('tstep', 0.005)
nsteps = opts.get('nsteps', 2000)
_, xs = rungekutta.rk4(
pfunc,
0.0,
numpy.array([theta0, omega0], dtype=numpy.float64),
tstep,
nsteps
)
fix_domain = lambda x: mod2pi(x) if kwargs.get('mod2pi', False) else x
xs[0,:] = numpy.array([
fix_domain(theta) for theta in xs[0,:]
], dtype=numpy.float64)
render_plot(xs[0,:], xs[1,:], *args, **plot_args)
def main(argv=None):
if argv is None:
argv = sys.argv
file_prefix = None
try:
options, args = getopt.getopt(argv[1:], 'f:')
for opt, arg in options:
if opt == '-f':
file_prefix = arg
except getopt.GetoptError as err:
print str(err)
return 2
suffixed = lambda s, suf: None if s is None else '{0}{1}'.format(s, suf)
pfunc = pendulum(0.1, 0.1, 0)
ts, xs = rungekutta.rk4(
pfunc,
0.0,
numpy.array([3.0, 0.1], dtype=numpy.float64),
0.005,
2000
)
render_plot(xs[0,:],
xs[1,:],
title='Undriven, Undamped Pendulum',
file_prefix=suffixed(file_prefix, '_2a'),
xlabel=r'$\theta$',
ylabel=r'$\omega$'
)
ts1, xs1 = rungekutta.rk4(
pfunc,
0.0,
numpy.array([0.01, 0.0], dtype=numpy.float64),
0.005,
2000
)
render_plot(xs1[0,:],
xs1[1,:],
title='Undriven, Undamped Pendulum',
file_prefix=suffixed(file_prefix, '_2b'),
xlabel=r'$\theta$',
ylabel=r'$\omega$'
)
make_phase_portrait(
pfunc,
'b',
title=r'Phase Portrait ($m=0.1, l=0.1, \beta=0)$',
file_prefix=suffixed(file_prefix, '_3')
)
pfunc2 = pendulum(0.1, 0.1, 0.25)
make_phase_portrait(
pfunc2,
'b',
title=r'Phase Portrait ($m=0.1, l=0.1, \beta=0.25)$',
file_prefix=suffixed(file_prefix, '_4')
)
make_phase_portrait_mod2pi(
pfunc2,
'b.',
title=r'Phase Portrait ($m=0.1, l=0.1, \beta=0.25)$ Mod $2\pi$',
file_prefix=suffixed(file_prefix, '_5'),
markersize=0.6
)
return 0
if __name__ == "__main__":
sys.exit(main())