-
Notifications
You must be signed in to change notification settings - Fork 3
/
TrackMain.py
433 lines (348 loc) · 16.8 KB
/
TrackMain.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 13 11:08:41 2016
@author: MaxHenger
"""
import TrackBounds
import TrackSettings
import TrackCommon
import TrackStitching
import TrackClimbOptimize
import TrackLookup
import TrackStorage
import TrackPower
import AircraftBatteries
import Atmosphere
import TimeEstimator
import numpy as np
import os.path as os_p
import scipy.integrate as scp_int
import matplotlib.pyplot as plt
def Main():
filename = 'final.csv'
RESULT_PENDING = 0
RESULT_DONE = 1
RESULT_ERROR = 2
# Determine track operational points
axisHeight = np.linspace(30e3, 80e3, 75)
axisDeltaV = np.linspace(-100, 50, 100)
axisSeverity = np.linspace(-2.5, 2.5, 75)
settings = TrackSettings.Settings()
atmosphere = Atmosphere.Atmosphere()
powerEstimator = TrackPower.TrackPower(settings, atmosphere)
PReqMin = 10
PReq = 32e3
dt = 0.25
climbLowerBoundOffset = 0.0
# Check if an operational regions file was created before
severity = []
upperHeight = []
upperDeltaV = []
lowerHeight = []
lowerDeltaV = []
result = []
simulationFilename = []
areaTop = []
batteryCapacity = []
batteryWeight = []
toProcess = 0
fileHeader = 'severity; upper height [m]; upper deltaV [m/s]; lower height [m]; ' + \
'lower deltaV [m/s]; success; filename; areaTop [m2]; ' + \
'capacity [J]; battery mass [kg]\n'
if os_p.isfile(filename):
# Read csv file
fh = open(filename, 'r')
line = fh.readline() # first line is the header
line = fh.readline()
while len(line) != 0:
# Process the current line
seperated = line.split(';')
for iSeperated in range(0, len(seperated)):
seperated[iSeperated] = seperated[iSeperated].strip()
severity.append(float(seperated[0]))
upperHeight.append(float(seperated[1]))
upperDeltaV.append(float(seperated[2]))
lowerHeight.append(float(seperated[3]))
lowerDeltaV.append(float(seperated[4]))
result.append(int(seperated[5]))
simulationFilename.append(seperated[6])
areaTop.append(seperated[7])
batteryCapacity.append(seperated[8])
batteryWeight.append(seperated[9])
if result[-1] == RESULT_PENDING:
toProcess += 1
line = fh.readline()
fh.close()
else:
# No file existed yet
operationalRegions = TrackBounds.DetermineTracks(axisHeight, axisDeltaV,
settings.latitude, settings.longitude, PReq, axisSeverity)
print(' > Found', len(operationalRegions), 'operational regions')
fh = open('final.csv', 'w')
fh.write(fileHeader)
for iRegion in range(0, len(operationalRegions)):
# Loop through all regionss
curOperationalRegion = operationalRegions[iRegion]
curSeverity = curOperationalRegion[0]
curUpperDeltaV = curOperationalRegion[1]
curUpperHeight = curOperationalRegion[2]
curLowerDeltaV = curOperationalRegion[3]
curLowerHeight = curOperationalRegion[4]
# Create permutations of all possible height/speed combinations
for iUpper in range(0, len(curUpperDeltaV)):
for iLower in range(0, len(curLowerDeltaV)):
if curUpperHeight[iUpper] > curLowerHeight[iLower]:
severity.append(curSeverity)
upperHeight.append(curUpperHeight[iUpper])
upperDeltaV.append(curUpperDeltaV[iUpper])
lowerHeight.append(curLowerHeight[iLower])
lowerDeltaV.append(curLowerDeltaV[iLower])
result.append(RESULT_PENDING)
simulationFilename.append('')
areaTop.append(0)
batteryCapacity.append(0)
batteryWeight.append(0)
toProcess += 1
fh.write(str(curSeverity) + "; ")
fh.write(str(curUpperHeight[iUpper]) + "; ")
fh.write(str(curUpperDeltaV[iUpper]) + "; ")
fh.write(str(curLowerHeight[iLower]) + "; ")
fh.write(str(curLowerDeltaV[iLower]) + "; ")
fh.write(str(RESULT_PENDING) + "; ")
fh.write("nope.txt; ")
fh.write("0.0; ")
fh.write("0.0; ")
fh.write("0.0\n")
fh.close()
print(' >', toProcess, 'permutations left to process')
ascentMaps = {}
for iPermutation in range(0, len(severity)):
# Check if the current solution wasn't already processed
if result[iPermutation] != RESULT_PENDING:
print('... skipping')
continue
# Check if an ascent map should be constructed
lowerGuide = None
if severity[iPermutation] in ascentMaps:
# Already exists
lowerGuide = ascentMaps[severity[iPermutation]]
else:
# Ascent guide does not exist yet
print(TrackCommon.StringHeader("Determining ascent guides", 60))
print(TrackCommon.StringPad(" > Severity:", severity[iPermutation], 2, 6))
ascentGuide = TrackClimbOptimize.GenerateAscentMaps(axisHeight,
axisDeltaV, settings.W, settings.S, settings.inclination,
settings.lookupCl, settings.lookupCd, atmosphere, settings.qInfMin,
settings.qInfMax, settings.alphaMin, settings.alphaMax, PReqMin, PReq,
settings.latitude, settings.longitude, settings.reynoldsLength,
severity=severity[iPermutation])
ascentDeltaV = ascentGuide['pathMinDeltaV'] + climbLowerBoundOffset * \
(ascentGuide['pathMaxDeltaV'] - ascentGuide['pathMinDeltaV'])
lowerGuide = TrackLookup.Lookup1D(ascentGuide['pathHeight'],
ascentDeltaV)
ascentMaps[severity[iPermutation]] = lowerGuide
settings.lowerBound = lowerGuide
settings.upperBound = None
# Start processing data
print(TrackCommon.StringHeader("Simulating response", 60))
print(TrackCommon.StringPad("severity: ", severity[iPermutation], 3, 6))
print(TrackCommon.StringPad("upper height:", upperHeight[iPermutation] / 1e3, 2, 6), "km")
print(TrackCommon.StringPad("upper deltaV:", upperDeltaV[iPermutation], 2, 6), "m/s")
print(TrackCommon.StringPad("lower height:", lowerHeight[iPermutation] / 1e3, 2, 6), "km")
print(TrackCommon.StringPad("lower deltaV:", lowerDeltaV[iPermutation], 2, 6), "m/s")
try:
# Attempt to simulate track. It will throw an exception if
# somehow the solution cannot be simulated or will not
# converge
stitched = TrackStitching.StitchTracks(
upperHeight[iPermutation], upperDeltaV[iPermutation],
lowerHeight[iPermutation], lowerDeltaV[iPermutation], 10,
lowerDeltaV[iPermutation], upperDeltaV[iPermutation], PReqMin,
PReq, dt, settings, severity[iPermutation], plotResult=False,
saveResult=True)
result[iPermutation] = RESULT_DONE
simulationFilename[iPermutation] = stitched
# Very backwards, I know. But I don't have the time nor the energy
# and certainly not the motivation, at the moment, to do this
# properly. And I have an SSD. Muhahaha
loader = TrackStorage.DataStorage()
loader.load(stitched)
time = loader.getVariable('timeTotal').getValues()
height = loader.getVariable('heightTotal').getValues()
alpha = loader.getVariable('alphaTotal').getValues()
gamma = loader.getVariable('gammaTotal').getValues()
power = loader.getVariable('powerTotal').getValues()
latitude = loader.getVariable('latitude').getValues()
longitude = loader.getVariable('longitude').getValues()
PRequired = loader.getVariable('PReqMax').getValues()
# Determine area and capacity
area, capacity = TrackStitching.DetermineArea(time, height, alpha,
gamma, power, latitude, longitude, np.asarray([2.0]), PRequired,
powerEstimator, settings, plotResults=False)
batWeight, _ = AircraftBatteries.AircraftBatterySizing(
capacity[0] / 3600, settings.batteryDepthOfDischarge,
settings.batterySafetyFactor)
areaTop[iPermutation] = area[0]
batteryCapacity[iPermutation] = capacity[0]
batteryWeight[iPermutation] = batWeight
except Exception as ex:
print(TrackCommon.StringHeader("ERROR: " + str(ex), 60, '!', '!', '!'))
result[iPermutation] = RESULT_ERROR
# Very inefficient. But the following means I can ctrl+c out of the
# script at any time I want without issues with having to rerun
# everything. Neat isn't it... Hacky and neat.
print(TrackCommon.StringHeader("WRITING FILE, DO NOT CTRL+C", 60, '!', '!', '!'))
fh = open(filename, 'w')
fh.write(fileHeader)
for i in range(0, len(severity)):
fh.write(str(severity[i]) + "; ")
fh.write(str(upperHeight[i]) + "; ")
fh.write(str(upperDeltaV[i]) + "; ")
fh.write(str(lowerHeight[i]) + "; ")
fh.write(str(lowerDeltaV[i]) + "; ")
fh.write(str(result[i]) + "; ")
fh.write(simulationFilename[i] + "; ")
fh.write(str(areaTop[i]) + "; ")
fh.write(str(batteryCapacity[i]) + "; ")
fh.write(str(batteryWeight[i]) + "\n")
fh.close()
# Save all final results to a csv file
fh.close()
def PostProcess(filenames):
settings = TrackSettings.Settings()
atmosphere = Atmosphere.Atmosphere()
for filename in filenames:
file = TrackStorage.DataStorage()
file.load(filename)
powerEstimator = TrackPower.TrackPower(settings, atmosphere)
time = file.getVariable('timeTotal').getValues()
height = file.getVariable('heightTotal').getValues()
alpha = file.getVariable('alphaTotal').getValues()
gamma = file.getVariable('gammaTotal').getValues()
power = file.getVariable('powerTotal').getValues()
latitude = file.getVariable('latitude').getValues()
longitude = file.getVariable('longitude').getValues()
PRequired = file.getVariable('PReqMax').getValues()
area, capacity = TrackStitching.DetermineArea(time, height, alpha,
gamma, power, latitude, longitude, np.asarray([2.0]), PRequired,
powerEstimator, settings, plotResults=False)
area = area[0]
capacity = capacity[0]
print(' * For file:', filename)
print(TrackCommon.StringPad(" > Atop: ", area, 2, 6) + " m2")
print(TrackCommon.StringPad(" > Capacity: ", capacity / 1e6, 2, 6) + " MJ")
batWeight, batVolume = AircraftBatteries.AircraftBatterySizing(
capacity / 3600, settings.batteryDepthOfDischarge,
settings.batterySafetyFactor)
solarPanelWeight = 2.0 * area * settings.specificWeightPanels
print(TrackCommon.StringPad(" > mBattery: ", batWeight, 2, 6) + " kg")
print(TrackCommon.StringPad(" > mSolar: ", solarPanelWeight, 2, 6) + " kg")
def PostBatteryAnalysis(filename):
file = TrackStorage.DataStorage()
file.load(filename)
numArea = 500
numCapacities = 500
time = file.getVariable('timeTotal').getValues()
height = file.getVariable('heightTotal').getValues()
alpha = file.getVariable('alphaTotal').getValues()
gamma = file.getVariable('gammaTotal').getValues()
power = file.getVariable('powerTotal').getValues()
latitude = file.getVariable('latitude').getValues()
longitude = file.getVariable('longitude').getValues()
atmosphere = Atmosphere.Atmosphere()
settings = TrackSettings.Settings()
powerEstimator = TrackPower.TrackPower(settings, atmosphere)
numSubUpdate = 10
numUpdate = 50
estimator = TimeEstimator.TimeEstimator(numArea)
A = np.linspace(5, 40, numArea)
Cmin = np.zeros(A.shape)
Cmax = np.zeros(A.shape)
# Precalculate constants related to efficiencies
effDir = np.zeros(time.shape)
effInd = np.zeros(time.shape)
effUni = np.zeros(time.shape)
for i in range(0, len(time)):
effDir[i], effInd[i], effUni[i] = powerEstimator.getPowerEfficiency(
height[i], latitude, longitude, alpha[i] / 180.0 * np.pi, gamma[i])
estimator.startTiming()
for iArea in range(0, len(A)):
estimator.startIteration(iArea)
isExcessArray = np.empty([len(time)], dtype=np.bool)
isExcessArray.fill(False)
curRequired = power / settings.efficiencyPropellers + settings.PConsumption
curAvailable = 2 * A[iArea] * settings.fluxVenus * (effDir + effInd + 2.0 * effUni)
# Turn the boolean list into a set of ranges
isExcess = curAvailable[0] > curRequired[0]
iOld = 0
rangesExcess = []
rangesLack = []
for i in range(1, len(time)):
if curAvailable[i] > curRequired[i]:
if not isExcess:
# Current available is larger than required but was
# previously tracking a non-excess region
rangesLack.append([iOld, i])
isExcess = True
iOld = i
else:
if isExcess:
# Currently lacking power but was tracking a charging
# region
rangesExcess.append([iOld, i])
isExcess = False
iOld = i
# Add the last region
if isExcess:
rangesExcess.append([iOld, len(time)])
else:
rangesLack.append([iOld, len(time)])
# Perform the intergral calculations
upIntRequired = 0
upIntAvailable = 0
lowIntRequired = 0
lowIntAvailable = 0
for region in rangesExcess:
upIntRequired += scp_int.simps(curRequired[region[0]:region[1]], time[region[0]:region[1]])
upIntAvailable += scp_int.simps(curAvailable[region[0]:region[1]], time[region[0]:region[1]])
for region in rangesLack:
lowIntRequired += scp_int.simps(curRequired[region[0]:region[1]], time[region[0]:region[1]])
lowIntAvailable += scp_int.simps(curAvailable[region[0]:region[1]], time[region[0]:region[1]])
Cmax[iArea] = - settings.efficiencyCharging / settings.efficiencyPower * \
upIntRequired + settings.efficiencyCharging * upIntAvailable
Cmin[iArea] = 1 / (settings.efficiencyPower * settings.efficiencyCharging) * \
lowIntRequired - lowIntAvailable / settings.efficiencyCharging
estimator.finishedIteration(iArea)
if (iArea + 1) % numSubUpdate == 0:
print('.', end='')
if (iArea + 1) % numUpdate == 0:
print(' spent:', estimator.getTotalElapsed(),
', remaining:', estimator.getEstimatedRemaining())
totalWeight = np.zeros([numCapacities, len(A)])
capacities = np.linspace(np.min(Cmin), np.max(Cmax), numCapacities)
for iArea in range(0, len(A)):
for iVert in range(0, numCapacities):
totalWeight[iVert, iArea], _ = AircraftBatteries.AircraftBatterySizing(
capacities[iVert] / 3600, settings.batteryDepthOfDischarge,
settings.batterySafetyFactor)
totalWeight[iVert, iArea] += A[iArea] * 2.0 * settings.specificWeightPanels * \
settings.efficiencyPackingFactor
# Put all excessive capacities to the maximum value
maxWeight = np.max(totalWeight)
for iArea in range(0, len(A)):
for iVert in range(0, numCapacities):
if capacities[iVert] < Cmin[iArea] or capacities[iVert] > Cmax[iArea]:
totalWeight[iVert, iArea] = maxWeight
boundsData, boundsLegend = TrackCommon.ImageAxes(0, 1, 0, 1)
fig = plt.figure()
ax = fig.add_axes(boundsData)
axLegend = fig.add_axes(boundsLegend)
TrackCommon.PlotImage(fig, ax, axLegend, A, r'$A_\mathrm{top} \; [m^2]$',
capacities / 1e6, r'$C \; [MJ]$', totalWeight, r'$m_\mathrm{power} \; [kg]$')
ax.plot(A, Cmax / 1e6, 'r', label=r'$C_\mathrm{max}$')
ax.plot(A, Cmin / 1e6, 'g', label=r'$C_\mathrm{min}$')
ax.legend()
ax.grid(True)
#Main()
PostBatteryAnalysis('stitched_66076.9at5.0to50000.0at-22.2.dat')