0% found this document useful (0 votes)
9 views26 pages

Py Con Presentation

Uploaded by

Ryadh Haddad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views26 pages

Py Con Presentation

Uploaded by

Ryadh Haddad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Visual Python in a

Computational Physics
Course
Richard P. Olenick
David Case
Eric Pepin
William Spearman
Outline
• Physical Modeling Demos with VPython
– How Does a Slider Differ from a Fastball?
– Boids
– Stability of L44 and L55 Lagrange Points
– How Lightning Forms
• Background and Mathematical Tools
– Euler Methods, Runge-Kutta, Poincare Maps
• Advantages of VPython
• Demos of Students’ Semester Projects
– 3D Dynamic Lattice
– Gravitational Shaping by Galactic Encounters
– Sand Avalanches
– Percolation Growth of Galaxies
– Non-relativistic Electron Scattering
Elements of the
Computational Physics
• Emphasizes the Course
modeling of physical
systems.
• Lectures are kept to a
minimum and students
work on programs
during classes.
• Instructor provides in-
class debugging.
• Students have different
initial conditions to run
and their results are
shown to the entire
class and discussed.
Magnus Effect and Baseball
Pitches

•Application of the Magnus effect


and empirical viscous drag
•Prospective
• Fastballs, curveballs, sliders, and
screwballs thrown by both right-
handed and left-handed pitchers
can be simulated and viewed from
the prospective of the pitcher,
catcher, or umpire.
def go():
global omega, w, ball, g, dt, B, phi, rot, speed
#initial velocity of pitch for throwing angle
ball.v=vector(-s*math.sqrt(1-math.cos(theta)*math.cos(theta)-
math.cos(beta)*math.cos(beta)),s*math.cos(theta),s*math.cos(beta))
while( ball.x>-.5 and ball.y>0): #while ball in the air and not passed
home plate
c.interact()
rate(20*speed)
ball.rotate(angle=omega*dt,axis=rot,origin=ball.pos)#animates the
rotation of the ball
ball.v_mid = ball.v + (ball.v + (-F(ball.v.mag)*ball.v.mag*ball.v +
B*cross(w,ball.v)-g)*dt/2.)*dt/2 #calculates new pos and vel
ball.v = ball.v + (-F(ball.v_mid.mag)*ball.v_mid.mag*ball.v_mid +
B*cross(w,ball.v_mid)-g)*dt
ball.pos = ball.pos + ball.v*dt
ball.path.append(pos=ball.pos) #marks path of pitch
#if a strike, make ball red
if ((ball.pos.x<0 and ball.pos.x>-.216)and(ball.pos.y<1.24 and
ball.pos.y>.52)and(ball.pos.z<.216 and ball.pos.z>-.216)):
ball2.color=color.red
Boids

• Models the distributive


behavior that is observed in
nature among groups such as
swarms, flocks, and herds
with an application to
ferromagnetism.
• Speed, cohesion, separation,
alignment and goal seeking
can be controlled in the
simulation. The average
separation is measured as the
boids flock.
for i in range(numboids):
v1 = rule_1(i)*D1 # velocity correction based on rule 1 (cohesion)
v2 = rule_2(i)*D2 # velocity correction based on rule 2 (separation)
v3 = rule_3(i)*D3 # velocity correction based on rule 3 (alignment)
v4 = tend_to_place(i)*D4 # velocity correction based on common goal point
boids[i].velocity = Speedness*(boids[i].velocity+v1+v2+v3+v4)/
abs(boids[i].velocity+v1+v2+v3+v4)
boids[i].pos += (boids[i].velocity)*dt
directed[i].pos = boids[i].pos
directed[i].axis = (0.5*boids[i].velocity/abs(boids[i].velocity))
total += boids[i].pos
Stability of Lagrange Points

• The stability of particle orbits near the L4 and L5 Lagrange


points is simulated.
• The effects of mass ratio, , of the two major bodies as well as
the initial offset positions, x and y can be studied.
• Simulations use the Runge-Kutta-Fehlberg algorithm.
if graphics == 1:
# draws the scene
objectpos=vector(x,y,0)
# use angular velocity to find how much the objects are
rotated
objectpos=rotate(objectpos, angle=omega*t, axis=[0,0,1])
body2pos=vector(1-mu, 0,0) # draws the scene

body2pos=rotate(body2pos, angle=omega*t,axis=[0,0,1])
body1pos=vector(-mu, 0,0)
body1pos=rotate(body1pos, angle=omega*t,axis=[0,0,1])
object.pos=objectpos
body2.pos=body2pos
body1.pos=body1pos
object.trail.append(pos=object.pos)
How Lightning Forms—Fractal
Growth Processes

• Diffusion-limited
growth is simulated.
• Box method used to
calculate the fractal
dimension.
• The effects of growth
time and fractal
dimension can be
studied.
def walk ():
Checks to see if particle became attached from the movement of another
particle,
If not, moves the particles,
Checks for attachment
Only checks if particle is within the radius of the existing structure
def move(point):
Move the particle randomly,
The grid is cyclic - going off one side puts it on the other
def check(point):
Checks to see if the particle is touching any particle in the aggregate

Changes the radius if necessary


def fractDim ():
Uses boxes to calculate box-dimension
First box has smallest dimensions that enclose the aggregate
Subsequent boxes are 1/8 the size
Counts the number of boxes necessary to cover the aggregate at each
size
Plots the common log of box diameter versus the common log of the count

Calculates best fit linear regression line (R^2>0.999)


Negative of the slope is the box-dimension
Some Mathematical
Methods

• Euler Methods
yn  yn  1  f tn  1  t

• Improved Euler
f tn   f tn  t 
y tn  t   y tn   h
2
•Runge-Kutta 4th order

