# Modeling and Simulation in Python

Case study: Spider-Man

Copyright 2017 Allen Downey



In :

# Configure Jupyter so figures appear in the notebook
%matplotlib inline

# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

# import functions from the modsim.py module
from modsim import *



I'll start by getting the units we'll need from Pint.



In :

m = UNITS.meter
s = UNITS.second
kg = UNITS.kilogram
N = UNITS.newton
degree = UNITS.degree
radian = UNITS.radian




Out:

radian



### Spider-Man

In this case study we'll develop a model of Spider-Man swinging from a springy cable of webbing attached to the top of the Empire State Building. Initially, Spider-Man is at the top of a nearby building, as shown in this diagram.

The origin, O⃗, is at the base of the Empire State Building. The vector H⃗ represents the position where the webbing is attached to the building, relative to O⃗. The vector P⃗ is the position of Spider-Man relative to O⃗. And L⃗ is the vector from the attachment point to Spider-Man.

By following the arrows from O⃗, along H⃗, and along L⃗, we can see that

H⃗ + L⃗ = P⃗

So we can compute L⃗ like this:

L⃗ = P⃗ - H⃗

The goals of this case study are:

1. Implement a model of this scenario to predict Spider-Man's trajectory.

2. Choose the right time for Spider-Man to let go of the webbing in order to maximize the distance he travels before landing.

3. Choose the best angle for Spider-Man to jump off the building, and let go of the webbing, to maximize range.

I'll create a Params object to contain the quantities we'll need:

1. According to the Spider-Man Wiki, Spider-Man weighs 76 kg.

2. Let's assume his terminal velocity is 60 m/s.

3. The length of the web is 100 m.

4. The initial angle of the web is 45 degrees to the left of straight down.

5. The spring constant of the web is 40 N / m when the cord is stretched, and 0 when it's compressed.

Here's a Params object.



In :

params = Params(height = 381 * m,
g = 9.8 * m/s**2,
mass = 75 * kg,
area = 1 * m**2,
rho = 1.2 * kg/m**3,
v_term = 60 * m / s,
length = 100 * m,
angle = (270 - 45) * degree,
k = 40 * N / m,
t_0 = 0 * s,
t_end = 30 * s)




Out:

values

height
381 meter

g
9.8 meter / second ** 2

mass
75 kilogram

area
1 meter ** 2

rho
1.2 kilogram / meter ** 3

v_term
60.0 meter / second

length
100 meter

angle
225 degree

k
40.0 newton / meter

t_0
0 second

t_end
30 second



Compute the initial position



In :

def initial_condition(params):
"""Compute the initial position and velocity.

params: Params object
"""
height, length, angle = params.height, params.length, params.angle

H⃗ = Vector(0, height)
theta = angle.to(radian)
x, y = pol2cart(theta, length)
L⃗ = Vector(x, y)
P⃗ = H⃗ + L⃗
V⃗ = Vector(0, 0) * m/s

return State(P⃗=P⃗, V⃗=V⃗)




In :

initial_condition(params)




Out:

values

