"""Tier-2 physical systems: damped or with a second active force term.""" from __future__ import annotations import numpy as np from physix.systems.base import PhysicalSystem, SystemTier class DampedPendulum(PhysicalSystem): """Pendulum with linear angular damping. Equation of motion: ``d2theta/dt2 = -(g/L)*sin(theta) - b*dtheta``. """ system_id: str = "damped_pendulum" tier: SystemTier = SystemTier.TIER_2 state_variables: tuple[str, ...] = ("theta", "dtheta") hint_template: str = ( "Pendulum of length {L:.2f} m. Oscillation amplitude visibly decreases " "over time, suggesting linear angular damping." ) def sample_parameters(self, rng: np.random.Generator) -> dict[str, float]: return { "g": 9.81, "L": float(rng.uniform(0.5, 2.0)), "b": float(rng.uniform(0.05, 0.30)), } def sample_initial_conditions(self, rng: np.random.Generator) -> dict[str, float]: return { "theta": float(rng.uniform(0.3, 1.0)), "dtheta": 0.0, } def rhs( self, t: float, state: np.ndarray, params: dict[str, float], ) -> np.ndarray: theta, dtheta = state d2theta = -(params["g"] / params["L"]) * np.sin(theta) - params["b"] * dtheta return np.array([dtheta, d2theta], dtype=float) def ground_truth_equation(self) -> str: return "d2theta/dt2 = -(g/L)*sin(theta) - b*dtheta" class SpringMass(PhysicalSystem): """Undamped harmonic oscillator. Equation of motion: ``d2x/dt2 = -(k/m) * x``. """ system_id: str = "spring_mass" tier: SystemTier = SystemTier.TIER_2 state_variables: tuple[str, ...] = ("x", "vx") hint_template: str = ( "Mass {m:.2f} kg attached to a spring of stiffness {k:.2f} N/m, " "frictionless surface." ) def sample_parameters(self, rng: np.random.Generator) -> dict[str, float]: return { "k": float(rng.uniform(2.0, 20.0)), "m": float(rng.uniform(0.5, 2.0)), } def sample_initial_conditions(self, rng: np.random.Generator) -> dict[str, float]: return { "x": float(rng.uniform(0.5, 2.0)), "vx": 0.0, } def rhs( self, t: float, state: np.ndarray, params: dict[str, float], ) -> np.ndarray: x, vx = state return np.array([vx, -(params["k"] / params["m"]) * x], dtype=float) def ground_truth_equation(self) -> str: return "d2x/dt2 = -(k/m) * x" class DampedSpring(PhysicalSystem): """Damped harmonic oscillator. Equation of motion: ``d2x/dt2 = -(k/m)*x - (c/m)*vx``. """ system_id: str = "damped_spring" tier: SystemTier = SystemTier.TIER_2 state_variables: tuple[str, ...] = ("x", "vx") hint_template: str = ( "Mass {m:.2f} kg on a spring of stiffness {k:.2f} N/m with viscous " "damping coefficient {c:.2f}. Oscillation amplitude decays over time." ) def sample_parameters(self, rng: np.random.Generator) -> dict[str, float]: return { "k": float(rng.uniform(2.0, 20.0)), "m": float(rng.uniform(0.5, 2.0)), "c": float(rng.uniform(0.1, 1.0)), } def sample_initial_conditions(self, rng: np.random.Generator) -> dict[str, float]: return { "x": float(rng.uniform(0.5, 2.0)), "vx": 0.0, } def rhs( self, t: float, state: np.ndarray, params: dict[str, float], ) -> np.ndarray: x, vx = state d2x = -(params["k"] / params["m"]) * x - (params["c"] / params["m"]) * vx return np.array([vx, d2x], dtype=float) def ground_truth_equation(self) -> str: return "d2x/dt2 = -(k/m)*x - (c/m)*vx"