Coverage for docs/source/examples/simpleflight.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

52 statements  

1"Minimizes airplane drag for a simple drag and structure model." 

2import pickle 

3import numpy as np 

4from gpkit import Variable, Model, SolutionArray 

5pi = np.pi 

6 

7 

8# Constants 

9k = Variable("k", 1.2, "-", "form factor") 

10e = Variable("e", 0.95, "-", "Oswald efficiency factor") 

11mu = Variable("\\mu", 1.78e-5, "kg/m/s", "viscosity of air") 

12rho = Variable("\\rho", 1.23, "kg/m^3", "density of air") 

13tau = Variable("\\tau", 0.12, "-", "airfoil thickness to chord ratio") 

14N_ult = Variable("N_{ult}", 3.8, "-", "ultimate load factor") 

15V_min = Variable("V_{min}", 22, "m/s", "takeoff speed") 

16C_Lmax = Variable("C_{L,max}", 1.5, "-", "max CL with flaps down") 

17S_wetratio = Variable("(\\frac{S}{S_{wet}})", 2.05, "-", "wetted area ratio") 

18W_W_coeff1 = Variable("W_{W_{coeff1}}", 8.71e-5, "1/m", 

19 "Wing Weight Coefficent 1") 

20W_W_coeff2 = Variable("W_{W_{coeff2}}", 45.24, "Pa", 

21 "Wing Weight Coefficent 2") 

22CDA0 = Variable("(CDA0)", 0.031, "m^2", "fuselage drag area") 

23W_0 = Variable("W_0", 4940.0, "N", "aircraft weight excluding wing") 

24 

25# Free Variables 

26D = Variable("D", "N", "total drag force") 

27A = Variable("A", "-", "aspect ratio") 

28S = Variable("S", "m^2", "total wing area") 

29V = Variable("V", "m/s", "cruising speed") 

30W = Variable("W", "N", "total aircraft weight") 

31Re = Variable("Re", "-", "Reynold's number") 

32C_D = Variable("C_D", "-", "Drag coefficient of wing") 

33C_L = Variable("C_L", "-", "Lift coefficent of wing") 

34C_f = Variable("C_f", "-", "skin friction coefficient") 

35W_w = Variable("W_w", "N", "wing weight") 

36 

37constraints = [] 

38 

39# Drag model 

40C_D_fuse = CDA0/S 

41C_D_wpar = k*C_f*S_wetratio 

42C_D_ind = C_L**2/(pi*A*e) 

43constraints += [C_D >= C_D_fuse + C_D_wpar + C_D_ind] 

44 

45# Wing weight model 

46W_w_strc = W_W_coeff1*(N_ult*A**1.5*(W_0*W*S)**0.5)/tau 

47W_w_surf = W_W_coeff2 * S 

48constraints += [W_w >= W_w_surf + W_w_strc] 

49 

50# and the rest of the models 

51constraints += [D >= 0.5*rho*S*C_D*V**2, 

52 Re <= (rho/mu)*V*(S/A)**0.5, 

53 C_f >= 0.074/Re**0.2, 

54 W <= 0.5*rho*S*C_L*V**2, 

55 W <= 0.5*rho*S*C_Lmax*V_min**2, 

56 W >= W_0 + W_w] 

57 

58print("SINGLE\n======") 

59m = Model(D, constraints) 

60sol = m.solve(verbosity=0) 

61print(sol.summary()) 

62# save solution to a file and retrieve it 

63sol.save("solution.pkl") 

64sol.save_compressed("solution.pgz") 

65print(sol.diff("solution.pkl")) 

66 

67print("SWEEP\n=====") 

68N = 2 

69sweeps = {V_min: ("sweep", np.linspace(20, 25, N)), 

70 V: ("sweep", np.linspace(45, 55, N)), } 

71m.substitutions.update(sweeps) 

72sweepsol = m.solve(verbosity=0) 

73print(sweepsol.summary()) 

74sol_loaded = pickle.load(open("solution.pkl", "rb")) 

75assert sol_loaded.almost_equal(SolutionArray.decompress_file("solution.pgz")) 

76print(sweepsol.diff(sol_loaded, absdiff=True, senssdiff=True))