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_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])
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_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 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)