Skip to content

Commit

Permalink
Merge pull request #55 from NREL/fix-demos
Browse files Browse the repository at this point in the history
adding demo files to build_and_test.sh run
  • Loading branch information
calbaker authored Dec 12, 2023
2 parents 5f3d69a + 92b3f2f commit ecbf517
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 525 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: tests

on:
push:
branches: [fastsim-2, fastsim-3]
pull_request:
workflow_dispatch:

Expand Down Expand Up @@ -36,4 +38,5 @@ jobs:
- name: Python unit tests
run: |
pip install -e ".[dev]" && pytest -v python/fastsim/tests/
pip install -e ".[dev]" && pytest -v python/fastsim/tests/
pytest -v python/fastsim/demos/
7 changes: 6 additions & 1 deletion build_and_test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# build and test with local version of `fastsim-proc-macros`
echo "Testing rust" && \
(cd rust/fastsim-core/ && cargo test) && \
(cd rust/fastsim-cli/ && cargo test) && \
pip install -qe ".[dev]" && \
pytest -v python/fastsim/tests/
echo "Running python tests" && \
pytest -v python/fastsim/tests/ && \
echo "Verifying that demos run" && \
pytest -v python/fastsim/demos/ && \
echo "Complete success!"
1 change: 0 additions & 1 deletion python/fastsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def package_root() -> Path:
logger = logging.getLogger(__name__)

from pkg_resources import get_distribution

__version__ = get_distribution("fastsim").version

__doc__ += "\nhttps://pypi.org/project/fastsim/"
Expand Down
99 changes: 43 additions & 56 deletions python/fastsim/demos/cav_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,78 @@

import fastsim as fsim
from fastsim.tests.test_coasting import make_coasting_plot
from fastsim.demos.utils import maybe_str_to_bool, DEMO_TEST_ENV_VAR

RAN_SUCCESSFULLY = False

IS_INTERACTIVE = maybe_str_to_bool(os.getenv(DEMO_TEST_ENV_VAR))

# for testing demo files, false when running automatic tests
SHOW_PLOTS = fsim.utils.show_plots()

# %% [markdown]
# ## Create a Vehicle and Cycle
#
#
# We're going to use a conventional vehicle with
# the UDDS cycle.
# %%
cyc = fsim.cycle.Cycle.from_file('udds').to_rust()
cyc = fsim.cycle.Cycle.from_file("udds").to_rust()
veh = fsim.vehicle.Vehicle.from_vehdb(1).to_rust()
sd = fsim.simdrive.RustSimDrive(cyc, veh)
sd.sim_drive()

base_mpg = sd.mpgge
if IS_INTERACTIVE:
if SHOW_PLOTS:
print(f"Base fuel economy over UDDS: {sd.mpgge} mpg")
make_coasting_plot(sd.cyc0, sd.cyc, do_show=True)
make_coasting_plot(sd.cyc0, sd.cyc, do_show=SHOW_PLOTS)

