使用 OpenMDAO v1.7.3 Alpha
我有一个简单的示例OpenMDAO问题,其中包含2个隐式组件:
(问题底部的代码)
不幸的是,当我在OpenMDAO中运行它时,它未能收敛。我认为问题是我没有设置初始参数值。因为OpenMDAO正在打印以下消息以输出:
以下参数与未知变量连接,这些未知变量会不按顺序更新,因此它们的初始值可能包含未初始化的未知值:['battery_temp_comp.volt']
我一直在搜寻documentation,但无法弄清楚如何正确设置这种类型的问题(使用隐式组件)。任何帮助表示赞赏。谢谢!
代码:
from __future__ import print_function
import math as m
import numpy
from openmdao.api import Component, Group, Problem, Newton, ScipyGMRES
class BatteryVoltImplicitComp(Component):
""" A Simple Implicit Component modelling a Battery's Voltage"""
def __init__(self):
super(BatteryVoltImplicitComp, self).__init__()
# Params
self.add_param('temp', 30.0)
# Unknowns
self.add_output('volt', 12.0)
# States
self.add_state('delta', 0.0)
def calc_voltage(self, params, unknowns, resids):
temp = params['temp']
voltage_shift = (25.667 - temp) * 0.02933
return 12.77 + voltage_shift
def solve_nonlinear(self, params, unknowns, resids):
pass
def apply_nonlinear(self, params, unknowns, resids):
print("Params: BatteryVoltImplicitComp.temp: {}".format(params['temp']))
print("Unknowns: BatteryVoltImplicitComp.volt: {}".format(unknowns['volt']))
print("Resids: BatteryVoltImplicitComp.delta: {}".format(resids['delta']))
calculated_voltage = self.calc_voltage(params, unknowns, resids)
resids['delta'] = calculated_voltage - unknowns['volt']
class BatteryTempImplicitComp(Component):
""" A Simple Implicit Component modelling a Battery's Temperature"""
def __init__(self):
super(BatteryTempImplicitComp, self).__init__()
# Params
self.add_param('volt', 12.0)
# Unknowns
self.add_output('temp', 30.0)
# States
self.add_state('delta', 0.0)
def calc_temp(self, params, unknowns, resids):
v = params['volt']
room_temp = 23.0 # celsius
batt_resist = 50e-3
power = v**2 / batt_resist
temp_increase = power / 50 # magic number
return room_temp + temp_increase
def solve_nonlinear(self, params, unknowns, resids):
pass
def apply_nonlinear(self, params, unknowns, resids):
print("Params: BatteryTempImplicitComp.volt: {}".format(params['volt']))
print("Unknowns: BatteryTempImplicitComp.temp: {}".format(unknowns['temp']))
print("Resids: BatteryTempImplicitComp.delta: {}".format(resids['delta']))
calculated_temp = self.calc_temp(params, unknowns, resids)
resids['delta'] = calculated_temp - unknowns['temp']
if __name__ == '__main__':
top = Problem()
root = top.root = Group()
root.add('battery_temp_comp', BatteryTempImplicitComp())
root.add('battery_volt_comp', BatteryVoltImplicitComp())
root.connect('battery_volt_comp.volt', 'battery_temp_comp.volt')
root.connect('battery_temp_comp.temp', 'battery_volt_comp.temp')
root.battery_temp_comp.deriv_options['type'] = 'fd'
root.battery_temp_comp.deriv_options['form'] = 'central'
root.battery_temp_comp.deriv_options['step_size'] = 1.0e-1
root.battery_volt_comp.deriv_options['type'] = 'fd'
root.battery_volt_comp.deriv_options['form'] = 'central'
root.battery_volt_comp.deriv_options['step_size'] = 1.0e-1
root.nl_solver = Newton()
root.ln_solver = ScipyGMRES()
top.setup()
top.run()
print('Solution (battery_volt_comp): volt = {}, temp = {}, resid = {}'.format(top['battery_volt_comp.volt'], top['battery_volt_comp.temp'], top['battery_volt_comp.delta']))
print('Solution (battery_temp_comp): volt = {}, temp = {}, resid = {}'.format(top['battery_temp_comp.volt'], top['battery_temp_comp.temp'], top['battery_temp_comp.delta']))
输出:
#######################################设置:正在检查根本问题以发现潜在问题...
未指定记录器,因此不会保存任何数据。组'' 具有以下周期:[[''battery_temp_comp','battery_volt_comp']]
以下参数与未知对象相关联,这些未知对象通过更新 顺序,因此它们的初始值可能包含未初始化的未知数 值:['battery_temp_comp.volt']
设置:根本问题检查完毕。
#######################################参数:BatteryTempImplicitComp.volt:12.0
未知:BatteryTempImplicitComp.temp:30.0
残留物:BatteryTempImplicitComp.delta:0.0
参数:BatteryVoltImplicitComp.temp:30.0
未知数:BatteryVoltImplicitComp.volt:12.0
残留物:BatteryVoltImplicitComp.delta:0.0
参数:BatteryTempImplicitComp.volt:12.1
未知:BatteryTempImplicitComp.temp:30.0
残留物:BatteryTempImplicitComp.delta:50.6
参数:BatteryTempImplicitComp.volt:11.9
未知:BatteryTempImplicitComp.temp:30.0
残留物:BatteryTempImplicitComp.delta:50.6
参数:BatteryTempImplicitComp.volt:12.0
未知:BatteryTempImplicitComp.temp:30.0
残留物:BatteryTempImplicitComp.delta:50.6
参数:BatteryTempImplicitComp.volt:12.0
未知:BatteryTempImplicitComp.temp:30.0
残留物:BatteryTempImplicitComp.delta:50.6
参数:BatteryVoltImplicitComp.temp:30.1
未知数:BatteryVoltImplicitComp.volt:12.0
残留物:BatteryVoltImplicitComp.delta:0.64291311
参数:BatteryVoltImplicitComp.temp:29.9
未知数:BatteryVoltImplicitComp.volt:12.0
残留物:BatteryVoltImplicitComp.delta:0.64291311
...
参数:BatteryVoltImplicitComp.temp:nan
未知:BatteryVoltImplicitComp.volt:nan
残留物:BatteryVoltImplicitComp.delta:0.64291311
[root] NL:NEWTON 1 | 1次迭代后无法收敛
解决方案(battery_volt_comp):伏特= nan,温度= nan,残渣= -inf
解决方案(battery_temp_comp):伏特= nan,温度= nan,残渣= -inf
答案 0 :(得分:0)
我解决了这个问题。代替
# Params
self.add_param('temp', 30.0)
# Unknowns
self.add_output('volt', 12.0)
# States
self.add_state('delta', 0.0)
...
resids['delta'] = calculated_temp - unknowns['temp']
我需要这样做:
# Params
self.add_param('temp', 30.0)
# Unknowns / Residuals
self.add_state('volt', 12.0)
...
resids['volt'] = calculated_voltage - unknowns['volt']
不幸的是,当我在OpenMDAO中运行它时,它未能收敛。我认为问题在于我没有设置初始参数值。
虽然初始参数值不是我的问题的原因,但设置初始值的最简单方法是:
top.setup()
top['battery_temp_comp.temp'] = 23.0
top.run()
第一次执行的顺序由connect
语句的顺序确定。 Group.list_order()
将打印出组中组件的执行顺序。
代码:
from __future__ import print_function
from openmdao.api import Component, Group, Problem, Newton, ScipyGMRES
class BatteryVoltImplicitComp(Component):
""" A Simple Implicit Component modeling a Battery's Voltage"""
def __init__(self):
super(BatteryVoltImplicitComp, self).__init__()
# Params
self.add_param('temp', 30.0)
# Unknowns / Residuals
self.add_state('volt', 12.0)
def calc_voltage(self, params, unknowns, resids):
temp = params['temp']
voltage_shift = (25.667 - temp) * 0.02933
return 12.77 + voltage_shift
def solve_nonlinear(self, params, unknowns, resids):
pass
def apply_nonlinear(self, params, unknowns, resids):
print("Params: BatteryVoltImplicitComp.temp: {}".format(params['temp']))
print("Unknowns: BatteryVoltImplicitComp.volt: {}".format(unknowns['volt']))
print("Resids: BatteryVoltImplicitComp.volt: {}".format(resids['volt']))
calculated_voltage = self.calc_voltage(params, unknowns, resids)
resids['volt'] = calculated_voltage - unknowns['volt']
class BatteryTempImplicitComp(Component):
""" A Simple Implicit Component modeling a Battery's Temperature"""
def __init__(self):
super(BatteryTempImplicitComp, self).__init__()
# Params
self.add_param('volt', 12.0)
# Unknowns / Residuals
self.add_state('temp', 30.0)
def calc_temp(self, params, unknowns, resids):
v = params['volt']
room_temp = 23.0 # celsius
batt_resist = 50e-3
power = v**2 / batt_resist
temp_increase = power / 50 # magic number
return room_temp + temp_increase
def solve_nonlinear(self, params, unknowns, resids):
pass
def apply_nonlinear(self, params, unknowns, resids):
print("Params: BatteryTempImplicitComp.volt: {}".format(params['volt']))
print("Unknowns: BatteryTempImplicitComp.temp: {}".format(unknowns['temp']))
print("Resids: BatteryTempImplicitComp.temp: {}".format(resids['temp']))
calculated_temp = self.calc_temp(params, unknowns, resids)
resids['temp'] = calculated_temp - unknowns['temp']
if __name__ == '__main__':
top = Problem()
root = top.root = Group()
root.add('battery_volt_comp', BatteryVoltImplicitComp())
root.add('battery_temp_comp', BatteryTempImplicitComp())
root.connect('battery_volt_comp.volt', 'battery_temp_comp.volt')
root.connect('battery_temp_comp.temp', 'battery_volt_comp.temp')
root.battery_temp_comp.deriv_options['type'] = 'fd'
root.battery_temp_comp.deriv_options['form'] = 'central'
root.battery_temp_comp.deriv_options['step_size'] = 1.0e-4
root.battery_volt_comp.deriv_options['type'] = 'fd'
root.battery_volt_comp.deriv_options['form'] = 'central'
root.battery_volt_comp.deriv_options['step_size'] = 1.0e-4
root.nl_solver = Newton()
root.ln_solver = ScipyGMRES()
top.setup()
top['battery_temp_comp.temp'] = 23.0
top.run()
print('Solution (battery_volt_comp): volt = {}, temp = {}'.format(top['battery_volt_comp.volt'], top['battery_volt_comp.temp']))
print('Solution (battery_temp_comp): volt = {}, temp = {}'.format(top['battery_temp_comp.volt'], top['battery_temp_comp.temp']))
输出:
#######################################设置:正在检查根本问题以发现潜在问题...
未指定记录器,因此不会保存任何数据。组'' 具有以下周期:[[''battery_temp_comp','battery_volt_comp']]
以下参数与无序更新的未知数相关, 因此它们的初始值可能包含未初始化的未知值: ['battery_temp_comp.volt']
设置:根本问题检查完毕。
#######################################参数:BatteryTempImplicitComp.volt:12.0
未知:BatteryTempImplicitComp.temp:100.0
残留物:BatteryTempImplicitComp.temp:0.0
参数:BatteryVoltImplicitComp.temp:100.0
未知数:BatteryVoltImplicitComp.volt:12.0
残留物:BatteryVoltImplicitComp.volt:0.0
参数:BatteryVoltImplicitComp.temp:100.0001
未知数:BatteryVoltImplicitComp.volt:12.0
残留物:BatteryVoltImplicitComp.volt:-1.41018689
参数:BatteryVoltImplicitComp.temp:99.9999
未知数:BatteryVoltImplicitComp.volt:12.0
残留物:BatteryVoltImplicitComp.volt:-1.41018689
参数:BatteryVoltImplicitComp.temp:100.0
未知数:BatteryVoltImplicitComp.volt:12.0001
残留物:BatteryVoltImplicitComp.volt:-1.41018689
参数:BatteryVoltImplicitComp.temp:100.0
未知数:BatteryVoltImplicitComp.volt:11.9999
残留物:BatteryVoltImplicitComp.volt:-1.41018689
参数:BatteryTempImplicitComp.volt:12.0001
未知:BatteryTempImplicitComp.temp:100.0
残留物:BatteryTempImplicitComp.temp:-19.4
参数:BatteryTempImplicitComp.volt:11.9999
未知:BatteryTempImplicitComp.temp:100.0
残留物:BatteryTempImplicitComp.temp:-19.4
参数:BatteryTempImplicitComp.volt:12.0
未知:BatteryTempImplicitComp.temp:100.0001
残留物:BatteryTempImplicitComp.temp:-19.4
参数:BatteryTempImplicitComp.volt:12.0
未知:BatteryTempImplicitComp.temp:99.9999
残留物:BatteryTempImplicitComp.temp:-19.4
参数:BatteryTempImplicitComp.volt:11.3436283598
未知:BatteryTempImplicitComp.temp:74.2988322548
残留物:BatteryTempImplicitComp.temp:-19.4
参数:BatteryVoltImplicitComp.temp:74.2988322548
未知数:BatteryVoltImplicitComp.volt:11.3436283598
残留物:BatteryVoltImplicitComp.volt:-1.41018689
参数:BatteryVoltImplicitComp.temp:74.2989322548
未知数:BatteryVoltImplicitComp.volt:11.3436283598
残留物:BatteryVoltImplicitComp.volt:1.77166725734e-10
参数:BatteryVoltImplicitComp.temp:74.2987322548
未知数:BatteryVoltImplicitComp.volt:11.3436283598
残留物:BatteryVoltImplicitComp.volt:1.77166725734e-10
参数:BatteryVoltImplicitComp.temp:74.2988322548
未知数:BatteryVoltImplicitComp.volt:11.3437283598
残留物:BatteryVoltImplicitComp.volt:1.77166725734e-10
参数:BatteryVoltImplicitComp.temp:74.2988322548
未知数:BatteryVoltImplicitComp.volt:11.3435283598
残留物:BatteryVoltImplicitComp.volt:1.77166725734e-10
参数:BatteryTempImplicitComp.volt:11.3437283598
未知:BatteryTempImplicitComp.temp:74.2988322548
残留物:BatteryTempImplicitComp.temp:0.172329491217
参数:BatteryTempImplicitComp.volt:11.3435283598
未知:BatteryTempImplicitComp.temp:74.2988322548
残留物:BatteryTempImplicitComp.temp:0.172329491217
参数:BatteryTempImplicitComp.volt:11.3436283598
未知:BatteryTempImplicitComp.temp:74.2989322548
残留物:BatteryTempImplicitComp.temp:0.172329491217
参数:BatteryTempImplicitComp.volt:11.3436283598
未知:BatteryTempImplicitComp.temp:74.2987322548
残留物:BatteryTempImplicitComp.temp:0.172329491217
参数:BatteryTempImplicitComp.volt:11.3396364502
未知:BatteryTempImplicitComp.temp:74.4349355547
残留物:BatteryTempImplicitComp.temp:0.172329491217
参数:BatteryVoltImplicitComp.temp:74.4349355547
未知数:BatteryVoltImplicitComp.volt:11.3396364502
残留物:BatteryVoltImplicitComp.volt:1.77166725734e-10
参数:BatteryVoltImplicitComp.temp:74.4350355547
未知数:BatteryVoltImplicitComp.volt:11.3396364502
残留物:BatteryVoltImplicitComp.volt:2.87769807983e-13
参数:BatteryVoltImplicitComp.temp:74.4348355547
未知数:BatteryVoltImplicitComp.volt:11.3396364502
残留物:BatteryVoltImplicitComp.volt:2.87769807983e-13
参数:BatteryVoltImplicitComp.temp:74.4349355547
未知数:BatteryVoltImplicitComp.volt:11.3397364502
残留物:BatteryVoltImplicitComp.volt:2.87769807983e-13
参数:BatteryVoltImplicitComp.temp:74.4349355547
未知数:BatteryVoltImplicitComp.volt:11.3395364502
残留物:BatteryVoltImplicitComp.volt:2.87769807983e-13
参数:BatteryTempImplicitComp.volt:11.3397364502
未知:BatteryTempImplicitComp.temp:74.4349355547
残留物:BatteryTempImplicitComp.temp:6.37414106563e-06
参数:BatteryTempImplicitComp.volt:11.3395364502
未知:BatteryTempImplicitComp.temp:74.4349355547
残留物:BatteryTempImplicitComp.temp:6.37414106563e-06
参数:BatteryTempImplicitComp.volt:11.3396364502
未知:BatteryTempImplicitComp.temp:74.4350355547
残留物:BatteryTempImplicitComp.temp:6.37414106563e-06
参数:BatteryTempImplicitComp.volt:11.3396364502
未知:BatteryTempImplicitComp.temp:74.4348355547
残留物:BatteryTempImplicitComp.temp:6.37414106563e-06
参数:BatteryTempImplicitComp.volt:11.3396363025
未知:BatteryTempImplicitComp.temp:74.4349405893
残留物:BatteryTempImplicitComp.temp:6.37414106563e-06
参数:BatteryVoltImplicitComp.temp:74.4349405893
未知数:BatteryVoltImplicitComp.volt:11.3396363025
残留物:BatteryVoltImplicitComp.volt:2.87769807983e-13
解决方案(battery_volt_comp):伏特= 11.3396363025,温度= 74.4349405893
解决方案(battery_temp_comp):伏特= 11.3396363025,温度= 74.4349405893