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


Python Group.add方法代码示例

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


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

示例1: test_heat_exchanger

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_heat_exchanger(self): 
        comp = HeatExchanger()
        g = Group()
        g.add('comp', comp)
        p = Problem(root=g)
        p.setup()

        comp.params['flow_in:in:Tt'] = 1423.8
        comp.params['flow_in:in:Pt'] = 0.302712118187
        comp.params['flow_in:in:W'] = 1.0
        comp.params['dPqP'] = 0.0
        comp.params['design'] = True
        p.run()
        
        TOL = 0.005
        assert_rel_error(self, comp.unknowns['flow_out:out:Tt'], 539.94, TOL)
        assert_rel_error(self, comp.unknowns['Qreleased'], 327.22, TOL)
        assert_rel_error(self, comp.unknowns['Qabsorbed'], 327.22, TOL)
        assert_rel_error(self, comp.unknowns['Qmax'], 335.1, TOL)
        assert_rel_error(self, comp.unknowns['T_cold_out'], 749.96, TOL)
        
        #check off design 
        comp.params['design'] = False
        
        p.run()

        assert_rel_error(self, comp.unknowns['flow_out:out:Tt'], 539.94, TOL)
        assert_rel_error(self, comp.unknowns['Qreleased'], 327.22, TOL)
        assert_rel_error(self, comp.unknowns['Qabsorbed'], 327.22, TOL)
        assert_rel_error(self, comp.unknowns['Qmax'], 335.1, TOL)
        assert_rel_error(self, comp.unknowns['T_cold_out'], 749.96, TOL)
开发者ID:brentschroeter,项目名称:pyCycle,代码行数:33,代码来源:test_heat_exchanger.py

示例2: test_simple_matvec

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_simple_matvec(self):

        class VerificationComp(SimpleCompDerivMatVec):

            def jacobian(self, params, unknowns, resids):
                raise RuntimeError("Derivative functions on this comp should not run.")

            def apply_linear(self, params, unknowns, dparams, dunknowns,
                             dresids, mode):
                raise RuntimeError("Derivative functions on this comp should not run.")

        sub = Group()
        sub.add('mycomp', VerificationComp())

        prob = Problem()
        prob.root = Group()
        prob.root.add('sub', sub)
        prob.root.add('x_param', IndepVarComp('x', 1.0))
        prob.root.connect('x_param.x', "sub.mycomp.x")

        sub.fd_options['force_fd'] = True
        prob.setup(check=False)
        prob.run()

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

        J = prob.calc_gradient(['x_param.x'], ['sub.mycomp.y'], mode='rev',
                              return_format='dict')
        assert_rel_error(self, J['sub.mycomp.y']['x_param.x'][0][0], 2.0, 1e-6)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:33,代码来源:test_group_derivatives.py

示例3: test_array2D_index_connection

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_array2D_index_connection(self):
        group = Group()
        group.add('x_param', IndepVarComp('x', np.ones((2, 2))), promotes=['*'])
        sub = group.add('sub', Group(), promotes=['*'])
        sub.add('mycomp', ArrayComp2D(), promotes=['x', 'y'])
        group.add('obj', ExecComp('b = a'))
        group.connect('y', 'obj.a',  src_indices=[3])

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

        J = prob.calc_gradient(['x'], ['obj.b'], mode='fwd', return_format='dict')
        Jbase = prob.root.sub.mycomp._jacobian_cache
        assert_rel_error(self, Jbase[('y', 'x')][3][0], J['obj.b']['x'][0][0], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][1], J['obj.b']['x'][0][1], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][2], J['obj.b']['x'][0][2], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][3], J['obj.b']['x'][0][3], 1e-8)

        J = prob.calc_gradient(['x'], ['obj.b'], mode='rev', return_format='dict')
        Jbase = prob.root.sub.mycomp._jacobian_cache
        assert_rel_error(self, Jbase[('y', 'x')][3][0], J['obj.b']['x'][0][0], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][1], J['obj.b']['x'][0][1], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][2], J['obj.b']['x'][0][2], 1e-8)
        assert_rel_error(self, Jbase[('y', 'x')][3][3], J['obj.b']['x'][0][3], 1e-8)
开发者ID:briantomko,项目名称:OpenMDAO,代码行数:29,代码来源:test_ln_gauss_seidel.py

