当前位置: 首页>>代码示例>>Python>>正文


Python Problem.setup方法代码示例

本文整理汇总了Python中openmdao.core.Problem.setup方法的典型用法代码示例。如果您正苦于以下问题:Python Problem.setup方法的具体用法?Python Problem.setup怎么用?Python Problem.setup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在openmdao.core.Problem的用法示例。


在下文中一共展示了Problem.setup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_derivatives

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:32,代码来源:test_meta_model.py

示例2: test_prom_conns

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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])
开发者ID:jcchin,项目名称:project_clippy,代码行数:36,代码来源:test_prom_conns.py

示例3: test_array_outputs

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:30,代码来源:test_meta_model.py

示例4: test_unequal_training_inputs

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:29,代码来源:test_meta_model.py

示例5: test_parab_FD_subbed_Pcomps

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:giridhar1991,项目名称:OpenMDAO,代码行数:30,代码来源:test_mpi_opt.py

示例6: test_vector_inputs

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:29,代码来源:test_meta_model.py

示例7: test_parab_FD

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:giridhar1991,项目名称:OpenMDAO,代码行数:33,代码来源:test_mpi_opt.py

示例8: test_one_dim_bi_fidelity_training

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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])
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:33,代码来源:test_multifi_meta_model.py

示例9: test_one_dim_one_fidelity_training

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:29,代码来源:test_multifi_meta_model.py

示例10: test_double_arraycomp

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:32,代码来源:test_petsc_ksp.py

示例11: test_array_inputs

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:kishenr12,项目名称:OpenMDAO,代码行数:28,代码来源:test_meta_model.py

示例12: test_converge_diverge_compfd

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:29,代码来源:test_mpi_derivs.py

示例13: test_parab_FD

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:kishenr12,项目名称:OpenMDAO,代码行数:33,代码来源:test_mpi_opt.py

示例14: test_simple_deriv_xfer

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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.)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:27,代码来源:test_mpi_derivs.py

示例15: test_parab_subbed_Pcomps

# 需要导入模块: from openmdao.core import Problem [as 别名]
# 或者: from openmdao.core.Problem import setup [as 别名]
    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)
开发者ID:kishenr12,项目名称:OpenMDAO,代码行数:30,代码来源:test_mpi_opt.py


注:本文中的openmdao.core.Problem.setup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。