-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
484 lines (283 loc) · 13.5 KB
/
utils.py
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import matplotlib
import re
import numpy as np
import casadi as ca
from types import MethodType
from distutils.spawn import find_executable
def var(tag, dim=None):
"Wrapper to create symbolic variables in casadi."
v = ca.MX.sym(tag) if dim is None else \
ca.vcat([ca.MX.sym('{}_{}'.format(tag, i)) for i in range(dim)])
return v
def vecToList(x):
"Convert a vector (numpy.array or casadi.DM) to list."
try:
x = x.full() # convert DM to array
except:
pass # already an array
return x.flatten().tolist()
def vecToNum(x):
"Convert a vector (numpy.array or casadi.DM) with one element to float."
xList = vecToList(x)
if len(xList) > 1:
raise ValueError("Vector should have only one element!")
return xList[0]
class Options():
"Parent class for options of different solvers."
def __init__(self, paramsDict):
self.overwriteDefaults(paramsDict)
self.checkValues()
def checkValues(self):
pass
def checkPositiveInteger(self, num, fieldName, allowZero=True):
if int(num) != num or (not allowZero and not num > 0) or (allowZero and not num >= 0):
raise ValueError("{} must be a {} positive integer!".format(fieldName, 'strictly' if not allowZero else ''))
def checkBounds(self, num, fieldName, lowerBound, upperBound):
if not lowerBound <= num <= upperBound:
raise ValueError("{} must be between {} and {}!".format(fieldName, lowerBound, upperBound))
def overwriteDefaults(self, paramsDict):
"Overwrite default options with user input."
for key in paramsDict:
if not hasattr(self, key):
raise ValueError("Specified option ({}) does not exist!".format(key))
else:
if isinstance(getattr(self, key), Options):
if not isinstance(paramsDict[key], dict):
raise ValueError("Nested options must be specified as a dictionary!")
getattr(self, key).overwriteDefaults(paramsDict[key])
else:
setattr(self, key, paramsDict[key])
def toDict(self):
attributes = [a for a in dir(self) if not a.startswith('__') and type(getattr(self, a)) != MethodType and a != 'ignoreFields']
optsDict = {}
for a in attributes:
optsDict[a] = getattr(self, a) if not isinstance(getattr(self, a), Options) else getattr(self, a).toDict()
return optsDict
class IVP():
"Solve the initial value problem in time domain with CVODES."
def __init__(self, model, lossesFun=None) -> None:
# solver
s = var('s')
v = var('v')
e = var('e') if lossesFun is not None else []
f = var('force')
gd = var('gradient')
cr = var('curvature')
dt = var('dt')
m = var('mass') if lossesFun is not None else []
rollingResistance = model.sr0 + model.sr1*v + model.sr2*v**2
curvatureResistance = ca.if_else(ca.fabs(cr)<=1/300, model.g*0.5*ca.fabs(cr)/(1-30*ca.fabs(cr)),
model.g*0.65*ca.fabs(cr)/(1-55*ca.fabs(cr)))
acceleration = f - rollingResistance - model.g*gd*(1/model.rho) - curvatureResistance*(1/model.rho)
ode = ca.vertcat(dt*v, dt*acceleration, dt*lossesFun(f*m, v)/m if lossesFun is not None else [])
t0, tf = 0, 1
self.fun = ca.integrator('integrator', 'cvodes', {'x':ca.vertcat(s,v,e), 'p':ca.vertcat(f,gd,cr,dt,m), 'ode':ode}, t0, tf,
{'abstol':1e-12, 'reltol':1e-14})
self.withLosses = lossesFun is not None
def solve(self, tf, t0=0, f=0, grd=0, cr=0, v0=0, s0=0, m=None):
if m is None and self.withLosses:
raise ValueError("Need to specify total mass when integrating losses!")
out = self.fun(x0=[s0, v0]+([0] if self.withLosses else []), p=[f, grd, cr, tf-t0] + ([m] if self.withLosses else []))['xf']
sf = vecToNum(out[0])
vf = vecToNum(out[1])
ef = vecToNum(out[2]) if self.withLosses else None
self.t0 = t0
self.tf = tf
self.f = f
self.grd = grd
self.cr = cr
self.s0 = s0
self.sf = sf
self.v0 = v0
self.vf = vf
self.ef = ef
return sf, vf
def simulateCVODES(dfIn, model, totalMass, accumulatedErrors=True):
"Simulate optimal controls with CVODES integrator to compare integration errors."
numIntervals = len(dfIn) - 1
posCVODES = [dfIn['Position [m]'].iloc[0]]
velCVODES = [dfIn['Velocity [m/s]'].iloc[0]]
ivp = IVP(model)
for ii in range(numIntervals):
dt = dfIn.index[ii+1] - dfIn.index[ii]
pos = posCVODES[-1] if accumulatedErrors else dfIn['Position [m]'].iloc[ii]
vel = velCVODES[-1] if accumulatedErrors else dfIn['Velocity [m/s]'].iloc[ii]
posNxt, velNxt = ivp.solve(tf=dt, f=dfIn['Force [N]'].iloc[ii]/totalMass, grd=dfIn['Gradient [permil]'].iloc[ii]/1e3,
cr=dfIn['Curvature [1/m]'].iloc[ii], v0=vel, s0=pos)
posCVODES += [posNxt]
velCVODES += [velNxt]
dfOut = dfIn.copy()
dfOut['Position - cvodes [m]'] = posCVODES
dfOut['Velocity - cvodes [m/s]'] = velCVODES
dfOut['Error position [m]'] = abs(dfOut['Position - cvodes [m]'] - dfOut['Position [m]'])
dfOut['Error velocity [m/s]'] = abs(dfOut['Velocity - cvodes [m/s]'] - dfOut['Velocity [m/s]'])
return dfOut
def splitLosses(fun):
"Split losses function in two parts that are differentiable at zero."
tol = 1e-10
f = var('f')
v = var('v')
jac = ca.Function('jacobian', [f,v], [ca.jacobian(fun(f,v), f)])
def funTr(f,v):
alpha1 = jac(tol, v)
beta1 = fun(0, v)
return fun(f,v)*(f >= 0) + (alpha1*f+beta1)*(f < 0)
def funRgb(f,v):
alpha2 = jac(-tol, v)
beta2 = fun(0, v)
return fun(f,v)*(f < 0) + (alpha2*f+beta2)*(f >= 0)
return funTr, funRgb
def postProcessDataFrame(dfIn, points, train, CVODES=True, integrateLosses=False, integrateRollingResistance=False):
unitScaling = 1e-6/3.6 # Nm -> kWh conversion
totalMass = train.mass*train.rho
dfOut = dfIn.copy()
dfOut['Speed limit [m/s]'] = points['Speed limit [m/s]'].values
dfOut['Gradient [permil]'] = points['Gradient [permil]'].values
dfOut['Curvature [1/m]'] = points['Curvature [1/m]'].values
dfOut['Force (acc) [N]'] = dfOut['Force (el) [N]']*(dfOut['Force (el) [N]'] >= 0)
dfOut['Force (rgb) [N]'] = dfOut['Force (el) [N]']*(dfOut['Force (el) [N]'] < 0)
dfOut['Force [N]'] = dfOut['Force (acc) [N]'] + dfOut['Force (rgb) [N]'] + dfOut['Force (pnb) [N]']
tractionPowerAtBeginning = dfOut['Force (acc) [N]']*dfOut['Velocity [m/s]']/1e3
tractionPowerAtEnd = dfOut['Force (acc) [N]']*dfOut['Velocity [m/s]'].shift(-1)/1e3
regBrakePowerAtBeginning = dfOut['Force (rgb) [N]']*dfOut['Velocity [m/s]']/1e3
regBrakePowerAtEnd = dfOut['Force (rgb) [N]']*dfOut['Velocity [m/s]'].shift(-1)/1e3
dfOut['Max. Power [kW]'] = np.maximum(tractionPowerAtBeginning, tractionPowerAtEnd)
dfOut['Min. Power [kW]'] = np.minimum(regBrakePowerAtBeginning, regBrakePowerAtEnd)
metersPerInterval = dfOut['Position [m]'].diff().shift(-1)
TractiveEnergyWheel = unitScaling*metersPerInterval*dfOut['Force (acc) [N]']
BrakingEnergyWheel = -unitScaling*metersPerInterval*dfOut['Force (rgb) [N]']
specificPowerLosses = train.powerLossesFuns(split=False)
powerLosses = lambda f,v: totalMass*specificPowerLosses(f/totalMass, v)
if not integrateLosses:
dfOut['ds [m]'] = metersPerInterval
dfOut['vm [m/s]'] = (dfOut['Velocity [m/s]'] + dfOut['Velocity [m/s]'].shift(-1))/2
energy = lambda x: unitScaling*x['ds [m]']*powerLosses(x['Force (el) [N]'], x['vm [m/s]'])/x['vm [m/s]']
dfOut['Losses [kWh]'] = dfOut.apply(energy, axis=1)
dfOut.drop(columns=['ds [m]', 'vm [m/s]'], inplace=True)
elif integrateLosses:
from train import TrainIntegrator
trainIntegrator = TrainIntegrator(train.exportModel(), 'RK')
powTr, powRgb = splitLosses(train.powerLosses)
trainIntegrator.initLosses(powTr, powRgb, totalMass, solver='CVODES')
ts = dfOut.index.tolist()
ss = dfOut['Position [m]'].values.tolist()
vs = dfOut['Velocity [m/s]'].values.tolist()
fs = (dfOut['Force (el) [N]']/totalMass).values.tolist()
ps = (dfOut['Force (pnb) [N]']/totalMass).values.tolist()
gs = (dfOut['Gradient [permil]']/1e3).values.tolist()
cr = dfOut['Curvature [1/m]'].values.tolist()
losses = []
for jj in range(len(ts)-1):
dt = ts[jj+1]-ts[jj]
eTr, eRgb = trainIntegrator.calcLosses(vs[jj], dt, fs[jj], ps[jj], gs[jj], cr[jj])
eEl = eTr if fs[jj] >= 0 else eRgb
losses.append(totalMass*vecToNum(eEl))
dfOut['Losses [kWh]'] = np.append(unitScaling*np.array(losses), None)
dfOut['Energy [kWh]'] = TractiveEnergyWheel - BrakingEnergyWheel + dfOut['Losses [kWh]']
dfOut['Energy (pnb) [kWh]'] = -unitScaling*metersPerInterval*dfOut['Force (pnb) [N]']
dfOut['Energy (kin) [kWh]'] = unitScaling*0.5*train.mass*(dfOut['Velocity [m/s]']**2)
if integrateRollingResistance:
from train import TrainIntegrator
trainIntegrator = TrainIntegrator(train.exportModel(), 'RK')
trainIntegrator.initRollingResistance(solver='CVODES')
ss = dfOut['Position [m]'].values.tolist()
vs = dfOut['Velocity [m/s]'].values.tolist()
fs = (dfOut['Force (acc) [N]']/totalMass).values.tolist()
ps = (dfOut['Force (pnb) [N]']/totalMass).values.tolist()
gs = (dfOut['Gradient [permil]']/1e3).values.tolist()
cr = dfOut['Curvature [1/m]'].values.tolist()
rr = []
for jj in range(len(ss)-1):
ds = ss[jj+1]-ss[jj]
loss, _ = trainIntegrator.calcRollingResistance(vs[jj], ds, fs[jj], ps[jj], gs[jj], cr[jj])
rr.append(totalMass*vecToNum(loss))
dfOut['Rolling resistance [kWh]'] = np.append(unitScaling*np.array(rr), None)
rrFun = lambda v: (train.r0 + train.r1*v + train.r2*v**2)/totalMass
crFun = lambda cr: train.g*0.5*abs(cr)/((1-30*abs(cr))*train.rho)*(abs(cr)<=1/300) + train.g*0.65*abs(cr)/((1-55*abs(cr))*train.rho)*(abs(cr)>1/300)
# instantaneous specific forces
rollingResistance = dfOut['Velocity [m/s]'].apply(rrFun)
gradientResistance = train.g*(dfOut['Gradient [permil]']/1000)/train.rho
curvatureResistance = dfOut['Curvature [1/m]'].apply(crFun)
dfOut['Acceleration [m/s^2]'] = dfOut['Force [N]']/totalMass - rollingResistance - gradientResistance - curvatureResistance
if CVODES:
dfOut = simulateCVODES(dfOut, train.exportModel(), totalMass)
return dfOut
def checkTTOBenchVersion(jsonDict, supportedVersions):
if not isinstance(supportedVersions, list) or not all([isinstance(x, str) for x in supportedVersions]):
raise TypeError("'supportedVersions' must be specified a list of strings!")
if 'metadata' not in jsonDict or 'library version' not in jsonDict['metadata']:
raise ValueError("Library version not found in json file!")
else:
pattern = r'v([\d.]+)'
match = re.search(pattern, jsonDict['metadata']['library version'])
if match:
version = match.group(1)
if version not in supportedVersions:
raise ValueError("Import function works only for library versions {}!".format(','.join(supportedVersions)))
else:
raise ValueError("Unexpected format of 'library version' in json file!")
def convertUnit(value, unit):
"""
Convert from any known unit to internally used unit.
"""
if unit in {'m', 'm/s', 'permil', 'kg', 'W', 'N', 'm/s^2', '-', 'N/(m/s)', 'N/(m/s)^2', 'kg/m'}:
valueOut = value
elif unit == 'km':
valueOut = value/1e3
elif unit == 'km/h':
valueOut = value/3.6
elif unit == 't':
valueOut = value*1e3
elif unit == '%':
valueOut = value/100
elif unit == 'kW':
valueOut = value*1e3
elif unit == 'MW':
valueOut = value*1e6
elif unit == 'kN':
valueOut = value*1e3
elif unit == 'kN/(m/s)':
valueOut = value*1e3
elif unit == 'kN/(km/h)':
valueOut = value*1e3
valueOut = valueOut*3.6
elif unit == 'N/(km/h)':
valueOut = value*3.6
elif unit == 'kN/(m/s)^2':
valueOut = value*1e3
elif unit == 'kN/(km/h)^2':
valueOut = value*1e3
valueOut = valueOut*3.6**2
elif unit == 'N/(km/h)^2':
valueOut = value*3.6**2
elif unit == 't/m':
valueOut = value*1e3
else:
raise ValueError("Unknown unit: {}!".format(unit))
return valueOut
def saveFig(fig, axs, filename):
if filename is None:
return
matplotlib.pyplot.savefig(filename, bbox_inches='tight')
def show():
matplotlib.pyplot.show()
def latexify():
if find_executable('latex'):
params = {
"backend": "ps",
"text.latex.preamble": r"\usepackage{gensymb} \usepackage{amsmath}",
"axes.labelsize": 10,
"axes.titlesize": 10,
"legend.fontsize": 10,
"xtick.labelsize": 10,
"ytick.labelsize": 10,
"text.usetex": True,
"font.family": "serif",
}
matplotlib.rcParams.update(params)
latexFound = True
else:
latexFound = False
return latexFound
if __name__ == '__main__':
pass