示例#1
0
    def test_uniform(self):
        prob = Problem()
        model = prob.model

        model.add_subsystem('p1', IndepVarComp('x', 0.), promotes=['*'])
        model.add_subsystem('p2', IndepVarComp('y', 0.), promotes=['*'])
        model.add_subsystem('comp', Paraboloid(), promotes=['*'])

        model.add_design_var('x', lower=-10, upper=10)
        model.add_design_var('y', lower=-10, upper=10)
        model.add_objective('f_xy')

        prob.driver = DOEDriver(UniformGenerator(num_samples=5, seed=0))
        prob.driver.add_recorder(SqliteRecorder("CASES.db"))

        prob.setup(check=False)
        prob.run_driver()
        prob.cleanup()

        # all values should be between -10 and 10, check expected values for seed = 0
        expected = {
            0: {'x': np.array([ 0.97627008]), 'y': np.array([ 4.30378733])},
            1: {'x': np.array([ 2.05526752]), 'y': np.array([ 0.89766366])},
            2: {'x': np.array([-1.52690401]), 'y': np.array([ 2.91788226])},
            3: {'x': np.array([-1.24825577]), 'y': np.array([ 7.83546002])},
            4: {'x': np.array([ 9.27325521]), 'y': np.array([-2.33116962])},
        }

        cases = CaseReader("CASES.db").driver_cases

        self.assertEqual(cases.num_cases, 5)

        for n in range(cases.num_cases):
            assert_rel_error(self, cases.get_case(n).outputs['x'], expected[n]['x'], 1e-4)
            assert_rel_error(self, cases.get_case(n).outputs['y'], expected[n]['y'], 1e-4)
示例#2
0
    def test_plackett_burman(self):
        prob = Problem()
        model = prob.model

        model.add_subsystem('p1', IndepVarComp('x', 0.0), promotes=['x'])
        model.add_subsystem('p2', IndepVarComp('y', 0.0), promotes=['y'])
        model.add_subsystem('comp', Paraboloid(), promotes=['x', 'y', 'f_xy'])

        model.add_design_var('x', lower=0.0, upper=1.0)
        model.add_design_var('y', lower=0.0, upper=1.0)
        model.add_objective('f_xy')

        prob.driver = DOEDriver(PlackettBurmanGenerator())
        prob.driver.add_recorder(SqliteRecorder("CASES.db"))

        prob.setup(check=False)
        prob.run_driver()
        prob.cleanup()

        expected = {
            0: {'x': np.array([0.]), 'y': np.array([0.]), 'f_xy': np.array([22.00])},
            1: {'x': np.array([1.]), 'y': np.array([0.]), 'f_xy': np.array([17.00])},
            2: {'x': np.array([0.]), 'y': np.array([1.]), 'f_xy': np.array([31.00])},
            3: {'x': np.array([1.]), 'y': np.array([1.]), 'f_xy': np.array([27.00])},
        }

        cases = CaseReader("CASES.db").driver_cases

        self.assertEqual(cases.num_cases, 4)

        for n in range(cases.num_cases):
            self.assertEqual(cases.get_case(n).outputs['x'], expected[n]['x'])
            self.assertEqual(cases.get_case(n).outputs['y'], expected[n]['y'])
            self.assertEqual(cases.get_case(n).outputs['f_xy'], expected[n]['f_xy'])
示例#3
0
    def test_uniform(self):
        from openmdao.api import Problem, IndepVarComp
        from openmdao.test_suite.components.paraboloid import Paraboloid

        from openmdao.api import DOEDriver, UniformGenerator, SqliteRecorder, CaseReader

        prob = Problem()
        model = prob.model

        model.add_subsystem('p1', IndepVarComp('x', 0.), promotes=['*'])
        model.add_subsystem('p2', IndepVarComp('y', 0.), promotes=['*'])
        model.add_subsystem('comp', Paraboloid(), promotes=['*'])

        model.add_design_var('x', lower=-10, upper=10)
        model.add_design_var('y', lower=-10, upper=10)
        model.add_objective('f_xy')

        prob.driver = DOEDriver(UniformGenerator(num_samples=5))
        prob.driver.add_recorder(SqliteRecorder("CASES.db"))

        prob.setup()
        prob.run_driver()
        prob.cleanup()

        cases = CaseReader("CASES.db").driver_cases

        self.assertEqual(cases.num_cases, 5)

        values = []
        for n in range(cases.num_cases):
            case = cases.get_case(n)
            values.append((case.outputs['x'], case.outputs['y'], case.outputs['f_xy']))
