我正在玩OpenMDAO中的Paraboloid教程。我尝试了一些简单的方法,基本上将输入值更改为Paraboloid组件。请参阅以下代码。当我运行它时,它将打印出相同的结果,好像什么也没发生。那么发生了什么?如果我有一个小组,我该如何修改输入?
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'])
答案 0 :(得分: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