本文整理汇总了Python中openmdao.core.group.Group.connect方法的典型用法代码示例。如果您正苦于以下问题:Python Group.connect方法的具体用法?Python Group.connect怎么用?Python Group.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类openmdao.core.group.Group
的用法示例。
在下文中一共展示了Group.connect方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_array2D_index_connection
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [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)
示例2: test_conflicting_connections
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [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")
示例3: test_calc_gradient
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [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,
)
示例4: test_multiple_connect
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [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)
示例5: build_sequence
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [as 别名]
def build_sequence(child_factory, num_children, conns=(), parent=None):
if parent is None:
parent = Group()
cnames = []
for i in range(num_children):
child = child_factory()
cname = _child_name(child, i)
parent.add(cname, child)
if i:
for u,v in conns:
parent.connect('.'.join((cnames[-1],u)),
'.'.join((cname,v)))
cnames.append(cname)
return parent
示例6: test_multiple_connect
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [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)
示例7: test_indices
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [as 别名]
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)
示例8: test_linear_system
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [as 别名]
def test_linear_system(self):
root = Group()
root.add('lin', LinearSystem(3))
x = np.array([1, 2, -3])
A = np.array([[ 5.0, -3.0, 2.0], [1.0, 7.0, -4.0], [1.0, 0.0, 8.0]])
b = A.dot(x)
root.add('p1', ParamComp('A', A))
root.add('p2', ParamComp('b', b))
root.connect('p1.A', 'lin.A')
root.connect('p2.b', 'lin.b')
prob = Problem(root)
prob.setup(check=False)
prob.run()
# Make sure it gets the right answer
assert_rel_error(self, prob['lin.x'], x, .0001)
assert_rel_error(self, np.linalg.norm(prob.root.resids.vec), 0.0, 1e-10)
# Compare against calculated derivs
Ainv = np.linalg.inv(A)
dx_dA = np.outer(Ainv, -x).reshape(3, 9)
dx_db = Ainv
J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='fwd', return_format='dict')
assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001)
assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001)
J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='rev', return_format='dict')
assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001)
assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001)
J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='fd', return_format='dict')
assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001)
assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001)
示例9: test_calc_gradient
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [as 别名]
def test_calc_gradient(self):
root = Group()
parm = root.add('parm', ParamComp('x', np.array([1., 1., 1., 1.])))
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., -3., -17., 9.],
]))
np.testing.assert_almost_equal(J['comp.g']['parm.x'], np.array([
[ 3., 1., 3., 1.],
[ 1., 4., 2., 3.],
[ 6., 1., 2., -1.],
]))
# 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., -3., -17., 9.],
[ 3., 1., 3., 1.],
[ 1., 4., 2., 3.],
[ 6., 1., 2., -1.],
]))
# 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., -3., -17., 9.],
]))
np.testing.assert_almost_equal(J['comp.g']['parm.x'], np.array([
[ 3., 1., 3., 1.],
[ 1., 4., 2., 3.],
[ 6., 1., 2., -1.],
]))
# 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., -3., -17., 9.],
[ 3., 1., 3., 1.],
[ 1., 4., 2., 3.],
[ 6., 1., 2., -1.],
]))
# 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., -3., -17., 9.],
]), decimal=5)
np.testing.assert_almost_equal(J['comp.g']['parm.x'], np.array([
[ 3., 1., 3., 1.],
[ 1., 4., 2., 3.],
[ 6., 1., 2., -1.],
]), 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., -3., -17., 9.],
[ 3., 1., 3., 1.],
[ 1., 4., 2., 3.],
[ 6., 1., 2., -1.],
]), decimal=5)
示例10: Group
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [as 别名]
x = inputs['x']
y = inputs['y']
outputs['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0
if __name__ == "__main__":
from openmdao.core.problem import Problem
from openmdao.core.group import Group
from openmdao.core.indepvarcomp import IndepVarComp
model = Group()
ivc = IndepVarComp()
ivc.add_output('x', 3.0)
ivc.add_output('y', -4.0)
model.add_subsystem('des_vars', ivc)
model.add_subsystem('parab_comp', Paraboloid())
model.connect('des_vars.x', 'parab_comp.x')
model.connect('des_vars.y', 'parab_comp.y')
prob = Problem(model)
prob.setup()
prob.run_model()
print(prob['parab_comp.f_xy'])
prob['des_vars.x'] = 5.0
prob['des_vars.y'] = -2.0
prob.run_model()
print(prob['parab_comp.f_xy'])
示例11: Group
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [as 别名]
g = Group()
p = Problem(impl=impl, root=g)
if 'gmres' in sys.argv:
from openmdao.solvers.scipy_gmres import ScipyGMRES
p.root.ln_solver = ScipyGMRES()
g.add("P", IndepVarComp('x', numpy.ones(vec_size)))
p.driver.add_desvar("P.x")
par = g.add("par", ParallelGroup())
for pt in range(pts):
ptname = "G%d"%pt
ptg = par.add(ptname, Group())
create_dyncomps(ptg, num_comps, 2, 2, 2,
var_factory=lambda: numpy.zeros(vec_size))
g.connect("P.x", "par.%s.C0.p0" % ptname)
cname = ptname + '.' + "C%d"%(num_comps-1)
p.driver.add_objective("par.%s.o0" % cname)
p.driver.add_constraint("par.%s.o1" % cname, lower=0.0)
p.setup()
p.run()
#g.dump(verbose=True)
#p.root.list_connections()
stats(p)
示例12: Problem
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [as 别名]
p = Problem(impl=impl, root=g)
if 'lings' in sys.argv:
from openmdao.solvers.ln_gauss_seidel import LinearGaussSeidel
p.root.ln_solver = LinearGaussSeidel()
g.add("P", IndepVarComp('x', numpy.ones(vec_size)))
p.driver.add_desvar("P.x")
par = g.add("par", ParallelGroup())
for pt in range(pts):
ptg = Group()
par.add(_child_name(ptg, pt), ptg)
build_sequence(lambda: ABCDArrayComp(vec_size),
num_comps, [('c', 'a'),('d','b')], ptg)
g.connect("P.x", "par.%s.%s.a" % (_child_name(ptg, pt),
_child_name(None, 0)))
cname = _child_name(ptg, pt) + '.' + _child_name(None, num_comps-1)
p.driver.add_objective("par.%s.c" % cname)
p.driver.add_constraint("par.%s.d" % cname, lower=0.0)
p.setup()
p.run()
#g.dump(verbose=True)
print("Pts:", pts)
print("Comps per pt:", num_comps)
print("Var size:", vec_size)
stats(p)
示例13: abs
# 需要导入模块: from openmdao.core.group import Group [as 别名]
# 或者: from openmdao.core.group.Group import connect [as 别名]
unknowns['Q_resid'] = abs(unknowns['Qout_tot'] - unknowns['Qin_tot'])
if __name__ == '__main__':
from openmdao.core.group import Group
from openmdao.core.problem import Problem
from openmdao.drivers.scipy_optimizer import ScipyOptimizer
from openmdao.components.param_comp import ParamComp
from openmdao.components.constraint import ConstraintComp
g = Group()
p = Problem(root=g, driver=ScipyOptimizer())
p.driver.options['optimizer'] = 'COBYLA'
g.add('temp_boundary', ParamComp('T', 340.0))
g.add('tube_wall', TubeWallTemp())
g.connect('temp_boundary.T', 'tube_wall.temp_boundary')
g.add('con', ConstraintComp('temp_boundary > 305.7', out='out'))
g.connect('temp_boundary.T', 'con.temp_boundary')
p.driver.add_param('temp_boundary.T', low=0.0, high=10000.0)
p.driver.add_objective('tube_wall.Q_resid')
p.driver.add_constraint('con.out')
p.setup()
g.tube_wall.params['flow_nozzle:in:Tt'] = 1710.0
g.tube_wall.params['flow_nozzle:in:Pt'] = 0.304434211
g.tube_wall.params['flow_nozzle:in:W'] = 1.08
g.tube_wall.params['flow_bearings:in:W'] = 0.0
g.tube_wall.params['r_tube_outer'] = 2.22504 / 2.0
g.tube_wall.params['tube_len'] = 482803.0