示例#4
0
    def compute(self, inputs, outputs):
        
        folder_output       = self.options['folder_output']
        optimization_log    = self.options['folder_output'] + self.options['optimization_log']

        if os.path.exists(optimization_log):
        
            cr = CaseReader(optimization_log)
            cases = cr.list_cases()
            rec_data = {}
            iterations = []
            for i, casei in enumerate(cases):
                iterations.append(i)
                it_data = cr.get_case(casei)
                
                # parameters = it_data.get_responses()
                for parameters in [it_data.get_responses(), it_data.get_design_vars()]:
                    for j, param in enumerate(parameters.keys()):
                        if i == 0:
                            rec_data[param] = []
                        rec_data[param].append(parameters[param])

            for param in rec_data.keys():
                fig, ax = plt.subplots(1,1,figsize=(5.3, 4))
                ax.plot(iterations, rec_data[param])
                ax.set(xlabel='Number of Iterations' , ylabel=param)
                fig_name = 'Convergence_trend_' + param + '.pdf'
                fig.savefig(folder_output + fig_name)
                plt.close(fig)
 def _check_recorder_file(self, pb, cstr, filename):
     pb.driver = OneraSegoDriver()
     pb.driver.options["optimizer"] = "SEGOMOE"
     pb.driver.opt_settings["maxiter"] = 10
     # default model
     n_var = 2
     mod_obj = {
         "type": "Krig",
         "corr": "squared_exponential",
         "regr": "constant",
         "theta0": [1.0] * n_var,
         "thetaL": [0.1] * n_var,
         "thetaU": [10.0] * n_var,
         "normalize": True,
     }
     model_type = {"obj": mod_obj}
     if cstr:
         model_type["con"] = mod_obj
     pb.driver.opt_settings["model_type"] = model_type
     recorder = SqliteRecorder(self.case_recorder_filename)
     pb.model.add_recorder(recorder)
     pb.setup()
     self.pb.run_driver()
     self.assertTrue(os.path.exists(self.case_recorder_filename))
     reader = CaseReader(self.case_recorder_filename)
     for case_id in reader.list_cases():
         case = reader.get_case(case_id)
         print(case.outputs["obj"])
示例#6
0
    def test_full_factorial_array(self):
        prob = Problem()
        model = prob.model

        model.add_subsystem('p1',
                            IndepVarComp('xy', np.array([0., 0.])),
                            promotes=['*'])
        model.add_subsystem('comp', ParaboloidArray(), promotes=['*'])

        model.add_design_var('xy',
                             lower=np.array([-50., -50.]),
                             upper=np.array([50., 50.]))
        model.add_objective('f_xy')

        prob.driver = DOEDriver(FullFactorialGenerator(levels=3))
        prob.driver.add_recorder(SqliteRecorder("cases.sql"))

        prob.setup(check=False)
        prob.run_driver()
        prob.cleanup()

        expected = {
            0: {
                'xy': np.array([-50., -50.])
            },
            1: {
                'xy': np.array([0., -50.])
            },
            2: {
                'xy': np.array([50., -50.])
            },
            3: {
                'xy': np.array([-50., 0.])
            },
            4: {
                'xy': np.array([0., 0.])
            },
            5: {
                'xy': np.array([50., 0.])
            },
            6: {
                'xy': np.array([-50., 50.])
            },
            7: {
                'xy': np.array([0., 50.])
            },
            8: {
                'xy': np.array([50., 50.])
            },
        }

        cases = CaseReader("cases.sql").driver_cases

        self.assertEqual(cases.num_cases, 9)

        for n in range(cases.num_cases):
            outputs = cases.get_case(n).outputs
            self.assertEqual(outputs['xy'][0], expected[n]['xy'][0])
            self.assertEqual(outputs['xy'][1], expected[n]['xy'][1])
示例#7
0
    def test_box_behnken(self):
        upper = 10.
        center = 1

        prob = Problem()
        model = prob.model

        indep = model.add_subsystem('indep', IndepVarComp(), promotes=['*'])
        indep.add_output('x', 0.0)
        indep.add_output('y', 0.0)
        indep.add_output('z', 0.0)

        model.add_subsystem('comp', ExecComp('a = x**2 + y - z'), promotes=['*'])

        model.add_design_var('x', lower=0., upper=upper)
        model.add_design_var('y', lower=0., upper=upper)
        model.add_design_var('z', lower=0., upper=upper)

        model.add_objective('a')

        prob.driver = DOEDriver(BoxBehnkenGenerator(center=center))
        prob.driver.add_recorder(SqliteRecorder("CASES.db"))

        prob.setup(check=False)
        prob.run_driver()
        prob.cleanup()

        cases = CaseReader("CASES.db").driver_cases

        # The Box-Behnken design for 3 factors involves three blocks, in each of
        # which 2 factors are varied thru the 4 possible combinations of high & low.
        # It also includes centre points (all factors at their central values).
        # ref: https://en.wikipedia.org/wiki/Box-Behnken_design
        self.assertEqual(cases.num_cases, (3*4)+center)

        expected = {
            0:  {'x': np.array([ 0.]), 'y': np.array([ 0.]), 'z': np.array([ 5.])},
            1:  {'x': np.array([10.]), 'y': np.array([ 0.]), 'z': np.array([ 5.])},
            2:  {'x': np.array([ 0.]), 'y': np.array([10.]), 'z': np.array([ 5.])},
            3:  {'x': np.array([10.]), 'y': np.array([10.]), 'z': np.array([ 5.])},

            4:  {'x': np.array([ 0.]), 'y': np.array([ 5.]), 'z': np.array([ 0.])},
            5:  {'x': np.array([10.]), 'y': np.array([ 5.]), 'z': np.array([ 0.])},
            6:  {'x': np.array([ 0.]), 'y': np.array([ 5.]), 'z': np.array([10.])},
            7:  {'x': np.array([10.]), 'y': np.array([ 5.]), 'z': np.array([10.])},

            8:  {'x': np.array([ 5.]), 'y': np.array([ 0.]), 'z': np.array([ 0.])},
            9:  {'x': np.array([ 5.]), 'y': np.array([10.]), 'z': np.array([ 0.])},
            10: {'x': np.array([ 5.]), 'y': np.array([ 0.]), 'z': np.array([10.])},
            11: {'x': np.array([ 5.]), 'y': np.array([10.]), 'z': np.array([10.])},

            12: {'x': np.array([ 5.]), 'y': np.array([ 5.]), 'z': np.array([ 5.])},
        }

        for n in range(cases.num_cases):
            self.assertEqual(cases.get_case(n).outputs['x'], expected[n]['x'])
            self.assertEqual(cases.get_case(n).outputs['y'], expected[n]['y'])
            self.assertEqual(cases.get_case(n).outputs['z'], expected[n]['z'])
    def assert_morris_case_generation(self, nt, driver):
        pb, case_recorder_filename = TestSalibDoeDriver.run_driver(
            "morris" + str(nt), driver)

        self.assertTrue(os.path.exists(case_recorder_filename))
        reader = CaseReader(case_recorder_filename)
        cases = reader.list_cases("driver")
        os.remove(case_recorder_filename)
        n = sum(data["size"] for data in pb.model.get_design_vars().values())
        self.assertEqual(len(cases), (n + 1) * nt)
