-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain_laptimesim.py
More file actions
448 lines (358 loc) · 20.7 KB
/
main_laptimesim.py
File metadata and controls
448 lines (358 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import laptimesim
import time
import os
import numpy as np
import matplotlib.pyplot as plt
import pkg_resources
import pickle
"""
author:
Alexander Heilmeier (based on the term thesis of Maximilian Geisslinger)
date:
23.12.2018
.. description::
The file contains the script to run the lap time simulation starting with the import of various parameters and ending
with the visualization of the calculated data.
.. hints:
Input tracks must be unclosed, i.e. last point != first point!
"""
# ----------------------------------------------------------------------------------------------------------------------
# MAIN FUNCTION --------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------
def main(track_opts: dict,
solver_opts: dict,
driver_opts: dict,
sa_opts: dict,
debug_opts: dict) -> laptimesim.src.lap.Lap:
# ------------------------------------------------------------------------------------------------------------------
# CHECK PYTHON DEPENDENCIES ----------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
# get repo path
repo_path = os.path.dirname(os.path.abspath(__file__))
# read dependencies from requirements.txt
requirements_path = os.path.join(repo_path, 'requirements.txt')
dependencies = []
with open(requirements_path, 'r') as fh:
line = fh.readline()
while line:
dependencies.append(line.rstrip())
line = fh.readline()
# check dependencies
pkg_resources.require(dependencies)
# ------------------------------------------------------------------------------------------------------------------
# INITIALIZATION ---------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
output_path = os.path.join(repo_path, "laptimesim", "output")
output_path_velprofile = os.path.join(output_path, "velprofile")
os.makedirs(output_path_velprofile, exist_ok=True)
output_path_testobjects = os.path.join(output_path, "testobjects")
os.makedirs(output_path_testobjects, exist_ok=True)
if debug_opts["use_plot_comparison_tph"]:
output_path_veh_dyn_info = os.path.join(output_path, "veh_dyn_info")
os.makedirs(output_path_veh_dyn_info, exist_ok=True)
# ------------------------------------------------------------------------------------------------------------------
# CREATE TRACK INSTANCE --------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
parfilepath = os.path.join(repo_path, "laptimesim", "input", "tracks", "track_pars.ini")
if not track_opts["use_pit"]: # normal case
trackfilepath = os.path.join(repo_path, "laptimesim", "input", "tracks", "racelines",
track_opts["trackname"] + ".csv")
else: # case pit
trackfilepath = os.path.join(repo_path, "laptimesim", "input", "tracks", "racelines",
track_opts["trackname"] + "_pit.csv")
# set velocity limit
if driver_opts["vel_lim_glob"] is not None:
vel_lim_glob = driver_opts["vel_lim_glob"]
elif solver_opts["series"] == "FE":
vel_lim_glob = 225.0 / 3.6
else:
vel_lim_glob = np.inf
# create instance
track = laptimesim.src.track.Track(pars_track=track_opts,
parfilepath=parfilepath,
trackfilepath=trackfilepath,
vel_lim_glob=vel_lim_glob,
yellow_s1=driver_opts["yellow_s1"],
yellow_s2=driver_opts["yellow_s2"],
yellow_s3=driver_opts["yellow_s3"])
# debug plot
if debug_opts["use_debug_plots"]:
# check if track map exists and set path accordingly
mapfolderpath = os.path.join(repo_path, "laptimesim", "input", "tracks", "maps")
mapfilepath = ""
for mapfile in os.listdir(mapfolderpath):
if track_opts["trackname"] in mapfile:
mapfilepath = os.path.join(mapfolderpath, mapfile)
break
# plot trackmap
track.plot_trackmap(mapfilepath=mapfilepath)
# plot curvature
track.plot_curvature()
# recalculate raceline based on curvature
track.check_track()
# ------------------------------------------------------------------------------------------------------------------
# CREATE CAR INSTANCE ----------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
parfilepath = os.path.join(repo_path, "laptimesim", "input", "vehicles", solver_opts["vehicle"])
# create instance
if solver_opts["series"] == "F1":
car = laptimesim.src.car_hybrid.CarHybrid(parfilepath=parfilepath)
elif solver_opts["series"] == "FE":
car = laptimesim.src.car_electric.CarElectric(parfilepath=parfilepath)
else:
raise IOError("Unknown racing series!")
# debug plot
if debug_opts["use_debug_plots"]:
# plot tire force potential characteristics
car.plot_tire_characteristics()
# plot engine power characteristics
if car.powertrain_type == "combustion":
car.plot_power_engine()
# ------------------------------------------------------------------------------------------------------------------
# CREATE DRIVER INSTANCE -------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
# create instance
driver = laptimesim.src.driver.Driver(carobj=car,
pars_driver=driver_opts,
trackobj=track,
stepsize=track.stepsize)
# ------------------------------------------------------------------------------------------------------------------
# CREATE LAP INSTANCE ----------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
lap = laptimesim.src.lap.Lap(driverobj=driver,
trackobj=track,
pars_solver=solver_opts,
debug_opts=debug_opts)
# ------------------------------------------------------------------------------------------------------------------
# CALL SOLVER ------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
# save start time
t_start = time.perf_counter()
# call simulation
if not sa_opts["use_sa"]:
# normal simulation --------------------------------------------------------------------------------------------
lap.simulate_lap()
# debug plot
if debug_opts["use_debug_plots"]:
# plot torques
lap.plot_torques()
# plot lateral acceleration profile
lap.plot_lat_acc()
# plot tire load profile
lap.plot_tire_loads()
# plot aero forces
lap.plot_aero_forces()
# plot engine speed and gear selection
lap.plot_enginespeed_gears()
else:
# sensitivity analysis -----------------------------------------------------------------------------------------
if debug_opts["use_print"]:
print("INFO: Performing sensitivity analysis!")
# turn debug messages off
lap.pars_solver["print_debug"] = False
# create parameter ranges
sa_range_1 = np.linspace(sa_opts["range_1"][0], sa_opts["range_1"][1], sa_opts["range_1"][2])
# if sa_opts["range_2"] is not None:
# sa_range_2 = np.linspace(sa_opts["range_2"][0], sa_opts["range_2"][1], sa_opts["range_2"][2])
# perform analysis
if sa_opts["sa_type"] == "mass":
sa_t_lap = np.zeros(sa_opts["range_1"][2])
sa_fuel_cons = np.zeros(sa_opts["range_1"][2])
for i, cur_mass in enumerate(sa_range_1):
print("SA: Starting solver run (%i)" % (i + 1))
# change mass of vehicle
lap.driverobj.carobj.pars_general["m"] = cur_mass
# simulate lap and save lap time
lap.simulate_lap()
sa_t_lap[i] = lap.t_cl[-1]
sa_fuel_cons[i] = lap.fuel_cons_cl[-1]
# reset lap
lap.reset_lap()
print("SA: Finished solver run (%i)" % (i + 1))
else:
sa_t_lap = np.zeros((sa_opts["range_1"][2], sa_opts["range_2"][2]))
# TODO: implementation of COG and aero variation missing
# ------------------------------------------------------------------------------------------------------------------
# EXPORT -----------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
# print to command window
if not sa_opts["use_sa"] and debug_opts["use_print_result"]:
print("-" * 50)
print("Forward/Backward Plus Solver")
print("Solver runtime: %.2f s" % (time.perf_counter() - t_start))
print("-" * 50)
print("Lap time: %.3f s" % lap.t_cl[-1])
print("S1: %.3f s | S2: %.3f s | S3: %.3f s" %
(lap.t_cl[track.zone_inds["s12"]],
lap.t_cl[track.zone_inds["s23"]] - lap.t_cl[track.zone_inds["s12"]],
lap.t_cl[-1] - lap.t_cl[track.zone_inds["s23"]]))
print("-" * 50)
v_tmp = lap.vel_cl[0] * 3.6
print("Start velocity: %.1f km/h" % v_tmp)
v_tmp = lap.vel_cl[-1] * 3.6
print("Final velocity: %.1f km/h" % v_tmp)
v_tmp = (lap.vel_cl[0] - lap.vel_cl[-1]) * 3.6
print("Delta: %.1f km/h" % v_tmp)
print("-" * 50)
print("Consumption: %.2f kg/lap | %.2f kJ/lap" % (lap.fuel_cons_cl[-1], lap.e_cons_cl[-1] / 1000.0))
# [J] -> [kJ]
print("-" * 50)
elif debug_opts["use_print_result"]:
print("-" * 50)
print("Forward/Backward Plus Solver")
print("Runtime for sensitivity analysis: %.2f s" % (time.perf_counter() - t_start))
print("-" * 50)
if sa_opts["sa_type"] == "mass":
m_diff = sa_range_1[-1] - sa_range_1[0]
t_lap_diff = sa_t_lap[-1] - sa_t_lap[0]
fuel_cons_diff = sa_fuel_cons[-1] - sa_fuel_cons[0]
print("Average sensitivity of lap time to mass: %.3f s/kg" % (t_lap_diff / m_diff))
print("Average sensitivity of fuel consumption to mass: %.5f kg/kg" % (fuel_cons_diff / m_diff))
print("-" * 50)
else:
pass
# TODO: implementation of COG and aero variation missing
# write velocity profile output
output_path = os.path.join(output_path_velprofile, "velprofile_" + track_opts["trackname"].lower() + ".csv")
tmp_data = np.column_stack((lap.trackobj.dists_cl[:-1], lap.vel_cl[:-1]))
with open(output_path, "wb") as fh:
np.savetxt(fh, tmp_data, fmt="%.5f,%.5f", header="distance_m,vel_mps")
# ------------------------------------------------------------------------------------------------------------------
# PLOTS ------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
if not sa_opts["use_sa"]:
if debug_opts["use_plot"]:
lap.plot_overview()
# lap.plot_revs_gears()
else:
if sa_opts["sa_type"] == "mass":
# lap time
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(sa_range_1, sa_t_lap, "x")
ax.set_xlim(sa_range_1[0], sa_range_1[-1])
ax.set_ylim(sa_t_lap[0], sa_t_lap[-1])
ax.set_title("SA of lap time to mass")
ax.set_xlabel("mass m in kg")
ax.set_ylabel("lap time t in s")
plt.grid()
plt.show()
# fuel consumption
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(sa_range_1, sa_fuel_cons, "x")
ax.set_xlim(sa_range_1[0], sa_range_1[-1])
ax.set_ylim(sa_fuel_cons[0], sa_fuel_cons[-1])
ax.set_title("SA of fuel consumption to mass")
ax.set_xlabel("mass m in kg")
ax.set_ylabel("fuel consumption in kg/lap")
plt.grid()
plt.show()
# ------------------------------------------------------------------------------------------------------------------
# CI TESTING -------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
# pickle lap object for possible CI testing
result_objects_file_path = os.path.join(output_path_testobjects,
"testobj_laptimesim_" + track_opts["trackname"] + ".pkl")
with open(result_objects_file_path, 'wb') as fh:
pickle.dump(lap, fh)
return lap # return required in case of CI testing
# ----------------------------------------------------------------------------------------------------------------------
# MAIN FUNCTION CALL ---------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
# ------------------------------------------------------------------------------------------------------------------
# USER INPUT -------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
# F1 qualifying mode: DRS activated, EM strategy FCFB, initial_energy 4.0MJ, full power of 575kW
# F1 race mode: DRS as desired, EM strategy LBP, initial_energy 0.0MJ, power reduced to 546kW (-5%)
# FE qualifying mode: DRS deactivated, EM strategy FCFB
# FE race mode: DRS deactivated, EM strategy FCFB + lift&coast
# tracks must be unclosed, i.e. last point != first point!
# track options ----------------------------------------------------------------------------------------------------
# trackname: track name of desired race track (file must be available in the input folder)
# flip_track: switch to flip track direction if required
# mu_weather: [-] factor to consider wet track, e.g. by mu_weather = 0.6
# interp_stepsize_des: [m], desired stepsize after interpolation of the input raceline points
# curv_filt_width: [m] window width of moving average filter -> set None for deactivation
# use_drs1: DRS zone 1 switch
# use_drs2: DRS zone 2 switch
# use_pit: activate pit stop (requires _pit track file!)
track_opts_ = {"trackname": "Shanghai",
"flip_track": False,
"mu_weather": 1.0,
"interp_stepsize_des": 5.0,
"curv_filt_width": 10.0,
"use_drs1": True,
"use_drs2": True,
"use_pit": False}
# solver options ---------------------------------------------------------------------------------------------------
# vehicle: vehicle parameter file
# series: F1, FE
# limit_braking_weak_side: can be None, 'FA', 'RA', 'all' -> set if brake force potential should be determined
# based on the weak (i.e. inner) side of the car, e.g. when braking into a corner
# v_start: [m/s] velocity at start
# find_v_start: determine the real velocity at start
# max_no_em_iters: maximum number of iterations for EM recalculation
# es_diff_max: [J] stop criterion -> maximum difference between two solver runs
solver_opts_ = {"vehicle": "F1_Shanghai.ini",
"series": "F1",
"limit_braking_weak_side": 'FA',
"v_start": 100.0 / 3.6,
"find_v_start": True,
"max_no_em_iters": 5,
"es_diff_max": 1.0}
# driver options ---------------------------------------------------------------------------------------------------
# vel_subtr_corner: [m/s] velocity subtracted from max. cornering vel. since drivers will not hit the maximum
# perfectly
# vel_lim_glob: [m/s] velocity limit, set None if unused
# yellow_s1: yellow flag in sector 1
# yellow_s2: yellow flag in sector 2
# yellow_s3: yellow flag in sector 3
# yellow_throttle: throttle position in a yellow flag sector
# initial_energy: [J] initial energy (F1: max. 4 MJ/lap, FE Berlin: 4.58 MJ/lap)
# em_strategy: FCFB, LBP, LS, NONE -> FCFB = First Come First Boost, LBP = Longest (time) to Breakpoint,
# LS = Lowest Speed, FE requires FCFB as it only drives in electric mode!
# use_recuperation: set if recuperation by e-motor and electric turbocharger is allowed or not (lift&coast is
# currently only considered with FCFB)
# use_lift_coast: switch to turn lift and coast on/off
# lift_coast_dist: [m] lift and coast before braking point
driver_opts_ = {"vel_subtr_corner": 0.5,
"vel_lim_glob": None,
"yellow_s1": False,
"yellow_s2": False,
"yellow_s3": False,
"yellow_throttle": 0.3,
"initial_energy": 4.0e6,
"em_strategy": "FCFB",
"use_recuperation": True,
"use_lift_coast": False,
"lift_coast_dist": 10.0}
# sensitivity analysis options -------------------------------------------------------------------------------------
# use_sa: switch to deactivate sensitivity analysis
# sa_type: 'mass', 'aero', 'cog'
# range_1: range of parameter variation [start, end, number of steps]
# range_2: range of parameter variation [start, end, number of steps] -> CURRENTLY NOT IMPLEMENTED
sa_opts_ = {"use_sa": False,
"sa_type": "mass",
"range_1": [733.0, 833.0, 5],
"range_2": None}
# debug options ----------------------------------------------------------------------------------------------------
# use_plot: plot results
# use_debug_plots: plot additional plots for debugging
# use_plot_comparison_tph: calculate velocity profile with TPH FB solver and plot a comparison
# use_print: set if prints to console should be used or not (does not suppress hints/warnings)
# use_print_result: set if result should be printed to console or not
debug_opts_ = {"use_plot": False,
"use_debug_plots": False,
"use_plot_comparison_tph": False,
"use_print": True,
"use_print_result": True}
# ------------------------------------------------------------------------------------------------------------------
# SIMULATION CALL --------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
main(track_opts=track_opts_,
solver_opts=solver_opts_,
driver_opts=driver_opts_,
sa_opts=sa_opts_,
debug_opts=debug_opts_)