modelicadymolaredeclare

pleaceable function with replaceable parameter lead to warning in Dymola


I'm working with several sets of different hardware data stored in constant records. Sets share some common data which are consewuently common in the records. In order to simplify the usage by user, we have created, for each set of hardware, enumeration of record, so the user can choose the hardware from the list restricted to the set in the user interface. This also allows us to directly get the proper record by using a vector of record and an enumration to get the record given the enumeration.

For each set, we have created "get" functions to access specific value of the record. finally, we have defined a partial model that is not dependent of the set, and call the "get" function. Then whe have defined a set of model extending from the partial model and implementing the specificities for each speicifc set. The extend specifies which function and hardware the partial model is to use.

I get «Redeclaration requires a subtype» when checking with Dymola. This comes from the fact the enumration list from which the specific hardawre is used differ from the original one declared in the partial model.

to make it practicable here is a simplified example to reproduce the issue. It might have made it event simplifier, but I expect that they are other means to globaly code it.

My question are:

Here's the simplified code:

package Experiments2
record DataBasic
  constant Real a;
end DataBasic;
// define Data Sets ; "catalogue using «enumeration» can be easly selected by end-used in Dymola Interface"
constant DataBasic dataSetBasic\[BasicCatalogue\]={DataBasic(a=1.1),DataBasic(a=1.2)};
type BasicCatalogue = enumeration(
ItemBasicA "Basic A",
itemBasicB "Basic B");
record DataExtended
extends DataBasic;
constant Real b;
end DataExtended;
constant DataExtended dataSetExtended\[ExtendedCatalogue\]={DataExtended(a=10.1, b=10.2),DataExtended(a=10.3, b=10.4),DataExtended(a=10.5, b=10.6)};
type ExtendedCatalogue = enumeration(
ItemExtA "Ext A",
ItemExtB "Ext B",
ItemExtC "Ext C");
//get property function
function getPropertyBasic
input BasicCatalogue item;
output Real y;
algorithm
y := dataSetBasic\[item\].a;
end getPropertyBasic;

function getPropertyExtended
input ExtendedCatalogue item;
output Real y;
algorithm
y := dataSetExtended\[item\].a;
end getPropertyExtended;
//let's use this Data
//first define a generic model that uses the data
partial model PR
replaceable parameter BasicCatalogue item;
replaceable function getProperty=getPropertyBasic;
final parameter Real prop=getProperty(item);
end PR;

model R1
extends PR(redeclare final parameter BasicCatalogue item=item2, redeclare function  getProperty=getPropertyBasic);
parameter BasicCatalogue item2 annotation(choicesAllMatching=true);

end R1;

model R2
extends PR(redeclare final parameter ExtendedCatalogue item=item2, redeclare function  getProperty=getPropertyExtended);
parameter ExtendedCatalogue item2 annotation(choicesAllMatching=true);
end R2;

model Use
R1 r1_1(item2 = Experiments2.BasicCatalogue.itemBasicB)   annotation (Placement(transformation(extent={{-86,50},{-66,70}})));
R2 r2_1(item2 = Experiments2.ExtendedCatalogue.ItemExtB)  annotation (Placement(transformation(extent={{-44,-16},{-24,4}})));
annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(coordinateSystem(preserveAspectRatio=false)));
end Use;
end Experiments2;

Solution

  • The warning is somewhat important. In most cases one would expect the enumeration to actually be used in the model. A typical problem would be:

    partial model PR
      replaceable parameter BasicCatalogue item;
      replaceable function getProperty=getPropertyBasic;
      final parameter Real prop=getProperty(item);
      final parameter Boolean b=item==BasicCatalogue.ItemBasicA "New! Failing";
    end PR;
    

    It is possible to avoid this, by having the enumeration as a replaceable type instead, something like:

    package Experiments2
    record DataBasic
      constant Real a;
    end DataBasic;
    // define Data Sets ; "catalogue using «enumeration» can be easly selected by end-used in Dymola Interface"
    constant DataBasic dataSetBasic[BasicCatalogue]={DataBasic(a=1.1),DataBasic(a=1.2)};
    type BasicCatalogue = enumeration(
    ItemBasicA "Basic A",
    itemBasicB "Basic B");
    record DataExtended
    extends DataBasic;
    constant Real b;
    end DataExtended;
    constant DataExtended dataSetExtended[ExtendedCatalogue]={DataExtended(a=10.1, b=10.2),DataExtended(a=10.3, b=10.4),DataExtended(a=10.5, b=10.6)};
    type ExtendedCatalogue = enumeration(
    ItemExtA "Ext A",
    ItemExtB "Ext B",
    ItemExtC "Ext C");
    //get property function
    function getPropertyBasic
    input BasicCatalogue item;
    output Real y;
    algorithm 
    y := dataSetBasic[item].a;
    end getPropertyBasic;
    
    type T=enumeration(:);
    partial function getPropertyBase
    input  T item;
    output Real y;
    end getPropertyBase;
    
    function getPropertyExtended
    input ExtendedCatalogue item;
    output Real y;
    algorithm 
    y := dataSetExtended[item].a;
    end getPropertyExtended;
    //let's use this Data
    //first define a generic model that uses the data
    partial model PR
      replaceable type Cat=T;
      parameter Cat item;
    replaceable function getProperty=getPropertyBase;
    final parameter Real prop=getProperty(item);
    end PR;
    
    model R1
      extends PR(redeclare type Cat=BasicCatalogue, final item=item2, redeclare function  getProperty=getPropertyBasic);
      parameter BasicCatalogue item2  annotation(choicesAllMatching=true);
    end R1;
    
    model R2
      extends PR(redeclare type Cat=ExtendedCatalogue, final item=item2, redeclare function  getProperty=getPropertyExtended);
      parameter ExtendedCatalogue item2  annotation(choicesAllMatching=true);
    end R2;
    
    model Use
    R1 r1_1(item2 = Experiments2.BasicCatalogue.itemBasicB)   annotation (Placement(transformation(extent={{-86,50},{-66,70}})));
    R2 r2_1(item2 = Experiments2.ExtendedCatalogue.ItemExtB)  annotation (Placement(transformation(extent={{-44,-16},{-24,4}})));
    annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(coordinateSystem(preserveAspectRatio=false)));
    end Use;
    end Experiments2;
    

    For some reason the item2 is still needed in current Dymola to get the drop-down menus.