def test_includes_and_excludes(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['includes'] = ['comp1.*'] self.recorder.options['excludes'] = ["*.y2"] self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1,)] expected_params = [ ("comp1.x1", 2.0) ] expected_unknowns = [ ("comp1.y1", 8.0) ] expected_resids = [ ("comp1.y1", 0.0) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids),), self.eps)
def test_only_unknowns_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1, )] expected_unknowns = [ ("comp1.y1", 8.0), ("comp1.y2", 6.0), ("comp2.y1", 4.0), ("comp3.y1", 21.0), ("comp4.y1", 46.0), ("comp4.y2", -93.0), ("comp5.y1", 36.8), ("comp6.y1", -46.5), ("comp7.y1", -102.7), ("p.x", 2.0) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), None, expected_unknowns, None),), self.eps)
def test_only_params_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = False self.recorder.options['record_unknowns'] = False prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1,)] expected_params = [ ("comp1.x1", 2.0), ("comp2.x1", 8.0), ("comp3.x1", 6.0), ("comp4.x1", 4.0), ("comp4.x2", 21.0), ("comp5.x1", 46.0), ("comp6.x1", -93.0), ("comp7.x1", 36.8), ("comp7.x2", -46.5) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), expected_params, None, None),), self.eps)
def test_converge_diverge(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.ln_solver = LinearGaussSeidel() prob.setup(check=False) prob.run() indep_list = ['p.x'] unknown_list = ['comp7.y1'] prob.run() # Make sure value is fine. assert_rel_error(self, prob['comp7.y1'], -102.7, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='fd', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)
def test_only_resids_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['record_params'] = False self.recorder.options['record_unknowns'] = False self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1, )] expected_resids = [ ("comp1.y1", 0.0), ("comp1.y2", 0.0), ("comp2.y1", 0.0), ("comp3.y1", 0.0), ("comp4.y1", 0.0), ("comp4.y2", 0.0), ("comp5.y1", 0.0), ("comp6.y1", 0.0), ("comp7.y1", 0.0), ("p.x", 0.0) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), None, None, expected_resids),), self.eps)
def test_solver_record(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.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) prob.cleanup() # close recorders coordinate = [0, 'Driver', (1, ), "root", (1, )] expected_params = [("comp1.x1", 2.0), ("comp2.x1", 8.0), ("comp3.x1", 6.0), ("comp4.x1", 4.0), ("comp4.x2", 21.0), ("comp5.x1", 46.0), ("comp6.x1", -93.0), ("comp7.x1", 36.8), ("comp7.x2", -46.5)] expected_unknowns = [("comp1.y1", 8.0), ("comp1.y2", 6.0), ("comp2.y1", 4.0), ("comp3.y1", 21.0), ("comp4.y1", 46.0), ("comp4.y2", -93.0), ("comp5.y1", 36.8), ("comp6.y1", -46.5), ("comp7.y1", -102.7), ("p.x", 2.0)] expected_resids = [("comp1.y1", 0.0), ("comp1.y2", 0.0), ("comp2.y1", 0.0), ("comp3.y1", 0.0), ("comp4.y1", 0.0), ("comp4.y2", 0.0), ("comp5.y1", 0.0), ("comp6.y1", 0.0), ("comp7.y1", 0.0), ("p.x", 0.0)] self.assertIterationDataRecorded( ((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids), ), self.eps)
def test_analysis_error(self): prob = Problem(impl=impl) prob.root = ConvergeDiverge() prob.root.ln_solver = PetscKSP() prob.root.ln_solver.options['maxiter'] = 2 prob.root.ln_solver.options['err_on_maxiter'] = True prob.setup(check=False) prob.run() indep_list = ['p.x'] unknown_list = ['comp7.y1'] prob.run() # Make sure value is fine. assert_rel_error(self, prob['comp7.y1'], -102.7, 1e-6) try: J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') except AnalysisError as err: self.assertEqual( str(err), "Solve in '': PetscKSP FAILED to converge in 6 iterations") else: self.fail("expected AnalysisError")
def test_converge_diverge(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.ln_solver = DirectSolver() prob.root.ln_solver.options['jacobian_method'] = 'assemble' prob.setup(check=False) prob.run() indep_list = ['p.x'] unknown_list = ['comp7.y1'] prob.run() # Make sure value is fine. assert_rel_error(self, prob['comp7.y1'], -102.7, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='fd', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)
def test_root_solver_doesnt_record_metadata(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.nl_solver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = False prob.setup(check=False) prob.cleanup() # closes recorders self.assertMetadataRecorded(None)
def test_driver_doesnt_record_metadata(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = False prob.setup(check=False) self.recorder.close() self.assertMetadataRecorded(None)
def test_root_solver_records_metadata(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.nl_solver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = True try: prob.setup(check=False) except Exception as err: self.assertEqual( str(err), "Recording of metadata is not supported by CsvRecorder.")
def test_root_solver_records_metadata(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.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_recording_model_viewer_data(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = True prob.setup(check=False) prob.cleanup() # closes recorders # do some basic tests to make sure the model_viewer_data was recorded db = SqliteDict(filename=self.filename, flag='r', tablename='metadata') model_viewer_data = db['model_viewer_data'] tr = model_viewer_data['tree'] self.assertEqual(set(['name', 'type', 'subsystem_type', 'children']), set(tr.keys())) cl = model_viewer_data['connections_list'] for c in cl: self.assertEqual(set(['src', 'tgt']), set(c.keys())) db.close()
def test_recording_system_metadata(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.add_metadata('string', 'just a test') prob.root.add_metadata('ints', [1, 2, 3]) prob.driver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = True prob.setup(check=False) prob.cleanup() # closes recorders # check the system metadata recording sqlite_metadata = SqliteDict(filename=self.filename, flag='r', tablename='metadata') system_metadata = sqlite_metadata['system_metadata'] self.assertEqual(len(system_metadata), 2) self.assertEqual(system_metadata['string'], 'just a test') self.assertEqual(system_metadata['ints'], [1, 2, 3]) sqlite_metadata.close()
def test_recording_system_metadata(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.add_metadata('string', 'just a test') prob.root.add_metadata('ints', [1, 2, 3]) prob.driver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = True prob.setup(check=False) prob.cleanup() # closes recorders hdf = h5py.File(self.filename, 'r') metadata = hdf.get('metadata', None) system_metadata = pickle.loads(metadata.get('system_metadata').value) self.assertEqual(len(system_metadata), 2) self.assertEqual(system_metadata['string'], 'just a test') self.assertEqual(system_metadata['ints'], [1, 2, 3]) hdf.close()
def test_recording_model_viewer_data(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = True prob.setup(check=False) prob.cleanup() # closes recorders hdf = h5py.File(self.filename, 'r') metadata = hdf.get('metadata', None) model_viewer_data = pickle.loads( metadata.get('model_viewer_data').value) self.assertEqual(len(model_viewer_data), 2) tr = model_viewer_data['tree'] self.assertEqual(set(['name', 'type', 'subsystem_type', 'children']), set(tr.keys())) cl = model_viewer_data['connections_list'] for c in cl: self.assertEqual(set(['src', 'tgt']), set(c.keys())) hdf.close()