Propagating packages using inner/outer
Asked Answered
H

1

6

I would like to place a "System" component in my simulation (similar to Modelica.Fluid.System and Modelica.Mechanics.MultiBody.World) from which all other components can access the Medium package, in order to set the working fluid only once for the entire flowsheet. My System is defined as follows:

model System  annotation(defaultAttributes="inner");
    replaceable package Medium = Modelica.Media.Interfaces.PartialMedium 
        annotation(choicesAllMatching=true);
    parameter Modelica.SIunits.Temperature T_amb=293.15;
    // ...
equation
    // empty
end System;

I have referenced the System in other components using outer System system;, and I can thus access all variables/parameters contained therein, e.g. system.T_amb. However, trying to pull the Medium package like this does not work:

model MixingVolume
    outer System system;
    package Medium = system.Medium;
    // ...
equation
    // ...
end MixingVolume;

I get a message saying the base class "system.Medium" is missing. (This is re-translated from an extremely poor German translation within CATIA V6's Modelica environment that I am doomed to use - perhaps the original message would have been more informative.) What am I doing wrong? I am puzzled because this...

model MixingVolume
    outer System system;
    Constant Integer nXi = system.Medium.nXi; // number of independent mass fractions
    // ...
equation
    // ...
end MixingVolume;

...works fine, so MixingVolume does see the system.Medium component. Any clues? Many thanks for any help.

Hulton answered 13/6, 2014 at 14:21 Comment(2)
Dominic, using dynamic scoping in this way is dangerous. At first, it seems like a good idea because it allows you to share a definition among many components in a hierarchy. But it is better to explicitly propagate medium properties because there are many important use cases where you may have two working fluids at the same level (e.g. heat exchangers). This doesn't directly answer your question (which is why I made it a comment), but it is a caution against this kind of approach in the first place.Vaginate
Funny, years later, it turns out I had the exact same question/use case - one shared medium type definition! #71882224Shanney
A
7

You cannot access packages inside components via dot notation.

If the first name is a component reference after dot only a component reference or a function can follow. Read the Modelica Specification: https://www.modelica.org/documents/ModelicaSpec32Revision2.pdf.

It could be a bug in the tool if system.Medium.nXi is allowed.

Ama answered 13/6, 2014 at 16:4 Comment(6)
Thank you, Adrian. I skimmed the Modelica spec, especially chapters 5 (Scoping) and 13 (Packages) before posting (and again just now), but I couldn't find the answer there. It is a very tough read for end-users. Could you point me to the right chapter?Hulton
See 5.3.2 Composite Name Lookup. For a composite name of the form A.B or A.B.C, etc. lookup is performed as follows: • The first identifier (A) is looked up as defined above. • If the first identifier denotes a component, the rest of the name (e.g., B or B.C) is looked up among the declared named component elements of the component.Ama
Thanks again. So the clue here is that a package is not a component?Hulton
For me is very clear that a package is a class so is not a component. However, for people not familiar with programming languages might not be so evident.Ama
The definition of "component" says "basically a variable or an instance of a class", so that is probable more helpful for folks not intimately familiar with Modelica. A "package" would be something like a "composite type" in other languages.Shanney
It's a pity that propagating classes like that is not allowed by Modelica (especially if you work with Medium) :-/Shanney

© 2022 - 2024 — McMap. All rights reserved.