Skip to content

Commit

Permalink
EUREKA! downcasting solved the problem!
Browse files Browse the repository at this point in the history
  • Loading branch information
calbaker committed Jan 4, 2025
1 parent 697a7b4 commit 09948b6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
Empty file modified cal_and_val/f3-vehicles/2022 MINI Cooper SE Hardtop 2 door.yaml
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion fastsim-core/fastsim-proc-macros/src/fastsim_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ fn add_serde_methods(py_impl_block: &mut TokenStream2) {
#[staticmethod]
#[pyo3(name = "from_msg_pack")]
#[pyo3(signature = (msg_pack, skip_init=None))]
pub fn from_msg_pack_py(msg_pack: &[u8], skip_init: Option<bool>) -> PyResult<Self> {
pub fn from_msg_pack_py(msg_pack: &Bound<PyAny>, skip_init: Option<bool>) -> PyResult<Self> {
let msg_pack = msg_pack.downcast::<PyBytes>()?.as_bytes();
Self::from_msg_pack(
msg_pack,
skip_init.unwrap_or_default()
Expand Down
10 changes: 1 addition & 9 deletions python/fastsim/demos/demo_bev.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

from plot_utils import *

# if enivronment var `DEBUG_LOG=true` is set, turns on debug logging
DEBUG_LOG = os.environ.get("DEBUG_LOG", "false").lower() == "true"
# if environment var `SHOW_PLOTS=false` is set, no plots are shown
SHOW_PLOTS = os.environ.get("SHOW_PLOTS", "true").lower() == "true"
# if environment var `SAVE_FIGS=true` is set, save plots
Expand All @@ -44,13 +42,7 @@
# simulation start time
t0 = time.perf_counter()
# run simulation
# toggle commented code to enable logging
if DEBUG_LOG:
print('Running `sd.walk()` with debug logging')
with fsim.utils.with_logging():
sd.walk()
else:
sd.walk()
sd.walk()
# simulation end time
t1 = time.perf_counter()
t_fsim3_si1 = t1 - t0
Expand Down
10 changes: 1 addition & 9 deletions python/fastsim/demos/demo_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

from plot_utils import *

# if enivronment var `DEBUG_LOG=true` is set, turns on debug logging
DEBUG_LOG = os.environ.get("DEBUG_LOG", "false").lower() == "true"
# if environment var `SHOW_PLOTS=false` is set, no plots are shown
SHOW_PLOTS = os.environ.get("SHOW_PLOTS", "true").lower() == "true"
# if environment var `SAVE_FIGS=true` is set, save plots
Expand All @@ -44,13 +42,7 @@
# simulation start time
t0 = time.perf_counter()
# run simulation
# toggle commented code to enable logging
if DEBUG_LOG:
print('Running `sd.walk()` with debug logging')
with fsim.utils.with_logging():
sd.walk()
else:
sd.walk()
sd.walk()
# simulation end time
t1 = time.perf_counter()
t_fsim3_si1 = t1 - t0
Expand Down
2 changes: 0 additions & 2 deletions python/fastsim/demos/demo_hev.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
sns.set_theme()


# if enivronment var `DEBUG_LOG=true` is set, turns on debug logging
DEBUG_LOG = os.environ.get("DEBUG_LOG", "false").lower() == "true"
# if environment var `SHOW_PLOTS=false` is set, no plots are shown
SHOW_PLOTS = os.environ.get("SHOW_PLOTS", "true").lower() == "true"
# if environment var `SAVE_FIGS=true` is set, save plots
Expand Down
33 changes: 33 additions & 0 deletions python/fastsim/tests/test_serde.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np
from pathlib import Path
import time
import json
import msgpack
import os
from typing import Tuple
import fastsim as fsim

# load 2012 Ford Fusion from file
veh = fsim.Vehicle.from_resource("2022_Renault_Zoe_ZE50_R135.yaml")

# Set `save_interval` at vehicle level -- cascades to all sub-components with time-varying states
fsim.set_param_from_path(veh, "save_interval" , 1)

# load cycle from file
cyc = fsim.Cycle.from_resource("udds.csv")

# instantiate SimDrive
sd = fsim.SimDrive(veh, cyc)

# simulation start time
# run simulation
sd.walk()
# simulation end time

# TODO: make performmance benchmarks comparing full circle (de)serialization
# with different formats (e.g. message pack, json, yaml, ...)

def test_msg_pack():
sd_dict = msgpack.loads(sd.to_msg_pack())
assert sd.to_msg_pack() == msgpack.packb(sd_dict)
fsim.SimDrive.from_msg_pack(msgpack.packb(sd_dict))

0 comments on commit 09948b6

Please sign in to comment.