P⃗
[-70.71067811865477 meter, 310.28932188134524 ...

V⃗
[0.0 meter / second, 0.0 meter / second]



Now here's a version of make_system that takes a Params object as a parameter.

make_system uses the given value of v_term to compute the drag coefficient C_d.



In :

def make_system(params):
"""Makes a System object for the given conditions.

params: Params object

returns: System object
"""
init = initial_condition(params)

mass, g = params.mass, params.g
rho, area, v_term = params.rho, params.area, params.v_term
C_d = 2 * mass * g / (rho * area * v_term**2)

return System(params, init=init, C_d=C_d)



Let's make a System



In :

system = make_system(params)




Out:

values

height
381 meter

g
9.8 meter / second ** 2

mass
75 kilogram

area
1 meter ** 2

rho
1.2 kilogram / meter ** 3

v_term
60.0 meter / second

length
100 meter

angle
225 degree

k
40.0 newton / meter

t_0
0 second

t_end
30 second

init
P⃗    [-70.71067811865477 meter, 310.289321881...

C_d
0.3402777777777778 dimensionless




In :

system.init




Out:

values

P⃗
[-70.71067811865477 meter, 310.28932188134524 ...

V⃗
[0.0 meter / second, 0.0 meter / second]



### Drag and spring forces

Here's drag force, as we saw in Chapter 22.



In :

def drag_force(V⃗, system):
"""Compute drag force.

V⃗: velocity Vector
system: System object

returns: force Vector
"""
rho, C_d, area = system.rho, system.C_d, system.area

mag = rho * V⃗.mag**2 * C_d * area / 2
direction = -V⃗.hat()
f_drag = direction * mag
return f_drag




In :

V⃗_test = Vector(10, 10) * m/s




Out:

$\begin{pmatrix}10.0 & 10.0\end{pmatrix} meter/second$




In :

drag_force(V⃗_test, system)




Out:

$\begin{pmatrix}-28.873526898450695 & -28.873526898450695\end{pmatrix} kilogram meter/second2$



And here's the 2-D version of spring force. We saw the 1-D version in Chapter 21.



In :

def spring_force(L⃗, system):
"""Compute drag force.

L⃗: Vector representing the webbing
system: System object

returns: force Vector
"""
extension = L⃗.mag - system.length
if magnitude(extension) < 0:
mag = 0
else:
mag = system.k * extension

direction = -L⃗.hat()
f_spring = direction * mag
return f_spring




In :

L⃗_test = Vector(0, -system.length-1*m)




Out:

$\begin{pmatrix}0 & -101\end{pmatrix} meter$




In :

f_spring = spring_force(L⃗_test, system)




Out:

$\begin{pmatrix}-0.0 & 40.0\end{pmatrix} newton$



Here's the slope function, including acceleration due to gravity, drag, and the spring force of the webbing.



In :

def slope_func(state, t, system):
"""Computes derivatives of the state variables.

state: State (x, y, x velocity, y velocity)
t: time
system: System object with g, rho, C_d, area, mass

returns: sequence (vx, vy, ax, ay)
"""
P⃗, V⃗ = state
g, mass = system.g, system.mass

H⃗ = Vector(0, system.height)
L⃗ = P⃗ - H⃗

a_grav = Vector(0, -g)
a_spring = spring_force(L⃗, system) / mass
a_drag = drag_force(V⃗, system) / mass

A⃗ = a_grav + a_drag + a_spring

return V⃗, A⃗



As always, let's test the slope function with the initial conditions.



In :

slope_func(system.init, 0, system)




Out:

(array([0., 0.]) <Unit('meter / second')>,
array([ 5.35924893e-15, -9.80000000e+00]) <Unit('meter / second ** 2')>)



And then run the simulation.



In :

results, details = run_ode_solver(system, slope_func)
details




Out:

values

success
True

message
The solver successfully reached the end of the...



### Visualizing the results

We can extract the x and y components as Series objects.

The simplest way to visualize the results is to plot x and y as functions of time.



In :

def plot_position(P⃗):
x = P⃗.extract('x')
y = P⃗.extract('y')
plot(x, label='x')
plot(y, label='y')

decorate(xlabel='Time (s)',
ylabel='Position (m)')

plot_position(results.P⃗)






We can plot the velocities the same way.



In :

def plot_velocity(V⃗):
vx = V⃗.extract('x')
vy = V⃗.extract('y')
plot(vx, label='vx')
plot(vy, label='vy')

decorate(xlabel='Time (s)',
ylabel='Velocity (m/s)')

plot_velocity(results.V⃗)






Another way to visualize the results is to plot y versus x. The result is the trajectory through the plane of motion.



In :

def plot_trajectory(P⃗, **options):
x = P⃗.extract('x')
y = P⃗.extract('y')
plot(x, y, **options)

decorate(xlabel='x position (m)',
ylabel='y position (m)')

plot_trajectory(results.P⃗, label='trajectory')






### Letting go

Now let's find the optimal time for Spider-Man to let go. We have to run the simulation in two phases because the spring force changes abruptly when Spider-Man lets go, so we can't integrate through it.

Here are the parameters for Phase 1, running for 9 seconds.



In :

params1 = Params(params, t_end=9*s)
system1 = make_system(params1)
results1, details1 = run_ode_solver(system1, slope_func)
plot_trajectory(results1.P⃗, label='Phase 1')






The final conditions from Phase 1 are the initial conditions for Phase 2.



In :

t_final = get_last_label(results1) * s




Out:

8.999999999999991 second



Here's the position Vector.



In :

init = results1.last_row()
init.P⃗




Out:

$\begin{pmatrix}42.09220704823149 & 273.0520143333356\end{pmatrix} meter$



And the velocity Vector.



In :

init.V⃗




Out:

$\begin{pmatrix}14.625507683978038 & 7.937765075110924\end{pmatrix} meter/second$



Here is the System for Phase 2. We can turn off the spring force by setting k=0, so we don't have to write a new slope function.



In :

system2 = System(system1, t_0=t_final, t_end=t_final+10*s, init=init, k=0*N/m)




Out:

values

height
381 meter

g
9.8 meter / second ** 2

mass
75 kilogram

area
1 meter ** 2

rho
1.2 kilogram / meter ** 3

v_term
60.0 meter / second

length
100 meter

angle
225 degree

k
0.0 newton / meter

t_0
8.999999999999991 second

t_end
18.999999999999993 second

init
P⃗    [42.09220704823149 meter, 273.0520143333...

C_d
0.3402777777777778 dimensionless



Here's an event function that stops the simulation when Spider-Man reaches the ground.



In :

def event_func(state, t, system):
"""Stops when y=0.

state: State object
t: time
system: System object

returns: height
"""
P⃗, V⃗ = state
return P⃗.y



Run Phase 2.



In :

results2, details2 = run_ode_solver(system2, slope_func, events=event_func)



Plot the results.



In :

plot_trajectory(results1.P⃗, label='Phase 1')
plot_trajectory(results2.P⃗, label='Phase 2')






Now we can gather all that into a function that takes t_release and V_0, runs both phases, and returns the results.



In :

def run_two_phase(t_release, V⃗_0, params):
"""Run both phases.

t_release: time when Spider-Man lets go of the webbing
V_0: initial velocity
"""
params1 = Params(params, t_end=t_release, V⃗_0=V⃗_0)
system1 = make_system(params1)
results1, details1 = run_ode_solver(system1, slope_func)

t_0 = get_last_label(results1) * s
t_end = t_0 + 10 * s
init = results1.last_row()

system2 = System(system1, t_0=t_0, t_end=t_end, init=init, k=0*N/m)
results2, details2 = run_ode_solver(system2, slope_func, events=event_func)

results = results1.combine_first(results2)
return TimeFrame(results)



And here's a test run.



In :

t_release = 9 * s
V⃗_0 = Vector(0, 0) * m/s

results = run_two_phase(t_release, V⃗_0, params)
plot_trajectory(results.P⃗)
x_final = results.P⃗.last_value().x




Out:

143.42923899746137 meter



### Animation

Here's a draw function we can use to animate the results.



In :

xs = results.P⃗.extract('x')
ys = results.P⃗.extract('y')

def draw_func(state, t):
set_xlim(xs)
set_ylim(ys)
x, y = state.P⃗
plot(x, y, 'bo')
decorate(xlabel='x position (m)',
ylabel='y position (m)')




In :

animate(results, draw_func)






### Maximizing range

To find the best value of t_release, we need a function that takes possible values, runs the simulation, and returns the range.



In [ ]:

def range_func(t_release, params):
"""Compute the final value of x.

t_release: time to release web
params: Params object
"""
V_0 = Vector(0, 0) * m/s
results = run_two_phase(t_release, V_0, params)
x_final = results.P⃗.last_value().x
print(t_release, x_final)
return x_final



We can test it.



In [ ]:

range_func(9*s, params)



And run it for a few values.



In [ ]:

for t_release in linrange(3, 15, 3) * s:
range_func(t_release, params)



Now we can use maximize_scalar to find the optimum.



In [ ]:

bounds = [6, 12] * s
res = maximize_golden(range_func, bounds, params)



Finally, we can run the simulation with the optimal value.



In [ ]:

best_time = res.x
V⃗_0 = Vector(0, 0) * m/s
results = run_two_phase(best_time, V⃗_0, params)
plot_trajectory(results.P⃗)
x_final = results.P⃗.last_value().x




In [ ]: