ghorwin / FMICodeGenerator

A Code Generator that creates native C/C++ code to build Functional Mock-Up Units (FMU) with support for FMI version 2.0 (rollback)
BSD 3-Clause "New" or "Revised" License
51 stars 25 forks source link

Not possible to open results file: No such file or directory #37

Closed ilyaelizarov closed 2 years ago

ilyaelizarov commented 3 years ago

Hi guys!

I generated an FMU from P_Controller tutorial, and tried to use it in Dymola, but the following error appears: "Not possible to open file "Unnamed2.mat": No such file or directory".

Unnamed2.mat is an output file for storing simulation results in Dymola.

The log is: [fmi2OK]: P_Control_fmu: fmi2SetupExperiment: Call of setup experiment. [fmi2OK]: P_Control_fmu: fmi2EnterInitializationMode: Go into initialization mode. [fmi2OK]: P_Control_fmu: Starting initialization. [fmi2OK]: P_Control_fmu: Initialization complete. [fmi2OK]: P_Control_fmu: fmi2ExitInitializationMode: Go out from initialization mode. Simulation successfully terminated Terminate signaled by FMU Error: Terminate command at starting point. [fmi2OK]: P_Control_fmu: fmi2Terminate: Terminate model. [fmi2OK]: P_Control_fmu: fmi2FreeInstance: Model instance deleted.

At the same time, I could run fmuCheck on this FMU without any errors. It seems that OpenModelica and SimulationX work with this FMU well too.

It seems that only co-simulation works in Dymola. Model exchange fails.

What could it be?

ghorwin commented 3 years ago

Hi, this is likely a Dymola model-related feature. Custom mat files do not have anything to do with FMUs (unless you try to read this file from within the FMU). My guess is that the error message by Dymola is rather a warning and is unrelated to the actual FMU termination problem.

