/
performance_modeling.py
255 lines (195 loc) · 5.94 KB
/
performance_modeling.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
"""Modular aircraft concept"""
import pickle
import numpy as np
from gpkit import Model, Vectorize, parse_variables
class AircraftP(Model):
"""Aircraft flight physics: weight <= lift, fuel burn
Variables
---------
Wfuel [lbf] fuel weight
Wburn [lbf] segment fuel burn
Upper Unbounded
---------------
Wburn, aircraft.wing.c, aircraft.wing.A
Lower Unbounded
---------------
Wfuel, aircraft.W, state.mu
"""
@parse_variables(__doc__, globals())
def setup(self, aircraft, state):
self.aircraft = aircraft
self.state = state
self.wing_aero = aircraft.wing.dynamic(aircraft.wing, state)
self.perf_models = [self.wing_aero]
W = aircraft.W
S = aircraft.wing.S
V = state.V
rho = state.rho
D = self.wing_aero.D
CL = self.wing_aero.CL
return Wburn >= 0.1*D, W + Wfuel <= 0.5*rho*CL*S*V**2, {
"performance":
self.perf_models}
class Aircraft(Model):
"""The vehicle model
Variables
---------
W [lbf] weight
Upper Unbounded
---------------
W
Lower Unbounded
---------------
wing.c, wing.S
"""
@parse_variables(__doc__, globals())
def setup(self):
self.fuse = Fuselage()
self.wing = Wing()
self.components = [self.fuse, self.wing]
return [W >= sum(c.W for c in self.components),
self.components]
dynamic = AircraftP
class FlightState(Model):
"""Context for evaluating flight physics
Variables
---------
V 40 [knots] true airspeed
mu 1.628e-5 [N*s/m^2] dynamic viscosity
rho 0.74 [kg/m^3] air density
"""
@parse_variables(__doc__, globals())
def setup(self):
pass
class FlightSegment(Model):
"""Combines a context (flight state) and a component (the aircraft)
Upper Unbounded
---------------
Wburn, aircraft.wing.c, aircraft.wing.A
Lower Unbounded
---------------
Wfuel, aircraft.W
"""
def setup(self, aircraft):
self.aircraft = aircraft
self.flightstate = FlightState()
self.aircraftp = aircraft.dynamic(aircraft, self.flightstate)
self.Wburn = self.aircraftp.Wburn
self.Wfuel = self.aircraftp.Wfuel
return {"aircraft performance": self.aircraftp,
"flightstate": self.flightstate}
class Mission(Model):
"""A sequence of flight segments
Upper Unbounded
---------------
aircraft.wing.c, aircraft.wing.A
Lower Unbounded
---------------
aircraft.W
"""
def setup(self, aircraft):
self.aircraft = aircraft
with Vectorize(4): # four flight segments
self.fs = FlightSegment(aircraft)
Wburn = self.fs.aircraftp.Wburn
Wfuel = self.fs.aircraftp.Wfuel
self.takeoff_fuel = Wfuel[0]
return {
"fuel constraints":
[Wfuel[:-1] >= Wfuel[1:] + Wburn[:-1],
Wfuel[-1] >= Wburn[-1]],
"flight segment":
self.fs}
class WingAero(Model):
"""Wing aerodynamics
Variables
---------
CD [-] drag coefficient
CL [-] lift coefficient
e 0.9 [-] Oswald efficiency
Re [-] Reynold's number
D [lbf] drag force
Upper Unbounded
---------------
D, Re, wing.A, state.mu
Lower Unbounded
---------------
CL, wing.S, state.mu, state.rho, state.V
"""
@parse_variables(__doc__, globals())
def setup(self, wing, state):
self.wing = wing
self.state = state
c = wing.c
A = wing.A
S = wing.S
rho = state.rho
V = state.V
mu = state.mu
return [D >= 0.5*rho*V**2*CD*S,
Re == rho*V*c/mu,
CD >= 0.074/Re**0.2 + CL**2/np.pi/A/e]
class Wing(Model):
"""Aircraft wing model
Variables
---------
W [lbf] weight
S [ft^2] surface area
rho 1 [lbf/ft^2] areal density
A 27 [-] aspect ratio
c [ft] mean chord
Upper Unbounded
---------------
W
Lower Unbounded
---------------
c, S
"""
@parse_variables(__doc__, globals())
def setup(self):
return [c == (S/A)**0.5,
W >= S*rho]
dynamic = WingAero
class Fuselage(Model):
"""The thing that carries the fuel, engine, and payload
A full model is left as an exercise for the reader.
Variables
---------
W 100 [lbf] weight
"""
@parse_variables(__doc__, globals())
def setup(self):
pass
AC = Aircraft()
MISSION = Mission(AC)
M = Model(MISSION.takeoff_fuel, [MISSION, AC])
print(M)
sol = M.solve(verbosity=0)
# save solution to some files
sol.savemat()
sol.savecsv()
sol.savetxt()
sol.save("solution.pkl")
# retrieve solution from a file
sol_loaded = pickle.load(open("solution.pkl", "rb"))
vars_of_interest = set(AC.varkeys)
# note that there's two ways to access submodels
assert (MISSION["flight segment"]["aircraft performance"]
is MISSION.fs.aircraftp)
vars_of_interest.update(MISSION.fs.aircraftp.unique_varkeys)
vars_of_interest.add(M["D"])
print(sol.summary(vars_of_interest))
print(sol.table(tables=["loose constraints"]))
M.append(MISSION.fs.aircraftp.Wburn >= 0.2*MISSION.fs.aircraftp.wing_aero.D)
sol = M.solve(verbosity=0)
print(sol.diff("solution.pkl", showvars=vars_of_interest, sortbymodel=False))
try:
from gpkit.interactive.sankey import Sankey
variablesankey = Sankey(sol, M).diagram(AC.wing.A)
sankey = Sankey(sol, M).diagram(width=1200, height=400, maxlinks=30)
# the line below shows an interactive graph if run in jupyter notebook
sankey # pylint: disable=pointless-statement
except (ImportError, ModuleNotFoundError):
print("Making Sankey diagrams requires the ipysankeywidget package")
from gpkit.interactive.references import referencesplot
referencesplot(M, openimmediately=False)