Search code examples
nestedmodelica

Extend a class from a nested replaceable class in Modelica


I wonder if it's Modelica conform to extend a model from a nested replaceable model, i.e. in the particular example:

package ReplaceableBaseClass
  model ExampleUseReplaceable
    ModelWithReplaceableExtend replaceableExtend1(
      redeclare model LocalModelBase = Extend1,
      input1 = time)
      "Instance with one input and one output";
    ModelWithReplaceableExtend replaceableExtend2(
      redeclare model LocalModelBase = Extend2,
      input1 = Modelica.Math.sin(4*time + 0.3))
      "Instance with one input and two output";
  end ExampleUseReplaceable;

  model ModelWithReplaceableExtend
    "Model which extends from its nested replaceable class"
    extends LocalModelBase;
    replaceable model LocalModelBase = Extend1
      constrainedby PartialToBeExtended;
  end ModelWithReplaceableExtend;

  partial model PartialToBeExtended
    input Real input1;
    output Real output1;
  end PartialToBeExtended;

  model Extend1
    extends PartialToBeExtended;
  equation 
    output1 = 3*input1;
  end Extend1;

  model Extend2
    extends PartialToBeExtended;
    output Real output2;
  equation 
    output1 = input1 + 0.2;
    output2 = input1 * input1;
  end Extend2;
end ReplaceableBaseClass;

This in fact works in Dymola but I have a strange feeling about it and not sure if it's really good idea to use it.


Solution

  • That is not legal Modelica (since Modelica 3.0), and Dymola will generate a diagnostic (but just a warning) since Dymola 2017.

    The restriction in Modelica is that a base-class must be transitively non-replaceable (section 7.1.4 and 6.2.1).