modelicaopenmodelicagraphical-interaction

Use of prefix constant or parameter for structural choices?


I try to make Modelica library code that works for different platforms.

The use of the prefix "constant" I understand as protecting a parameter from being changed by user interaction AFTER compilation. If you want to allow changes you use the prefix "parameter" instead.

Structural parameters, e.g. number of inlets to a Tank, you may naturally declare as

constant integer n_inlets = 1;

When the component tank with n_inlets should be configured you can write the code

Tank tank(n_inlets=0);

if you decide that the tank should not have any inlets. Then after compilation n_inlets is protected to remain 0 and no user interaction will change that.

However, if you like to use GUI to configure the tank, then I think you must declare n_inlets as a parameter, otherwise the interaction menu will not contain n_inlets. This is the case in OpenModelica at least, and I guess it is similar for GUI in other Modelica implementations.

Once the configuration is done, I still would prefer to have n_inlets protected from later user interaction AFTER compilation. Is there a way to write code in that way?


Solution

  • For Modelica 3.7 this has been clarified and you could declare it as an evaluable parameter https://specification.modelica.org/master/class-predefined-types-and-declarations.html#component-variability

    parameter integer n_inlets = 1 annotation(Evaluate=true);
    

    Evaluable parameters should be settable before translation, but not after. (In practice using it for an array size will also force evaluation anyway.)

    Even before Modelica 3.7 that will work, and indicate the intent.

    Added: However, if n_inlets give the size for an array of connectors you should use connectorSizing instead https://specification.modelica.org/master/annotations.html#connector-sizing . That parameter should be automatically updated when connecting and not appear in the parameter dialog.