Pratyush-01's picture
Initial physix-live source for HF Jobs training
b99b9ee verified
"""Tier-1 physical systems: simple, single-variable, no damping."""
from __future__ import annotations
import numpy as np
from physix.systems.base import PhysicalSystem, SystemTier
class FreeFall(PhysicalSystem):
"""Simple free fall under constant gravity.
Equation of motion: ``d2y/dt2 = -g``.
State variables: ``y`` (vertical position), ``vy`` (vertical velocity).
"""
system_id: str = "free_fall"
tier: SystemTier = SystemTier.TIER_1
state_variables: tuple[str, ...] = ("y", "vy")
duration: float = 3.0 # short enough that the object does not pass y=0
hint_template: str = (
"Object dropped near Earth's surface in vacuum. "
"Mass {mass:.1f} kg, released from rest at altitude {y0:.1f} m."
)
def sample_parameters(self, rng: np.random.Generator) -> dict[str, float]:
return {
"g": 9.81,
"mass": float(rng.uniform(0.5, 5.0)),
}
def sample_initial_conditions(self, rng: np.random.Generator) -> dict[str, float]:
return {
"y": float(rng.uniform(20.0, 60.0)),
"vy": 0.0,
}
def rhs(
self,
t: float,
state: np.ndarray,
params: dict[str, float],
) -> np.ndarray:
_y, vy = state
return np.array([vy, -params["g"]], dtype=float)
def ground_truth_equation(self) -> str:
return "d2y/dt2 = -g"
def hint(self, parameters: dict[str, float]) -> str:
ic = self.initial_conditions or {"y": 30.0}
return self.hint_template.format(mass=parameters["mass"], y0=ic["y"])
class FreeFallWithDrag(PhysicalSystem):
"""Free fall with quadratic air drag — the demo system.
Equation of motion: ``d2y/dt2 = -g + k * vy**2`` (drag opposes motion;
when ``vy < 0`` the ``vy**2`` term provides a positive deceleration).
"""
system_id: str = "free_fall_drag"
tier: SystemTier = SystemTier.TIER_1
state_variables: tuple[str, ...] = ("y", "vy")
duration: float = 6.0 # long enough to clearly see terminal-velocity onset
hint_template: str = (
"Object dropped from altitude {y0:.1f} m, mass {mass:.1f} kg, "
"in air. Air resistance may be non-negligible."
)
def sample_parameters(self, rng: np.random.Generator) -> dict[str, float]:
return {
"g": 9.81,
"mass": float(rng.uniform(1.0, 3.0)),
# Drag coefficient tuned so terminal velocity is reached within ~5s
# for the altitudes we sample.
"k": float(rng.uniform(0.02, 0.10)),
}
def sample_initial_conditions(self, rng: np.random.Generator) -> dict[str, float]:
return {
"y": float(rng.uniform(40.0, 80.0)),
"vy": 0.0,
}
def rhs(
self,
t: float,
state: np.ndarray,
params: dict[str, float],
) -> np.ndarray:
_y, vy = state
# vy is negative on descent; vy**2 keeps the magnitude correct.
return np.array([vy, -params["g"] + params["k"] * vy * vy], dtype=float)
def ground_truth_equation(self) -> str:
return "d2y/dt2 = -g + k * vy**2"
def hint(self, parameters: dict[str, float]) -> str:
ic = self.initial_conditions or {"y": 50.0}
return self.hint_template.format(mass=parameters["mass"], y0=ic["y"])
class SimplePendulum(PhysicalSystem):
"""Idealised pendulum (small or large angle), no damping.
Equation of motion: ``d2theta/dt2 = -(g / L) * sin(theta)``.
"""
system_id: str = "simple_pendulum"
tier: SystemTier = SystemTier.TIER_1
state_variables: tuple[str, ...] = ("theta", "dtheta")
hint_template: str = (
"Simple pendulum of length {L:.2f} m swinging in vacuum. "
"No friction, no air resistance."
)
def sample_parameters(self, rng: np.random.Generator) -> dict[str, float]:
return {
"g": 9.81,
"L": float(rng.uniform(0.5, 2.0)),
}
def sample_initial_conditions(self, rng: np.random.Generator) -> dict[str, float]:
return {
"theta": float(rng.uniform(0.3, 1.0)), # ~17-57 degrees
"dtheta": 0.0,
}
def rhs(
self,
t: float,
state: np.ndarray,
params: dict[str, float],
) -> np.ndarray:
theta, dtheta = state
return np.array(
[dtheta, -(params["g"] / params["L"]) * np.sin(theta)],
dtype=float,
)
def ground_truth_equation(self) -> str:
return "d2theta/dt2 = -(g / L) * sin(theta)"