Example #1
0
    def test_model_viewer_has_correct_data_from_sqlite(self):
        """
        Verify that the correct data exists when a model structure is recorded
        and then pulled out of a sqlite db file and compared to the expected
        structure.  Uses the SellarStateConnection model.
        """
        p = om.Problem(model=SellarStateConnection())

        r = SqliteRecorder(self.sqlite_db_filename)
        p.driver.add_recorder(r)

        p.setup()
        p.final_setup()
        r.shutdown()

        model_viewer_data = _get_viewer_data(self.sqlite_db_filename)

        with open(os.path.join(self.parent_dir,
                               'sellar_tree.json')) as json_file:
            expected_tree = json.load(json_file)

        # check expected model tree
        self.check_model_viewer_data(
            model_viewer_data, expected_tree, self.expected_pathnames,
            self.expected_conns, self.expected_abs2prom,
            self.expected_declare_partials, self.expected_driver_name,
            self.expected_design_vars_names, self.expected_responses_names)
Example #2
0
 def setUp(self):
     self.dir = mkdtemp()
     self.filename = os.path.join(self.dir, "sqlite_test")
     self.tablename = 'openmdao'
     self.recorder = SqliteRecorder(self.filename)
     self.recorder.options['record_metadata'] = False
     self.eps = 1e-5
Example #3
0
    def test_n2_from_sqlite(self):
        """
        Test that an n2 html file is generated from a sqlite file.
        """
        p = om.Problem()
        p.model = SellarStateConnection()
        r = SqliteRecorder(self.sqlite_db_filename2)
        p.driver.add_recorder(r)
        p.setup()
        p.final_setup()
        r.shutdown()
        n2(p, outfile=self.compare_html_filename, show_browser=DEBUG_BROWSER)
        n2(self.sqlite_db_filename2,
           outfile=self.sqlite_html_filename,
           show_browser=DEBUG_BROWSER)

        # Check that the html file has been created and has something in it.
        self.assertTrue(os.path.isfile(self.sqlite_html_filename),
                        (self.sqlite_html_filename + " is not a valid file."))
        self.assertGreater(os.path.getsize(self.sqlite_html_filename), 100)

        # Check that there are no errors when running from the command line with a recording.
        check_call('openmdao n2 --no_browser %s' % self.sqlite_db_filename2)

        # Compare models from the files generated from the Problem and the recording
        sqlite_model_data = self._extract_compressed_model(
            self.sqlite_html_filename)
        compare_model_data = self._extract_compressed_model(
            self.compare_html_filename)

        self.assertTrue(
            sqlite_model_data == compare_model_data,
            'Model data from sqlite does not match data from Problem.')
Example #4
0
    def test_n2_from_sqlite(self):
        """
        Test that an n2 html file is generated from a sqlite file.
        """
        p = Problem()
        p.model = SellarStateConnection()
        r = SqliteRecorder(self.sqlite_db_filename2)
        p.driver.add_recorder(r)
        p.setup()
        p.final_setup()
        r.shutdown()
        n2(p, outfile=self.compare_html_filename, show_browser=DEBUG_BROWSER)
        n2(self.sqlite_db_filename2,
           outfile=self.sqlite_html_filename,
           show_browser=DEBUG_BROWSER)

        # Check that the html file has been created and has something in it.
        self.assertTrue(os.path.isfile(self.sqlite_html_filename),
                        (self.sqlite_html_filename + " is not a valid file."))
        self.assertGreater(os.path.getsize(self.sqlite_html_filename), 100)

        # Check that there are no errors when running from the command line with a recording.
        check_call('openmdao n2 --no_browser %s' % self.sqlite_db_filename2)

        # Compare the sizes of the files generated from the Problem and the recording
        size1 = os.path.getsize(self.sqlite_html_filename)
        size2 = os.path.getsize(self.compare_html_filename)
        self.assertTrue(
            size1 == size2, 'File size of ' + self.sqlite_html_filename +
            ' is ' + str(size1) + ', but size of ' +
            self.compare_html_filename + ' is ' + str(size2))
Example #5
0
    def test_view_model_from_sqlite(self):
        """
        Test that an n2 html file is generated from a sqlite file.
        """
        p = Problem()
        p.model = SellarStateConnection()
        r = SqliteRecorder(self.sqlite_db_filename2)
        p.driver.add_recorder(r)
        p.setup(check=False)
        p.final_setup()
        r.close()
        view_model(self.sqlite_db_filename2, outfile=self.sqlite_filename, show_browser=False)

        # Check that the html file has been created and has something in it.
        self.assertTrue(os.path.isfile(self.sqlite_html_filename), (self.problem_html_filename + " is not a valid file."))
        self.assertGreater(os.path.getsize(self.sqlite_html_filename), 100)
Example #6
0
    def test_view_model_from_sqlite(self):
        """
        Test that an n2 html file is generated from a sqlite file.
        """
        p = Problem()
        p.model = SellarStateConnection()
        r = SqliteRecorder(self.sqlite_db_filename2)
        p.driver.add_recorder(r)
        p.setup(check=False)
        p.final_setup()
        r.shutdown()
        view_model(self.sqlite_db_filename2, outfile=self.sqlite_filename, show_browser=False)

        # Check that the html file has been created and has something in it.
        self.assertTrue(os.path.isfile(self.sqlite_html_filename), (self.problem_html_filename + " is not a valid file."))
        self.assertGreater(os.path.getsize(self.sqlite_html_filename), 100)