示例#9
0
    def test_latin_hypercube_center(self):
        samples = 4
        upper = 10.

        prob = Problem()
        model = prob.model

        indep = model.add_subsystem('indep', IndepVarComp())
        indep.add_output('x', 0.0)
        indep.add_output('y', 0.0)

        model.add_subsystem('comp', Paraboloid())

        model.connect('indep.x', 'comp.x')
        model.connect('indep.y', 'comp.y')

        model.add_design_var('indep.x', lower=0., upper=upper)
        model.add_design_var('indep.y', lower=0., upper=upper)

        model.add_objective('comp.f_xy')

        prob.driver = DOEDriver(LatinHypercubeGenerator(samples=samples, criterion='c'))
        prob.driver.add_recorder(SqliteRecorder("CASES.db"))

        prob.setup(check=False)
        prob.run_driver()
        prob.cleanup()

        cases = CaseReader("CASES.db").driver_cases

        self.assertEqual(cases.num_cases, samples)

        # the sample space for each variable (0 to upper) should be divided into
        # equal size buckets and each variable should have a value in each bucket
        bucket_size = upper/samples
        all_buckets = set(range(samples))

        x_buckets_filled = set()
        y_buckets_filled = set()

        # with criterion of 'center', each value should be in the center of it's bucket
        valid_values = [round(bucket_size*(bucket + 1/2), 3) for bucket in all_buckets]

        for n in range(cases.num_cases):
            x = float(cases.get_case(n).outputs['indep.x'])
            y = float(cases.get_case(n).outputs['indep.y'])

            x_buckets_filled.add(int(x/bucket_size))
            y_buckets_filled.add(int(y/bucket_size))

            self.assertTrue(round(x, 3) in valid_values, '%f not in %s' % (x, valid_values))
            self.assertTrue(round(y, 3) in valid_values, '%f not in %s' % (y, valid_values))

        self.assertEqual(x_buckets_filled, all_buckets)
        self.assertEqual(y_buckets_filled, all_buckets)
示例#10
0
    def test_recording(self):
        # coloring involves an underlying call to run_model (and final_setup),
        # this verifies that it is handled properly by the recording setup logic
        recorder = SqliteRecorder('cases.sql')

        p = run_opt(pyOptSparseDriver, 'auto', assemble_type='csc', optimizer='SNOPT',
                    dynamic_simul_derivs=True, print_results=False, recorder=recorder)

        cr = CaseReader('cases.sql')

        self.assertEqual(cr.list_cases(), ['rank0:SNOPT|%d' % i for i in range(p.driver.iter_count)])
示例#11
0
    def test_recording(self):
        # coloring involves an underlying call to run_model (and final_setup),
        # this verifies that it is handled properly by the recording setup logic
        recorder = SqliteRecorder('cases.sql')

        p = run_opt(pyOptSparseDriver, 'auto', assemble_type='csc', optimizer='SNOPT',
                    dynamic_simul_derivs=True, print_results=False, recorder=recorder)

        cr = CaseReader('cases.sql')

        self.assertEqual(cr.list_cases(), ['rank0:SNOPT|%d' % i for i in range(p.driver.iter_count)])
示例#12
0
 def test_resids(self):
     """ Tests that the reader returns resids correctly. """
     cr = CaseReader(self.filename)
     last_case = cr.get_case(-1)
     last_case_id = cr.list_cases()[-1]
     with SqliteDict(self.filename, 'iterations', flag='r') as db:
         for key in db[last_case_id]['Residuals'].keys():
             val = db[last_case_id]['Residuals'][key][()]
             np.testing.assert_almost_equal(last_case.resids[key], val,
                                            err_msg='Case reader gives '
                                                    'incorrect Unknown value'
                                                    ' for {0}'.format(key))
