Ambient Temperature Example#
In this example, we'll demonstrate how to use RouteE Compass to plan routes that incorporate ambient temperature data for electric vehicles.
This builds off the Open Street Maps Example and assumes that we've already downloaded a road network, so be sure to check that one out first.
import matplotlib.pyplot as plt
import pandas as pd
from nrel.routee.compass import CompassApp
First, we'll load the application from the pre-built configuration file that includes a traversal model that injects ambient temperature.
app = CompassApp.from_config_file("./denver_co/osm_default_temperature.toml")
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
Cell In[2], line 1
----> 1 app = CompassApp.from_config_file("./denver_co/osm_default_temperature.toml")
2
File ~/miniconda3/envs/test/lib/python3.11/site-packages/nrel/routee/compass/compass_app.py:84, in CompassApp.from_config_file(cls, config_file, parallelism)
81 with open(config_path) as f:
82 toml_config = tomlkit.load(f)
---> 84 return cls.from_dict(toml_config, config_path, parallelism=parallelism)
File ~/miniconda3/envs/test/lib/python3.11/site-packages/nrel/routee/compass/compass_app.py:124, in CompassApp.from_dict(cls, config, working_dir, parallelism)
122 path_str = str(working_dir.absolute()) if working_dir is not None else ""
123 toml_string = tomlkit.dumps(config)
--> 124 app = cls.get_constructor()._from_config_toml_string(toml_string, path_str)
125 return cls(app, config)
Exception: Error while creating CompassApp from config toml string: failure building traversal model: vehicle model missing 'model_key' field in '/home/runner/work/routee-compass/routee-compass/docs/examples/denver_co/vehicles/2016_Nissan_Leaf_30_kWh_Steady_Thermal.json' (vehicle 'name' was replaced by 'model_key'; RouteE Powertrain V2 metadata.json files provide it)
Basic Route With Mild Ambient Temperature#
Let's start with a basic route query for a 2016 Nissan Leaf 30 kWh with mild ambient temperature considerations and search for the shortest time route.
query = {
"origin_x": -104.969307,
"origin_y": 39.779021,
"destination_x": -104.975360,
"destination_y": 39.693005,
"model_name": "2016_Nissan_Leaf_30_kWh_Steady_Thermal",
"weights": {"trip_distance": 0, "trip_time": 1, "trip_energy_electric": 0},
"ambient_temperature": {"value": 72, "unit": "fahrenheit"},
}
result = app.run(query)
if "error" in result:
raise ValueError(f"Error: {result['error']}")
elif "route" not in result or result["route"] is None:
raise ValueError("No route found.")
else:
energy = result["route"]["traversal_summary"]["trip_energy_electric"]["value"]
print(
f"Ambient Temperature: {query['ambient_temperature']['value']} F, Trip Energy: {round(energy, 3)} kWh"
)
Next, let's look at how the ambient temperature affects the energy consumption by running the same route query with different temperature settings.
temp_results = []
for temp in [0, 15, 32, 50, 72, 90, 110]:
query["ambient_temperature"] = {"value": temp, "unit": "fahrenheit"}
result = app.run(query)
if "error" in result:
raise ValueError(f"Error for temperature {temp} F: {result['error']}")
elif "route" not in result or result["route"] is None:
raise ValueError(f"No route found for temperature {temp} F.")
else:
energy = result["route"]["traversal_summary"]["trip_energy_electric"][
"value"
]
temp_results.append(
{
"ambient_temperature_f": temp,
"trip_energy_electric": energy,
"vehicle_model": query["model_name"],
}
)
print(f"Ambient Temperature: {temp} F, Trip Energy: {round(energy, 3)} kWh")
plot_df = pd.DataFrame(temp_results)
plt.figure(figsize=(10, 6))
plt.plot(
plot_df["ambient_temperature_f"], plot_df["trip_energy_electric"], marker="o"
)
plt.title("Effect of Ambient Temperature on Trip Energy Consumption")
plt.xlabel("Ambient Temperature (F)")
plt.ylabel("Trip Energy (kWh)")
plt.grid(True)
plt.show()
Next, let's take a look at the 2022 Tesla Model 3 and the 2020 Chevy Bolt and compare their energy consumption across the same range of ambient temperatures.
for model in [
"2022_Tesla_Model_3_RWD_Steady_Thermal",
"2020_Chevrolet_Bolt_EV_Steady_Thermal",
]:
for temp in [0, 15, 32, 50, 72, 90, 110]:
query["model_name"] = model
query["ambient_temperature"] = {"value": temp, "unit": "fahrenheit"}
result = app.run(query)
if "error" in result:
raise ValueError(f"Error for temperature {temp} F: {result['error']}")
else:
energy = result["route"]["traversal_summary"]["trip_energy_electric"][
"value"
]
temp_results.append(
{
"ambient_temperature_f": temp,
"trip_energy_electric": energy,
"vehicle_model": model,
}
)
print(
f"Model: {model}, Ambient Temperature: {temp} F, Trip Energy: {round(energy, 3)} kWh"
)
plot_df = pd.DataFrame(temp_results)
plt.figure(figsize=(10, 6))
for model in plot_df["vehicle_model"].unique():
model_data = plot_df[plot_df["vehicle_model"] == model]
plt.plot(
model_data["ambient_temperature_f"],
model_data["trip_energy_electric"],
marker="o",
label=model,
)
plt.title(
"Effect of Ambient Temperature on Trip Energy Consumption by Vehicle Model"
)
plt.xlabel("Ambient Temperature (F)")
plt.ylabel("Trip Energy (kWh)")
plt.grid(True)
plt.legend()
plt.show()