-
Notifications
You must be signed in to change notification settings - Fork 6
/
ps6.py
193 lines (174 loc) · 5.58 KB
/
ps6.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
###############################################################################
#
# CSCI 4446 - Chaotic Dynamics
#
# File: ps6.py
# Author: Ken Sheedlo
#
# Super important PS6 program / frobozz kernel extensions
#
###############################################################################
from __future__ import division
import getopt
import numpy
import pendulum
import plot
import poincare
import rungekutta
import sys
def _suffixed(word, suf):
return None if word is None else '{0}{1}'.format(word, suf)
def pr1a(file_prefix):
pfunc = pendulum.pendulum(0.1, 0.1, 0)
ts, xs = rungekutta.rk4(
pfunc,
0.0,
numpy.array([0.01, 0.0], dtype=numpy.float64),
0.005,
80000
)
points = xs.transpose()
ps = poincare.section(ts, points, interval=0.6346975625940523)
plot.render(
ps[:,0],
ps[:,1],
'k.',
xbound=(-0.015, 0.015),
ybound=(-0.15, 0.15),
xlabel=r'$\theta$',
ylabel=r'$\omega$',
markersize=0.6,
title='Poincare Section (Natural Frequency)',
file_prefix=_suffixed(file_prefix, '_1a')
)
return ts, points
def pr1b(file_prefix, ts, points):
ps2 = poincare.section(ts, points, interval=0.51)
plot.render(
ps2[:,0],
ps2[:,1],
'k.',
xbound=(-0.015, 0.015),
ybound=(-0.15, 0.15),
xlabel=r'$\theta$',
ylabel=r'$\omega$',
markersize=0.6,
title='Poincare Section (Irrational Frequency)',
file_prefix=_suffixed(file_prefix, '_1b')
)
def pr1c(file_prefix):
drive_freq = 7.4246
pfunc2 = pendulum.pendulum(0.1, 0.1, 0.25, ampl=1, freq=drive_freq)
ts2, xs2 = rungekutta.rk4(
pfunc2,
0.0,
numpy.array([3.0, 0.1], dtype=numpy.float64),
0.005,
1000000
)
xs2[0,:] = numpy.array(
[pendulum.mod2pi(x) for x in xs2[0,:]],
dtype=numpy.float64
)
points2 = xs2.transpose()
ps3 = poincare.section(ts2, points2, interval=2*numpy.pi/drive_freq)
plot.mod2pi(
ps3[:,0],
ps3[:,1],
'k.',
xlabel=r'$\theta$',
ylabel=r'$\omega$',
markersize=0.6,
title='Poincare Section (Chaotic Trajectory)',
file_prefix=_suffixed(file_prefix, '_1c')
)
def pr1d(file_prefix):
drive_freq = 7.4246
pfunc2 = pendulum.pendulum(0.1, 0.1, 0.25, ampl=1, freq=drive_freq)
ts3, xs3 = rungekutta.rk4(
pfunc2,
0.0,
numpy.array([3.0, 0.1], dtype=numpy.float64),
0.02,
250000
)
xs3[0,:] = numpy.array(
[pendulum.mod2pi(x) for x in xs3[0,:]],
dtype=numpy.float64
)
points3 = xs3.transpose()
ps4 = poincare.section(ts3, points3, interval=2*numpy.pi/drive_freq)
plot.mod2pi(
ps4[:,0],
ps4[:,1],
'k.',
xlabel=r'$\theta$',
ylabel=r'$\omega$',
markersize=0.6,
title='Poincare section, increased step size',
file_prefix=_suffixed(file_prefix, '_1d')
)
def pr2a(file_prefix):
pr2x(file_prefix, 0.005, 1000000, 'Poincare section, linear interpolation', '_2a')
def pr2b(file_prefix):
pr2x(file_prefix, 0.02, 250000, 'Poincare section, increased step size', '_2b')
def pr2x(file_prefix, step, nsteps, title, suffix):
drive_freq = 7.4246
pfunc = pendulum.pendulum(0.1, 0.1, 0.25, ampl=1.0, freq=drive_freq)
ts, xs = rungekutta.rk4(
pfunc,
0.0,
numpy.array([3.0, 0.1], dtype=numpy.float64),
step,
nsteps
)
xs[0,:] = numpy.array(
[pendulum.mod2pi(x) for x in xs[0,:]],
dtype=numpy.float64
)
points = xs.transpose()
ps = poincare.linear(ts, points, interval=2*numpy.pi/drive_freq)
plot.mod2pi(
ps[:,0],
ps[:,1],
'k.',
xlabel=r'$\theta$',
ylabel=r'$\omega$',
markersize=0.6,
title=title,
file_prefix=_suffixed(file_prefix, suffix)
)
def main(argv=None):
if argv is None:
argv = sys.argv
file_prefix = None
def aThenB(prefix):
ts, points = pr1a(prefix)
pr1b(prefix, ts, points)
keyfuncs = (
('a', pr1a),
('b', aThenB),
('c', pr1c),
('d', pr1d),
('e', pr2a),
('f', pr2b)
)
ops = 'bcdef'
try:
options, args = getopt.getopt(argv[1:], 'f:p:')
for opt, arg in options:
if opt == '-f':
file_prefix = arg
if opt == '-p':
ops = arg
except getopt.GetoptError as err:
print str(err)
return 2
if 'b' in ops:
ops = ops.replace('a', '')
for (key, op) in keyfuncs:
if key in ops:
op(file_prefix)
return 0
if __name__ == "__main__":
sys.exit(main())