Skip to content

Commit

Permalink
Added a test for size mismatch.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Moore authored and Kenneth Moore committed Sep 30, 2020
1 parent a0893ac commit 4511de5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
13 changes: 12 additions & 1 deletion openmdao/core/group.py
Expand Up @@ -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 " + \
Expand Down
52 changes: 37 additions & 15 deletions openmdao/core/tests/test_group.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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 (<model>): 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):
Expand Down

0 comments on commit 4511de5

Please sign in to comment.