0

我只是在玩 OpenMDAO 中的抛物面教程。我尝试了一些简单的方法,基本上将输入值更改为抛物面组件。请参阅以下代码。但是,当我运行它时,它会打印相同的结果,就好像什么都没发生一样。那么发生了什么?如果我有一个组,我将如何修改输入?

from __future__ import print_function


from openmdao.api import IndepVarComp, Component, Problem, Group

class Paraboloid(Component):
    """ Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """

    def __init__(self):
        super(Paraboloid, self).__init__()

        self.add_param('x', val=0.0)
        self.add_param('y', val=0.0)

        self.add_output('f_xy', val=0.0)

    def solve_nonlinear(self, params, unknowns, resids):
        """f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
        """

        x = params['x']
        y = params['y']

        unknowns['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0

    def linearize(self, params, unknowns, resids):
        """ Jacobian for our paraboloid."""

        x = params['x']
        y = params['y']
        J = {}

        J['f_xy', 'x'] = 2.0*x - 6.0 + y
        J['f_xy', 'y'] = 2.0*y + 8.0 + x
        return J

if __name__ == "__main__":

    top = Problem()

    root = top.root = Group()

    root.add('p1', IndepVarComp('x', 3.0))
    root.add('p2', IndepVarComp('y', -4.0))
    root.add('p', Paraboloid())

    root.connect('p1.x', 'p.x')
    root.connect('p2.y', 'p.y')


    root.p1.x=3.0;
    root.p2.y=-4.0;
    top.setup()
    top.run()
    print(root.p.unknowns['f_xy'])

    root.p1.x=5.0;
    root.p2.y=5.0;
    top.setup()
    top.run()
    print(root.p.unknowns['f_xy'])
4

1 回答 1

0

您必须先调用setup(),然后才能设置任何值。之后,您可以通过字典来设置它们,例如从问题实例访问。对于您的示例代码,它应该如下所示:

top.setup()
top['p1.x'] = 3.
top['p2.y'] = 4.
top.run()
print(top['p.f_xy'])

top['p1.x'] = 10.
top['p2.y'] = 10.
top.run()
print(top['p.f_xy'])

当我使用调整后的脚本运行此模型时,我得到:

##############################################
Setup: Checking for potential issues...

No recorders have been specified, so no data will be saved.

Setup: Check complete.
##############################################

73.0
342.0
于 2016-02-16T13:17:06.200 回答