Skip to content

Commit 674d150

Browse files
committed
fixed acc vs force typo
1 parent c748f7a commit 674d150

3 files changed

Lines changed: 15 additions & 22 deletions

File tree

0_getting_started/simulator0.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
import pygame # for visualization
66

77
# simulation setup
8-
m = 1000 # mass of particle
98
x = np.array([0.0, 0.0]) # position of particle
10-
v = np.array([0.1, 0.1]) # velocity of particle
11-
g = np.array([0, -9.81]) # gravitational acceleration
12-
h = 0.1 # time step size in seconds
9+
v = np.array([0.0, 0.0]) # velocity of particle
10+
g = np.array([0.0, -10.0]) # gravitational acceleration
11+
h = 0.01 # time step size in seconds
1312

1413
# visualization/rendering setup
1514
pygame.init()
@@ -41,7 +40,7 @@ def screen_projection(x): # convert simulated coordinates to window co
4140
pygame.time.wait(int(1000.0 / render_FPS)) # wait to render the next frame
4241

4342
# step forward the simulation by updating particle velocity and position
44-
v += h * g / m
43+
v += h * g
4544
x += h * v
4645

4746
# pause the simulation when the particle touches on the ground

0_getting_started/simulator1.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
# simulation setup
88
m = 1000 # mass of particle
9-
x = np.array([0.1, 0.0]) # position of particle
9+
x = np.array([0.3, 0.0]) # position of particle
1010
v = np.array([0.0, 0.0]) # velocity of particle
11-
g = np.array([0, -9.81]) # gravitational acceleration
11+
g = np.array([0.0, -10.0]) # gravitational acceleration
1212
spring_rest_len = 0.3 # rest length of the spring ###
13-
spring_stiffness = 1000 # stiffness of the spring ###
14-
h = 0.1 # time step size in seconds
13+
spring_stiffness = 1e5 # stiffness of the spring ###
14+
h = 0.01 # time step size in seconds
1515

1616
# visualization/rendering setup
1717
pygame.init()
@@ -34,10 +34,9 @@ def screen_projection(x): # convert simulated coordinates to window co
3434
# update the frame to display according to render_FPS
3535
if time_step % int(math.ceil((1.0 / render_FPS) / h)) == 0:
3636
# fill the background with white color, display simulation time at the top,
37-
# render a floor at y=-1, draw the spring segment, and render the particle as a circle:
37+
# draw the spring segment, and render the particle as a circle:
3838
screen.fill((255, 255, 255))
3939
pygame.display.set_caption('Current time: ' + f'{time_step * h: .2f}s')
40-
pygame.draw.aaline(screen, (0, 0, 255), screen_projection([-2, -1]), screen_projection([2, -1]))
4140
pygame.draw.aaline(screen, (0, 0, 255), screen_projection([0, 0]), screen_projection(x)) ###
4241
pygame.draw.circle(screen, (0, 0, 255), screen_projection(x), 0.1 * scale)
4342
pygame.display.flip() # flip the display
@@ -47,12 +46,7 @@ def screen_projection(x): # convert simulated coordinates to window co
4746
spring_cur_len = math.sqrt(x[0] * x[0] + x[1] * x[1]) ###
4847
spring_displacement = spring_cur_len - spring_rest_len ###
4948
spring_force = -spring_stiffness * spring_displacement * (x / spring_cur_len) ###
50-
v += h * (g + spring_force) / m
49+
v += h * (g + spring_force / m)
5150
x += h * v
5251

53-
# pause the simulation when the particle touches on the ground
54-
if x[1] <= -1:
55-
input()
56-
break
57-
5852
time_step += 1 # update time step counter

0_getting_started/simulator2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
n_seg = 4 # number of springs per side of the square
1212
[x, e] = square_mesh.generate(side_length, n_seg) # array of particle positions and springs ###
1313
v = np.array([[0.0, 0.0]] * len(x)) # velocity array of particles ###
14-
g = np.array([0, -9.81]) # gravitational acceleration
14+
g = np.array([0.0, -10.0]) # gravitational acceleration
1515
spring_rest_len = [] # rest length array of the springs ###
1616
for i in range(0, len(e)): # calculate the rest length of each spring
1717
spring_vec = x[e[i][0]] - x[e[i][1]] # the vector connecting two ends of spring i
1818
spring_rest_len.append(math.sqrt(spring_vec[0] * spring_vec[0] + spring_vec[1] * spring_vec[1]))
19-
spring_stiffness = 1000 # stiffness of the spring
20-
h = 0.1 # time step size in seconds
19+
spring_stiffness = 1e6 # stiffness of the spring
20+
h = 0.01 # time step size in seconds
2121

2222
# visualization/rendering setup
2323
pygame.init()
@@ -58,8 +58,8 @@ def screen_projection(x): # convert simulated coordinates to window co
5858
spring_displacement = spring_cur_len - spring_rest_len[i]
5959
spring_force = -spring_stiffness * spring_displacement * (spring_vec / spring_cur_len)
6060
# update the velocity of the two ends of spring i
61-
v[e[i][0]] += h * (g + spring_force) / m
62-
v[e[i][1]] += h * (g - spring_force) / m
61+
v[e[i][0]] += h * (g + spring_force / m)
62+
v[e[i][1]] += h * (g - spring_force / m)
6363
# fix the top left and top right corner by setting velocity to 0:
6464
v[n_seg] = v[(n_seg + 1) * (n_seg + 1) - 1] = np.array([0, 0])
6565
# update the position of each particle:

0 commit comments

Comments
 (0)