-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparametric.html
More file actions
236 lines (190 loc) · 7.37 KB
/
Copy pathparametric.html
File metadata and controls
236 lines (190 loc) · 7.37 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
<!DOCTYPE html>
<html>
<head>
<title>Parameteric Amplification Applet</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="assets/css/main.css" />
<script type="text/javascript">
window.languagePluginUrl = 'https://cdn.jsdelivr.net/pyodide/v0.15.0/full/';
</script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.15.0/full/pyodide.js"></script>
</head>
<body class="subpage">
<!-- Header -->
<header id="header">
<div class="inner">
<a href="applets.html" class="logo">Back to Applets</a>
<nav id="nav">
</div>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
</header>
<!-- Three -->
<section id="three" class="wrapper">
<div class="inner" id="container">
<p></p>
<header>
<h2>Parametric Amplification</h2>
</header>
<p id='text'>
Parametric amplification is a simple concept that appears both in intuitive, everyday situations,
as well as advanced technologies like lasers and quantum computers. From what I can tell, the
concept is rarely introduced to undergraduates who are nevertheless well-equipped to understand it.
Mathematically, parametric amplification falls under the category of nonlinear dynamics.
This field focuses on equations that are famously hard to solve, but parametric amplification
can readily be understood in terms of a quite straightforward example.
If you've ever swung on a playground swing before (and no one was pushing you!),
you were using your legs to parametrically amplify your motion. Let's understand
what's going on before we define what "parametric" means.
First, you and the swing comprise a pendulum, which has equation of motion
\[
\ddot{\theta} = \omega_0 \sin \theta
\]
where \(\theta\) is the angle you are from hanging vertical and \(\omega_0\) is the typical \(\sqrt{g/l}\), where \(g\) is the gravitational constant
and \(l\) is the length of the swing. Let's consider just small oscillations where \(\sin x \approx x\) and let's also add a damping term with damping constant
\(\beta\).
\[
\ddot{\theta} + 2 \beta \dot{\theta} + \omega_0 \theta = 0
\]
In classical mechanics courses, students typically study the this system with a driving term added to the right hand side, often a sinusoidal force.
\[
\ddot{\theta} + 2 \beta \dot{\theta} + \omega_0 \theta = f_0 \sin(\omega t)
\]
But there's no net force or torque on the pendulum - the pendulum can't push against itself! So we are actually not driving the syste
So what do you do on a swing when you want to start moving? You start swinging your legs back and forth, extending and tucking them in over and over.
</p>
<script> <!-- this JS script runs on startup -->
const output = document.getElementById("output");
const text = document.getElementById("text");
//the follwing is python code that contains almost all the python we want to do
code = `
from js import document # for interacting with JS
import io, base64 # for saving the figure
from numpy import zeros, complex128, pi, array, sin, cos, linspace, exp, real
import matplotlib.pyplot as plt
def paramp(isOn, omega_s, omega_p, omega_0):
N = 2
t = linspace(0,100,1001)
x = zeros([len(t),N],dtype=complex128)
dt = t[1] - t[0]
x0 = array([1,1])*0.0
x[0,:] = x0
def f(x,t, isOn, omega_s, omega_p, omega_0):
x1 = x[0]
x2 = x[1]
beta = 1/2
RHS = 1*exp(1j*omega_s*t)*beta**2
dx1dt = x2
dx2dt = -2*beta*x2 - ((omega_0)*(1 - 0.1*isOn*sin(omega_p*t + 0)))**2 * sin(x1) + RHS
return array([dx1dt,dx2dt])
for i in range(0, len(t)-1):#
k1 = f(x[i,:], t[i], isOn, omega_s, omega_p, omega_0)
k2 = f(x[i,:] + k1*dt/2, t[i] + dt/2, isOn, omega_s, omega_p, omega_0)
k3 = f(x[i,:] + k2*dt/2, t[i] + dt/2, isOn, omega_s, omega_p, omega_0)
k4 = f(x[i,:] + k3*dt, t[i] + dt, isOn, omega_s, omega_p, omega_0)
x[i+1,:] = x[i,:] + (dt/6)*(k1 + 2*k2 + 2*k3 + k4)
return t, x[:,0]
def plot_paramp(omega_s, omega_p, omega_0):
t, x1 = paramp(True, omega_s, omega_p, omega_0)
fig = plt.figure(1)
ax1 = fig.add_subplot(1, 1, 1)
ax1.grid()
ax1.plot(t,real(x1),label='paramp')
ax1.legend()
div_container = document.createElement('div') # now we call some HTML from python to add it to the page
div_container.innerHTML = '''
<br>
<br>
<h> Generator Ready!
</h>
<p>
Let's simulate the driven harmonic oscillator.
</p>
<br>
Signal Frequency \(\omega_s\):
<input id='omega_s' value='1' type="number">
<br><br>
Parameteric Pump Frequency \(\omega_p\):
<input id='omega_0' value='1' type="number">
<br><br>
Resonance Frequency \(\omega_0\):
<input id='omega_p' value='1' type="number">
<br><br>
<button onclick='pyodide.globals.generate_plot_img()'>Plot</button>
<br>
<img id="fig" />'''
div_container2 = document.createElement('div') # now we call some HTML from python to add it to the page
div_container2.innerHTML = '''
<br>
<br>
<p>
Here's another simulation.
</p>
<br>
Parameter 1
<input id='A' value='1' type="number">
<br><br>
Parameter 2
<input id='B' value='1' type="number">
<br><br>
Parameter 3
<input id='c' value='1' type="number">
'''
document.getElementById("container").appendChild(div_container)
document.getElementById("container").appendChild(div_container2)
def generate_plot_img():
# get values from inputs
print('1')
omega_s = int(document.getElementById('omega_s').value)
omega_0 = int(document.getElementById('omega_0').value) # generate an interval
omega_p = int(document.getElementById('omega_p').value) # generate an interval
print('2')
plot_paramp(omega_s, omega_p, omega_0)
print('3')
buf = io.BytesIO()
# copy the plot into the buffer
plt.savefig(buf, format='png')
buf.seek(0)
print('4')
# encode the image as Base64 string
img_str = 'data:image/png;base64,' + base64.b64encode(buf.read()).decode('UTF-8') # show the image
img_tag = document.getElementById('fig')
img_tag.src = img_str
buf.close()
print('5')
`
//end of python code
function evaluatePython() {
console.log(pyodide.runPythonAsync(code));
}
// init pyodide
languagePluginLoader.then(() => { evaluatePython()});
</script>
</div>
</section>
<!-- Footer -->
<footer id="footer">
<div class="inner">
<div class="flex">
<div class="copyright">
© Untitled. Design: <a href="https://templated.co">TEMPLATED</a>. Images: <a href="https://unsplash.com">Unsplash</a>.
</div>
<ul class="icons">
<li><a href="#" class="icon fa-facebook"><span class="label">Facebook</span></a></li>
<li><a href="#" class="icon fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="#" class="icon fa-linkedin"><span class="label">linkedIn</span></a></li>
<li><a href="#" class="icon fa-vimeo"><span class="label">Vimeo</span></a></li>
</ul>
</div>
</div>
</footer>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>