def test_fd_params(self): # tests retrieval of a list of any internal params whose source is either # a IndepVarComp or is outside of the Group prob = Problem(root=ExampleGroup()) prob.setup(check=False) root = prob.root self.assertEqual(root._get_fd_params(), ['G2.G1.C2.x']) self.assertEqual(root.G2._get_fd_params(), ['G1.C2.x']) self.assertEqual(root.G2.G1._get_fd_params(), ['C2.x']) self.assertEqual(root.G3._get_fd_params(), ['C3.x']) self.assertEqual(root.G3.C3._get_fd_params(), ['x']) self.assertEqual(root.G2.G1.C2._get_fd_params(), ['x'])
def test_subsolver_records_metadata(self): prob = Problem() prob.root = ExampleGroup() prob.root.G2.G1.nl_solver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = True prob.setup(check=False) prob.cleanup() # closes recorders expected_params = list(iteritems(prob.root.params)) expected_unknowns = list(iteritems(prob.root.unknowns)) expected_resids = list(iteritems(prob.root.resids)) self.assertMetadataRecorded( (expected_params, expected_unknowns, expected_resids))
def test_multilevel_record(self): prob = Problem() prob.root = ExampleGroup() prob.root.G2.G1.nl_solver.add_recorder(self.recorder) prob.driver.add_recorder(self.recorder) self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) self.recorder.close() solver_coordinate = ['Driver', (1,), "root", (1,), "G2", (1,), "G1", (1,)] g1_expected_params = [ ("C2.x", 5.0) ] g1_expected_unknowns = [ ("C2.y", 10.0) ] g1_expected_resids = [ ("C2.y", 0.0) ] g1_expected = (g1_expected_params, g1_expected_unknowns, g1_expected_resids) driver_coordinate = ['Driver', (1,)] driver_expected_params = [ ("G3.C3.x", 10.0) ] driver_expected_unknowns = [ ("G2.C1.x", 5.0), ("G2.G1.C2.y", 10.0), ("G3.C3.y", 20.0), ("G3.C4.y", 40.0), ] driver_expected_resids = [ ("G2.C1.x", 0.0), ("G2.G1.C2.y", 0.0), ("G3.C3.y", 0.0), ("G3.C4.y", 0.0), ] expected = [] expected.append((solver_coordinate, (t0, t1), g1_expected_params, g1_expected_unknowns, g1_expected_resids)) expected.append((driver_coordinate, (t0, t1), driver_expected_params, driver_expected_unknowns, driver_expected_resids)) self.assertIterationDataRecorded(expected, self.eps)
def test_basic_run(self): prob = Problem(root=ExampleGroup()) prob.setup(check=False) prob.run() self.assertAlmostEqual(prob['G3.C4.y'], 40.) stream = cStringIO() # get test coverage for list_connections and make sure it # doesn't barf prob.root.list_connections(stream=stream) prob.root.list_connections(unconnected=False, stream=stream) prob.root.list_connections(group_by_comp=False, stream=stream)
def test_fd_unknowns(self): # tests retrieval of a list of any internal unknowns with IndepVarComp # variables filtered out. prob = Problem(root=ExampleGroup()) prob.setup(check=False) root = prob.root self.assertEqual(root._get_fd_unknowns(), ['G2.G1.C2.y', 'G3.C3.y', 'G3.C4.y']) self.assertEqual(root.G2._get_fd_unknowns(), ['G1.C2.y']) self.assertEqual(root.G2.G1._get_fd_unknowns(), ['C2.y']) self.assertEqual(root.G3._get_fd_unknowns(), ['C3.y', 'C4.y']) self.assertEqual(root.G3.C3._get_fd_unknowns(), ['y']) self.assertEqual(root.G2.G1.C2._get_fd_unknowns(), ['y'])
def test_basic_run(self): subprob = Problem(root=ExampleGroup()) prob = Problem(root=Group()) prob.root.add('subprob', SubProblem(subprob, ['G2.C1.x'], ['G3.C4.y'])) prob.setup(check=False) prob.run() self.assertAlmostEqual(prob['subprob.G3.C4.y'], 40.) stream = cStringIO() # get test coverage for list_connections and make sure it doesn't barf prob.root.subprob.list_connections(stream=stream)
def test_variable_access_before_setup(self): prob = Problem(root=ExampleGroup()) try: prob['G2.C1.x'] = 5. except AttributeError 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') try: prob.run() except AttributeError as err: msg = "'unknowns' has not been initialized, setup() must be called before 'x' can be accessed" self.assertEqual(text_type(err), msg) else: self.fail('Exception expected')
def test_sublevel_record(self): prob = Problem() prob.root = ExampleGroup() prob.root.G2.G1.nl_solver.add_recorder(self.recorder) self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) self.recorder.close() coordinate = ['Driver', (1, ), "root", (1, ), "G2", (1, ), "G1", (1, )] expected_params = [("C2.x", 5.0)] expected_unknowns = [("C2.y", 10.0)] expected_resids = [("C2.y", 0.0)] self.assertIterationDataRecorded( ((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids), ), self.eps)
def test_variable_access_before_setup(self): prob = Problem(root=ExampleGroup()) try: prob['G2.C1.x'] = 5. except AttributeError 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') try: prob.run() except RuntimeError as err: msg = "Before running the model, setup() must be called. If " + \ "the configuration has changed since it was called, then " + \ "setup must be called again before running the model." self.assertEqual(text_type(err), msg) else: self.fail('Exception expected')
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_dump(self): prob = Problem(root=ExampleGroup()) prob.setup(check=False) save = StringIO() prob.root.dump(out_stream=save)
def test_connect(self): root = ExampleGroup() 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') # verify variables are set up correctly #root._setup_variables() # 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.G1.C2.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.G1.C2.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.C1.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': 'G3.C4.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.G1.C2.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': 'G3.C4.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.C1.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 })]) # verify we get correct connection information #connections = root._get_explicit_connections() expected_connections = { 'G2.G1.C2.x': ('G2.C1.x', None), 'G3.C3.x': ('G2.G1.C2.y', None), 'G3.C4.x': ('G3.C3.y', None) } self.assertEqual(root.connections, expected_connections) expected_root_params = ['G3.C3.x'] expected_root_unknowns = [ 'G2.C1.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 = ['C1.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.C1.x'] = 99. self.assertEqual(root.G2.unknowns['C1.x'], 99.) # verify subsystem is getting correct metadata from parent unknowns vector self.assertEqual(root.unknowns.metadata('G2.C1.x'), root.G2.unknowns.metadata('C1.x'))