【发布时间】:2017-03-20 18:38:57
【问题描述】:
【问题讨论】:
标签: openmdao
【问题讨论】:
标签: openmdao
您需要执行以下操作:
assembly2.create_passthrough('component5.input')
然后你就可以了
connect('component1.output', 'assembly2.input')
【讨论】:
对于遇到这种情况的人,我们能够实现图中所示的连接。我们不需要使用create_passthrough。
from openmdao.main.api import Component, Assembly
from openmdao.main.datatypes.api import Float, Bool, Int, Str, Array, Enum
from openmdao.main.api import Assembly
from openmdao.examples.simple.paraboloid import Paraboloid
class New1(Assembly):
x=Float(iotype='in')
f_xy=Float(iotype='out')
out=Float(iotype='out')
def configure(self):
self.add('C5',Paraboloid())
self.add('C6',Paraboloid())
self.connect('x','C5.x')
self.driver.workflow.add(['C5','C6'])
self.connect("C5.f_xy","C6.x")
self.connect('C6.f_xy', 'f_xy')
class New(Assembly):
def configure(self):
self.add('C1',Paraboloid())
self.add('C2',Paraboloid())
self.add('C4',Paraboloid())
self.add('A2',New1())
self.connect("C1.f_xy","A2.x")
self.connect("A2.f_xy", "C4.x")
self.driver.workflow.add(['C1','C2','C4','A2'])
self.connect("C1.f_xy","C2.x")
self.connect("C2.f_xy","C4.y")
if __name__ == "__main__":
a = New()
a.C1.x = 2.3
a.C1.y = 7.2
a.C2.y = 9.8
a.C4.x = 1.5
a.run()
print "C1 has output of %0.2f and C5 has input of %0.2f" % (a.C1.f_xy,a.A2.C5.x)
print "C4 has input of %0.2f and C6 has output of %0.2f" % (a.C4.x,a.A2.C6.f_xy)
输出:
C1 has output of 139.49 and C5 has input of 139.49
C4 has input of 347431722.56 and C6 has output of 347431722.56
编辑
这是带有外部优化的相同程序集
from openmdao.main.api import Component, Assembly
from openmdao.main.datatypes.api import Float, Bool, Int, Str, Array, Enum
from openmdao.lib.drivers.api import SLSQPdriver
from openmdao.main.api import Assembly
from openmdao.examples.simple.paraboloid import Paraboloid
class New1(Assembly):
x=Float(iotype='in')
f_xy=Float(iotype='out')
def configure(self):
self.add('C5',Paraboloid())
self.add('C6',Paraboloid())
self.connect('x','C5.x')
self.driver.workflow.add(['C5','C6'])
self.connect("C5.f_xy","C6.x")
self.connect('C6.f_xy', 'f_xy')
class New(Assembly):
x=Float(3., iotype='in')
f_xy=Float(iotype='out')
def configure(self):
self.add('C1',Paraboloid())
self.add('C2',Paraboloid())
self.add('C4',Paraboloid())
self.add('A2',New1())
self.connect("A2.f_xy", "C4.x")
self.driver.workflow.add(['C1','C2','C4','A2'])
self.connect('x', 'A2.x')
self.connect('A2.f_xy', 'f_xy')
self.connect("C1.f_xy","C2.x")
self.connect("C2.f_xy","C4.y")
if __name__ == "__main__":
class optim(Assembly):
def configure(self):
self.add('driver', SLSQPdriver())
self.add('modl', New())
self.driver.workflow.add('modl')
self.driver.add_parameter('modl.x', low=-2., high=2.)
self.driver.add_objective('modl.f_xy')
a = optim()
print "C1 has output of %0.2f and C5 has input of %0.2f" % (a.modl.C1.f_xy,a.modl.A2.C5.x)
print "C4 has input of %0.2f and C6 has output of %0.2f" % (a.modl.C4.x,a.modl.A2.C6.f_xy)
print 'initial objective: ', a.modl.f_xy
a.run()
print 'optimized objective: ', a.modl.f_xy
print "New has output of ", a.modl.f_xy
输出:
C1 has output of 0.00 and C5 has input of 0.00
C4 has input of 0.00 and C6 has output of 0.00
initial objective: 0.0
optimized objective: 113.0
New has output of 113.0
【讨论】: