2017-09-06 23 views
0

私はOpenMDAOを使って簡単な暗黙の方程式を解こうとしました。式を以下に示すが、
X * Z + Z - = 0~4
Y = X + 2 * ZOpenMDAOの方程式系を解く方法は?

溶液は、Yは、x = 0.5のための5.833333を=、= 2.666667 Zです。この場合、私は以下に示されているコードを使用している

from __future__ import print_function 

from openmdao.api import Component, Group, Problem, Newton, ScipyGMRES 

class SimpleEquationSystem(Component): 
    """Solve the Equation 
      x*z + z - 4 = 0 
      y = x + 2*z 

     Solution: z = 2.666667, y = 5.833333 for x = 0.5 
    """ 

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


     self.add_param('x', 0.5) 
     self.add_state('y', 0.0) 
     self.add_state('z', 0.0) 
     self.iter=0 

    def solve_nonlinear(self, params, unknowns, resids): 
     """This component does no calculation on its own. It mainly holds the 
     initial value of the state. An OpenMDAO solver outside of this 
     component varies it to drive the residual to zero.""" 
     pass 

    def apply_nonlinear(self, params, unknowns, resids): 
     """ Report the residual """ 
     self.iter+=1 
     x=params['x'] 
     y = unknowns['y'] 
     z = unknowns['z'] 


     resids['y'] = x*z + z - 4 
     resids['z'] = x + 2*z - y 

     print('y_%d' % self.iter,'=%f' %resids['y'], 'z_%d' % self.iter, '=%f' %resids['z']) 
     print('x' ,'=%f' %x, 'y', '=%f' %y, 'z', '=%f' %z) 

top = Problem() 
root = top.root = Group() 
root.add('comp', SimpleEquationSystem()) 


# Tell these components to finite difference 
root.comp.deriv_options['type'] = 'fd' 
root.comp.deriv_options['form'] = 'central' 
root.comp.deriv_options['step_size'] = 1.0e-4 
root.nl_solver = Newton() 
root.ln_solver = ScipyGMRES() 

top.setup() 
top.print_all_convergence(level=1, depth=2) 
top.run() 
print('Solution x=%.2f, y=%.2f, z=%.2f' % (top['comp.x'], top['comp.y'], top['comp.z'])) 

私はSolving an Implicit Relationship with a Newton Solver方式に基づいてコードを書きました。 は、このコードを実行するために、私は

############################################## 
Setup: Checking root problem for potential issues... 

No recorders have been specified, so no data will be saved. 

The following parameters have no associated unknowns: 
comp.x 

The following components have no connections: 
comp 

Setup: Check of root problem complete. 
############################################## 

y_1 =-4.000000 z_1 =0.500000 
x =0.500000 y =0.000000 z =0.000000 
y_2 =-4.000000 z_2 =0.499900 
x =0.500000 y =0.000100 z =0.000000 
y_3 =-4.000000 z_3 =0.500100 
x =0.500000 y =-0.000100 z =0.000000 
y_4 =-3.999850 z_4 =0.500200 
x =0.500000 y =0.000000 z =0.000100 
y_5 =-4.000150 z_5 =0.499800 
x =0.500000 y =0.000000 z =-0.000100 
    [root] LN: GMRES 1 | 0 0 
    [root] LN: GMRES 1 | Converged in 1 iterations 
y_6 =-inf z_6 =-inf 
x =0.500000 y =inf z =-inf 
y_7 =-inf z_7 =-inf 
x =0.500000 y =inf z =-inf 
y_8 =-inf z_8 =-inf 
x =0.500000 y =inf z =-inf 
y_9 =-inf z_9 =-inf 
x =0.500000 y =inf z =-inf 
y_10 =-inf z_10 =-inf 
x =0.500000 y =inf z =-inf 
    [root] LN: GMRES 1000 | nan nan 
    [root] LN: GMRES 1000 | Converged in 1000 iterations 
y_11 =nan z_11 =nan 
x =0.500000 y =nan z =nan 
[root] NL: NEWTON 2 | nan nan (nan) 
[root] NL: NEWTON 2 | FAILED to converge after 2 iterations 
Solution x=0.50, y=nan, z=nan 
C:\ProgramData\Anaconda3\Anaconda3\lib\site-packages\openmdao\core\system.py:750: RuntimeWarning: invalid value encountered in subtract 
    resultvec.vec[:] -= cache2 

はあなたがそれぞれの反復でこの問題を解決する方法を、知ることができ、このようなソリューションを持って?。そして、この場合はsolve_nonlinear & apply_nonlinearをどのように構築するか教えてください。あなたが独立変数のためIndepvarcompを追加するときに問題が晴れるよう

答えて

1

はルックス:

root.add('p1', IndepVarComp('x', 0.5)) 
root.add('comp', SimpleEquationSystem()) 
root.connect('p1.x', 'comp.x') 

そして、それはすぐに収束します。

[root] NL: NEWTON 1 | 2.73692411e-11 6.7894731e-12 (6.41396046825) 
[root] NL: NEWTON 1 | Converged in 1 iterations 

これを機能させるために追加する必要はありません。おそらくバグです。私はこの問題がOpenMDAOの最新開発版で修正されたことを確認しました。

+0

ありがとう、ケネス。それは私たちにとって非常に便利です。しかし、私は最新の開発版[OpenMDAO](https://github.com/OpenMDAO/OpenMDAO)でこれらを試しました。それも同じエラーを繰り返した。 IndepVarComp変数を追加せずに問題を実行することはできません。 –

+0

Moore博士は、まだリリースされていない2.xバージョンを使用していたと思います。 – kmarsteller

関連する問題