Search code examples
modelicaopenmodelica

How to avoid Modelica Large Output File


I have an external function that I require to call at most twice during the simulation. It has a large output of size Real[n][m][k] where n, m, and ks product is a relatively large integer. I call the function inside a when statement. The issue I have is that the output of the function is copied over and over in .mat file for each iteration and causes it to be really large even though I use the output infrequently. I wonder if there is a way to store the output only once and avoid storing the copies of the output in the results file for each time step.

P.s. If that is not possible I might try to store the results of the function in another file, but need to use them in the simulation. So as an alternative is there also a way to make the simulation ignore storing a variable in the results file.


Solution

  • I don't think there is a way to store a subset of variables only at certain points in time.

    Select variables

    What can be done is using HideResult to avoid storing them in the result file. They can still be used in the simulation though. As an example:

    model HideRes
      Real y1 annotation(HideResult=true);
      Integer y2;
      parameter Real x1 = 1;
      parameter Real x2 = 2 annotation(HideResult=true);
    equation 
      when time > 0.1 then
        y1 = sin(x1);
      elsewhen time > 0.2 then
        y1 = sin(x2);
      end when;
      y2 = 10*integer(y1);
    
    end HideRes;
    

    In the above code, y1 and x2 will not be shown in the result file.

    Protect variables

    As an alternative to the above (thanks Hans O. for pointing at this) it is also possible to use protected variables. By default, they will not be stored in the result file. Additionally, they will not be accessible from higher levels of hierarchy in the model itself. Also parameters will not show up in the parameter dialog.
    Below is the same example with protected applied (the parameter x2 cannot be modified from the outside).

    model HideRes
      Integer y2;
      parameter Real x1 = 1;
      
      protected
      Real y1;
      parameter Real x2 = 2;
      
    equation 
      when time > 0.1 then
        y1 = sin(x1);
      elsewhen time > 0.2 then
        y1 = sin(x2);
      end when;
      y2 = 10*integer(y1);
    
    end HideRes;
    

    Chose timing

    Some simulators also offer the possibility to store only at defined events. For Dymola, this would e.g. be:

    when x > 0 then
      Dymola.Simulation.TriggerResultSnapshot();
    end when;
    

    Note: it stores all variables, not just a subset.