示例4: test_conflicting_promotions

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_conflicting_promotions(self):
        # verify we get an error if we have conflicting promotions
        root = Group()

        # promoting G1.x will create an implicit connection to G3.x
        # this is a conflict because G3.x (aka G3.C4.x) is already connected
        # to G3.C3.x
        G2 = root.add('G2', Group())
        G2.add('C1', ParamComp('x', 5.), promotes=['x'])

        G1 = G2.add('G1', Group(), promotes=['x'])
        G1.add('C2', ExecComp('y=x*2.0'), promotes=['x'])

        G3 = root.add('G3', Group(), promotes=['x'])
        G3.add('C3', ExecComp('y=x*2.0'), promotes=['y'])          # promoting y
        G3.add('C4', ExecComp('y=x*2.0'), promotes=['x', 'y'])     # promoting y again.. BAD

        prob = Problem(root)

        try:
            prob.setup(check=False)
        except Exception as error:
            msg = "Promoted name 'G3.y' matches multiple unknowns: ['G3.C3.y', 'G3.C4.y']"
            self.assertEqual(text_type(error), msg)
        else:
            self.fail("Error expected")
开发者ID:jcchin,项目名称:project_clippy,代码行数:28,代码来源:test_problem.py

示例5: test_double_arraycomp

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_double_arraycomp(self):
        # Mainly testing a bug in the array return for multiple arrays

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

        prob = Problem()
        prob.root = group
        prob.root.ln_solver = ScipyGMRES()
        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)
开发者ID:hschilling,项目名称:OpenMDAO,代码行数:27,代码来源:test_scipy_gmres.py

示例6: test_simple_matvec

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_simple_matvec(self):
        class VerificationComp(SimpleCompDerivMatVec):
            def jacobian(self, params, unknowns, resids):
                raise RuntimeError("Derivative functions on this comp should not run.")

            def apply_linear(self, params, unknowns, dparams, dunknowns, dresids, mode):
                raise RuntimeError("Derivative functions on this comp should not run.")

        sub = Group()
        sub.add("mycomp", VerificationComp())

        prob = Problem()
        prob.root = Group()
        prob.root.add("sub", sub)
        prob.root.add("x_param", ParamComp("x", 1.0))
        prob.root.connect("x_param.x", "sub.mycomp.x")

        sub.fd_options["force_fd"] = True
        prob.setup(check=False)
        prob.run()

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

        J = prob.calc_gradient(["x_param.x"], ["sub.mycomp.y"], mode="rev", return_format="dict")
        assert_rel_error(self, J["sub.mycomp.y"]["x_param.x"][0][0], 2.0, 1e-6)
开发者ID:hschilling,项目名称:OpenMDAO,代码行数:28,代码来源:test_group_derivatives.py

示例7: test_conflicting_connections

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_conflicting_connections(self):
        # verify we get an error if we have conflicting implicit and explicit connections
        root = Group()

        # promoting G1.x will create an implicit connection to G3.x
        # this is a conflict because G3.x (aka G3.C4.x) is already connected
        # to G3.C3.x
        G2 = root.add('G2', Group(), promotes=['x'])  # BAD PROMOTE
        G2.add('C1', ParamComp('x', 5.), promotes=['x'])

        G1 = G2.add('G1', Group(), promotes=['x'])
        G1.add('C2', ExecComp('y=x*2.0'), promotes=['x'])

        G3 = root.add('G3', Group(), promotes=['x'])
        G3.add('C3', ExecComp('y=x*2.0'))
        G3.add('C4', ExecComp('y=x*2.0'), promotes=['x'])

        root.connect('G2.G1.C2.y', 'G3.C3.x')
        G3.connect('C3.y', 'x')

        prob = Problem(root)

        try:
            prob.setup(check=False)
        except Exception as error:
            msg = "Target 'G3.C4.x' is connected to multiple unknowns: ['G2.C1.x', 'G3.C3.y']"
            self.assertEqual(text_type(error), msg)
        else:
            self.fail("Error expected")
开发者ID:jcchin,项目名称:project_clippy,代码行数:31,代码来源:test_problem.py

示例8: CycleComponentTestCase

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
class CycleComponentTestCase(unittest.TestCase):
    def setUp(self): 
        '''Initialization function called before every test function''' 
        self.g = Group()
        self.comp1 = DummyComp()
        self.comp2 = DummyComp()
        self.g.add('comp1', self.comp1)
        self.g.add('comp2', self.comp2)
        self.p = Problem(root=self.g)
        self.p.setup()

    def tearDown(self):
        '''Function called after every test function'''
        self.g = None
        self.p = None

    def test_copy_flow(self): 
        self.comp1.params['flow:in:W'] = 100.0
        self.comp1.params['flow:in:Tt'] = 518.0
        self.comp1.params['flow:in:Pt'] = 15.0
        CycleComponent.copy_from(self.comp1, 'flow', self.comp2, 'flow')

        self.p.run()

        TOL = 0.0001
        assert_rel_error(self, self.comp2.unknowns['flow:out:Tt'], 518.0, TOL)
        assert_rel_error(self, self.comp2.unknowns['flow:out:Pt'], 15.0, TOL)
