方向导数误差(用于模型交换的FMU)

时间:2018-06-05 07:35:05

标签: python openmodelica fmi

我使用FMPy和PyFMI的OpenModelica生成的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)

谢谢!

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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