Commit b214980b authored by Jinhuang (Jin) Zhou's avatar Jinhuang (Jin) Zhou
Browse files

go

parent d5b02d42
Showing with 128 additions and 107 deletions
+128 -107
# Probably prudent not to push private OAuth keys to Gitlab. # Probably prudent not to push private OAuth keys to Gitlab.
config.json config.json
venv/
{ {
"upload": ["src/constants.py", "visualize_cradle_run.py", "src/special_cradles.py", "src/swinging.py"] "upload": ["visualize_cradle_run.py", "src/special_cradles.py", "src/swinging.py", "src/pds_helper.py"]
} }
File added
File added
File added
File added
File added
File added
File added
File added
mass,restitution_coeff,color,texture,objects,theta,omega
5,1,cyan,earth,(arrow box cone),-1,10
5,1,green,flower,(curve cylinder elliposoid),2,7
5,1,white,metal,(helix label arrow),15,56
5,1,green,flower,(points pyramid cylinder),0.2,3
5,1,white,metal,(box sphere cylinder),0.6,19
5,1,blue,metal,(cylinder sphere ring),90,78
\ No newline at end of file
import pandas as pd
from typing import Any
from support.constants import NC_FP
def extract_and_convert(relative_filepath:str)-> list[dict[str, Any]]:
"""
Creates a list of pendula given a CSV file.
Args:
relative_filepath (str): filepath to access CSV with pendula data
Returns:
(list[dict]): list of dictionaries, each dictionary corresponding to
a pendulum, with keys as physical attributes
"""
df = pd.read_csv(relative_filepath)
bad_rep = df.to_dict(orient="index")
# TODO 0: Fix the bad representation
...
# Provided
def write_to_csv(pend_list):
"""
Writes a pend_list to the Newton's Cradle CSV file called "newton_cradle_pends.csv"
Args:
pend_list (list[dict]): list of dictionaries, each dictionary corresponding to
a pendulum, with keys as physical attributes
"""
df = pd.DataFrame.from_dict(pend_list)
df.to_csv(NC_FP, index=False)
# Provided
if __name__ == "__main__":
print(pd.read_csv(NC_FP))
print(extract_and_convert(NC_FP))
\ No newline at end of file
""" """
CS1 24fa - Assignment 4
Helper functions and implementation of a Newton's cradle simulation. Helper functions and implementation of a Newton's cradle simulation.
""" """
from vpython import vector # type: ignore from vpython import vector
from support.pendulum import make_objects_of_pendulum
from support.constants import *
from src.pds_helper import extract_and_convert
from src.constants import COLOR_MAP, TEXTURE_MAP import pandas as pd
from src.pendulum import make_pendulum
from support.sheets_api import update_values, get_pendulum_info
from support.types import PendulumEntry
# TODO 1: write the following 5 functions using relevant helper functions from `sheets_api`. # TODO 1: write the following 5 functions using relevant helper functions from 'pds_helper'.
def uniform_mass_newton_cradle(spreadsheet_id: str, def uniform_mass_newton_cradle(relative_filepath: str, mass: float):
mass_range: str,
mass: float,
num_pends: int,
) -> None:
""" """
Updates the spreadsheet such that all pendula have the same mass. Creates a list of pendula and updates them to have the same mass.
Args: Args:
spreadsheet_id (str): unique ID of Google Sheet relative_filepath (str): filepath to access CSV with pendula data
mass_range (str): range of cells that have mass values in the spreadsheet
mass (float): mass to be uniformly applied to each pendulum mass (float): mass to be uniformly applied to each pendulum
num_pends (int): number of pendula in the Newton's cradle
Returns:
(list[dict]): list of dictionaries, each dictionary corresponding to
a pendulum, with keys as physical attributes
""" """
# TODO: create a list that will serve as the required nested list
# as described in the guide using a helper function. Then, for each pendulum,
# set all the mass values in the dictionary to the required argument mass value
... ...
# TODO: create an empty list that will serve as the required nested list
# as described in the guide. Then, for each pendulum, append a list with
# the required string mass value to the empty list def uniform_e_newton_cradle(relative_filepath: str, e_val: float):
# Then, use a helper function from `sheets_api.py` to update this nested
# list of values to the specific spreadsheet defined by the
# `spreadsheet_id`
def uniform_e_newton_cradle(spreadsheet_id: str,
e_range: str,
e_val: float,
num_pends: int,
) -> None:
""" """
Updates the spreadsheet such that all pendula have the same coefficient of Creates a list of pendula and updates them to have the same coefficient of
restitution. restitution.
Args: Args:
spreadsheet_id (str): unique ID of Google Sheet relative_filepath (str): filepath to access CSV with pendula data
e_range (str): range of cells that have restitution_coeffs values in the
spreadsheet
e_val (float): coefficient of restitution to be uniformly applied to each e_val (float): coefficient of restitution to be uniformly applied to each
pendulum pendulum
num_pends (int): number of pendula in the Newton's cradle
Returns:
(list[dict]): list of dictionaries, each dictionary corresponding to
a pendulum, with keys as physical attributes
""" """
...
# TODO: similar to the previous function, but you will be updating # TODO: similar to the previous function, but you will be updating
# the coefficients of restitution with the relevant value instead # the coefficients of restitution with the relevant value instead
...
def elastic_newton_cradle(spreadsheet_id: str, def elastic_newton_cradle(relative_filepath: str):
e_range: str,
num_pends: int,
) -> None:
""" """
Updates the spreadsheet such that all pendula have perfectly elastic Creates a list of pendula and updates them to have perfectly elastic
collisions with each other. collisions with each other.
Args: Args:
spreadsheet_id (str): unique ID of Google Sheet relative_filepath (str): filepath to access CSV with pendula data
e_range (str): range of cells that have restitution_coeffs values in the
spreadsheet Returns:
num_pends (int): number of pendula in the Newton's cradle (list[dict]): list of dictionaries, each dictionary corresponding to
a pendulum, with keys as physical attributes
""" """
# TODO: Use a helper function to return a list of pendula with the
# elastic coefficient of restitution value
... ...
# TODO: Use a helper function to update the spreadsheet with the elastic
# coefficient of restitution value.
def inelastic_newton_cradle(spreadsheet_id: str, def inelastic_newton_cradle(relative_filepath: str):
e_range: str,
num_pends: int,
) -> None:
""" """
Updates the spreadsheet such that all pendula have perfectly inelastic Creates a list of pendula and updates them to have perfectly inelastic
collisions with each other. collisions with each other.
Args: Args:
spreadsheet_id (str): unique ID of Google Sheet relative_filepath (str): filepath to access CSV with pendula data
e_range (str): range of cells that have restitution_coeffs values in the
spreadsheet Returns:
num_pends (int): number of pendula in the Newton's cradle (list[dict]): list of dictionaries, each dictionary corresponding to
a pendulum, with keys as physical attributes
""" """
# TODO: Use a helper function to return a list of pendula with
# the inelastic coefficient of restitution value
... ...
# TODO: Use a helper function to update the spreadsheet with the inelastic
# coefficient of restitution value.
def make_newton_cradle(ranges: str, def make_newton_cradle(relative_filepath: str, r_param: float, theta_init: float):
spreadsheet_id: str,
r_param: float,
theta_init: float,
) -> list[PendulumEntry]:
""" """
Uses information from a specific Google Sheet to make a list of Uses information from a specific CSV file to make a list of
dictionaries representing each pendula and their physical quantities. dictionaries representing each pendula and their physical attributes.
Args: Args:
ranges (str): range of cells in the Google Sheet relative_filepath (str): relative filepath of the CSV file
spreadsheet_id (str): unique ID of Google Sheet
r_param (float): size parameter used to define pendulum components r_param (float): size parameter used to define pendulum components
theta_init (float): initial angle of the first pendulum (in radians) theta_init (float): initial angle of the first pendulum (in radians)
Returns: Returns:
(list[dict]): list of dictionaries, each dictionary representing a (list[dict]): list of dictionaries, each dictionary corresponding
pendulum, with keys as physical attributes to each pendulum, with keys representing physical attributes
""" """
... # TODO: Use a helper function from 'pds_helper.py' to make a 'pend_list'
# TODO: use a helper function from `sheets_api.py` to make a `pend_list`
# TODO: write a `for` loop that goes through each pendulum dictionary # TODO: write a 'for' loop that goes through each pendulum dictionary
# in `pend_list` and: # in 'pend_list' and:
# finds the bob color, bob texture, and calculates the axle position # finds the bob color, bob texture, and calculates the axle position
# makes a pendulum with the aforementioned attributes # makes a pendulum with the aforementioned attributes
# assigns that pendulum as the value of the key 'pendulum' # assigns that pendulum as the value of the key 'objects'
# initializes the value for the key 'theta' depending on the value of i # initializes the value for the key 'theta' depending on the value of i
# initializes the value for the key 'omega' # initializes the value for the key 'omega'
...
\ No newline at end of file
""" """
CS1 24fa - Assignment 4
Helper functions and implementation of a Newton's cradle simulation. Helper functions and implementation of a Newton's cradle simulation.
""" """
from vpython import ( # type: ignore from vpython import sin, cos, vector
cos, # type: ignore
sin, # type: ignore
vector, # type: ignore
)
from support.types import PendulumEntry def angle_update(omega: float, theta: float, r_param: float, dt: float, g: float):
# TODO 2: update angular components of a pendulum with the derivation provided
def angle_update(omega: float,
theta: float,
r_param: float,
dt: float,
g: float,
) -> tuple[float, float]:
""" """
Updates the angle and angular velocity of a pendulum system. Updates the angle and angular velocity of a pendulum system.
...@@ -32,15 +18,12 @@ def angle_update(omega: float, ...@@ -32,15 +18,12 @@ def angle_update(omega: float,
Returns: Returns:
(tuple[float, float]): time-updated omega, theta (tuple[float, float]): time-updated omega, theta
""" """
# TODO 2: Update angular components of a pendulum with the derivation provided
... ...
# Provided # Provided
def swing_update(pend_dict: PendulumEntry, def swing_update(pend, r_param: float, dt: float, g: float):
r_param: float,
dt: float,
g: float,
) -> None:
""" """
Updates the angle & position of the pendulum components in a single swing. Updates the angle & position of the pendulum components in a single swing.
...@@ -50,25 +33,20 @@ def swing_update(pend_dict: PendulumEntry, ...@@ -50,25 +33,20 @@ def swing_update(pend_dict: PendulumEntry,
dt (float): time step of update dt (float): time step of update
g (float): gravitational acceleration g (float): gravitational acceleration
""" """
theta = pend_dict["theta"] theta = pend["theta"]
omega = pend_dict["omega"] omega = pend["omega"]
axle, bob, cord = pend_dict["pendulum"] axle, bob, cord = pend["objects"]
omega_new, theta_new = angle_update(omega, theta, r_param, dt, g) omega_new, theta_new = angle_update(omega, theta, r_param, dt, g)
bob.pos = axle.pos + r_param * vector(sin(theta_new), -cos(theta_new), 0) bob.pos = axle.pos + r_param * vector(sin(theta_new), -cos(theta_new), 0)
cord.axis = bob.pos - axle.pos cord.axis = bob.pos - axle.pos
pend_dict["theta"] = theta_new pend["theta"] = theta_new
pend_dict["omega"] = omega_new pend["omega"] = omega_new
# TODO 3: update the swings for each pendulum in the list def full_swing_update(pend_list, r_param: float, dt: float, g: float):
def full_swing_update(pend_list: list[PendulumEntry],
r_param: float,
dt: float,
g: float,
) -> None:
""" """
Updates the omegas for all the pendula in the cradle, updating each Updates the omegas for all the pendula in the cradle, updating each
pendulum dictionary. pendulum dictionary.
...@@ -80,4 +58,5 @@ def full_swing_update(pend_list: list[PendulumEntry], ...@@ -80,4 +58,5 @@ def full_swing_update(pend_list: list[PendulumEntry],
dt (float): time step of update dt (float): time step of update
g (float): gravitational acceleration g (float): gravitational acceleration
""" """
# TODO 3: Update the swings for each pendulum in the list
... ...
mass,restitution_coeff,color,texture,objects,theta,omega
53,0.8,gray,earth,(arrow box cone),-1,10
2,0.2,black,flower,(curve cylinder elliposoid),2,7
3,0.3,blue,metal,(helix label arrow),-2,4
90,0.7,red,flower,(points pyramid cylinder),15,2
68,1,magenta,metal,(box sphere cylinder),5,56
22,0.9,orange,metal,(cylinder sphere ring),90,3
\ No newline at end of file
element,atomic_number,mass,family
hydrogen,1,1.0078,nonmetal
carbon,6,12.011,nonmetal
gold,79,196.97,transition_metal
uranium,92,238.03,actinide
\ No newline at end of file
fortnite,csgo,valorant
\ No newline at end of file
File added
File added
File added
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment