def test_shape(self):
        import numpy as np
        from openmdao.api import Group, Problem, IndepVarComp
        from openmdao.components.meta_model_structured import MetaModelStructured

        # create input param training data, of sizes 25, 5, and 10 points resp.
        p1 = np.linspace(0, 100, 25)
        p2 = np.linspace(-10, 10, 5)
        p3 = np.linspace(0, 1, 10)

        # can use meshgrid to create a 3D array of test data
        P1, P2, P3 = np.meshgrid(p1, p2, p3, indexing='ij')
        f = np.sqrt(P1) + P2 * P3

        # verify the shape matches the order and size of the input params
        print(f.shape)


        # Create regular grid interpolator instance
        interp = MetaModelStructured(method='cubic')
        interp.add_input('p1', 0.5, p1)
        interp.add_input('p2', 0.0, p2)
        interp.add_input('p3', 3.14, p3)

        interp.add_output('f', 0.0, f)


        # Set up the OpenMDAO model
        model = Group()
        model.add_subsystem('comp', interp, promotes=["*"])
        prob = Problem(model)
        prob.setup()

        # set inputs
        prob['p1'] = 55.12
        prob['p2'] = -2.14
        prob['p3'] = 0.323

        prob.run_model()

        computed = prob['f']
        actual = 6.73306472

        assert_almost_equal(computed, actual)

        # we can verify all gradients by checking against finit-difference
        prob.check_partials(compact_print=True)
示例#2
0
    def test_training_derivatives(self):
        import numpy as np
        from openmdao.api import Group, Problem, IndepVarComp
        from openmdao.components.meta_model_structured import MetaModelStructured

        # create input param training data, of sizes 25, 5, and 10 points resp.
        p1 = np.linspace(0, 100, 25)
        p2 = np.linspace(-10, 10, 5)
        p3 = np.linspace(0, 1, 10)

        # can use meshgrid to create a 3D array of test data
        P1, P2, P3 = np.meshgrid(p1, p2, p3, indexing='ij')
        f = np.sqrt(P1) + P2 * P3

        # verify the shape matches the order and size of the input params
        print(f.shape)

        # Create regular grid interpolator instance
        interp = MetaModelStructured(method='cubic',
                                     training_data_gradients=True)
        interp.add_input('p1', 0.5, p1)
        interp.add_input('p2', 0.0, p2)
        interp.add_input('p3', 3.14, p3)

        interp.add_output('f', 0.0, f)

        # Set up the OpenMDAO model
        model = Group()
        model.add_subsystem('comp', interp, promotes=["*"])
        prob = Problem(model)
        prob.setup()

        # set inputs
        prob['p1'] = 55.12
        prob['p2'] = -2.14
        prob['p3'] = 0.323

        prob.run_model()

        computed = prob['f']
        actual = 6.73306472

        assert_almost_equal(computed, actual)

        # we can verify all gradients by checking against finit-difference
        prob.check_partials(compact_print=True)
示例#3
0
    def test_meta_model_structured_deprecated(self):
        # run same test as above, only with the deprecated component,
        # to ensure we get the warning and the correct answer.
        # self-contained, to be removed when class name goes away.
        import numpy as np
        from openmdao.api import Group, Problem, IndepVarComp
        from openmdao.components.meta_model_structured_comp import MetaModelStructured  # deprecated
        import warnings

        with warnings.catch_warnings(record=True) as w:
            xor_interp = MetaModelStructured(method='slinear')

        self.assertEqual(len(w), 1)
        self.assertTrue(issubclass(w[0].category, DeprecationWarning))
        self.assertEqual(str(w[0].message), "'MetaModelStructured' has been deprecated. Use "
                                            "'MetaModelStructuredComp' instead.")

        # set up inputs and outputs
        xor_interp.add_input('x', 0.0, training_data=np.array([0.0, 1.0]), units=None)
        xor_interp.add_input('y', 1.0, training_data=np.array([0.0, 1.0]), units=None)

        xor_interp.add_output('xor', 1.0, training_data=np.array([[0.0, 1.0], [1.0, 0.0]]), units=None)

        # Set up the OpenMDAO model
        model = Group()
        ivc = IndepVarComp()
        ivc.add_output('x', 0.0)
        ivc.add_output('y', 1.0)
        model.add_subsystem('ivc', ivc, promotes=["*"])
        model.add_subsystem('comp', xor_interp, promotes=["*"])
        prob = Problem(model)
        prob.setup()

        # Now test out a 'fuzzy' XOR
        prob['x'] = 0.9
        prob['y'] = 0.001242

        prob.run_model()

        computed = prob['xor']
        actual = 0.8990064

        assert_almost_equal(computed, actual)

        # we can verify all gradients by checking against finite-difference
        prob.check_partials(compact_print=True)
