gpt4 book ai didi

python - 从 python 脚本重现 Dymola 信息层

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:37 24 4
gpt4 key购买 nike

我想编写一个 Python 脚本来提取有关 Modelica 函数的输入和输出的相同信息,如 Dymola 信息层中所示,例如对于正弦函数:

4782

我想展示一个最小的例子,但我什至不知道从哪里开始。

最佳答案

Dymola 为模型文档动态生成此信息,但我不知道有什么方法可以检索它。

ModelManagement 库(随 Dymola 一起提供并包含在标准许可证中)允许从加载的类中提取此类信息。但是,使用起来有点痛苦。问题是这个库中的大多数函数只分析类的确切代码——不考虑扩展类。因此,您必须自己循环扩展类。

下面是一个示例,它试图获取类的所有组件定义,包括从扩展类继承的组件定义。对于每个组件,都会返回一个具有多个属性的字典,这对应于 Dymola 中的记录 ModelManagement.Structure.AST.ComponentAttributes。它包含组件名称、描述、可变性、前缀 inputoutput 等信息。

因此,对于您的示例类 Modelica.Math.sin,可以使用字典键 isOutputisInput 轻松识别输入和输出。

但请注意,这不适用于像 Modelica.Blocks.Interfaces.RealInput 这样的 block 的输入和输出连接器 - 以及像电针这样的因果连接器。你必须自己实现一些东西来识别那些。

下面的代码只是使用固定的类路径来列出 Modelica.Blocks.Continuous.Integrator 的所有输入和输出连接器。此外,它还输出所有参数。

import pprint
from dymola.dymola_interface import DymolaInterface


def get_extends(dym, c):
""" return a list with all extended classes (including inherited extends) """

extends = dym.ExecuteCommand(f'ModelManagement.Structure.AST.ExtendsInClassAttributes("{c}")')

for e in extends.copy():

sub_extends = get_extends(dym, e['fullTypeName'])

# only add sub-extends if they are not already in the list
for s in sub_extends:
if not any(s['fullTypeName'] == tmp_e['fullTypeName'] for tmp_e in extends):
extends.append(s)

return extends


def get_components(dym, c):
""" return a list with all components in a class (including inherited ones )"""

# get components defined in the class itself
comp = dym.ExecuteCommand(f'ModelManagement.Structure.AST.ComponentsInClassAttributes("{c}")')

# get components defined in extended classes itself
for e in get_extends(dym, c):
comp.extend(dym.ExecuteCommand(
f'ModelManagement.Structure.AST.ComponentsInClassAttributes("{e["fullTypeName"]}")'))

return comp


if __name__ == '__main__':

modelica_class = 'Modelica.Blocks.Continuous.Integrator'

# get all components in the model
dymola = DymolaInterface(showwindow=False)
components = get_components(dymola, modelica_class)
dymola.close()

# extract inputs and parameters
inputs = ['Modelica.Blocks.Interfaces.RealInput', 'Modelica.Blocks.Interfaces.BooleanInput',
'Modelica.Blocks.Interfaces.BooleanInput']

inputs = [c for c in components if c['fullTypeName'] in inputs]
parameters = [c for c in components if c['variability'] == 'parameter']

print('-------- inputs ------------')
pprint.pprint(inputs)
print('\n\n-------- parameters ------------')
pprint.pprint(parameters)

关于python - 从 python 脚本重现 Dymola 信息层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58627324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com