如何在Dymola的Python接口中添加向量形式的参数?
解决Python调用Dymola时向量参数传递失败的问题
问题描述
用Python调用Dymola的simulateExtendedModel接口运行自定义模型时,遇到向量类型参数传递报错:
- 模型中
HPT.Xi_cool_stat和HPT.Xi_cool_rot必须以向量形式输入(Dymola手动操作时用{0.0208189}格式可正常运行) - 直接传入数值或拼接成
"{0.0208189}"字符串均失败,报错信息:Error: Failed to parse response from Dymola. Error when calling Dymola function "simulateExtendedModel".
问题相关代码
初始未处理向量的代码
result = dymola.simulateExtendedModel( problem=My_Model, startTime=0.0, stopTime=50000, numberOfIntervals=500, method="Dassl", tolerance=1e-07, resultFile = r"C:\Users\arty\Desktop\DYMOLAEXTRACT\Resultats", initialNames=["evaporatorGas_altered.Shot", "evaporatorGas_altered.Scold", "PI2.y_start", "HPT.Xi_cool_stat", "HPT.Xi_cool_rot"], initialValues=[i, i, PI_initial_output_propeller_torque[counter], 0.0208189 , 0.03122835], finalNames=dym_lst_vars )
尝试的错误写法(拼接字符串)
result = dymola.simulateExtendedModel( problem=My_Model, startTime=0.0, stopTime=50000, numberOfIntervals=500, method="Dassl", tolerance=1e-07, resultFile = r"C:\Users\arty\Desktop\DYMOLAEXTRACT\Resultats", initialNames=["evaporatorGas_altered.Shot", "evaporatorGas_altered.Scold", "PI2.y_start", "HPT.Xi_cool_stat", "HPT.Xi_cool_rot"], initialValues=[i, i, PI_initial_output_propeller_torque[counter], "{"+str(0.0208189)+"}" , "{"+str(0.03122835)+"}"], finalNames=dym_lst_vars )
解决方案
Dymola Python接口会自动将Python列表映射为Dymola的向量格式,无需手动拼接大括号字符串,直接传入单元素列表即可:
result = dymola.simulateExtendedModel( problem=My_Model, startTime=0.0, stopTime=50000, numberOfIntervals=500, method="Dassl", tolerance=1e-07, resultFile = r"C:\Users\arty\Desktop\DYMOLAEXTRACT\Resultats", initialNames=["evaporatorGas_altered.Shot", "evaporatorGas_altered.Scold", "PI2.y_start", "HPT.Xi_cool_stat", "HPT.Xi_cool_rot"], initialValues=[i, i, PI_initial_output_propeller_torque[counter], [0.0208189], [0.03122835]], finalNames=dym_lst_vars )
说明
Dymola的Python API内置了类型映射逻辑,Python列表会被自动转换为Dymola识别的{...}向量格式,既避免了字符串解析错误,也保证了参数类型与模型要求匹配。
内容的提问来源于stack exchange,提问作者AaronModelica
相关产品推荐
相关产品推荐