Example #7
0
    def test_pyxdsm_case_reading(self):
        """
        Writes a recorder file, and the XDSM writer makes the diagram based on the SQL file
        and not the Problem instance.
        """
        from openmdao.recorders.sqlite_recorder import SqliteRecorder

        filename = 'xdsm_from_sql'
        case_recording_filename = filename + '.sql'

        prob = Problem()
        prob.model = model = SellarNoDerivatives()
        model.add_design_var('z', lower=np.array([-10.0, 0.0]),
                             upper=np.array([10.0, 10.0]), indices=np.arange(2, dtype=int))
        model.add_design_var('x', lower=0.0, upper=10.0)
        model.add_objective('obj')
        model.add_constraint('con1', equals=np.zeros(1))
        model.add_constraint('con2', upper=0.0)

        recorder = SqliteRecorder(case_recording_filename)
        prob.driver.add_recorder(recorder)

        prob.setup(check=False)
        prob.final_setup()

        # Write output
        write_xdsm(case_recording_filename, filename=filename, out_format='tex',
                   show_browser=False, quiet=QUIET)

        # Check if file was created
        self.assertTrue(os.path.isfile(case_recording_filename))
        self.assertTrue(os.path.isfile('.'.join([filename, 'tex'])))

        # Check that there are no errors when running from the command line with a recording.
        check_call('openmdao xdsm --no_browser %s' % case_recording_filename)
Example #8
0
    def setUp(self):

        recording_iteration.stack = []  # reset to avoid problems from earlier tests
        self.dir = mkdtemp()
        self.filename = os.path.join(self.dir, "sqlite_test")
        self.recorder = SqliteRecorder(self.filename)
        self.original_path = os.getcwd()
        os.chdir(self.dir)
Example #9
0
    def test_model_viewer_has_correct_data_from_sqlite(self):
        """
        Verify that the correct data exists when a model structure is recorded
        and then pulled out of a sqlite db file and compared to the expected
        structure.  Uses the SellarStateConnection model.
        """
        p = Problem(model=SellarStateConnection())

        r = SqliteRecorder(self.sqlite_db_filename)
        p.driver.add_recorder(r)

        p.setup()
        p.final_setup()
        r.shutdown()

        model_viewer_data = _get_viewer_data(self.sqlite_db_filename)
        print(model_viewer_data['tree'])

        # check expected model tree
        self.assertDictEqual(model_viewer_data['tree'], self.expected_tree)

        # check expected system pathnames
        pathnames = model_viewer_data['sys_pathnames_list']
        self.assertListEqual(sorted(pathnames), self.expected_pathnames)

        # check expected connections, after mapping cycle_arrows indices back to pathnames
        connections = sorted(model_viewer_data['connections_list'],
                             key=lambda x: (x['tgt'], x['src']))
        for conn in connections:
            if 'cycle_arrows' in conn:
                cycle_arrows = []
                for src, tgt in conn['cycle_arrows']:
                    cycle_arrows.append(' '.join(
                        [pathnames[src], pathnames[tgt]]))
                conn['cycle_arrows'] = sorted(cycle_arrows)
        self.assertEqual(len(connections), len(self.expected_conns))
        for c, ex in zip(connections, self.expected_conns):
            self.assertEqual(c['src'], ex['src'])
            self.assertEqual(c['tgt'], ex['tgt'])
            self.assertEqual(c.get('cycle_arrows', []),
                             ex.get('cycle_arrows', []))

        # check expected abs2prom map
        self.assertDictEqual(model_viewer_data['abs2prom'],
                             self.expected_abs2prom)
Example #10
0
    def test_model_viewer_has_correct_data_from_sqlite(self):
        """
        Verify that the correct data exists when a model structure is recorded
        and then pulled out of a sqlite db file and compared to the expected
        structure.  Uses the SellarStateConnection model.
        """
        p = Problem()
        p.model = SellarStateConnection()
        r = SqliteRecorder(self.sqlite_db_filename)
        p.driver.add_recorder(r)
        p.setup(check=False)
        p.final_setup()
        r.close()

        model_viewer_data = _get_viewer_data(self.sqlite_db_filename)
        tree_json = json.dumps(model_viewer_data['tree'])
        conns_json = json.dumps(model_viewer_data['connections_list'])

        self.assertEqual(self.expected_tree_json, tree_json)
        self.assertEqual(self.expected_conns_json, conns_json)
Example #11
0
    def test_model_viewer_has_correct_data_from_sqlite(self):
        """
        Verify that the correct data exists when a model structure is recorded
        and then pulled out of a sqlite db file and compared to the expected
        structure.  Uses the SellarStateConnection model.
        """
        p = Problem()
        p.model = SellarStateConnection()
        r = SqliteRecorder(self.sqlite_db_filename)
        p.driver.add_recorder(r)
        p.setup(check=False)
        p.final_setup()
        r.close()

        model_viewer_data = _get_viewer_data(self.sqlite_db_filename)
        tree_json = json.dumps(model_viewer_data['tree'])
        conns_json = json.dumps(model_viewer_data['connections_list'])

        self.assertEqual(self.expected_tree_json, tree_json)
        self.assertEqual(self.expected_conns_json, conns_json)
Example #12
0
    def test_view_model_from_sqlite(self):
        """
        Test that an n2 html file is generated from a sqlite file.
        """
        p = Problem()
        p.model = SellarStateConnection()
        r = SqliteRecorder(self.sqlite_db_filename2)
        p.driver.add_recorder(r)
        p.setup(check=False)
        p.final_setup()
        r.shutdown()
        view_model(self.sqlite_db_filename2, outfile=self.sqlite_html_filename, show_browser=DEBUG)

        # Check that the html file has been created and has something in it.
        self.assertTrue(os.path.isfile(self.sqlite_html_filename),
                        (self.problem_html_filename + " is not a valid file."))
        self.assertGreater(os.path.getsize(self.sqlite_html_filename), 100)

        # Check that there are no errors when running from the command line with a recording.
        check_call('openmdao view_model --no_browser %s' % self.sqlite_db_filename2)
