simulation/¶
API reference for the simulation package.
simulation.simulation¶
Core physics model and energy simulation.
simulation.simulation
¶
Simulation for MSXVI.
Date: 2025-11-07 Author: Midnight Sun Team #24 - MSXVI Group: Strategy_XVI
VehicleParams
dataclass
¶
A data class to store parameters for the model.
Source code in simulation/simulation.py
SimResult
¶
rolling_power(v, M, g, C_RR)
¶
drag_power(v, rho, Cd, A)
¶
grade_power(v, theta_rad, M, g)
¶
Calculates gravitational power (positive uphill, negative downhill).
solar_power(G_wm2, A_solar, panel_eff)
¶
simulate(v, dt, d0, theta_fn, ghi_fn, params=VehicleParams())
¶
Vectorized simulation over a fixed horizon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
ndarray
|
Speed profile (m/s). |
required |
dt
|
float
|
Timestep (s). |
required |
d0
|
float
|
Starting distance (m). |
required |
theta_fn
|
Callable[[ndarray], ndarray]
|
Callable that returns road grade (deg) at given distance(s). |
required |
ghi_fn
|
Callable[[ndarray], ndarray]
|
Callable that returns GHI (W/m^2) at given distance(s). |
required |
params
|
VehicleParams
|
Vehicle parameters (see dataclass). |
VehicleParams()
|
Returns:
| Type | Description |
|---|---|
SimResult
|
SimResult(final_distance_m, final_soc_J, traces). |
Source code in simulation/simulation.py
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 145 146 147 148 149 150 151 152 153 154 155 | |
wh_from_joules(E_J)
¶
Convert energy from Joules to Watt-hours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E_J
|
ndarray | float
|
Energy in Joules. |
required |
Returns:
| Type | Description |
|---|---|
ndarray | float
|
Energy in Watt-hours. |
simulation.optimizer¶
Velocity profile optimization.
simulation.optimizer
¶
Optimizer
Date: 2025-11-09 Author: Midnight Sun Team #24 - MSXVI Group: Strategy_XVI
OptimizeConfig
dataclass
¶
A configuration dataclass for the optimizer.
Source code in simulation/optimizer.py
objective(vs, dt, d0, theta_fn, ghi_fn, params, cfg)
¶
Objective for SciPy minimize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vs
|
ndarray
|
The speed profile (m/s). |
required |
theta_fn
|
Callable
|
Callable returning road grade (deg) at given distance(s). |
required |
ghi_fn
|
Callable
|
Callable returning GHI (W/m^2) at given distance(s). |
required |
params
|
VehicleParams
|
For simulation. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Negative distance (so minimizing -> maximize distance). |
Source code in simulation/optimizer.py
SLSQP_velocity(cfg, theta_fn, ghi_fn, params)
¶
Runs an SLSQP optimization on given slope and irradiance callables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
OptimizeConfig
|
Optimizer configuration data (see OptimizeConfig dataclass). |
required |
theta_fn
|
Callable
|
Callable returning road grade (deg) at given distance(s). |
required |
ghi_fn
|
Callable
|
Callable returning GHI (W/m^2) at given distance(s). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, float]
|
Best velocity array and objective value. |
Source code in simulation/optimizer.py
exhaustive_search_velocity(cfg, theta_fn, ghi_fn, params)
¶
Perform an exhaustive search (try every single velocity vector) to find optimized velocity
Mainly used for benchmarking, hence small N
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
OptimizeConfig
|
Optimizer configuration data (see OptimizeConfig dataclass). |
required |
theta_fn
|
Callable
|
Callable that takes an array of indices or distances and returns road grade (deg) array. |
required |
ghi_fn
|
Callable
|
Callable that takes an array of indices or distances and returns GHI (W/m^2) array. |
required |
params
|
VehicleParams
|
Vehicle parameters. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, float]
|
Best velocity array and objective value. |
Source code in simulation/optimizer.py
simulation.config¶
Runtime configuration management.
simulation.config
¶
Configuration management for strategy simulations.
Date: 2025-11-25 Author: Midnight Sun Team #24 - MSXVI Group: Strategy_XVI
SimConfig
dataclass
¶
Configuration parameters for simulation and optimization.
Source code in simulation/config.py
display()
¶
Display all configuration parameters with indices.
Source code in simulation/config.py
update_param(param_name, value)
¶
Update a configuration parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_name
|
str
|
Name of the parameter to update. |
required |
value
|
any
|
New value for the parameter. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False otherwise. |
Source code in simulation/config.py
simulation.scenarios¶
Test and race day scenario runners.
simulation.scenarios
¶
Scenario runners for test and race day simulations.
Date: 2025-11-25 Author: Midnight Sun Team #24 - MSXVI Group: Strategy_XVI
run_test_scenario(test_path, config)
¶
Run a test scenario using mock YAML or CSV data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_path
|
str
|
Path to test file (YAML or CSV). |
required |
config
|
SimConfig
|
Simulation configuration. |
required |
Returns:
| Type | Description |
|---|---|
SimResult
|
Simulation result. |
Source code in simulation/scenarios.py
run_raceday_scenario(config)
¶
Run a race day scenario using GPX data and optionally Solcast.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SimConfig
|
Simulation configuration. |
required |
Returns:
| Type | Description |
|---|---|
SimResult
|
Simulation result. |
Source code in simulation/scenarios.py
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
simulation.plots¶
Result visualization: speed and battery plots.
simulation.plots
¶
Generate plots for results.
Date: 2025-11-25 Author: Midnight Sun Team #24 - MSXVI Group: Strategy_XVI
generate_plots(dist_km, res, soc_wh, min_soc, bat_max_wh=None)
¶
Generates speed and battery plots from simulation results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dist_km
|
ndarray
|
Distance array in kilometers. |
required |
res
|
SimResult
|
SimResult object containing simulation traces. |
required |
soc_wh
|
ndarray
|
Battery state of charge array in Watt-hours. |
required |
min_soc
|
float
|
Minimum SOC Line |
required |
bat_max_wh
|
float
|
Maximum battery capacity in Wh |
None
|
Source code in simulation/plots.py
simulation.mock_data¶
YAML-based test data loader for mock scenarios.
simulation.mock_data
¶
Mock data loader for YAML-based test scenarios.
Date: 2025-11-23 Author: Midnight Sun Team #24 - MSXVI Group: Strategy_XVI
load_mock_csv(path)
¶
Load a CSV mock profile for testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to CSV file containing mock scenario data. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray, Dict[str, Any]]
|
Tuple of (theta_deg, ghi, meta) where meta contains dt and d0. |
Source code in simulation/mock_data.py
load_mock_yaml(path)
¶
Load a YAML mock profile for testing (legacy).
Source code in simulation/mock_data.py
simulation.map_visualization¶
GPX route parsing, segment computation, and Grafana map export.
simulation.map_visualization
¶
GPX utilities + mapping for strategy XVI.
Date: 2025-11-10 Author: Midnight Sun Team #24 - MSXVI Group: Strategy_XVI
Note
This is mainly for ASC. For FSGP elevation data isn't AS important since it's a grand prix. However, would be cool to have lap by lap coloured map gradients for best speeds for FSGP.
AscTrack
¶
Bases: IntEnum
To store track indices.
Note
This is not fully listed out since this repo still uses asc2024 track data. Will be updated when we get asc2026 track data.
Source code in simulation/map_visualization.py
haversine(lat1, lon1, lat2, lon2)
¶
Distance in meters between two points using GPS data.
Source code in simulation/map_visualization.py
load_gpx_points(path, track)
¶
Loads GPX points from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to GPX file. |
required |
track
|
IntEnum
|
Specific track along the route (e.g., NashvilleToPaducah). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray of shape [N, 3] as (lat, lon, elevation). |
Source code in simulation/map_visualization.py
compute_segments(pts)
¶
Computes cumulative distances and grade angles between GPS points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pts
|
ndarray
|
[N,3] array of (lat, lon, elevation). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
(distance, grade_deg) - cumulative distance and grade in degrees. |
Source code in simulation/map_visualization.py
interpolate_to_time_grid(theta_deg, ghi, dist_m, dt, avg_speed=15.0)
¶
Interpolates GPS-based data to match simulation timesteps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta_deg
|
ndarray
|
Grade angles at GPS points. |
required |
ghi
|
ndarray
|
Global horizontal irradiance at GPS points. |
required |
dist_m
|
ndarray
|
Cumulative distance at GPS points. |
required |
dt
|
float
|
Time step for simulation (seconds). |
required |
avg_speed
|
float
|
Estimated average speed for calculating time grid (m/s). |
15.0
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
(theta_interp, ghi_interp) - interpolated arrays matching simulation steps. |
Source code in simulation/map_visualization.py
color_for_speed(v, vmin=10.0, vmax=20.0)
¶
Returns color gradient hex code based on speed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
float
|
Current speed (m/s). |
required |
vmin
|
float
|
Minimum speed in range. |
10.0
|
vmax
|
float
|
Maximum speed in range. |
20.0
|
Returns:
| Type | Description |
|---|---|
str
|
Hex color code (e.g., '#ff0000'). |
Source code in simulation/map_visualization.py
export_grafana_json(gpx_path, track, speed, theta_deg=None, ghi=None, vmin=10.0, vmax=20.0, outfile=None)
¶
Builds a JSON object suitable for Grafana map visualization.
Each entry includes lat/lon, segment info, and color-coded speed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gpx_path
|
str
|
Path to GPX file. |
required |
track
|
AscTrack
|
Track index to load. |
required |
speed
|
ndarray
|
Numpy array of target speeds (m/s). |
required |
theta_deg
|
Optional[ndarray]
|
Grade angles. |
None
|
ghi
|
Optional[ndarray]
|
Global horizontal irradiance. |
None
|
vmin
|
float
|
Minimum speed for color scaling. |
10.0
|
vmax
|
float
|
Maximum speed for color scaling. |
20.0
|
outfile
|
Optional[str]
|
Optional output file path for JSON. |
None
|
Returns:
| Type | Description |
|---|---|
Dict
|
Dictionary containing segment data for Grafana. |