Example #1
0
    def test_array_outputs(self):
        meta = MetaModel()
        meta.add_param('x', np.zeros((2, 2)))
        meta.add_output('y', np.zeros(2,))
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add('meta', meta)
        prob.setup(check=False)

        prob['meta.train:x'] = [
            [[1.0, 1.0], [1.0, 1.0]],
            [[2.0, 1.0], [1.0, 1.0]],
            [[1.0, 2.0], [1.0, 1.0]],
            [[1.0, 1.0], [2.0, 1.0]],
            [[1.0, 1.0], [1.0, 2.0]]
        ]

        prob['meta.train:y'] = [[3.0, 1.0],
                                [2.0, 4.0],
                                [1.0, 7.0],
                                [6.0, -3.0],
                                [-2.0, 3.0]]

        prob['meta.x'] = [[1.0, 2.0], [1.0, 1.0]]
        prob.run()

        assert_rel_error(self, prob['meta.y'], np.array([1.0, 7.0]), .00001)
Example #2
0
    def test_prom_conns(self):
        # this test mimics some of the connections found in test_nozzle in pycycle. The bug was that
        # an unknown that was connected to one parameter
        # (desVars.Ps_exhaust to nozzle.press_calcs.Ps_exhaust), was not being connected to the
        # other parameters ('nozzle.ideal_flow.chem_eq.n2ls.P', 'nozzle.ideal_flow.mach_calc.Ps',
        # and 'nozzle.ideal_flow.props.tp2props.P') that were connected via input-input connections
        # to nozzle.press_calcs.Ps_exhaust.

        prob = Problem(root=Group())
        root = prob.root
        desVars = root.add("desVars", ParamComp('Ps_exhaust', 1.0), promotes=('Ps_exhaust',))
        nozzle = root.add("nozzle", Group())
        press_calcs = nozzle.add('press_calcs', ExecComp('out=Ps_exhaust'), promotes=('Ps_exhaust',))
        ideal_flow = nozzle.add("ideal_flow", Group())
        chem_eq = ideal_flow.add('chem_eq', Group(), promotes=('P',))
        n2ls = chem_eq.add("n2ls", ExecComp('out=P'), promotes=('P',))
        props = ideal_flow.add("props", Group(), promotes=('P',))
        tp2props = props.add("tp2props", ExecComp('out=P'), promotes=('P',))
        mach_calc = ideal_flow.add("mach_calc", ExecComp('out=Ps'), promotes=('Ps',))

        nozzle.connect('Ps_exhaust', 'ideal_flow.Ps')
        root.connect('Ps_exhaust', 'nozzle.Ps_exhaust')
        ideal_flow.connect('Ps', 'P')

        prob.setup(check=False)

        expected_targets = set(['nozzle.ideal_flow.chem_eq.n2ls.P',
                                'nozzle.press_calcs.Ps_exhaust',
                                'nozzle.ideal_flow.mach_calc.Ps',
                                'nozzle.ideal_flow.props.tp2props.P'])
        self.assertEqual(set(prob.root.connections), expected_targets)

        for tgt in expected_targets:
            self.assertTrue('desVars.Ps_exhaust' in prob.root.connections[tgt])
Example #3
0
    def test_unequal_training_inputs(self):

        meta = MetaModel()
        meta.add_param('x', 0.)
        meta.add_param('y', 0.)
        meta.add_output('f', 0.)
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add('meta', meta)
        prob.setup(check=False)

        prob['meta.train:x'] = [1.0, 1.0, 1.0, 1.0]
        prob['meta.train:y'] = [1.0, 2.0]
        prob['meta.train:f'] = [1.0, 1.0, 1.0, 1.0]

        prob['meta.x'] = 1.0
        prob['meta.y'] = 1.0

        with self.assertRaises(RuntimeError) as cm:
            prob.run()

        expected = "MetaModel: Each variable must have the same number" \
                   " of training points. Expected 4 but found" \
                   " 2 points for 'y'."

        self.assertEqual(str(cm.exception), expected)
Example #4
0
    def test_unequal_training_outputs(self):
        meta = MetaModel()
        meta.add_param("x", 0.0)
        meta.add_param("y", 0.0)
        meta.add_output("f", 0.0)
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add("meta", meta)
        prob.setup(check=False)

        prob["meta.train:x"] = [1.0, 1.0, 1.0, 1.0]
        prob["meta.train:y"] = [1.0, 2.0, 3.0, 4.0]
        prob["meta.train:f"] = [1.0, 1.0]

        prob["meta.x"] = 1.0
        prob["meta.y"] = 1.0

        with self.assertRaises(RuntimeError) as cm:
            prob.run()

        expected = (
            "MetaModel: Each variable must have the same number"
            " of training points. Expected 4 but found"
            " 2 points for 'f'."
        )

        self.assertEqual(str(cm.exception), expected)
Example #5
0
    def test_array_inputs(self):
        meta = MetaModel()
        meta.add_param("x", np.zeros((2, 2)))
        meta.add_output("y1", 0.0)
        meta.add_output("y2", 0.0)
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add("meta", meta)
        prob.setup(check=False)

        prob["meta.train:x"] = [
            [[1.0, 1.0], [1.0, 1.0]],
            [[2.0, 1.0], [1.0, 1.0]],
            [[1.0, 2.0], [1.0, 1.0]],
            [[1.0, 1.0], [2.0, 1.0]],
            [[1.0, 1.0], [1.0, 2.0]],
        ]
        prob["meta.train:y1"] = [3.0, 2.0, 1.0, 6.0, -2.0]
        prob["meta.train:y2"] = [1.0, 4.0, 7.0, -3.0, 3.0]

        prob["meta.x"] = [[1.0, 2.0], [1.0, 1.0]]
        prob.run()

        assert_rel_error(self, prob["meta.y1"], 1.0, 0.00001)
        assert_rel_error(self, prob["meta.y2"], 7.0, 0.00001)
Example #6
0
    def test_parab_FD(self):

        model = Problem(impl=impl)
        root = model.root = Group()
        par = root.add('par', ParallelGroup())

        par.add('c1', Parab1D(root=2.0))
        par.add('c2', Parab1D(root=3.0))

        root.add('p1', ParamComp('x', val=0.0))
        root.add('p2', ParamComp('x', val=0.0))
        root.connect('p1.x', 'par.c1.x')
        root.connect('p2.x', 'par.c2.x')

        root.add('sumcomp', ExecComp('sum = x1+x2'))
        root.connect('par.c1.y', 'sumcomp.x1')
        root.connect('par.c2.y', 'sumcomp.x2')

        driver = model.driver = pyOptSparseDriver()
        driver.add_param('p1.x', low=-100, high=100)
        driver.add_param('p2.x', low=-100, high=100)
        driver.add_objective('sumcomp.sum')

        root.fd_options['force_fd'] = True

        model.setup(check=False)
        model.run()

        if not MPI or self.comm.rank == 0:
            assert_rel_error(self, model['p1.x'], 2.0, 1.e-6)
            assert_rel_error(self, model['p2.x'], 3.0, 1.e-6)
Example #7
0
    def test_parab_subbed_Pcomps(self):

        model = Problem(impl=impl)
        root = model.root = Group()
        root.ln_solver = lin_solver()

        par = root.add('par', ParallelGroup())

        par.add('s1', MP_Point(root=2.0))
        par.add('s2', MP_Point(root=3.0))

        root.add('sumcomp', ExecComp('sum = x1+x2'))
        root.connect('par.s1.c.y', 'sumcomp.x1')
        root.connect('par.s2.c.y', 'sumcomp.x2')

        driver = model.driver = pyOptSparseDriver()
        driver.add_param('par.s1.p.x', low=-100, high=100)
        driver.add_param('par.s2.p.x', low=-100, high=100)
        driver.add_objective('sumcomp.sum')

        model.setup(check=False)
        model.run()

        if not MPI or self.comm.rank == 0:
            assert_rel_error(self, model['par.s1.p.x'], 2.0, 1.e-6)

        if not MPI or self.comm.rank == 1:
            assert_rel_error(self, model['par.s2.p.x'], 3.0, 1.e-6)
