OpenMDAO 0.13,将组件连接到更大的组件中的组件

时间:2017-03-20 18:38:57

标签: openmdao

任何人都可以建议如何连接和组装到更大的组件内的组件。我正在寻找帮助来建模一个看起来像这样的系统。 提前致谢

enter image description here

2 个答案:

答案 0 :(得分:1)

你需要做类似的事情:

  

assembly2.create_passthrough(' component5.input&#39)

然后你可以

  

connect(' component1.output',' assembly2.input')

答案 1 :(得分:0)

对于遇到此问题的人,我们能够实现图中显示的连接。我们不需要使用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