From ede335af1b2f16b1f10eb37f830aac6bcc7e3549 Mon Sep 17 00:00:00 2001 From: 1ozturkbe <1ozturkbe@gmail.com> Date: Wed, 11 Sep 2019 15:44:45 -0400 Subject: [PATCH] Now testing legacy code. --- robust/simple_wing/__init__.py | 0 robust/simple_wing/models.py | 107 --------------------------------- robust/testing/models.py | 53 +++++++++++++++- robust/testing/t_legacy.py | 66 ++++++++++++++++++++ run_tests.py | 3 + 5 files changed, 121 insertions(+), 108 deletions(-) delete mode 100644 robust/simple_wing/__init__.py delete mode 100644 robust/simple_wing/models.py create mode 100644 robust/testing/t_legacy.py diff --git a/robust/simple_wing/__init__.py b/robust/simple_wing/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/robust/simple_wing/models.py b/robust/simple_wing/models.py deleted file mode 100644 index bc671ab..0000000 --- a/robust/simple_wing/models.py +++ /dev/null @@ -1,107 +0,0 @@ -from gpkit import Variable, Model, SignomialsEnabled, VarKey, units, Vectorize -import numpy as np -import os - -from robust.simulations import simulate, read_simulation_data - - -def simple_wing(): - # Uncertain parameters - k = Variable("k", 1.17, "-", "form factor", pr=31.111111) - e = Variable("e", 0.92, "-", "Oswald efficiency factor", pr=7.6086956) - mu = Variable("\\mu", 1.775e-5, "kg/m/s", "viscosity of air", pr=4.225352) - rho = Variable("\\rho", 1.23, "kg/m^3", "density of air", pr=10) - tau = Variable("\\tau", 0.12, "-", "airfoil thickness to chord ratio", pr=33.333333) - N_ult = Variable("N_{ult}", 3.3, "-", "ultimate load factor", pr=33.333333) - V_min = Variable("V_{min}", 25, "m/s", "takeoff speed", pr=20) - C_Lmax = Variable("C_{L,max}", 1.6, "-", "max CL with flaps down", pr=25) - S_wetratio = Variable("(\\frac{S}{S_{wet}})", 2.075, "-", "wetted area ratio", pr=3.6144578) - W_W_coeff1 = Variable("W_{W_{coeff1}}", 12e-5, "1/m", "Wing Weight Coefficent 1", pr=60) - W_W_coeff2 = Variable("W_{W_{coeff2}}", 60, "Pa", "Wing Weight Coefficent 2", pr=66) - CDA0 = Variable("(CDA0)", 0.035, "m^2", "fuselage drag area", pr=42.857142) - W_0 = Variable("W_0", 6250, "N", "aircraft weight excluding wing", pr=60) - - # Free Variables - D = Variable("D", "N", "total drag force") - A = Variable("A", "-", "aspect ratio", fix=True) - S = Variable("S", "m^2", "total wing area", fix=True) - V = Variable("V", "m/s", "cruising speed") - W = Variable("W", "N", "total aircraft weight") - Re = Variable("Re", "-", "Reynold's number") - C_D = Variable("C_D", "-", "Drag coefficient of wing") - C_L = Variable("C_L", "-", "Lift coefficient of wing") - C_f = Variable("C_f", "-", "skin friction coefficient") - W_w = Variable("W_w", "N", "wing weight") - constraints = [] - - # Drag Model - C_D_fuse = CDA0 / S - C_D_wpar = k * C_f * S_wetratio - C_D_ind = C_L ** 2 / (np.pi * A * e) - constraints += [C_D >= C_D_ind + C_D_fuse + C_D_wpar] - - # 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 - constraints += [W_w >= W_w_surf + W_w_strc] - - # and the rest of the models - constraints += [D >= 0.5 * rho * S * C_D * V ** 2, - Re <= (rho / mu) * V * (S / A) ** 0.5, - C_f >= 0.074 / Re ** 0.2, - W <= 0.5 * rho * S * C_L * V ** 2, - W <= 0.5 * rho * S * C_Lmax * V_min ** 2, - W >= W_0 + W_w] - - m = Model(D, constraints) - return m - - -if __name__ == '__main__': - model = simple_wing() - number_of_time_average_solves = 100 - number_of_iterations = 1000 - nominal_solution, nominal_solve_time, nominal_number_of_constraints, directly_uncertain_vars_subs = \ - simulate.generate_model_properties(model, number_of_time_average_solves, number_of_iterations) - model_name = 'Simple Wing' - gammas = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] - min_num_of_linear_sections = 3 - max_num_of_linear_sections = 99 - linearization_tolerance = 1e-3 - verbosity = 0 - - methods = [{'name': 'Best Pairs', 'twoTerm': True, 'boyd': False, 'simpleModel': False}, - {'name': 'Linear. Perts.', 'twoTerm': False, 'boyd': False, 'simpleModel': False}, - {'name': 'Simple Cons.', 'twoTerm': False, 'boyd': False, 'simpleModel': True}, - {'name': 'Two Term', 'twoTerm': False, 'boyd': True, 'simpleModel': False}] - uncertainty_sets = ['box', 'elliptical'] - - model = simple_wing() - - variable_gamma_file_name = os.path.dirname(__file__) + '/simulation_data_variable_gamma.txt' - simulate.print_variable_gamma_results(model, model_name, gammas, number_of_iterations, - min_num_of_linear_sections, - max_num_of_linear_sections, verbosity, linearization_tolerance, - variable_gamma_file_name, number_of_time_average_solves, methods, - uncertainty_sets, nominal_solution, nominal_solve_time, - nominal_number_of_constraints, directly_uncertain_vars_subs) - - gamma = 1 - numbers_of_linear_sections = [12, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 32, 36, 44, 52, 60, 70, 80] - - methods = [{'name': 'Best Pairs', 'twoTerm': True, 'boyd': False, 'simpleModel': False}, - {'name': 'Linear. Perts.', 'twoTerm': False, 'boyd': False, 'simpleModel': False}, - {'name': 'Two Term', 'twoTerm': False, 'boyd': True, 'simpleModel': False}] - uncertainty_sets = ['box', 'elliptical'] - - variable_pwl_file_name = os.path.dirname(__file__) + '/simulation_data_variable_pwl.txt' - simulate.print_variable_pwlsections_results(model, model_name, gamma, number_of_iterations, - numbers_of_linear_sections, linearization_tolerance, - verbosity, variable_pwl_file_name, - number_of_time_average_solves, methods, uncertainty_sets, - nominal_solution, nominal_solve_time, - nominal_number_of_constraints, directly_uncertain_vars_subs) - - file_path_gamma = os.path.dirname(__file__) + '/simulation_data_variable_gamma.txt' - file_path_pwl = os.path.dirname(__file__) + '/simulation_data_variable_pwl.txt' - read_simulation_data.generate_all_plots(file_path_gamma, file_path_pwl) diff --git a/robust/testing/models.py b/robust/testing/models.py index 81ec9ef..e33840e 100644 --- a/robust/testing/models.py +++ b/robust/testing/models.py @@ -1,5 +1,57 @@ +import numpy as np from gpkit import Variable, Model, SignomialsEnabled +def simple_wing(): + # Uncertain parameters + k = Variable("k", 1.17, "-", "form factor", pr=31.111111) + e = Variable("e", 0.92, "-", "Oswald efficiency factor", pr=7.6086956) + mu = Variable("\\mu", 1.775e-5, "kg/m/s", "viscosity of air", pr=4.225352) + rho = Variable("\\rho", 1.23, "kg/m^3", "density of air", pr=10) + tau = Variable("\\tau", 0.12, "-", "airfoil thickness to chord ratio", pr=33.333333) + N_ult = Variable("N_{ult}", 3.3, "-", "ultimate load factor", pr=33.333333) + V_min = Variable("V_{min}", 25, "m/s", "takeoff speed", pr=20) + C_Lmax = Variable("C_{L,max}", 1.6, "-", "max CL with flaps down", pr=25) + S_wetratio = Variable("(\\frac{S}{S_{wet}})", 2.075, "-", "wetted area ratio", pr=3.6144578) + W_W_coeff1 = Variable("W_{W_{coeff1}}", 12e-5, "1/m", "Wing Weight Coefficent 1", pr=60) + W_W_coeff2 = Variable("W_{W_{coeff2}}", 60, "Pa", "Wing Weight Coefficent 2", pr=66) + CDA0 = Variable("(CDA0)", 0.035, "m^2", "fuselage drag area", pr=42.857142) + W_0 = Variable("W_0", 6250, "N", "aircraft weight excluding wing", pr=60) + + # Free Variables + D = Variable("D", "N", "total drag force") + A = Variable("A", "-", "aspect ratio", fix=True) + S = Variable("S", "m^2", "total wing area", fix=True) + V = Variable("V", "m/s", "cruising speed") + W = Variable("W", "N", "total aircraft weight") + Re = Variable("Re", "-", "Reynold's number") + C_D = Variable("C_D", "-", "Drag coefficient of wing") + C_L = Variable("C_L", "-", "Lift coefficient of wing") + C_f = Variable("C_f", "-", "skin friction coefficient") + W_w = Variable("W_w", "N", "wing weight") + constraints = [] + + # Drag Model + C_D_fuse = CDA0 / S + C_D_wpar = k * C_f * S_wetratio + C_D_ind = C_L ** 2 / (np.pi * A * e) + constraints += [C_D >= C_D_ind + C_D_fuse + C_D_wpar] + + # 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 + constraints += [W_w >= W_w_surf + W_w_strc] + + # and the rest of the models + constraints += [D >= 0.5 * rho * S * C_D * V ** 2, + Re <= (rho / mu) * V * (S / A) ** 0.5, + C_f >= 0.074 / Re ** 0.2, + W <= 0.5 * rho * S * C_L * V ** 2, + W <= 0.5 * rho * S * C_Lmax * V_min ** 2, + W >= W_0 + W_w] + + m = Model(D, constraints) + return m + def gp_test_model(): x = Variable('x') y = Variable('y') @@ -11,7 +63,6 @@ def gp_test_model(): b * x / y + b * x * y + a*b**2 * x ** 2 <= 1] return Model((x * y) ** -1, constraints) - def sp_test_model(): x = Variable('x') y = Variable('y') diff --git a/robust/testing/t_legacy.py b/robust/testing/t_legacy.py new file mode 100644 index 0000000..aa55981 --- /dev/null +++ b/robust/testing/t_legacy.py @@ -0,0 +1,66 @@ +"""Testing legacy code""" +import numpy as np +import os + +import unittest +from gpkit.tests.helpers import run_tests + +from robust.testing.models import simple_wing +from robust.simulations import simulate +from robust.simulations import read_simulation_data + +class TestLegacy(unittest.TestCase): + def test_simple_wing(self): + model = simple_wing() + number_of_time_average_solves = 3 + number_of_iterations = 10 + nominal_solution, nominal_solve_time, nominal_number_of_constraints, directly_uncertain_vars_subs = \ + simulate.generate_model_properties(model, number_of_time_average_solves, number_of_iterations) + model_name = 'Simple Wing' + gammas = np.linspace(0, 1, 3) + min_num_of_linear_sections = 3 + max_num_of_linear_sections = 99 + linearization_tolerance = 1e-4 + verbosity = 0 + + methods = [{'name': 'Best Pairs', 'twoTerm': True, 'boyd': False, 'simpleModel': False}, + {'name': 'Linear. Perts.', 'twoTerm': False, 'boyd': False, 'simpleModel': False}, + {'name': 'Simple Cons.', 'twoTerm': False, 'boyd': False, 'simpleModel': True}, + {'name': 'Two Term', 'twoTerm': False, 'boyd': True, 'simpleModel': False}] + uncertainty_sets = ['box', 'elliptical'] + + variable_gamma_file_name = os.path.dirname(__file__) + '/simulation_data_variable_gamma.txt' + simulate.print_variable_gamma_results(model, model_name, gammas, number_of_iterations, + min_num_of_linear_sections, + max_num_of_linear_sections, verbosity, linearization_tolerance, + variable_gamma_file_name, number_of_time_average_solves, methods, + uncertainty_sets, nominal_solution, nominal_solve_time, + nominal_number_of_constraints, directly_uncertain_vars_subs) + + gamma = 1. + numbers_of_linear_sections = [12, 20, 30, 44, 60, 80] + + methods = [{'name': 'Best Pairs', 'twoTerm': True, 'boyd': False, 'simpleModel': False}, + {'name': 'Linear. Perts.', 'twoTerm': False, 'boyd': False, 'simpleModel': False}, + {'name': 'Two Term', 'twoTerm': False, 'boyd': True, 'simpleModel': False}] + uncertainty_sets = ['box', 'elliptical'] + + variable_pwl_file_name = os.path.dirname(__file__) + '/simulation_data_variable_pwl.txt' + simulate.print_variable_pwlsections_results(model, model_name, gamma, number_of_iterations, + numbers_of_linear_sections, linearization_tolerance, + verbosity, variable_pwl_file_name, + number_of_time_average_solves, methods, uncertainty_sets, + nominal_solution, nominal_solve_time, + nominal_number_of_constraints, directly_uncertain_vars_subs) + + file_path_gamma = os.path.dirname(__file__) + '/simulation_data_variable_gamma.txt' + file_path_pwl = os.path.dirname(__file__) + '/simulation_data_variable_pwl.txt' + read_simulation_data.generate_all_plots(file_path_gamma, file_path_pwl) + +TESTS = [TestLegacy] + +def test(): + run_tests(TESTS) + +if __name__ == "__main__": + test() diff --git a/run_tests.py b/run_tests.py index f2c9fd2..3d00e9f 100644 --- a/run_tests.py +++ b/run_tests.py @@ -23,6 +23,9 @@ def import_tests(): from robust.testing import t_simulation tests += t_simulation.TESTS + from robust.testing import t_legacy + tests += t_legacy.TESTS + return tests