File size: 4,215 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 | """Tier-3 physical systems: held out of training to support a generalisation
claim ("converges on systems it never trained on")."""
from __future__ import annotations
import numpy as np
from physix.systems.base import PhysicalSystem, SystemTier
class ProjectileWithDrag(PhysicalSystem):
"""2-D projectile with quadratic air drag.
Equations of motion::
d2x/dt2 = -k * |v| * vx
d2y/dt2 = -g - k * |v| * vy
where ``|v| = sqrt(vx**2 + vy**2)``.
"""
system_id: str = "projectile_drag"
tier: SystemTier = SystemTier.TIER_3
state_variables: tuple[str, ...] = ("x", "y", "vx", "vy")
duration: float = 5.0 # typical flight time for the parameter ranges below
hint_template: str = (
"Projectile launched at angle {angle_deg:.0f} degrees with initial "
"speed {v0:.1f} m/s. Air drag is non-negligible."
)
def sample_parameters(self, rng: np.random.Generator) -> dict[str, float]:
return {
"g": 9.81,
"k": float(rng.uniform(0.005, 0.02)),
}
def sample_initial_conditions(self, rng: np.random.Generator) -> dict[str, float]:
speed = float(rng.uniform(15.0, 30.0))
angle = float(rng.uniform(np.deg2rad(30.0), np.deg2rad(70.0)))
return {
"x": 0.0,
"y": 0.0,
"vx": float(speed * np.cos(angle)),
"vy": float(speed * np.sin(angle)),
}
def rhs(
self,
t: float,
state: np.ndarray,
params: dict[str, float],
) -> np.ndarray:
_x, _y, vx, vy = state
speed = float(np.sqrt(vx * vx + vy * vy))
return np.array(
[
vx,
vy,
-params["k"] * speed * vx,
-params["g"] - params["k"] * speed * vy,
],
dtype=float,
)
def ground_truth_equation(self) -> str:
# Two-equation system rendered in a single string so the parser can
# split on ';' or '\n'. Verifier handles both delimiters.
return (
"d2x/dt2 = -k*sqrt(vx**2 + vy**2)*vx; "
"d2y/dt2 = -g - k*sqrt(vx**2 + vy**2)*vy"
)
def hint(self, parameters: dict[str, float]) -> str:
ic = self.initial_conditions
if not ic:
return self.hint_template
v0 = float(np.sqrt(ic["vx"] ** 2 + ic["vy"] ** 2))
angle_deg = float(np.rad2deg(np.arctan2(ic["vy"], ic["vx"])))
return self.hint_template.format(angle_deg=angle_deg, v0=v0)
class ChargedInBField(PhysicalSystem):
"""Charged particle in a uniform magnetic field along z (circular motion).
Equations of motion (assuming B = B_z * ẑ and v in xy-plane)::
d2x/dt2 = (q*B/m) * vy
d2y/dt2 = -(q*B/m) * vx
"""
system_id: str = "charged_b_field"
tier: SystemTier = SystemTier.TIER_3
state_variables: tuple[str, ...] = ("x", "y", "vx", "vy")
hint_template: str = (
"Charged particle in a uniform magnetic field. Charge-to-mass ratio "
"q/m = {qm:.2f}, field strength {B:.2f} T."
)
def sample_parameters(self, rng: np.random.Generator) -> dict[str, float]:
return {
"q": float(rng.choice([-1.0, 1.0])),
"m": float(rng.uniform(0.5, 2.0)),
"B": float(rng.uniform(0.5, 2.0)),
}
def sample_initial_conditions(self, rng: np.random.Generator) -> dict[str, float]:
return {
"x": 0.0,
"y": 0.0,
"vx": float(rng.uniform(0.5, 2.0)),
"vy": float(rng.uniform(-2.0, 2.0)),
}
def rhs(
self,
t: float,
state: np.ndarray,
params: dict[str, float],
) -> np.ndarray:
_x, _y, vx, vy = state
omega = params["q"] * params["B"] / params["m"]
return np.array([vx, vy, omega * vy, -omega * vx], dtype=float)
def ground_truth_equation(self) -> str:
return (
"d2x/dt2 = (q*B/m)*vy; "
"d2y/dt2 = -(q*B/m)*vx"
)
def hint(self, parameters: dict[str, float]) -> str:
qm = parameters["q"] / parameters["m"]
return self.hint_template.format(qm=qm, B=parameters["B"])
|