示例#4
0
    def test_xor(self):
        import numpy as np
        from openmdao.api import Group, Problem, IndepVarComp
        from openmdao.components.meta_model_structured import MetaModelStructured

        # Create regular grid interpolator instance
        xor_interp = MetaModelStructured(method='slinear')

        # set up inputs and outputs
        xor_interp.add_input('x', 0.0, np.array([0.0, 1.0]), units=None)
        xor_interp.add_input('y', 1.0, np.array([0.0, 1.0]), units=None)

        xor_interp.add_output('xor',
                              1.0,
                              np.array([[0.0, 1.0], [1.0, 0.0]]),
                              units=None)

        # Set up the OpenMDAO model
        model = Group()
        ivc = IndepVarComp()
        ivc.add_output('x', 0.0)
        ivc.add_output('y', 1.0)
        model.add_subsystem('ivc', ivc, promotes=["*"])
        model.add_subsystem('comp', xor_interp, promotes=["*"])
        prob = Problem(model)
        prob.setup()

        # Now test out a 'fuzzy' XOR
        prob['x'] = 0.9
        prob['y'] = 0.001242

        prob.run_model()

        computed = prob['xor']
        actual = 0.8990064

        assert_almost_equal(computed, actual)

        # we can verify all gradients by checking against finit-difference
        prob.check_partials(compact_print=True)
    def test_xor(self):
        import numpy as np
        from openmdao.api import Group, Problem, IndepVarComp
        from openmdao.components.meta_model_structured import MetaModelStructured

        # Create regular grid interpolator instance
        xor_interp = MetaModelStructured(method='slinear')

        # set up inputs and outputs
        xor_interp.add_input('x', 0.0, np.array([0.0, 1.0]), units=None)
        xor_interp.add_input('y', 1.0, np.array([0.0, 1.0]), units=None)

        xor_interp.add_output('xor', 1.0, np.array([[0.0, 1.0], [1.0, 0.0]]), units=None)

        # Set up the OpenMDAO model
        model = Group()
        ivc = IndepVarComp()
        ivc.add_output('x', 0.0)
        ivc.add_output('y', 1.0)
        model.add_subsystem('ivc', ivc, promotes=["*"])
        model.add_subsystem('comp', xor_interp, promotes=["*"])
        prob = Problem(model)
        prob.setup()

        # Now test out a 'fuzzy' XOR
        prob['x'] = 0.9
        prob['y'] = 0.001242

        prob.run_model()

        computed = prob['xor']
        actual = 0.8990064

        assert_almost_equal(computed, actual)

        # we can verify all gradients by checking against finit-difference
        prob.check_partials(compact_print=True)
示例#6
0
            self.set_order([
                'flow_in', 'corrinputs', 'map', 'press_rise', 'ideal_flow',
                'enth_rise', 'real_flow', 'eff_poly_calc', 'blds_pwr',
                'FAR_passthru'
            ] + bleed_names + ['out_stat'])

        else:
            self.add_subsystem('W_passthru',
                               PassThrough('W_out',
                                           'Fl_O:stat:W',
                                           1.0,
                                           units="lbm/s"),
                               promotes=['*'])
            self.set_order([
                'flow_in', 'corrinputs', 'map', 'press_rise', 'ideal_flow',
                'enth_rise', 'real_flow', 'eff_poly_calc', 'blds_pwr',
                'FAR_passthru'
            ] + bleed_names + ['W_passthru'])


if __name__ == "__main__":
    from openmdao.core.problem import Problem

    p = Problem()
    p.root = Compressor(design=True)

    p.setup()
    p.run_model()
    p.check_partials()
    # p.run()
示例#7
0
        # J['TSFC', 'Wfuel'] = 3600.0/(outputs['Fg'] - inputs['ram_drag'])
        if self.Wfuel_vals:
            J['TSFC', 'ram_drag'] = 3600.0 * wfuel / fn ** 2
            J['PSFC', 'power'] = -3600. * wfuel / power ** 2



if __name__ == "__main__":
    from openmdao.core.problem import Problem, IndepVarComp

    p = Problem()

    des_vars = p.model.add('des_vars', IndepVarComp(), promotes=['*'])
    des_vars.add_output('power', 200.0, units='hp')
    des_vars.add_output('Pt2', 204.696, units='psi')
    des_vars.add_output('Pt3', 104.696, units='psi')
    des_vars.add_output('Wfuel_0', 2, units='lbm/s')
    des_vars.add_output('ram_drag', 100, units='lbf')
    des_vars.add_output('Fg_0', 1200, units='lbf')
    des_vars.add_output('Fg_1', 2000, units='lbf')

    p.model.add('comp', Performance(num_nozzles=2, num_burners=1), promotes=['*'])
    # p.model.comp.fd_options['form'] = 'complex_step'



    p.setup(check=True)
    p.run_model()

    p.check_partials(compact_print=True)
示例#8
0
                design=True,
                elements=AIR_MIX,
                bleed_names=['bld1']))

    connect_flow(prob.model, 'flow_start.Fl_O', 'turbine.Fl_I')
    connect_flow(prob.model,
                 'bld_start.Fl_O',
                 'turbine.bld1',
                 connect_stat=False)

    prob.model.connect("P", "flow_start.P")
    prob.model.connect("T", "flow_start.T")
    prob.model.connect("W", "flow_start.W")
    prob.model.connect("P_bld", "bld_start.P")
    prob.model.connect("T_bld", "bld_start.T")
    prob.model.connect("W_bld", "bld_start.W")
    prob.model.connect("PR", "turbine.PR")
    prob.model.connect('eff', 'turbine.eff')
    prob.model.connect("frac_P", "turbine.bld1:frac_P")

    prob.setup()
    # prob.model.flow_start.list_connections()
    prob.run_model()

    print(prob['turbine.Fl_O:tot:T'])
    print(prob['turbine.Fl_O:tot:P'])
    print(prob['turbine.Fl_O:stat:W'])
    print(prob['bld_start.Fl_O:stat:W'])

    prob.check_partials(compact_print=True, abs_err_tol=1e-3, rel_err_tol=1e-3)