From 8eaafb6bdadc0b16dab1c6683b54484a8dba1712 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sun, 30 Mar 2025 13:10:48 +0200 Subject: [PATCH] refactored --- deep_diff.py | 5 ++- eval_records.py | 6 ++- innenwiderstand.py | 78 +++++++++++++++++---------------- jay_diff.py | 6 +-- luftwiderstand.py | 106 +++++++++++++++++++++++---------------------- we_collector.py | 8 ++-- 6 files changed, 109 insertions(+), 100 deletions(-) diff --git a/deep_diff.py b/deep_diff.py index 1349df2..c9523f7 100644 --- a/deep_diff.py +++ b/deep_diff.py @@ -34,8 +34,7 @@ def deep_merge(d, u, only_existing=True): d[k] = u[k] return d - -if __name__ == '__main__': +def main() -> None: files = [ "results/WVWZZZE1ZMP010760/2024/10/18/vehicle_WVWZZZE1ZMP010760_2024-10-15T12-05-23.json", "results/WVWZZZE1ZMP010760/2024/10/19/vehicle_WVWZZZE1ZMP010760_2024-10-19T00-00-34.json", @@ -72,3 +71,5 @@ if __name__ == '__main__': # Check integrity assert src == merged_list +if __name__ == '__main__': + main() diff --git a/eval_records.py b/eval_records.py index 777cb75..931e6c6 100644 --- a/eval_records.py +++ b/eval_records.py @@ -235,8 +235,7 @@ def make_values(_ts_list, _ep: Endpoint, ic=0): return v_list - -if __name__ == '__main__': +def main() -> None: end_points = [] for k in settings.eps_defs.keys(): end_points.append(Endpoint(k, settings.eps_defs[k])) @@ -326,3 +325,6 @@ if __name__ == '__main__': plot.grid() plot.show() + +if __name__ == '__main__': + main() diff --git a/innenwiderstand.py b/innenwiderstand.py index efec6c2..122e5ff 100644 --- a/innenwiderstand.py +++ b/innenwiderstand.py @@ -29,46 +29,50 @@ def model_bat_dcr(temperature: float, soc: float, p: Params): return rb * np.exp(p.alpha*(t2 - tb2) + p.beta*(_t - tb)) -temps = [0, 5, 10, 20, 25, 40] -pl = 10000 -il = 100 -ul = 100 -rl = ul/il -p_ = np.linspace(0, 20000, 100) -i_ = p_/ul +def main() -> None: + temps = [0, 5, 10, 20, 25, 40] + pl = 10000 + il = 100 + ul = 100 + rl = ul/il + p_ = np.linspace(0, 20000, 100) + i_ = p_/ul -bat_params = Params(alpha=0.0007545, beta=-0.07033) -rt_ = [] -for t in temps: - rt_.append(model_bat_dcr(t, 100, bat_params)) + bat_params = Params(alpha=0.0007545, beta=-0.07033) + rt_ = [] + for t in temps: + rt_.append(model_bat_dcr(t, 100, bat_params)) -plot.figure() -plot.plot(temps, rt_) -r, t = bas(100) -plot.title(f"DCR vs temperature (Base DCR={r}@{t}°C)") -plot.xlabel("Temperature [°C]") -plot.ylabel("R [Ohm]") -plot.grid() - -titles = [ - "Battery power loss vs internal battery resistance (DCR)", - "Batter power consumption vs internal battery resistance (DCR)" -] -for sp, name, title in zip([0, 1], ["P_loss [kW]", "P_batt [kW]"], titles): plot.figure() - legs = [] + plot.plot(temps, rt_) + r, t = bas(100) + plot.title(f"DCR vs temperature (Base DCR={r}@{t}°C)") + plot.xlabel("Temperature [°C]") + plot.ylabel("R [Ohm]") plot.grid() - for temp in temps: - dcr = model_bat_dcr(temp, 100, bat_params) - pi_ = (dcr * i_ * i_) + sp*p_ - x = p_/1000 - y = pi_/1000 - legs.append(f"temp={temp} °C") - plot.plot(x, y) - plot.xlabel("P_demand [kW]") - plot.title(title) - plot.legend(legs) - plot.ylabel(name) + titles = [ + "Battery power loss vs internal battery resistance (DCR)", + "Batter power consumption vs internal battery resistance (DCR)" + ] + for sp, name, title in zip([0, 1], ["P_loss [kW]", "P_batt [kW]"], titles): + plot.figure() + legs = [] + plot.grid() + for temp in temps: + dcr = model_bat_dcr(temp, 100, bat_params) + pi_ = (dcr * i_ * i_) + sp*p_ + x = p_/1000 + y = pi_/1000 + legs.append(f"temp={temp} °C") + plot.plot(x, y) + plot.xlabel("P_demand [kW]") -plot.show() + plot.title(title) + plot.legend(legs) + plot.ylabel(name) + + plot.show() + +if __name__ == '__main__': + main() diff --git a/jay_diff.py b/jay_diff.py index bfcef0f..033fe12 100644 --- a/jay_diff.py +++ b/jay_diff.py @@ -120,9 +120,7 @@ def jay_merge_full(_old, _diff): return res - -if __name__ == '__main__': - +def main() -> None: result = jay_diff_add({}, {'a': 'hamburger', 'b': 'fries', 'c': {'e': 'coke', 'f': 'coffee'}}) assert result == {'a':'hamburger', 'b':'fries', 'c': {'e': 'coke', 'f': 'coffee'}} @@ -566,3 +564,5 @@ if __name__ == '__main__': result = jay_merge_full(old, {'update':update, 'add':add, 'delete':delete}) assert result == old +if __name__ == '__main__': + main() diff --git a/luftwiderstand.py b/luftwiderstand.py index 8831656..b85a72b 100644 --- a/luftwiderstand.py +++ b/luftwiderstand.py @@ -54,7 +54,6 @@ car_list = [ def rho(temperature_degc: float, p_pa: float =g_p0): return p_pa/(g_RS*(temperature_degc + 273.15)) - # Force : N = kg·m·s2 def fd(rho: float, velocity_kmh: float, c_val: float, area: float): vel = velocity_kmh / 3.6 @@ -62,63 +61,66 @@ def fd(rho: float, velocity_kmh: float, c_val: float, area: float): res = 0.5*rho*vel*vel*cwa return res +def main() -> None: + # Fd vs. velocity, param: temperature + temps_list = [-20, -10, 0, 10, 20, 30] + vel_list = [20, 40, 60, 80, 100, 120, 130, 140, 160] + reference = fd(rho(20), 100, c_val_id3, area_id3) + temps_list_leg = [f"{t}°C" for t in temps_list] -# Fd vs. velocity, param: temperature -temps_list = [-20, -10, 0, 10, 20, 30] -vel_list = [20, 40, 60, 80, 100, 120, 130, 140, 160] -reference = fd(rho(20), 100, c_val_id3, area_id3) -temps_list_leg = [f"{t}°C" for t in temps_list] + plot.figure() + for temp_degc in temps_list: + rho_t = rho(temp_degc) + y_fd = [] + for vel_kmh in vel_list: + fd_t = (fd(rho_t, vel_kmh, c_val_id3, area_id3)/reference - 1) * 100 + y_fd.append(fd_t) -plot.figure() -for temp_degc in temps_list: - rho_t = rho(temp_degc) + plot.plot(vel_list, y_fd) + + plot.title("Windwiderstand vs Geschwindigkeit") + plot.ylabel("Windwiderstand [%]") + plot.xlabel("Geschwindigkeit [km/h]") + plot.legend(temps_list_leg) + plot.grid() + + # Fd vs. velocity, param: car + temp = 20 + vel_list = [20, 40, 60, 80, 100, 120, 130, 140, 160] + car_list_leg = [f"{t['car']}" for t in car_list] + + rho_t = rho(temp) + reference = fd(rho_t, 100, c_val_id3, area_id3) + + plot.figure() + for car in car_list: + y_fd = [] + for vel_kmh in vel_list: + fd_t = (fd(rho_t, vel_kmh, car['cw'], car['area'])/reference - 1) * 100 + y_fd.append(fd_t) + + plot.plot(vel_list, y_fd) + + plot.ylabel("Windwiderstand ID.3 [%]") + plot.xlabel("Geschwindigkeit [km/h]") + plot.title("Windwiderstand vs Geschwindigkeit") + plot.legend(car_list_leg) + plot.grid() + + plot.figure() y_fd = [] for vel_kmh in vel_list: - fd_t = (fd(rho_t, vel_kmh, c_val_id3, area_id3)/reference - 1) * 100 + fd_t = fd(rho_t, vel_kmh, c_val_id3, area_id3) y_fd.append(fd_t) plot.plot(vel_list, y_fd) -plot.title("Windwiderstand vs Geschwindigkeit") -plot.ylabel("Windwiderstand [%]") -plot.xlabel("Geschwindigkeit [km/h]") -plot.legend(temps_list_leg) -plot.grid() + plot.ylabel("Windwiderstand [N]") + plot.xlabel("Geschwindigkeit [km/h]") + plot.title("Windwiderstand vs Geschwindigkeit") + plot.legend(car_list_leg) + plot.grid() + plot.show() -# Fd vs. velocity, param: car -temp = 20 -vel_list = [20, 40, 60, 80, 100, 120, 130, 140, 160] -car_list_leg = [f"{t['car']}" for t in car_list] - -rho_t = rho(temp) -reference = fd(rho_t, 100, c_val_id3, area_id3) - -plot.figure() -for car in car_list: - y_fd = [] - for vel_kmh in vel_list: - fd_t = (fd(rho_t, vel_kmh, car['cw'], car['area'])/reference - 1) * 100 - y_fd.append(fd_t) - - plot.plot(vel_list, y_fd) - -plot.ylabel("Windwiderstand ID.3 [%]") -plot.xlabel("Geschwindigkeit [km/h]") -plot.title("Windwiderstand vs Geschwindigkeit") -plot.legend(car_list_leg) -plot.grid() - -plot.figure() -y_fd = [] -for vel_kmh in vel_list: - fd_t = fd(rho_t, vel_kmh, c_val_id3, area_id3) - y_fd.append(fd_t) - -plot.plot(vel_list, y_fd) - -plot.ylabel("Windwiderstand [N]") -plot.xlabel("Geschwindigkeit [km/h]") -plot.title("Windwiderstand vs Geschwindigkeit") -plot.legend(car_list_leg) -plot.grid() -plot.show() +if __name__ == '__main__': + main() diff --git a/we_collector.py b/we_collector.py index 0f7c363..4847f0e 100644 --- a/we_collector.py +++ b/we_collector.py @@ -23,7 +23,6 @@ domains = [ "measurements" ] - class WeCollector: def __init__(self, user): # Init collecting records @@ -92,7 +91,6 @@ class WeCollector: except Exception as e: print(f"Exception: {e}") - def request_loop(wc: WeCollector): _date_last = None _file_timestamp = None @@ -122,8 +120,7 @@ def request_loop(wc: WeCollector): time_to_wait = 0 time.sleep(time_to_wait) - -if __name__ == '__main__': +def main() -> None: parser = argparse.ArgumentParser(prog="We Collect", description="Collects vehicle data") parser.add_argument("username", help="Username to collect data for (must exist in credentials.py)") parser.add_argument("--loop", help="Execute once or create event loop", type=bool, default=False) @@ -142,3 +139,6 @@ if __name__ == '__main__': print(f"Terminated with Exception {e}") print(f"Terminated Normally") + +if __name__ == '__main__': + main()