def inv_sigmoid(x): return cas.log(1 / x - 1)
def test_gpkit_style_solve(): """ Here, the problem is formulated *exactly* as it is in the GPKit paper. This isn't how you would actually want to solve this problem (lots of redundant variables here...), but this test just confirms that it's a valid mathematical formulation and still works. Also note that all constraints and the objective are log-transformed, so under the hood, this is basically exactly a geometric program (and should be convex, yay). """ opti = asb.Opti() # initialize an optimization environment ### Variables D = opti.variable(init_guess=1e3, log_transform=True) # total drag force [N] A = opti.variable(init_guess=1e1, log_transform=True) # aspect ratio S = opti.variable(init_guess=1e2, log_transform=True) # total wing area [m^2] V = opti.variable(init_guess=1e2, log_transform=True) # cruising speed [m/s] W = opti.variable(init_guess=8e3, log_transform=True) # total aircraft weight [N] Re = opti.variable(init_guess=5e6, log_transform=True) # Reynolds number [-] C_D = opti.variable(init_guess=3e-2, log_transform=True) # Drag coefficient of wing [-] C_L = opti.variable(init_guess=1, log_transform=True) # Lift coefficient of wing [-] C_f = opti.variable(init_guess=1e-2, log_transform=True) # Skin friction coefficient [-] W_w = opti.variable(init_guess=3e3, log_transform=True) # Wing weight [N] ### Constraints # Drag model C_D_fuse = CDA0 / S C_D_wpar = k * C_f * S_wetratio C_D_ind = C_L**2 / (pi * A * e) opti.subject_to(cas.log(C_D) >= cas.log(C_D_fuse + C_D_wpar + C_D_ind)) # Wing weight model W_w_strc = W_W_coeff1 * (N_ult * A**1.5 * (W_0 * W * S)**0.5) / tau W_w_surf = W_W_coeff2 * S opti.subject_to(cas.log(W_w) >= cas.log(W_w_surf + W_w_strc)) # Other models opti.subject_to([ cas.log(D) >= cas.log(0.5 * rho * S * C_D * V**2), cas.log(Re) <= cas.log((rho / mu) * V * (S / A)**0.5), cas.log(C_f) >= cas.log(0.074 / Re**0.2), cas.log(W) <= cas.log(0.5 * rho * S * C_L * V**2), cas.log(W) <= cas.log(0.5 * rho * S * C_Lmax * V_min**2), cas.log(W) >= cas.log(W_0 + W_w), ]) # Objective opti.minimize(cas.log(D)) sol = opti.solve() assert sol.value(D) == pytest.approx(303.1, abs=0.1)
error = model(machs_to_fit, fit_params) - beta(machs_to_fit) error = np.array(error) machs_to_plot = np.linspace(-0.5, 1.5, 500) fig, ax = plt.subplots(1, 1, figsize=(8, 7), dpi=200) plt.subplot("221") plt.plot(machs_to_plot, beta(machs_to_plot), ".", label="") plt.plot(machs_to_plot, model(machs_to_plot, fit_params)) plt.ylim(-0.05, 1.05) plt.title("Fit: Normal Space") plt.xlabel(r"Mach $M$ [-]") plt.ylabel(r"$\beta = \sqrt{1-M^2}$") plt.subplot("222") plt.plot(machs_to_plot, inv_sigmoid(beta(machs_to_plot)), ".", label="") plt.plot(machs_to_plot, inv_sigmoid(model(machs_to_plot, fit_params))) plt.ylim(-15, 5) plt.title("Fit: Inverse Sigmoid Space") plt.xlabel(r"Mach $M$ [-]") plt.ylabel(r"$\sigma^{-1}\left(\beta\right)$") plt.subplot("223") plt.plot(machs_to_plot, cas.log(beta(machs_to_plot)), ".", label="") plt.plot(machs_to_plot, cas.log(model(machs_to_plot, fit_params))) plt.ylim(-2.5, 0.5) plt.title("Fit: Log Space") plt.xlabel(r"Mach $M$ [-]") plt.ylabel(r"$\ln(\beta)$") plt.subplot("224") plt.plot(machs_to_fit, error) plt.title("Error in Fit range") plt.savefig("machfitting.png")
def test_original_gpkit_like_solve(): opti = asb.Opti() # initialize an optimization environment ### Variables D = opti.variable(log_transform=True, init_guess=1000, scale=1e3) # total drag force [N] A = opti.variable(log_transform=True, init_guess=10, scale=1e1) # aspect ratio S = opti.variable(log_transform=True, init_guess=100, scale=1e2) # total wing area [m^2] V = opti.variable(log_transform=True, init_guess=100, scale=1e2) # cruising speed [m/s] W = opti.variable(log_transform=True, init_guess=8e3, scale=1e4) # total aircraft weight [N] Re = opti.variable(log_transform=True, init_guess=5e6, scale=1e6) # Reynolds number [-] C_D = opti.variable(log_transform=True, init_guess=0.03, scale=1e-2) # Drag coefficient of wing [-] C_L = opti.variable(log_transform=True, init_guess=1, scale=1e-1) # Lift coefficient of wing [-] C_f = opti.variable(log_transform=True, init_guess=0.01, scale=1e-2) # Skin friction coefficient [-] W_w = opti.variable(log_transform=True, init_guess=3e3, scale=1e3) # Wing weight [N] ### Constraints # Drag model C_D_fuse = CDA0 / S C_D_wpar = k * C_f * S_wetratio C_D_ind = C_L ** 2 / (pi * A * e) opti.subject_to(cas.log(C_D) >= cas.log(C_D_fuse + C_D_wpar + C_D_ind)) # Wing weight model W_w_strc = W_W_coeff1 * (N_ult * A ** 1.5 * (W_0 * W * S) ** 0.5) / tau W_w_surf = W_W_coeff2 * S opti.subject_to(cas.log(W_w) >= cas.log(W_w_surf + W_w_strc)) # Other models opti.subject_to([ cas.log(D) >= cas.log(0.5 * rho * S * C_D * V ** 2), cas.log(Re) <= cas.log((rho / mu) * V * (S / A) ** 0.5), cas.log(C_f) >= cas.log(0.074 / Re ** 0.2), cas.log(W) <= cas.log(0.5 * rho * S * C_L * V ** 2), cas.log(W) <= cas.log(0.5 * rho * S * C_Lmax * V_min ** 2), cas.log(W) >= cas.log(W_0 + W_w), ]) # Objective opti.minimize(cas.log(D)) sol = opti.solve() assert sol.value(D) == pytest.approx(303.1, abs=0.1)