我正在运行教程中的“Sellar exmaple”代码。根据教程页面上给出的文档,这ExecComp
只是声明一个 normal 的简写Component
。所以我尝试将示例中的 s 重新定义ExecComp
为普通Component
s 并在同一个示例中使用它们。
示例中的ExecComp
s 定义如下 -
self.add('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0),
promotes=['*'])
self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['*'])
self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['*'])
我定义的正常Component
s如下-
目标组件
class SellarObjective(Component):
def __init__(self):
super(SellarObjective, self).__init__()
self.add_param('x', val=0.0)
self.add_param('y2', val=0.0)
self.add_param('y1', val=0.0)
self.add_param('z', val=np.zeros(2))
self.add_output('obj', val=0.0)
def solve_nonlinear(self, params, unknowns, resids):
unknowns['obj'] = params['x']**2 + params['z'][0] + params['y1'] + exp(-params['y2'])
def linearize(self, params, unknowns, resids):
J = {}
J['obj', 'x'] = 2 * params['x']
J['obj', 'y2'] = (-1) * exp(-params['y2'])
J['obj', 'y1'] = 1.0
J['obj', 'z[0]'] = 1.0
return J
约束 1
class SellarConstraint1(Component):
def __init__(self):
super(SellarConstraint1, self).__init__()
self.add_param('y1', val=0.0)
self.add_output('con1', val=0.0)
def solve_nonlinear(self, params, unknowns, resids):
unknowns['con1'] = 3.16 - params['y1']
def linearize(self, params, unknowns, resids):
J = {}
J['con1', 'y1'] = -1.0
return J
约束 2
class SellarConstraint2(Component):
def __init__(self):
super(SellarConstraint2, self).__init__()
self.add_param('y2', val=0.0)
self.add_output('con2', val=0.0)
def solve_nonlinear(self, params, unknowns, resids):
unknowns['con2'] = params['y2'] - 24.0
def linearize(self, params, unknowns, resids):
J = {}
J['con2', 'y2'] = 1.0
return J
我Component
在重写的实现中将这些新声明的 s 实例化为 -
self.add('obj_cmp', SellarObjective(), promotes=['*'])
self.add('con_cmp1', SellarConstraint1(), promotes=['*'])
self.add('con_cmp2', SellarConstraint2(), promotes=['*'])
代码中的其他所有内容都与教程中的相同。但是在执行它们之后,当我比较结果时 - 结果不匹配。
我在这里遗漏了一些明显的东西吗?感谢您的时间。