Skip to content

Commit

Permalink
Merge branch 'main' into fix/speed-trace-from-slts-in-ssts
Browse files Browse the repository at this point in the history
some tests are failing related to changes that got merged
  • Loading branch information
calbaker committed Sep 12, 2024
2 parents 48cd117 + 14700b4 commit 6a4cf54
Show file tree
Hide file tree
Showing 75 changed files with 11,018 additions and 49,187 deletions.
14 changes: 7 additions & 7 deletions applications/calibration/zanzeff_set_speed_train_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ def update_params(
x
)

# set `drag_area_empty_square_meters` to be same as `drag_area_loaded_square_meters`
# set `cd_area_empty_square_meters` to be same as `cd_area_loaded_square_meters`
alt.set_param_from_path(
rail_vehicle_map[train_type],
"drag_area_empty_square_meters",
rail_vehicle_map[train_type].drag_area_loaded_square_meters
"cd_area_empty_square_meters",
rail_vehicle_map[train_type].cd_area_loaded_square_meters
)

train_sim = tsb.make_set_speed_train_sim(
Expand Down Expand Up @@ -280,10 +280,10 @@ def get_mod_err(


params_and_bounds = (
("drag_area_loaded_square_meters", (1, 8)),
# `drag_area_empty_square_meters` gets manually set equal to
# `drag_area_loaded_square_meters`
# ("drag_area_empty_square_meters", (1, 8)),
("cd_area_loaded_square_meters", (1, 8)),
# `cd_area_empty_square_meters` gets manually set equal to
# `cd_area_loaded_square_meters`
# ("cd_area_empty_square_meters", (1, 8)),
# ("davis_b_seconds_per_meter", (0, 0.1)),
("rolling_ratio", (0.0003, 0.003)),
("bearing_res_per_axle_newtons", (40, 320)),
Expand Down
4 changes: 2 additions & 2 deletions applications/calibration/zanzeff_set_speed_train_cal_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
)

# %%
# ("drag_area_loaded_square_meters", (1, 6)),
# ("drag_area_empty_square_meters", (1, 8)),
# ("cd_area_loaded_square_meters", (1, 6)),
# ("cd_area_empty_square_meters", (1, 8)),
# # ("davis_b_seconds_per_meter", (0, 0.1)),
# ("rolling_ratio", (0.0003, 0.003)),
# ("bearing_res_per_axle_newtons", (40, 320)),
Expand Down
2 changes: 1 addition & 1 deletion python/altrios/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def history_path_list(self, element_as_list:bool=False) -> List[str]:
"""
Returns a list of relative paths to all history variables (all variables
that contain history as a subpath).
See example usage in `altrios/demos/demo_variable_paths.py`.
See example usage in `altrios/demo_data/demo_variable_paths.py`.
# Arguments
- `element_as_list`: if True, each element is itself a list of the path elements
Expand Down
25 changes: 11 additions & 14 deletions python/altrios/altrios_pyo3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ class TrainState:
dt_seconds: float
length_meters: float
mass_static_kilograms: float
mass_adj_kilograms: float
mass_rot_kilograms: float
mass_freight_kilograms: float
weight_static_newtons: float
res_rolling_newtons: float
Expand Down Expand Up @@ -761,7 +761,7 @@ class CatPowerLimit(SerdeAPI):
class TrainParams(SerdeAPI):
length: float
speed_max: float
mass_total: float
mass_static: float
mass_per_brake: float
axle_count: int
train_type: TrainType
Expand Down Expand Up @@ -893,7 +893,7 @@ class TrainSimBuilder(SerdeAPI):
) -> None: ...

def make_set_speed_train_sim(
rail_vehicle: RailVehicle,
rail_vehicles: List[RailVehicle],
network: List[Link],
link_path: List[LinkIdx],
speed_trace: SpeedTrace,
Expand All @@ -903,7 +903,7 @@ class TrainSimBuilder(SerdeAPI):

def make_speed_limit_train_sim(
self,
rail_vehicle: RailVehicle,
rail_vehicles: List[RailVehicle],
location_map: Dict[str, List[Location]],
save_interval: Optional[int],
simulation_days: Optional[int],
Expand All @@ -914,13 +914,12 @@ class TrainSimBuilder(SerdeAPI):

@dataclass
class TrainConfig(SerdeAPI):
rail_vehicle_type: str
cars_empty: int
cars_loaded: int
train_type: str
n_cars_by_type: Dict[str, int]
rail_vehicle_type: Optional[str]
train_type: Optional[TrainType]
train_length_meters: Optional[float]
train_mass_kilograms: Optional[float]
drag_coeff_vec: Optional[List[float]]
cd_area_vec: Optional[List[float]]
@classmethod
def default(cls) -> Self: ...

Expand All @@ -933,10 +932,10 @@ class RailVehicle(SerdeAPI):
braking_ratio_loaded: float
car_type: str
davis_b_seconds_per_meter: float
drag_area_empty_square_meters: float
drag_area_loaded_square_meters: float
cd_area_empty_square_meters: float
cd_area_loaded_square_meters: float
length_meters: float
mass_extra_per_axle_kilograms: float
mass_rot_per_axle_kilograms: float
mass_static_empty_kilograms: float
mass_static_loaded_kilograms: float
rolling_ratio: float
Expand Down Expand Up @@ -989,8 +988,6 @@ class SpeedSet(SerdeAPI): ...
# TODO: finish fleshing this out

def import_locations(filename: str) -> Dict[str, List[Location]]: ...
def import_rail_vehicles(filename: str) -> Dict[str, RailVehicle]: ...


def build_speed_limit_train_sims(
train_sim_builders: List[TrainSimBuilder],
Expand Down
39 changes: 21 additions & 18 deletions python/altrios/demos/demo_variable_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@
import time

import altrios as alt

SAVE_INTERVAL = 100

# Build the train config
rail_vehicle_loaded = alt.RailVehicle.from_file(
alt.resources_root() / "rolling_stock/Manifest_Loaded.yaml")
rail_vehicle_empty = alt.RailVehicle.from_file(
alt.resources_root() / "rolling_stock/Manifest_Empty.yaml")

# https://docs.rs/altrios-core/latest/altrios_core/train/struct.TrainConfig.html
train_config = alt.TrainConfig(
cars_empty=50,
cars_loaded=50,
rail_vehicle_type="Manifest",
train_type=alt.TrainType.Freight,
rail_vehicles=[rail_vehicle_loaded, rail_vehicle_empty],
n_cars_by_type={
"Manifest_Loaded": 50,
"Manifest_Empty": 50,
},
train_length_meters=None,
train_mass_kilograms=None,
)

# Build the locomotive consist model
# instantiate battery model
# https://docs.rs/altrios-core/latest/altrios_core/consist/locomotive/powertrain/reversible_energy_storage/struct.ReversibleEnergyStorage.html#
res = alt.ReversibleEnergyStorage.from_file(
Expand Down Expand Up @@ -50,6 +59,7 @@
SAVE_INTERVAL,
)

# Instantiate the intermediate `TrainSimBuilder`
tsb = alt.TrainSimBuilder(
train_id="0",
origin_id="Minneapolis",
Expand All @@ -58,20 +68,16 @@
loco_con=loco_con,
)

rail_vehicle_file = "rolling_stock/" + train_config.rail_vehicle_type + ".yaml"
rail_vehicle = alt.RailVehicle.from_file(
alt.resources_root() / rail_vehicle_file)

# Load the network and construct the timed link path through the network.
network = alt.Network.from_file(
alt.resources_root() / "networks/Taconite-NoBalloon.yaml")

location_map = alt.import_locations(
alt.resources_root() / "networks/default_locations.csv")

train_sim: alt.SpeedLimitTrainSim = tsb.make_speed_limit_train_sim(
rail_vehicle=rail_vehicle,
location_map=location_map,
save_interval=1,
save_interval=SAVE_INTERVAL,
)
train_sim.set_save_interval(SAVE_INTERVAL)

Expand All @@ -85,9 +91,6 @@
False,
)[0]

# uncomment this line to see example of logging functionality
# alt.utils.set_log_level("DEBUG")

t0 = time.perf_counter()
train_sim.walk_timed_path(
network=network,
Expand All @@ -102,10 +105,10 @@
# whether to override reference files used in assertions, disabled by default
ENABLE_REF_OVERRIDE = os.environ.get("ENABLE_REF_OVERRIDE", "false").lower() == "true"
# directory for reference files for checking sim results against expected results
ref_dir = alt.resources_root() / "demos/demo_variable_paths/"
ref_dir = alt.resources_root() / "demo_data/demo_variable_paths/"

# print out all subpaths for variables in SimDrive
print("List of variable paths for SimDrive:" + "\n".join(train_sim.variable_path_list()))
# print out all subpaths for variables in train_sim
print("List of variable paths for train_sim:" + "\n".join(train_sim.variable_path_list()))
if ENABLE_REF_OVERRIDE:
ref_dir.mkdir(exist_ok=True, parents=True)
with open(ref_dir / "variable_path_list_expected.txt", 'w') as f:
Expand All @@ -118,8 +121,8 @@
assert variable_path_list_expected == train_sim.variable_path_list()
print("\n")

# print out all subpaths for history variables in SimDrive
print("List of history variable paths for SimDrive:" + "\n".join(train_sim.history_path_list()))
# print out all subpaths for history variables in train_sim
print("List of history variable paths for train_sim:" + "\n".join(train_sim.history_path_list()))
print("\n")

# print results as dataframe
Expand Down
47 changes: 32 additions & 15 deletions python/altrios/demos/set_speed_simple_corr_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@

SAVE_INTERVAL = 1

# Uncomment and run `maturin develop --release --features logging` to enable logging,
# which is needed because logging bogs the CPU and is off by default.
# alt.utils.set_log_level("DEBUG")

# Build the train config
rail_vehicle_loaded = alt.RailVehicle.from_file(
alt.resources_root() / "rolling_stock/Manifest_Loaded.yaml")
rail_vehicle_empty = alt.RailVehicle.from_file(
alt.resources_root() / "rolling_stock/Manifest_Empty.yaml")

# https://docs.rs/altrios-core/latest/altrios_core/train/struct.TrainConfig.html
train_config = alt.TrainConfig(
cars_empty=50,
cars_loaded=50,
rail_vehicle_type="Manifest",
train_type=None,
rail_vehicles=[rail_vehicle_loaded, rail_vehicle_empty],
n_cars_by_type={
"Manifest_Loaded": 50,
"Manifest_Empty": 50,
},
train_length_meters=None,
train_mass_kilograms=None,
)

# Build the locomotive consist model
bel: alt.Locomotive = alt.Locomotive.default_battery_electric_loco()

# construct a vector of one BEL and several conventional locomotives
Expand All @@ -36,16 +48,14 @@
SAVE_INTERVAL,
)

# Instantiate the intermediate `TrainSimBuilder`
tsb = alt.TrainSimBuilder(
train_id="0",
train_config=train_config,
loco_con=loco_con,
)

rail_vehicle_file = "rolling_stock/rail_vehicles.csv"
rail_vehicle_map = alt.import_rail_vehicles(alt.resources_root() / rail_vehicle_file)
rail_vehicle = rail_vehicle_map[train_config.rail_vehicle_type]

# Load the network and link path through the network.
network = alt.Network.from_file(
alt.resources_root() / 'networks/simple_corridor_network.yaml')
# This data in this file were generated by running
Expand All @@ -57,12 +67,12 @@
alt.resources_root() / "demo_data/link_points_idx_simple_corridor.csv")


# load the prescribed speed trace that the train will follow
speed_trace = alt.SpeedTrace.from_csv_file(
alt.resources_root() / "demo_data/speed_trace_simple_corridor.csv"
)

train_sim: alt.SetSpeedTrainSim = tsb.make_set_speed_train_sim(
rail_vehicle=rail_vehicle,
network=network,
link_path=link_path,
speed_trace=speed_trace,
Expand All @@ -78,13 +88,20 @@
# pull out solved locomotive for plotting convenience
loco0:alt.Locomotive = train_sim.loco_con.loco_vec.tolist()[0]

fig, ax = plt.subplots(4, 1, sharex=True)
fig, ax = plt.subplots(4, 1, sharex=True, figsize=((8, 6)))
ax[0].plot(
np.array(train_sim.history.time_seconds) / 3_600,
train_sim.history.pwr_whl_out_watts,
np.array(train_sim.history.pwr_whl_out_watts) / 1e6,
label="tract pwr",
)
ax[0].set_ylabel('Power')
ax[0].set_ylabel('Power [MW]')
# to accommodate the legend
ax[0].set_xlim(
[
ax[0].get_xlim()[0],
ax[0].get_xlim()[0] + (ax[1].get_xlim()[1] - ax[1].get_xlim()[0]) * 1.2
]
)
ax[0].legend()

ax[1].plot(
Expand Down Expand Up @@ -113,7 +130,7 @@
label='grade',
)
ax[1].set_ylabel('Force [N]')
ax[1].legend()
ax[1].legend(loc='right')

ax[-1].plot(
np.array(train_sim.history.time_seconds) / 3_600,
Expand All @@ -128,12 +145,12 @@
)

ax[2].set_ylabel('SOC')
ax[2].legend()

plt.suptitle("Set Speed Train Sim Demo")
plt.tight_layout()

print("SHOW_PLOTS: ", SHOW_PLOTS)
if SHOW_PLOTS:
fig.show()
plt.show()

# %%
Loading

0 comments on commit 6a4cf54

Please sign in to comment.