Example #13
0
    def test_view_model_from_sqlite(self):
        """
        Test that an n2 html file is generated from a sqlite file.
        """
        p = Problem()
        p.model = SellarStateConnection()
        r = SqliteRecorder(self.sqlite_db_filename2)
        p.driver.add_recorder(r)
        p.setup()
        p.final_setup()
        r.shutdown()
        view_model(self.sqlite_db_filename2,
                   outfile=self.sqlite_html_filename,
                   show_browser=DEBUG)

        # Check that the html file has been created and has something in it.
        self.assertTrue(os.path.isfile(self.sqlite_html_filename),
                        (self.problem_html_filename + " is not a valid file."))
        self.assertGreater(os.path.getsize(self.sqlite_html_filename), 100)

        # Check that there are no errors when running from the command line with a recording.
        check_call('openmdao view_model --no_browser %s' %
                   self.sqlite_db_filename2)
Example #14
0
    def test_model_viewer_has_correct_data_from_sqlite(self):
        """
        Verify that the correct data exists when a model structure is recorded
        and then pulled out of a sqlite db file and compared to the expected
        structure.  Uses the SellarStateConnection model.
        """
        p = Problem(model=SellarStateConnection())

        r = SqliteRecorder(self.sqlite_db_filename)
        p.driver.add_recorder(r)

        p.setup(check=False)
        p.final_setup()
        r.shutdown()

        model_viewer_data = _get_viewer_data(self.sqlite_db_filename)

        # check expected model tree
        self.assertDictEqual(model_viewer_data['tree'], self.expected_tree)

        # check expected system pathnames
        pathnames = model_viewer_data['sys_pathnames_list']
        self.assertListEqual(sorted(pathnames), self.expected_pathnames)

        # check expected connections, after mapping cycle_arrows indices back to pathnames
        connections = model_viewer_data['connections_list']
        for conn in connections:
            if 'cycle_arrows' in conn:
                cycle_arrows = []
                for src, tgt in conn['cycle_arrows']:
                    cycle_arrows.append(' '.join([pathnames[src], pathnames[tgt]]))
                conn['cycle_arrows'] = sorted(cycle_arrows)
        self.assertListEqual(connections, self.expected_conns)

        # check expected abs2prom map
        self.assertDictEqual(model_viewer_data['abs2prom'], self.expected_abs2prom)
Example #15
0
    def test_pyxdsm_case_reading(self):
        """
        Writes a recorder file, and the XDSM writer makes the diagram based on the SQL file
        and not the Problem instance.
        """
        from openmdao.recorders.sqlite_recorder import SqliteRecorder

        filename = 'xdsm_from_sql'
        case_recording_filename = filename + '.sql'

        prob = Problem()
        prob.model = model = SellarNoDerivatives()
        model.add_design_var('z',
                             lower=np.array([-10.0, 0.0]),
                             upper=np.array([10.0, 10.0]),
                             indices=np.arange(2, dtype=int))
        model.add_design_var('x', lower=0.0, upper=10.0)
        model.add_objective('obj')
        model.add_constraint('con1', equals=np.zeros(1))
        model.add_constraint('con2', upper=0.0)

        recorder = SqliteRecorder(case_recording_filename)
        prob.driver.add_recorder(recorder)

        prob.setup(check=False)
        prob.final_setup()

        # Write output
        msg = (
            'For SQL input the XDSM writer shows only the model hierarchy, '
            'and the driver, design variables and responses are not part of the '
            'diagram.')
        with assert_warning(Warning, msg):
            write_xdsm(case_recording_filename,
                       filename=filename,
                       out_format='tex',
                       show_browser=False,
                       quiet=QUIET)

        # Check if file was created
        self.assertTrue(os.path.isfile(case_recording_filename))
        self.assertTrue(os.path.isfile('.'.join([filename, 'tex'])))
Example #16
0
 def setUp(self):
     self.dir = mkdtemp()
     self.filename = os.path.join(self.dir, "sqlite_test")
     self.tablename = 'openmdao'
     self.recorder = SqliteRecorder(self.filename)
Example #17
0
# recording_includes_options = ['obj.val']
# for j in range(npts):
#     recording_includes_options.append('pt%s.ConCh' % str(j))
#     recording_includes_options.append('pt%s.ConDs' % str(j))
#     recording_includes_options.append('pt%s.ConS0' % str(j))
#     recording_includes_options.append('pt%s.ConS1' % str(j))
#     recording_includes_options.append('pt%s_con5.val' % str(j))
#
# #from openmdao.recorders import DumpRecorder
# #rec = DumpRecorder(out='data.dmp')
# #model.driver.add_recorder(rec)
# #rec.options['includes'] = recording_includes_options

from openmdao.recorders.sqlite_recorder import SqliteRecorder

rec = SqliteRecorder(out='data.sql')
model.driver.add_recorder(rec)
# rec.options['includes'] = recording_includes_options

model.setup()
model.run()

import resource

print("Memory Usage:",
      resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000.0,
      "MB (on unix)")

