File size: 3,852 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
"""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"