Skip to content

Commit

Permalink
Merge branch 'fastsim-2' of github.com:NREL/fastsim into update-test-…
Browse files Browse the repository at this point in the history
…output

# Conflicts:
#	python/fastsim/demos/vehicle_import_demo.py
  • Loading branch information
calbaker committed Mar 26, 2024
2 parents d11dfc1 + 92da6ca commit 3d17c4b
Show file tree
Hide file tree
Showing 49 changed files with 10,597 additions and 2,326 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: stable
override: true

- run: rustup target add aarch64-apple-darwin
Expand All @@ -44,7 +44,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: stable
override: true
- run: cd rust/ && cargo test

Expand All @@ -71,9 +71,9 @@ jobs:
# TODO: why doesn't pytest work with cibuildwheel?
# CIBW_TEST_COMMAND: "pytest -v {project}/python/fastsim/tests"
CIBW_TEST_COMMAND: "python -m unittest discover {project}/python/fastsim/tests"
CIBW_ARCHS_MACOS: 'universal2'
CIBW_ARCHS_MACOS: "universal2"
# see https://cibuildwheel.readthedocs.io/en/stable/faq/#universal2
CIBW_TEST_SKIP: '*_universal2:arm64'
CIBW_TEST_SKIP: "*_universal2:arm64"
CIBW_ENVIRONMENT: 'PATH="$HOME/.cargo/bin:$PATH"'
CIBW_ENVIRONMENT_WINDOWS: 'PATH="$UserProfile\.cargo\bin;$PATH"'
CIBW_MANYLINUX_X86_64_IMAGE: "manylinux2014"
Expand All @@ -83,6 +83,7 @@ jobs:
rustup default stable &&
rustup show
CIBW_BEFORE_BUILD_LINUX: >
yum -y install openssl openssl-devel &&
pip install -U setuptools-rust &&
curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=nightly --profile=minimal -y &&
rustup show
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
run: |
cd rust/ && cargo test
# TODO: For some reason, `-e` is required in the pip install command
# This slows down installation, so should be fixed
# https://github.nrel.gov/MBAP/fastsim/issues/310
- name: Python unit tests
run: |
pip install -e ".[dev]" && pytest -v python/fastsim/tests/
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/wheels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: stable
override: true

- run: rustup target add aarch64-apple-darwin
Expand All @@ -46,7 +46,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: stable
override: true
- run: cd rust/ && cargo test

Expand All @@ -73,9 +73,9 @@ jobs:
# TODO: why doesn't pytest work with cibuildwheel?
# CIBW_TEST_COMMAND: "pytest -v {project}/python/fastsim/tests"
CIBW_TEST_COMMAND: "python -m unittest discover {project}/python/fastsim/tests"
CIBW_ARCHS_MACOS: 'universal2'
CIBW_ARCHS_MACOS: "universal2"
# see https://cibuildwheel.readthedocs.io/en/stable/faq/#universal2
CIBW_TEST_SKIP: '*_universal2:arm64'
CIBW_TEST_SKIP: "*_universal2:arm64"
CIBW_ENVIRONMENT: 'PATH="$HOME/.cargo/bin:$PATH"'
CIBW_ENVIRONMENT_WINDOWS: 'PATH="$UserProfile\.cargo\bin;$PATH"'
CIBW_MANYLINUX_X86_64_IMAGE: "manylinux2014"
Expand All @@ -85,6 +85,7 @@ jobs:
rustup default stable &&
rustup show
CIBW_BEFORE_BUILD_LINUX: >
yum -y install openssl openssl-devel &&
pip install -U setuptools-rust &&
curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=nightly --profile=minimal -y &&
rustup show
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

