File size: 4,580 Bytes
b99b9ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | """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)"
|