无法为声明的输入强制使用整数

Enforcing integers for declared inputs is not possible

我尝试使用默认整数声明输入,但似乎不可能。我是在犯错还是在 openmdao 核心中强制执行浮动。

这是我试过的代码片段;

预期输出类似于:array([1, 1, 1])

收到输出:[1。 1.1.]

from openmdao.api import ExplicitComponent,  Problem, IndepVarComp
import numpy as np

class CompAddWithArrayIndices(ExplicitComponent):
    """Component for tests for declaring with array val and array indices."""
    def setup(self):
        self.add_input('x_a', val=np.ones(6,dtype=int))
        self.add_input('x_b', val=[1]*5)
        self.add_output('y')

p = Problem(model=CompAddWithArrayIndices())
p.setup()
p.run_model()

print(p['x_a'])        
print(p['x_b'])   
#%%
from openmdao.api import ExplicitComponent,  Problem, IndepVarComp
import numpy as np

class CompAddWithArrayIndices(ExplicitComponent):
    """Component for tests for declaring with array val and array indices."""
    def setup(self):
        self.add_input('x_a', val=np.zeros(3,dtype=int))
        self.add_output('y')

prob = Problem()
ivc=IndepVarComp()
prob.model.add_subsystem('ivc', ivc,promotes=['*'])
ivc.add_output('x_a', val=np.ones(3,dtype=int))

prob.model.add_subsystem('comp1', CompAddWithArrayIndices(),promotes=['*'])

prob.setup()
prob.run_model()

print(prob['x_a'])

通过 add_inputsadd_outputs 添加的变量将被转换为浮点数或浮点数组。如果你想让一个变量是一个int或任何其他离散类型,你必须使用add_discrete_inputadd_discrete_output。此类变量将根据连接信息在系统之间传递,但不会尝试计算它们的导数。

在 OpenMDAO v2.5 中添加了离散变量支持作为一项实验性功能(仍在开发中)。在 master 分支上有提交 ID 709401e535cf6933215abd942d4b4d49dbf61b2b,升级问题已经出现fixed.Make确保您使用的是该提交或更高版本的 OpenMDAO 的最新版本