【问题标题】:OpenMDAO 0.13, connecting a component to an assembly within a bigger assemblyOpenMDAO 0.13,将组件连接到更大组件中的组件
【发布时间】:2017-03-20 18:38:57
【问题描述】:

谁能告诉我如何在一个更大的组件中连接和组装到一个组件。我正在寻求帮助来模拟一个看起来像这样的系统。 提前致谢

【问题讨论】:

    标签: openmdao


    【解决方案1】:

    您需要执行以下操作:

    assembly2.create_passthrough('component5.input')

    然后你就可以了

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

    【讨论】:

    • 如何在另一个程序集中添加一个程序集?
    • 请注意,OpenMDAO v0.13 已不再受支持且未积极开发。您应该将建模切换到 1.7.x
    • 组件的行为就像一个组件(它实际上是一个组件,只是一个更复杂的组件),并且添加方式与添加任何其他组件的方式相同。此外,您应该根据 Justin 的评论查看更新的、受支持的 OpenMDAO 版本。
    • 组件内部是否可以有组件?如果可以,如何将组件的变量连接到组件的输入变量
    • @kilojoules create_passthrough() 只是将输入/输出变量添加到程序集并像您所做的那样连接它们的简写。
    【解决方案2】:

    对于遇到这种情况的人,我们能够实现图中所示的连接。我们不需要使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 2015-01-25
      • 1970-01-01
      • 2019-08-22
      • 2016-06-22
      • 2012-03-17
      • 2021-01-01
      相关资源
      最近更新 更多