#----------------------------------------------------------------
# Below this line, code I was using for verifying and profiling.
#----------------------------------------------------------------
Example #18
0
    def compute(self, inputs, outputs):
        # type: (Vector, Vector) -> None
        """Computation performed by the component.

        Parameters
        ----------
        inputs : all inputs coming from outside the component in the group
        outputs : all outputs provided outside the component in the group"""

        # Define problem of subdriver
        p = self.prob
        m = p.model

        # Push global inputs down
        for input_name in m.model_constants:
            p[input_name] = inputs[input_name]

        failed_experiments = {}
        # sort to have outputs first
        sorted_model_super_inputs = sorted(m.model_super_inputs.keys(), reverse=True)
        for input_name in sorted_model_super_inputs:
            if input_name in m.sm_of_training_params.keys():  # Add these inputs as training data
                sm_uid = m.sm_of_training_params[input_name]
                pred_param = m.find_mapped_parameter(input_name,
                                                     m.sm_prediction_inputs[sm_uid] |
                                                     m.sm_prediction_outputs[sm_uid])
                sm_comp = getattr(m, str_to_valid_sys_name(sm_uid))
                if sm_uid not in failed_experiments.keys():
                    failed_experiments[sm_uid] = (None, None)
                sm_comp.options['train:'+pred_param], failed_experiments[sm_uid]\
                    = p.postprocess_experiments(inputs[input_name], input_name,
                                                failed_experiments[sm_uid])
            else:
                p[input_name] = inputs[input_name]

        # Provide message on failed experiments
        warn_about_failed_experiments(failed_experiments)

        # Set initial values of design variables back to original ones (to avoid using values of
        # last run)
        for des_var, attrbs in m.design_vars.items():
            p[des_var] = attrbs['initial']

        # Run the driver
        print('Running subdriver {}'.format(self.options['driver_uid']))
        if 'Sub-Optimizer' not in p.case_reader_path:
            p.driver.cleanup()
            basename, extension = os.path.splitext(p.case_reader_path)
            case_reader_filename = basename + '_loop' + str(self._run_count) + extension
            p.driver.add_recorder(SqliteRecorder(case_reader_filename))
            p.driver.recording_options['includes'] = ['*']
            p.driver.recording_options['record_model_metadata'] = True
            p.driver._setup_recording()
        p.run_driver()
        self._add_run_count()

        # Pull the value back up to the output array
        doe_out_vecs = {}
        for output_name in m.model_super_outputs:
            # Add these outputs as vectors based on DOE driver
            if output_name in m.doe_parameters.keys():
                doe_out_vecs[output_name] = []
            else:
                if not p.driver.fail:
                    outputs[output_name] = p[output_name]
                else:
                    outputs[output_name] = float('nan')

        # If the driver failed (hence, optimization failed), then send message and clean
        if p.driver.fail:
            print('Driver run failed!')
            p.clean_driver_after_failure()

        # Provide DOE output vectors as output of the component, if this is expected
        if doe_out_vecs:
            # First read out the case reader
            cr = CaseReader(case_reader_filename)
            cases = cr.list_cases('driver')
            for n in range(len(cases)):
                cr_outputs = cr.get_case(n).outputs
                doe_param_matches = {}
                for output_name in doe_out_vecs.keys():
                    doe_param_matches[output_name] = doe_param_match \
                        = m.find_mapped_parameter(output_name, cr_outputs.keys())
                    doe_out_vecs[output_name].append(cr_outputs[doe_param_match][0])

            # Then write the final vectors to the global output array
            for output_name in doe_out_vecs.keys():
                if output_name in p.doe_samples[p.driver_uid]['inputs']:
                    des_var_match = m.find_mapped_parameter(output_name, m._design_vars.keys())
                    doe_out_vecs[output_name] = unscale_value(doe_out_vecs[output_name],
                                                              m._design_vars[des_var_match]['ref0'],
                                                              m._design_vars[des_var_match]['ref'])
                outputs[output_name] = np.array(doe_out_vecs[output_name])
