def test_variable_access(self): prob = Problem(root=ExampleGroup()) # set with a different shaped array try: prob['G2.C1.x'] except Exception as err: msg = "'unknowns' has not been initialized, setup() must be called before 'G2.C1.x' can be accessed" self.assertEqual(text_type(err), msg) else: self.fail('Exception expected') prob.setup(check=False) self.assertEqual(prob['G2.C1.x'], 5.) # default output from IndepVarComp self.assertEqual(prob['G2.G1.C2.y'], 5.5) # output from ExecComp self.assertEqual(prob.root.G3.C3.params['x'], 0.) # initial value for a parameter self.assertEqual(prob.root.G2.G1.C2.params['x'], 0.) # initial value for a parameter prob = Problem(root=ExampleGroupWithPromotes()) prob.setup(check=False) self.assertEqual(prob.root.G2.G1.C2.params['x'], 0.) # initial value for a parameter # __setitem__ prob['G2.G1.C2.y'] = 99. self.assertEqual(prob['G2.G1.C2.y'], 99.)
def test_list_and_set_order(self): prob = Problem(root=ExampleGroupWithPromotes()) order1 = prob.root.list_order() self.assertEqual(order1, ['G2', 'G3']) # Big boy rules order2 = ['G3', 'G2'] prob.root.set_order(order2) order1 = prob.root.list_order() self.assertEqual(order1, ['G3', 'G2']) # Extra order2 = ['G3', 'G2', 'junk'] with self.assertRaises(ValueError) as cm: prob.root.set_order(order2) msg = "Unexpected new order. " msg += "The following are extra: ['junk']. " self.assertEqual(str(cm.exception), msg) # Missing order2 = ['G3'] with self.assertRaises(ValueError) as cm: prob.root.set_order(order2) msg = "Unexpected new order. " msg += "The following are missing: ['G2']. " self.assertEqual(str(cm.exception), msg) # Extra and Missing order2 = ['G3', 'junk'] with self.assertRaises(ValueError) as cm: prob.root.set_order(order2) msg = "Unexpected new order. " msg += "The following are missing: ['G2']. " msg += "The following are extra: ['junk']. " self.assertEqual(str(cm.exception), msg) # Dupes order2 = ['G3', 'G2', 'G3'] with self.assertRaises(ValueError) as cm: prob.root.set_order(order2) msg = "Duplicate name(s) found in order list: ['G3']" self.assertEqual(str(cm.exception), msg) # Don't let user call add. with self.assertRaises(RuntimeError) as cm: prob.root.add('C5', Group()) msg = 'You cannot call add after specifying an order.' self.assertEqual(str(cm.exception), msg)
def test_data_xfer(self): prob = Problem(root=ExampleGroupWithPromotes()) prob.setup(check=False) prob.root.unknowns['G2.G1.C2.y'] = 99. self.assertEqual(prob['G2.G1.C2.y'], 99.) prob.root._transfer_data('G3') self.assertEqual(prob.root.params['G3.C3.x'], 99.) self.assertEqual(prob['G3.C3.x'], 99.)
def test_variable_access(self): prob = Problem(root=ExampleGroup()) # try accessing variable value before setup() try: prob['G2.C1.x'] except Exception as err: msg = "'unknowns' has not been initialized, setup() must be called before 'G2.C1.x' can be accessed" self.assertEqual(text_type(err), msg) else: self.fail('Exception expected') prob.setup(check=False) # check that we can access values from unknowns (default) and params self.assertEqual(prob['G2.C1.x'], 5.) # default output from IndepVarComp self.assertEqual(prob['G2.G1.C2.y'], 5.5) # output from ExecComp self.assertEqual(prob.root.G3.C3.params['x'], 0.) # initial value for a parameter self.assertEqual(prob.root.G2.G1.C2.params['x'], 0.) # initial value for a parameter # now try same thing in a Group with promotes prob = Problem(root=ExampleGroupWithPromotes()) prob.setup(check=False) prob.root.G2.unknowns['x'] = 99. self.assertEqual(prob['G2.x'], 99.) self.assertEqual(prob.root.G2.G1.C2.params['x'], 0.) # initial value for a parameter # and make sure we get the correct value after a transfer prob.root.G2._transfer_data('G1') self.assertEqual(prob.root.G2.G1.C2.params['x'], 99.) # transferred value of parameter
def test_subsystem_access(self): prob = Problem(root=ExampleGroup()) root = prob.root self.assertEqual(root.G2, prob.root.G2) self.assertEqual(root.G2.C1, prob.root.C1) self.assertEqual(root.G2.G1, prob.root.G1) self.assertEqual(root.G2.G1.C2, prob.root.C2) self.assertEqual(root.G3, prob.root.G3) self.assertEqual(root.G3.C3, prob.root.C3) self.assertEqual(root.G3.C4, prob.root.C4) prob = Problem(root=ExampleGroupWithPromotes()) root = prob.root self.assertEqual(root.G2, prob.root.G2) self.assertEqual(root.G2.C1, prob.root.C1) self.assertEqual(root.G2.G1, prob.root.G1) self.assertEqual(root.G2.G1.C2, prob.root.C2) self.assertEqual(root.G3, prob.root.G3) self.assertEqual(root.G3.C3, prob.root.C3) self.assertEqual(root.G3.C4, prob.root.C4)
def test_list_states(self): top = Problem() root = top.root = Group() sub = root.add('sub', Group()) sub.add('comp', SimpleImplicitComp()) sub.ln_solver = ScipyGMRES() top.setup(check=False) top['sub.comp.z'] = 7.7 stream = cStringIO() root.list_states(stream=stream) self.assertTrue('sub.comp.z' in stream.getvalue()) self.assertTrue('Value: 7.7' in stream.getvalue()) self.assertTrue('Residual: 0.0' in stream.getvalue()) self.assertTrue('States in model:' in stream.getvalue()) stream = cStringIO() sub.list_states(stream=stream) self.assertTrue('comp.z' in stream.getvalue()) self.assertTrue('Value: 7.7' in stream.getvalue()) self.assertTrue('Residual: 0.0' in stream.getvalue()) self.assertTrue('sub.comp.z' not in stream.getvalue()) self.assertTrue('States in sub:' in stream.getvalue()) top = Problem() root = top.root = ExampleGroupWithPromotes() top.setup(check=False) stream = cStringIO() root.list_states(stream=stream) self.assertTrue('No states in model.' in stream.getvalue()) stream = cStringIO() root.G2.list_states(stream=stream) self.assertTrue('No states in G2.' in stream.getvalue()) class ArrayImplicitComp(Component): """ Needed to test arrays here. """ def __init__(self): super(ArrayImplicitComp, self).__init__() # Params self.add_param('x', np.zeros((3, 1))) # Unknowns self.add_output('y', np.zeros((3, 1))) # States self.add_state('z', 2.0 * np.ones((3, 1)), lower=1.5, upper=np.array([2.5, 2.6, 2.7])) def solve_nonlinear(self, params, unknowns, resids): pass def apply_nonlinear(self, params, unknowns, resids): """ Don't solve; just calculate the residual.""" pass def linearize(self, params, unknowns, resids): """Analytical derivatives.""" pass top = Problem() root = top.root = Group() root.add('comp', ArrayImplicitComp()) root.ln_solver.options['maxiter'] = 2 top.setup(check=False) stream = cStringIO() root.list_states(stream=stream) base = 'States in model:\n\ncomp.z\nValue: [[ 2.]\n [ 2.]\n [ 2.]]\nResidual: [[ 0.]\n [ 0.]\n [ 0.]]' self.assertTrue(base in stream.getvalue())
def test_promotes(self): root = ExampleGroupWithPromotes() prob = Problem(root=root) prob.setup(check=False) self.assertEqual(root.pathname, '') self.assertEqual(root.G3.pathname, 'G3') self.assertEqual(root.G2.pathname, 'G2') self.assertEqual(root.G1.pathname, 'G2.G1') # TODO: check for expected results from _setup_variables self.assertEqual(list(root.G1._params_dict.items()), [('G2.G1.C2.x', { 'shape': 1, 'pathname': 'G2.G1.C2.x', 'val': 3.0, 'top_promoted_name': 'G2.x', 'size': 1 })]) self.assertEqual(list(root.G1._unknowns_dict.items()), [('G2.G1.C2.y', { 'shape': 1, 'pathname': 'G2.G1.C2.y', 'val': 5.5, 'top_promoted_name': 'G2.G1.C2.y', 'size': 1 })]) self.assertEqual(list(root.G2._params_dict.items()), [('G2.G1.C2.x', { 'shape': 1, 'pathname': 'G2.G1.C2.x', 'val': 3.0, 'top_promoted_name': 'G2.x', 'size': 1 })]) self.assertEqual(list(root.G2._unknowns_dict.items()), [('G2.C1.x', { 'shape': 1, 'pathname': 'G2.C1.x', 'val': 5.0, 'top_promoted_name': 'G2.x', '_canset_': True, 'size': 1 }), ('G2.G1.C2.y', { 'shape': 1, 'pathname': 'G2.G1.C2.y', 'val': 5.5, 'top_promoted_name': 'G2.G1.C2.y', 'size': 1 })]) self.assertEqual(list(root.G3._params_dict.items()), [('G3.C3.x', { 'shape': 1, 'pathname': 'G3.C3.x', 'val': 3.0, 'top_promoted_name': 'G3.C3.x', 'size': 1 }), ('G3.C4.x', { 'shape': 1, 'pathname': 'G3.C4.x', 'val': 3.0, 'top_promoted_name': 'x', 'size': 1 })]) self.assertEqual(list(root.G3._unknowns_dict.items()), [('G3.C3.y', { 'shape': 1, 'pathname': 'G3.C3.y', 'val': 5.5, 'top_promoted_name': 'G3.C3.y', 'size': 1 }), ('G3.C4.y', { 'shape': 1, 'pathname': 'G3.C4.y', 'val': 5.5, 'top_promoted_name': 'G3.C4.y', 'size': 1 })]) self.assertEqual(list(root._params_dict.items()), [('G2.G1.C2.x', { 'shape': 1, 'pathname': 'G2.G1.C2.x', 'val': 3.0, 'top_promoted_name': 'G2.x', 'size': 1 }), ('G3.C3.x', { 'shape': 1, 'pathname': 'G3.C3.x', 'val': 3.0, 'top_promoted_name': 'G3.C3.x', 'size': 1 }), ('G3.C4.x', { 'shape': 1, 'pathname': 'G3.C4.x', 'val': 3.0, 'top_promoted_name': 'x', 'size': 1 })]) self.assertEqual(list(root._unknowns_dict.items()), [('G2.C1.x', { 'shape': 1, 'pathname': 'G2.C1.x', 'val': 5.0, 'top_promoted_name': 'G2.x', '_canset_': True, 'size': 1 }), ('G2.G1.C2.y', { 'shape': 1, 'pathname': 'G2.G1.C2.y', 'val': 5.5, 'top_promoted_name': 'G2.G1.C2.y', 'size': 1 }), ('G3.C3.y', { 'shape': 1, 'pathname': 'G3.C3.y', 'val': 5.5, 'top_promoted_name': 'G3.C3.y', 'size': 1 }), ('G3.C4.y', { 'shape': 1, 'pathname': 'G3.C4.y', 'val': 5.5, 'top_promoted_name': 'G3.C4.y', 'size': 1 })]) expected_root_params = ['G3.C3.x'] expected_root_unknowns = ['G2.x', 'G2.G1.C2.y', 'G3.C3.y', 'G3.C4.y'] expected_G3_params = ['C4.x', 'C3.x'] expected_G3_unknowns = ['C3.y', 'C4.y'] expected_G2_params = ['G1.C2.x'] expected_G2_unknowns = ['x', 'G1.C2.y'] expected_G1_params = ['C2.x'] expected_G1_unknowns = ['C2.y'] voi = None self.assertEqual(list(root.params.keys()), expected_root_params) self.assertEqual(list(root.dpmat[voi].keys()), expected_root_params) self.assertEqual(list(root.unknowns.keys()), expected_root_unknowns) self.assertEqual(list(root.dumat[voi].keys()), expected_root_unknowns) self.assertEqual(list(root.resids.keys()), expected_root_unknowns) self.assertEqual(list(root.drmat[voi].keys()), expected_root_unknowns) self.assertEqual(list(root.G3.params.keys()), expected_G3_params) self.assertEqual(list(root.G3.dpmat[voi].keys()), expected_G3_params) self.assertEqual(list(root.G3.unknowns.keys()), expected_G3_unknowns) self.assertEqual(list(root.G3.dumat[voi].keys()), expected_G3_unknowns) self.assertEqual(list(root.G3.resids.keys()), expected_G3_unknowns) self.assertEqual(list(root.G3.drmat[voi].keys()), expected_G3_unknowns) self.assertEqual(list(root.G2.params.keys()), expected_G2_params) self.assertEqual(list(root.G2.dpmat[voi].keys()), expected_G2_params) self.assertEqual(list(root.G2.unknowns.keys()), expected_G2_unknowns) self.assertEqual(list(root.G2.dumat[voi].keys()), expected_G2_unknowns) self.assertEqual(list(root.G2.resids.keys()), expected_G2_unknowns) self.assertEqual(list(root.G2.drmat[voi].keys()), expected_G2_unknowns) self.assertEqual(list(root.G1.params.keys()), expected_G1_params) self.assertEqual(list(root.G1.dpmat[voi].keys()), expected_G1_params) self.assertEqual(list(root.G1.unknowns.keys()), expected_G1_unknowns) self.assertEqual(list(root.G1.dumat[voi].keys()), expected_G1_unknowns) self.assertEqual(list(root.G1.resids.keys()), expected_G1_unknowns) self.assertEqual(list(root.G1.drmat[voi].keys()), expected_G1_unknowns) # verify subsystem is using shared view of parent unknowns vector root.unknowns['G2.x'] = 99. self.assertEqual(root.G2.unknowns['x'], 99.) # verify subsystem is getting correct metadata from parent unknowns vector self.assertEqual(root.unknowns.metadata('G2.x'), root.G2.unknowns.metadata('x'))