示例#13
0
 def test_unknowns(self):
     """ Tests that the reader returns unknowns correctly. """
     cr = CaseReader(self.filename)
     last_case = cr.get_case(-1)
     last_case_id = cr.list_cases()[-1]
     with h5py.File(self.filename, 'r') as f:
         for key in f[last_case_id]['Unknowns'].keys():
             val = f[last_case_id]['Unknowns'][key][()]
             np.testing.assert_almost_equal(last_case[key], val,
                                            err_msg='Case reader gives '
                                                    'incorrect Unknown value'
                                                    ' for {0}'.format(key))
示例#14
0
 def test_unknowns(self):
     """ Tests that the reader returns unknowns correctly. """
     cr = CaseReader(self.filename)
     last_case = cr.get_case(-1)
     last_case_id = cr.list_cases()[-1]
     with h5py.File(self.filename, 'r') as f:
         for key in f[last_case_id]['Unknowns'].keys():
             val = f[last_case_id]['Unknowns'][key][()]
             np.testing.assert_almost_equal(last_case[key],
                                            val,
                                            err_msg='Case reader gives '
                                            'incorrect Unknown value'
                                            ' for {0}'.format(key))
示例#15
0
 def test_params(self):
     """ Tests that the reader returns params correctly. """
     cr = CaseReader(self.filename)
     last_case = cr.get_case(-1)
     last_case_id = cr.list_cases()[-1]
     n = cr.num_cases
     with SqliteDict(self.filename, 'iterations', flag='r') as db:
         for key in db[last_case_id]['Parameters'].keys():
             val = db[last_case_id]['Parameters'][key]
             np.testing.assert_almost_equal(last_case.parameters[key], val,
                                            err_msg='Case reader gives '
                                                'incorrect Parameter value'
                                                ' for {0}'.format(key))
示例#16
0
 def test_resids(self):
     """ Tests that the reader returns resids correctly. """
     cr = CaseReader(self.filename)
     last_case = cr.get_case(-1)
     last_case_id = cr.list_cases()[-1]
     n = cr.num_cases
     with h5py.File(self.filename, 'r') as f:
         for key in f[last_case_id]['Residuals'].keys():
             val = f[last_case_id]['Residuals'][key][()]
             np.testing.assert_almost_equal(last_case.resids[key], val,
                                            err_msg='Case reader gives'
                                                    ' incorrect Unknown'
                                                    ' value for'
                                                    ' {0}'.format(key))
    def assert_case_generation(self, n, driver):
        pb = SellarProblem()
        pb.driver = driver
        case_recorder_filename = "test_smt_doe_driver_{}.sqlite".format(n)
        recorder = SqliteRecorder(case_recorder_filename)
        pb.driver.add_recorder(recorder)
        pb.setup()
        pb.run_driver()
        pb.cleanup()

        reader = CaseReader(case_recorder_filename)
        cases = reader.list_cases("driver")
        os.remove(case_recorder_filename)
        self.assertEqual(len(cases), n)
示例#18
0
    def test_full_factorial(self):
        from openmdao.api import Problem, IndepVarComp
        from openmdao.test_suite.components.paraboloid import Paraboloid

        from openmdao.api import DOEDriver, FullFactorialGenerator
        from openmdao.api import SqliteRecorder, CaseReader

        from mpi4py import MPI

        prob = Problem()
        model = prob.model

        model.add_subsystem('p1', IndepVarComp('x', 0.0), promotes=['x'])
        model.add_subsystem('p2', IndepVarComp('y', 0.0), promotes=['y'])
        model.add_subsystem('comp', Paraboloid(), promotes=['x', 'y', 'f_xy'])

        model.add_design_var('x', lower=0.0, upper=1.0)
        model.add_design_var('y', lower=0.0, upper=1.0)
        model.add_objective('f_xy')

        prob.driver = DOEDriver(FullFactorialGenerator(levels=3))
        prob.driver.options['parallel'] = True

        prob.driver.add_recorder(SqliteRecorder("cases.sql"))

        prob.setup()
        prob.run_driver()
        prob.cleanup()

        self.assertEqual(MPI.COMM_WORLD.size, 2)

        # check recorded cases from each case file
        rank = MPI.COMM_WORLD.rank
        filename = "cases.sql_%d" % rank
        self.assertEqual(filename, "cases.sql_%d" % rank)

        cases = CaseReader(filename).driver_cases
        self.assertEqual(cases.num_cases, 5 if rank == 0 else 4)

        values = []
        for n in range(cases.num_cases):
            outputs = cases.get_case(n).outputs
            values.append((outputs['x'], outputs['y'], outputs['f_xy']))

        self.assertEqual(
            "\n" + "\n".join([
                "x: %5.2f, y: %5.2f, f_xy: %6.2f" % (x, y, f_xy)
                for x, y, f_xy in values
            ]), self.expect_text)