Example #19
0
class TestSqliteRecorder(MPITestCase):
    filename = ""
    dir = ""
    N_PROCS = 2

    def setUp(self):
        self.dir = mkdtemp()
        self.filename = os.path.join(self.dir, "sqlite_test")
        self.tablename = "openmdao"
        self.recorder = SqliteRecorder(self.filename)
        self.recorder.options["record_metadata"] = False
        self.eps = 1e-5

    def tearDown(self):
        try:
            rmtree(self.dir)
        except OSError as e:
            # If directory already deleted, keep going
            if e.errno != errno.ENOENT:
                raise e

    def assertMetadataRecorded(self, expected):
        if self.comm.rank != 0:
            return

        db = SqliteDict(self.filename, self.tablename)
        _assertMetadataRecorded(self, db, expected)
        db.close()

    def assertIterationDataRecorded(self, expected, tolerance, root):
        if self.comm.rank != 0:
            return

        db = SqliteDict(self.filename, self.tablename)
        _assertIterationDataRecorded(self, db, expected, tolerance)
        db.close()

    def test_basic(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add("G1", ParallelGroup())
        G1.add("P1", IndepVarComp("x", np.ones(size, float) * 1.0))
        G1.add("P2", IndepVarComp("x", np.ones(size, float) * 2.0))

        prob.root.add("C1", ABCDArrayComp(size))

        prob.root.connect("G1.P1.x", "C1.a")
        prob.root.connect("G1.P2.x", "C1.b")

        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(prob)
        self.recorder.close()

        coordinate = ["Driver", (1,)]

        expected_params = [("C1.a", [1.0, 1.0, 1.0]), ("C1.b", [2.0, 2.0, 2.0])]

        expected_unknowns = [
            ("G1.P1.x", np.array([1.0, 1.0, 1.0])),
            ("G1.P2.x", np.array([2.0, 2.0, 2.0])),
            ("C1.c", np.array([3.0, 3.0, 3.0])),
            ("C1.d", np.array([-1.0, -1.0, -1.0])),
            ("C1.out_string", "_C1"),
            ("C1.out_list", [1.5]),
        ]
        expected_resids = [
            ("G1.P1.x", np.array([0.0, 0.0, 0.0])),
            ("G1.P2.x", np.array([0.0, 0.0, 0.0])),
            ("C1.c", np.array([0.0, 0.0, 0.0])),
            ("C1.d", np.array([0.0, 0.0, 0.0])),
            ("C1.out_string", ""),
            ("C1.out_list", []),
        ]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids),), self.eps, prob.root
        )

    def test_includes(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add("G1", ParallelGroup())
        G1.add("P1", IndepVarComp("x", np.ones(size, float) * 1.0))
        G1.add("P2", IndepVarComp("x", np.ones(size, float) * 2.0))

        prob.root.add("C1", ABCDArrayComp(size))

        prob.root.connect("G1.P1.x", "C1.a")
        prob.root.connect("G1.P2.x", "C1.b")

        prob.driver.add_recorder(self.recorder)
        self.recorder.options["record_params"] = True
        self.recorder.options["record_resids"] = True

        self.recorder.options["includes"] = ["C1.*"]
        prob.setup(check=False)

        t0, t1 = run(prob)
        self.recorder.close()

        coordinate = ["Driver", (1,)]

        expected_params = [("C1.a", [1.0, 1.0, 1.0]), ("C1.b", [2.0, 2.0, 2.0])]
        expected_unknowns = [
            ("C1.c", np.array([3.0, 3.0, 3.0])),
            ("C1.d", np.array([-1.0, -1.0, -1.0])),
            ("C1.out_string", "_C1"),
            ("C1.out_list", [1.5]),
        ]
        expected_resids = [
            ("C1.c", np.array([0.0, 0.0, 0.0])),
            ("C1.d", np.array([0.0, 0.0, 0.0])),
            ("C1.out_string", ""),
            ("C1.out_list", []),
        ]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids),), self.eps, prob.root
        )

    def test_includes_and_excludes(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add("G1", ParallelGroup())
        G1.add("P1", IndepVarComp("x", np.ones(size, float) * 1.0))
        G1.add("P2", IndepVarComp("x", np.ones(size, float) * 2.0))

        prob.root.add("C1", ABCDArrayComp(size))

        prob.root.connect("G1.P1.x", "C1.a")
        prob.root.connect("G1.P2.x", "C1.b")

        prob.driver.add_recorder(self.recorder)

        self.recorder.options["includes"] = ["C1.*"]
        self.recorder.options["excludes"] = ["*.out*"]
        self.recorder.options["record_params"] = True
        self.recorder.options["record_resids"] = True

        prob.setup(check=False)

        t0, t1 = run(prob)
        self.recorder.close()

        coordinate = ["Driver", (1,)]

        expected_params = [("C1.a", [1.0, 1.0, 1.0]), ("C1.b", [2.0, 2.0, 2.0])]

        expected_unknowns = [("C1.c", np.array([3.0, 3.0, 3.0])), ("C1.d", np.array([-1.0, -1.0, -1.0]))]

        expected_resids = [("C1.c", np.array([0.0, 0.0, 0.0])), ("C1.d", np.array([0.0, 0.0, 0.0]))]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids),), self.eps, prob.root
        )

    def test_solver_record(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add("G1", ParallelGroup())
        G1.add("P1", IndepVarComp("x", np.ones(size, float) * 1.0))
        G1.add("P2", IndepVarComp("x", np.ones(size, float) * 2.0))

        prob.root.add("C1", ABCDArrayComp(size))

        prob.root.connect("G1.P1.x", "C1.a")
        prob.root.connect("G1.P2.x", "C1.b")
        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(prob)
        self.recorder.close()

        coordinate = ["Driver", (1,), "root", (1,)]

        expected_params = [("C1.a", [1.0, 1.0, 1.0]), ("C1.b", [2.0, 2.0, 2.0])]
        expected_unknowns = [
            ("G1.P1.x", np.array([1.0, 1.0, 1.0])),
            ("G1.P2.x", np.array([2.0, 2.0, 2.0])),
            ("C1.c", np.array([3.0, 3.0, 3.0])),
            ("C1.d", np.array([-1.0, -1.0, -1.0])),
            ("C1.out_string", "_C1"),
            ("C1.out_list", [1.5]),
        ]
        expected_resids = [
            ("G1.P1.x", np.array([0.0, 0.0, 0.0])),
            ("G1.P2.x", np.array([0.0, 0.0, 0.0])),
            ("C1.c", np.array([0.0, 0.0, 0.0])),
            ("C1.d", np.array([0.0, 0.0, 0.0])),
            ("C1.out_string", ""),
            ("C1.out_list", []),
        ]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids),), self.eps, prob.root
        )

    def test_driver_records_metadata(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add("G1", ParallelGroup())
        G1.add("P1", IndepVarComp("x", np.ones(size, float) * 1.0))
        G1.add("P2", IndepVarComp("x", np.ones(size, float) * 2.0))

        prob.root.add("C1", ABCDArrayComp(size))

        prob.root.connect("G1.P1.x", "C1.a")
        prob.root.connect("G1.P2.x", "C1.b")

        prob.driver.add_recorder(self.recorder)

        self.recorder.options["record_metadata"] = True
        prob.setup(check=False)

        self.recorder.close()

        expected = (
            list(prob.root.params.iteritems()),
            list(prob.root.unknowns.iteritems()),
            list(prob.root.resids.iteritems()),
        )

        self.assertMetadataRecorded(expected)

    def test_driver_doesnt_records_metadata(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add("G1", ParallelGroup())
        G1.add("P1", IndepVarComp("x", np.ones(size, float) * 1.0))
        G1.add("P2", IndepVarComp("x", np.ones(size, float) * 2.0))

        prob.root.add("C1", ABCDArrayComp(size))

        prob.root.connect("G1.P1.x", "C1.a")
        prob.root.connect("G1.P2.x", "C1.b")

        prob.driver.add_recorder(self.recorder)

        self.recorder.options["record_metadata"] = False
        prob.setup(check=False)

        self.recorder.close()

        self.assertMetadataRecorded(None)
Example #20
0
recording_includes_options = ["obj.val"]
for j in range(npts):
    recording_includes_options.append("pt%s.ConCh" % str(j))
    recording_includes_options.append("pt%s.ConDs" % str(j))
    recording_includes_options.append("pt%s.ConS0" % str(j))
    recording_includes_options.append("pt%s.ConS1" % str(j))
    recording_includes_options.append("pt%s_con5.val" % str(j))

# from openmdao.recorders import DumpRecorder
# rec = DumpRecorder(out='data.dmp')
# model.driver.add_recorder(rec)
# rec.options['includes'] = recording_includes_options

from openmdao.recorders.sqlite_recorder import SqliteRecorder

rec = SqliteRecorder(out="data.sql")
model.driver.add_recorder(rec)
rec.options["includes"] = recording_includes_options

model.setup()
model.run()

import resource

print("Memory Usage:", resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000.0, "MB (on unix)")

# ----------------------------------------------------------------
# Below this line, code I was using for verifying and profiling.
# ----------------------------------------------------------------
# profile = False
# params = model.driver.get_desvars().keys()
Example #21
0
# Parallel Derivative calculation
for con_name in ['.ConCh','.ConDs','.ConS0','.ConS1','_con5.val']:
    model.driver.parallel_derivs(['%s%s'%(n,con_name) for n in names])

# Recording
# Some constraints only exit on one process so cannot record everything
recording_includes_options = ['obj.val']
for j in range(npts):
    recording_includes_options.append('pt%s.ConCh' % str(j))
    recording_includes_options.append('pt%s.ConDs' % str(j))
    recording_includes_options.append('pt%s.ConS0' % str(j))
    recording_includes_options.append('pt%s.ConS1' % str(j))
    recording_includes_options.append('pt%s_con5.val' % str(j))

from openmdao.recorders.sqlite_recorder import SqliteRecorder
rec = SqliteRecorder(out='data.sql')
model.driver.add_recorder(rec)
rec.options['includes'] = recording_includes_options

model.setup()
model.run()

import resource
print("Memory Usage:", resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1000.0, "MB (on unix)")

#----------------------------------------------------------------
# Below this line, code I was using for verifying and profiling.
#----------------------------------------------------------------
#profile = False
#params = model.driver.get_desvars().keys()
#unks = model.driver.get_objectives().keys() + model.driver.get_constraints().keys()
Example #22
0
# Parallel Derivative calculation
for con_name in ['.ConCh','.ConDs','.ConS0','.ConS1','_con5.val']:
    model.driver.parallel_derivs(['%s%s'%(n,con_name) for n in names])

# Recording
# Some constraints only exit on one process so cannot record everything
recording_includes_options = ['obj.val']
for j in range(npts):
    recording_includes_options.append('pt%s.ConCh' % str(j))
    recording_includes_options.append('pt%s.ConDs' % str(j))
    recording_includes_options.append('pt%s.ConS0' % str(j))
    recording_includes_options.append('pt%s.ConS1' % str(j))
    recording_includes_options.append('pt%s_con5.val' % str(j))

from openmdao.recorders.sqlite_recorder import SqliteRecorder
rec = SqliteRecorder(out='data.sql')
model.driver.add_recorder(rec)
rec.options['includes'] = recording_includes_options
rec.options['record_derivs'] = False

model.setup()
model.run()

import resource
print("Memory Usage:", resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1000.0, "MB (on unix)")

#----------------------------------------------------------------
# Below this line, code I was using for verifying and profiling.
#----------------------------------------------------------------
#profile = False
#params = list(model.driver.get_desvars().keys())
Example #23
0
class TestSqliteRecorder(RecorderTests.Tests):
    filename = ""
    dir = ""

    def setUp(self):
        self.dir = mkdtemp()
        self.filename = os.path.join(self.dir, "sqlite_test")
        self.tablename = 'openmdao'
        self.recorder = SqliteRecorder(self.filename)

    def tearDown(self):
        super(TestSqliteRecorder, self).tearDown()
        try:
            rmtree(self.dir)
        except OSError as e:
            # If directory already deleted, keep going
            if e.errno != errno.ENOENT:
                raise e

    def assertDatasetEquals(self, expected, tolerance):
        # Close the file to ensure it is written to disk.
        self.recorder.close()
        # self.recorder.out = None

        sentinel = object()

        db = SqliteDict( self.filename, self.tablename )

        ###### Need a way to get a list of the group_names in the order in which they were written and put it in  a variable named order
        order = db['order']
        del db['order']

        for coord, expect in expected:
            iter_coord = format_iteration_coordinate(coord)

            self.assertEqual(order.pop(0), iter_coord)

            groupings = (
                ("Parameters", expect[0]),
                ("Unknowns", expect[1]),
                ("Residuals", expect[2])
            )

            #### Need to get the record with the key of 'iter_coord'
            actual_group = db[iter_coord]

            for label, values in groupings:
                actual = actual_group[label]
                # If len(actual) == len(expected) and actual <= expected, then
                # actual == expected.
                self.assertEqual(len(actual), len(values))
                for key, val in values:
                    found_val = actual.get(key, sentinel)
                    if found_val is sentinel:
                        self.fail("Did not find key '{0}'".format(key))
                    assert_rel_error(self, found_val, val, tolerance)
            del db[iter_coord]
            ######## delete the record with the key 'iter_coord'

        # Having deleted all found values, the file should now be empty.
        ###### Need a way to get the number of records in the main table
        self.assertEqual(len(db), 0)

        # As should the ordering.
        self.assertEqual(len(order), 0)

        db.close()
Example #24
0
class TestSqliteRecorder(unittest.TestCase):
    def setUp(self):
        self.dir = mkdtemp()
        self.filename = os.path.join(self.dir, "sqlite_test")
        self.tablename = 'openmdao'
        self.recorder = SqliteRecorder(self.filename)
        self.recorder.options['record_metadata'] = False
        self.eps = 1e-5

    def tearDown(self):
        try:
            rmtree(self.dir)
        except OSError as e:
            # If directory already deleted, keep going
            if e.errno != errno.ENOENT:
                raise e

    def assertMetadataRecorded(self, expected):
        with SqliteDict( self.filename, self.tablename, flag='r' ) as db:
            _assertMetadataRecorded( self, db, expected )

    def assertIterationDataRecorded(self, expected, tolerance):
        db = SqliteDict( self.filename, self.tablename )
        _assertIterationDataRecorded(self, db, expected, tolerance)
        db.close()

    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)
        self.recorder.close()

        coordinate = ['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_only_unknowns_recorded(self):
        prob = Problem()
        prob.root = ConvergeDiverge()
        prob.driver.add_recorder(self.recorder)
        prob.setup(check=False)

        t0, t1 = run_problem(prob)
        self.recorder.close()

        coordinate = ['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)
        self.recorder.close()

        coordinate = ['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_basic(self):
        prob = Problem()
        prob.root = ConvergeDiverge()
        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()

        coordinate = ['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)
        ]

        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_includes(self):
        prob = Problem()
        prob.root = ConvergeDiverge()
        prob.driver.add_recorder(self.recorder)
        self.recorder.options['includes'] = ['comp1.*']
        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,)]

        expected_params = [
            ("comp1.x1", 2.0)
        ]
        expected_unknowns = [
            ("comp1.y1", 8.0),
            ("comp1.y2", 6.0)
        ]
        expected_resids = [
            ("comp1.y1", 0.0),
            ("comp1.y2", 0.0)
        ]

        self.assertIterationDataRecorded(((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids),), self.eps)

    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)
        self.recorder.close()

        coordinate = ['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_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)
        self.recorder.close()

        coordinate = ['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_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_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)
        ]

        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_driver_records_metadata(self):
        prob = Problem()
        prob.root = ConvergeDiverge()
        prob.driver.add_recorder(self.recorder)
        self.recorder.options['record_metadata'] = True
        prob.setup(check=False)
        self.recorder.close()

        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_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
        prob.setup(check=False)
        self.recorder.close()

        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_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)
        self.recorder.close()

        self.assertMetadataRecorded(None)

    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)
        self.recorder.close()

        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_subsolver_doesnt_record_metadata(self):
        prob = Problem()
        prob.root = ExampleGroup()
        prob.root.G2.G1.nl_solver.add_recorder(self.recorder)
        self.recorder.options['record_metadata'] = False
        prob.setup(check=False)
        self.recorder.close()

        self.assertMetadataRecorded(None)
