【发布时间】:2019-01-28 16:37:11
【问题描述】:
我想使用 Group 的 Newton 非线性求解器为 Sellar 求解 MDA。我已经定义了带有导数的学科(使用'compute_partials'),但我想检查在强制学科不使用其分析导数(使用问题中的'declare_partials'时)调用学科'compute'和'compute_partials'的次数定义 )。问题是,即使我强制不使用它,似乎仍然调用了“compute_partials”函数。 这是一个例子(Sellar)
所以对于纪律 2,我添加了一个计数器,我有
from openmdao.test_suite.components.sellar import SellarDis1, SellarDis2
class SellarDis2withDerivatives(SellarDis2):
"""
Component containing Discipline 2 -- derivatives version.
"""
def _do_declares(self):
# Analytic Derivs
self.declare_partials(of='*', wrt='*')
self.exec_count_d = 0
def compute_partials(self, inputs, J):
"""
Jacobian for Sellar discipline 2.
"""
y1 = inputs['y1']
if y1.real < 0.0:
y1 *= -1
J['y2', 'y1'] = .5*y1**-.5
J['y2', 'z'] = np.array([[1.0, 1.0]])
self.exec_count_d += 1
我创建了一个与 on OpendMDAO docs 类似的 MDA,但调用我创建的 SellarDis2withDerivatives 和 SellarDis1withDerivatives 并像这样更改 Newton_solver() 的非线性求解器
cycle.add_subsystem('d1', SellarDis1withDerivatives(), promotes_inputs=['x', 'z', 'y2'], promotes_outputs=['y1'])
cycle.add_subsystem('d2', SellarDis2withDerivatives(), promotes_inputs=['z', 'y1'], promotes_outputs=['y2'])
# Nonlinear Block Gauss Seidel is a gradient free solver
cycle.nonlinear_solver = NewtonSolver()
cycle.linear_solver = DirectSolver()
然后我运行以下问题
prob2 = Problem()
prob2.model = SellarMDA()
prob2.setup()
prob2.model.cycle.d1.declare_partials('*', '*', method='fd')
prob2.model.cycle.d2.declare_partials('*', '*', method='fd')
prob2['x'] = 2.
prob2['z'] = [-1., -1.]
prob2.run_model()
count = prob2.model.cycle.d2.exec_count_d
print("Number of derivatives calls (%i)"% (count))
结果,我得到了
=====
周期
NL:牛顿在 3 次迭代中收敛 衍生品调用次数 (3)
因此,似乎仍然以某种方式调用了函数“compute_partials”(即使导数是使用 FD 计算的)。有人作为解释吗?
【问题讨论】:
标签: derivative openmdao