本文整理汇总了Python中openmdao.core.group.Group类的典型用法代码示例。如果您正苦于以下问题:Python Group类的具体用法?Python Group怎么用?Python Group使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Group类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_splitterW
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)
示例2: test_conflicting_promotions
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")
示例3: setUp
def setUp(self):
model = Group()
ivc = IndepVarComp()
mapdata = SampleMap()
params = mapdata.param_data
x, y, z = params
outs = mapdata.output_data
z = outs[0]
ivc.add_output('x', x['default'], units=x['units'])
ivc.add_output('y', y['default'], units=y['units'])
ivc.add_output('z', z['default'], units=z['units'])
model.add_subsystem('des_vars', ivc, promotes=["*"])
comp = MetaModelStructured(method='slinear', extrapolate=True)
for param in params:
comp.add_input(param['name'], param['default'], param['values'])
for out in outs:
comp.add_output(out['name'], out['default'], out['values'])
model.add_subsystem('comp', comp, promotes=["*"])
self.prob = Problem(model)
self.prob.setup()
self.prob['x'] = 1.0
self.prob['y'] = 0.75
self.prob['z'] = -1.7
示例4: test_simple_matvec
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)
示例5: test_simple_matvec
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)
示例6: test_double_arraycomp
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)
示例7: test_heat_exchanger
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)
示例8: test_conflicting_connections
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")
示例9: CycleComponentTestCase
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)
示例10: test_calc_gradient
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,
)
示例11: test_add
def test_add(self):
group = Group()
comp = ExecComp('y=x*2.0')
group.add('mycomp', comp)
subs = list(group.subsystems())
self.assertEqual(len(subs), 1)
self.assertEqual(subs[0], comp)
self.assertEqual(subs[0].name, 'mycomp')
comp2 = ExecComp('y=x*2.0')
group.add("nextcomp", comp2)
subs = list(group.subsystems())
self.assertEqual(len(subs), 2)
self.assertEqual(subs[0], comp)
self.assertEqual(subs[1], comp2)
with self.assertRaises(RuntimeError) as cm:
group.add('mycomp', comp)
expected_msg = "Group '' already contains a subsystem with name 'mycomp'."
self.assertEqual(str(cm.exception), expected_msg)
示例12: test_array2D_index_connection
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)
示例13: setUp
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()
示例14: test_simple_matvec
def test_simple_matvec(self):
group = Group()
group.add('x_param', IndepVarComp('x', 1.0), promotes=['*'])
group.add('mycomp', SimpleCompDerivMatVec(), promotes=['x', 'y'])
prob = Problem()
prob.root = group
prob.root.ln_solver = LinearGaussSeidel()
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)
示例15: test_simple_jac
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)