开发者ID:brentschroeter,项目名称:pyCycle,代码行数:29,代码来源:test_flowstation.py

示例9: test_calc_gradient

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_calc_gradient(self):
        root = Group()
        parm = root.add("parm", ParamComp("x", np.array([1.0, 1.0, 1.0, 1.0])))
        comp = root.add("comp", RosenSuzuki())

        root.connect("parm.x", "comp.x")

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

        param_list = ["parm.x"]
        unknown_list = ["comp.f", "comp.g"]

        # check that calc_gradient returns proper dict value when mode is 'fwd'
        J = prob.calc_gradient(param_list, unknown_list, mode="fwd", return_format="dict")
        np.testing.assert_almost_equal(J["comp.f"]["parm.x"], np.array([[-3.0, -3.0, -17.0, 9.0]]))
        np.testing.assert_almost_equal(
            J["comp.g"]["parm.x"], np.array([[3.0, 1.0, 3.0, 1.0], [1.0, 4.0, 2.0, 3.0], [6.0, 1.0, 2.0, -1.0]])
        )

        # check that calc_gradient returns proper array value when mode is 'fwd'
        J = prob.calc_gradient(param_list, unknown_list, mode="fwd", return_format="array")
        np.testing.assert_almost_equal(
            J, np.array([[-3.0, -3.0, -17.0, 9.0], [3.0, 1.0, 3.0, 1.0], [1.0, 4.0, 2.0, 3.0], [6.0, 1.0, 2.0, -1.0]])
        )

        # check that calc_gradient returns proper dict value when mode is 'rev'
        J = prob.calc_gradient(param_list, unknown_list, mode="rev", return_format="dict")
        np.testing.assert_almost_equal(J["comp.f"]["parm.x"], np.array([[-3.0, -3.0, -17.0, 9.0]]))
        np.testing.assert_almost_equal(
            J["comp.g"]["parm.x"], np.array([[3.0, 1.0, 3.0, 1.0], [1.0, 4.0, 2.0, 3.0], [6.0, 1.0, 2.0, -1.0]])
        )

        # check that calc_gradient returns proper array value when mode is 'rev'
        J = prob.calc_gradient(param_list, unknown_list, mode="rev", return_format="array")
        np.testing.assert_almost_equal(
            J, np.array([[-3.0, -3.0, -17.0, 9.0], [3.0, 1.0, 3.0, 1.0], [1.0, 4.0, 2.0, 3.0], [6.0, 1.0, 2.0, -1.0]])
        )

        # check that calc_gradient returns proper dict value when mode is 'fd'
        J = prob.calc_gradient(param_list, unknown_list, mode="fd", return_format="dict")
        np.testing.assert_almost_equal(J["comp.f"]["parm.x"], np.array([[-3.0, -3.0, -17.0, 9.0]]), decimal=5)
        np.testing.assert_almost_equal(
            J["comp.g"]["parm.x"],
            np.array([[3.0, 1.0, 3.0, 1.0], [1.0, 4.0, 2.0, 3.0], [6.0, 1.0, 2.0, -1.0]]),
            decimal=5,
        )

        # check that calc_gradient returns proper array value when mode is 'fd'
        J = prob.calc_gradient(param_list, unknown_list, mode="fd", return_format="array")
        np.testing.assert_almost_equal(
            J,
            np.array([[-3.0, -3.0, -17.0, 9.0], [3.0, 1.0, 3.0, 1.0], [1.0, 4.0, 2.0, 3.0], [6.0, 1.0, 2.0, -1.0]]),
            decimal=5,
        )
开发者ID:hschilling,项目名称:OpenMDAO,代码行数:58,代码来源:test_problem.py

示例10: test_variables

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_variables(self):
        group = Group()
        group.add("C1", ExecComp("y=x*2.0"), promotes=["x"])
        group.add("C2", ExecComp("y=x*2.0"), promotes=["y"])

        # paths must be initialized prior to calling _setup_variables
        group._setup_paths("")
        params_dict, unknowns_dict = group._setup_variables()

        self.assertEqual(list(params_dict.keys()), ["C1.x", "C2.x"])
        self.assertEqual(list(unknowns_dict.keys()), ["C1.y", "C2.y"])

        self.assertEqual([m["promoted_name"] for n, m in params_dict.items()], ["x", "C2.x"])
        self.assertEqual([m["promoted_name"] for n, m in unknowns_dict.items()], ["C1.y", "y"])