示例#19
0
 def test_resids(self):
     """ Tests that the reader returns resids correctly. """
     cr = CaseReader(self.filename)
     last_case = cr.get_case(-1)
     last_case_id = cr.list_cases()[-1]
     n = cr.num_cases
     with h5py.File(self.filename, 'r') as f:
         for key in f[last_case_id]['Residuals'].keys():
             val = f[last_case_id]['Residuals'][key][()]
             np.testing.assert_almost_equal(last_case.resids[key],
                                            val,
                                            err_msg='Case reader gives'
                                            ' incorrect Unknown'
                                            ' value for'
                                            ' {0}'.format(key))
示例#20
0
def load_from_sqlite(filename):
    reader = CaseReader(filename)
    cases = reader.list_cases("driver")
    if len(cases) == 0:
        raise Exception("No case found in {}".format(filename))

    # find driver name
    driver_first_coord = cases[0]
    m = re.match(r"\w+:(\w+)|.*", driver_first_coord)
    name = os.path.splitext(os.path.basename(filename))[0]
    if m:
        name = m.group(1)

    # format cases and statuses
    cases, statuses = format_upload_cases(reader)
    return name, cases, statuses
    def upload(self, sqlite_filename, analysis_id=None, operation_id=None, cleanup=False, dry_run=False):
        from socket import gethostname
        mda_id = self.get_analysis_id() if not analysis_id else analysis_id
        reader = CaseReader(sqlite_filename)
        cases = reader.list_cases('driver')
        if len(cases)==0:
            raise Exception("No case found in {}".format(sqlite_filename))

        driver_first_coord = cases[0]
        name = os.path.splitext(sqlite_filename)[0]
        m = re.match(r"\w+:(\w+)|.*", driver_first_coord)
        if m:
            name = m.group(1)
        cases, statuses = self._format_upload_cases(reader)
        if dry_run:
            WhatsOpt._print_cases(cases, statuses)
            exit()
        resp = None
        if operation_id:
            url =  self._endpoint(('/api/v1/operations/%s') % operation_id)
            operation_params = {'cases': cases}
            resp = self.session.patch(url, headers=self.headers, 
                                      json={'operation': operation_params})
        else:
            url =  self._endpoint(('/api/v1/analyses/%s/operations') % mda_id)
            if name=='LHS':
                driver='smt_doe_lhs'
            elif name=='Morris':
                driver='salib_doe_morris'
            elif name=='SLSQP':
                driver='scipy_optimizer_slsqp'
            else:
                # suppose name well-formed <lib>-<doe|optimizer|screening>-<algoname>
                # otherwise it will default to doe
                driver=name.lower()  
            operation_params = {'name': name,
                                'driver': driver,
                                'host': gethostname(),
                                'cases': cases,
                                'success': statuses}
            resp = self.session.post(url, headers=self.headers, 
                                     json={'operation': operation_params})
        resp.raise_for_status()
        print("Results data from {} uploaded with driver {}".format(sqlite_filename, driver))
        if cleanup:
            os.remove(sqlite_filename)
            print("%s removed" % sqlite_filename)
示例#22
0
    def test_fan_in_grouped(self):
        from openmdao.api import Problem
        from openmdao.test_suite.groups.parallel_groups import FanInGrouped

        from openmdao.api import DOEDriver, FullFactorialGenerator
        from openmdao.api import SqliteRecorder, CaseReader

        from mpi4py import MPI

        prob = Problem(FanInGrouped())
        model = prob.model

        model.add_design_var('iv.x1', lower=0.0, upper=1.0)
        model.add_design_var('iv.x2', lower=0.0, upper=1.0)

        model.add_objective('c3.y')

        prob.driver = DOEDriver(FullFactorialGenerator(levels=3))
        prob.driver.add_recorder(SqliteRecorder("cases.sql"))

        # run 2 cases at a time, each using 2 of our 4 procs
        doe_parallel = prob.driver.options['parallel'] = 2

        prob.setup()
        prob.run_driver()
        prob.cleanup()

        rank = MPI.COMM_WORLD.rank

        # check recorded cases from each case file
        if rank < doe_parallel:
            filename = "cases.sql_%d" % rank

            cases = CaseReader(filename).driver_cases

            values = []
            for n in range(cases.num_cases):
                outputs = cases.get_case(n).outputs
                values.append(
                    (outputs['iv.x1'], outputs['iv.x2'], outputs['c3.y']))

            self.assertEqual(
                "\n" + "\n".join([
                    "iv.x1: %5.2f, iv.x2: %5.2f, c3.y: %6.2f" % (x1, x2, y)
                    for x1, x2, y in values
                ]), self.expect_text)
示例#23
0
 def test_format_upload_cases(self):
     data, statuses = format_upload_cases(
         CaseReader(os.path.join(TestUtils.DATA_PATH, "test_doe.sqlite")))
     self.assertEqual(19.52945, round(data[0]["values"][4], 5))
     self.assertEqual("z", data[1]["varname"])
     self.assertEqual(1, data[2]["coord_index"])
     for i in statuses:
         self.assertEqual(1, i)
