-
Notifications
You must be signed in to change notification settings - Fork 1
/
functionals.py
526 lines (416 loc) · 17.1 KB
/
functionals.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
from __future__ import division, print_function
import numpy as np
from openmdao.api import Component, Group
try:
import OAS_API
fortran_flag = True
data_type = float
except:
fortran_flag = False
data_type = complex
class FunctionalBreguetRange(Component):
"""
Computes the fuel burn using the Breguet range equation using
the computed CL, CD, weight, and provided specific fuel consumption, speed of sound,
Mach number, initial weight, and range.
Note that we add information from each lifting surface.
Parameters
----------
CL : float
Total coefficient of lift (CL) for the lifting surface.
CD : float
Total coefficient of drag (CD) for the lifting surface.
weight : float
Total weight of the structural spar.
Returns
-------
fuelburn : float
Computed fuel burn in kg based on the Breguet range equation.
"""
def __init__(self, surfaces, prob_dict):
super(FunctionalBreguetRange, self).__init__()
self.surfaces = surfaces
self.prob_dict = prob_dict
for surface in surfaces:
name = surface['name']
self.add_param(name+'structural_weight', val=0.)
self.add_param('CL', val=0.)
self.add_param('CD', val=0.)
self.add_output('fuelburn', val=0.)
self.add_output('weighted_obj', val=0.)
self.deriv_options['type'] = 'cs'
self.deriv_options['form'] = 'central'
def solve_nonlinear(self, params, unknowns, resids):
CT = self.prob_dict['CT']
a = self.prob_dict['a']
R = self.prob_dict['R']
M = self.prob_dict['M']
W0 = self.prob_dict['W0'] * self.prob_dict['g']
beta = self.prob_dict['beta']
# Loop through the surfaces and add up the structural weights
# to get the total structural weight.
Ws = 0.
for surface in self.surfaces:
name = surface['name']
Ws += params[name+'structural_weight']
CL = params['CL']
CD = params['CD']
fuelburn = np.sum((W0 + Ws) * (np.exp(R * CT / a / M * CD / CL) - 1))
# Convert fuelburn from N to kg
unknowns['fuelburn'] = fuelburn / self.prob_dict['g']
# This lines makes the 'weight' the total aircraft weight
unknowns['weighted_obj'] = (beta * fuelburn + (1 - beta) * (W0 + Ws + fuelburn)) / self.prob_dict['g']
# Whereas this line only considers the structural weight
# unknowns['weighted_obj'] = (beta * fuelburn + (1 - beta) * Ws) / self.prob_dict['g']
class FunctionalEquilibrium(Component):
"""
Lift = weight constraint.
Note that we add information from each lifting surface.
Parameters
----------
L : float
Total lift for the lifting surface.
structural_weight : float
Total weight of the structural spar.
fuelburn : float
Computed fuel burn in kg based on the Breguet range equation.
Returns
-------
L_equals_W : float
Equality constraint for lift = total weight. L_equals_W = 0 for the constraint to be satisfied.
total_weight : float
Total weight of the entire aircraft, including W0, all structural weights,
and fuel.
"""
def __init__(self, surfaces, prob_dict):
super(FunctionalEquilibrium, self).__init__()
self.surfaces = surfaces
self.prob_dict = prob_dict
for surface in surfaces:
name = surface['name']
self.add_param(name+'L', val=0.)
self.add_param(name+'structural_weight', val=0.)
self.add_param('fuelburn', val=0.)
self.add_output('L_equals_W', val=0.)
self.add_output('total_weight', val=0.)
self.deriv_options['type'] = 'cs'
self.deriv_options['form'] = 'central'
def solve_nonlinear(self, params, unknowns, resids):
structural_weight = 0.
L = 0.
W0 = self.prob_dict['W0'] * self.prob_dict['g']
for surface in self.surfaces:
name = surface['name']
structural_weight += params[name+'structural_weight']
L += params[name+'L']
tot_weight = structural_weight + params['fuelburn'] * self.prob_dict['g'] + W0
unknowns['total_weight'] = tot_weight
unknowns['L_equals_W'] = (tot_weight - L) / tot_weight
class ComputeCG(Component):
"""
Compute the center of gravity of the entire aircraft based on the inputted W0
and its corresponding cg and the weighted sum of each surface's structural
weight and location.
Note that we add information from each lifting surface.
Parameters
----------
nodes[ny, 3] : numpy array
Flattened array with coordinates for each FEM node.
structural_weight : float
Total weight of the structural spar for a given surface.
cg_location[3] : numpy array
Location of the structural spar's cg for a given surface.
total_weight : float
Total weight of the entire aircraft, including W0, all structural weights,
and fuel.
fuelburn : float
Computed fuel burn in kg based on the Breguet range equation.
Returns
-------
cg[3] : numpy array
The x, y, z coordinates of the center of gravity for the entire aircraft.
"""
def __init__(self, surfaces, prob_dict):
super(ComputeCG, self).__init__()
self.prob_dict = prob_dict
self.surfaces = surfaces
for surface in surfaces:
name = surface['name']
# self.add_param(name+'nodes', val=0.)
self.add_param(name+'structural_weight', val=0.)
self.add_param(name+'cg_location', val=np.zeros((3), dtype=data_type))
self.add_param('total_weight', val=0.)
self.add_param('fuelburn', val=0.)
self.add_output('cg', val=np.zeros((3), dtype=complex))
self.deriv_options['type'] = 'cs'
self.deriv_options['form'] = 'central'
def solve_nonlinear(self, params, unknowns, resids):
g = self.prob_dict['g']
W0 = self.prob_dict['W0']
W0_cg = W0 * self.prob_dict['cg'] * g
spar_cg = 0.
structural_weight = 0.
for surface in self.surfaces:
name = surface['name']
spar_cg = params[name + 'cg_location'] * params[name + 'structural_weight']
structural_weight += params[name + 'structural_weight']
tot_weight = structural_weight + params['fuelburn'] * g + W0
unknowns['cg'] = (W0_cg + spar_cg) / (params['total_weight'] - params['fuelburn'] * g)
class ComputeCM(Component):
"""
Compute the coefficient of moment (CM) for the entire aircraft.
Parameters
----------
b_pts[nx-1, ny, 3] : numpy array
Bound points for the horseshoe vortices, found along the 1/4 chord.
widths[ny-1] : numpy array
The spanwise widths of each individual panel.
chords[ny] : numpy array
The chordwise length of the entire airfoil following the camber line.
S_ref : float
The reference area of the lifting surface.
sec_forces[nx-1, ny-1, 3] : numpy array
Contains the sectional forces acting on each panel.
Stored in Fortran order (only relevant with more than one chordwise
panel).
cg[3] : numpy array
The x, y, z coordinates of the center of gravity for the entire aircraft.
v : float
Freestream air velocity in m/s.
rho : float
Air density in kg/m^3.
Returns
-------
CM[3] : numpy array
The coefficient of moment around the x-, y-, and z-axes at the cg point.
"""
def __init__(self, surfaces, prob_dict):
super(ComputeCM, self).__init__()
tot_panels = 0
for surface in surfaces:
name = surface['name']
ny = surface['num_y']
nx = surface['num_x']
self.add_param(name+'b_pts', val=np.zeros((nx-1, ny, 3), dtype=data_type))
self.add_param(name+'widths', val=np.zeros((ny-1), dtype=data_type))
self.add_param(name+'chords', val=np.zeros((ny), dtype=data_type))
self.add_param(name+'S_ref', val=0.)
self.add_param(name+'sec_forces', val=np.zeros((nx-1, ny-1, 3), dtype=data_type))
self.add_param('cg', val=np.zeros((3), dtype=data_type))
self.add_param('v', val=10.)
self.add_param('rho', val=3.)
self.add_output('CM', val=np.zeros((3), dtype=data_type))
self.surfaces = surfaces
self.prob_dict = prob_dict
if not fortran_flag:
self.deriv_options['type'] = 'cs'
self.deriv_options['form'] = 'central'
def solve_nonlinear(self, params, unknowns, resids):
rho = params['rho']
cg = params['cg']
S_ref_tot = 0.
M = np.zeros((3), dtype=data_type)
for j, surface in enumerate(self.surfaces):
name = surface['name']
nx = surface['num_x']
ny = surface['num_y']
b_pts = params[name+'b_pts']
widths = params[name+'widths']
chords = params[name+'chords']
S_ref = params[name+'S_ref']
sec_forces = params[name+'sec_forces']
panel_chords = (chords[1:] + chords[:-1]) / 2.
MAC = 1. / S_ref * np.sum(panel_chords**2 * widths)
if surface['symmetry']:
MAC *= 2
if fortran_flag:
M_tmp = OAS_API.oas_api.momentcalc(b_pts, cg, chords, widths, S_ref, sec_forces, surface['symmetry'])
M += M_tmp
else:
pts = (params[name+'b_pts'][:, 1:, :] + \
params[name+'b_pts'][:, :-1, :]) / 2
diff = (pts - cg) / MAC
moment = np.zeros((ny - 1, 3), dtype=data_type)
for ind in range(nx-1):
moment += np.cross(diff[ind, :, :], sec_forces[ind, :, :], axis=1)
if surface['symmetry']:
moment[:, 0] = 0.
moment[:, 1] *= 2
moment[:, 2] = 0.
M += np.sum(moment, axis=0)
# For the first (main) lifting surface, we save the MAC
if j == 0:
MAC_wing = MAC
S_ref_tot += S_ref
self.M = M
# Use the user-provided reference area from the main wing;
# otherwise compute the total area of all lifting surfaces.
if self.surfaces[0]['S_ref'] is None:
self.S_ref_tot = S_ref_tot
else:
self.S_ref_tot = self.surfaces[0]['S_ref']
unknowns['CM'] = M / (0.5 * rho * params['v']**2 * self.S_ref_tot * MAC_wing)
def linearize(self, params, unknowns, resids):
jac = self.alloc_jacobian()
cg = params['cg']
rho = params['rho']
v = params['v']
for j in range(3):
CMb = np.zeros((3))
CMb[j] = 1.
for i, surface in enumerate(self.surfaces):
name = surface['name']
ny = surface['num_y']
b_pts = params[name+'b_pts']
widths = params[name+'widths']
chords = params[name+'chords']
S_ref = params[name+'S_ref']
sec_forces = params[name+'sec_forces']
if i == 0:
panel_chords = (chords[1:] + chords[:-1]) / 2.
MAC = 1. / S_ref * np.sum(panel_chords**2 * widths)
if surface['symmetry']:
MAC *= 2
temp1 = self.S_ref_tot * MAC
temp0 = 0.5 * rho * v**2
temp = temp0 * temp1
tempb = np.sum(-(self.M * CMb / temp)) / temp
Mb = CMb / temp
Mb_master = Mb.copy()
jac['CM', 'rho'][j] += v**2*temp1*0.5*tempb
jac['CM', 'v'][j] += 0.5*rho*temp1*2*v*tempb
s_totb = temp0 * MAC * tempb
macb = temp0 * self.S_ref_tot * tempb
if surface['symmetry']:
macb *= 2
chordsb = np.zeros((ny))
tempb0 = macb / S_ref
panel_chordsb = 2*panel_chords*widths*tempb0
widthsb = panel_chords**2*tempb0
sb = -np.sum(panel_chords**2*widths)*tempb0/S_ref
chordsb[1:] += panel_chordsb/2.
chordsb[:-1] += panel_chordsb/2.
cb = chordsb
wb = widthsb
else:
cb = 0.
wb = 0.
sb = 0.
Mb = Mb_master.copy()
bptsb, cgb, chordsb, widthsb, S_refb, sec_forcesb, _ = OAS_API.oas_api.momentcalc_b(b_pts, cg, chords, widths, S_ref, sec_forces, surface['symmetry'], Mb)
jac['CM', 'cg'][j, :] += cgb
jac['CM', name+'b_pts'][j, :] += bptsb.flatten()
jac['CM', name+'chords'][j, :] += chordsb + cb
jac['CM', name+'widths'][j, :] += widthsb + wb
jac['CM', name+'sec_forces'][j, :] += sec_forcesb.flatten()
jac['CM', name+'S_ref'][j, :] += S_refb + sb + s_totb
return jac
class ComputeTotalCLCD(Component):
"""
Compute the coefficients of lift (CL) and drag (CD) for the entire aircraft.
Parameters
----------
CL : float
Coefficient of lift (CL) for one lifting surface.
CD : float
Coefficient of drag (CD) for one lifting surface.
S_ref : float
Surface area for one lifting surface.
v : float
Fresstream air velocity.
rho : float
Air density in kg/m^3.
Returns
-------
CL : float
Total coefficient of lift (CL) for the entire aircraft.
CD : float
Total coefficient of drag (CD) for the entire aircraft.
"""
def __init__(self, surfaces, prob_dict):
super(ComputeTotalCLCD, self).__init__()
for surface in surfaces:
name = surface['name']
self.add_param(name+'CL', val=0.)
self.add_param(name+'CD', val=0.)
self.add_param(name+'S_ref', val=0.)
self.add_param('v', val=10.)
self.add_param('rho', val=3.)
self.add_output('CL', val=0.)
self.add_output('CD', val=0.)
self.surfaces = surfaces
self.prob_dict = prob_dict
def solve_nonlinear(self, params, unknowns, resids):
rho = params['rho']
v = params['v']
CL = 0.
CD = 0.
computed_total_S_ref = 0.
for surface in self.surfaces:
name = surface['name']
S_ref = params[name+'S_ref']
CL += params[name+'CL'] * S_ref
CD += params[name+'CD'] * S_ref
computed_total_S_ref += S_ref
if self.surfaces[0]['S_ref'] is not None:
S_ref_total = self.surfaces[0]['S_ref']
else:
S_ref_total = computed_total_S_ref
unknowns['CL'] = CL / S_ref_total
unknowns['CD'] = CD / S_ref_total
self.S_ref_total = S_ref_total
def linearize(self, params, unknowns, resids):
jac = self.alloc_jacobian()
for surface in self.surfaces:
name = surface['name']
S_ref = params[name+'S_ref']
jac['CL', name+'CL'] = S_ref / self.S_ref_total
jac['CD', name+'CD'] = S_ref / self.S_ref_total
dCL_dS_ref = 0.
surf_CL = params[name + 'CL']
dCD_dS_ref = 0.
surf_CD = params[name + 'CD']
for surface_ in self.surfaces:
name_ = surface_['name']
if not name == name_:
S_ref_ = params[name_ + 'S_ref']
dCL_dS_ref += surf_CL * S_ref_
dCL_dS_ref -= params[name_ + 'CL'] * S_ref_
dCD_dS_ref += surf_CD * S_ref_
dCD_dS_ref -= params[name_ + 'CD'] * S_ref_
jac['CL', name + 'S_ref'] = dCL_dS_ref / self.S_ref_total**2
jac['CD', name + 'S_ref'] = dCD_dS_ref / self.S_ref_total**2
return jac
class TotalPerformance(Group):
"""
Group to contain the total aerostructural performance components.
"""
def __init__(self, surfaces, prob_dict):
super(TotalPerformance, self).__init__()
self.add('fuelburn',
FunctionalBreguetRange(surfaces, prob_dict),
promotes=['*'])
self.add('L_equals_W',
FunctionalEquilibrium(surfaces, prob_dict),
promotes=['*'])
self.add('CG',
ComputeCG(surfaces, prob_dict),
promotes=['*'])
self.add('moment',
ComputeCM(surfaces, prob_dict),
promotes=['*'])
self.add('CL_CD',
ComputeTotalCLCD(surfaces, prob_dict),
promotes=['*'])
class TotalAeroPerformance(Group):
"""
Group to contain the total aerodynamic performance components.
"""
def __init__(self, surfaces, prob_dict):
super(TotalAeroPerformance, self).__init__()
self.add('moment',
ComputeCM(surfaces, prob_dict),
promotes=['*'])
self.add('CL_CD',
ComputeTotalCLCD(surfaces, prob_dict),
promotes=['*'])