Example #25
0
# Some constraints only exit on one process so cannot record everything
recording_includes_options = ['obj.val']
for j in range(npts):
    recording_includes_options.append('pt%s.ConCh' % str(j))
    recording_includes_options.append('pt%s.ConDs' % str(j))
    recording_includes_options.append('pt%s.ConS0' % str(j))
    recording_includes_options.append('pt%s.ConS1' % str(j))
    recording_includes_options.append('pt%s_con5.val' % str(j))

#from openmdao.recorders import DumpRecorder
#rec = DumpRecorder(out='data.dmp')
#model.driver.add_recorder(rec)
#rec.options['includes'] = recording_includes_options

from openmdao.recorders.sqlite_recorder import SqliteRecorder
rec = SqliteRecorder(out='data.sql')
model.driver.add_recorder(rec)
rec.options['includes'] = recording_includes_options

model.setup()
model.run()

import resource
print("Memory Usage:", resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1000.0, "MB (on unix)")

#----------------------------------------------------------------
# Below this line, code I was using for verifying and profiling.
#----------------------------------------------------------------
#profile = False
#params = model.driver.get_desvars().keys()
#unks = model.driver.get_objectives().keys() + model.driver.get_constraints().keys()
Example #26
0
class TestSqliteRecorder(MPITestCase):
    filename = ""
    dir = ""
    N_PROCS = 2

    def setUp(self):
        self.dir = mkdtemp()
        self.filename = os.path.join(self.dir, "sqlite_test")
        self.tablename = 'openmdao'
        self.recorder = SqliteRecorder(self.filename)
        self.recorder.options['record_metadata'] = False
        self.eps = 1e-5

    def tearDown(self):
        try:
            rmtree(self.dir)
        except OSError as e:
            # If directory already deleted, keep going
            if e.errno != errno.ENOENT:
                raise e

    def assertMetadataRecorded(self, expected):
        if self.comm.rank != 0:
            return

        db = SqliteDict(self.filename, self.tablename)
        _assertMetadataRecorded(self, db, expected)
        db.close()

    def assertIterationDataRecorded(self, expected, tolerance, root):
        if self.comm.rank != 0:
            return

        db = SqliteDict(self.filename, self.tablename)
        _assertIterationDataRecorded(self, db, expected, tolerance)
        db.close()

    def test_basic(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')

        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(prob)
        self.recorder.close()

        coordinate = ['Driver', (1, )]

        expected_params = [
            ("C1.a", [1.0, 1.0, 1.0]),
            ("C1.b", [2.0, 2.0, 2.0]),
        ]

        expected_unknowns = [
            ("G1.P1.x", np.array([1.0, 1.0, 1.0])),
            ("G1.P2.x", np.array([2.0, 2.0, 2.0])),
            ("C1.c", np.array([3.0, 3.0, 3.0])),
            ("C1.d", np.array([-1.0, -1.0, -1.0])),
            ("C1.out_string", "_C1"),
            ("C1.out_list", [1.5]),
        ]
        expected_resids = [
            ("G1.P1.x", np.array([0.0, 0.0, 0.0])),
            ("G1.P2.x", np.array([0.0, 0.0, 0.0])),
            ("C1.c", np.array([0.0, 0.0, 0.0])),
            ("C1.d", np.array([0.0, 0.0, 0.0])),
            ("C1.out_string", ""),
            ("C1.out_list", []),
        ]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns,
              expected_resids), ), self.eps, prob.root)

    def test_includes(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')

        prob.driver.add_recorder(self.recorder)
        self.recorder.options['record_params'] = True
        self.recorder.options['record_resids'] = True

        self.recorder.options['includes'] = ['C1.*']
        prob.setup(check=False)

        t0, t1 = run(prob)
        self.recorder.close()

        coordinate = ['Driver', (1, )]

        expected_params = [
            ("C1.a", [1.0, 1.0, 1.0]),
            ("C1.b", [2.0, 2.0, 2.0]),
        ]
        expected_unknowns = [
            ("C1.c", np.array([3.0, 3.0, 3.0])),
            ("C1.d", np.array([-1.0, -1.0, -1.0])),
            ("C1.out_string", "_C1"),
            ("C1.out_list", [1.5]),
        ]
        expected_resids = [
            ("C1.c", np.array([0.0, 0.0, 0.0])),
            ("C1.d", np.array([0.0, 0.0, 0.0])),
            ("C1.out_string", ""),
            ("C1.out_list", []),
        ]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns,
              expected_resids), ), self.eps, prob.root)

    def test_includes_and_excludes(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')

        prob.driver.add_recorder(self.recorder)

        self.recorder.options['includes'] = ['C1.*']
        self.recorder.options['excludes'] = ['*.out*']
        self.recorder.options['record_params'] = True
        self.recorder.options['record_resids'] = True

        prob.setup(check=False)

        t0, t1 = run(prob)
        self.recorder.close()

        coordinate = ['Driver', (1, )]

        expected_params = [
            ("C1.a", [1.0, 1.0, 1.0]),
            ("C1.b", [2.0, 2.0, 2.0]),
        ]

        expected_unknowns = [
            ("C1.c", np.array([3.0, 3.0, 3.0])),
            ("C1.d", np.array([-1.0, -1.0, -1.0])),
        ]

        expected_resids = [
            ("C1.c", np.array([0.0, 0.0, 0.0])),
            ("C1.d", np.array([0.0, 0.0, 0.0])),
        ]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns,
              expected_resids), ), self.eps, prob.root)

    def test_solver_record(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')
        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(prob)
        self.recorder.close()

        coordinate = ['Driver', (1, ), "root", (1, )]

        expected_params = [
            ("C1.a", [1.0, 1.0, 1.0]),
            ("C1.b", [2.0, 2.0, 2.0]),
        ]
        expected_unknowns = [
            ("G1.P1.x", np.array([1.0, 1.0, 1.0])),
            ("G1.P2.x", np.array([2.0, 2.0, 2.0])),
            ("C1.c", np.array([3.0, 3.0, 3.0])),
            ("C1.d", np.array([-1.0, -1.0, -1.0])),
            ("C1.out_string", "_C1"),
            ("C1.out_list", [1.5]),
        ]
        expected_resids = [
            ("G1.P1.x", np.array([0.0, 0.0, 0.0])),
            ("G1.P2.x", np.array([0.0, 0.0, 0.0])),
            ("C1.c", np.array([0.0, 0.0, 0.0])),
            ("C1.d", np.array([0.0, 0.0, 0.0])),
            ("C1.out_string", ""),
            ("C1.out_list", []),
        ]

        self.assertIterationDataRecorded(
            ((coordinate, (t0, t1), expected_params, expected_unknowns,
              expected_resids), ), self.eps, prob.root)

    def test_driver_records_metadata(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')

        prob.driver.add_recorder(self.recorder)

        self.recorder.options['record_metadata'] = True
        prob.setup(check=False)

        self.recorder.close()

        expected = (
            list(prob.root.params.iteritems()),
            list(prob.root.unknowns.iteritems()),
            list(prob.root.resids.iteritems()),
        )

        self.assertMetadataRecorded(expected)

    def test_driver_doesnt_records_metadata(self):
        size = 3

        prob = Problem(Group(), impl=impl)

        G1 = prob.root.add('G1', ParallelGroup())
        G1.add('P1', IndepVarComp('x', np.ones(size, float) * 1.0))
        G1.add('P2', IndepVarComp('x', np.ones(size, float) * 2.0))

        prob.root.add('C1', ABCDArrayComp(size))

        prob.root.connect('G1.P1.x', 'C1.a')
        prob.root.connect('G1.P2.x', 'C1.b')

        prob.driver.add_recorder(self.recorder)

        self.recorder.options['record_metadata'] = False
        prob.setup(check=False)

        self.recorder.close()

        self.assertMetadataRecorded(None)