Openmdao V1.7 Sellar MDF

Openmdao V1.7 Sellar MDF,mdf,openmdao,Mdf,Openmdao,我在OpenMDAO()的文档页面上发现了一些奇怪的sellar问题MDA 如果我提取代码并只运行MDA(在规程中添加计数器),我会发现规程之间的调用数不同(d1规程的调用数是d2的两倍),这是不期望的。有人有答案吗 以下是结果 耦合变量:25.588303,12.058488 专业1和专业2的电话数量(10,5) 这是代码 # For printing, use this import if you are running Python 2.x from __future__ import p

我在OpenMDAO()的文档页面上发现了一些奇怪的sellar问题MDA

如果我提取代码并只运行MDA(在规程中添加计数器),我会发现规程之间的调用数不同(d1规程的调用数是d2的两倍),这是不期望的。有人有答案吗

以下是结果

耦合变量:25.588303,12.058488 专业1和专业2的电话数量(10,5)

这是代码

# For printing, use this import if you are running Python 2.x from __future__ import print_function


import numpy as np

from openmdao.api import Component from openmdao.api import ExecComp, IndepVarComp, Group, NLGaussSeidel, \
                         ScipyGMRES

class SellarDis1(Component):
    """Component containing Discipline 1."""

    def __init__(self):
        super(SellarDis1, self).__init__()

        # Global Design Variable
        self.add_param('z', val=np.zeros(2))

        # Local Design Variable
        self.add_param('x', val=0.)

        # Coupling parameter
        self.add_param('y2', val=1.0)

        # Coupling output
        self.add_output('y1', val=1.0)
        self.execution_count = 0

    def solve_nonlinear(self, params, unknowns, resids):
        """Evaluates the equation
        y1 = z1**2 + z2 + x1 - 0.2*y2"""

        z1 = params['z'][0]
        z2 = params['z'][1]
        x1 = params['x']
        y2 = params['y2']

        unknowns['y1'] = z1**2 + z2 + x1 - 0.2*y2
        self.execution_count += 1
    def linearize(self, params, unknowns, resids):
        """ Jacobian for Sellar discipline 1."""
        J = {}

        J['y1','y2'] = -0.2
        J['y1','z'] = np.array([[2*params['z'][0], 1.0]])
        J['y1','x'] = 1.0

        return J


class SellarDis2(Component):
    """Component containing Discipline 2."""

    def __init__(self):
        super(SellarDis2, self).__init__()

        # Global Design Variable
        self.add_param('z', val=np.zeros(2))

        # Coupling parameter
        self.add_param('y1', val=1.0)

        # Coupling output
        self.add_output('y2', val=1.0)
        self.execution_count = 0
    def solve_nonlinear(self, params, unknowns, resids):
        """Evaluates the equation
        y2 = y1**(.5) + z1 + z2"""

        z1 = params['z'][0]
        z2 = params['z'][1]
        y1 = params['y1']

        # Note: this may cause some issues. However, y1 is constrained to be
        # above 3.16, so lets just let it converge, and the optimizer will
        # throw it out
        y1 = abs(y1)

        unknowns['y2'] = y1**.5 + z1 + z2
        self.execution_count += 1
    def linearize(self, params, unknowns, resids):
        """ Jacobian for Sellar discipline 2."""
        J = {}

        J['y2', 'y1'] = .5*params['y1']**-.5

        #Extra set of brackets below ensure we have a 2D array instead of a 1D array
        # for the Jacobian;  Note that Jacobian is 2D (num outputs x num inputs).
        J['y2', 'z'] = np.array([[1.0, 1.0]])

        return J



class SellarDerivatives(Group):
    """ Group containing the Sellar MDA. This version uses the disciplines
    with derivatives."""

    def __init__(self):
        super(SellarDerivatives, self).__init__()

        self.add('px', IndepVarComp('x', 1.0), promotes=['x'])
        self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z'])

        self.add('d1', SellarDis1(), promotes=['z', 'x', 'y1', 'y2'])
        self.add('d2', SellarDis2(), promotes=['z', 'y1', 'y2'])

        self.add('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                                     z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0),
                 promotes=['obj', 'z', 'x', 'y1', 'y2'])

        self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['y1', 'con1'])
        self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2'])

        self.nl_solver = NLGaussSeidel()
        self.nl_solver.options['atol'] = 1.0e-12

        self.ln_solver = ScipyGMRES()
         from openmdao.api import Problem, ScipyOptimizer

top = Problem() top.root = SellarDerivatives()

#top.driver = ScipyOptimizer()
#top.driver.options['optimizer'] = 'SLSQP'
#top.driver.options['tol'] = 1.0e-8
#
#top.driver.add_desvar('z', lower=np.array([-10.0, 0.0]),
#                     upper=np.array([10.0, 10.0]))
#top.driver.add_desvar('x', lower=0.0, upper=10.0)
#
#top.driver.add_objective('obj')
#top.driver.add_constraint('con1', upper=0.0)
#top.driver.add_constraint('con2', upper=0.0)

top.setup()

# Setting initial values for design variables top['x'] = 1.0 top['z'] = np.array([5.0, 2.0])

top.run()

print("\n")

print("Coupling vars: %f, %f" % (top['y1'], top['y2']))


count1 = top.root.d1.execution_count 
count2 = top.root.d2.execution_count 
print("Number of discipline 1 and 2 calls (%i,%i)"% (count1,count2))

这是一个很好的观察结果。只要有一个循环,“头部”组件就会再次运行。原因如下:

如果模型的组件包含隐式状态,则单个执行如下所示:

  • 调用
    solve\u非线性
    执行组件
  • 调用
    apply\u非线性
    计算残差
  • 我们在这个模型中没有任何隐式状态的组件,但是我们通过一个循环间接地创造了对一个组件的需求。我们的执行情况如下:

  • 调用
    solve\u非线性
    执行所有组件
  • 调用
    仅对“head”组件应用非线性
    (它缓存未知数,调用
    解非线性
    ,并保存未知数的差异),以生成我们可以收敛的残差

  • 在这里,head组件只是根据执行的第一个组件,但是它决定了运行循环的顺序。通过构建包含两个以上组件的循环,您可以验证只有一个头部组件获得额外运行。

    谢谢您的回答!我现在更了解这种行为了。我现在更了解这种行为了。但是,如果您使用牛顿方法的stateconnection组件的教程,我希望现在我们添加了一个具有“apply_Nonal”函数的组件,这两个规程将仅用于“solve_linear”部分。但在这里,学科1也被称为2次(顺序是:学科1,学科2,学科1和状态连接)。openmdao还需要一个“头”组件吗?我认为使用状态连接组件,模型中仍然有一个循环。通过手动设置执行顺序,使状态连接组件(具有自己的apply_linear)成为顺序中的“头”或第一个组件,可以防止任何规程组件重复运行。