0

我正在使用 python fmpy 来运行 fmu 模型。执行、运行和绘制结果工作正常。也定义输入(按照教程)。

但我正在努力改变 fmu 模型中全局参数的值。

例如,如果我在 GUI ( https://fmpy.readthedocs.io/en/latest/tutorial/ ) 中运行相同的模型,我将获得所有可用参数的完整列表。在那里,我可以将名为“l1”的参数的值设置为 412。如果我记录 GUI 的 FMI 调用,我可以看到,使用以下命令将“l1”设置为 412:

fmi2SetReal(fmu, vr=[671088641], nvr=1, value=[412])

如何使用 python 命令将“l1”设置为 412?

from fmpy.util import plot_result
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from fmpy import *

fmu = 'CrankCase_ME2.fmu'
dump(fmu)

# read the model description
model_description = read_model_description(fmu)

# collect the value references
vrs = {}
for variable in model_description.modelVariables:
    vrs[variable.name] = variable.valueReference

L = 412
hPLug = 215
alphaDrvie = 60
start_vrs = [vrs['l1'], vrs['h_plug'], vrs['Alpha_drive']]
start_values = [L, hPLug, alphaDrvie]

dtype = [('time', np.double), ('l1', np.int), ('h_plug', np.int), ('Alpha_drive', np.int)]
signals = np.array([(0.0, L, hPLug, alphaDrvie)], dtype=dtype)

print_interval = 1e-5
result = simulate_fmu(fmu, start_time=0, stop_time=0.1, relative_tolerance=1e-7, output_interval=print_interval, input=signals)

t = result['time']
y_Force = result['expseu_.Out2'] # y force
plt.plot(t, y_Force)

我的 fmu 是 Model Exchange 类型。我试图将其定义为输入,但这不起作用。

有什么建议么?

干杯

4

1 回答 1

0

所以我才想通...

要在 fmpy 中定义全局参数,使用“start_values”语句。在Coupled_clutches.py中有一个很好的例子。但为了完整起见,这里有一个“Rectifier.fmu”模型的最小工作示例。'vrs' 包含 fmu 模型中所有保存的变量,在那里你可以找到频率'f'。默认值设置为 50Hz,如果频率参数应设置为 100Hz,只需使用 'start_values={'f': 100}' ,例如:

from fmpy import dump, simulate_fmu, read_model_description
from fmpy.util import plot_result

fmu = 'Rectifier.fmu'
dump(fmu)

model_description = read_model_description(fmu)

vrs = {}
for variable in model_description.modelVariables:
    vrs[variable.name] = variable.valueReference

print_interval = 1e-5
result = simulate_fmu(fmu, 
                      start_time=0, 
                      stop_time=0.1, 
                      relative_tolerance=1e-7, 
                      output_interval=print_interval, 
                      start_values={'f': 100})

plot_result(result)

如果您想直观地探索保存的参数,您可以在FMPy GUI中打开 fmu

于 2019-10-17T12:23:12.280 回答