Determine the number of a specific component in a Modelica model at translation (using Dymola)

Viewed 96

How could you determine the number of instances of a given class within this model at translation. This size is to be used to set the size of a vector.

model ModelToBeCounted
end ModelToBeCounted;
  
model Example
  ModelToBeCounted A;
  ModelToBeCounted B;
  ModelToBeCounted C;
end Example;

So in this example I would like to like to determine the number instances of ModelToBeCounted. These instances could also be within instances of other classes. The code at the top level could be altered to do this and the ModelToBeCounted could also be altered.

I tried using external objects however I could not find a way to ensure that the external objects are all created before the function used to report the total number of external objects was run.

Any ideas?

Thanks

2 Answers

If you can modify the model ModelToBeCounted you can do this using inner/outer:

  connector C
    Real totalNum;
    flow Real addNum;
  end C;

  model InnerObjectCounter
    C c;
  equation 
    c.totalNum+c.addNum=0 "Correct sign";
    annotation(missingInnerMessage="Add inner component to count objects");
  end InnerObjectCounter;

  model FlowSource
    C c;
  equation 
    c.addNum=1;
  end FlowSource;


  model AddCounter
    outer InnerObjectCounter objectCounter;
    final Integer totalNum=integer(flowSource.c.totalNum);
    FlowSource flowSource;
  equation 
    connect(flowSource.c, objectCounter.c);
  end AddCounter;

  model ModelToBeCounted
    AddCounter c;
  end ModelToBeCounted;

All components are automatically connected to the inner object using inner/outer-mechanism, and they have a flow of 1 that is summed together.

If you cannot modify the model ModelToBeCounted, and you are willing to use non-standarized methods you can set the flag:

Hidden.AllowAutomaticInstanceOf=true;

and then use:

final parameter Integer m=sum(1 for m in P.ModelToBeCounted);

From: https://github.com/modelica/ModelicaSpecification/blob/MCP/0021/RationaleMCP/0021/

The ModelManagement library can do that.

In fact, there is exactly what you need available as a function:

ModelManagement.Structure.Instantiated.CountUsedModels("Example", {"ModelToBeCounted"});
= {3}

The library is installed with Dymola and available with the standard license. You just have to open it from the Libraries menu.

A problem could be, that this function has to translate your model. I am not sure how well this will work for your requirement:

This size is to be used to set the size of a vector.

Related