VP4 2020
VP4 2020
VP4 2020
I. List Representation
1. range(). Notice the number in range should be integer (int)
L = range(5) # list(L) = [0, 1, 2, 3, 4].
L = range(4, 9) # list(L) = [4, 5, 6, 7, 8]
L = range(1, 6, 2) # list(L) = [1, 3, 5] 1 to 6 every other 2 numbers
2. list representation. Sometimes we want to generate a list with some conditions, e.g.
L = [i**2 for i in range(5)] # = [0, 1, 4, 9, 16]
L = [0.1*i*pi for i in range(-3, 3)] # = [-0.3*pi, -0.2*pi, -0.1*pi, 0, 0.1*pi, 0.2*pi]
L = [i**2 for i in range (5) if i != 3] # = [0, 1, 4, 16]
3. List representation can be used in a nested structure, or for dictionary or tuple. e.g.
L = [i*10 + j for i in range(3) for j in range(5) ] # = [0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24]
D = {i:i**2 for i in [0, 1, 2]} # = {0:0, 1:1, 2:4}
start = timer()
for x in range(100):
j = []
for i in range(10000): j.append(i**2)
end = timer()
print(end-start)
start = timer()
for x in range(100): j=[i**2 for j in range(10000)]
end = timer()
print(end-start)
start = timer()
for x in range(100):
j=arange(10000)**2
end = timer()
print(end-start)
III. Damped Oscillator (SHM)
from vpython import *
size, m = 0.02, 0.2 # ball size = 0.02 m, ball mass = 0.2kg
L, k = 0.2, 20 # spring original length = 0.2m, force constant = 20 N/m
amplitude = 0.03
b = 0.05 * m * sqrt(k/m)
scene = canvas(width=600, height=400, range = 0.3, align = 'left', center=vec(0.3, 0, 0), background=vec(0.5,0.5,0))
wall_left = box(length=0.005, height=0.3, width=0.3, color=color.blue) # left wall
ball = sphere(radius = size, color=color.red) # ball
spring = helix(radius=0.015, thickness =0.01)
oscillation = graph(width = 400, align = 'left', xtitle='t',ytitle='x',background=vec(0.5,0.5,0))
x = gcurve(color=color.red,graph = oscillation)
Modified from hw2, the above code simulates the horizontal oscillation of a given amplitude.
! !
1. Now, in addition to the restoring force from the spring, add the air resistance force f = -bv to the ball
with damping factor 𝑏 = 0.05𝑚'𝑘/𝑚 .
2. Follow 1, but instead of letting the ball to oscillate from the initial amplitude, i.e.
ball.pos = vector(L+amplitude, 0, 0), now let the ball to be initially at rest at x = L ,
!
i.e. ball.pos = vector(L, 0, 0), and allow a sinusoidal force F = f a sin(wd t ) 𝑥+ (𝑥+ is the
unit vector in x-axis) applied on the ball, with f a = 0.1 and wd = k / m . We know
! !
when a force F exerts on an object of velocity v , the power on the object by the
! !
force is P = F × v . Find by your simulation 𝑃! , the power averaged over a period
T = 2p / wd at the end of each period. In additional to the curve graph for ball’s
position versus time, plot a dot graph for 𝑃! versus time. Observe the results with
different settings, such as wd =0.8 k / m , 0.9 k / m , 1.1 k / m , or 1.2 k / m
and/or with different b values. Think about the results. Before proceeding to Practice
3, change wd and b back to the original values.
3. Often, we do not want an animated simulation, which is slow due to the animation, but only the calculation
results. We can modify the above computer codes easily for such purpose. We can just delete the code that
creates the canvas, the plot, and the graphs (i.e., those bold blackened codes), delete rate(1000), and replace
codes that generate visual objects by the following codes that generate objects from an empty class.
With these, we still do the same simulation but do not animate them. This will speed up the simulation.
Instead of plotting 𝑃! , now only print 𝑃! every period. You can see after certain number of periods, the
system reaches a steady state and the 𝑃! is almost a constant. Also notice that how much faster the
simulation can run without the animation. (NOTICE: for this technique to work, you need to have all the
proper parameters set for every object after they have been created by the empty class obj.)
The following is example codes:
from vpython import *
size, m = 0.02, 0.2 # ball size = 0.02 m, ball mass = 0.2kg
L, k = 0.2, 20 # spring original length = 0.2m, force constant = 20 N/m
fa = 0.1
b = 0.05 * m * sqrt(k/m)
omega_d = sqrt(k/m)
'''
scene = canvas(width=600, height=400, fov = 0.03, align = 'left', center=vec(0.3, 0, 0), background=vec(0.5,0.5,0))
wall_left = box(length=0.005, height=0.3, width=0.3, color=color.blue) # left wall
ball = sphere(radius = size, color=color.red) # ball
spring = helix(radius=0.015, thickness =0.01)
oscillation1 = graph(width = 400, align = 'left', xtitle='t',ytitle='x',background=vec(0.5,0.5,0))
x=gcurve(color=color.red,graph = oscillation1)
'''
oscillation2 = graph(width = 400, align = 'left', xtitle = 't', ytitle = 'average_power', background=vec(0.5,0.5,0))
p = gdots(color=color.cyan, graph = oscillation2)
spring.pos = vector(0, 0, 0)
ball.pos = vector(L, 0 , 0) # ball initial position
ball.v = vector(0, 0, 0) # ball initial velocity
ball.m = m
T = 2*pi / omega_d
t, dt, n = 0, 0.001, 1
power = 0.0
while True:
#rate(1000)
spring.axis = ball.pos - spring.pos
spring_force = - k * (mag(spring.axis) - L) * norm(spring.axis)
force = vector(fa*sin(omega_d*t), 0, 0)
ball.a = (force + spring_force - b * ball.v ) / m
ball.v += ball.a*dt
ball.pos += ball.v*dt
t += dt
#x.plot(pos=(t,ball.pos.x - L))
3. Since we already have the clear representation of the wave, we can remove the ball-spring plotting,
marked by #3. Notice that although the wave shown is with transverse motion, but it is indeed a longitudinal
wave with a transverse wave representation.
4. You may have seen such things in a video game that an object moves out of the right end of the screen
and comes back from the left end of the screen. This is called “periodically boundary condition”, a very often
used assumption in physics and engineering. In our system here, it means that the last ball and the zeroth
ball is also connected by a spring in a fashion that looks like
the zeroth ball is to the right of the last ball, or the last ball is
to the left of the zeroth ball.
Now, modify the codes marked by ‘##’, and add one or two
more lines to achieve such “periodically boundary condition”.
You will see that the wave is generated from both ends
towards the center because now the last one is also
connected to the zeroth one, which is the source of the wave.
5. As you saw in Practice 4, the wave amplitude at times gets larger and at times gets smaller. This means
that this wave we try to create is not a “normal mode” of the system. The normal modes of the system are
those waves whose amplitude can keep constant. This means that after going a full circle (Notice that we
have the tail connected to the head, so we can view this
system as a circle), the wave repeats itself, or we can say
that wave satisfies the periodically boundary condition.
These are waves with wavelength l = Nd / n , where
n = 1, 2, 3,... (i.e. wavenumber K = 2p / l = 2p n / ( Nd ) , which is also called wavevector). We can observe
such wave by initially assigning balls to their proper positions, removing the external source, and letting the
system to go by itself. This means changing #5 to
Unit_K, n = 2 * pi/(N*d), 10
Wavevector = n * Unit_K
phase = Wavevector * arange(N) * d
ball_pos, ball_orig, ball_v, spring_len = np.arange(N)*d + A*np.sin(phase), np.arange(N)*d, np.zeros(N), np.ones(N)*d
Notice in the last line, ball_pos is set to np.arange(N)*d + A*np.sin(phase), in which np.arange(N)*d is the array for the
balls’ original positions at balance points. A in A*np.sin(phase) is the amplitude of the longitudinal wave, and
each ball is originally displaced by A*sin(phase) before the simulation begins. phase in phase = Wavevector *
arange(N) * d is the array for the phase of each ball’s position in the longitudinal wave. We use np.sin(phase)
because we want an array, elements of which are the sin function of phase, which is also an array. Then this
array can be added to another array, np.arange(N)*d.
You also need to remove #4 and add after #6 a line to handle zeroth ball’s velocity ball_v[0].
In the simulation, you will see clearly the oscillating standing wave with n = 10. Try to obtain the period (T)
of the standing wave and the corresponding angular frequency (omega = 2*pi / T). For higher accuracy, please
change dt = 0.001 to dt = 0.0003.
V. Homework
In a crystal, only certain normal modes of waves can exist. These modes are called “Phonons”. Practice 5.
shows one of such modes in a simplified 1-dimensional crystal consisted of only 50 balls (atoms). As you
already see, when the wavevector is given, the angular frequency (omega =2 pi/T) is decided by the system.
Now we want to know the relationship between the angular frequency and the wavevector, which is called
the dispersion relationship. Modify you program from Practice 5. such that you can obtain the angular
frequency (omega) for n from 1 to N/2-1. Use the graph plotting similar to lesson 5 homework (must) to plot
the the angular frequency versus the wavevector.