外部代码与元数据不兼容?

时间:2018-05-02 18:17:59

标签: openmdao

如果我使用与外部代码组件和显式组件的元数据相同的设置,它似乎与externalcode comp失败。下面的代码包含两个组件。因为没有错误,如果我注释掉显式组件部分,我会收到错误:

comp = GFWrapper(DVLO = np.array(DVLIST))

TypeError: init ()得到了一个意外的关键字参数'DVLO'

import numpy as np
import json  as js
import re,sys,subprocess,os
from openmdao.api import Problem, Group, IndepVarComp
from openmdao.api import  ScipyOptimizeDriver 
from openmdao.api import ExternalCode

class GFWrapper(ExternalCode):
    def initialize(self):
        self.metadata.declare('DVLO', types=np.ndarray)
    def setup(self):
        DVLO = self.metadata['DVLO']      
        for dv in DVLO:
            self.add_input(dv)
        self.add_output('OBJECTIVE')

        self.input_file  = 'GFWrapper_input.dat'
        self.output_file = 'GFWrapper_output.dat'
        self.options['external_input_files'] = [self.input_file,]
        self.options['external_output_files'] = [self.output_file,]

        self.options['command'] = [
            'python', 'run.py', self.input_file, self.output_file
        ]

        #self.declare_partials(of='*', wrt='*', method='fd')
        for dv in DVLO:
            self.declare_partials(of='OBJECTIVE', wrt=dv, method='fd')

    def compute(self, inputs, outputs):
        DVLO = self.metadata['DVLO']      

        # generate the input file for the  external code
        outF = open(self.input_file, "w")
        for dv in DVLO:
          outF.write(inputs[dv])
          print(dv,inputs[dv])
          outF.write("\n")
        outF.close()         

        # the parent compute function actually runs the external code
        super(GFWrapper, self).compute(inputs, outputs)
        # parse the output file from the external code
        file_contents=np.loadtxt(self.output_file)

        outputs['OBJECTIVE']          = file_contents[0]

from openmdao.api import ExplicitComponent
#
class GFWrapper(ExplicitComponent):
    def initialize(self):
        self.metadata.declare('DVLO', types=np.ndarray)
    def setup(self):
        DVLO = self.metadata['DVLO']      
        for dv in DVLO:
            self.add_input(dv)
        self.add_output('OBJECTIVE')

        #self.declare_partials(of='*', wrt='*', method='fd')
        for dv in DVLO:
            self.declare_partials(of='OBJECTIVE', wrt=dv, method='fd')
    def compute(self, inputs, outputs):
        DVLO = self.metadata['DVLO']  

        powetemp = 0
        for dv in DVLO:
            powetemp += inputs[dv]

        outputs['OBJECTIVE'] = powetemp        
#

DVLIST=['DV1','DV2']
DVMIN =[2,11] 
DVMAX =[3,12]
InitDVVal=[3,5]
nr_of_desvar=len(DVLIST)
top = Problem()
top.model = model = Group() 
" Introduce independent variables later will be design variables (optimization parameters) "
inputs_comp = IndepVarComp()
for i in range(nr_of_desvar):
    inputs_comp.add_output(DVLIST[i],InitDVVal[i]) 

" Add components/subsystems to the Group-model- "
model.add_subsystem('inputs_comp', inputs_comp)
comp = GFWrapper(DVLO=np.array(DVLIST))
model.add_subsystem('GFWrapper', comp) 

1 个答案:

答案 0 :(得分:1)

这是OpenMDAO 2.2版中的错误。发生错误是因为我们在ExternalCode的init语句中没有包含** kwargs。我们打算为V2.3修复此问题,但与此同时,您可以通过添加如下的init语句来解决GFWrapper组件:

array=malloc(n*sizeof(char*));