runtime-errormodelicaopenmodelicasystemmodeler

Can a parameter be used to set the unit attribute for a component?


So far, using Wolfram System Modeler 4.3 and 5.1 the following minimal example would compile without errors:

model UnitErrorModel
    MyComponent c( hasUnit = "myUnit" );

    block MyComponent
        parameter String hasUnit = "1";
        output Real y( unit = hasUnit );
    equation
        y = 10;
    end MyComponent;

end UnitErrorModel;

But with the new release of WSM 12.0 (the jump in version is due to an alignment with the current release of Wolfram's flagship Mathematica) I am getting an error message:

Internal error: Codegen.getValueString: Non-constant expression:c.hasUnit

(Note: The error is given by WSMLink'WSMSimulate in Mathematica 12.0 which is running System Modeler 12.0 internally; here asking for the "InternalValues" property of the above model since I have not installed WSM 12.0 right now).

Trying to simulate the above model in OpenModelica [OMEdit v. 1.13.2 (64-bit)] reveals:

SimCodeUtil.mo: 8492:9-8492:218]: Internal error Unexpected expression (should have been handled earlier, probably in the front-end. Unit/displayUnit expression is not a string literal: c.hasUnit

So it seems that to set the unit attribute I cannot make use of a variable that has parameter variability? Why is this - after all shouldn't it suffice that the compiler can hard-wire the unit when compiling for runtime (after all the given model will run without any error in WSM 4.3 and 5.1)?

EDIT: From the answer to an older question of mine I had believed that at least final parameters might be used to set the unit-attribute. Making the modification final (e.g. c( final hasUnit = "myUnit" ) does not resolve the issue.


Solution

  • I have been given feedback on Wolfram Community by someone from Wolfram MathCore regarding this issue:

    You are correct in that it's not in violation with the specification, although making it a constant makes more sense since you would invalidate all your static unit checking if you are allowed to change the unit after building the simulation. We filed an issue on the specification regarding this (Modelica Specification Issue # 2362).

    So, MatheCore is a bit ahead of the game in proposing a Modelica specification change that they have already implemented. ;-)

    Note: That in Wolfram System Modeler (12.0) using the annotation Evaluate = true will not cure the problem (cf. the comment above by @matth).

    As a workaround variables used to set the unit attribute should have constant variability, but can nevertheless by included in user dialogs to be interactively changed using annotation(Dialog(group = "GroupName")).