0

我正在使用 OpenModelica 从 FMPy 和 PyFMI 生成的 FMU。

当我尝试从 FMU 获取方向导数时,会出现崩溃(两个库)。

FMU 是从控制台生成的:

c:\openmodelica1.13.0-dev-64bit\bin\omc -n=1 -d=-disableDirectionalDerivatives --postOptModules+=wrapFunctionCalls fmu.mos > error_fmu.txt 2>&1

我做错了什么还是FMU代码中的错误?

fmu.mos

loadModel(Modelica);
loadFile("E10_linear.mo");
translateModelFMU(E10_linear,version="2",fmuType="me", platforms={"static"}); 
getErrorString();

E10_linear.mo

model E10_linear
  // Parameters
  parameter Real a = 10.0;
  // Variables
  Real x(start = 1.0, fixed = true);

equation

  der(x) = a;

annotation(
    experiment(StartTime = 0, StopTime = 2, Tolerance = 1e-06, Interval = 1));
end E10_linear;

pyfmi_test_script.py

#!/usr/bin/env python 
# -*- coding: utf-8 -*-

import os as O

import matplotlib
import matplotlib.pyplot as plt

import numpy as N

from pyfmi import load_fmu

curr_dir = O.path.dirname(O.path.abspath(__file__));
path_to_fmus = O.path.join(curr_dir, 'FMUs') # The FMU must be in this folder!!!!!!


def run_fmu_native(filename, list_seed_vector, with_plots=True):
    """E10_linear (native) 

    Based on https://jmodelica.org/pyfmi/_modules/pyfmi/examples/fmi_bouncing_ball_native.html#run_demo

    """
    fmu_name = O.path.join(path_to_fmus, filename)
    model = load_fmu(fmu_name)

    Tstart = 0.0 #The start time.
    Tend   = 2.0 #The final simulation time.

    model.time = Tstart #Set the start time before the initialization.

    #Initialize the model. Also sets all the start attributes defined in the 
    # XML file.
    model.initialize() 

    #Get Continuous States
    x = model.continuous_states
    #Get the Nominal Values
    x_nominal = model.nominal_continuous_states
    #Get the Event Indicators
    event_ind = model.get_event_indicators()

    # Snippet from https://jmodelica.org/pyfmi/pyfmi.html#pyfmi.fmi.FMUModelBase2.get_directional_derivative
    assert model.get_capability_flags()["providesDirectionalDerivatives"]==True #Assert directional derivatives are provided

    states = model.get_states_list()
    states_references = [s.value_reference for s in states.values()]
    derivatives = model.get_derivatives_list()
    derivatives_references = [d.value_reference for d in derivatives.values()]

    print('state_references: {}'.format(states_references))
    print('derivatives_references: {}'.format(derivatives_references))
    print('seed vector: {}'.format(list_seed_vector))

    dd = model.get_directional_derivative(states_references, derivatives_references, list_seed_vector)

    #print('dd: {}'.format(dd))

if __name__ == "__main__":

    run_fmu_native('E10_linear.fmu', [1], with_plots=True)

谢谢!!!

4

2 回答 2

2

我认为 FMU 导出的定向导数的实现在大多数平台上都没有经过很好的测试。JModelica 最近在这方面取得了很大进展。您可以尝试使用提供它们的 JModelica 生成 FMU(它有一个编译器标志)。我们对此进行了测试,它适用于我们的大多数测试。

于 2018-06-06T14:45:56.383 回答
1

OpenModelica 中有一个错误,现在已修复。在下一个版本 1.13 中,方向导数应该再次正常工作。

于 2018-07-05T07:54:33.200 回答