From 916a5199ec33ea1bbf5d18c1ba4abfaeed7bad1c Mon Sep 17 00:00:00 2001 From: Srikar-Kolli Date: Sun, 15 Nov 2026 14:43:30 -0500 Subject: [PATCH 1/2] Rename car parameter keys to include units (#54) mass->mass_kg, battery_capacity->battery_capacity_wh, solar_panel_area->solar_panel_area_m2, C_dA->cda_m2, rho->air_density_kg_per_m3, tire_pressure->tire_pressure_bar. Legacy names still load from JSON. Docs and tests updated. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- car_params.json | 12 +++---- docs/scenario_inputs.md | 28 +++++++++++---- interactive_laps_sim.py | 4 +-- sim/config.py | 59 ++++++++++++++++++++++--------- sim/controllers.py | 4 +-- sim/physics.py | 12 +++---- sim/planning/comparison.py | 6 ++-- sim/planning/report.py | 4 +-- sim/scenarios.py | 15 ++++---- sim/simulator.py | 4 +-- tests/planning/test_comparison.py | 8 ++--- tests/scenarios_test.py | 38 ++++++++++---------- tests/test_config.py | 21 +++++++++-- tests/test_physics.py | 16 ++++----- tests/test_simulator.py | 2 +- tests/test_time_step.py | 6 ++-- 17 files changed, 150 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 336690c..d534264 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,7 @@ Vehicle parameters are loaded from: car_params.json ``` -Typical parameters include mass, battery capacity, solar panel area, solar panel +Typical parameters include mass (`mass_kg`), battery capacity (`battery_capacity_wh`), solar panel area, solar panel efficiency, drag, rolling resistance inputs, and regenerative braking efficiency. diff --git a/car_params.json b/car_params.json index e41e8f9..eaa7f14 100644 --- a/car_params.json +++ b/car_params.json @@ -4,12 +4,12 @@ "electrical_efficiency": 0.99, "solar_panel_efficiency": 0.23, "wheels": 4, - "mass": 337, - "battery_capacity": 5000, + "mass_kg": 337, + "battery_capacity_wh": 5000, "battery_voltage": "inf", - "solar_panel_area": 4, - "C_dA": 0.73809, - "rho": 1.192, + "solar_panel_area_m2": 4, + "cda_m2": 0.73809, + "air_density_kg_per_m3": 1.192, "v_starting": 90, - "tire_pressure": 5 + "tire_pressure_bar": 5 } diff --git a/docs/scenario_inputs.md b/docs/scenario_inputs.md index 358d883..96ff022 100644 --- a/docs/scenario_inputs.md +++ b/docs/scenario_inputs.md @@ -27,17 +27,33 @@ Source: `car_params.json` | Parameter | Units | Required | Affects | |---|---:|---|---| -| `mass` | kg | Yes | Rolling resistance, Regen energy, total energy use | -| `battery_capacity` | Wh | Yes | Available battery energy, SOC changes | -| `solar_panel_area` | m² | Yes | Solar power collected | +| `mass_kg` | kg | Yes | Rolling resistance, Regen energy, total energy use | +| `battery_capacity_wh` | Wh | Yes | Available battery energy, SOC changes | +| `solar_panel_area_m2` | m² | Yes | Solar power collected | | `solar_panel_efficiency` | fraction | Yes | Solar power collected | | `electrical_efficiency` | fraction | Yes | Driving power consumption | -| `C_dA` | m² | Yes | Aerodynamic drag, energy use | -| `tire_pressure` | pressure units | Yes | Rolling resistance, energy use | +| `cda_m2` | m² | Yes | Aerodynamic drag, energy use | +| `tire_pressure_bar` | bar | Yes | Rolling resistance, energy use | | `regen_efficiency` | fraction | Yes | Regen energy | -| `rho` | kg/m³ | Yes | Aerodynamic drag | +| `air_density_kg_per_m3` | kg/m³ | Yes | Aerodynamic drag | | `g` | m/s² | Yes | Rolling resistance | +Tire pressure is in bar: the rolling-resistance formula in `sim/physics.py` +(`Crr = 0.005 + (1/P)(0.01 + 0.0095(v/100)²)`) expects bar. + +**Migration:** parameters were renamed to include units. Old names are still +accepted when loading JSON files (`CarConfig.from_json` and the scenario +loader), but new files should use the new names: + +| Old name | New name | +|---|---| +| `mass` | `mass_kg` | +| `battery_capacity` | `battery_capacity_wh` | +| `solar_panel_area` | `solar_panel_area_m2` | +| `C_dA` | `cda_m2` | +| `rho` | `air_density_kg_per_m3` | +| `tire_pressure` | `tire_pressure_bar` | + ### Course Configuration Course definitions are stored as separate JSON files in the repository's diff --git a/interactive_laps_sim.py b/interactive_laps_sim.py index 614c7b7..08a1717 100644 --- a/interactive_laps_sim.py +++ b/interactive_laps_sim.py @@ -139,7 +139,7 @@ def project_remaining(self): power_out = self.physics.power_drained(speed) energy_delta = (power_in - power_out) * dt_hours - soc += energy_delta / self.car.battery_capacity + soc += energy_delta / self.car.battery_capacity_wh soc = max(soc, self.race.min_soc) dist_step = speed * (self.race.time_step_minutes * 60) @@ -157,7 +157,7 @@ def project_remaining(self): # Apply regenerative braking energy if decelerating regen_wh = self.physics.regen_energy(prev_speed, speed) if regen_wh > 0: - soc += regen_wh / self.car.battery_capacity + soc += regen_wh / self.car.battery_capacity_wh soc = min(soc, 1.0) # Cap at 100% speeds[i] = speed diff --git a/sim/config.py b/sim/config.py index 9d80669..8a2c259 100644 --- a/sim/config.py +++ b/sim/config.py @@ -73,30 +73,56 @@ def from_json(cls, filepath: str) -> "TrackConfig": ) +# Old parameter names -> current unit-suffixed names. Old names are still +# accepted when loading JSON files so existing parameter files keep working. +LEGACY_CAR_PARAM_NAMES = { + "mass": "mass_kg", + "battery_capacity": "battery_capacity_wh", + "solar_panel_area": "solar_panel_area_m2", + "C_dA": "cda_m2", + "rho": "air_density_kg_per_m3", + "tire_pressure": "tire_pressure_bar", +} + + +def migrate_legacy_car_params(params: dict) -> dict: + """Return a copy of params with legacy keys renamed to the new names. + + If both the old and new name are present, the new name wins. + """ + migrated = {} + for key, value in params.items(): + new_key = LEGACY_CAR_PARAM_NAMES.get(key, key) + if new_key != key and new_key in params: + continue + migrated[new_key] = value + return migrated + + @dataclass class CarConfig: - mass: float = 337 - battery_capacity: float = 5000 - solar_panel_area: float = 4 + mass_kg: float = 337 + battery_capacity_wh: float = 5000 + solar_panel_area_m2: float = 4 solar_panel_efficiency: float = 0.23 electrical_efficiency: float = 0.99 - C_dA: float = 0.73809 - tire_pressure: float = 5 + cda_m2: float = 0.73809 + tire_pressure_bar: float = 5 regen_efficiency: float = 0.5 - rho: float = 1.192 + air_density_kg_per_m3: float = 1.192 g: float = 9.80665 def __post_init__(self): self.validate() def validate(self): - if self.mass <= 0: + if self.mass_kg <= 0: raise ValueError("Mass must be greater than 0.") - if self.tire_pressure <= 0: + if self.tire_pressure_bar <= 0: raise ValueError("Tire pressure must be greater than 0.") - if self.battery_capacity <= 0: + if self.battery_capacity_wh <= 0: raise ValueError("Battery capacity must be greater than 0.") - if self.solar_panel_area < 0: + if self.solar_panel_area_m2 < 0: raise ValueError("Solar panel area must be non-negative.") if not (0 <= self.solar_panel_efficiency <= 1): raise ValueError("Solar panel efficiency must be between 0 and 1.") @@ -110,20 +136,21 @@ def from_json(cls, filepath: str) -> "CarConfig": with open(filepath, "r") as f: params = json.load(f) + params = migrate_legacy_car_params(params) for key, value in params.items(): if value == "inf": params[key] = math.inf return cls( - mass=params.get("mass", 337), - battery_capacity=params.get("battery_capacity", 5000), - solar_panel_area=params.get("solar_panel_area", 4), + mass_kg=params.get("mass_kg", 337), + battery_capacity_wh=params.get("battery_capacity_wh", 5000), + solar_panel_area_m2=params.get("solar_panel_area_m2", 4), solar_panel_efficiency=params.get("solar_panel_efficiency", 0.23), electrical_efficiency=params.get("electrical_efficiency", 0.99), - C_dA=params.get("C_dA", 0.73809), - tire_pressure=params.get("tire_pressure", 5), + cda_m2=params.get("cda_m2", 0.73809), + tire_pressure_bar=params.get("tire_pressure_bar", 5), regen_efficiency=params.get("regen_efficiency", 0.5), - rho=params.get("rho", 1.192), + air_density_kg_per_m3=params.get("air_density_kg_per_m3", 1.192), g=params.get("g", 9.80665), ) diff --git a/sim/controllers.py b/sim/controllers.py index 4fb13ed..070ad70 100644 --- a/sim/controllers.py +++ b/sim/controllers.py @@ -28,7 +28,7 @@ def compute_ideal_soc_curve(cloud_data: np.ndarray, race_config, alpha: float = # Find a constant speed that consumes the total available race energy budget. # Used as baseline speed for SOC feedback strategies to adjust around. def solve_optimal_constant_speed(physics, race_config: RaceConfig, ghi_data: np.ndarray, dt_hours: float) -> float: - cap = physics.car.battery_capacity + cap = physics.car.battery_capacity_wh usable_energy = (race_config.start_soc - race_config.target_soc) * cap total_solar = np.sum(physics.solar_power(ghi_data)) * dt_hours total_available = usable_energy + total_solar @@ -136,7 +136,7 @@ def __init__(self, race_config, interval_minutes: int = 5): # Remaining budget = usable battery above target SOC + expected solar input. def _solve_remaining_speed(self, current_soc: float, physics, ghi_remaining: np.ndarray, dt_hours: float) -> float: - cap = physics.car.battery_capacity + cap = physics.car.battery_capacity_wh usable_energy = max(0.0, (current_soc - self.config.target_soc) * cap) solar_energy = np.sum(physics.solar_power(ghi_remaining)) * dt_hours total_available = usable_energy + solar_energy diff --git a/sim/physics.py b/sim/physics.py index d5a3bd1..35de830 100644 --- a/sim/physics.py +++ b/sim/physics.py @@ -7,13 +7,13 @@ def __init__(self, car): def get_crr(self, v: float) -> float: v_kmh = v * 3.6 - return 0.005 + (1 / self.car.tire_pressure) * (0.01 + 0.0095 * (v_kmh / 100) ** 2) + return 0.005 + (1 / self.car.tire_pressure_bar) * (0.01 + 0.0095 * (v_kmh / 100) ** 2) def force_drag(self, v: float) -> float: - return 0.5 * self.car.C_dA * self.car.rho * v ** 2 + return 0.5 * self.car.cda_m2 * self.car.air_density_kg_per_m3 * v ** 2 def force_rolling_resistance(self, v: float) -> float: - return self.car.mass * self.car.g * self.get_crr(v) + return self.car.mass_kg * self.car.g * self.get_crr(v) def power_drained(self, v: float) -> float: f_d = self.force_drag(v) @@ -21,15 +21,15 @@ def power_drained(self, v: float) -> float: return (f_d + f_r) * v * self.car.electrical_efficiency def solar_power(self, ghi: float) -> float: - return self.car.solar_panel_area * ghi * self.car.solar_panel_efficiency + return self.car.solar_panel_area_m2 * ghi * self.car.solar_panel_efficiency def regen_energy(self, v_old: float, v_new: float) -> float: if v_new >= v_old: return 0.0 - delta_ke = 0.5 * self.car.mass * (v_old ** 2 - v_new ** 2) + delta_ke = 0.5 * self.car.mass_kg * (v_old ** 2 - v_new ** 2) return (delta_ke / 3600.0) * self.car.regen_efficiency def battery_drain_rate(self, v: float, ghi: float) -> float: power_out = self.power_drained(v) power_in = self.solar_power(ghi) - return (power_out - power_in) / self.car.battery_capacity + return (power_out - power_in) / self.car.battery_capacity_wh diff --git a/sim/planning/comparison.py b/sim/planning/comparison.py index 4deb95a..d4639ca 100644 --- a/sim/planning/comparison.py +++ b/sim/planning/comparison.py @@ -20,9 +20,9 @@ def build_comparison_row( "min_soc_pct": results.min_soc_pct, "target_soc_pct": race.target_soc * 100.0, "soc_margin_pct": results.final_soc_pct - (race.target_soc * 100.0), - "target_battery_energy_wh": car.battery_capacity * race.target_soc, - "final_battery_energy_wh": car.battery_capacity * results.final_soc_pct / 100.0, - "energy_margin_wh": (results.final_soc_pct / 100.0 - race.target_soc) * car.battery_capacity, + "target_battery_energy_wh": car.battery_capacity_wh * race.target_soc, + "final_battery_energy_wh": car.battery_capacity_wh * results.final_soc_pct / 100.0, + "energy_margin_wh": (results.final_soc_pct / 100.0 - race.target_soc) * car.battery_capacity_wh, "avg_speed_mph": results.avg_speed_mph, "max_speed_mph": results.max_speed_mph, "min_speed_mph": results.min_speed_mph, diff --git a/sim/planning/report.py b/sim/planning/report.py index 966de7d..6579407 100644 --- a/sim/planning/report.py +++ b/sim/planning/report.py @@ -27,8 +27,8 @@ def print_planning_report( print() print(f"Track: {track.name}") print(f"Location: {track.location}") - print(f"Battery capacity: {car_config.battery_capacity} Wh") - print(f"Target final battery energy: {car_config.battery_capacity * race_config.target_soc} Wh") + print(f"Battery capacity: {car_config.battery_capacity_wh} Wh") + print(f"Target final battery energy: {car_config.battery_capacity_wh * race_config.target_soc} Wh") print(f"Target final SoC: {race_config.target_soc * 100:.1f}%") print("Best Fixed Speed:") diff --git a/sim/scenarios.py b/sim/scenarios.py index 2c7d9db..6fdc50d 100644 --- a/sim/scenarios.py +++ b/sim/scenarios.py @@ -7,7 +7,7 @@ from dataclasses import fields from typing import Dict -from sim.config import CarConfig +from sim.config import CarConfig, migrate_legacy_car_params CONSERVATIVE = "conservative" BASE = "base" @@ -18,14 +18,14 @@ # -1: conservative = value - uncertainty (bigger is better for performance) # Optimistic is always the opposite. Parameters not listed (e.g. g) are held fixed. CONSERVATIVE_DIRECTION: Dict[str, int] = { - "mass": +1, # heavier -> more rolling loss, more energy to accelerate - "C_dA": +1, # more drag - "rho": +1, # denser air -> more drag - "battery_capacity": -1, # less usable energy - "solar_panel_area": -1, # less solar power + "mass_kg": +1, # heavier -> more rolling loss, more energy to accelerate + "cda_m2": +1, # more drag + "air_density_kg_per_m3": +1, # denser air -> more drag + "battery_capacity_wh": -1, # less usable energy + "solar_panel_area_m2": -1, # less solar power "solar_panel_efficiency": -1, # less solar power "electrical_efficiency": -1, # more losses (see note below) - "tire_pressure": -1, # lower pressure -> higher Crr + "tire_pressure_bar": -1, # lower pressure -> higher Crr "regen_efficiency": -1, # less energy recovered } # NOTE: physics.py currently multiplies drain by electrical_efficiency, so a lower @@ -51,6 +51,7 @@ def make_scenario(params: Dict[str, dict], scenario: str) -> CarConfig: if scenario not in SCENARIOS: raise ValueError(f"unknown scenario {scenario!r}; expected one of {SCENARIOS}") sign = {CONSERVATIVE: +1, BASE: 0, OPTIMISTIC: -1}[scenario] + params = migrate_legacy_car_params(params) # accept pre-unit-suffix names kwargs = {} for f in fields(CarConfig): diff --git a/sim/simulator.py b/sim/simulator.py index c594564..86e0b61 100644 --- a/sim/simulator.py +++ b/sim/simulator.py @@ -74,7 +74,7 @@ def run(self) -> SimulationResults: prev_soc = soc energy_delta = (power_in - power_out) * step_hours - next_soc = prev_soc + energy_delta / self.car.battery_capacity + next_soc = prev_soc + energy_delta / self.car.battery_capacity_wh step_fraction = 1.0 hit_min_soc = prev_soc > self.race.min_soc and next_soc <= self.race.min_soc @@ -113,7 +113,7 @@ def run(self) -> SimulationResults: regen_wh = self.physics.regen_energy(prev_speed, speed) if regen_wh > 0: - soc += regen_wh / self.car.battery_capacity + soc += regen_wh / self.car.battery_capacity_wh soc = min(soc, 1.0) time_arr.append(time_minutes[i] + step_minutes * step_fraction) diff --git a/tests/planning/test_comparison.py b/tests/planning/test_comparison.py index a5fe73b..285cb4d 100644 --- a/tests/planning/test_comparison.py +++ b/tests/planning/test_comparison.py @@ -7,7 +7,7 @@ class TestComparison(unittest.TestCase): def test_battery_energy_calculations(self): - car = CarConfig(battery_capacity=5000) + car = CarConfig(battery_capacity_wh=5000) race = RaceConfig(target_soc=0.4) results = SimpleNamespace( total_laps=1, @@ -27,7 +27,7 @@ def test_battery_energy_calculations(self): self.assertAlmostEqual(row["energy_margin_wh"], 1000.0) # (0.6-0.4) * 5000 = 1000 def test_positive_margin(self): - car = CarConfig(battery_capacity=5000) + car = CarConfig(battery_capacity_wh=5000) race = RaceConfig(target_soc=0.4) results = SimpleNamespace( total_laps=1, @@ -47,7 +47,7 @@ def test_positive_margin(self): self.assertGreater(row["energy_margin_wh"], 0) def test_negative_margin(self): - car = CarConfig(battery_capacity=5000) + car = CarConfig(battery_capacity_wh=5000) race = RaceConfig(target_soc=0.4) results = SimpleNamespace( total_laps=1, @@ -67,7 +67,7 @@ def test_negative_margin(self): self.assertLess(row["energy_margin_wh"], 0) def test_zero_margin(self): - car = CarConfig(battery_capacity=5000) + car = CarConfig(battery_capacity_wh=5000) race = RaceConfig(target_soc=0.4) results = SimpleNamespace( total_laps=1, diff --git a/tests/scenarios_test.py b/tests/scenarios_test.py index 0eab772..69a9379 100644 --- a/tests/scenarios_test.py +++ b/tests/scenarios_test.py @@ -6,15 +6,15 @@ def _params(): return { - "mass": {"value": 300, "uncertainty": 10}, - "battery_capacity": {"value": 5000, "uncertainty": 200}, - "solar_panel_area": {"value": 4, "uncertainty": 0.2}, + "mass_kg": {"value": 300, "uncertainty": 10}, + "battery_capacity_wh": {"value": 5000, "uncertainty": 200}, + "solar_panel_area_m2": {"value": 4, "uncertainty": 0.2}, "solar_panel_efficiency": {"value": 0.23, "uncertainty": 0.01}, "electrical_efficiency": {"value": 0.99, "uncertainty": 0.02}, - "C_dA": {"value": 0.7, "uncertainty": 0.05}, - "tire_pressure": {"value": 5, "uncertainty": 0.5}, + "cda_m2": {"value": 0.7, "uncertainty": 0.05}, + "tire_pressure_bar": {"value": 5, "uncertainty": 0.5}, "regen_efficiency": {"value": 0.5, "uncertainty": 0.1}, - "rho": {"value": 1.2, "uncertainty": 0.05}, + "air_density_kg_per_m3": {"value": 1.2, "uncertainty": 0.05}, "g": {"value": 9.80665, "uncertainty": None}, } @@ -29,18 +29,18 @@ def test_returns_three_car_configs(self): def test_base_uses_nominal_values(self): base = make_scenario(_params(), "base") - self.assertEqual(base.mass, 300) - self.assertEqual(base.battery_capacity, 5000) + self.assertEqual(base.mass_kg, 300) + self.assertEqual(base.battery_capacity_wh, 5000) def test_mass_direction(self): s = make_scenarios(_params()) - self.assertEqual(s["conservative"].mass, 310) - self.assertEqual(s["optimistic"].mass, 290) + self.assertEqual(s["conservative"].mass_kg, 310) + self.assertEqual(s["optimistic"].mass_kg, 290) def test_battery_capacity_direction(self): s = make_scenarios(_params()) - self.assertEqual(s["conservative"].battery_capacity, 4800) - self.assertEqual(s["optimistic"].battery_capacity, 5200) + self.assertEqual(s["conservative"].battery_capacity_wh, 4800) + self.assertEqual(s["optimistic"].battery_capacity_wh, 5200) def test_every_directional_param_ordered(self): s = make_scenarios(_params()) @@ -61,18 +61,18 @@ def test_g_is_not_shifted(self): def test_null_uncertainty_treated_as_zero(self): p = _params() - p["mass"]["uncertainty"] = None + p["mass_kg"]["uncertainty"] = None s = make_scenarios(p) - self.assertEqual(s["conservative"].mass, 300) - self.assertEqual(s["optimistic"].mass, 300) + self.assertEqual(s["conservative"].mass_kg, 300) + self.assertEqual(s["optimistic"].mass_kg, 300) def test_missing_or_null_value_uses_default(self): p = _params() - del p["mass"] - p["rho"]["value"] = None + del p["mass_kg"] + p["air_density_kg_per_m3"]["value"] = None cfg = make_scenario(p, "conservative") - self.assertEqual(cfg.mass, CarConfig().mass) - self.assertEqual(cfg.rho, CarConfig().rho) + self.assertEqual(cfg.mass_kg, CarConfig().mass_kg) + self.assertEqual(cfg.air_density_kg_per_m3, CarConfig().air_density_kg_per_m3) def test_fractions_clamped(self): p = _params() diff --git a/tests/test_config.py b/tests/test_config.py index 25f3af9..8563a30 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -5,13 +5,13 @@ def test_invalid_car_config(): with pytest.raises(ValueError, match="Battery capacity must be greater than 0."): - CarConfig(battery_capacity=0, mass=1000, solar_panel_area=1, solar_panel_efficiency=0.2, electrical_efficiency=0.9, regen_efficiency=0.8) + CarConfig(battery_capacity_wh=0, mass_kg=1000, solar_panel_area_m2=1, solar_panel_efficiency=0.2, electrical_efficiency=0.9, regen_efficiency=0.8) with pytest.raises(ValueError, match="Mass must be greater than 0."): - CarConfig(battery_capacity=50, mass=0, solar_panel_area=1, solar_panel_efficiency=0.2, electrical_efficiency=0.9, regen_efficiency=0.8) + CarConfig(battery_capacity_wh=50, mass_kg=0, solar_panel_area_m2=1, solar_panel_efficiency=0.2, electrical_efficiency=0.9, regen_efficiency=0.8) with pytest.raises(ValueError, match="Tire pressure must be greater than 0."): - CarConfig(tire_pressure=0) + CarConfig(tire_pressure_bar=0) def test_invalid_race_config(): with pytest.raises(ValueError, match="Start SOC must be between 0 and 1."): @@ -35,3 +35,18 @@ def test_invalid_track_config(): lap_distance_m=0, timezone="UTC", ) + + +def test_from_json_accepts_legacy_names(tmp_path): + import json + legacy = {"mass": 300, "battery_capacity": 4000, "solar_panel_area": 3, + "C_dA": 0.5, "rho": 1.1, "tire_pressure": 4} + path = tmp_path / "legacy.json" + path.write_text(json.dumps(legacy)) + car = CarConfig.from_json(str(path)) + assert car.mass_kg == 300 + assert car.battery_capacity_wh == 4000 + assert car.solar_panel_area_m2 == 3 + assert car.cda_m2 == 0.5 + assert car.air_density_kg_per_m3 == 1.1 + assert car.tire_pressure_bar == 4 diff --git a/tests/test_physics.py b/tests/test_physics.py index a3e476e..d4f6b0d 100644 --- a/tests/test_physics.py +++ b/tests/test_physics.py @@ -8,10 +8,10 @@ def make_physics(): car = CarConfig( - mass=337, - C_dA=0.73809, - tire_pressure=5, - rho=1.192, + mass_kg=337, + cda_m2=0.73809, + tire_pressure_bar=5, + air_density_kg_per_m3=1.192, g=9.80665, electrical_efficiency=0.99, ) @@ -73,14 +73,14 @@ def test_solar_power_scales_with_ghi(): def test_solar_power_scales_with_panel_area(): car = CarConfig( - solar_panel_area=4, + solar_panel_area_m2=4, solar_panel_efficiency=0.25, ) physics = PhysicsEngine(car) power_at_4 = physics.solar_power(1000) - car.solar_panel_area = 8 + car.solar_panel_area_m2 = 8 power_at_8 = physics.solar_power(1000) assert math.isclose(power_at_8, 2 * power_at_4) @@ -88,7 +88,7 @@ def test_solar_power_scales_with_panel_area(): def test_solar_power_scales_with_panel_efficiency(): car = CarConfig( - solar_panel_area=4, + solar_panel_area_m2=4, solar_panel_efficiency=0.20, ) physics = PhysicsEngine(car) @@ -103,7 +103,7 @@ def test_solar_power_scales_with_panel_efficiency(): def test_solar_power_exact_value(): # 1000 W/m^2 * 4 m^2 * 25% efficiency = 1000 W car = CarConfig( - solar_panel_area=4, + solar_panel_area_m2=4, solar_panel_efficiency=0.25, ) physics = PhysicsEngine(car) diff --git a/tests/test_simulator.py b/tests/test_simulator.py index c8ef314..fff53c0 100644 --- a/tests/test_simulator.py +++ b/tests/test_simulator.py @@ -46,7 +46,7 @@ def make_simulator(lap_distance_m=1000.0, time_step_minutes=1.0, simulator = LapsRaceSimulator.__new__(LapsRaceSimulator) simulator.track = SimpleNamespace(lap_distance_m=lap_distance_m) - simulator.car = SimpleNamespace(battery_capacity=1_000_000.0) + simulator.car = SimpleNamespace(battery_capacity_wh=1_000_000.0) simulator.race = SimpleNamespace( time_step_minutes=time_step_minutes, start_soc=1.0, diff --git a/tests/test_time_step.py b/tests/test_time_step.py index 407c93f..fa7d059 100644 --- a/tests/test_time_step.py +++ b/tests/test_time_step.py @@ -37,7 +37,7 @@ def run_sim(track, step, strategy="fixed", car=None, seed=1, **race_kwargs): # A battery big enough that the car never hits min SoC, so the whole window runs. -BIG_BATTERY = CarConfig(battery_capacity=1_000_000) +BIG_BATTERY = CarConfig(battery_capacity_wh=1_000_000) class TestResample: @@ -384,11 +384,11 @@ def test_fixed_speed_distance_is_speed_times_window(self, track, step): @pytest.mark.parametrize("step", ALL_STEPS) def test_energy_used_is_independent_of_step(self, track, step): # No solar and constant speed: SoC drop is exactly power * duration / capacity. - car = CarConfig(battery_capacity=50_000, solar_panel_area=0) + car = CarConfig(battery_capacity_wh=50_000, solar_panel_area_m2=0) r = run_sim(track, step, car=car, fixed_speed_mps=15.0) from sim.physics import PhysicsEngine power_w = PhysicsEngine(car).power_drained(15.0) - expected_soc = 1.0 - power_w * (WINDOW_MINUTES / 60.0) / car.battery_capacity + expected_soc = 1.0 - power_w * (WINDOW_MINUTES / 60.0) / car.battery_capacity_wh assert r.completed_full_window assert r.final_soc == pytest.approx(expected_soc, abs=1e-9) From c2e6da14bc8953cabd777cf710b99a00702d8607 Mon Sep 17 00:00:00 2001 From: Srikar-Kolli Date: Sun, 15 Nov 2026 14:43:49 -0500 Subject: [PATCH 2/2] Update parameters_needed.md to unit-suffixed names (#54) Co-Authored-By: Claude Sonnet 5.5 --- docs/parameters_needed.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/parameters_needed.md b/docs/parameters_needed.md index 95cc578..89f98e8 100644 --- a/docs/parameters_needed.md +++ b/docs/parameters_needed.md @@ -9,15 +9,15 @@ How to update: edit table cell, change `source` from `unknown` when confirmed. | Param | Value now | Unit | Owner | Source | Effect | |---|---|---|---|---|---| -| `mass` | 337 | kg | Mechanical | unknown — confirm weigh-in | Heavier = more rolling + regen energy | -| `battery_capacity` | 5000 | Wh | Electrical | unknown — confirm pack spec | Bigger = more laps, slower SoC drop | -| `solar_panel_area` | 4 | m² | Electrical | unknown — confirm array size | Bigger = more solar power | +| `mass_kg` | 337 | kg | Mechanical | unknown — confirm weigh-in | Heavier = more rolling + regen energy | +| `battery_capacity_wh` | 5000 | Wh | Electrical | unknown — confirm pack spec | Bigger = more laps, slower SoC drop | +| `solar_panel_area_m2` | 4 | m² | Electrical | unknown — confirm array size | Bigger = more solar power | | `solar_panel_efficiency` | 0.23 | constant (NA)| Electrical | unknown — confirm datasheet | Higher = more solar power | | `electrical_efficiency` | 0.99 | constant (NA) | Electrical | unknown — confirm MPPT/motor | BUG? (`physics.py:21`) multiplies, should divide | -| `C_dA` | 0.73809 | m² | Mechanical | unknown — confirm aero test/CFD | Higher = more drag, slower optimal speed | -| `tire_pressure` | 5 | bar | Mechanical | unknown — confirm actual pressure | Higher = less rolling resistance in current formula | +| `cda_m2` | 0.73809 | m² | Mechanical | unknown — confirm aero test/CFD | Higher = more drag, slower optimal speed | +| `tire_pressure_bar` | 5 | bar | Mechanical | unknown — confirm actual pressure | Higher = less rolling resistance in current formula | | `regen_efficiency` | 0.5 | fraction | Electrical | unknown — confirm | Higher = more energy back on slowdown | -| `rho` | 1.192 | kg/m³ | Software | estimate — std air | Higher = more drag | +| `air_density_kg_per_m3` | 1.192 | kg/m³ | Software | estimate — std air | Higher = more drag | | `g` | 9.80665 | m/s² | Software | measured — constant | Fixed, do not change | ## Ignored keys in `car_params.json` (not read by `CarConfig.from_json`)