[![homepage](https://img.shields.io/badge/homepage-fastsim-blue)](https://www.nrel.gov/transportation/fastsim.html) [![tests](/~https://github.com/NREL/fastsim/actions/workflows/tests.yaml/badge.svg)](/~https://github.com/NREL/fastsim/actions/workflows/tests.yaml) [![wheels](/~https://github.com/NREL/fastsim/actions/workflows/wheels.yaml/badge.svg)](/~https://github.com/NREL/fastsim/actions/workflows/wheels.yaml?event=release) [![python](https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10-blue)](https://pypi.org/project/fastsim/) [![documentation](https://img.shields.io/badge/documentation-book-blue.svg)](https://nrel.github.io/fastsim/) [![github](https://img.shields.io/badge/github-fastsim-blue.svg)](/~https://github.com/NREL/fastsim)


## Description

This is the python/rust flavor of [NREL's FASTSim<sup>TM</sup>](https://www.nrel.gov/transportation/fastsim.html), which is based on the original Excel implementation. Effort will be made to keep the core methodology between this software and the Excel flavor in line with one another.
Expand Down Expand Up @@ -59,17 +58,25 @@ Developers might want to install the code in place so that FASTSim files can be

To see and run examples, navigate to `./python/fastsim/demos` and run the various *demo.py files to see fastsim use cases. There are other examples in fastsim/tests.

## Adding FASTSim as a Depency in Rust
## Adding FASTSim as a Dependency in Rust

### Via GitHub

Add this line:
`fastsim-core = { git = "https://github.nrel.gov/MBAP/fastsim", branch = "rust-port" }`
Add this line:

```
fastsim-core = { git = "/~https://github.com/NREL/fastsim/", branch = "fastsim-2" }
```

to your Cargo.toml file, modifying the `branch` key as appropriate.

### Via Cargo

This has not been implemented yet.
FASTSim is [available as a Rust crate](https://crates.io/crates/fastsim-core), which can be added to your dependencies via the following command:

```
cargo add fastsim-core
```

## List of Abbreviations

Expand Down Expand Up @@ -104,6 +111,7 @@ Rust versions of classes have limited Language Server Protocol integration, and

## Release Notes

2.1.2 -- SerdeAPI revamp with many new functions, various new vehicles, calibration demo, better error propagation, demo testing
2.1.1 -- license changed to Apache 2.0, default cycle grade and road type to zero if not provided, defaults to regenerative braking parameters, optional documentation fields now generated in Rust
2.1.0 -- release and installation improvements, RustVehicle init cleanup, calibration improvements
2.0.11 - 2.0.22 -- PyPI fixes. Also, Rust version is now >100x faster than Python version.
Expand Down Expand Up @@ -147,6 +155,7 @@ Rust versions of classes have limited Language Server Protocol integration, and
Chad Baker -- <Chad.Baker@nrel.gov>
Aaron Brooker -- <Aaron.Brooker@nrel.gov>
Kyle Carow -- <Kyle.Carow@nrel.gov>
Robin Steuteville -- <Robin.Steuteville@nrel.gov>
Jeffrey Gonder -- <Jeff.Gonder@nrel.gov>
Jacob Holden -- <Jacob.Holden@nrel.gov>
Jinghu Hu -- <Jinghu.Hu@nrel.gov>
Expand Down
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
- [Documentation](./fastsim-doc.md)
- [Python](./python-doc.md)
- [Rust](./rust-doc.md)
- [Calibration/Validation](./cal_and_val.md)
- [How to Update This Book](./how-to-update.md)
88 changes: 88 additions & 0 deletions docs/src/cal_and_val.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Calibration and Validation of Vehicle Models
FASTSim powertrain models can have varying levels of calibration and resolution based on available calibration and validation data. In the simplest (US) cases, the only available validation data for a powertrain model is the EPA "window sticker" energy consumption rates. However, there are also situations in which detailed dynamometer or on-road data is available for a particular vehicle, enabling much more detailed model calibration. This documentation is meant to summarize these various calibration levels and the tools available to help with more detailed calibration.

## Calibration/Validation Levels

| Level | Calibration | Validation |
| --- | --- | --- |
| 0 | Vehicle is parameterized without any fitting to performance data. This is called __parameterization__, not calibration. | Could be none or could be validated against aggregate energy consumption data like EPA window sticker values. |
| 1 | Vehicle parameters are adjusted so that model results reasonably match test data for aggregate, cycle-level data (e.g. fuel usage, net SOC change). | Model results reasonably match at least some aggregate, cycle-level test data not used in any calibration process. |
| 2 | Vehicle parameters are adjusted so that model results reasonably match test data for time-resolved test data (e.g. instantaneous fuel usage, instantaneous cumulative fuel usage, instantaneous SOC). | Model results reasonably match at least some time-resolved test data not used in any calibration process. |
| 3 | Some amount of component-level thermal modeling is included and vehicle parameters are adjusted so that model results reasonably match test data for time-resolved test data (e.g. instantaneous fuel usage, instantaneous cumulative fuel usage, instantaneous SOC). | Model results reasonably match time-resolved test data not used in any calibration process that covers various temperatures and/vehcile transient thermal states. |

Examples of calibration levels 0, 2, and 3 from the [FASTSim Validation Report](https://www.nrel.gov/docs/fy22osti/81097.pdf):

![image](/~https://github.com/NREL/fastsim/assets/4818940/1b7dae5d-b328-406e-9e2c-07abadff7a3a)

![image](/~https://github.com/NREL/fastsim/assets/4818940/530f6a15-8400-4618-a97a-da67609f6ecd)

![image](/~https://github.com/NREL/fastsim/assets/4818940/8483661f-dee4-4d59-9d69-e6d54dae0100)

## Calibration Level 0 (Parameterization) Guidelines
As noted in the table above, parameterization of a new FASTSim powertrain model is performed when little or no ground truth performance data is available for a specific vehicle. One example of this is if EPA window-sticker fuel economy is the only available performance data. In this situation, it is recommended to parameterize a FASTSim powertrain model using the most reliable vehicle parameters from available information (e.g., specification websites). This helps to avoid overfitting and relies on the robustness of the FASTSim approach to capture the most important powertrain dynamics and simulate energy consumption.

- Create a new vehicle file, either from a template or an existing vehicle model (ideally of the same powertrain type)
- Enter vehicle parameters from various specification sources (__note__: it is recommended to document the source of specifications that are used to determine each parameter)
- `veh_pt_type` and `fc_eff_type` are important high level descriptors that define the powertrain technology
- `veh_pt_type`: Vehicle powertrain type
- Parameter values:
- `Conv`: conventional (ICE, gasoline or diesel) vehicle
- `HEV`: hybrid electric vehicle
- `PHEV`: plug-in hybrid electric vehicle
- `BEV`: battery electric vehicle
- `fc_eff_type`: Fuel converter efficiency type
- This parameter is used to retrieve the default `fc_eff_map` for a particular engine type if a custom map is not provided
- Unnecessary and not used for vehicles without a fuel converter (e.g. BEVs)
- Parameter values:
- `SI`: spark ignition
- `Atkinson`: Atkinson cycle (typical for hybrids)
- `Diesel`: diesel (compression ignition)
- `H2FC`: hydrogen fuel cell (use with `veh_pt_type` set to `HEV`)
- `HD_Diesel`: heavy-duty diesel
- `veh_override_kg` is the simplest way to specify total vehicle mass
- If not provided, the various component mass parameters will be used to calculate total vehicle mass
- If `veh_override_kg` is provided, component mass parameters are unnecessary and not used
- `drag_coef` and `wheel_rr_coef` can be calculated from dynamometer road load equation coefficients (ABCs) for vehicles tested by the US EPA using `fastsim.auxiliaries.abc_to_drag_coeffs`. Test data, including the road load coefficients from coast-down testing, for cars tested by the US EPA is available [here](https://www.epa.gov/compliance-and-fuel-economy-data/data-cars-used-testing-fuel-economy).
- `drag_coef` is sometimes provided on specification websites and reasonable values informed by engineering judgement for `wheel_rr_coef` can be used , but when possible the ABCs and `fastsim.auxiliaries.abc_to_drag_coeffs` method should be used instead
- `wheel_radius_m` is often not explicitly available for a vehicle, but a tire code can be supplied to `fastsim.utils.calculate_tire_radius` to calculate a radius
- Note: For hybrids, 'total system power' is often provided (e.g., combined ICE and electric motor powers). This should not be used for either `fc_max_kw` or `mc_max_kw`, peak engine-only power should be used for `fc_max_kw` and peak electric motor-only power for `mc_max_kw`.

## Calibration Level 2 Guidelines
- Copy
[calibration_demo.py](/~https://github.com/NREL/fastsim/blob/fastsim-2/python/fastsim/demos/calibration_demo.py)
to your project directory and modify as needed.
- By default, this script selects the model that minimizes the euclidean error across
all objectives, which may not be the way that you want to select your final design.
By looking at the plots that get generated in `save_path`, you can use both the time
series and parallel coordinates plots to down select an appropriate design.
- Because PyMOO is a multi-objective optimizer that finds a multi-dimensional Pareto
surface, it will not necessarily return a single _best_ result -- rather, it will
produce a pareto-optimal set of results, and you must down select. Often, the design
with minimal euclidean error will be the best design, but it's good to pick a handful
of designs from the pareto set and check how they behave in the time-resolved plots
that can be optionally generated by the optimization script.
- Run `python calibration_demo.py --help` to see details about how to run calibration
and validation. Greater population size typically results in faster convergence at the
expense of increased run time for each generation. There's no benefit in having a
number of processes larger than the population size. `xtol` and `ftol` (see CLI help)
can be used to adjust when the minimization is considered converged. If the
optimization is terminating when `n_max_gen` is hit, then that means it has not
converged, and you may want to increase `n_max_gen`.
- Usually, start out with an existing vehicle model that is reasonably close to the
new vehicle, and make sure to provide as many explicit parameters as possible. In
some cases, a reasonable engineering judgment is appropriate.
- Resample data to 1 Hz. This is a good idea because higher frequency data will cause
fastsim to run more slowly. This can be done with `fastsim.resample.resample`. Be
sure to specify `rate_vars` (e.g. fuel power flow rate [W]), which will be time
averaged over the previous time step in the new frequency.
- Identify test data signals and corresponding fastsim signals that need to match.
These pairs of signals will be used to construct minimization objectives. See
where `obj_names` is defined in `calibration_demo.py` for an example.
- See where `cycs[key]` gets assigned to see an example of constructing a Cycle from a dataframe.
- Partition out calibration/validation data by specifying a tuple of regex patterns
that correspond to cycle names. See where `cal_cyc_patterns` is defined for an
example. Typically, it's good to reserve about 25-33% of your data for validation.
- To set parameters and corresponding ranges that the optimizer is allowed to use in
getting the model to match test data, see where `params_and_bounds` is defined
below.

4 changes: 2 additions & 2 deletions docs/src/how-to-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Run the following in the repository root directory:

## Publishing
1. Update `book.toml` or files in `docs/src/`
1. Make sure the docs look good locally by running the
1. Make sure the docs look good locally by running the steps in [Serving Locally](#serving-locally)
1. Commit files and push to `main` branch

After that, a GitHub action will build the book and publish it [here](https://pages.github.nrel.gov/MBAP/mbap-computing/)
After that, a GitHub action will build the book and publish it.
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "fastsim"
version = "2.1.1"
version = "2.1.2"
authors = [{ name = "NREL/MTES/CIMS/MBAP Group", email = "fastsim@nrel.gov" }]
description = "Tool for modeling vehicle powertrains"
readme = "README.md"
Expand All @@ -27,7 +27,8 @@ dependencies = [
"pyyaml",
"pytest",
"setuptools<=65.6.3", # suppresses pkg_resources deprecation warning
"openpyxl>=3.1.2"
"openpyxl>=3.1.2",
"plotly==5.18",
]

[project.urls]
Expand Down
16 changes: 8 additions & 8 deletions python/fastsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import logging
import traceback

from . import fastsimrust
from . import fastsimrust as fsr
from . import parameters as params
from . import utils
from . import simdrive, vehicle, cycle, calibration, tests
from . import calibration as cal
from .resample import resample
from . import auxiliaries
from fastsim import parameters as params
from fastsim import utils
from fastsim import simdrive, vehicle, cycle, calibration, tests
from fastsim import calibration as cal
from fastsim.resample import resample
from fastsim import auxiliaries
from fastsim import fastsimrust
from fastsim import fastsimrust as fsr


def package_root() -> Path:
Expand Down
Loading

0 comments on commit 3d17c4b

Please sign in to comment.