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 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 get_cd_trim(cl): ivc = IndepVarComp() ivc.add_output( "data:aerodynamics:aircraft:cruise:CL", 150 * [cl] ) # needed because size of input array is fixed problem = run_system(CdTrim(), ivc) return problem["data:aerodynamics:aircraft:cruise:CD:trim"][0]
def test_compute_with_provided_path(): """ Test that option "use_exe_path" works """ ivc = IndepVarComp() ivc.add_output("xfoil:reynolds", 1e6) ivc.add_output("xfoil:mach", 0.20) # Clear saved polar results if pth.exists( pth.join(resources.__path__[0], _DEFAULT_AIRFOIL_FILE.replace('af', 'csv'))): os.remove( pth.join(resources.__path__[0], _DEFAULT_AIRFOIL_FILE.replace('af', 'csv'))) if pth.exists( pth.join(resources.__path__[0], _DEFAULT_AIRFOIL_FILE.replace('.af', '_sym.csv'))): os.remove( pth.join(resources.__path__[0], _DEFAULT_AIRFOIL_FILE.replace('.af', '_sym.csv'))) xfoil_comp = XfoilPolar(alpha_start=0.0, alpha_end=20.0, iter_limit=20) xfoil_comp.options["xfoil_exe_path"] = "Dummy" # bad name with pytest.raises(ValueError): _ = run_system(xfoil_comp, ivc) xfoil_comp.options["xfoil_exe_path"] = ( xfoil_path if xfoil_path else pth.join( pth.dirname(__file__), pth.pardir, "xfoil699", "xfoil.exe")) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.48, 1e-2)
def get_cd_compressibility(mach, cl): ivc = IndepVarComp() ivc.add_output("data:aerodynamics:aircraft:cruise:CL", 150 * [cl]) # needed because size of input array is fixed ivc.add_output("data:TLAR:cruise_mach", mach) problem = run_system(CdCompressibility(), ivc) return problem["data:aerodynamics:aircraft:cruise:CD:compressibility"][ 0]
def get_cd_compressibility(mach, cl, sweep, thickness_ratio, delta_charac_mach=0.0): ivc = IndepVarComp() ivc.add_output( "data:aerodynamics:aircraft:cruise:CL", 150 * [cl] ) # needed because size of input array is fixed ivc.add_output("data:TLAR:cruise_mach", mach) ivc.add_output("data:geometry:wing:sweep_25", sweep, units="deg") ivc.add_output("data:geometry:wing:thickness_ratio", thickness_ratio) ivc.add_output( "tuning:aerodynamics:aircraft:cruise:CD:compressibility:characteristic_mach_increment", delta_charac_mach, ) problem = run_system(CdCompressibility(), ivc) return problem["data:aerodynamics:aircraft:cruise:CD:compressibility"][0]
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_compute(): """ Tests a simple XFOIL run""" if pth.exists(XFOIL_RESULTS): shutil.rmtree(XFOIL_RESULTS) ivc = IndepVarComp() ivc.add_output("xfoil:reynolds", 1e6, units="m**-1") ivc.add_output("xfoil:mach", 0.60) xfoil_comp = XfoilPolar(alpha_start=0.0, alpha_end=15.0, iter_limit=100, symmetrical=True, xfoil_exe_path=xfoil_path) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.31, 1e-2) assert problem["xfoil:CL_min_2D"] == pytest.approx(-0.59, 1e-2) assert not pth.exists(XFOIL_RESULTS) # Deactivate warnings for wished crash of xfoil warnings.simplefilter("ignore") xfoil_comp = XfoilPolar(alpha_start=50.0, alpha_end=60.0, iter_limit=2, xfoil_exe_path=xfoil_path) # will not converge problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(DEFAULT_2D_CL_MAX, 1e-2) assert problem["xfoil:CL_min_2D"] == pytest.approx(DEFAULT_2D_CL_MIN, 1e-2) assert not pth.exists(XFOIL_RESULTS) # Reactivate warnings warnings.simplefilter("default") xfoil_comp = XfoilPolar(iter_limit=20, result_folder_path=XFOIL_RESULTS, xfoil_exe_path=xfoil_path) run_system(xfoil_comp, ivc) assert pth.exists(XFOIL_RESULTS) assert pth.exists(pth.join(XFOIL_RESULTS, "polar_result.txt")) # remove folder if pth.exists(XFOIL_RESULTS): shutil.rmtree(XFOIL_RESULTS)
def test_compute(): """ Tests a simple XFOIL run""" if pth.exists(XFOIL_RESULTS): shutil.rmtree(XFOIL_RESULTS) ivc = IndepVarComp() ivc.add_output("xfoil:unit_reynolds", 18000000) ivc.add_output("xfoil:mach", 0.80) ivc.add_output("data:geometry:wing:thickness_ratio", 0.1284) ivc.add_output("xfoil:length", 1.0, units="m") xfoil_comp = XfoilPolar(alpha_start=15.0, alpha_end=25.0, iter_limit=20, xfoil_exe_path=xfoil_path) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.9, 1e-2) assert not pth.exists(XFOIL_RESULTS) # Deactivate warnings for wished crash of xfoil warnings.simplefilter("ignore") xfoil_comp = XfoilPolar( alpha_start=12.0, alpha_end=20.0, iter_limit=20, xfoil_exe_path=xfoil_path) # will stop before real max CL problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.9, 1e-2) assert not pth.exists(XFOIL_RESULTS) xfoil_comp = XfoilPolar(alpha_start=50.0, alpha_end=55.0, iter_limit=2, xfoil_exe_path=xfoil_path) # will not converge problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(DEFAULT_2D_CL_MAX, 1e-2) assert not pth.exists(XFOIL_RESULTS) # Reactivate warnings warnings.simplefilter("default") xfoil_comp = XfoilPolar(iter_limit=20, result_folder_path=XFOIL_RESULTS, xfoil_exe_path=xfoil_path) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.9, 1e-2) assert pth.exists(XFOIL_RESULTS) assert pth.exists(pth.join(XFOIL_RESULTS, "polar_result.txt")) # remove folder if pth.exists(XFOIL_RESULTS): shutil.rmtree(XFOIL_RESULTS)
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_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)
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)
def test_compute_with_provided_path(): """ Test that option "use_exe_path" works """ ivc = IndepVarComp() ivc.add_output("xfoil:unit_reynolds", 18000000) ivc.add_output("xfoil:mach", 0.20) ivc.add_output("data:geometry:wing:thickness_ratio", 0.1284) ivc.add_output("xfoil:length", 1.0, units="m") xfoil_comp = XfoilPolar(alpha_start=18.0, alpha_end=21.0, iter_limit=20) xfoil_comp.options["xfoil_exe_path"] = "Dummy" # bad name with pytest.raises(ValueError): _ = run_system(xfoil_comp, ivc) xfoil_comp.options["xfoil_exe_path"] = ( xfoil_path if xfoil_path else pth.join( pth.dirname(__file__), pth.pardir, "xfoil699", "xfoil.exe")) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.85, 1e-2)
def test_compute(): """ Tests a simple XFOIL run""" if pth.exists(XFOIL_RESULTS): shutil.rmtree(XFOIL_RESULTS) ivc = IndepVarComp() ivc.add_output("xfoil:reynolds", 18000000) ivc.add_output("xfoil:mach", 0.20) ivc.add_output("data:geometry:wing:thickness_ratio", 0.1284) # Base test ---------------------------------------------------------------- xfoil_comp = XfoilPolar(alpha_start=15.0, alpha_end=25.0, iter_limit=20, xfoil_exe_path=xfoil_path) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.94, 1e-2) assert not pth.exists(XFOIL_RESULTS) # Test that will stop before real max CL ----------------------------------- xfoil_comp = XfoilPolar(alpha_start=12.0, alpha_end=20.0, iter_limit=20, xfoil_exe_path=xfoil_path) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.92, 1e-2) assert not pth.exists(XFOIL_RESULTS) # Test that will not converge ---------------------------------------------- xfoil_comp = XfoilPolar(alpha_start=50.0, alpha_end=55.0, iter_limit=2, xfoil_exe_path=xfoil_path) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(DEFAULT_2D_CL_MAX, 1e-2) assert not pth.exists(XFOIL_RESULTS) # Test that will output results in provided folder ------------------------- xfoil_comp = XfoilPolar(iter_limit=20, result_folder_path=XFOIL_RESULTS, xfoil_exe_path=xfoil_path) problem = run_system(xfoil_comp, ivc) assert problem["xfoil:CL_max_2D"] == pytest.approx(1.94, 1e-2) assert pth.exists(XFOIL_RESULTS) assert pth.exists(pth.join(XFOIL_RESULTS, "polar_result.txt"))
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)
""" x = inputs['x'] 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()
Optimal solution (minimum): x = 6.6667; y = -7.3333 """ 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()
def __init__(self, ode_class, time_options, state_options, control_options, polynomial_control_options, design_parameter_options, input_parameter_options, traj_parameter_options, ode_init_kwargs=None): # Get the state vector. This isn't necessarily ordered # so just pick the default ordering and go with it. self.state_options = OrderedDict() self.time_options = time_options self.control_options = control_options self.polynomial_control_options = polynomial_control_options self.design_parameter_options = design_parameter_options self.input_parameter_options = input_parameter_options self.traj_parameter_options = traj_parameter_options self.control_interpolants = {} self.polynomial_control_interpolants = {} pos = 0 for state, options in iteritems(state_options): self.state_options[state] = { 'rate_source': options['rate_source'], 'pos': pos, 'shape': options['shape'], 'size': np.prod(options['shape']), 'units': options['units'], 'targets': options['targets'] } pos += self.state_options[state]['size'] self._state_vec = np.zeros(pos, dtype=float) self._state_rate_vec = np.zeros(pos, dtype=float) # # Build odeint problem interface # self.prob = Problem(model=Group()) model = self.prob.model # The time IVC ivc = IndepVarComp() time_units = self.time_options['units'] ivc.add_output('time', val=0.0, units=time_units) ivc.add_output('time_phase', val=-88.0, units=time_units) ivc.add_output('t_initial', val=-99.0, units=time_units) ivc.add_output('t_duration', val=-111.0, units=time_units) model.add_subsystem('time_input', ivc, promotes_outputs=['*']) model.connect( 'time', ['ode.{0}'.format(tgt) for tgt in self.time_options['targets']]) model.connect('time_phase', [ 'ode.{0}'.format(tgt) for tgt in self.time_options['time_phase_targets'] ]) model.connect('t_initial', [ 'ode.{0}'.format(tgt) for tgt in self.time_options['t_initial_targets'] ]) model.connect('t_duration', [ 'ode.{0}'.format(tgt) for tgt in self.time_options['t_duration_targets'] ]) # The States Comp for name, options in iteritems(self.state_options): ivc.add_output('states:{0}'.format(name), shape=(1, np.prod(options['shape'])), units=options['units']) rate_src = self._get_rate_source_path(name) model.connect( rate_src, 'state_rate_collector.state_rates_in:{0}_rate'.format(name)) if options['targets'] is not None: model.connect( 'states:{0}'.format(name), ['ode.{0}'.format(tgt) for tgt in options['targets']]) if self.control_options or self.polynomial_control_options: self._interp_comp = \ ODEIntControlInterpolationComp(time_units=time_units, control_options=self.control_options, polynomial_control_options=self.polynomial_control_options) self._interp_comp.control_interpolants = self.control_interpolants self._interp_comp.polynomial_control_interpolants = self.polynomial_control_interpolants model.add_subsystem('indep_controls', self._interp_comp, promotes_outputs=['*']) model.connect('time', ['indep_controls.time']) if self.control_options: for name, options in iteritems(self.control_options): if options['targets']: model.connect( 'controls:{0}'.format(name), ['ode.{0}'.format(tgt) for tgt in options['targets']]) if options['rate_targets']: model.connect('control_rates:{0}_rate'.format(name), [ 'ode.{0}'.format(tgt) for tgt in options['rate_targets'] ]) if options['rate2_targets']: model.connect('control_rates:{0}_rate2'.format(name), [ 'ode.{0}'.format(tgt) for tgt in options['rate2_targets'] ]) if self.polynomial_control_options: for name, options in iteritems(self.polynomial_control_options): tgts = options['targets'] rate_tgts = options['rate_targets'] rate2_tgts = options['rate2_targets'] if options['targets']: if isinstance(tgts, string_types): tgts = [tgts] model.connect('polynomial_controls:{0}'.format(name), ['ode.{0}'.format(tgt) for tgt in tgts]) if options['rate_targets']: if isinstance(rate_tgts, string_types): rate_tgts = [rate_tgts] model.connect( 'polynomial_control_rates:{0}_rate'.format(name), ['ode.{0}'.format(tgt) for tgt in rate_tgts]) if options['rate2_targets']: if isinstance(rate2_tgts, string_types): rate2_tgts = [rate2_tgts] model.connect( 'polynomial_control_rates:{0}_rate2'.format(name), ['ode.{0}'.format(tgt) for tgt in rate2_tgts]) if self.design_parameter_options: for name, options in iteritems(self.design_parameter_options): ivc.add_output('design_parameters:{0}'.format(name), shape=options['shape'], units=options['units']) if options['targets'] is not None: tgts = options['targets'] if isinstance(tgts, string_types): tgts = [tgts] model.connect('design_parameters:{0}'.format(name), ['ode.{0}'.format(tgt) for tgt in tgts]) if self.input_parameter_options: for name, options in iteritems(self.input_parameter_options): ivc.add_output('input_parameters:{0}'.format(name), shape=options['shape'], units=options['units']) if options['targets'] is not None: tgts = options['targets'] if isinstance(tgts, string_types): tgts = [tgts] model.connect('input_parameters:{0}'.format(name), ['ode.{0}'.format(tgt) for tgt in tgts]) if self.traj_parameter_options: for name, options in iteritems(self.traj_parameter_options): ivc.add_output('traj_parameters:{0}'.format(name), shape=options['shape'], units=options['units']) if options['targets'] is not None: tgts = options['targets'] if isinstance(tgts, string_types): tgts = [tgts] model.connect('traj_parameters:{0}'.format(name), ['ode.{0}'.format(tgt) for tgt in tgts]) # The ODE System if ode_class is not None: model.add_subsystem('ode', subsys=ode_class(num_nodes=1, **ode_init_kwargs)) # The state rate collector comp self.prob.model.add_subsystem( 'state_rate_collector', StateRateCollectorComp(state_options=self.state_options, time_units=time_options['units'])) # Flag that is set to true if has_controls is called self._has_dynamic_controls = False
def __init__(self, phase_name, ode_class, time_options, state_options, control_options, design_parameter_options, input_parameter_options, ode_init_kwargs=None): self.phase_name = phase_name self.prob = Problem(model=Group()) self.ode = ode # The ODE System self.prob.model.add_subsystem('ode', subsys=ode_class(num_nodes=1, **ode_init_kwargs)) self.ode_options = ode_class.ode_options # Get the state vector. This isn't necessarily ordered # so just pick the default ordering and go with it. self.state_options = OrderedDict() self.time_options = time_options self.control_options = control_options self.design_parameter_options = design_parameter_options self.input_parameter_options = input_parameter_options pos = 0 for state, options in iteritems(state_options): self.state_options[state] = { 'rate_source': options['rate_source'], 'pos': pos, 'shape': options['shape'], 'size': np.prod(options['shape']), 'units': options['units'], 'targets': options['targets'] } pos += self.state_options[state]['size'] self._state_vec = np.zeros(pos, dtype=float) self._state_rate_vec = np.zeros(pos, dtype=float) # The Time Comp self.prob.model.add_subsystem( 'time_input', IndepVarComp('time', val=0.0, units=self.ode_options._time_options['units']), promotes_outputs=['time']) if self.ode_options._time_options['targets'] is not None: self.prob.model.connect('time', [ 'ode.{0}'.format(tgt) for tgt in self.ode_options._time_options['targets'] ]) # The States Comp indep = IndepVarComp() for name, options in iteritems(self.state_options): indep.add_output('states:{0}'.format(name), shape=(1, np.prod(options['shape'])), units=options['units']) if options['targets'] is not None: self.prob.model.connect( 'states:{0}'.format(name), ['ode.{0}'.format(tgt) for tgt in options['targets']]) self.prob.model.connect( 'ode.{0}'.format(options['rate_source']), 'state_rate_collector.state_rates_in:{0}_rate'.format(name)) self.prob.model.add_subsystem('indep_states', subsys=indep, promotes_outputs=['*']) # The Control interpolation comp time_units = self.ode_options._time_options['units'] self._interp_comp = ControlInterpolationComp( time_units=time_units, control_options=control_options) # The state rate collector comp self.prob.model.add_subsystem( 'state_rate_collector', StateRateCollectorComp(state_options=self.state_options, time_units=time_options['units'])) # Flag that is set to true if has_controls is called self._has_dynamic_controls = False
Rhub = initial['planform']['hub_radius'] z = np.array( initial['planform']['Z'] ) # no x,y for now so not worrying about coordinate transformation. r = Rhub + z[1:-1] # chop off hub/tip (loads are zero) Rtip = Rhub + z[-1] # load other constraints Tfactor = initial['optimization']['Constraints']['Rotor_Thrust'][ 'Upper_Limit'] Bfwfactor = initial['optimization']['Constraints'][ 'Root_Flap_Wise_Bending_Moment']['Upper_Limit'] # create an input component ivc = IndepVarComp() ivc.add_output('r', r) ivc.add_output('Rhub', Rhub) ivc.add_output('Rtip', Rtip) ivc.add_output('hubHt', 0.0) # irrelevant if no shear ivc.add_output('precone', 0.0) ivc.add_output('tilt', 0.0) ivc.add_output('yaw', 0.0) ivc.add_output('B', 3) # never explicitly stated ivc.add_output('rho', initial['environment']['Air_Density']) ivc.add_output('mu', 0.0000181206) # irrelevant if no Re dependency ivc.add_output('shearExp', 0.0) ivc.add_output('nSector', 1) # ivc.add_output('blend', blendingweight) ivc.add_output('tmin', tmin) n -= 2 # removed hub/tip