开发者ID:ramtej,项目名称:OpenMDAO,代码行数:16,代码来源:test_group.py

示例11: test_variables

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_variables(self):
        group = Group()
        group.add('C1', ExecComp('y=x*2.0'), promotes=['x'])
        group.add("C2", ExecComp('y=x*2.0'), promotes=['y'])

        # paths must be initialized prior to calling _setup_variables
        group._setup_paths('')
        params_dict, unknowns_dict = group._setup_variables()

        self.assertEqual(list(params_dict.keys()), ['C1.x', 'C2.x'])
        self.assertEqual(list(unknowns_dict.keys()), ['C1.y', 'C2.y'])

        self.assertEqual([m['promoted_name'] for n,m in params_dict.items()], ['x', 'C2.x'])
        self.assertEqual([m['promoted_name'] for n,m in unknowns_dict.items()], ['C1.y', 'y'])
开发者ID:seanmwu,项目名称:OpenMDAO,代码行数:16,代码来源:test_group.py

示例12: setUp

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def setUp(self):
        self.comp = comp = Nozzle()
        g = Group()
        g.add('comp', comp)
        self.p = p = Problem(root=g)
        p.setup(check=False)
        
        comp.params['flow_in:in:W'] = 100.0
        comp.params['flow_in:in:Tt'] = 700.0
        comp.params['flow_in:in:Pt'] = 50.0
        comp.params['flow_in:in:Mach'] = 0.40
        comp.params['back_Ps'] = 15.0
        comp.params['design'] = True

        p.run()
开发者ID:brentschroeter,项目名称:pyCycle,代码行数:17,代码来源:test_nozzle.py

示例13: test_multiple_connect

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_multiple_connect(self):
        root = Group()
        C1 = root.add("C1", ExecComp("y=x*2.0"))
        C2 = root.add("C2", ExecComp("y=x*2.0"))
        C3 = root.add("C3", ExecComp("y=x*2.0"))

        root.connect("C1.y", ["C2.x", "C3.x"])

        root._setup_paths("")
        params_dict, unknowns_dict = root._setup_variables()

        # verify we get correct connection information
        connections = root._get_explicit_connections()
        expected_connections = {"C2.x": ["C1.y"], "C3.x": ["C1.y"]}
        self.assertEqual(connections, expected_connections)
开发者ID:ramtej,项目名称:OpenMDAO,代码行数:17,代码来源:test_group.py

示例14: test_simple_jac

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_simple_jac(self):
        group = Group()
        group.add('x_param', ParamComp('x', 1.0), promotes=['*'])
        group.add('mycomp', ExecComp(['y=2.0*x']), promotes=['x', 'y'])

        prob = Problem()
        prob.root = group
        prob.root.ln_solver = ScipyGMRES()
        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)
开发者ID:seanmwu,项目名称:OpenMDAO,代码行数:18,代码来源:test_scipy_gmres.py

示例15: test_splitterW

# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import add [as 别名]
    def test_splitterW(self):
        g = Group()
        p = Problem(root=g)
        comp = g.add('comp', SplitterW())
        p.setup(check=False)
        
        comp.params['W1_des'] = 1.08
        comp.params['MNexit1_des'] = 1.0
        comp.params['MNexit2_des'] = 1.0
        comp.params['design'] = True

        comp.params['flow_in:in:W'] = 3.48771299
        comp.params['flow_in:in:Tt'] = 630.74523
        comp.params['flow_in:in:Pt'] = 0.0271945
        comp.params['flow_in:in:Mach'] = 1.0
        p.run()
        self.check(comp)

        comp.params['design'] = False
        comp.params['flow_out_1:in:is_super'] = True
        comp.params['flow_out_2:in:is_super'] = True
        p.run()
        self.check(comp)

        comp.params['flow_in:in:W'] *= 0.95
        comp.params['flow_out_1:in:is_super'] = False
        comp.params['flow_out_2:in:is_super'] = False
        p.run()
        TOL = 0.001
        assert_rel_error(self, comp.unknowns['flow_out_1:out:Mach'], 0.76922, TOL)
        assert_rel_error(self, comp.unknowns['flow_out_2:out:Mach'], 0.76922, TOL)
开发者ID:brentschroeter,项目名称:pyCycle,代码行数:33,代码来源:test_splitter.py


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