Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding demo files to build_and_test.sh run #55

Merged
merged 24 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3ea2802
[DRAFT] adding demo files to build_and_test.sh run
robinsteuteville Oct 4, 2023
178448c
making run_all.py, running all .py files with demo in title, adding t…
robinsteuteville Oct 12, 2023
c9de773
first attempt at getting the demos files to download with proper vers…
robinsteuteville Oct 12, 2023
87eaa62
updating comments
robinsteuteville Oct 12, 2023
7ab893a
adding demos testing to sh file like in altrios
robinsteuteville Oct 31, 2023
5ba2efb
adding requests to pyproject.toml
robinsteuteville Oct 31, 2023
8110144
tests are now (hopefully) triggered on push to `fastsim-2` or `fastsi…
calbaker Oct 31, 2023
145328a
adding demo tests to git tests and attempt at copy files function
robinsteuteville Nov 1, 2023
5a3e0cd
adding comments
robinsteuteville Nov 1, 2023
d911bb4
updated attempt at adding demos tests to git
robinsteuteville Nov 1, 2023
70d34a0
third attempt at adding demos test to git
robinsteuteville Nov 1, 2023
127f6aa
updating demo copy function
Nov 5, 2023
6846c89
adding working copy_demo_files function to utilities
Nov 8, 2023
9bbbdf8
adding test for copy_demo_files function, deleting unneeded test demo…
Nov 9, 2023
fcc1ecb
attempting to fix conflict with main
Nov 9, 2023
ab74072
troubleshooting fusion thermal demo
Nov 28, 2023
d5f3ba2
continuing troubleshooting fusion_thermal_demo
Nov 29, 2023
139c5f9
fixed fusion_thermal_demo.py file
Nov 29, 2023
4bef2ae
addressing merge conflicts between branch and fastsim-2
Nov 30, 2023
1bf29fc
Update .github/workflows/tests.yaml
robinsteuteville Nov 30, 2023
615ebf3
Update python/fastsim/__init__.py
robinsteuteville Nov 30, 2023
7752854
Update python/fastsim/__init__.py
robinsteuteville Nov 30, 2023
f79cc4f
fixing merge conflict with fastsim-2
Dec 4, 2023
92b3f2f
Merge branch 'fastsim-2' of https://github.com/NREL/fastsim into fix-…
Dec 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!"
41 changes: 37 additions & 4 deletions python/fastsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,45 @@ def package_root() -> Path:
from .resample import resample
from . import auxiliaries

from pkg_resources import get_distribution
# from pkg_resources import get_distribution

__version__ = get_distribution("fastsim").version
# __version__ = get_distribution("fastsim").version

__doc__ += "\nhttps://pypi.org/project/fastsim/"
__doc__ += "\nhttps://www.nrel.gov/transportation/fastsim.html"
# #download demo files
# import fastsim
# import os
# import pathlib
# import fnmatch
# import requests

# p = 'https://github.com/NREL/fastsim/tree/' + fastsim.__version__ + '/python/fastsim/demos'
# d = pathlib.Path(__file__).parent
# has_demos = False
# demos_dir = ''
# for dir in os.walk(d):
# if fnmatch.fnmatch(dir[0], '*demos'):
# has_demos = True
# demos_dir = dir[0]
# break
# if has_demos:
# #the problem is I can't figure out how to list the contents of the online demos file
# for f in p.get_dir_contents():
# already_downloaded = False
# for file in demos_dir.glob('*demo*.py'):
# #need a way to get the "basename" for the online demos files as well for this to work
# if f == os.path.basename(file.replace('\\', '/')): #necessary to ensure command works on all operating systems
# already_downloaded = True
# print('{} = {} already downloaded'.format(file, f))
# break
# if already_downloaded == False:
# #download file
# print('{} != {} needs downloading'.format(file, f)) #placeholder under I figure out how to download the file
# else:
# #just download demos folder
# print('demos folder needs downloading') #placeholder until I figure out how to download the file

# __doc__ += "\nhttps://pypi.org/project/fastsim/"
robinsteuteville marked this conversation as resolved.
Show resolved Hide resolved
robinsteuteville marked this conversation as resolved.
Show resolved Hide resolved
# __doc__ += "\nhttps://www.nrel.gov/transportation/fastsim.html"
robinsteuteville marked this conversation as resolved.
Show resolved Hide resolved

# Enable np.array() on array structs
import numpy as np
Expand Down
38 changes: 16 additions & 22 deletions python/fastsim/demos/cav_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

import fastsim as fsim
from fastsim.tests.test_coasting import make_coasting_plot
import fastsim.utilities as utils

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

def maybe_str_to_bool(x, default=True):
"""
Expand All @@ -37,8 +39,6 @@ def maybe_str_to_bool(x, default=True):
except:
return default

IS_INTERACTIVE = maybe_str_to_bool(os.getenv('FASTSIM_DEMO_IS_INTERACTIVE'))


# %% [markdown]
# ## Create a Vehicle and Cycle
Expand All @@ -52,9 +52,9 @@ def maybe_str_to_bool(x, default=True):
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
Expand All @@ -70,11 +70,11 @@ def maybe_str_to_bool(x, default=True):
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))
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_PLOTSe)

# %% [markdown]
# # Car Following at Average Speed
Expand Down Expand Up @@ -103,11 +103,11 @@ def maybe_str_to_bool(x, default=True):
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))
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 Down Expand Up @@ -135,7 +135,7 @@ def maybe_str_to_bool(x, default=True):
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}")
Expand All @@ -144,7 +144,7 @@ def maybe_str_to_bool(x, default=True):
(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,
idm_allow=True,
Expand All @@ -167,18 +167,17 @@ def maybe_str_to_bool(x, default=True):
sd.sim_params = fsim.auxiliaries.set_nested_values(sd.sim_params,
idm_v_desired_m_per_s=mt_avg_spd_m_per_s
)
if IS_INTERACTIVE:
print(f"... setting idm_v_desired_m_per_s = {sd.sim_params.idm_v_desired_m_per_s}")
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))
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
Expand Down Expand Up @@ -208,15 +207,10 @@ def maybe_str_to_bool(x, default=True):
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))
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)

# %%
# The flag below lets us know if this module ran successfully without error
RAN_SUCCESSFULLY = True
# %%
55 changes: 38 additions & 17 deletions python/fastsim/demos/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@

# local modules
import fastsim as fsim
import fastsim.utilities as utils
# importlib.reload(simdrive) importlib.reload(cycle)

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

#%%

v0 = fsim.vehicle.Vehicle.from_vehdb(10, to_rust=False)
Expand Down Expand Up @@ -128,7 +132,8 @@
ax[1].set_xlabel('Cycle Time [s]')
ax[1].set_ylabel('Speed [MPH]')

plt.show()
if SHOW_PLOTS:
plt.show()

# %%
fig, ax = plt.subplots(2, 1, figsize=(9, 5))
Expand All @@ -144,7 +149,8 @@
ax[1].set_xlabel('Cycle Time [s]')
ax[1].set_ylabel('Speed [MPH]')

plt.show()
if SHOW_PLOTS:
plt.show()

# %% [markdown]
# ## Running sim_drive_step() with modified auxInKw
Expand Down Expand Up @@ -174,7 +180,8 @@
plt.xlabel('Time [s]')
plt.ylabel('Power [kW]')
plt.legend()
plt.show()
if SHOW_PLOTS:
plt.show()
print(f'Time to simulate: {time.perf_counter() - t0:.2e} s')

# %%
Expand Down Expand Up @@ -205,7 +212,8 @@
plt.xlabel('Time [s]')
plt.ylabel('Power [kW]')
plt.legend()
plt.show()
if SHOW_PLOTS:
plt.show()
print(f'Time to simulate: {time.perf_counter() - t0:.2e} s')

# %% [markdown]
Expand All @@ -230,7 +238,8 @@
plt.xlabel('Time [s]')
plt.ylabel('Power [kW]')
plt.legend()
plt.show()
if SHOW_PLOTS:
plt.show()

print(f'Time to simulate: {time.perf_counter() - t0:.2e} s')

Expand All @@ -253,7 +262,8 @@
plt.xlabel('Time [s]')
plt.ylabel('Power [kW]')
plt.legend()
plt.show()
if SHOW_PLOTS:
plt.show()

print(f'Time to simulate: {time.perf_counter() - t0:.2e} s')

Expand Down Expand Up @@ -286,7 +296,8 @@
plt.xlabel('Time [s]')
plt.ylabel('Power [kW]')
plt.legend()
plt.show()
if SHOW_PLOTS:
plt.show()

print(f'Time to simulate: {time.perf_counter() - t0:.2e} s')

Expand Down Expand Up @@ -316,7 +327,8 @@
plt.xlabel('Time [s]')
plt.ylabel('Power [kW]')
plt.legend()
plt.show()
if SHOW_PLOTS:
plt.show()

print(f'Time to simulate: {time.perf_counter() - t0:.2e} s')

Expand All @@ -339,7 +351,8 @@
plt.xlabel('Time [s]')
plt.ylabel('Power [kW]')
plt.legend()
plt.show()
if SHOW_PLOTS:
plt.show()

print(f'Time to simulate: {time.perf_counter() - t0:.2e} s')

Expand All @@ -362,7 +375,8 @@
plt.xlabel('Time [s]')
plt.ylabel('Power [kW]')
plt.legend()
plt.show()
if SHOW_PLOTS:
plt.show()

print(f'Time to simulate: {time.perf_counter() - t0:.2e} s')

Expand Down Expand Up @@ -580,7 +594,8 @@ def get_sim_drive_vec(
df_fltr.mpgge.hist(bins=20, rwidth=.9)
plt.xlabel('Miles per Gallon')
plt.ylabel('Number of Cycles')
plt.show()
if SHOW_PLOTS:
plt.show()


# %%
Expand All @@ -607,7 +622,8 @@ def get_sim_drive_vec(
scatterpoints=1)
plt.xlabel('Average Cycle Speed [MPH]')
plt.ylabel('Fuel Economy [MPG]')
plt.show()
if SHOW_PLOTS:
plt.show()

# %% [markdown]
# # Cycle manipulation tools
Expand Down Expand Up @@ -661,7 +677,8 @@ def get_sim_drive_vec(
ax2.set_ylabel('Speed [MPH]', weight='bold', color='xkcd:pale red')
ax2.grid(False)
ax2.tick_params('y', colors='xkcd:pale red')
plt.show()
if SHOW_PLOTS:
plt.show()

# %% [markdown]
# ## Concat cycles/trips
Expand Down Expand Up @@ -717,7 +734,8 @@ def get_sim_drive_vec(
ax2.set_ylabel('Speed [MPH]', weight='bold', color='xkcd:pale red')
ax2.grid(False)
ax2.tick_params('y', colors='xkcd:pale red')
plt.show()
if SHOW_PLOTS:
plt.show()

# %% [markdown]
# ## Cycle comparison
Expand Down Expand Up @@ -751,7 +769,8 @@ def get_sim_drive_vec(
plt.plot(cyc10s.time_s, cyc10s.mph, marker=',')
plt.xlabel('Cycle Time [s]')
plt.ylabel('Vehicle Speed [mph]')
plt.show()
if SHOW_PLOTS:
plt.show()
print(f'Time to load and resample: {time.perf_counter() - t0:.2e} s')

# %% [markdown]
Expand Down Expand Up @@ -811,7 +830,8 @@ def get_sim_drive_vec(
ax2.set_ylabel('Speed [MPH]', weight='bold', color='xkcd:pale red')
ax2.grid(False)
ax2.tick_params('y', colors='xkcd:pale red')
plt.show()
if SHOW_PLOTS:
plt.show()

# %% [markdown]
# ## Clip by times
Expand Down Expand Up @@ -861,7 +881,8 @@ def get_sim_drive_vec(
ax2.set_ylabel('Speed [MPH]', weight='bold', color='xkcd:pale red')
ax2.grid(False)
ax2.tick_params('y', colors='xkcd:pale red')
plt.show()
if SHOW_PLOTS:
plt.show()

# %% [markdown]
# ### Test Coefficients Calculation
Expand Down
8 changes: 6 additions & 2 deletions python/fastsim/demos/demo_abc_drag_coef_conv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import fastsim as fsim
from fastsim.auxiliaries import abc_to_drag_coeffs, drag_coeffs_to_abc
import fastsim.utilities as utils
v = fsim.vehicle.Vehicle.from_vehdb(1).to_rust()
v2 = fsim.vehicle.Vehicle.from_vehdb(1).to_rust()

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

a = 25.91
b = 0.1943
c = 0.01796
Expand All @@ -13,12 +17,12 @@
c_lbf__mph2=c,
custom_rho=False,
simdrive_optimize=True,
show_plots=True,
show_plots=SHOW_PLOTS,
use_rust=True)
print(drag_coef)
print(wheel_rr_coef)

a_test, b_test, c_test = drag_coeffs_to_abc(veh=v,
fit_with_curve=False,
show_plots=True)
show_plots=SHOW_PLOTS)
print(a_test,b_test,c_test)
Loading