Example #8
0
    def test_parab_FD(self):

        model = Problem(impl=impl)
        root = model.root = Group()
        par = root.add("par", ParallelGroup())

        par.add("c1", Parab1D(root=2.0))
        par.add("c2", Parab1D(root=3.0))

        root.add("p1", ParamComp("x", val=0.0))
        root.add("p2", ParamComp("x", val=0.0))
        root.connect("p1.x", "par.c1.x")
        root.connect("p2.x", "par.c2.x")

        root.add("sumcomp", ExecComp("sum = x1+x2"))
        root.connect("par.c1.y", "sumcomp.x1")
        root.connect("par.c2.y", "sumcomp.x2")

        driver = model.driver = pyOptSparseDriver()
        driver.add_param("p1.x", low=-100, high=100)
        driver.add_param("p2.x", low=-100, high=100)
        driver.add_objective("sumcomp.sum")

        root.fd_options["force_fd"] = True

        model.setup(check=False)
        model.run()

        if not MPI or self.comm.rank == 0:
            assert_rel_error(self, model["p1.x"], 2.0, 1.0e-6)
            assert_rel_error(self, model["p2.x"], 3.0, 1.0e-6)
Example #9
0
    def test_parab_FD_subbed_Pcomps(self):

        model = Problem(impl=impl)
        root = model.root = Group()
        par = root.add("par", ParallelGroup())

        par.add("s1", MP_Point(root=2.0))
        par.add("s2", MP_Point(root=3.0))

        root.add("sumcomp", ExecComp("sum = x1+x2"))
        root.connect("par.s1.c.y", "sumcomp.x1")
        root.connect("par.s2.c.y", "sumcomp.x2")

        driver = model.driver = pyOptSparseDriver()
        driver.add_param("par.s1.p.x", low=-100, high=100)
        driver.add_param("par.s2.p.x", low=-100, high=100)
        driver.add_objective("sumcomp.sum")

        root.fd_options["force_fd"] = True

        model.setup(check=False)
        model.run()

        if not MPI or self.comm.rank == 0:
            assert_rel_error(self, model["par.s1.p.x"], 2.0, 1.0e-6)

        if not MPI or self.comm.rank == 1:
            assert_rel_error(self, model["par.s2.p.x"], 3.0, 1.0e-6)
    def test_one_dim_bi_fidelity_training(self):

        mm = MultiFiMetaModel(nfi=2)
        mm.add_param('x', 0.)
        surr = MockSurrogate()
        mm.add_output('y', 0., surrogate = surr)

        prob = Problem(Group())
        prob.root.add('mm', mm)
        prob.setup(check=False)

        prob['mm.train:x']= [0.0, 0.4, 1.0]
        prob['mm.train:x_fi2'] = [0.1, 0.2, 0.3, 0.5, 0.6,
                                  0.7, 0.8, 0.9, 0.0, 0.4, 1.0]
        prob['mm.train:y'] = [3.02720998, 0.11477697, 15.82973195]
        prob['mm.train:y_fi2'] = [-9.32828839, -8.31986355, -7.00778837,
                                  -4.54535129, -4.0747189 , -5.30287702,
                                  -4.47456522, 1.85597517, -8.48639501,
                                  -5.94261151, 7.91486597]
        expected_xtrain=[np.array([[0.0], [0.4], [1.0]]),
                         np.array([[0.1], [0.2], [0.3], [0.5], [0.6], [0.7],
                                   [0.8], [0.9], [0.0], [0.4], [1.0]])]
        expected_ytrain=[np.array([[  3.02720998], [0.11477697], [15.82973195]]),
                         np.array([[-9.32828839], [-8.31986355], [-7.00778837], [-4.54535129],
                                   [-4.0747189], [-5.30287702], [-4.47456522], [1.85597517],
                                   [-8.48639501], [-5.94261151],  [7.91486597]])]
        prob.run()
        np.testing.assert_array_equal(surr.xtrain[0], expected_xtrain[0])
        np.testing.assert_array_equal(surr.xtrain[1], expected_xtrain[1])
        np.testing.assert_array_equal(surr.ytrain[0], expected_ytrain[0])
        np.testing.assert_array_equal(surr.ytrain[1], expected_ytrain[1])
Example #11
0
    def test_vector_inputs(self):

        meta = MetaModel()
        meta.add_param('x', np.zeros(4))
        meta.add_output('y1', 0.)
        meta.add_output('y2', 0.)
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add('meta', meta)
        prob.setup(check=False)

        prob['meta.train:x'] = [
            [1.0, 1.0, 1.0, 1.0],
            [2.0, 1.0, 1.0, 1.0],
            [1.0, 2.0, 1.0, 1.0],
            [1.0, 1.0, 2.0, 1.0],
            [1.0, 1.0, 1.0, 2.0]
        ]
        prob['meta.train:y1'] = [3.0, 2.0, 1.0, 6.0, -2.0]
        prob['meta.train:y2'] = [1.0, 4.0, 7.0, -3.0, 3.0]

        prob['meta.x'] = [1.0, 2.0, 1.0, 1.0]
        prob.run()

        assert_rel_error(self, prob['meta.y1'], 1.0, .00001)
        assert_rel_error(self, prob['meta.y2'], 7.0, .00001)
Example #12
0
    def test_simple_deriv_xfer(self):

        prob = Problem(impl=impl)
        prob.root = FanInGrouped()
        prob.setup(check=False)

        prob.root.comp3.dpmat[None]['x1'] = 7.
        prob.root.comp3.dpmat[None]['x2'] = 11.
        prob.root._transfer_data(mode='rev', deriv=True)

        if not MPI or self.comm.rank == 0:
            self.assertEqual(prob.root.sub.comp1.dumat[None]['y'], 7.)

        if not MPI or self.comm.rank == 1:
            self.assertEqual(prob.root.sub.comp2.dumat[None]['y'], 11.)

        prob.root.comp3.dpmat[None]['x1'] = 0.
        prob.root.comp3.dpmat[None]['x2'] = 0.
        self.assertEqual(prob.root.comp3.dpmat[None]['x1'], 0.)
        self.assertEqual(prob.root.comp3.dpmat[None]['x2'], 0.)

        prob.root._transfer_data(mode='fwd', deriv=True)

        self.assertEqual(prob.root.comp3.dpmat[None]['x1'], 7.)
        self.assertEqual(prob.root.comp3.dpmat[None]['x2'], 11.)
Example #13
0
    def test_math(self):
        prob = Problem(root=Group())
        C1 = prob.root.add('C1', ExecComp('y=sin(x)', x=2.0))
        self.assertTrue('x' in C1._params_dict)
        self.assertTrue('y' in C1._unknowns_dict)

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

        assert_rel_error(self, C1.unknowns['y'], math.sin(2.0), 0.00001)
Example #14
0
    def test_array(self):
        prob = Problem(root=Group())
        C1 = prob.root.add('C1', ExecComp('y=x[1]', x=np.array([1.,2.,3.]), y=0.0))
        self.assertTrue('x' in C1._params_dict)
        self.assertTrue('y' in C1._unknowns_dict)

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

        assert_rel_error(self, C1.unknowns['y'], 2.0, 0.00001)
Example #15
0
    def test_double_arraycomp(self):
        # Mainly testing a bug in the array return for multiple arrays

        group = Group()
        group.add('x_param1', IndepVarComp('x1', np.ones((2))), promotes=['*'])
        group.add('x_param2', IndepVarComp('x2', np.ones((2))), promotes=['*'])
        group.add('mycomp', DoubleArrayComp(), promotes=['*'])

        prob = Problem(impl=impl)
        prob.root = group
        prob.root.ln_solver = PetscKSP()
        prob.setup(check=False)
        prob.run()

        Jbase = group.mycomp.JJ

        J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='fwd',
                               return_format='array')
        diff = np.linalg.norm(J - Jbase)
        assert_rel_error(self, diff, 0.0, 1e-8)

        J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='fd',
                               return_format='array')
        diff = np.linalg.norm(J - Jbase)
        assert_rel_error(self, diff, 0.0, 1e-8)

        J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='rev',
                               return_format='array')
        diff = np.linalg.norm(J - Jbase)
        assert_rel_error(self, diff, 0.0, 1e-8)