# %% [markdown]
# ## Eco-Coasting
# %%
cyc = fsim.cycle.Cycle.from_file('udds').to_rust()
cyc = fsim.cycle.Cycle.from_file("udds").to_rust()
veh = fsim.vehicle.Vehicle.from_vehdb(1).to_rust()
sd = fsim.simdrive.RustSimDrive(cyc, veh)
sd.sim_params = fsim.auxiliaries.set_nested_values(sd.sim_params,
sd.sim_params = fsim.auxiliaries.set_nested_values(
sd.sim_params,
coast_allow=True,
coast_allow_passing=False,
coast_start_speed_m_per_s=-1.0
coast_start_speed_m_per_s=-1.0,
)
sd.sim_drive()

coast_mpg = sd.mpgge
if IS_INTERACTIVE:
if SHOW_PLOTS:
print(f"Coast fuel economy over UDDS: {sd.mpgge} mpg")
pct_savings = ((1.0/base_mpg) - (1.0/coast_mpg)) * 100.0 / ((1.0/base_mpg))
pct_savings = ((1.0 / base_mpg) - (1.0 / coast_mpg)) * 100.0 / ((1.0 / base_mpg))
print(f"Percent Savings: {pct_savings} %")
make_coasting_plot(sd.cyc0, sd.cyc, do_show=True)
make_coasting_plot(sd.cyc0, sd.cyc, do_show=SHOW_PLOTS)

# %% [markdown]
# # Car Following at Average Speed
#
# Here we set up an "automated cruise control" for a system
# that drives the average speed of the cycle.
cyc_udds = fsim.cycle.Cycle.from_file('udds').to_dict()
cyc_udds = fsim.cycle.Cycle.from_file("udds").to_dict()
cyc_stop = fsim.cycle.resample(
fsim.cycle.make_cycle([0.0, 200.0], [0.0, 0.0]),
new_dt=1.0,
)
cyc = fsim.cycle.Cycle.from_dict(
fsim.cycle.concat([cyc_udds, cyc_stop])
).to_rust()
cyc = fsim.cycle.Cycle.from_dict(fsim.cycle.concat([cyc_udds, cyc_stop])).to_rust()
veh = fsim.vehicle.Vehicle.from_vehdb(1).to_rust()
sd = fsim.simdrive.RustSimDrive(cyc, veh)

sd.sim_params = fsim.auxiliaries.set_nested_values(sd.sim_params,
sd.sim_params = fsim.auxiliaries.set_nested_values(
sd.sim_params,
idm_allow=True,
idm_accel_m_per_s2=1.0,
idm_decel_m_per_s2=-2.5,
idm_dt_headway_s=2.0,
idm_minimum_gap_m=0.0,
idm_v_desired_m_per_s=np.average(np.array(cyc.mps))
idm_v_desired_m_per_s=np.average(np.array(cyc.mps)),
)
sd.sim_drive()

cruise_mpg = sd.mpgge
if IS_INTERACTIVE:
if SHOW_PLOTS:
print(f"Cruise fuel economy over UDDS: {sd.mpgge} mpg")
pct_savings = ((1.0/base_mpg) - (1.0/cruise_mpg)) * 100.0 / ((1.0/base_mpg))
pct_savings = ((1.0 / base_mpg) - (1.0 / cruise_mpg)) * 100.0 / ((1.0 / base_mpg))
print(f"Percent Savings: {pct_savings} %")
make_coasting_plot(sd.cyc0, sd.cyc, do_show=True)
make_coasting_plot(sd.cyc0, sd.cyc, do_show=SHOW_PLOTS)

# %% [markdown]
# # Eco-Cruising at Multiple Average Speeds
Expand All @@ -96,14 +93,12 @@
# the average speed of the microtrip assuming the vehicle
# is able to get the average speed per microtrip from some
# external source.
cyc_udds = fsim.cycle.Cycle.from_file('udds').to_dict()
cyc_udds = fsim.cycle.Cycle.from_file("udds").to_dict()
cyc_stop = fsim.cycle.resample(
fsim.cycle.make_cycle([0.0, 200.0], [0.0, 0.0]),
new_dt=1.0,
)
cyc = fsim.cycle.Cycle.from_dict(
fsim.cycle.concat([cyc_udds, cyc_stop])
).to_rust()
cyc = fsim.cycle.Cycle.from_dict(fsim.cycle.concat([cyc_udds, cyc_stop])).to_rust()
veh = fsim.vehicle.Vehicle.from_vehdb(1).to_rust()
sd = fsim.simdrive.RustSimDrive(cyc, veh)
dist_and_avg_speeds = []
Expand All @@ -115,24 +110,23 @@
mt_dist_m = sum(mt_cyc.dist_m)
mt_time_s = mt_cyc.time_s[-1]
mt_avg_spd_m_per_s = mt_dist_m / mt_time_s if mt_time_s > 0.0 else 0.0
if IS_INTERACTIVE:
if SHOW_PLOTS:
print(f"mt num points : {len(mt_cyc.time_s)}")
print(f"mt dist (m) : {mt_dist_m}")
print(f"mt time (s) : {mt_time_s}")
print(f"mt avg speed (m/s) : {mt_avg_spd_m_per_s}")
dist_and_avg_speeds.append(
(dist_at_start_of_microtrip_m, mt_avg_spd_m_per_s)
)
dist_and_avg_speeds.append((dist_at_start_of_microtrip_m, mt_avg_spd_m_per_s))
dist_at_start_of_microtrip_m += mt_dist_m
if IS_INTERACTIVE:
if SHOW_PLOTS:
print(f"Found speeds for {len(dist_and_avg_speeds)} microtrips")
sd.sim_params = fsim.auxiliaries.set_nested_values(sd.sim_params,
sd.sim_params = fsim.auxiliaries.set_nested_values(
sd.sim_params,
idm_allow=True,
idm_accel_m_per_s2=0.5,
idm_decel_m_per_s2=-2.5,
idm_dt_headway_s=2.0,
idm_minimum_gap_m=10.0,
idm_v_desired_m_per_s=dist_and_avg_speeds[0][1]
idm_v_desired_m_per_s=dist_and_avg_speeds[0][1],
)
sd.init_for_step(init_soc=veh.max_soc)
current_mt_idx = 0
Expand All @@ -144,34 +138,33 @@
if current_mt_idx < len(dist_and_avg_speeds):
mt_start_dist_m, mt_avg_spd_m_per_s = dist_and_avg_speeds[current_mt_idx]
if dist_traveled_m >= mt_start_dist_m:
sd.sim_params = fsim.auxiliaries.set_nested_values(sd.sim_params,
idm_v_desired_m_per_s=mt_avg_spd_m_per_s
sd.sim_params = fsim.auxiliaries.set_nested_values(
sd.sim_params, idm_v_desired_m_per_s=mt_avg_spd_m_per_s
)
print(
f"... setting idm_v_desired_m_per_s = {sd.sim_params.idm_v_desired_m_per_s}"
)
if IS_INTERACTIVE:
print(f"... setting idm_v_desired_m_per_s = {sd.sim_params.idm_v_desired_m_per_s}")
current_mt_idx += 1
sd.sim_drive_step()
sd.set_post_scalars()

cruise_mpg = sd.mpgge
if IS_INTERACTIVE:
if SHOW_PLOTS:
print(f"Cruise fuel economy over UDDS: {sd.mpgge} mpg")
pct_savings = ((1.0/base_mpg) - (1.0/cruise_mpg)) * 100.0 / ((1.0/base_mpg))
pct_savings = ((1.0 / base_mpg) - (1.0 / cruise_mpg)) * 100.0 / ((1.0 / base_mpg))
print(f"Percent Savings: {pct_savings} %")
make_coasting_plot(sd.cyc0, sd.cyc, do_show=True)
make_coasting_plot(sd.cyc0, sd.cyc, do_show=SHOW_PLOTS)

# %% [markdown]
# # Eco-Cruise and Eco-Approach running at the same time
#
# Here, we run an Eco-Cruise and Eco-Approach at the same time.
cyc_udds = fsim.cycle.Cycle.from_file('udds').to_dict()
cyc_udds = fsim.cycle.Cycle.from_file("udds").to_dict()
cyc_stop = fsim.cycle.resample(
fsim.cycle.make_cycle([0.0, 400.0], [0.0, 0.0]),
new_dt=1.0,
)
cyc = fsim.cycle.Cycle.from_dict(
fsim.cycle.concat([cyc_udds, cyc_stop])
).to_rust()
cyc = fsim.cycle.Cycle.from_dict(fsim.cycle.concat([cyc_udds, cyc_stop])).to_rust()
veh = fsim.vehicle.Vehicle.from_vehdb(1).to_rust()
sd = fsim.simdrive.RustSimDrive(cyc, veh)
params = sd.sim_params
Expand All @@ -184,19 +177,13 @@
params.idm_decel_m_per_s2 = -2.5
params.idm_dt_headway_s = 2.0
params.idm_minimum_gap_m = 10.0
params.idm_v_desired_m_per_s = 15.0 # np.sum(cyc_udds['mps']) / cyc_udds['time_s'][-1]
params.idm_v_desired_m_per_s = 15.0 # np.sum(cyc_udds['mps']) / cyc_udds['time_s'][-1]
sd.sim_params = params
sd.sim_drive()

if IS_INTERACTIVE:
if SHOW_PLOTS:
eco_mpg = sd.mpgge
print(f"Cruise and Coast fuel economy over UDDS: {sd.mpgge} mpg")
pct_savings = ((1.0/base_mpg) - (1.0/eco_mpg)) * 100.0 / ((1.0/base_mpg))
pct_savings = ((1.0 / base_mpg) - (1.0 / eco_mpg)) * 100.0 / ((1.0 / base_mpg))
print(f"Percent Savings: {pct_savings} %")
make_coasting_plot(sd.cyc0, sd.cyc, do_show=True)


# %%
# The flag below lets us know if this module ran successfully without error
RAN_SUCCESSFULLY = True
# %%
make_coasting_plot(sd.cyc0, sd.cyc, do_show=SHOW_PLOTS)
Loading

0 comments on commit ecbf517

Please sign in to comment.