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

updated

parent b214980b
No related merge requests found
Showing with 881 additions and 82 deletions
+881 -82
A:
script: /testers/cs1/project04/A/test
B:
script: /testers/cs1/project04/B/test
C:
script: /testers/cs1/project04/C/test
D:
script: /testers/cs1/project04/D/test
\ No newline at end of file
import pandas as pd
from src.pds_helper import extract_and_convert
from support.constants import NC_FP
# Provided
if __name__ == "__main__":
print(pd.read_csv(NC_FP))
print(extract_and_convert(NC_FP))
\ No newline at end of file
import pandas as pd
from typing import Any
from support.constants import NC_FP
from support.types import PendulumEntry
def extract_and_convert(relative_filepath:str)-> list[dict[str, Any]]:
def extract_and_convert(relative_filepath:str)-> list[PendulumEntry]:
"""
Creates a list of pendula given a CSV file.
......@@ -14,14 +14,15 @@ def extract_and_convert(relative_filepath:str)-> list[dict[str, Any]]:
a pendulum, with keys as physical attributes
"""
df = pd.read_csv(relative_filepath)
bad_rep = df.to_dict(orient="index")
df = pd.read_csv(relative_filepath) # type: ignore
bad_rep = df.to_dict(orient="index") # type: ignore
# TODO 0: Fix the bad representation
...
# Provided
def write_to_csv(pend_list):
def write_to_csv(pend_list: list[PendulumEntry]) -> None:
"""
Writes a pend_list to the Newton's Cradle CSV file called "newton_cradle_pends.csv"
......@@ -29,10 +30,5 @@ def write_to_csv(pend_list):
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
df = pd.DataFrame.from_dict(pend_list) # type: ignore
df.to_csv(NC_FP, index=False)
\ No newline at end of file
......@@ -2,16 +2,16 @@
Helper functions and implementation of a Newton's cradle simulation.
"""
from vpython import vector
from support.pendulum import make_objects_of_pendulum
from support.constants import *
from src.pds_helper import extract_and_convert
import pandas as pd
from vpython import vector # type: ignore
from support.constants import *
from support.pendulum import make_objects_of_pendulum # type: ignore
from support.types import PendulumEntry
# TODO 1: write the following 5 functions using relevant helper functions from 'pds_helper'.
def uniform_mass_newton_cradle(relative_filepath: str, mass: float):
def uniform_mass_newton_cradle(relative_filepath: str,
mass: float) -> list[PendulumEntry]:
"""
Creates a list of pendula and updates them to have the same mass.
......@@ -29,7 +29,8 @@ def uniform_mass_newton_cradle(relative_filepath: str, mass: float):
...
def uniform_e_newton_cradle(relative_filepath: str, e_val: float):
def uniform_e_newton_cradle(relative_filepath: str,
e_val: float) -> list[PendulumEntry]:
"""
Creates a list of pendula and updates them to have the same coefficient of
restitution.
......@@ -48,7 +49,7 @@ def uniform_e_newton_cradle(relative_filepath: str, e_val: float):
...
def elastic_newton_cradle(relative_filepath: str):
def elastic_newton_cradle(relative_filepath: str) -> list[PendulumEntry]:
"""
Creates a list of pendula and updates them to have perfectly elastic
collisions with each other.
......@@ -65,7 +66,7 @@ def elastic_newton_cradle(relative_filepath: str):
...
def inelastic_newton_cradle(relative_filepath: str):
def inelastic_newton_cradle(relative_filepath: str) -> list[PendulumEntry]:
"""
Creates a list of pendula and updates them to have perfectly inelastic
collisions with each other.
......@@ -82,7 +83,9 @@ def inelastic_newton_cradle(relative_filepath: str):
...
def make_newton_cradle(relative_filepath: str, r_param: float, theta_init: float):
def make_newton_cradle(relative_filepath: str,
r_param: float,
theta_init: float) -> list[PendulumEntry]:
"""
Uses information from a specific CSV file to make a list of
dictionaries representing each pendula and their physical attributes.
......
......@@ -2,9 +2,18 @@
Helper functions and implementation of a Newton's cradle simulation.
"""
from vpython import sin, cos, vector
from vpython import ( # type: ignore
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):
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.
......@@ -18,12 +27,16 @@ def angle_update(omega: float, theta: float, r_param: float, dt: float, g: float
Returns:
(tuple[float, float]): time-updated omega, theta
"""
# TODO 2: Update angular components of a pendulum with the derivation provided
# TODO 2: Update angular components of a pendulum with the derivation
# provided
...
# Provided
def swing_update(pend, r_param: float, dt: float, g: float):
def swing_update(pend: PendulumEntry,
r_param: float,
dt: float,
g: float) -> None:
"""
Updates the angle & position of the pendulum components in a single swing.
......@@ -36,17 +49,20 @@ def swing_update(pend, r_param: float, dt: float, g: float):
theta = pend["theta"]
omega = pend["omega"]
axle, bob, cord = pend["objects"]
axle, bob, cord = pend["objects"] # type: ignore
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)
cord.axis = bob.pos - axle.pos
bob.pos = axle.pos + r_param * vector(sin(theta_new), -cos(theta_new), 0) # type: ignore
cord.axis = bob.pos - axle.pos # type: ignore
pend["theta"] = theta_new
pend["omega"] = omega_new
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
pendulum dictionary.
......
......@@ -45,14 +45,4 @@ TEXTURE_MAP: dict[str, VPythonTexture] = {
"stucco": textures.stucco,
"wood": textures.wood,
"wood_old": textures.wood_old,
}
PENDULUM_ATTRIBUTES: list[str] = [
"mass",
"restitution_coeff",
"color",
"texture",
"objects",
"theta",
"omega"
]
}
\ No newline at end of file
......@@ -16,6 +16,36 @@ from vpython import ( # type: ignore
from support.types import Pendulum, VPythonColor, VPythonTexture
def make_objects_of_pendulum(axle_pos: vector,
r_param: float,
theta: float,
bob_color: VPythonColor = color.white,
bob_texture: VPythonTexture = textures.metal,
) -> Pendulum:
"""
Builds a pendulum with a bob, axle and cord.
Args:
axle_pos (VPython vector): position of center of axle
r_param (float): size parameter used to define pendulum components
theta (float); angle of the bob with respect to the axle
bob_color (optional, VPython color): color of bob, defaults to white
bob_texture (optional, VPython texture): texture of bob, defaults to wood
Returns:
(VPython box, VPython sphere, VPython cylinder): pendulum axle, bob, cord
"""
axle = make_axle(axle_pos, r_param)
bob_pos = axle.pos + r_param * vector(sin(theta), -cos(theta), 0)
bob = make_bob(bob_pos, r_param, bob_color, bob_texture)
cord_axis = bob.pos - axle.pos
cord = make_cord(axle.pos, cord_axis, r_param)
return axle, bob, cord
def make_bob(bob_pos: vector,
r_param: float,
clr: VPythonColor,
......@@ -74,33 +104,3 @@ def make_cord(cord_pos: vector,
(VPython cylinder): cylinder representing pendulum cord
"""
return cylinder(pos=cord_pos, axis=axis, radius=r_param/50, color=clr)
def make_objects_of_pendulum(axle_pos: vector,
r_param: float,
theta: float,
bob_color: VPythonColor = color.white,
bob_texture: VPythonTexture = textures.metal,
) -> Pendulum:
"""
Builds a pendulum with a bob, axle and cord.
Args:
axle_pos (VPython vector): position of center of axle
r_param (float): size parameter used to define pendulum components
theta (float); angle of the bob with respect to the axle
bob_color (optional, VPython color): color of bob, defaults to white
bob_texture (optional, VPython texture): texture of bob, defaults to wood
Returns:
(VPython box, VPython sphere, VPython cylinder): pendulum axle, bob, cord
"""
axle = make_axle(axle_pos, r_param)
bob_pos = axle.pos + r_param * vector(sin(theta), -cos(theta), 0)
bob = make_bob(bob_pos, r_param, bob_color, bob_texture)
cord_axis = bob.pos - axle.pos
cord = make_cord(axle.pos, cord_axis, r_param)
return axle, bob, cord
from vpython import box, cylinder, sphere, vector
from pandas import DataFrame, read_csv
from typing import Any, TypedDict
VPythonColor = vector
......@@ -8,8 +8,8 @@ VPythonTexture = str
Pendulum = tuple[box, sphere, cylinder]
class PendulumEntry(TypedDict):
mass: str
restitution_coeffs: str
mass: float
restitution_coeff: float
color: str
texture: str
objects: Pendulum
......
......@@ -72,13 +72,13 @@ def test_handle_two_bobs_no_collision(omega1, omega2, rad1, rad2, pos1, pos2):
bob1 = FakeBob(pos1, rad1, color.white, textures.metal)
pend1 = {
"objects": (1, bob1, 1),
"theta": 0.5, "omega": omega1, "mass": 1, "restitution_coeffs": 1
"theta": 0.5, "omega": omega1, "mass": 1, "restitution_coeff": 1
}
bob2 = FakeBob(pos2, rad2, color.white, textures.metal)
pend2 = {
"objects": (1, bob2, 1),
"theta": 0.1, "omega": omega2, "mass": 2, "restitution_coeffs": 0
"theta": 0.1, "omega": omega2, "mass": 2, "restitution_coeff": 0
}
r1 = 1
......
"""
This is a MyPy stub file for VPython's code modules, to use with MyPy and other
tools that use '.pyi' files to determine static type checking compliance.
Note: It is not complete yet. I only implemented the areas I'm using in VPython.
"""
from __future__ import annotations
from typing import Union, List, Tuple, ClassVar, Iterable, Optional
__author__ = "Jim Tooker"
class vector:
""" From vector.py """
@staticmethod
def random() -> vector: ...
def __init__(self, x: float, y: float, z: float) -> None: ...
def __add__(self, other: vector) -> vector: ...
def __sub__(self, other: vector) -> vector: ...
def __mul__(self, other: Union[float, vector]) -> vector: ...
def __rmul__(self, other: Union[float, vector]) -> vector: ...
def __truediv__(self, other: Union[float, vector]) -> vector: ...
def __neg__(self) -> vector: ...
def __pos__(self) -> vector: ...
def __eq__(self: object, other: object) -> bool: ...
def __neq__(self: object, other: object) -> bool: ...
@property
def x(self) -> float: ...
@x.setter
def x(self, value: float) -> None: ...
@property
def y(self) -> float: ...
@y.setter
def y(self, value: float) -> None: ...
@property
def z(self) -> float: ...
@z.setter
def z(self, value: float) -> None: ...
@property
def mag(self) -> float: ...
@mag.setter
def mag(self, value: float) -> None: ...
@property
def mag2(self) -> float: ...
@mag2.setter
def mag2(self, value: float) -> None: ...
@property
def hat(self) -> vector: ...
@hat.setter
def hat(self, value: vector) -> None: ...
@property
def value(self) -> List[float]: ...
def norm(self) -> vector: ...
def dot(self, other: vector) -> float: ...
def cross(self, other: vector) -> vector: ...
def proj(self, other: vector) -> vector: ...
def equals(self, other: vector) -> bool: ...
def comp(self, other: vector) -> float: ...
def diff_angle(self, other: vector) -> float: ...
def rotate(self, angle: float = ..., axis: Optional[vector] = ...) -> vector: ...
def rotate_in_place(self, angle: float = ..., axis: Optional[vector] = ...) -> None: ...
class color:
""" From vpython.py """
black: ClassVar[vector]
white: ClassVar[vector]
red: ClassVar[vector]
green: ClassVar[vector]
blue: ClassVar[vector]
yellow: ClassVar[vector]
cyan: ClassVar[vector]
magenta: ClassVar[vector]
orange: ClassVar[vector]
purple: ClassVar[vector]
@classmethod
def gray(cls, luminance: float) -> vector: ...
@classmethod
def rgb_to_hsv(cls, v: vector) -> vector: ...
@classmethod
def hsv_to_rgb(cls, v: vector) -> vector: ...
@classmethod
def rgb_to_grayscale(cls, v: vector) -> vector: ...
class Mouse:
...
class Camera:
def rotate(self, angle: float = ..., axis: Optional[vector] = ..., origin: Optional[vector] = ...) -> None: ...
class canvas:
""" From vpython.py """
title_anchor: object
caption_anchor: object
def __init__(
self,
title: str = ...,
caption: str = ...,
width: int = ...,
height: int = ...,
background: vector = ...,
center: vector = ...,
visible: bool = ...,
resizable: bool = ...,
align: str = ...,
pixel_to_world: int = ...,
) -> None: ...
@classmethod
def get_selected(cls) -> canvas: ...
@property
def title(self) -> str: ...
@title.setter
def title(self, value: str) -> None: ...
@property
def caption(self) -> str: ...
@caption.setter
def caption(self, value: str) -> None: ...
@property
def mouse(self) -> Mouse: ...
@property
def camera(self) -> Camera: ...
@property
def visible(self) -> bool: ...
@visible.setter
def visible(self, value: bool) -> None: ...
@property
def resizable(self) -> bool: ...
@resizable.setter
def resizable(self, value: bool) -> None: ...
@property
def background(self) -> vector: ...
@background.setter
def background(self, value: vector) -> None: ...
@property
def ambient(self) -> vector: ...
@ambient.setter
def ambient(self, value: vector) -> None: ...
@property
def width(self) -> int: ...
@width.setter
def width(self, value: int) -> None: ...
@property
def height(self) -> int: ...
@height.setter
def height(self, value: int) -> None: ...
@property
def align(self) -> str: ...
@align.setter
def align(self, value: str) -> None: ...
@property
def center(self) -> vector: ...
@center.setter
def center(self, value: vector) -> None: ...
@property
def axis(self) -> vector: ...
@axis.setter
def axis(self, value: vector) -> None: ...
@property
def forward(self) -> vector: ...
@forward.setter
def forward(self, value: vector) -> None: ...
@property
def range(self) -> int: ...
@range.setter
def range(self, value: int) -> None: ...
@property
def up(self) -> vector: ...
@up.setter
def up(self, value: vector) -> None: ...
@property
def autoscale(self) -> bool: ...
@autoscale.setter
def autoscale(self, value: bool) -> None: ...
@property
def userzoom(self) -> bool: ...
@userzoom.setter
def userzoom(self, value: bool) -> None: ...
@property
def userspin(self) -> bool: ...
@userspin.setter
def userspin(self, value: bool) -> None: ...
@property
def userpan(self) -> bool: ...
@userpan.setter
def userpan(self, value: bool) -> None: ...
@property
def lights(self) -> List[object]: ...
@lights.setter
def lights(self, value: List[object]) -> None: ...
@property
def fov(self) -> float: ...
@fov.setter
def fov(self, value: float) -> None: ...
@property
def objects(self) -> List[object]: ...
@property
def pixel_to_world(self) -> int: ...
@pixel_to_world.setter
def pixel_to_world(self, value: int) -> None: ...
def select(self) -> None: ...
def delete(self) -> None: ...
def follow(self, obj: object) -> None: ...
def append_to_title(self, title: str) -> None: ...
def append_to_caption(self, caption: str) -> None: ...
def objz(self, obj: object, operation: str) -> None: ...
class sphere:
""" From vpython.py """
pos: vector
visible: bool
make_trail: bool
def __init__(
self,
pos: vector = ...,
radius: float = ...,
color: vector = ...,
size: vector = ...,
axis: vector = ...,
opacity: float = ...,
shininess: float = ...,
emissive: bool = ...,
texture: object = ...,
visible: bool = ...,
canvas: canvas = ...,
make_trail: bool = ...,
up: vector = ...,
group: object = ...,
trail_type: str = ...,
trail_radius: float = ...,
interval: int = ...,
retain: int = ...,
trail_color: vector = ...,
) -> None: ...
@property
def radius(self) -> float: ...
@radius.setter
def radius(self, value: float) -> None: ...
@property
def size(self) -> vector: ...
@size.setter
def size(self, value: vector) -> None: ...
@property
def axis(self) -> vector: ...
@axis.setter
def axis(self, value: vector) -> None: ...
@property
def trail_type(self) -> str: ...
@trail_type.setter
def trail_type(self, value: str) -> None: ...
@property
def trail_radius(self) -> float: ...
@trail_radius.setter
def trail_radius(self, value: float) -> None: ...
@property
def interval(self) -> int: ...
@interval.setter
def interval(self, value: int) -> None: ...
@property
def retain(self) -> int: ...
@retain.setter
def retain(self, value: int) -> None: ...
@property
def trail_color(self) -> vector: ...
@trail_color.setter
def trail_color(self, value: vector) -> None: ...
@property
def emissive(self) -> bool: ...
@emissive.setter
def emissive(self, value: bool) -> None: ...
@property
def shininess(self) -> float: ...
@shininess.setter
def shininess(self, value: float) -> None: ...
def clear_trail(self) -> None: ...
def rotate(self, angle: float = ..., axis: Optional[vector] = ..., origin: Optional[vector] = ...) -> None: ...
class ellipsoid:
""" From vpython.py """
pos: vector
visible: bool
length: float
width: float
height: float
size: vector
axis: vector
make_trail: bool
def __init__(
self,
pos: vector = ...,
color: vector = ...,
length: float = ...,
height: float = ...,
width: float = ...,
size: vector = ...,
axis: vector = ...,
opacity: float = ...,
shininess: float = ...,
emissive: bool = ...,
visible: bool = ...,
canvas: canvas = ...,
make_trail: bool = ...,
up: vector = ...,
group: object = ...,
) -> None: ...
@property
def radius(self) -> float: ...
@radius.setter
def radius(self, value: float) -> None: ...
def clear_trail(self) -> None: ...
def rotate(self, angle: float = ..., axis: Optional[vector] = ..., origin: Optional[vector] = ...) -> None: ...
class label:
""" From vpython.py """
pos: vector
visible: bool
def __init__(
self,
pos: vector = ...,
text: str = ...,
color: vector = ...,
align: str = ...,
xoffset: int = ...,
yoffset: int = ...,
font: str = ...,
background: vector = ...,
opacity: float = ...,
box: bool = ...,
border: int = ...,
line: bool = ...,
linecolor: vector = ...,
linewidth: int = ...,
space: int = ...,
pixel_pos: bool = ...,
height: int = ...,
visible: bool = ...
) -> None: ...
@property
def xoffset(self) -> int: ...
@xoffset.setter
def xoffset(self, value: int) -> None: ...
@property
def yoffset(self) -> int: ...
@yoffset.setter
def yoffset(self, value: int) -> None: ...
@property
def text(self) -> str: ...
@text.setter
def text(self, value: str) -> None: ...
@property
def align(self) -> str: ...
@align.setter
def align(self, value: str) -> None: ...
@property
def font(self) -> str: ...
@font.setter
def font(self, value: str) -> None: ...
@property
def height(self) -> int: ...
@height.setter
def height(self, value: int) -> None: ...
@property
def background(self) -> vector: ...
@background.setter
def background(self, value: vector) -> None: ...
@property
def border(self) -> int: ...
@border.setter
def border(self, value: int) -> None: ...
@property
def box(self) -> bool: ...
@box.setter
def box(self, value: bool) -> None: ...
@property
def line(self) -> bool: ...
@line.setter
def line(self, value: bool) -> None: ...
@property
def linewidth(self) -> int: ...
@linewidth.setter
def linewidth(self, value: int) -> None: ...
@property
def linecolor(self) -> vector: ...
@linecolor.setter
def linecolor(self, value: vector) -> None: ...
@property
def space(self) -> int: ...
@space.setter
def space(self, value: int) -> None: ...
@property
def pixel_pos(self) -> bool: ...
@pixel_pos.setter
def pixel_pos(self, value: bool) -> None: ...
class curve:
""" From vpython.py """
def __init__(
self,
pos: List[vector] = ...,
color: vector = ...,
radius: float = ...,
size: vector = ...,
origin: vector = ...,
axis: vector = ...,
) -> None: ...
def append(self, pos: vector) -> None: ...
def modify(self, index: int, pos: vector) -> None: ...
def clear(self) -> None: ...
class graph:
""" From vpython.py """
def __init__(
self,
title: str = ...,
xtitle: str = ...,
ytitle: str = ...,
xmin: float = ...,
xmax: float = ...,
ymin: float = ...,
ymax: float = ...,
width: int = ...,
height: int = ...,
background: vector = ...,
foreground: vector = ...,
align: str = ...,
scroll: bool = ...,
fast: bool = ...,
logx: bool = ...,
logy: bool = ...,
) -> None: ...
@classmethod
def get_selected(cls) -> 'graph': ...
@property
def interval(self) -> int: ...
@interval.setter
def interval(self, value: int) -> None: ...
def select(self) -> None: ...
def delete(self) -> None: ...
class gcurve:
""" From vpython.py """
color: vector
label: str
legend: bool
width: int
visible: bool
data: List[Union[Tuple[float, float], List[float]]]
def __init__(
self,
color: vector = ...,
label: str = ...,
legend: bool = ...,
width: int = ...,
markers: bool = ...,
marker_color: vector = ...,
dot: bool = ...,
dot_radius: int = ...,
dot_color: vector = ...,
visible: bool = ...,
data: List[Union[Tuple[float, float], List[float]]] = ...,
graph: graph = ...,
) -> None: ...
@property
def markers(self) -> bool: ...
@markers.setter
def markers(self, value: bool) -> None: ...
@property
def marker_color(self) -> vector: ...
@marker_color.setter
def marker_color(self, value: vector) -> None: ...
@property
def dot(self) -> bool: ...
@dot.setter
def dot(self, value: bool) -> None: ...
@property
def dot_radius(self) -> int: ...
@dot_radius.setter
def dot_radius(self, value: int) -> None: ...
@property
def dot_color(self) -> vector: ...
@dot_color.setter
def dot_color(self, value: vector) -> None: ...
def plot(self, x: float, y: float) -> None: ...
def delete(self) -> None: ...
class cylinder:
""" From vpython.py """
pos: vector
visible: bool
make_trail: bool
def __init__(
self,
pos: vector = ...,
axis: vector = ...,
color: vector = ...,
radius: float = ...,
length: float = ...,
size: vector = ...,
opacity: float = ...,
shininess: float = ...,
emissive: bool = ...,
texture: object = ...,
visible: bool = ...,
canvas: canvas = ...,
make_trail: bool = ...,
up: vector = ...,
group: object = ...,
) -> None: ...
@property
def radius(self) -> float: ...
@radius.setter
def radius(self, value: float) -> None: ...
@property
def size(self) -> vector: ...
@size.setter
def size(self, value: vector) -> None: ...
@property
def axis(self) -> vector: ...
@axis.setter
def axis(self, value: vector) -> None: ...
def clear_trail(self) -> None: ...
def rotate(self, angle: float = ..., axis: Optional[vector] = ..., origin: Optional[vector] = ...) -> None: ...
class arrow:
""" From vpython.py """
pos: vector
axis: vector
visible: bool
make_trail: bool
def __init__(
self,
pos: vector = ...,
axis: vector = ...,
color: vector = ...,
round: bool = ...,
shaftwidth: float = ...,
headwidth: float = ...,
headlength: float = ...,
opacity: float = ...,
shininess: float = ...,
emissive: bool = ...,
texture: object = ...,
visible: bool = ...,
canvas: canvas = ...,
make_trail: bool = ...,
up: vector = ...,
group: object = ...,
) -> None: ...
@property
def round(self) -> bool: ...
@property
def shaftwidth(self) -> float: ...
@shaftwidth.setter
def shaftwidth(self, value: float) -> None: ...
@property
def headwidth(self) -> float: ...
@headwidth.setter
def headwidth(self, value: float) -> None: ...
@property
def headlength(self) -> float: ...
@headlength.setter
def headlength(self, value: float) -> None: ...
@property
def scale(self) -> float: ...
@scale.setter
def scale(self, value: float) -> None: ...
def stop(self) -> None: ...
def start(self) -> None: ...
def clear_trail(self) -> None: ...
def rotate(self, angle: float = ..., axis: Optional[vector] = ..., origin: Optional[vector] = ...) -> None: ...
class textures:
""" From vpython.py """
flower: str
granite: str
gravel: str
earth: str
metal: str
rock: str
rough: str
rug: str
stones: str
stucco: str
wood: str
wood_old: str
class button:
""" From vpython.py """
disabled: bool
def __init__(
self,
bind: object = ...,
text: str = ...,
pos: object = ...,
color: vector = ...,
background: vector = ...,
disabled: bool = ...,
) -> None: ...
@property
def text(self) -> str: ...
@text.setter
def text(self, value: str) -> None: ...
@property
def color(self) -> vector: ...
@color.setter
def color(self, value: vector) -> None: ...
@property
def background(self) -> vector: ...
@background.setter
def background(self, value: vector) -> None: ...
def delete(self) -> None: ...
class local_light:
""" From vpython.py """
pos: vector
color: vector
def __init__(
self,
pos: vector = ...,
color: vector = ...,
) -> None: ...
class distant_light:
""" From vpython.py """
direction: vector
color: vector
def __init__(
self,
direction: vector = ...,
color: vector = ...,
) -> None: ...
""" vector.py module methods """
def mag(A: vector) -> float: ...
def mag2(A: vector) -> float: ...
def norm(A: vector) -> vector: ...
def hat(A: vector) -> vector: ...
def cross(A: vector) -> vector: ...
def proj(A: vector) -> vector: ...
def dot(A: vector, B: vector) -> float: ...
def comp(A: vector) -> float: ...
def diff_angle(A: vector, B: vector) -> float: ...
def rotate(A: vector, angle: float = ..., axis: Optional[vector] = ...) -> vector: ...
def adjust_up(oldaxis: vector, newaxis: vector, up: vector, save_oldaxis: Optional[vector]) -> Optional[vector]: ...
def adjust_axis(oldup: vector, newup: vector, axis: vector, save_oldup: Optional[vector]) -> Optional[vector]: ...
def object_rotate(objaxis: vector, objup: vector, angle: float, axis: vector) -> None: ...
""" vpython.py module methods """
def rate(A: int) -> None: ...
def arange(A: int, B: int, step: int) -> Iterable[int]: ...
......@@ -4,6 +4,7 @@ Helper functions and implementation of a Newton's cradle simulation.
from vpython import mag, scene, rate
from support.types import PendulumEntry
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
......@@ -11,7 +12,12 @@ from src.swinging import full_swing_update
scene.title = "Newton's cradle"
def handle_collision(e: float, omega1: float, omega2: float, m1: float, m2: float, r_param: float):
def handle_collision(e: float,
omega1: float,
omega2: float,
m1: float,
m2: float,
r_param: float) -> tuple[float, float]:
"""
In case two bobs collide, handles the physics (momentum conservation).
......@@ -29,14 +35,17 @@ def handle_collision(e: float, omega1: float, omega2: float, m1: float, m2: floa
# TODO 4: Use the physics derivation provided to update velocities post collision
...
def handle_two_bobs(pend1, pend2, r_param):
def handle_two_bobs(pend1: PendulumEntry,
pend2: PendulumEntry,
r_param: float) -> None:
"""
Handles the collisions and updates of 2 pendula in the cradle.
Args:
pend1 (dict): dictionary representing 1st pendulum,
pend1 (PendulumEntry): dictionary representing 1st pendulum,
with keys as physical attributes
pend2 (dict): dictionary representing 1st pendulum,
pend2 (PendulumEntry): dictionary representing 2nd pendulum,
with keys as physical attributes
r_param (float): size parameter used to define pendulum components
"""
......@@ -44,13 +53,16 @@ def handle_two_bobs(pend1, pend2, r_param):
...
def newton_cradle_tick(pend_list, r_param, dt, g):
def newton_cradle_tick(pend_list: list[PendulumEntry],
r_param: float,
dt: float,
g: float) -> None:
"""
Performs one 'tick' of the entire Newton's cradle (propagates any
motion throughout all the pendula).
Args:
pend_list (list[dict]): list of pendulum dictionaries,
pend_list (list[PendulumEntry]): list of pendulum dictionaries,
with keys as physical attributes
r_param (float): size parameter used to define pendulum components
dt (float): time step of update
......
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