Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs1-25sp
project04
Commits
b214980b
Commit
b214980b
authored
2 months ago
by
Jinhuang (Jin) Zhou
Browse files
Options
Download
Email Patches
Plain Diff
go
parent
d5b02d42
master
No related merge requests found
Changes
41
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
visualize_cradle_run.py
+13
-31
visualize_cradle_run.py
with
13 additions
and
31 deletions
+13
-31
visualize_cradle_run.py
View file @
b214980b
"""
CS1 24fa - Assignment 4
Helper functions and implementation of a Newton's cradle simulation.
"""
from
vpython
import
mag
,
scene
,
rate
from
support.types
import
PendulumEntry
from
src.constants
import
(
RANGE
,
SPREADSHEET_ID
,
DT
,
GRAV
,
R
,
THETA_INIT
,
)
from
src.special_cradles
import
make_newton_cradle
from
support.constants
import
NC_FP
,
DT
,
GRAV
,
R
,
THETA_INIT
from
src.pds_helper
import
write_to_csv
from
src.special_cradles
import
uniform_mass_newton_cradle
,
uniform_e_newton_cradle
,
elastic_newton_cradle
,
inelastic_newton_cradle
,
make_newton_cradle
from
src.swinging
import
full_swing_update
scene
.
title
=
"Newton's cradle"
# TODO 4: use the physics derivation provided to update velocities post collision
def
handle_collision
(
e
:
float
,
omega1
:
float
,
omega2
:
float
,
m1
:
float
,
m2
:
float
,
r_param
:
float
,
)
->
tuple
[
float
,
float
]:
def
handle_collision
(
e
:
float
,
omega1
:
float
,
omega2
:
float
,
m1
:
float
,
m2
:
float
,
r_param
:
float
):
"""
In case two bobs collide, handles the physics (momentum conservation).
...
...
@@ -40,30 +26,25 @@ def handle_collision(e: float,
Returns:
(tuple[float, float]): post-collision omegas of 1st, 2nd pendula
"""
# TODO 4: Use the physics derivation provided to update velocities post collision
...
# TODO 5: handle pairwise pendulum motion and collisions
def
handle_two_bobs
(
pend1
:
PendulumEntry
,
pend2
:
PendulumEntry
,
r_param
:
float
)
->
None
:
def
handle_two_bobs
(
pend1
,
pend2
,
r_param
):
"""
Handles the collisions and updates of 2 pendula in the cradle.
Args:
pend1 (
PendulumEntry
): dictionary representing 1st pendulum,
pend1 (
dict
): dictionary representing 1st pendulum,
with keys as physical attributes
pend2 (
PendulumEntry
): dictionary representing 1st pendulum,
pend2 (
dict
): dictionary representing 1st pendulum,
with keys as physical attributes
r_param (float): size parameter used to define pendulum components
"""
# TODO 5: Handle pairwise pendulum motion and collisions
...
# TODO 6: perform one step of the newton cradle
def
newton_cradle_tick
(
pend_list
:
list
[
PendulumEntry
],
r_param
:
float
,
dt
:
float
,
g
:
float
,
)
->
None
:
def
newton_cradle_tick
(
pend_list
,
r_param
,
dt
,
g
):
"""
Performs one 'tick' of the entire Newton's cradle (propagates any
motion throughout all the pendula).
...
...
@@ -75,17 +56,18 @@ def newton_cradle_tick(pend_list: list[PendulumEntry],
dt (float): time step of update
g (float): gravitational acceleration
"""
# TODO 6: Perform one step of the newton cradle
...
if
__name__
==
"__main__"
:
# TODO 7.1: use a helper function to make pend_list with
#
RANGE, SPREADSHEET_ID
, R and THETA_INIT
#
a filepath called NC_FP
, R and THETA_INIT
pend_list
=
...
done
=
False
while
not
done
:
rate
(
1
/
DT
)
# The rate to be included in the animation
rate
(
1
/
DT
)
# The rate to be included in the animation
# TODO 7.2: Use another helper function to perform a timestep of the
# Newton's cradle
This diff is collapsed.
Click to expand it.
Prev
1
2
3
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help