def __init__(self, memory=10, delay=0.001, plot_initial=True, plot_improvements_only=False, ax=None, legendloc=1): """Initialize component for plotting turbine locations Parameters ---------- memory : int, optional Number of previous iterations to remember delay : float, optional Time delay in seconds between plotting updates plot_initial : bool, optional Flag to plot the initial turbine locations plot_improvements_only : bool, optional Flag to plot only improvements in cost ax : matplotlib axes, optional Axes into which to make the plot legendloc : int Location of the legend in the plot """ ExplicitComponent.__init__(self) self.delay = delay self.plot_improvements_only = plot_improvements_only self._ax = ax self.memory = memory self.delay = max([delay, 1e-6]) self.plot_initial = plot_initial self.history = [] self.counter = 0 self.by_pass = False self.legendloc = legendloc
def __init__(self, n_wt, xy_boundary=None, z_boundary=None, **kwargs): sys.stderr.write( "%s is deprecated. Use BoundaryConstraint from topfarm.constraint_components.boundary instead\n" % self.__class__.__name__) ExplicitComponent.__init__(self, **kwargs) self.n_wt = n_wt if xy_boundary is None: self.xy_boundary = np.zeros((0, 2)) else: self.xy_boundary = np.array(xy_boundary) if z_boundary is None: z_boundary = [] if len(z_boundary) > 0: z_boundary = np.asarray(z_boundary) assert z_boundary.shape[-1] == 2 if len(z_boundary.shape) == 1: z_boundary = np.zeros((self.n_wt, 2)) + [z_boundary] assert z_boundary.shape == (self.n_wt, 2) assert np.all(z_boundary[:, 0] < z_boundary[:, 1]) self.z_boundary = z_boundary
def setup(self, component: om.ExplicitComponent, mission_name: str = None): """ To be used during setup() of provided OpenMDAO component. It adds input and output variables deduced from mission definition file. :param component: the OpenMDAO component where the setup is done. :param mission_name: mission name (can be omitted if only one mission is defined) """ if mission_name is None: mission_name = self.get_unique_mission_name() self.mission_name = mission_name input_definition = self.get_input_variables(mission_name) output_definition = self._identify_outputs(mission_name) output_definition = { name: value for name, value in output_definition.items() if name not in input_definition } for name, units in input_definition.items(): if name.endswith(":CD") or name.endswith(":CL"): component.add_input(name, np.nan, units=units, shape=POLAR_POINT_COUNT) else: component.add_input(name, np.nan, units=units) for name, (units, desc) in output_definition.items(): component.add_output(name, units=units, desc=desc)
def test_comp_has_no_outputs(self): p = Problem() root = p.model indep = root.add_subsystem("indep", IndepVarComp('x', 1.0)) comp1 = root.add_subsystem("comp1", ExplicitComponent()) comp1.add_input('x', val=0.) root.connect('indep.x', 'comp1.x') testlogger = TestLogger() p.setup(check=True, logger=testlogger) p.final_setup() expected = ("The following Components do not have any outputs:\n" " comp1\n") self.assertTrue(testlogger.contains('warning', expected))
def test_error_handling(self): """Test error handling when adding inputs/outputs.""" comp = ExplicitComponent() msg = "Incompatible shape for '.*': Expected (.*) but got (.*)" with self.assertRaisesRegex(ValueError, msg): comp.add_output('arr', val=np.ones((2, 2)), shape=([2])) with self.assertRaisesRegex(ValueError, msg): comp.add_input('arr', val=np.ones((2, 2)), shape=([2])) msg = "Shape of indices does not match shape for '.*': Expected (.*) but got (.*)" with self.assertRaisesRegex(ValueError, msg): comp.add_input('arr', val=np.ones((2, 2)), src_indices=[0, 1]) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'numpy.ndarray'>' was given") with self.assertRaisesRegex(TypeError, msg): comp.add_output('arr', shape=np.array([2.])) with self.assertRaisesRegex(TypeError, msg): comp.add_input('arr', shape=np.array([2.])) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'float'>' was given") with self.assertRaisesRegex(TypeError, msg): comp.add_output('arr', shape=2.) with self.assertRaisesRegex(TypeError, msg): comp.add_input('arr', shape=2.) # check that a numpy integer type is accepted for shape shapes = np.array([3], dtype=np.uint32) comp.add_output('aro', shape=shapes[0]) comp.add_input('ari', shape=shapes[0]) msg = 'The val argument should be a float, list, tuple, ndarray or Iterable' val = Component with self.assertRaisesRegex(TypeError, msg): comp.add_input('x', val=val) with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=val) msg = 'The src_indices argument should be an int, list, tuple, ndarray or Iterable' src = Component with self.assertRaisesRegex(TypeError, msg): comp.add_input('x', val=np.ones((2, 2)), src_indices=src) msg = 'The units argument should be a str or None' units = Component with self.assertRaisesRegex(TypeError, msg): comp.add_input('x', val=np.ones((2, 2)), units=units) with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=np.ones((2, 2)), units=units) msg = 'The ref argument should be a float, list, tuple, ndarray or Iterable' val = Component with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=5.0, ref=val) msg = 'The ref0 argument should be a float, list, tuple, ndarray or Iterable' val = Component with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=5.0, ref0=val) msg = 'The res_ref argument should be a float, list, tuple, ndarray or Iterable' val = Component with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=5.0, res_ref=val) msg = 'The res_units argument should be a str or None' units = Component with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=5.0, res_units=val)
def test_invalid_name(self): comp = ExplicitComponent() add_input_methods = [comp.add_input, comp.add_discrete_input] add_output_methods = [comp.add_output, comp.add_discrete_output] # Test some forbidden names. invalid_names = ['a.b', 'a*b', 'a?b', 'a!', '[a', 'b]'] invalid_error = "<class ExplicitComponent>: '%s' is not a valid %s name." nostr_names = [3, None, object, object()] nostr_error = "<class ExplicitComponent>: The name argument should be a string." empty_in_error = "<class ExplicitComponent>: '' is not a valid input name." empty_out_error = "<class ExplicitComponent>: '' is not a valid output name." for func in add_input_methods: for name in invalid_names: with self.assertRaises(NameError) as cm: func(name, val=5.0) self.assertEqual(str(cm.exception), invalid_error % (name, 'input')) for name in nostr_names: with self.assertRaises(TypeError) as cm: func(name, val=5.0) self.assertEqual(str(cm.exception), nostr_error) with self.assertRaises(NameError) as cm: func('', val=5.0) self.assertEqual(str(cm.exception), empty_in_error) for func in add_output_methods: for name in invalid_names: with self.assertRaises(NameError) as cm: func(name, val=5.0) self.assertEqual(str(cm.exception), invalid_error % (name, 'output')) for name in nostr_names: with self.assertRaises(TypeError) as cm: func(name, val=5.0) self.assertEqual(str(cm.exception), nostr_error) with self.assertRaises(NameError) as cm: func('', val=5.0) self.assertEqual(str(cm.exception), empty_out_error) # Stuff we allow. comp.add_input('a:b', val=5.0) comp.add_output('b:c', val=5.0) comp.add_input('x-y', val=5.0) comp.add_output('---', val=5.0) comp.add_output('-+=&$(;"<>@;^', val=5.0)
def test_error_handling(self): """Test error handling when adding inputs/outputs.""" comp = ExplicitComponent() msg = "Incompatible shape for '.*': Expected (.*) but got (.*)" with assertRaisesRegex(self, ValueError, msg): comp.add_output('arr', val=np.ones((2, 2)), shape=([2])) with assertRaisesRegex(self, ValueError, msg): comp.add_input('arr', val=np.ones((2, 2)), shape=([2])) msg = "Shape of indices does not match shape for '.*': Expected (.*) but got (.*)" with assertRaisesRegex(self, ValueError, msg): comp.add_input('arr', val=np.ones((2, 2)), src_indices=[0, 1]) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'numpy.ndarray'>' was given") with assertRaisesRegex(self, TypeError, msg): comp.add_output('arr', shape=np.array([2.])) with assertRaisesRegex(self, TypeError, msg): comp.add_input('arr', shape=np.array([2.])) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'float'>' was given") with assertRaisesRegex(self, TypeError, msg): comp.add_output('arr', shape=2.) with assertRaisesRegex(self, TypeError, msg): comp.add_input('arr', shape=2.) # check that a numpy integer type is accepted for shape shapes = np.array([3], dtype=np.uint32) comp.add_output('aro', shape=shapes[0]) comp.add_input('ari', shape=shapes[0]) msg = "The name argument should be a string" name = 3 with assertRaisesRegex(self, TypeError, msg): comp.add_input(name, val=np.ones((2, 2))) with assertRaisesRegex(self, TypeError, msg): comp.add_output(name, val=np.ones((2, 2))) msg = 'The val argument should be a float, list, tuple, ndarray or Iterable' val = Component with assertRaisesRegex(self, TypeError, msg): comp.add_input('x', val=val) with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=val) msg = 'The src_indices argument should be an int, list, tuple, ndarray or Iterable' src = Component with assertRaisesRegex(self, TypeError, msg): comp.add_input('x', val=np.ones((2, 2)), src_indices=src) msg = 'The units argument should be a str or None' units = Component with assertRaisesRegex(self, TypeError, msg): comp.add_input('x', val=np.ones((2, 2)), units=units) with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=np.ones((2, 2)), units=units) msg = 'The ref argument should be a float, list, tuple, ndarray or Iterable' val = Component with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=5.0, ref=val) msg = 'The ref0 argument should be a float, list, tuple, ndarray or Iterable' val = Component with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=5.0, ref0=val) msg = 'The res_ref argument should be a float, list, tuple, ndarray or Iterable' val = Component with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=5.0, res_ref=val) msg = 'The res_units argument should be a str or None' units = Component with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=5.0, res_units=val) # Test some forbidden names. msg = "'x.y' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input('x.y', val=5.0) msg = "'*' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input('*', val=5.0) msg = "'?' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input('?', val=5.0) msg = "'\[' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input('[', val=5.0) msg = "'\]' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input(']', val=5.0) msg = "'x.y' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output('x.y', val=5.0) msg = "'*' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output('*', val=5.0) msg = "'?' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output('?', val=5.0) msg = "'\[' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output('[', val=5.0) msg = "'\]' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output(']', val=5.0) # Stuff we allow. comp.add_input('a:b', val=5.0) comp.add_output('b:c', val=5.0) comp.add_input('x-y', val=5.0) comp.add_output('---', val=5.0) comp.add_output('-+=&$(;"<>@;^', val=5.0)
def test_error_handling(self): """Test error handling when adding inputs/outputs.""" comp = ExplicitComponent() msg = "Incompatible shape for '.*': Expected (.*) but got (.*)" with assertRaisesRegex(self, ValueError, msg): comp.add_output('arr', val=np.ones((2, 2)), shape=([2])) with assertRaisesRegex(self, ValueError, msg): comp.add_input('arr', val=np.ones((2, 2)), shape=([2])) msg = "Shape of indices does not match shape for '.*': Expected (.*) but got (.*)" with assertRaisesRegex(self, ValueError, msg): comp.add_input('arr', val=np.ones((2, 2)), src_indices=[0, 1]) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'numpy.ndarray'>' was given") with assertRaisesRegex(self, TypeError, msg): comp.add_output('arr', shape=np.array([2.])) with assertRaisesRegex(self, TypeError, msg): comp.add_input('arr', shape=np.array([2.])) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'float'>' was given") with assertRaisesRegex(self, TypeError, msg): comp.add_output('arr', shape=2.) with assertRaisesRegex(self, TypeError, msg): comp.add_input('arr', shape=2.) # check that a numpy integer type is accepted for shape shapes = np.array([3], dtype=np.uint32) comp.add_output('aro', shape=shapes[0]) comp.add_input('ari', shape=shapes[0]) msg = "The name argument should be a string" name = 3 with assertRaisesRegex(self, TypeError, msg): comp.add_input(name, val=np.ones((2, 2))) with assertRaisesRegex(self, TypeError, msg): comp.add_output(name, val=np.ones((2, 2))) msg = 'The val argument should be a float, list, tuple, ndarray or Iterable' val = Component with assertRaisesRegex(self, TypeError, msg): comp.add_input('x', val=val) with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=val) msg = 'The src_indices argument should be an int, list, tuple, ndarray or Iterable' src = Component with assertRaisesRegex(self, TypeError, msg): comp.add_input('x', val=np.ones((2, 2)), src_indices=src) msg = 'The units argument should be a str or None' units = Component with assertRaisesRegex(self, TypeError, msg): comp.add_input('x', val=np.ones((2, 2)), units=units) with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=np.ones((2, 2)), units=units) msg = 'The ref argument should be a float, list, tuple, ndarray or Iterable' val = Component with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=5.0, ref=val) msg = 'The ref0 argument should be a float, list, tuple, ndarray or Iterable' val = Component with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=5.0, ref0=val) msg = 'The res_ref argument should be a float, list, tuple, ndarray or Iterable' val = Component with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=5.0, res_ref=val) msg = 'The res_units argument should be a str or None' units = Component with assertRaisesRegex(self, TypeError, msg): comp.add_output('x', val=5.0, res_units=val) # Test some forbidden names. msg = "'x.y' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input('x.y', val=5.0) msg = "'*' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input('*', val=5.0) msg = "'?' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input('?', val=5.0) msg = r"'\[' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input('[', val=5.0) msg = r"'\]' is not a valid input name." with assertRaisesRegex(self, NameError, msg): comp.add_input(']', val=5.0) msg = "'x.y' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output('x.y', val=5.0) msg = "'*' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output('*', val=5.0) msg = "'?' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output('?', val=5.0) msg = r"'\[' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output('[', val=5.0) msg = r"'\]' is not a valid output name." with assertRaisesRegex(self, NameError, msg): comp.add_output(']', val=5.0) # Stuff we allow. comp.add_input('a:b', val=5.0) comp.add_output('b:c', val=5.0) comp.add_input('x-y', val=5.0) comp.add_output('---', val=5.0) comp.add_output('-+=&$(;"<>@;^', val=5.0)
def __init__(self, problem): ExplicitComponent.__init__(self) self.problem = problem
def __init__(self, problem, additional_inputs=[]): ExplicitComponent.__init__(self) self.problem = problem self.additional_inputs = additional_inputs
def test_error_handling(self): """Test error handling when adding inputs/outputs.""" comp = ExplicitComponent() msg = "Incompatible shape for '.*': Expected (.*) but got (.*)" with self.assertRaisesRegex(ValueError, msg): comp.add_output('arr', val=np.ones((2, 2)), shape=([2])) with self.assertRaisesRegex(ValueError, msg): comp.add_input('arr', val=np.ones((2, 2)), shape=([2])) with self.assertRaises(ValueError) as cm: comp.add_input('arr', val=np.ones((2, 2)), src_indices=[0, 1], flat_src_indices=True) msg = "Shape of indices (2,) does not match shape of (2, 2) for 'arr'." self.assertEqual(str(cm.exception), msg) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'numpy.ndarray'>' was given") with self.assertRaisesRegex(TypeError, msg): comp.add_output('arr', shape=np.array([2.])) with self.assertRaisesRegex(TypeError, msg): comp.add_input('arr', shape=np.array([2.])) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'float'>' was given") with self.assertRaisesRegex(TypeError, msg): comp.add_output('arr', shape=2.) with self.assertRaisesRegex(TypeError, msg): comp.add_input('arr', shape=2.) # check that a numpy integer type is accepted for shape shapes = np.array([3], dtype=np.uint32) comp.add_output('aro', shape=shapes[0]) comp.add_input('ari', shape=shapes[0]) msg = 'The val argument should be a float, list, tuple, ndarray or Iterable' val = Component with self.assertRaisesRegex(TypeError, msg): comp.add_input('x', val=val) with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=val) msg = "When specifying src_indices for input 'x': Can't create an index array " \ "using indices of non-integral type 'object_'." src = Component with self.assertRaisesRegex(TypeError, msg): comp.add_input('x', val=np.ones((2, 2)), src_indices=src) msg = 'The units argument should be a str or None' units = Component with self.assertRaisesRegex(TypeError, msg): comp.add_input('x', val=np.ones((2, 2)), units=units) with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=np.ones((2, 2)), units=units) msg = 'The ref argument should be a float, list, tuple, ndarray or Iterable' val = Component with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=5.0, ref=val) msg = 'The ref0 argument should be a float, list, tuple, ndarray or Iterable' val = Component with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=5.0, ref0=val) msg = 'The res_ref argument should be a float, list, tuple, ndarray or Iterable' val = Component with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=5.0, res_ref=val) msg = 'The res_units argument should be a str or None' units = Component with self.assertRaisesRegex(TypeError, msg): comp.add_output('x', val=5.0, res_units=val)
def test_error_handling(self): """Test error handling when adding inputs/outputs.""" comp = ExplicitComponent() msg = "Incompatible shape for '.*': Expected (.*) but got (.*)" with assertRaisesRegex(self, ValueError, msg): comp.add_output('arr', val=np.ones((2,2)), shape=([2])) with assertRaisesRegex(self, ValueError, msg): comp.add_input('arr', val=np.ones((2,2)), shape=([2])) msg = "Shape of indices does not match shape for '.*': Expected (.*) but got (.*)" with assertRaisesRegex(self, ValueError, msg): comp.add_input('arr', val=np.ones((2,2)), src_indices=[0,1]) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'numpy.ndarray'>' was given") with assertRaisesRegex(self, TypeError, msg): comp.add_output('arr', shape=np.array([2.])) with assertRaisesRegex(self, TypeError, msg): comp.add_input('arr', shape=np.array([2.])) msg = ("The shape argument should be an int, tuple, or list " "but a '<(.*) 'float'>' was given") with assertRaisesRegex(self, TypeError, msg): comp.add_output('arr', shape=2.) with assertRaisesRegex(self, TypeError, msg): comp.add_input('arr', shape=2.) # check that a numpy integer type is accepted for shape shapes = np.array([3], dtype=np.uint32) comp.add_output('aro', shape=shapes[0]) comp.add_input('ari', shape=shapes[0])