Search code examples
modelicadymola

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


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


Solution

  • 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/