Search code examples
modelicadymolaopenmodelica

Modelica - Using parameter equations without losing the ability to parameterize after compilation


Often, I will build Modelica models with parameters that are dependent on other parameters:

model ParameterTest
  parameter Real a = 5;
  parameter Real b = a/2;
end ParameterTest;

Here, parameter b is no longer available to be parameterized in the compiled model and I've locked in the b=a/2 relationship.

Alternatively, I could have done the following:

model ParameterTest
  parameter Real a = 5;
  parameter Real b = 5/2;
end ParameterTest;

Here, I've lost some of the traceability and logic for my default value for parameter b and I need to manually maintain/update parameter b if I were to change parameter a in the future. This is a trivial example, but potentially in a more real-world example, the equation set and logic would be more sophisticated to determine parameter b.

While the above behaviors are perfectly logical and expected, what I would often prefer is a 3rd approach that gives me the best of both worlds: specify my default value for parameter b in terms of parameter a, but maintain the ability to change/parameterize b after compilation. Also, to give some motivation here: I would say that this situation/need arises when there isn't a strict relationship between the parameters (a and b in this case), but there is a convenient/typical default relationship.

For example, if an annotation like the following existed, it would be nice:

model ParameterTest
  parameter Real a = 5;
  parameter Real b = a/2 annotation(EvaluateExpression=true); // this would effectively replace "a/2" with "2.5" prior to compilation
end ParameterTest;

Is there any known way to achieve what I'm after? Or any other thoughts/suggestions?


Solution

  • I guess a model like the following would do what you want.

    model ParameterTest
      parameter Real a = 5;
      parameter Real bVal = 0;
      parameter Real b = if evaluateExpression then a/2 else bVal;
      parameter Boolean evaluateExpression = true;
    end ParameterTest;
    

    I added a boolean parameter evaluateExpression that will influence if b get's computed or uses a parameter bVal. In OpenModelica (OMEdit) you can change the value of b by changing evaluateExpression=false and bVal to some value at simulation time.

    OMEdit diagram view of a and b