Example #16
0
    def test_converge_diverge_compfd(self):

        prob = Problem(impl=impl)
        prob.root = ConvergeDivergePar()
        prob.root.ln_solver = PetscKSP()

        # fd comp2 and comp5. each is under a par group
        prob.root.par1.comp2.fd_options['force_fd'] = True
        prob.root.par2.comp5.fd_options['force_fd'] = True

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

        # Make sure value is fine.
        assert_rel_error(self, prob['comp7.y1'], -102.7, 1e-6)

        indep_list = ['p.x']
        unknown_list = ['comp7.y1']

        J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict')
        assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)

        J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict')
        assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)

        J = prob.calc_gradient(indep_list, unknown_list, mode='fd', return_format='dict')
        assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)
Example #17
0
    def test_mixed_type(self):
        prob = Problem(root=Group())
        C1 = prob.root.add('C1', ExecComp('y=numpy.sum(x)',
                                          x=np.arange(10,dtype=float)))
        self.assertTrue('x' in C1._params_dict)
        self.assertTrue('y' in C1._unknowns_dict)

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

        assert_rel_error(self, C1.unknowns['y'], 45.0, 0.00001)
Example #18
0
    def test_array_lhs(self):
        prob = Problem(root=Group())
        C1 = prob.root.add('C1', ExecComp(['y[0]=x[1]', 'y[1]=x[0]'],
                                          x=np.array([1.,2.,3.]), y=np.array([0.,0.])))
        self.assertTrue('x' in C1._params_dict)
        self.assertTrue('y' in C1._unknowns_dict)

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

        assert_rel_error(self, C1.unknowns['y'], np.array([2.,1.]), 0.00001)
Example #19
0
    def test_derivatives(self):
        meta = MetaModel()
        meta.add_param('x', 0.)
        meta.add_output('f', 0.)
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add('meta', meta, promotes=['x'])
        prob.root.add('p', IndepVarComp('x', 0.), promotes=['x'])
        prob.setup(check=False)

        prob['meta.train:x'] = [0., .25, .5, .75, 1.]
        prob['meta.train:f'] = [1., .75, .5, .25, 0.]
        prob['x'] = 0.125
        prob.run()

        Jf = prob.calc_gradient(['x'], ['meta.f'], mode='fwd')
        Jr = prob.calc_gradient(['x'], ['meta.f'], mode='rev')

        assert_rel_error(self, Jf[0][0], -1.00011, 1.0e-5)
        assert_rel_error(self, Jr[0][0], -1.00011, 1.0e-5)

        stream = cStringIO()
        prob.check_partial_derivatives(out_stream=stream)

        abs_errors = findall('Absolute Error \(.+\) : (.+)', stream.getvalue())
        self.assertTrue(len(abs_errors) > 0)
        for match in abs_errors:
            abs_error = float(match)
            self.assertTrue(abs_error < 1e-6)
Example #20
0
    def setUp(self):
        if SKIP:
            raise unittest.SkipTest('Could not import pyOptSparseDriver. Is pyoptsparse installed?')

        prob = Problem(impl=impl)
        root = prob.root = Group()
        #root.ln_solver = lin_solver()
        root.ln_solver = LinearGaussSeidel()
        par = root.add('par', ParallelGroup())
        par.ln_solver = LinearGaussSeidel()

        ser1 = par.add('ser1', Group())
        ser1.ln_solver = LinearGaussSeidel()

        ser1.add('p1', IndepVarComp('x', np.zeros([2])))
        ser1.add('comp', SimpleArrayComp())
        ser1.add('con', ExecComp('c = y - 20.0', c=np.array([0.0, 0.0]),
                                  y=np.array([0.0, 0.0])))
        ser1.add('obj', ExecComp('o = y[0]', y=np.array([0.0, 0.0])))

        ser2 = par.add('ser2', Group())
        ser2.ln_solver = LinearGaussSeidel()

        ser2.add('p1', IndepVarComp('x', np.zeros([2])))
        ser2.add('comp', SimpleArrayComp())
        ser2.add('con', ExecComp('c = y - 30.0', c=np.array([0.0, 0.0]),
                                 y=np.array([0.0, 0.0])))
        ser2.add('obj', ExecComp('o = y[0]', y=np.array([0.0, 0.0])))

        root.add('total', ExecComp('obj = x1 + x2'))

        ser1.connect('p1.x', 'comp.x')
        ser1.connect('comp.y', 'con.y')
        ser1.connect('comp.y', 'obj.y')
        root.connect('par.ser1.obj.o', 'total.x1')

        ser2.connect('p1.x', 'comp.x')
        ser2.connect('comp.y', 'con.y')
        ser2.connect('comp.y', 'obj.y')
        root.connect('par.ser2.obj.o', 'total.x2')

        prob.driver = pyOptSparseDriver()
        prob.driver.add_desvar('par.ser1.p1.x', low=-50.0, high=50.0)
        prob.driver.add_desvar('par.ser2.p1.x', low=-50.0, high=50.0)

        prob.driver.add_objective('total.obj')
        prob.driver.add_constraint('par.ser1.con.c', equals=0.0)
        prob.driver.add_constraint('par.ser2.con.c', equals=0.0)

        self.prob = prob
Example #21
0
    def test_complex_step(self):
        prob = Problem(root=Group())
        C1 = prob.root.add('C1', ExecComp(['y=2.0*x+1.'], x=2.0))

        self.assertTrue('x' in C1._params_dict)
        self.assertTrue('y' in C1._unknowns_dict)

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

        assert_rel_error(self, C1.unknowns['y'], 5.0, 0.00001)

        J = C1.jacobian(C1.params, C1.unknowns, C1.resids)

        assert_rel_error(self, J[('y','x')], 2.0, 0.00001)
Example #22
0
    def test_array_to_scalar(self):
        root = Group()

        root.add('P1', ParamComp('x', np.array([2., 3.])))
        root.add('C1', SimpleComp())
        root.add('C2', ExecComp('y = x * 3.', y=0., x=0.))

        root.connect('P1.x', 'C1.x', src_indices=[0,])
        root.connect('P1.x', 'C2.x', src_indices=[1,])

        prob = Problem(root)
        prob.setup(check=False)
        prob.run()

        self.assertAlmostEqual(root.C1.params['x'], 2.)
        self.assertAlmostEqual(root.C2.params['x'], 3.)
    def test_inputs_wrt_nfidelity(self):
        mm = MultiFiMetaModel(nfi=3)

        mm.add_param('x', 0.)
        mm.add_output('y', 0.)

        prob = Problem(Group())
        prob.root.add('mm', mm)
        prob.setup(check=False)

        self.assertEqual(prob['mm.train:x'], [])
        self.assertEqual(prob['mm.train:x_fi2'], [])
        self.assertEqual(prob['mm.train:x_fi3'], [])
        self.assertEqual(prob['mm.train:y'], [])
        self.assertEqual(prob['mm.train:y_fi2'], [])
        self.assertEqual(prob['mm.train:y_fi3'], [])