示例#24
0
class Reader:
    """`Reader` class loads and displays stored simulations using an OpenMDAO `CaseReader` class instance.

    Parameters
    ----------
    db : str
        Full path of the database where the solution is stored
    case_id : str, optional
        Case identifier, ``initial`` to load the first iteration, ``final`` to load the final solution.
        Default is ``final``
    db_exp : str or ``None``, optional
        Full path of the database where the explicit simulation is stored or ``None``. Default is ``None``

    Attributes
    ----------
    case_reader : CaseReader
        OpenMDAO `CaseReader` class instance for the implicit solution
    case_id : str
        Case identifier, ``initial`` to load the first iteration, ``final`` to load the final solution.
    case : Case
        OpenMDAO `Case` class instance identified by `case_id` within the `case_reader` object
    case_reader_exp : CaseReader or ``None``
        OpenMDAO `CaseReader` class instance for the explicit simulation or ``None``
    case : Case or ``None``
        OpenMDAO `Case` class instance identified by `case_id` within the `case_reader_exp` object or ``None``

    """
    def __init__(self, db, case_id='final', db_exp=None):
        """Init Reader class. """

        self.case_reader = CaseReader(db)

        if case_id in ['initial', 'final']:
            self.case_id = case_id
        else:
            raise ValueError("Case must be either 'initial' or 'final'")

        self.case = self.case_reader.get_case(self.case_id)

        # explicit simulation
        if db_exp is not None:
            self.case_reader_exp = CaseReader(db_exp)
            self.case_exp = self.case_reader_exp.get_case(-1)
        else:
            self.case_reader_exp = self.case_exp = None
示例#25
0
    def __init__(self, db, case_id='final', db_exp=None):
        """Init Reader class. """

        self.case_reader = CaseReader(db)

        if case_id in ['initial', 'final']:
            self.case_id = case_id
        else:
            raise ValueError("Case must be either 'initial' or 'final'")

        self.case = self.case_reader.get_case(self.case_id)

        # explicit simulation
        if db_exp is not None:
            self.case_reader_exp = CaseReader(db_exp)
            self.case_exp = self.case_reader_exp.get_case(-1)
        else:
            self.case_reader_exp = self.case_exp = None
示例#26
0
def load_sqlite_file(filename):
    log("Load {}...".format(filename))
    reader = CaseReader(filename)
    cases = reader.list_cases("driver", out_stream=None)
    if len(cases) == 0:
        raise Exception("No case found in {}".format(filename))

    # find driver name
    driver_first_coord = cases[0]
    m = re.match(r"\w+:(\w+)|.*", driver_first_coord)
    name = os.path.splitext(os.path.basename(filename))[0]
    if m:
        name = m.group(1)

    # format cases and statuses
    # cases : [{"varname": varname, "coord_index": idx, "values": [...]}*]
    cases, statuses = _format_upload_cases(reader)
    return name, cases, statuses
 def test_sellar(self):
     self.pb = pb = Problem(SellarMDA())
     pb.model.add_design_var("x", lower=0, upper=10)
     pb.model.add_design_var("z", lower=0, upper=10)
     pb.model.add_objective("obj")
     pb.model.add_constraint("con1", upper=0)
     pb.model.add_constraint("con2", upper=0)
     pb.driver = OneraSegoDriver(optimizer="SEGOMOE")
     pb.driver.opt_settings["maxiter"] = 10
     self.case_recorder_filename = "test_segomoe_driver_sellar.sqlite"
     recorder = SqliteRecorder(self.case_recorder_filename)
     pb.model.add_recorder(recorder)
     pb.setup()
     self.pb.run_driver()
     self.assertTrue(os.path.exists(self.case_recorder_filename))
     reader = CaseReader(self.case_recorder_filename)
     for case_id in reader.list_cases():
         case = reader.get_case(case_id)
         print(case.outputs["obj"])
示例#28
0
    def test_full_factorial(self):
        prob = Problem()
        model = prob.model

        model.add_subsystem('p1', IndepVarComp('x', 0.0), promotes=['x'])
        model.add_subsystem('p2', IndepVarComp('y', 0.0), promotes=['y'])
        model.add_subsystem('comp', Paraboloid(), promotes=['x', 'y', 'f_xy'])

        model.add_design_var('x', lower=0.0, upper=1.0)
        model.add_design_var('y', lower=0.0, upper=1.0)
        model.add_objective('f_xy')

        prob.driver = DOEDriver(generator=FullFactorialGenerator(levels=3))
        prob.driver.add_recorder(SqliteRecorder("CASES.db"))

        prob.setup(check=False)
        prob.run_driver()
        prob.cleanup()

        expected = {
            0: {'x': np.array([0.]), 'y': np.array([0.]), 'f_xy': np.array([22.00])},
            1: {'x': np.array([.5]), 'y': np.array([0.]), 'f_xy': np.array([19.25])},
            2: {'x': np.array([1.]), 'y': np.array([0.]), 'f_xy': np.array([17.00])},

            3: {'x': np.array([0.]), 'y': np.array([.5]), 'f_xy': np.array([26.25])},
            4: {'x': np.array([.5]), 'y': np.array([.5]), 'f_xy': np.array([23.75])},
            5: {'x': np.array([1.]), 'y': np.array([.5]), 'f_xy': np.array([21.75])},

            6: {'x': np.array([0.]), 'y': np.array([1.]), 'f_xy': np.array([31.00])},
            7: {'x': np.array([.5]), 'y': np.array([1.]), 'f_xy': np.array([28.75])},
            8: {'x': np.array([1.]), 'y': np.array([1.]), 'f_xy': np.array([27.00])},
        }

        cases = CaseReader("CASES.db").driver_cases

        self.assertEqual(cases.num_cases, 9)

        for n in range(cases.num_cases):
            self.assertEqual(cases.get_case(n).outputs['x'], expected[n]['x'])
            self.assertEqual(cases.get_case(n).outputs['y'], expected[n]['y'])
            self.assertEqual(cases.get_case(n).outputs['f_xy'], expected[n]['f_xy'])
