每次迭代的compute_partials调用顺序

时间:2019-03-29 06:50:01

标签: openmdao

为了尽量减少每次迭代的重复计算次数,我在计算方法中使用了一些额外的类变量,这些变量也用在compute_partials()中。

(请参见下面的代码片段,我的意思非常清楚。)

问题是;

在compute()之前是否有任何调用compute_partials()的情况。 与下面的代码类似,使用compute()和compute_partials()是否有任何风险(请参见这两种方法)

class MomentOfInertiaComp(ExplicitComponent):

    def initialize(self):
        self.options.declare('num_elements', types=int)
        self.options.declare('b')
        self.compcou=0
        self.partcou =0

    def setup(self):
        num_elements = self.options['num_elements']

        self.add_input('h', shape=num_elements)
        self.add_output('I', shape=num_elements)

        rows = np.arange(num_elements)
        cols = np.arange(num_elements)
        self.declare_partials('I', 'h', rows=rows, cols=cols)

    def compute(self, inputs, outputs):
        b = self.options['b']

        # Instead of this line 
        # outputs['I'] = 1./12. * b * inputs['h'] ** 3

        # these 2 lines are used
        self.var=inputs['h'] ** 2
        outputs['I'] = 1./12. * b * inputs['h'] * self.var


        self.compcou += 1

    def compute_partials(self, inputs, partials):
        b = self.options['b']
        self.partcou += 1
        # instead of this 
        # partials['I', 'h'] = 1./4. * b * inputs['h'] ** 2
        # this is used
        partials['I', 'h'] = 1./4. * b * self.var

1 个答案:

答案 0 :(得分:1)

Openmdao不保证在compute_partials之前调用compute。您需要假设它们是完全独立的。