def test_compute_cruise(): """ Tests cruise phase """ # Research independent input value in .xml file group = Group() group.add_subsystem("in_flight_cg_variation", InFlightCGVariation(), promotes=["*"]) group.add_subsystem("cruise", _compute_cruise(propulsion_id=ENGINE_WRAPPER), promotes=["*"]) ivc = get_indep_var_comp(list_inputs(group), __file__, XML_FILE) ivc.add_output("data:mission:sizing:taxi_out:fuel", 0.50, units="kg") ivc.add_output("data:mission:sizing:takeoff:fuel", 0.29, units="kg") ivc.add_output("data:mission:sizing:initial_climb:fuel", 0.07, units="kg") ivc.add_output("data:mission:sizing:main_route:climb:fuel", 5.56, units="kg") ivc.add_output("data:mission:sizing:main_route:climb:distance", 13.2, units="km") ivc.add_output("data:mission:sizing:main_route:descent:distance", 0.0, units="km") # Run problem and check obtained value(s) is/(are) correct register_wrappers() problem = run_system(group, ivc) fuel_mass = problem.get_val("data:mission:sizing:main_route:cruise:fuel", units="kg") assert fuel_mass == pytest.approx(155.003, abs=1e-1) duration = problem.get_val( "data:mission:sizing:main_route:cruise:duration", units="h") assert duration == pytest.approx(5.51, abs=1e-2)
def setUp(self): model = Group() ivc = IndepVarComp() mapdata = SampleMap() params = mapdata.param_data x, y, z = params outs = mapdata.output_data z = outs[0] ivc.add_output('x', x['default'], units=x['units']) ivc.add_output('y', y['default'], units=y['units']) ivc.add_output('z', z['default'], units=z['units']) model.add_subsystem('des_vars', ivc, promotes=["*"]) comp = MetaModelStructured(method='slinear', extrapolate=True) for param in params: comp.add_input(param['name'], param['default'], param['values']) for out in outs: comp.add_output(out['name'], out['default'], out['values']) model.add_subsystem('comp', comp, promotes=["*"]) self.prob = Problem(model) self.prob.setup() self.prob['x'] = 1.0 self.prob['y'] = 0.75 self.prob['z'] = -1.7
def test_compute_descent(): """ Tests descent phase """ # Research independent input value in .xml file group = Group() group.add_subsystem("in_flight_cg_variation", InFlightCGVariation(), promotes=["*"]) group.add_subsystem("descent", _compute_descent(propulsion_id=ENGINE_WRAPPER), promotes=["*"]) ivc = get_indep_var_comp(list_inputs(group), __file__, XML_FILE) ivc.add_output("data:mission:sizing:taxi_out:fuel", 0.98, units="kg") ivc.add_output("data:mission:sizing:takeoff:fuel", 0.29, units="kg") ivc.add_output("data:mission:sizing:initial_climb:fuel", 0.07, units="kg") ivc.add_output("data:mission:sizing:main_route:climb:fuel", 5.56, units="kg") ivc.add_output("data:mission:sizing:main_route:cruise:fuel", 188.05, units="kg") # Run problem and check obtained value(s) is/(are) correct register_wrappers() problem = run_system(group, ivc) fuel_mass = problem.get_val("data:mission:sizing:main_route:descent:fuel", units="kg") assert fuel_mass == pytest.approx(0.863, abs=1e-2) distance = problem.get_val( "data:mission:sizing:main_route:descent:distance", units="m") / 1000 # conversion to km assert distance == pytest.approx(81.255, abs=1e-2) duration = problem.get_val( "data:mission:sizing:main_route:descent:duration", units="min") assert duration == pytest.approx(28.277, abs=1e-2)
def test_compute_climb(): """ Tests climb phase """ # Run problem and check obtained value(s) is/(are) correct group = Group() group.add_subsystem("in_flight_cg_variation", InFlightCGVariation(), promotes=["*"]) group.add_subsystem("descent", _compute_climb(propulsion_id=ENGINE_WRAPPER), promotes=["*"]) ivc = get_indep_var_comp(list_inputs(group), __file__, XML_FILE) ivc.add_output("data:mission:sizing:taxi_out:fuel", 0.50, units="kg") ivc.add_output("data:mission:sizing:takeoff:fuel", 0.29, units="kg") ivc.add_output("data:mission:sizing:initial_climb:fuel", 0.07, units="kg") register_wrappers() problem = run_system(group, ivc) v_cas = problem.get_val("data:mission:sizing:main_route:climb:v_cas", units="kn") assert v_cas == pytest.approx(71.5, abs=1) fuel_mass = problem.get_val("data:mission:sizing:main_route:climb:fuel", units="kg") assert fuel_mass == pytest.approx(5.107, abs=1e-1) distance = problem.get_val("data:mission:sizing:main_route:climb:distance", units="m") / 1000.0 # conversion to km assert distance == pytest.approx(16.648, abs=1e-2) duration = problem.get_val("data:mission:sizing:main_route:climb:duration", units="min") assert duration == pytest.approx(5.645, abs=1e-2)
def test_polar_low_speed(): """Tests ComputePolar""" # Need to plug Cd modules, Reynolds and Oswald input_list = [ "data:aerodynamics:aircraft:takeoff:mach", "data:geometry:wing:area", "data:geometry:wing:span", "data:geometry:fuselage:maximum_height", "data:geometry:fuselage:maximum_width", "data:geometry:wing:root:chord", "data:geometry:wing:tip:chord", "data:geometry:wing:sweep_25", "data:geometry:wing:thickness_ratio", "data:geometry:wing:wetted_area", "data:geometry:wing:MAC:length", "data:geometry:fuselage:length", "data:geometry:fuselage:wetted_area", "data:geometry:horizontal_tail:MAC:length", "data:geometry:horizontal_tail:thickness_ratio", "data:geometry:horizontal_tail:sweep_25", "data:geometry:horizontal_tail:wetted_area", "data:geometry:vertical_tail:MAC:length", "data:geometry:vertical_tail:thickness_ratio", "data:geometry:vertical_tail:sweep_25", "data:geometry:vertical_tail:wetted_area", "data:geometry:propulsion:pylon:length", "data:geometry:propulsion:nacelle:length", "data:geometry:propulsion:pylon:wetted_area", "data:geometry:propulsion:nacelle:wetted_area", "data:geometry:propulsion:engine:count", "data:geometry:propulsion:fan:length", "data:geometry:aircraft:wetted_area", "tuning:aerodynamics:aircraft:cruise:CD:k", "tuning:aerodynamics:aircraft:cruise:CD:offset", "tuning:aerodynamics:aircraft:cruise:CD:winglet_effect:k", "tuning:aerodynamics:aircraft:cruise:CD:winglet_effect:offset", ] group = Group() group.add_subsystem("reynolds", ComputeReynolds(low_speed_aero=True), promotes=["*"]) group.add_subsystem("oswald", OswaldCoefficient(low_speed_aero=True), promotes=["*"]) group.add_subsystem( "induced_drag_coeff", InducedDragCoefficient(low_speed_aero=True), promotes=["*"] ) group.add_subsystem("cd0", CD0(low_speed_aero=True), promotes=["*"]) group.add_subsystem("cd_trim", CdTrim(low_speed_aero=True), promotes=["*"]) group.add_subsystem("polar", ComputePolar(polar_type=PolarType.LOW_SPEED), promotes=["*"]) ivc = get_indep_var_comp(input_list) ivc.add_output("data:aerodynamics:aircraft:low_speed:CL", np.arange(0.0, 1.5, 0.01)) problem = run_system(group, ivc) cd = problem["data:aerodynamics:aircraft:low_speed:CD"] cl = problem["data:aerodynamics:aircraft:low_speed:CL"] assert cd[cl == 0.5] == approx(0.033441, abs=1e-5) assert cd[cl == 1.0] == approx(0.077523, abs=1e-5)
def test_shape(self): import numpy as np from openmdao.api import Group, Problem, IndepVarComp from openmdao.components.meta_model_structured import MetaModelStructured # create input param training data, of sizes 25, 5, and 10 points resp. p1 = np.linspace(0, 100, 25) p2 = np.linspace(-10, 10, 5) p3 = np.linspace(0, 1, 10) # can use meshgrid to create a 3D array of test data P1, P2, P3 = np.meshgrid(p1, p2, p3, indexing='ij') f = np.sqrt(P1) + P2 * P3 # verify the shape matches the order and size of the input params print(f.shape) # Create regular grid interpolator instance interp = MetaModelStructured(method='cubic') interp.add_input('p1', 0.5, p1) interp.add_input('p2', 0.0, p2) interp.add_input('p3', 3.14, p3) interp.add_output('f', 0.0, f) # Set up the OpenMDAO model model = Group() model.add_subsystem('comp', interp, promotes=["*"]) prob = Problem(model) prob.setup() # set inputs prob['p1'] = 55.12 prob['p2'] = -2.14 prob['p3'] = 0.323 prob.run_model() computed = prob['f'] actual = 6.73306472 assert_almost_equal(computed, actual) # we can verify all gradients by checking against finit-difference prob.check_partials(compact_print=True)
def test_training_derivatives(self): import numpy as np from openmdao.api import Group, Problem, IndepVarComp from openmdao.components.meta_model_structured import MetaModelStructured # create input param training data, of sizes 25, 5, and 10 points resp. p1 = np.linspace(0, 100, 25) p2 = np.linspace(-10, 10, 5) p3 = np.linspace(0, 1, 10) # can use meshgrid to create a 3D array of test data P1, P2, P3 = np.meshgrid(p1, p2, p3, indexing='ij') f = np.sqrt(P1) + P2 * P3 # verify the shape matches the order and size of the input params print(f.shape) # Create regular grid interpolator instance interp = MetaModelStructured(method='cubic', training_data_gradients=True) interp.add_input('p1', 0.5, p1) interp.add_input('p2', 0.0, p2) interp.add_input('p3', 3.14, p3) interp.add_output('f', 0.0, f) # Set up the OpenMDAO model model = Group() model.add_subsystem('comp', interp, promotes=["*"]) prob = Problem(model) prob.setup() # set inputs prob['p1'] = 55.12 prob['p2'] = -2.14 prob['p3'] = 0.323 prob.run_model() computed = prob['f'] actual = 6.73306472 assert_almost_equal(computed, actual) # we can verify all gradients by checking against finit-difference prob.check_partials(compact_print=True)
def test_meta_model_structured_deprecated(self): # run same test as above, only with the deprecated component, # to ensure we get the warning and the correct answer. # self-contained, to be removed when class name goes away. import numpy as np from openmdao.api import Group, Problem, IndepVarComp from openmdao.components.meta_model_structured_comp import MetaModelStructured # deprecated import warnings with warnings.catch_warnings(record=True) as w: xor_interp = MetaModelStructured(method='slinear') self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[0].category, DeprecationWarning)) self.assertEqual(str(w[0].message), "'MetaModelStructured' has been deprecated. Use " "'MetaModelStructuredComp' instead.") # set up inputs and outputs xor_interp.add_input('x', 0.0, training_data=np.array([0.0, 1.0]), units=None) xor_interp.add_input('y', 1.0, training_data=np.array([0.0, 1.0]), units=None) xor_interp.add_output('xor', 1.0, training_data=np.array([[0.0, 1.0], [1.0, 0.0]]), units=None) # Set up the OpenMDAO model model = Group() ivc = IndepVarComp() ivc.add_output('x', 0.0) ivc.add_output('y', 1.0) model.add_subsystem('ivc', ivc, promotes=["*"]) model.add_subsystem('comp', xor_interp, promotes=["*"]) prob = Problem(model) prob.setup() # Now test out a 'fuzzy' XOR prob['x'] = 0.9 prob['y'] = 0.001242 prob.run_model() computed = prob['xor'] actual = 0.8990064 assert_almost_equal(computed, actual) # we can verify all gradients by checking against finite-difference prob.check_partials(compact_print=True)
def test_training_gradient(self): model = Group() ivc = IndepVarComp() mapdata = SampleMap() params = mapdata.param_data outs = mapdata.output_data ivc.add_output('x', np.array([-0.3, 0.7, 1.2])) ivc.add_output('y', np.array([0.14, 0.313, 1.41])) ivc.add_output('z', np.array([-2.11, -1.2, 2.01])) ivc.add_output('f_train', outs[0]['values']) ivc.add_output('g_train', outs[1]['values']) comp = MetaModelStructured(training_data_gradients=True, method='cubic', num_nodes=3) for param in params: comp.add_input(param['name'], param['default'], param['values']) for out in outs: comp.add_output(out['name'], out['default'], out['values']) model.add_subsystem('ivc', ivc, promotes=["*"]) model.add_subsystem('comp', comp, promotes=["*"]) prob = Problem(model) prob.setup() prob.run_model() val0 = np.array([ 50.26787317, 49.76106232, 19.66117913]) val1 = np.array([-32.62094041, -31.67449135, -27.46959668]) tol = 1e-5 assert_rel_error(self, prob['f'], val0, tol) assert_rel_error(self, prob['g'], val1, tol) self.run_and_check_derivs(prob)
def test_training_gradient(self): model = Group() ivc = IndepVarComp() mapdata = SampleMap() params = mapdata.param_data outs = mapdata.output_data ivc.add_output('x', np.array([-0.3, 0.7, 1.2])) ivc.add_output('y', np.array([0.14, 0.313, 1.41])) ivc.add_output('z', np.array([-2.11, -1.2, 2.01])) ivc.add_output('f_train', outs[0]['values']) ivc.add_output('g_train', outs[1]['values']) comp = MetaModelStructuredComp(training_data_gradients=True, method='cubic', num_nodes=3) for param in params: comp.add_input(param['name'], param['default'], param['values']) for out in outs: comp.add_output(out['name'], out['default'], out['values']) model.add_subsystem('ivc', ivc, promotes=["*"]) model.add_subsystem('comp', comp, promotes=["*"]) prob = Problem(model) prob.setup() prob.run_model() val0 = np.array([ 50.26787317, 49.76106232, 19.66117913]) val1 = np.array([-32.62094041, -31.67449135, -27.46959668]) tol = 1e-5 assert_rel_error(self, prob['f'], val0, tol) assert_rel_error(self, prob['g'], val1, tol) self.run_and_check_derivs(prob)
def test_raise_out_of_bounds_error(self): model = Group() ivc = IndepVarComp() mapdata = SampleMap() params = mapdata.param_data x, y, z = params outs = mapdata.output_data z = outs[0] ivc.add_output('x', x['default'], units=x['units']) ivc.add_output('y', y['default'], units=y['units']) ivc.add_output('z', z['default'], units=z['units']) model.add_subsystem('des_vars', ivc, promotes=["*"]) # Need to make sure extrapolate is False for bounds to be checked comp = MetaModelStructured(method='slinear', extrapolate=False) for param in params: comp.add_input(param['name'], param['default'], param['values']) for out in outs: comp.add_output(out['name'], out['default'], out['values']) model.add_subsystem('comp', comp, promotes=["*"]) self.prob = Problem(model) self.prob.setup() self.prob['x'] = 1.0 self.prob['y'] = 0.75 self.prob['z'] = 9.0 # intentionally set to be out of bounds # The interpolating output name is given as a regexp because the exception could # happen with f or g first. The order those are evaluated comes from the keys of # dict so no guarantee on the order except for Python 3.6 ! msg = "Error interpolating output '[f|g]' in 'comp' " + "because input 'comp.z' was " \ "out of bounds \('.*', '.*'\) with value '9.0'" with assertRaisesRegex(self, ValueError, msg): self.run_and_check_derivs(self.prob)
def test_xor(self): import numpy as np from openmdao.api import Group, Problem, IndepVarComp from openmdao.components.meta_model_structured import MetaModelStructured # Create regular grid interpolator instance xor_interp = MetaModelStructured(method='slinear') # set up inputs and outputs xor_interp.add_input('x', 0.0, np.array([0.0, 1.0]), units=None) xor_interp.add_input('y', 1.0, np.array([0.0, 1.0]), units=None) xor_interp.add_output('xor', 1.0, np.array([[0.0, 1.0], [1.0, 0.0]]), units=None) # Set up the OpenMDAO model model = Group() ivc = IndepVarComp() ivc.add_output('x', 0.0) ivc.add_output('y', 1.0) model.add_subsystem('ivc', ivc, promotes=["*"]) model.add_subsystem('comp', xor_interp, promotes=["*"]) prob = Problem(model) prob.setup() # Now test out a 'fuzzy' XOR prob['x'] = 0.9 prob['y'] = 0.001242 prob.run_model() computed = prob['xor'] actual = 0.8990064 assert_almost_equal(computed, actual) # we can verify all gradients by checking against finit-difference prob.check_partials(compact_print=True)
x = inputs['x'] y = inputs['y'] outputs['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0 if __name__ == "__main__": from openmdao.core.problem import Problem from openmdao.core.group import Group from openmdao.core.indepvarcomp import IndepVarComp model = Group() ivc = IndepVarComp() ivc.add_output('x', 3.0) ivc.add_output('y', -4.0) model.add_subsystem('des_vars', ivc) model.add_subsystem('parab_comp', Paraboloid()) model.connect('des_vars.x', 'parab_comp.x') model.connect('des_vars.y', 'parab_comp.y') prob = Problem(model) prob.setup() prob.run_model() print(prob['parab_comp.f_xy']) prob['des_vars.x'] = 5.0 prob['des_vars.y'] = -2.0 prob.run_model() print(prob['parab_comp.f_xy'])
Bfw0 = 24506700.5989 # spline parameterization nspline = 6 rspline = np.linspace(Rhub, Rtip, nspline) ivc.add_output('rspline', rspline) # starting point chordspline = np.interp(rspline, r, initial['planform']['Chord'][1:-1]) thetaspline = np.interp(rspline, r, initial['planform']['Twist'][1:-1]) tcspline = np.interp(rspline, r, tcinitial) ivc.add_output('chordspline', chordspline) # starting point ivc.add_output('thetaspline', thetaspline) # starting point tcspline[0] = 1.0 # must be cylinder at root ivc.add_output('tcspline', tcspline) # starting point model = Group() model.add_subsystem('inputs', ivc, promotes=['*']) model.add_subsystem('spline', Spline(), promotes=['*']) model.add_subsystem('blending', DetermineBlending(), promotes=['*']) model.add_subsystem('ccblade', CCBlade(), promotes=['*']) model.add_subsystem('thickness', ThicknessMargin(), promotes=['*']) model.add_subsystem('aep', AEP(), promotes=['*']) prob = Problem(model) # prob.setup() # prob.check_partials(compact_print=True) # # ------------ run actual optmization ----- # prob.driver = pyOptSparseDriver() # prob.driver.options['optimizer'] = "SNOPT"
def test_polar_high_speed(): """ Tests ComputePolar """ # Need to plug Cd modules, Reynolds and Oswald input_list = [ "tuning:aerodynamics:aircraft:cruise:CD:k", "tuning:aerodynamics:aircraft:cruise:CD:offset", "tuning:aerodynamics:aircraft:cruise:CD:winglet_effect:k", "tuning:aerodynamics:aircraft:cruise:CD:winglet_effect:offset", "data:geometry:fuselage:maximum_height", "data:geometry:fuselage:length", "data:geometry:fuselage:wetted_area", "data:geometry:fuselage:maximum_width", "data:geometry:wing:area", "data:geometry:horizontal_tail:MAC:length", "data:geometry:horizontal_tail:sweep_25", "data:geometry:horizontal_tail:thickness_ratio", "data:geometry:horizontal_tail:wetted_area", "data:geometry:wing:area", "data:geometry:propulsion:engine:count", "data:geometry:propulsion:fan:length", "data:geometry:propulsion:nacelle:length", "data:geometry:propulsion:nacelle:wetted_area", "data:geometry:propulsion:pylon:length", "data:geometry:propulsion:pylon:wetted_area", "data:geometry:wing:area", "data:geometry:aircraft:wetted_area", "data:geometry:vertical_tail:MAC:length", "data:geometry:vertical_tail:sweep_25", "data:geometry:vertical_tail:thickness_ratio", "data:geometry:vertical_tail:wetted_area", "data:geometry:wing:MAC:length", "data:geometry:wing:sweep_25", "data:geometry:wing:thickness_ratio", "data:geometry:wing:wetted_area", "data:geometry:wing:span", "data:geometry:wing:root:chord", "data:geometry:wing:tip:chord", "data:TLAR:cruise_mach", "data:mission:sizing:main_route:cruise:altitude", ] group = Group() group.add_subsystem("reynolds", ComputeReynolds(), promotes=["*"]) group.add_subsystem("oswald", OswaldCoefficient(), promotes=["*"]) group.add_subsystem("cd0", CD0(), promotes=["*"]) group.add_subsystem("cd_compressibility", CdCompressibility(), promotes=["*"]) group.add_subsystem("cd_trim", CdTrim(), promotes=["*"]) group.add_subsystem("polar", ComputePolar(), promotes=["*"]) ivc = get_indep_var_comp(input_list) ivc.add_output("data:aerodynamics:aircraft:cruise:CL", np.arange(0.0, 1.5, 0.01)) problem = run_system(group, ivc) cd = problem["data:aerodynamics:aircraft:cruise:CD"] cl = problem["data:aerodynamics:aircraft:cruise:CL"] assert cd[cl == 0.0] == approx(0.02102, abs=1e-5) assert cd[cl == 0.2] == approx(0.02281, abs=1e-5) assert cd[cl == 0.42] == approx(0.02977, abs=1e-5) assert cd[cl == 0.85] == approx(0.07062, abs=1e-5) assert problem["data:aerodynamics:aircraft:cruise:optimal_CL"] == approx( 0.51, abs=1e-3) assert problem["data:aerodynamics:aircraft:cruise:optimal_CD"] == approx( 0.03487, abs=1e-5)
y = inputs['y'] partials['f_xy', 'x'] = 2.0 * x - 6.0 + y partials['f_xy', 'y'] = 2.0 * y + 8.0 + x if __name__ == "__main__": from openmdao.core.problem import Problem from openmdao.core.group import Group from openmdao.core.indepvarcomp import IndepVarComp model = Group() ivc = IndepVarComp() ivc.add_output('x', 3.0) ivc.add_output('y', -4.0) model.add_subsystem('des_vars', ivc) model.add_subsystem('parab_comp', Paraboloid()) model.connect('des_vars.x', 'parab_comp.x') model.connect('des_vars.y', 'parab_comp.y') prob = Problem(model) prob.setup() prob.run_model() print(prob['parab_comp.f_xy']) prob['des_vars.x'] = 5.0 prob['des_vars.y'] = -2.0 prob.run_model() print(prob['parab_comp.f_xy'])