Example #24
0
    def test_subarray_to_promoted_var(self):
        root = Group()

        P = root.add('P', IndepVarComp('x', np.array([1., 2., 3., 4., 5.])))
        G = root.add('G', Group())
        C = root.add('C', SimpleComp())

        A  = G.add('A', SimpleArrayComp())
        G2 = G.add('G2', Group())

        A2 = G2.add('A2', SimpleArrayComp())

        root.connect('P.x', 'G.A.x', src_indices=[0,1])
        root.connect('P.x', 'C.x', src_indices=[2,])
        root.connect('P.x', 'G.G2.A2.x', src_indices=[3, 4])

        prob = Problem(root)
        prob.setup(check=False)
        prob.run()

        assert_rel_error(self, root.G.A.params['x'], np.array([1., 2.]), 0.0001)
        self.assertAlmostEqual(root.C.params['x'], 3.)
        assert_rel_error(self, root.G.G2.A2.params['x'], np.array([4., 5.]), 0.0001)

        # now try the same thing with promoted var
        root = Group()

        P = root.add('P', IndepVarComp('x', np.array([1., 2., 3., 4., 5.])))
        G = root.add('G', Group())
        C = root.add('C', SimpleComp())

        A  = G.add('A', SimpleArrayComp(), promotes=['x', 'y'])
        G2 = G.add('G2', Group())

        A2 = G2.add('A2', SimpleArrayComp(), promotes=['x', 'y'])

        root.connect('P.x', 'G.x', src_indices=[0,1])
        root.connect('P.x', 'C.x', src_indices=[2,])
        root.connect('P.x', 'G.G2.x', src_indices=[3, 4])

        prob = Problem(root)
        prob.setup(check=False)
        prob.run()

        assert_rel_error(self, root.G.A.params['x'], np.array([1., 2.]), 0.0001)
        self.assertAlmostEqual(root.C.params['x'], 3.)
        assert_rel_error(self, root.G.G2.A2.params['x'], np.array([4., 5.]), 0.0001)
    def test_one_dim_one_fidelity_training(self):

        mm = MultiFiMetaModel()

        mm.add_param('x', 0.)
        surr = MockSurrogate()
        mm.add_output('y', 0., surrogate = surr)

        prob = Problem(Group())
        prob.root.add('mm', mm)
        prob.setup(check=False)

        prob['mm.train:x'] = [0.0, 0.4, 1.0]
        prob['mm.train:y'] = [3.02720998, 0.11477697, 15.82973195]

        expected_xtrain=[np.array([ [0.0], [0.4], [1.0] ])]
        expected_ytrain=[np.array([ [3.02720998], [0.11477697], [15.82973195] ])]

        prob.run()
        np.testing.assert_array_equal(surr.xtrain, expected_xtrain)
        np.testing.assert_array_equal(surr.ytrain, expected_ytrain)

        expected_xpredict=0.5
        prob['mm.x'] = expected_xpredict
        prob.run()

        np.testing.assert_array_equal(surr.xpredict, expected_xpredict)
Example #26
0
    def test_fan_out_grouped(self):

        prob = Problem(impl=impl)
        prob.root = root = Group()

        root.add('p', IndepVarComp('x', 1.0))
        root.add('comp1', ExecComp(['y=3.0*x']))

        sub = root.add('sub', ParallelGroup())
        sub.add('comp2', ExecComp(['y=-2.0*x']))
        sub.add('comp3', ExecComp(['y=5.0*x']))

        root.add('c2', ExecComp(['y=-x']))
        root.add('c3', ExecComp(['y=3.0*x']))
        root.connect('sub.comp2.y', 'c2.x')
        root.connect('sub.comp3.y', 'c3.x')

        root.connect("comp1.y", "sub.comp2.x")
        root.connect("comp1.y", "sub.comp3.x")
        root.connect("p.x", "comp1.x")

        prob.root.ln_solver = LinearGaussSeidel()
        prob.root.sub.ln_solver = LinearGaussSeidel()

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

        param = 'p.x'
        unknown_list = ['sub.comp2.y', "sub.comp3.y"]

        J = prob.calc_gradient([param], unknown_list, mode='fwd', return_format='dict')

        assert_rel_error(self, J[unknown_list[0]][param][0][0], -6.0, 1e-6)
        assert_rel_error(self, J[unknown_list[1]][param][0][0], 15.0, 1e-6)

        J = prob.calc_gradient([param], unknown_list, mode='rev', return_format='dict')
        assert_rel_error(self, J[unknown_list[0]][param][0][0], -6.0, 1e-6)
        assert_rel_error(self, J[unknown_list[1]][param][0][0], 15.0, 1e-6)

        unknown_list = ['c2.y', "c3.y"]

        J = prob.calc_gradient([param], unknown_list, mode='fwd', return_format='dict')

        assert_rel_error(self, J[unknown_list[0]][param][0][0], 6.0, 1e-6)
        assert_rel_error(self, J[unknown_list[1]][param][0][0], 45.0, 1e-6)

        J = prob.calc_gradient([param], unknown_list, mode='rev', return_format='dict')
        assert_rel_error(self, J[unknown_list[0]][param][0][0], 6.0, 1e-6)
        assert_rel_error(self, J[unknown_list[1]][param][0][0], 45.0, 1e-6)
    def test_fd_options_meta_step_size(self):

        class MetaParaboloid(Component):
            """ Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """

            def __init__(self):
                super(MetaParaboloid, self).__init__()

                # Params
                self.add_param('x', 1.0, fd_step_size = 1.0e5)
                self.add_param('y', 1.0, fd_step_size = 1.0e5)

                # Unknowns
                self.add_output('f_xy', 0.0)

            def solve_nonlinear(self, params, unknowns, resids):
                """f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
                Optimal solution (minimum): x = 6.6667; y = -7.3333
                """

                x = params['x']
                y = params['y']

                f_xy = ((x-3.0)**2 + x*y + (y+4.0)**2 - 3.0)
                unknowns['f_xy'] = f_xy

            def jacobian(self, params, unknowns, resids):
                """Analytical derivatives"""

                x = params['x']
                y = params['y']
                J = {}

                J['f_xy', 'x'] = (2.0*x - 6.0 + y)
                J['f_xy', 'y'] = (2.0*y + 8.0 + x)

                return J

        prob = Problem()
        prob.root = Group()
        comp = prob.root.add('comp', MetaParaboloid())
        prob.root.add('p1', ParamComp('x', 15.0))
        prob.root.add('p2', ParamComp('y', 15.0))
        prob.root.connect('p1.x', 'comp.x')
        prob.root.connect('p2.y', 'comp.y')

        comp.fd_options['force_fd'] = True

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

        # Make sure bad meta step_size is used
        # Derivative should be way high with this.

        J = prob.calc_gradient(['p1.x'], ['comp.f_xy'], return_format='dict')
        self.assertGreater(J['comp.f_xy']['p1.x'][0][0], 1000.0)
    def setUp(self):
        self.startdir = os.getcwd()
        self.tempdir = tempfile.mkdtemp(prefix='test_extcode-')
        os.chdir(self.tempdir)
        shutil.copy(os.path.join(DIRECTORY, 'external_code_for_testing.py'),
                    os.path.join(self.tempdir, 'external_code_for_testing.py'))

        self.extcode = ExternalCodeForTesting()
        self.top = Problem()
        self.top.root = Group()

        self.top.root.add('extcode', self.extcode)
    def test_two_dim_bi_fidelity_training(self):
        mm = MultiFiMetaModel(nfi=2)
        mm.add_param('x1', 0.)
        mm.add_param('x2', 0.)
        surr_y1 = MockSurrogate()
        surr_y2 = MockSurrogate()
        mm.add_output('y1', 0., surrogate = surr_y1)
        mm.add_output('y2', 0., surrogate = surr_y2)

        prob = Problem(Group())
        prob.root.add('mm', mm)
        prob.setup(check=False)

        prob['mm.train:x1']     = [1.0, 2.0, 3.0]
        prob['mm.train:x1_fi2'] = [1.1, 2.1, 3.1, 1.0, 2.0, 3.0]
        prob['mm.train:x2']     = [1.0, 2.0, 3.0]
        prob['mm.train:x2_fi2'] = [2.1, 2.2, 2.3, 1.0, 2.0, 3.0]
        prob['mm.train:y1']     = [0.0, 0.1, 0.2]
        prob['mm.train:y1_fi2'] = [3.0, 3.1, 3.3, 3.4, 3.5 ,3.6]
        prob['mm.train:y2']     = [4.0, 4.0, 4.0]
        prob['mm.train:y2_fi2'] = [4.0, 4.1, 4.3, 4.4, 4.5 ,4.6]

        prob.run()
        expected_xtrain=[np.array([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]),
                         np.array([[1.1, 2.1], [2.1, 2.2], [3.1, 2.3],
                                   [1.0, 1.0], [2.0, 2.0], [3.0, 3.0]])]
        expected_y1train=[np.array([[0.0], [0.1], [0.2]]),
                          np.array([[3.0], [3.1], [3.3], [3.4], [3.5], [3.6]])]
        expected_y2train=[np.array([[4.0], [4.0], [4.0]]),
                          np.array([[4.0], [4.1], [4.3], [4.4], [4.5], [4.6]])]

        np.testing.assert_array_equal(surr_y1.ytrain[0], expected_y1train[0])
        np.testing.assert_array_equal(surr_y1.ytrain[1], expected_y1train[1])
        np.testing.assert_array_equal(surr_y2.ytrain[0], expected_y2train[0])
        np.testing.assert_array_equal(surr_y2.ytrain[1], expected_y2train[1])
        np.testing.assert_array_equal(surr_y1.ytrain[0], expected_y1train[0])
        np.testing.assert_array_equal(surr_y1.ytrain[1], expected_y1train[1])
        np.testing.assert_array_equal(surr_y2.ytrain[0], expected_y2train[0])
        np.testing.assert_array_equal(surr_y2.ytrain[1], expected_y2train[1])