k1  f tn , yn  t
Dt k
k 2 = f(tn + , y n + 1 )D t,
2 2
Dt k2
k3 = f(t n + , y n + ) D t,
2 2
k 4 = f(tn + D t , y n + k 3 ) D t,
5
y n + 1 = y n + 61 ( k 1 + 2 k 2 + 2 k 3 + k 4 ) + O[( D t ) ].
Poincare Maps
•Choose any three from x, y, vx, and vy and calculate the
fourth using the integral of the system, such as x, y, and vy.
•Follow a solution in the three-dimensional space of these
three coordinates. If the surface is the plane x = 0, then we
are looking at points that cross in the y-vy plane.
• Now let a solution cross this plane at the point P, where x is
increasing or vx > 0. The next crossing occurs at Q, where vx
< 0. The next cross occurs when x is increasing, at R.
Poincaré map for the
Hénon-Heiles problem
with E = 0.1053,
x(0)=0.4, y(0) = 0,
vx(0) = 0, vy(0) = 0.225
x2 y 2 1 y3
E   k x  y   x y 
2 2 2

2 2m 2 3

d 2x
 x  2 xy
dt 2
d2y 2 2
 y  x  y
dt 2

Poincaré map for the


Hénon-Heiles problem with
E = 0.1503,
x(0)=0.5, y(0) = 0, vx(0) =
0, vy(0) = 0.225
Advantages of VPython
The semester-long experiment using
VPython in a computational physics course
proved highly successful. The notable
successes included:
1. Shorter learning curve for programming.
2. 3D visualization.
3. Correspondence between vector laws and
object oriented programming.
4. More in-depth student projects.
5. Student enjoyment with developing
simulations.
6. Carry over of modeling into other courses.
Gravitational Shaping by
Galactic Encounters

As a galaxy
encounters
another galaxy
(represented as
a point mass),
the orbits of
individual stars
are perturbed.
Avalanches—An Example of
Self-Organized Criticality

Avalanches
occur when a
critical threshold
is exceeded.
def fall(x,y,D):
global grid, sand
grid[x+D%3-1][y+1-D/3][0]+=1 #adds one to
receiving square
s=grid[x][y][1].pop() #index of sand
being moved
grid[x+D%3-1][y+1-D/3][1].append(s) #adds
index to receiving square
grid[x][y][0]-=1 # removes one from
falling square
sand[s].pos=(x+D%3-1,y+1-D/3,grid[x+D%3-
1][y+1-D/3][0]) # relocates sand
Percolation Growth of
Galaxies

In this percolation
model of galaxies,
long range spiral
structure is achieved
through local
interactions.
def growth():
Each inhabited sector has a p chance of
giving birth to a new star, in which case it
is destroyed

def rotate():
Rotates each sector according to the
angular velocity
def spiral():
Determines if stars are born or have died
in each sector, updates image to reflect
current population and position
3D Dynamic Lattice

A 3D lattice of
particle connected
by springs allows
for an investigation
of normal modes.
# sum the forces acting on the
# current particle
Forces = (Force(i,j,k,k,j,idim)
+Force(i,j,k,k,j,iaug)
+Force(i,j,k,k,jdim,i)
+Force(i,j,k,k,jaug,i)
+Force(i,j,k,kdim,j,i)
+Force(i,j,k,kaug,j,i))
Forces += -(mass[k][j][i].velocity*b)
if dropping:
# add gravity and handle collisions
Forces += m*g*vector(0,1,0)
if mass[k][j][i].pos.y < floor:
if mass[k][j][i].velocity.y < 0:
mass[k][j][i].velocity.y = -mass[k][j][i].velocity.y
if mass[k][j][i].fixed:
# do no move if particle is fixed in place
Forces = vector(0,0,0)
mass[k][j][i].velocity = vector(0,0,0)
cur_Forces = Forces
# set variables for passing to rkf4
var[0]=mass[k][j][i].pos
var[1]=mass[k][j][i].velocity
# use RKF4 to evaluate differential equation
Non-relativistic Electron
Scattering
This project
involves a Monte
Carlo simulation
of non-relativistic
electron scattering
in a thin film. The
backscattering,
transmission, and
absorption can be
studied for various
metals.
# calcluate new positions for particles
for n in range(0, maxn):
c1.interact()
theta.append(0) # add the n+1 element
phi.append(0) # add the n+1 element
s.append(0) # initialize position element to 0
T.append(0)
R.append(vector(0,0,0))
Rtheta = random()
Rphi = random()
Rs = random()
p_costheta = 1-(2*beta*Rtheta / (1+beta-Rtheta))
p_theta = acos(1-(2*beta*Rtheta / (1+beta-Rtheta)))
#p_theta = 1 - p_theta
p_phi = 2*pi*Rphi
costhetanew = cos(theta[n])*cos(p_theta)+sin(theta[n])*sin(p_theta)*cos(p_phi)
theta[n+1] = acos(costhetanew)
top = ( sin(theta[n])*sin(p_theta)*sin(p_phi) )
bottom = ( cos(p_theta)-(costhetanew)*cos(theta[n]))
if bottom == 0:
bottom = 1
top = sin(p_theta)*sin(p_phi)
phi[n+1] = atan( top / bottom ) + phi[n]
s[n] = (-1.02*beta*(beta+1)*A*(T[n])**2 / (Z*(Z+1)*rho)) * log(Rs)
T[n+1] = T[n] - abs(dTds(T[n]))*s[n]
R[n+1] = R[n] + s[n]*(sin(theta[n+1])*cos(phi[n+1]) *vector(1,0,0)
+ sin(theta[n+1])*sin(phi[n+1])*vector(0,1,0)
+ cos(theta[n+1])*vector(0,0,1))
Follow-Up

For additional information please


contact [email protected].

You might also like