在OpenMDAO中重用具有不同参数名称的外部函数
我试图从外部规范生成一个MDAO问题。这需要自动创建组、规程和变量。我想重用一些分析函数,但参数不同。我必须假设这些参数的名称在重复使用的实例之间可能不同,因此我正在寻找一种方法来制定分析函数,而不必在函数的字典样式输入/输出参数中的键与规程输入和输出变量之间保持必要的一致性 在下面的示例中,是否可以(如果可以,如何)使用以下可重用函数之一在OpenMDAO中重用具有不同参数名称的外部函数,openmdao,Openmdao,我试图从外部规范生成一个MDAO问题。这需要自动创建组、规程和变量。我想重用一些分析函数,但参数不同。我必须假设这些参数的名称在重复使用的实例之间可能不同,因此我正在寻找一种方法来制定分析函数,而不必在函数的字典样式输入/输出参数中的键与规程输入和输出变量之间保持必要的一致性 在下面的示例中,是否可以(如果可以,如何)使用以下可重用函数之一MyReusableFunction/myreusablefunctionlt import openmdao.api as om ### External
MyReusableFunction
/myreusablefunctionlt
import openmdao.api as om
### External information
# I can choose the format of disciplinary functions. Some alternatives:
def MyNonReusableFunction1(inputs, outputs): # <- The way it works
# I have to use keys 'A', 'B', 'C' here
outputs['C'] = inputs['A']*inputs['B']
def MyNonReusableFunction2(inputs, outputs): # <- The way it works
# I have to use keys 'D', 'E', 'F' here
outputs['F'] = inputs['D']*inputs['E']
def MyReusableFunction(x, y): # <- The way I want it to work
return x*y
def MyReusableFunctionAlt(inputs, outputs): # <- This would also be fine
outputs['z'] = inputs['x']*inputs['y']
# Given structure of the problem
disciplines = {
'D1': {
'inputs': ['A', 'B'],
'outputs': ['C'],
'function': MyReusableFunction}, # <- instead of MyNonReusableFunction1
'D2': {
'inputs': ['D', 'E'],
'outputs': ['F'],
'function': MyReusableFunction}, # <- instead of MyNonReusableFunction2
}
connections = [('D2.F', 'D1.B')]
### My script starts here
problem = om.Problem()
for disc_name, disc_data in disciplines.items():
discipine = om.ExplicitComponent()
discipline.compute = disc_data['function']
for param_in in disc_data['inputs']:
discipline.add_input(param_in, 1)
for param_out in disc_data['outputs']:
discipline.add_output(param_out, 1)
problem.add_subsystem(disc_name, discipline)
for connection in connections:
problem.connect(connection[0], connection[1])
将openmdao.api作为om导入
###外部信息
#我可以选择纪律职能的形式。一些备选方案:
def MyNonReusableFunction1(输入、输出):#这感觉像是ExecComps中用户定义函数注册的用例。这是一个全新的功能
其使用示例如下:
这将为您处理导数,使用复步长或有限差分,这取决于给定函数是否复安全
下面是一个代码示例。它并没有完全复制存储用户函数的字典,但是使用此路径可能比重新分配计算更容易获得它们的函数
将openmdao.api作为om导入
def MyReusableFunction(x,y):#