将 Demux Comp 与 3 维数组 (OpenMDAO) 结合使用时出现问题

Issue using Demux Comp with 3 dimentional arrray (OpenMDAO)

我正在尝试解复用一个 3 轴数组,如以下示例代码所示

import openmdao.api as om

p = om.Problem()

ivc = p.model.add_subsystem('idv', om.IndepVarComp(), promotes=['*'])
ivc.add_output(name='f0', shape=(2,3), units='m', val=[[111,112,113], [121,122,123]])
ivc.add_output(name='f1', shape=(2,3), units='m', val=[[211,212,213], [221,222,223]])
ivc.add_output(name='f2', shape=(2,3), units='m', val=[[311,312,313], [321,322,323]])
ivc.add_output(name='f3', shape=(2,3), units='m', val=[[411,412,413], [421,422,423]])

mux_comp = p.model.add_subsystem(name='mux', subsys=om.MuxComp(vec_size=4))
mux_comp.add_var('r', shape=(2,3), axis=0, units='m')

demux_comp = p.model.add_subsystem(name='demux', subsys=om.DemuxComp(vec_size=4))
demux_comp.add_var('g', shape=(4,2,3), axis=0, units='m')

p.model.connect('f0', 'mux.r_0')
p.model.connect('f1', 'mux.r_1')
p.model.connect('f2', 'mux.r_2')
p.model.connect('f3', 'mux.r_3')

p.model.connect('mux.r', 'demux.g')

p.setup()
p.run_model()

print(p['mux.r'])
print(p['mux.r'].shape)

print(p['demux.g_0'])
print(p['demux.g_1'])
print(p['demux.g_2'])
print(p['demux.g_3'])
print(p['demux.g_0'].shape)

当这个 运行s 时,我得到以下错误

RuntimeError: DemuxComp (demux): d(g_0)/d(g): declare_partials has been called with rows and cols, which should be arrays of equal length, but rows is length 6 while cols is length 2.

由于错误仅与部分有关,我查看了 OpenMDAO 库中的 demux_comp.py 代码并修改了

中的 declare_partials 行
self.declare_partials(of=out_name, wrt=var, rows=rs, cols=cs, val=1.0)

self.declare_partials(of=out_name, wrt=var, val=1.0)

这允许代码 运行 成功并输出正确的多路分解变量。这会对我的其余代码和优化产生任何不利影响吗?

您似乎发现了一个错误。我们会着手解决这个问题。

虽然您的修复将允许它 运行,但偏音将不正确。同时,您最好将 declare partials 调用替换为使用有限差分或复数步长的说明。

self.declare_partials(of=out_name, wrt=var, method='cs')

对于复步,或method='fd'对于有限差分。