- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 OpenMDAO 求解简单的隐式方程。方程如下所示,
x*z + z - 4 = 0
y = x + 2*z
当 x = 0.5 时,解为 z = 2.666667,y = 5.833333。
对于这种情况,我使用了如下所示的代码,
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 时,问题似乎就解决了:
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 开发版本中得到修复。
关于python - 如何在 OpenMDAO 中求解方程组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073733/
我目前正在努力使用 python 解决线性方程组。我曾尝试使用 numpy.linalg.solve,但似乎这只适用于方形数组,而我的则不然。是否有另一个我可以用来解决我不知道的系统的函数,或者我应该
我的代码第一次迭代运行良好,但之后输出以下错误: ValueError: matrix must be 2-dimensional 据我所知(这在Python中并不多),我的代码是正确的。但我不知道为
是否可以使用 sympy 求解方程组(线性或非线性),其中输出为符号? 示例: 1. f_m = a0 + a1*(-dx) + a2*(-dx)^2 2. f_c = a0 3. f_p =
我想使用两个或更多输入来创建更精确的变量估计。我已经仅使用一个输入和一个 FOPDT 方程对其进行了估算,但是当我尝试添加一个输入和相应的 k、tau 和 theta 以及另一个方程时,我收到“未找到
我有一个像这样的字符串(变量和常量的数量并不重要): > my_string A b A x y z [1,] 1 0 1 [2,] 1 3 2 [3,] 3 1 1 > b [1]
我是一名优秀的程序员,十分优秀!