The interesting question is: where does the "Terminate signaled by FMU" error comes from? The P_Control_fmu appears to run through the initialization successfully (also, fmuCheck runs a full co-simulation, so that shouldn't be an issue).

From your observation about "co-sim works, model-exchange fails" I'd look at the ModelExchange code in your FMU. Maybe you could share it here and we look at potential out-of-index errors or exceptions.

ilyaelizarov commented 3 years ago

Hi,

Yes, you're right. The problem is that the FMU fails right after the initialization. Dymola is not able to open .mat file since there is no output results in model exchange mode.

Here is my code:

// model exchange: implementation of derivative and output update
void P_Control_final::updateIfModified() {
    if (!m_externalInputVarsModified)
        return;

    // get input variables
    double T = m_realVar[FMI_INPUT_T];
    double Tlimit = m_realVar[FMI_PARA_Tlimit];
    double kP = m_realVar[FMI_PARA_kP];

    // TODO : implement your code here
    double P = 0;
    if (T < Tlimit)
        P = kP*(Tlimit - T);

    // output variables
    m_realVar[FMI_OUTPUT_P] = P; // TODO : store your results here

    // reset externalInputVarsModified flag
    m_externalInputVarsModified = false;
}

and

// Co-simulation: time integration
void P_Control_final::integrateTo(double tCommunicationIntervalEnd) {

    // state of FMU before integration:
    //   m_currentTimePoint = t_IntervalStart;

    // get input variables
    // double T = m_realVar[FMI_INPUT_T];
    // double Tlimit = m_realVar[FMI_PARA_Tlimit];
    // double kP = m_realVar[FMI_PARA_kP];

    // TODO : implement your code here
    updateIfModified();

    // output variables
    //m_realVar[FMI_OUTPUT_P] = 0; // TODO : store your results here

    m_currentTimePoint = tCommunicationIntervalEnd;

    // state of FMU after integration:
    //   m_currentTimePoint = tCommunicationIntervalEnd;
}

It is exactly the same as in your tutorial for P_Control.FMU.

I was able to get some details for debugging if it helps:

[fmi2OK]: P_Control_final_fmu: fmi2SetupExperiment: Call of setup experiment. [fmi2OK]: P_Control_final_fmu: fmi2EnterInitializationMode: Go into initialization mode. [fmi2OK]: P_Control_final_fmu: Starting initialization. [fmi2OK]: P_Control_final_fmu: Initialization complete. Variable p_Control_final_fmu._first = 0 at time 0 Variable p_Control_final_fmu._startValuesNotSet = 0 at time 0 Expression P_Control_final_fmu.fmi_Functions.CheckIfDiscreteValueSet( p_Control_final_fmu.fmi, p_Control_final_fmu.myTime, {const.k}, fill(false, 0), fill(0, 0), {1}, fill(0, 0), fill(0, 0), {p_Control_final_fmu.P}, fill(false, 0), fill(0, 0)) > 0.5 became true ( (P_Control_final_fmu.fmi_Functions.CheckIfDiscreteValueSet( p_Control_final_fmu.fmi, p_Control_final_fmu.myTime, {const.k}, fill(false, 0), fill(0, 0), {1}, fill(0, 0), fill(0, 0), {p_Control_final_fmu.P}, fill(false, 0), fill(0, 0)))-(0.5) = 0.5 ) Expression P_Control_final_fmu.fmi_Functions.fmiCompletedIntegratorStep( p_Control_final_fmu.fmi, p_Control_final_fmu.myTime) > 0.5 became false ( (P_Control_final_fmu.fmi_Functions.fmiCompletedIntegratorStep( p_Control_final_fmu.fmi, p_Control_final_fmu.myTime))-(0.5) = -0.5 ) Iterating to find consistent restart conditions. Variable p_Control_final_fmu._first = 0 at time 0 Variable p_Control_final_fmu._startValuesNotSet = 0 at time 0 Variable p_Control_final_fmu.fmi_flip = 1 at time 0 [fmi2OK]: P_Control_final_fmu: fmi2ExitInitializationMode: Go out from initialization mode. Simulation successfully terminated Terminate signaled by FMU Variable p_Control_final_fmu.fmi_flip = 0 at time 0 Variable p_Control_final_fmu.fmi_iterationConverged = 1 at time 0 Error: Terminate command at starting point. [fmi2OK]: P_Control_final_fmu: fmi2Terminate: Terminate model. [fmi2OK]: P_Control_final_fmu: fmi2FreeInstance: Model instance deleted.

The same log, but for co-simulation mode

Log-file of program ./dymosim (generated: Fri Jul 30 16:14:51 2021)

dymosim started ... "dsin.txt" loading (dymosim input file) [fmi2OK]: P_Control_final_fmu: fmi2SetupExperiment: Call of setup experiment. [fmi2OK]: P_Control_final_fmu: fmi2EnterInitializationMode: Go into initialization mode. [fmi2OK]: P_Control_final_fmu: Starting initialization. [fmi2OK]: P_Control_final_fmu: Initialization complete. Iterating to find consistent restart conditions. Sample event (sample(0,0.02)) at time: 0 [fmi2OK]: P_Control_final_fmu: fmi2ExitInitializationMode: Go out from initialization mode. Variable p_Control_final_fmu.fmi_exitInit = 1 at time 0 Iterating to find consistent restart conditions. during event at Time : 0 ... "Unnamed.mat" creating (simulation result file)

Integration started at T = 0 using integration method DASSL (DAE multi-step solver (dassl/dasslrt of Petzold modified by Dassault Systemes)) Sample event (sample(0,0.02)) at time: 0.02 Variable p_Control_final_fmu.fmi_StepOK = 1 at time 0.02 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.02 Iterating to find consistent restart conditions. Variable p_Control_final_fmu._T_old = 280 at time 0.02 Iterating to find consistent restart conditions. during event at Time : 0.02 Sample event (sample(0,0.02)) at time: 0.04 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.04 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.04 Sample event (sample(0,0.02)) at time: 0.06 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.06 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.06 Sample event (sample(0,0.02)) at time: 0.08 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.08 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.08 Sample event (sample(0,0.02)) at time: 0.1 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.1 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.1 Sample event (sample(0,0.02)) at time: 0.12 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.12 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.12 Sample event (sample(0,0.02)) at time: 0.14 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.14 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.14 Sample event (sample(0,0.02)) at time: 0.16 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.16 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.16 Sample event (sample(0,0.02)) at time: 0.18 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.18 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.18 Sample event (sample(0,0.02)) at time: 0.2 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.2 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.2 Sample event (sample(0,0.02)) at time: 0.22 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.22 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.22 Sample event (sample(0,0.02)) at time: 0.24 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.24 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.24 Sample event (sample(0,0.02)) at time: 0.26 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.26 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.26 Sample event (sample(0,0.02)) at time: 0.28 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.28 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.28 Sample event (sample(0,0.02)) at time: 0.3 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.3 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.3 Sample event (sample(0,0.02)) at time: 0.32 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.32 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.32 Sample event (sample(0,0.02)) at time: 0.34 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.34 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.34 Sample event (sample(0,0.02)) at time: 0.36 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.36 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.36 Sample event (sample(0,0.02)) at time: 0.38 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.38 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.38 Sample event (sample(0,0.02)) at time: 0.4 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.4 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.4 Sample event (sample(0,0.02)) at time: 0.42 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.42 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.42 Sample event (sample(0,0.02)) at time: 0.44 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.44 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.44 Sample event (sample(0,0.02)) at time: 0.46 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.46 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.46 Sample event (sample(0,0.02)) at time: 0.48 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.48 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.48 Sample event (sample(0,0.02)) at time: 0.5 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.5 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.5 Sample event (sample(0,0.02)) at time: 0.52 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.52 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.52 Sample event (sample(0,0.02)) at time: 0.54 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.54 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.54 Sample event (sample(0,0.02)) at time: 0.5600000000000001 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.56 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.5600000000000001 Sample event (sample(0,0.02)) at time: 0.58 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.58 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.58 Sample event (sample(0,0.02)) at time: 0.6 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.6 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.6 Sample event (sample(0,0.02)) at time: 0.62 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.62 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.62 Sample event (sample(0,0.02)) at time: 0.64 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.64 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.64 Sample event (sample(0,0.02)) at time: 0.66 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.66 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.66 Sample event (sample(0,0.02)) at time: 0.68 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.68 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.68 Sample event (sample(0,0.02)) at time: 0.7000000000000001 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.7 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.7000000000000001 Sample event (sample(0,0.02)) at time: 0.72 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.72 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.72 Sample event (sample(0,0.02)) at time: 0.74 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.74 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.74 Sample event (sample(0,0.02)) at time: 0.76 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.76 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.76 Sample event (sample(0,0.02)) at time: 0.78 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.78 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.78 Sample event (sample(0,0.02)) at time: 0.8 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.8 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.8 Sample event (sample(0,0.02)) at time: 0.8200000000000001 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.82 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.8200000000000001 Sample event (sample(0,0.02)) at time: 0.84 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.84 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.84 Sample event (sample(0,0.02)) at time: 0.86 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.86 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.86 Sample event (sample(0,0.02)) at time: 0.88 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.88 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.88 Sample event (sample(0,0.02)) at time: 0.9 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.9 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.9 Sample event (sample(0,0.02)) at time: 0.92 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.92 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.92 Sample event (sample(0,0.02)) at time: 0.9400000000000001 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.94 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.9400000000000001 Sample event (sample(0,0.02)) at time: 0.96 Variable p_Control_final_fmu.fmi_flip = 0 at time 0.96 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.96 Sample event (sample(0,0.02)) at time: 0.98 Variable p_Control_final_fmu.fmi_flip = 1 at time 0.98 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 0.98 Integration terminated successfully at T = 1 WARNING: You have many time events. This is probably due to fast sampling. Enable logging of event in Simulation/Setup/Debug/Events during simulation CPU-time for integration : 0.04 seconds CPU-time for one grid interval : 0.08 milliseconds CPU-time for initialization : 0.001 seconds Number of result points : 551 Number of grid points : 501 Number of accepted steps : 22 Number of f-evaluations (dynamics) : 22 Number of Jacobian-evaluations : 21 Number of model time events : 50 Number of input time events : 0 Number of state events : 0 Number of step events : 0 Minimum integration stepsize : 2e-06 Maximum integration stepsize : 0.463 Maximum integration order : 1 Calling terminal section Sample event (sample(0,0.02)) at time: 1 Variable p_Control_final_fmu.fmi_flip = 0 at time 1 Iterating to find consistent restart conditions. Iterating to find consistent restart conditions. during event at Time : 1 ... "dsfinal.txt" creating (final states) [fmi2OK]: P_Control_final_fmu: fmi2Terminate: Terminate model. [fmi2OK]: P_Control_final_fmu: fmi2FreeInstance: Model instance deleted.

My Dymola version is 2020x (64-bit), 2019-10-10

ghorwin commented 3 years ago

Ok, from the output of Integration terminated successfully at T = 1 it looks as if the Co-simulation completed successfully. So FMU co-simulation seems to work ok.

However, the performance warning indicates that Dymola does a lot of sampling. This has to be resolved by adjusting Dymola simulation/solver parameters. Alas, I cannot help here, since I do not use Dymola myself (maybe ask Dymola support?).

ghorwin commented 3 years ago

As for the modelExchange, I have testet the code generator-modelExchange code only with SimulationX, but I'll try to run the FMU through PyFMI. Stay tuned....

ghorwin commented 2 years ago

No further insight into that matter... closing issue since I cannot reproduce this problem myself and I must assume, this is a Dymola-Issue