示例#29
0
    def test_metadata(self):
        """ Test that metadata is correctly read.

        format_version should always be present.
         """
        cr = CaseReader(self.filename)
        self.assertEqual(cr.format_version,
                         format_version,
                         msg='incorrect format version')
        self.assertIsNone(cr.parameters,
                          msg='parameter metadata should be None')
        self.assertIsNone(cr.unknowns, msg='unknown metadata should be None')
示例#30
0
    def test_full_factorial(self):
        prob = Problem()
        model = prob.model

        model.add_subsystem('p1', IndepVarComp('x', 0.0), promotes=['x'])
        model.add_subsystem('p2', IndepVarComp('y', 0.0), promotes=['y'])
        model.add_subsystem('comp', Paraboloid(), promotes=['x', 'y', 'f_xy'])

        model.add_design_var('x', lower=0.0, upper=1.0)
        model.add_design_var('y', lower=0.0, upper=1.0)
        model.add_objective('f_xy')

        prob.driver = DOEDriver(FullFactorialGenerator(levels=3), parallel=True)
        prob.driver.add_recorder(SqliteRecorder("CASES.db"))

        prob.setup()

        failed, output = run_driver(prob)
        self.assertFalse(failed)

        prob.cleanup()

        expected = {
            0: {'x': np.array([0.]), 'y': np.array([0.]), 'f_xy': np.array([22.00])},
            1: {'x': np.array([.5]), 'y': np.array([0.]), 'f_xy': np.array([19.25])},
            2: {'x': np.array([1.]), 'y': np.array([0.]), 'f_xy': np.array([17.00])},

            3: {'x': np.array([0.]), 'y': np.array([.5]), 'f_xy': np.array([26.25])},
            4: {'x': np.array([.5]), 'y': np.array([.5]), 'f_xy': np.array([23.75])},
            5: {'x': np.array([1.]), 'y': np.array([.5]), 'f_xy': np.array([21.75])},

            6: {'x': np.array([0.]), 'y': np.array([1.]), 'f_xy': np.array([31.00])},
            7: {'x': np.array([.5]), 'y': np.array([1.]), 'f_xy': np.array([28.75])},
            8: {'x': np.array([1.]), 'y': np.array([1.]), 'f_xy': np.array([27.00])},
        }

        size = prob.comm.size
        rank = prob.comm.rank

        # cases will be split across files for each proc
        filename = "CASES.db_%d" % rank

        expect_msg = "Cases from rank %d are being written to %s." % (rank, filename)
        self.assertTrue(expect_msg in output)

        cases = CaseReader(filename).driver_cases

        # cases recorded on this proc
        num_cases = cases.num_cases
        self.assertEqual(num_cases, len(expected)//size+(rank<len(expected)%size))

        for n in range(num_cases):
            case = cases.get_case(n)
            idx = n * size + rank  # index of expected case

            self.assertEqual(cases.get_case(n).outputs['x'], expected[idx]['x'])
            self.assertEqual(cases.get_case(n).outputs['y'], expected[idx]['y'])
            self.assertEqual(cases.get_case(n).outputs['f_xy'], expected[idx]['f_xy'])
示例#31
0
def load_simulation_results(f):
    """
    Load PhaseSimulationResults or TrajectorySimulationResults from the given file

    Parameters
    ----------
    f : str
        The file path from which to load the simulation results.

    Returns
    -------
    res : PhaseSimulationResults or TrajectorySimulationResults
        The PhaseSimulationResults or TrajectorySimulationResults loaded from the given file.

    """
    cr = CaseReader(f)

    try:
        case = cr.system_cases.get_case(-1)
示例#32
0
def read_db(filename):

    dvs = {
        'r': 'climb.states:r',
        'h': 'climb.states:h',
        'v': 'climb.states:v',
        'gam': 'climb.states:gam',
        'm': 'climb.states:m',
        'alpha': 'climb.controls:alpha',
        't_duration': 'climb.t_duration',
    }

    states = {}

    cons = {}

    objs = {}

    data_all_iters = []

    cr = CaseReader(filename)

    case_keys = cr.driver_cases.list_cases()
    for case_key in case_keys:

        case = cr.driver_cases.get_case(case_key)

        data = {}

        for key, cr_key in iteritems(dvs):
            data[key] = case.desvars[cr_key]

        for key, cr_key in iteritems(states):
            data[key] = case.sysincludes[cr_key]

        for key, cr_key in iteritems(objs):
            data[key] = case.objectives[cr_key]

        for key, cr_key in iteritems(cons):
            data[key] = case.constraints[cr_key]

        data_all_iters.append(data)
示例#33
0
    def get_case_reader(self):
        # type: () -> CaseReader
        """Get the case reader of the problem

        Returns
        -------
            CaseReader
                The configured case reader for the Problem object

        Raises
        ------
            AssertionError
                If case reader file cannot be found
        """
        if not os.path.isfile(self.case_reader_path):
            raise AssertionError(
                'Could not find the case reader file {}.'.format(
                    self.case_reader_path))
            # Get all cases from the case reader and determine amount of cases
        return CaseReader(self.case_reader_path)
示例#34
0
    def setup_prob(self):
        """
        Short method to select the optimizer. Uses pyOptSparse if available,
        or Scipy's SLSQP otherwise.
        """

        try:  # Use pyOptSparse optimizer if installed
            from openmdao.api import pyOptSparseDriver
            self.prob.driver = pyOptSparseDriver()
            if self.prob_dict['optimizer'] == 'SNOPT':
                self.prob.driver.options['optimizer'] = "SNOPT"
                self.prob.driver.opt_settings = {'Major optimality tolerance': 1.0e-8,
                                                 'Major feasibility tolerance': 1.0e-8,
                                                 'Major iterations limit':400,
                                                 'Minor iterations limit':2000,
                                                 'Iterations limit':1000
                                                 }
            elif self.prob_dict['optimizer'] == 'ALPSO':
                self.prob.driver.options['optimizer'] = 'ALPSO'
                self.prob.driver.opt_settings = {'SwarmSize': 40,
                                                'maxOuterIter': 200,
                                                'maxInnerIter': 6,
                                                'rtol': 1e-5,
                                                'atol': 1e-5,
                                                'dtol': 1e-5,
                                                'printOuterIters': 1
                                                 }
            elif self.prob_dict['optimizer'] == 'NOMAD':
                self.prob.driver.options['optimizer'] = 'NOMAD'
                self.prob.driver.opt_settings = {'maxiter':1000,
                                                'minmeshsize':1e-12,
                                                'minpollsize':1e-12,
                                                'displaydegree':0,
                                                'printfile':1
                                                }
            elif self.prob_dict['optimizer'] == 'SLSQP':
                self.prob.driver.options['optimizer'] = 'SLSQP'
                self.prob.driver.opt_settings = {'ACC' : 1e-10
                                                }

        except:  # Use Scipy SLSQP optimizer if pyOptSparse not installed
            self.prob.driver = ScipyOptimizer()
            self.prob.driver.options['optimizer'] = 'SLSQP'
            self.prob.driver.options['disp'] = True
            self.prob.driver.options['tol'] = 1.0e-10

        # Actually call the OpenMDAO functions to add the design variables,
        # constraints, and objective.
        for desvar_name, desvar_data in iteritems(self.desvars):
            self.prob.driver.add_desvar(desvar_name, **desvar_data)
        for con_name, con_data in iteritems(self.constraints):
            self.prob.driver.add_constraint(con_name, **con_data)
        for obj_name, obj_data in iteritems(self.objective):
            self.prob.driver.add_objective(obj_name, **obj_data)

        # Use finite differences over the entire model if user selected it
        if self.prob_dict['force_fd']:
            self.prob.root.deriv_options['type'] = 'fd'

        # Record optimization history to a database.
        # Data saved here can be examined using `plot_all.py` or `OptView.py`
        if self.prob_dict['record_db']:
            recorder = SqliteRecorder(self.prob_dict['prob_name']+".db")
            recorder.options['record_params'] = True
            recorder.options['record_derivs'] = True
            self.prob.driver.add_recorder(recorder)

        # Profile (time) the problem
        if self.prob_dict['profile']:
            profile.setup(self.prob)
            profile.start()

        # Set up the problem
        self.prob.setup()

        # Use warm start from previous db file if desired.
        # Note that we only have access to the unknowns, not the gradient history.
        if self.prob_dict['previous_case_db'] is not None:

            # Open the previous case and start from the last iteration.
            # Change the -1 value in get_case() if you want to select a different iteration.
            cr = CaseReader(self.prob_dict['previous_case_db'])
            case = cr.get_case(-1)

            # Loop through the unknowns and set them for this problem.
            for param_name, param_data in iteritems(case.unknowns):
                self.prob[param_name] = param_data
示例#35
0
 def test_resids(self):
     """ Test that params is None if not provided in the recording. """
     cr = CaseReader(self.filename)
     last_case = cr.get_case(-1)
     self.assertIsNone(last_case.resids,
                       "Case erroneously contains resids.")
示例#36
0
 def test_derivs(self):
     """ Test that derivs is None if not provided in the recording. """
     cr = CaseReader(self.filename)
     last_case = cr.get_case(-1)
     self.assertIsNone(last_case.derivs,
                       "Case erroneously contains derivs.")