1

I got the following, quiet fundamental, problem with JModelica. Consider a rather simple model:

model TEST
  Real Dum(start = 0);
  parameter Integer It = 5;
algorithm
  Dum := 0;
  for i in 1:It loop
    Dum := Dum + 1;
  end for;
end TEST;

I compile that with python and it runs, of course, and the variable Dum has always the value 5 as it is supposed to be. The problem is now that when I want to change to parameter "It" after loading the compiled fmu ( test.fmu ) with:

 test = load_fmu("test.fmu");
 test.set("It",4);

and then run the simulation, it still gives 5 for "Dum" and not 4 as it should. Although "It" gets changed to 4 actually as can be seen by plotting the time-curve of "It" after simulation. Does anybody know if there is a way to change It on the fly so that it actually effects the loop?

This is of course a toy example, but I need that functionality for a much bigger project where I desperately need to loop over a varying range and where re-compiling is just to time consuming.

Thanks so much

1 Answers1

2

The problem is that the compiler treats the parameter "It" as a structural parameter, i.e. it cannot be changed after compilation unfortunately. Running the same problem with the latest development version of JModelica (r9190) results in an exception when trying to set the variable "It"

In [9]: test.set("It", 6)
---------------------------------------------------------------------------
FMUException                              Traceback (most recent call last)
<ipython-input-9-b5b436a55add> in <module>()
----> 1 model.set("It", 6)

src\pyfmi\fmi.pyx in pyfmi.fmi.ModelBase.set (src\pyfmi\fmi.c:4648)()

src\pyfmi\fmi.pyx in pyfmi.fmi.FMUModelBase._set (src\pyfmi\fmi.c:17714)()

src\pyfmi\fmi.pyx in pyfmi.fmi.FMUModelBase.set_integer (src\pyfmi\fmi.c:15398)()

FMUException: Failed to set the Integer values.

If you check the log you'll see why it failed:

In [10]: test.get_log()
Out[10]: ['FMIL: module = Model, log level = 2: [ERROR][FMU status:Error]    <CannotSetVariable category="error">Cannot set Integer structural parameter <value name="variable">"It"</value></CannotSetVariable>']

So to answer your question, it cannot be done unfortunately.

You can though change the parameter during the compilation call:

from pymodelica import compile_fmu
name = compile_fmu("TEST(It=6)", "test.mo")
Christian Winther
  • 1,113
  • 1
  • 9
  • 17