Example #30
0
    def test_indices(self):
        size = 10

        root = Group()

        root.add('P1', ParamComp('x', np.zeros(size)))
        root.add('C1', ExecComp('y = x * 2.', y=np.zeros(size//2), x=np.zeros(size//2)))
        root.add('C2', ExecComp('y = x * 3.', y=np.zeros(size//2), x=np.zeros(size//2)))

        root.connect('P1.x', "C1.x", src_indices=list(range(size//2)))
        root.connect('P1.x', "C2.x", src_indices=list(range(size//2, size)))

        prob = Problem(root)
        prob.setup(check=False)

        root.P1.unknowns['x'][0:size//2] += 1.0
        root.P1.unknowns['x'][size//2:size] -= 1.0

        prob.run()

        assert_rel_error(self, root.C1.params['x'], np.ones(size//2), 0.0001)
        assert_rel_error(self, root.C2.params['x'], -np.ones(size//2), 0.0001)
    def test_overrides(self):
        class OverrideComp(Component):
            def __init__(self):
                super(OverrideComp, self).__init__()

                # Params
                self.add_param('x', 3.0)

                # Unknowns
                self.add_output('y', 5.5)

            def solve_nonlinear(self, params, unknowns, resids):
                """ Doesn't do much. """
                unknowns['y'] = 7.0 * params['x']

            def apply_linear(self, params, unknowns, dparams, dunknowns,
                             dresids, mode):
                """Never Call."""
                raise RuntimeError(
                    "This should have been overridden by force_fd.")

            def jacobian(self, params, unknowns, resids):
                """Never Call."""
                raise RuntimeError(
                    "This should have been overridden by force_fd.")

        prob = Problem()
        prob.root = Group()
        comp = prob.root.add('comp', OverrideComp())
        prob.root.add('p1', ParamComp('x', 2.0))
        prob.root.connect('p1.x', 'comp.x')

        comp.fd_options['force_fd'] = True

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

        J = prob.calc_gradient(['p1.x'], ['comp.y'],
                               mode='fwd',
                               return_format='dict')
        assert_rel_error(self, J['comp.y']['p1.x'][0][0], 7.0, 1e-6)
Example #32
0
    def test_single_diamond(self):

        prob = Problem(impl=impl)
        prob.root = SingleDiamondPar()
        prob.root.ln_solver = PetscKSP()
        prob.setup(check=False)
        prob.run()

        param_list = ['p.x']
        unknown_list = ['comp4.y1', 'comp4.y2']

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='fwd',
                               return_format='dict')
        assert_rel_error(self, J['comp4.y1']['p.x'][0][0], 25, 1e-6)
        assert_rel_error(self, J['comp4.y2']['p.x'][0][0], -40.5, 1e-6)

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='rev',
                               return_format='dict')
        assert_rel_error(self, J['comp4.y1']['p.x'][0][0], 25, 1e-6)
        assert_rel_error(self, J['comp4.y2']['p.x'][0][0], -40.5, 1e-6)
Example #33
0
    def test_fan_out_grouped(self):

        prob = Problem(impl=impl)
        prob.root = FanOutGrouped()
        prob.root.ln_solver = PetscKSP()
        prob.setup(check=False)
        prob.run()

        param_list = ['p.x']
        unknown_list = ['sub.comp2.y', "sub.comp3.y"]

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='fwd',
                               return_format='dict')
        assert_rel_error(self, J['sub.comp2.y']['p.x'][0][0], -6.0, 1e-6)
        assert_rel_error(self, J['sub.comp3.y']['p.x'][0][0], 15.0, 1e-6)

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='rev',
                               return_format='dict')
        assert_rel_error(self, J['sub.comp2.y']['p.x'][0][0], -6.0, 1e-6)
        assert_rel_error(self, J['sub.comp3.y']['p.x'][0][0], 15.0, 1e-6)
Example #34
0
    def test_fan_in(self):

        prob = Problem(impl=impl)
        prob.root = FanIn()
        prob.root.ln_solver = PetscKSP()
        prob.setup(check=False)
        prob.run()

        param_list = ['p1.x1', 'p2.x2']
        unknown_list = ['comp3.y']

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='fwd',
                               return_format='dict')
        assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6)
        assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='rev',
                               return_format='dict')
        assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6)
        assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)
Example #35
0
    def test_array_lhs(self):
        prob = Problem(root=Group())
        C1 = prob.root.add(
            'C1',
            ExecComp(['y[0]=x[1]', 'y[1]=x[0]'],
                     x=np.array([1., 2., 3.]),
                     y=np.array([0., 0.])))
        self.assertTrue('x' in C1._params_dict)
        self.assertTrue('y' in C1._unknowns_dict)

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

        assert_rel_error(self, C1.unknowns['y'], np.array([2., 1.]), 0.00001)
Example #36
0
    def test_complex_step(self):
        prob = Problem(root=Group())
        C1 = prob.root.add('C1', ExecComp(['y=2.0*x+1.'], x=2.0))

        self.assertTrue('x' in C1._params_dict)
        self.assertTrue('y' in C1._unknowns_dict)

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

        assert_rel_error(self, C1.unknowns['y'], 5.0, 0.00001)

        J = C1.jacobian(C1.params, C1.unknowns, C1.resids)

        assert_rel_error(self, J[('y', 'x')], 2.0, 0.00001)
Example #37
0
    def test_sin_metamodel(self):

        # create a MetaModel for Sin and add it to a Problem
        sin_mm = MetaModel()
        sin_mm.add_param('x', 0.)
        sin_mm.add_output('f_x', 0.)

        prob = Problem(Group())
        prob.root.add('sin_mm', sin_mm)

        # check that missing surrogate is detected in check_setup
        stream = cStringIO()
        prob.setup(out_stream=stream)
        msg = ("No default surrogate model is defined and the "
               "following outputs do not have a surrogate model:\n"
               "['f_x']\n"
               "Either specify a default_surrogate, or specify a "
               "surrogate model for all outputs.")
        self.assertTrue(msg in stream.getvalue())

        # check that output with no specified surrogate gets the default
        sin_mm.default_surrogate = FloatKrigingSurrogate()
        prob.setup(check=False)
        surrogate = prob.root.unknowns.metadata('sin_mm.f_x').get('surrogate')
        self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate),
                        'sin_mm.f_x should get the default surrogate')

        # train the surrogate and check predicted value
        prob['sin_mm.train:x'] = np.linspace(0, 10, 200)
        prob['sin_mm.train:f_x'] = .5 * np.sin(prob['sin_mm.train:x'])

        prob['sin_mm.x'] = 2.22

        prob.run()

        self.assertAlmostEqual(prob['sin_mm.f_x'],
                               .5 * np.sin(prob['sin_mm.x']),
                               places=5)
Example #38
0
    def test_sin_metamodel_obj_return(self):

        # create a MetaModel for Sin and add it to a Problem
        sin_mm = MetaModel()
        sin_mm.add_param('x', 0.)
        sin_mm.add_output('f_x', (0., 0.))

        prob = Problem(Group())
        prob.root.add('sin_mm', sin_mm)

        # check that missing surrogate is detected in check_setup
        stream = cStringIO()
        prob.setup(out_stream=stream)
        msg = ("No default surrogate model is defined and the "
               "following outputs do not have a surrogate model:\n"
               "['f_x']\n"
               "Either specify a default_surrogate, or specify a "
               "surrogate model for all outputs.")
        self.assertTrue(msg in stream.getvalue())

        # check that output with no specified surrogate gets the default
        sin_mm.default_surrogate = KrigingSurrogate()
        prob.setup(check=False)
        surrogate = prob.root.unknowns.metadata('sin_mm.f_x').get('surrogate')
        self.assertTrue(isinstance(surrogate, KrigingSurrogate),
                        'sin_mm.f_x should get the default surrogate')

        # train the surrogate and check predicted value
        prob['sin_mm.train:x'] = np.linspace(0, 10, 20)
        prob['sin_mm.train:f_x'] = np.sin(prob['sin_mm.train:x'])

        prob['sin_mm.x'] = 2.1

        prob.run()
        assert_rel_error(self, prob['sin_mm.f_x'][0], 0.86323233, 1e-4)  # mean
        self.assertTrue(self, prob['sin_mm.f_x'][1] < 1e-5)  #std deviation
Example #39
0
    def test_derivatives(self):
        meta = MetaModel()
        meta.add_param('x', 0.)
        meta.add_output('f', 0.)
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add('meta', meta, promotes=['x'])
        prob.root.add('p', ParamComp('x', 0.), promotes=['x'])
        prob.setup(check=False)

        prob['meta.train:x'] = [0., .25, .5, .75, 1.]
        prob['meta.train:f'] = [1., .75, .5, .25, 0.]
        prob['x'] = 0.125
        prob.run()

        stream = cStringIO()
        prob.check_partial_derivatives(out_stream=stream)

        abs_errors = findall('Absolute Error \(.+\) : (.+)', stream.getvalue())
        self.assertTrue(len(abs_errors) > 0)
        for match in abs_errors:
            abs_error = float(match)
            self.assertTrue(abs_error < 1e-6)
Example #40
0
    def test_array_to_scalar(self):
        root = Group()

        root.add('P1', ParamComp('x', np.array([2., 3.])))
        root.add('C1', SimpleComp())
        root.add('C2', ExecComp('y = x * 3.', y=0., x=0.))

        root.connect('P1.x', 'C1.x', src_indices=[
            0,
        ])
        root.connect('P1.x', 'C2.x', src_indices=[
            1,
        ])

        prob = Problem(root)
        prob.setup(check=False)
        prob.run()

        self.assertAlmostEqual(root.C1.params['x'], 2.)
        self.assertAlmostEqual(root.C2.params['x'], 3.)
Example #41
0
    def test_vector_inputs(self):

        meta = MetaModel()
        meta.add_param('x', np.zeros(4))
        meta.add_output('y1', 0.)
        meta.add_output('y2', 0.)
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add('meta', meta)
        prob.setup(check=False)

        prob['meta.train:x'] = [[1.0, 1.0, 1.0, 1.0], [2.0, 1.0, 1.0, 1.0],
                                [1.0, 2.0, 1.0, 1.0], [1.0, 1.0, 2.0, 1.0],
                                [1.0, 1.0, 1.0, 2.0]]
        prob['meta.train:y1'] = [3.0, 2.0, 1.0, 6.0, -2.0]
        prob['meta.train:y2'] = [1.0, 4.0, 7.0, -3.0, 3.0]

        prob['meta.x'] = [1.0, 2.0, 1.0, 1.0]
        prob.run()

        assert_rel_error(self, prob['meta.y1'], 1.0, .00001)
        assert_rel_error(self, prob['meta.y2'], 7.0, .00001)
Example #42
0
    def test_array_outputs(self):
        meta = MetaModel()
        meta.add_param('x', np.zeros((2, 2)))
        meta.add_output('y', np.zeros(2, ))
        meta.default_surrogate = FloatKrigingSurrogate()

        prob = Problem(Group())
        prob.root.add('meta', meta)
        prob.setup(check=False)

        prob['meta.train:x'] = [[[1.0, 1.0], [1.0, 1.0]],
                                [[2.0, 1.0], [1.0, 1.0]],
                                [[1.0, 2.0], [1.0, 1.0]],
                                [[1.0, 1.0], [2.0, 1.0]],
                                [[1.0, 1.0], [1.0, 2.0]]]

        prob['meta.train:y'] = [[3.0, 1.0], [2.0, 4.0], [1.0, 7.0],
                                [6.0, -3.0], [-2.0, 3.0]]

        prob['meta.x'] = [[1.0, 2.0], [1.0, 1.0]]
        prob.run()

        assert_rel_error(self, prob['meta.y'], np.array([1.0, 7.0]), .00001)
    def test_fd_options_form(self):

        prob = Problem()
        prob.root = Group()
        comp = prob.root.add('comp', Paraboloid())
        prob.root.add('p1', ParamComp('x', 15.0))
        prob.root.add('p2', ParamComp('y', 15.0))
        prob.root.connect('p1.x', 'comp.x')
        prob.root.connect('p2.y', 'comp.y')

        comp.fd_options['force_fd'] = True
        comp.fd_options['form'] = 'forward'

        param_list = ['p1.x']
        unknowns_list = ['comp.f_xy']
        prob.setup(check=False)
        prob.run()

        J = prob.calc_gradient(param_list, unknowns_list, return_format='dict')
        assert_rel_error(self, J['comp.f_xy']['p1.x'][0][0], 39.0, 1e-6)

        # Make sure it gives good result with small stepsize
        comp.fd_options['form'] = 'backward'

        J = prob.calc_gradient(['p1.x'], ['comp.f_xy'], return_format='dict')
        assert_rel_error(self, J['comp.f_xy']['p1.x'][0][0], 39.0, 1e-6)

        # Make sure it gives good result with small stepsize
        comp.fd_options['form'] = 'central'

        J = prob.calc_gradient(['p1.x'], ['comp.f_xy'], return_format='dict')
        assert_rel_error(self, J['comp.f_xy']['p1.x'][0][0], 39.0, 1e-6)

        # Now, Make sure we really are going foward and backward
        comp.fd_options['form'] = 'forward'
        comp.fd_options['step_size'] = 1e3
        J = prob.calc_gradient(['p1.x'], ['comp.f_xy'], return_format='dict')
        self.assertGreater(J['comp.f_xy']['p1.x'][0][0], 0.0)

        comp.fd_options['form'] = 'backward'
        J = prob.calc_gradient(['p1.x'], ['comp.f_xy'], return_format='dict')
        self.assertLess(J['comp.f_xy']['p1.x'][0][0], 0.0)

        # Central should get pretty close even for the bad stepsize
        comp.fd_options['form'] = 'central'
        J = prob.calc_gradient(['p1.x'], ['comp.f_xy'], return_format='dict')
        assert_rel_error(self, J['comp.f_xy']['p1.x'][0][0], 39.0, 1e-1)
    def test_fd_options_step_type(self):
        class ScaledParaboloid(Component):
            """ Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """
            def __init__(self):
                super(ScaledParaboloid, self).__init__()

                # Params
                self.add_param('x', 1.0)
                self.add_param('y', 1.0)

                # Unknowns
                self.add_output('f_xy', 0.0)

                self.scale = 1.0e-6

            def solve_nonlinear(self, params, unknowns, resids):
                """f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
                Optimal solution (minimum): x = 6.6667; y = -7.3333
                """

                x = params['x']
                y = params['y']

                f_xy = ((x - 3.0)**2 + x * y + (y + 4.0)**2 - 3.0)
                unknowns['f_xy'] = self.scale * f_xy

            def jacobian(self, params, unknowns, resids):
                """Analytical derivatives"""

                x = params['x']
                y = params['y']
                J = {}

                J['f_xy', 'x'] = (2.0 * x - 6.0 + y) * self.scale
                J['f_xy', 'y'] = (2.0 * y + 8.0 + x) * self.scale

                return J

        prob = Problem()
        prob.root = Group()
        comp = prob.root.add('comp', ScaledParaboloid())
        prob.root.add('p1', ParamComp('x', 8.0 * comp.scale))
        prob.root.add('p2', ParamComp('y', 8.0 * comp.scale))
        prob.root.connect('p1.x', 'comp.x')
        prob.root.connect('p2.y', 'comp.y')

        comp.fd_options['force_fd'] = True
        comp.fd_options['step_type'] = 'absolute'

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

        J1 = prob.calc_gradient(['p1.x'], ['comp.f_xy'], return_format='dict')

        comp.fd_options['step_type'] = 'relative'
        J2 = prob.calc_gradient(['p1.x'], ['comp.f_xy'], return_format='dict')

        # Couldnt put together a case where one is much worse, so just make sure they
        # are not equal.
        self.assertNotEqual(self, J1['comp.f_xy']['p1.x'][0][0],
                            J2['comp.f_xy']['p1.x'][0][0])
Example #45
0
    def test_subarray_to_promoted_var(self):
        root = Group()

        P = root.add('P', ParamComp('x', np.array([1., 2., 3.])))
        G = root.add('G', Group())
        C = root.add('C', SimpleComp())

        A = G.add('A', SimpleArrayComp())  # , promotes=['x', 'y'])

        root.connect('P.x', 'G.A.x', src_indices=[0, 1])
        root.connect('P.x', 'C.x', src_indices=[
            2,
        ])

        prob = Problem(root)
        prob.setup(check=False)
        prob.run()

        assert_rel_error(self, root.G.A.params['x'], np.array([1., 2.]),
                         0.0001)
        self.assertAlmostEqual(root.C.params['x'], 3.)

        # no try the same thing with promoted var
        root = Group()

        P = root.add('P', ParamComp('x', np.array([1., 2., 3.])))
        G = root.add('G', Group())
        C = root.add('C', SimpleComp())

        A = G.add('A', SimpleArrayComp(), promotes=['x', 'y'])

        root.connect('P.x', 'G.x', src_indices=[0, 1])
        root.connect('P.x', 'C.x', src_indices=[
            2,
        ])

        prob = Problem(root)
        prob.setup(check=False)
        prob.run()

        assert_rel_error(self, root.G.A.params['x'], np.array([1., 2.]),
                         0.0001)
        self.assertAlmostEqual(root.C.params['x'], 3.)
Example #46
0
    def test_converge_diverge(self):

        prob = Problem()
        prob.root = ConvergeDiverge()
        prob.root.ln_solver = ExplicitSolver()
        prob.setup(check=False)
        prob.run()

        param_list = ['p.x']
        unknown_list = ['comp7.y1']

        prob.run()

        # Make sure value is fine.
        assert_rel_error(self, prob['comp7.y1'], -102.7, 1e-6)

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='fwd',
                               return_format='dict')
        assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='rev',
                               return_format='dict')
        assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)

        J = prob.calc_gradient(param_list,
                               unknown_list,
                               mode='fd',
                               return_format='dict')
        assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)
    def test_fd_options_meta_form(self):
        class MetaParaboloid(Component):
            """ Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """
            def __init__(self):
                super(MetaParaboloid, self).__init__()

                # Params
                self.add_param('x1', 1.0, fd_form='forward')
                self.add_param('x2', 1.0, fd_form='backward')
                self.add_param('y', 1.0)

                # Unknowns
                self.add_output('f_xy', 0.0)

            def solve_nonlinear(self, params, unknowns, resids):
                """f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
                Optimal solution (minimum): x = 6.6667; y = -7.3333
                """

                x1 = params['x1']
                x2 = params['x2']
                y = params['y']

                f_xy = ((x1 - 3.0)**2 + (x2 - 3.0)**2 + (x2 + x2) * y +
                        (y + 4.0)**2 - 3.0)
                unknowns['f_xy'] = f_xy

            def jacobian(self, params, unknowns, resids):
                """Analytical derivatives"""

                x1 = params['x1']
                x2 = params['x2']
                y = params['y']
                J = {}

                J['f_xy', 'x1'] = (2.0 * x1 - 6.0 + x2 * y)
                J['f_xy', 'x2'] = (2.0 * x2 - 6.0 + x1 * y)
                J['f_xy', 'y'] = (2.0 * y + 8.0 + x1 + x2)

                return J

        prob = Problem()
        prob.root = Group()
        comp = prob.root.add('comp', MetaParaboloid())
        prob.root.add('p11', ParamComp('x1', 15.0))
        prob.root.add('p12', ParamComp('x2', 15.0))
        prob.root.add('p2', ParamComp('y', 15.0))
        prob.root.connect('p11.x1', 'comp.x1')
        prob.root.connect('p12.x2', 'comp.x2')
        prob.root.connect('p2.y', 'comp.y')

        comp.fd_options['force_fd'] = True
        comp.fd_options['step_size'] = 1e3

        params_list = ['p11.x1']
        unknowns_list = ['comp.f_xy']

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

        J = prob.calc_gradient(params_list,
                               unknowns_list,
                               return_format='dict')
        self.assertGreater(J['comp.f_xy']['p11.x1'][0][0], 0.0)

        J = prob.calc_gradient(['p12.x2'], unknowns_list, return_format='dict')
        self.assertLess(J['comp.f_xy']['p12.x2'][0][0], 0.0)
Example #48
0
from openmdao.core import Problem, Group

from seamloads.SEAMLoads import SEAMLoads
from seamtower.SEAMTower import SEAMTower
from seamrotor.seamrotor import SEAMBladeStructure
from seamaero.seam_aep import SEAM_PowerCurve

if __name__ == '__main__':

    prob = Problem(root=Group())

    prob.root.add('loads', SEAMLoads(26), promotes=['*'])
    prob.root.add('tower', SEAMTower(21), promotes=['*'])
    prob.root.add('blade', SEAMBladeStructure(), promotes=['*'])
    prob.root.add('power_curve', SEAM_PowerCurve(26), promotes=['*'])

    prob.setup()

    # global variables
    prob['tsr'] = 8.0
    prob['rated_power'] = 3.
    prob['max_tipspeed'] = 62.
    prob['min_wsp'] = 0.
    prob['max_wsp'] = 25.
    prob['project_lifetime'] = 20.

    prob['rotor_diameter'] = 101.0
    prob['hub_height'] = 100.0
    prob['tower_bottom_diameter'] = 4.
    prob['tower_top_diameter'] = 2.
Example #49
0
class TestExternalCode(unittest.TestCase):
    def setUp(self):
        self.startdir = os.getcwd()
        self.tempdir = tempfile.mkdtemp(prefix='test_extcode-')
        os.chdir(self.tempdir)
        shutil.copy(os.path.join(DIRECTORY, 'external_code_for_testing.py'),
                    os.path.join(self.tempdir, 'external_code_for_testing.py'))

        self.extcode = ExternalCodeForTesting()
        self.top = Problem()
        self.top.root = Group()

        self.top.root.add('extcode', self.extcode)

    def tearDown(self):
        os.chdir(self.startdir)
        if not os.environ.get('OPENMDAO_KEEPDIRS', False):
            try:
                shutil.rmtree(self.tempdir)
            except OSError:
                pass

    def test_normal(self):
        self.extcode.options['command'] = [
            'python', 'external_code_for_testing.py',
            'external_code_output.txt'
        ]

        self.extcode.options['external_input_files'] = [
            'external_code_for_testing.py',
        ]
        self.extcode.options['external_output_files'] = [
            'external_code_output.txt',
        ]

        dev_null = open(os.devnull, 'w')
        self.top.setup(check=True, out_stream=dev_null)
        self.top.run()

    # def test_ls_command(self):
    #     output_filename = 'ls_output.txt'
    #     if sys.platform == 'win32':
    #         self.extcode.options['command'] = ['dir', ]
    #     else:
    #         self.extcode.options['command'] = ['ls', ]

    #     self.extcode.stdout = output_filename

    #     self.extcode.options['external_output_files'] = [output_filename,]

    #     self.top.setup()
    #     self.top.run()

    #     # check the contents of the output file for 'external_code_for_testing.py'
    #     with open(os.path.join(self.tempdir, output_filename), 'r') as out:
    #         file_contents = out.read()
    #     self.assertTrue('external_code_for_testing.py' in file_contents)

    def test_timeout(self):

        self.extcode.options['command'] = [
            'python', 'external_code_for_testing.py',
            'external_code_output.txt', '--delay', '5'
        ]
        self.extcode.options['timeout'] = 1.0

        self.extcode.options['external_input_files'] = [
            'external_code_for_testing.py',
        ]

        dev_null = open(os.devnull, 'w')
        self.top.setup(check=True, out_stream=dev_null)
        try:
            self.top.run()
        except RuntimeError as exc:
            self.assertEqual(str(exc), 'Timed out')
            self.assertEqual(self.extcode.timed_out, True)
        else:
            self.fail('Expected RunInterrupted')

    def test_badcmd(self):

        # Set command to nonexistant path.
        self.extcode.options['command'] = [
            'no-such-command',
        ]

        self.top.setup(check=False)
        try:
            self.top.run()
        except ValueError as exc:
            msg = "The command to be executed, 'no-such-command', cannot be found"
            self.assertEqual(str(exc), msg)
            self.assertEqual(self.extcode.return_code, -999999)
        else:
            self.fail('Expected ValueError')

    def test_nullcmd(self):

        self.extcode.stdout = 'nullcmd.out'
        self.extcode.stderr = STDOUT

        self.top.setup(check=False)
        try:
            self.top.run()
        except ValueError as exc:
            self.assertEqual(str(exc), 'Empty command list')
        else:
            self.fail('Expected ValueError')
        finally:
            if os.path.exists(self.extcode.stdout):
                os.remove(self.extcode.stdout)

    def test_env_vars(self):

        self.extcode.options['env_vars'] = {
            'TEST_ENV_VAR': 'SOME_ENV_VAR_VALUE'
        }
        self.extcode.options['command'] = [
            'python', 'external_code_for_testing.py',
            'external_code_output.txt', '--write_test_env_var'
        ]

        dev_null = open(os.devnull, 'w')
        self.top.setup(check=True, out_stream=dev_null)
        self.top.run()

        # Check to see if output file contains the env var value
        with open(os.path.join(self.tempdir, 'external_code_output.txt'),
                  'r') as out:
            file_contents = out.read()
        self.assertTrue('SOME_ENV_VAR_VALUE' in file_contents)

    def test_check_external_outputs(self):

        # In the external_files list give it a file that will not be created
        # If check_external_outputs is True, there will be an exception, but since we set it
        #   to False, no exception should be thrown
        self.extcode.options['check_external_outputs'] = False
        self.extcode.options['external_input_files'] = [
            'external_code_for_testing.py',
        ]
        self.extcode.options['external_output_files'] = [
            'does_not_exist.txt',
        ]
        self.extcode.options['command'] = [
            'python', 'external_code_for_testing.py',
            'external_code_output.txt'
        ]

        self.top.setup(check=False)
        self.top.run()
Example #50
0
    def test_basics(self):
        # create a metamodel component
        mm = MetaModel()

        mm.add_param('x1', 0.)
        mm.add_param('x2', 0.)

        mm.add_output('y1', 0.)
        mm.add_output('y2', 0., surrogate=FloatKrigingSurrogate())

        mm.default_surrogate = ResponseSurface()

        # add metamodel to a problem
        prob = Problem(root=Group())
        prob.root.add('mm', mm)
        prob.setup(check=False)

        # check that surrogates were properly assigned
        surrogate = prob.root.unknowns.metadata('mm.y1').get('surrogate')
        self.assertTrue(isinstance(surrogate, ResponseSurface))

        surrogate = prob.root.unknowns.metadata('mm.y2').get('surrogate')
        self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate))

        # populate training data
        prob['mm.train:x1'] = [1.0, 2.0, 3.0]
        prob['mm.train:x2'] = [1.0, 3.0, 4.0]
        prob['mm.train:y1'] = [3.0, 2.0, 1.0]
        prob['mm.train:y2'] = [1.0, 4.0, 7.0]

        # run problem for provided data point and check prediction
        prob['mm.x1'] = 2.0
        prob['mm.x2'] = 3.0

        self.assertTrue(mm.train)  # training will occur before 1st run
        prob.run()

        assert_rel_error(self, prob['mm.y1'], 2.0, .00001)
        assert_rel_error(self, prob['mm.y2'], 4.0, .00001)

        # run problem for interpolated data point and check prediction
        prob['mm.x1'] = 2.5
        prob['mm.x2'] = 3.5

        self.assertFalse(mm.train)  # training will not occur before 2nd run
        prob.run()

        assert_rel_error(self, prob['mm.y1'], 1.5934, .001)

        # change default surrogate, re-setup and check that metamodel re-trains
        mm.default_surrogate = FloatKrigingSurrogate()
        prob.setup(check=False)

        surrogate = prob.root.unknowns.metadata('mm.y1').get('surrogate')
        self.assertTrue(isinstance(surrogate, FloatKrigingSurrogate))

        self.assertTrue(mm.train)  # training will occur after re-setup
        mm.warm_restart = True  # use existing training data

        prob['mm.x1'] = 2.5
        prob['mm.x2'] = 3.5

        prob.run()

        assert_rel_error(self, prob['mm.y1'], 1.4609, .001)
Example #51
0
def example():

    config = {'blade': 'seam', 'tower': 'seam'}

    turbine = FUSEDTurbineCostsModel(config)
    prob = Problem(turbine)
    prob.setup()

    prob['rotor_diameter'] = 126.0
    prob['blade_number'] = 3
    prob['machine_rating'] = 5000.0
    prob['hub_height'] = 90.0
    prob['bearing_number'] = 2
    prob['crane'] = True
    prob['offshore'] = False

    # Rotor force calculations for nacelle inputs
    maxTipSpd = 80.0
    maxEfficiency = 0.90

    ratedHubPower = prob['machine_rating'] * 1000. / maxEfficiency
    rotorSpeed = (maxTipSpd / (0.5 * prob['rotor_diameter'])) * (60.0 /
                                                                 (2 * np.pi))
    prob['rotor_torque'] = ratedHubPower / (rotorSpeed * (np.pi / 30))

    # other inputs
    prob['machine_rating'] = 5000.0
    prob['blade_number'] = 3
    prob['crane'] = True
    prob['offshore'] = True
    prob['bearing_number'] = 2

    if config['blade'] == 'csm':
        prob['turbine_class'] = 1
        prob['blade_has_carbon'] = False
    else:
        prob['tsr'] = 8.0
        prob['rated_power'] = 5.
        prob['max_tipspeed'] = 62.
        prob['min_wsp'] = 0.
        prob['max_wsp'] = 25.
        prob['project_lifetime'] = 20.

    if config['blade'] == 'seam' or config['tower'] == 'seam':
        # loads inputs
        prob['Iref'] = 0.16
        prob['F'] = 0.777
        prob['wohler_exponent_blade_flap'] = 10.0
        prob['wohler_exponent_tower'] = 4.
        prob['nSigma4fatFlap'] = 1.2
        prob['nSigma4fatTower'] = 0.8
        prob['dLoad_dU_factor_flap'] = 0.9
        prob['dLoad_dU_factor_tower'] = 0.8
        prob['lifetime_cycles'] = 1.0e07
        prob['EdgeExtDynFact'] = 2.5
        prob['EdgeFatDynFact'] = 0.75
        prob['WeibullInput'] = True
        prob['WeiA_input'] = 11.
        prob['WeiC_input'] = 2.00
        prob['Nsections'] = 21
        prob['lifetime_cycles'] = 1e7
        prob['wohler_exponent_blade_flap'] = 10.0
        prob['PMtarget'] = 1.0

    if config['blade'] == 'seam':
        prob['MaxChordrR'] = 0.2
        prob['TIF_FLext'] = 1.
        prob['TIF_EDext'] = 1.
        prob['TIF_FLfat'] = 1.
        prob['sc_frac_flap'] = 0.3
        prob['sc_frac_edge'] = 0.8
        prob['SF_blade'] = 1.1
        prob['Slim_ext_blade'] = 200.0
        prob['Slim_fat_blade'] = 27
        prob['AddWeightFactorBlade'] = 1.2
        prob['blade_density'] = 2100.

    if config['tower'] == 'seam':
        prob['tower_bottom_diameter'] = 6.
        prob['tower_top_diameter'] = 3.78
        prob['wohler_exponent_tower'] = 4.
        prob['stress_limit_extreme_tower'] = 235.0
        prob['stress_limit_fatigue_tower'] = 14.885
        prob['safety_factor_tower'] = 1.5

    return prob
Example #52
0
    def test_simple_matvec_subbed_like_multipoint(self):
        group = Group()
        group.add('mycomp', SimpleCompDerivMatVec(), promotes=['x', 'y'])

        prob = Problem(impl=impl)
        prob.root = Group()
        prob.root.add('sub', group, promotes=['*'])
        prob.root.sub.add('x_param', ParamComp('x', 1.0), promotes=['*'])

        prob.root.ln_solver = PetscKSP()
        prob.setup(check=False)
        prob.run()

        J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict')
        assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6)

        J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict')
        assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6)

        J = prob.calc_gradient(['x'], ['y'], mode='fd', return_format='dict')
        assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6)

        J = prob.calc_gradient(['x'], ['y'], mode='fd', return_format='array')
        assert_rel_error(self, J[0][0], 2.0, 1e-6)