diff --git a/openmdao/core/group.py b/openmdao/core/group.py index 06545c6a98..347ec64a3a 100644 --- a/openmdao/core/group.py +++ b/openmdao/core/group.py @@ -1217,8 +1217,19 @@ def _setup_global_connections(self, conns=None): meta = abs2meta[abs_in] if meta['src_indices'] is not None: if flat_src_indices and meta['flat_src_indices']: + # Flat-to-flat src_indices can be cascaded. - src_indices = src_indices[meta['src_indices']] + try: + src_indices = src_indices[meta['src_indices']] + except IndexError: + msg = f"{self.msginfo}: flat src_indices in connect and " + \ + f"promotes are incompatible for connection from " + \ + f"'{prom_out}' to '{prom_in}'." + if self._raise_connection_errors: + raise RuntimeError(msg) + else: + simple_warning(msg) + continue else: msg = f"{self.msginfo}: src_indices has been defined in both " + \ f"connect('{prom_out}', '{prom_in}') and " + \ diff --git a/openmdao/core/tests/test_group.py b/openmdao/core/tests/test_group.py index 0eaf53ac3c..74074796dd 100644 --- a/openmdao/core/tests/test_group.py +++ b/openmdao/core/tests/test_group.py @@ -110,6 +110,23 @@ def compute(self, inputs, outputs): outputs['y'] = np.sum(inputs['x'])**2.0 +class Inner(om.Group): + + def setup(self): + comp = om.ExecComp('y=2*x', x=np.zeros((3, 2)), y=np.zeros((3, 2))) + self.add_subsystem('comp', comp) + + +class Outer(om.Group): + + def setup(self): + self.add_subsystem('inner', Inner()) + + def configure(self): + self.promotes('inner', inputs=[('comp.x', 'desvar:x')], + src_indices=np.array([[0, 1], [0, 1], [0, 1]]), flat_src_indices=True) + + class TestGroup(unittest.TestCase): def test_add_subsystem_class(self): @@ -2030,21 +2047,6 @@ def configure(self): def test_flat_src_indices_promote_connect(self): - class Inner(om.Group): - - def setup(self): - comp = om.ExecComp('y=2*x', x=np.zeros((3, 2)), y=np.zeros((3, 2))) - self.add_subsystem('comp', comp) - - class Outer(om.Group): - - def setup(self): - self.add_subsystem('inner', Inner()) - - def configure(self): - self.promotes('inner', inputs=[('comp.x', 'desvar:x')], - src_indices=np.array([[0, 1], [0, 1], [0, 1]]), flat_src_indices=True) - prob = om.Problem() model = prob.model @@ -2064,6 +2066,26 @@ def configure(self): [15., 27.]]) assert_near_equal(prob.get_val('outer.desvar:x'), expected, 1e-6) + def test_flat_src_indices_promote_connect_size_error(self): + + prob = om.Problem() + model = prob.model + + comp = om.ExecComp('y=3*x', x=np.zeros((7)), y=np.zeros((7))) + model.add_subsystem('src', comp) + model.add_subsystem('outer', Outer()) + + model.connect('src.y', 'outer.desvar:x', src_indices=[2], flat_src_indices=True) + + with self.assertRaises(RuntimeError) as context: + prob.setup() + + msg = "Group (): flat src_indices in connect and " + \ + "promotes are incompatible for connection from " + \ + "'src.y' to 'outer.desvar:x'." + + self.assertEqual(str(context.exception), msg) + class MyComp(om.ExplicitComponent): def __init__(self, input_shape, src_indices=None, flat_src_indices=False):