Skip to content

Commit

Permalink
Improve handling of augmented flow variables (#12215)
Browse files Browse the repository at this point in the history
- Generate default connect equations for unconnected outside flow
  variables that were added during augmentation of expandable
  connectors.

Fixes #12159
  • Loading branch information
perost committed Apr 9, 2024
1 parent f391abb commit b77f6ab
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 3 deletions.
13 changes: 13 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFAttributes.mo
Expand Up @@ -106,6 +106,19 @@ public
Replaceable.NOT_REPLACEABLE()
);

constant Attributes AUGMENTED_ATTR =
ATTRIBUTES(
ConnectorType.AUGMENTED,
Parallelism.NON_PARALLEL,
Variability.CONTINUOUS,
Direction.NONE,
InnerOuter.NOT_INNER_OUTER,
false,
false,
Replaceable.NOT_REPLACEABLE()
);


record ATTRIBUTES
ConnectorType.Type connectorType;
Parallelism parallelism;
Expand Down
14 changes: 12 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFConnections.mo
Expand Up @@ -140,15 +140,25 @@ public
protected
Component comp;
Connector c;
DAE.ElementSource src;
algorithm
// Collect all flow variables.
for var in flatModel.variables loop
comp := InstNode.component(ComponentRef.node(var.name));

if Component.isFlow(comp) then
c := Connector.fromFacedCref(var.name, var.ty,
NFConnector.Face.INSIDE, ElementSource.createElementSource(Component.info(comp)));
// Add all flow variables as inside connectors, to generate default
// equations if they're not connected.
src := ElementSource.createElementSource(Component.info(comp));
c := Connector.fromFacedCref(var.name, var.ty, NFConnector.Face.INSIDE, src);
conns := addFlow(c, conns);

// Also add outside connectors for flow variables that were added during
// augmentation of expandable connectors.
if ConnectorType.isAugmented(var.attributes.connectorType) then
c := Connector.fromFacedCref(var.name, var.ty, NFConnector.Face.OUTSIDE, src);
conns := addFlow(c, conns);
end if;
end if;
end for;
end collectFlows;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFExpandableConnectors.mo
Expand Up @@ -443,7 +443,7 @@ algorithm
end for;
else
var := Variable.VARIABLE(connectorName, connectorType, NFBinding.EMPTY_BINDING,
Visibility.PUBLIC, NFAttributes.DEFAULT_ATTR, {}, {},
Visibility.PUBLIC, NFAttributes.AUGMENTED_ATTR, {}, {},
SOME(SCode.COMMENT(NONE(), SOME("virtual variable in expandable connector"))),
info, NFBackendExtension.DUMMY_BACKEND_INFO);
vars := var :: vars;
Expand Down
9 changes: 9 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFPrefixes.mo
Expand Up @@ -46,6 +46,7 @@ package ConnectorType
constant Type VIRTUAL = intBitLShift(1, 4) "A virtual connector used in a connection.";
constant Type CONNECTOR = intBitLShift(1, 5) "A non-expandable connector that contains elements.";
constant Type EXPANDABLE = intBitLShift(1, 6) "An expandable connector.";
constant Type AUGMENTED = intBitLShift(1, 7) "A variable added during augmentation of an expandable connector.";

// flow/stream
constant Type FLOW_STREAM_MASK = intBitOr(FLOW, STREAM);
Expand Down Expand Up @@ -217,6 +218,14 @@ package ConnectorType
annotation(__OpenModelica_EarlyInline = true);
end setPresent;

function isAugmented
input Type cty;
output Boolean augmented;
algorithm
augmented := intBitAnd(cty, AUGMENTED) > 0;
annotation(__OpenModelica_EarlyInline = true);
end isAugmented;

function toString
input Type cty;
output String str;
Expand Down
119 changes: 119 additions & 0 deletions testsuite/flattening/modelica/scodeinst/ExpandableConnector13.mo
@@ -0,0 +1,119 @@
// name: ExpandableConnector13
// keywords: expandable connector
// status: correct
// cflags: -d=newInst
//
// Checks that potentially present non-connector variables in an expandable
// connector doesn't generate warnings about unbalanced connectors.
//

connector Pin
Real v;
flow Real i;
end Pin;

model Ground
Pin p;
equation
p.v = 0;
end Ground;

expandable connector C
end C;

model ModelA
C c;
Ground ground1;
Ground ground2;
Ground ground3;
equation
connect(ground1.p, c.p1);
connect(ground2.p, c.p2);
connect(ground3.p, c.p3);
end ModelA;

model ModelB
C c;
Ground ground1;
equation
connect(ground1.p, c.p1);
end ModelB;

model ExpandableConnector13
ModelA m1;
ModelA m2;
ModelB m3;
equation
connect(m1.c, m2.c);
connect(m1.c, m3.c);
end ExpandableConnector13;

// Result:
// class ExpandableConnector13
// Real m1.c.p1.i "virtual variable in expandable connector";
// Real m1.c.p1.v "virtual variable in expandable connector";
// Real m1.c.p2.i "virtual variable in expandable connector";
// Real m1.c.p2.v "virtual variable in expandable connector";
// Real m1.c.p3.i "virtual variable in expandable connector";
// Real m1.c.p3.v "virtual variable in expandable connector";
// Real m2.c.p1.i "virtual variable in expandable connector";
// Real m2.c.p1.v "virtual variable in expandable connector";
// Real m2.c.p2.i "virtual variable in expandable connector";
// Real m2.c.p2.v "virtual variable in expandable connector";
// Real m2.c.p3.i "virtual variable in expandable connector";
// Real m2.c.p3.v "virtual variable in expandable connector";
// Real m3.c.p1.i "virtual variable in expandable connector";
// Real m3.c.p1.v "virtual variable in expandable connector";
// Real m3.c.p2.i "virtual variable in expandable connector";
// Real m3.c.p2.v "virtual variable in expandable connector";
// Real m3.c.p3.i "virtual variable in expandable connector";
// Real m3.c.p3.v "virtual variable in expandable connector";
// Real m1.ground1.p.v;
// Real m1.ground1.p.i;
// Real m1.ground2.p.v;
// Real m1.ground2.p.i;
// Real m1.ground3.p.v;
// Real m1.ground3.p.i;
// Real m2.ground1.p.v;
// Real m2.ground1.p.i;
// Real m2.ground2.p.v;
// Real m2.ground2.p.i;
// Real m2.ground3.p.v;
// Real m2.ground3.p.i;
// Real m3.ground1.p.v;
// Real m3.ground1.p.i;
// equation
// m1.ground1.p.v = m1.c.p1.v;
// m1.ground2.p.v = m1.c.p2.v;
// m1.ground3.p.v = m1.c.p3.v;
// m2.ground1.p.v = m2.c.p1.v;
// m2.ground2.p.v = m2.c.p2.v;
// m2.ground3.p.v = m2.c.p3.v;
// m3.ground1.p.v = m3.c.p1.v;
// m1.c.p1.v = m3.c.p1.v;
// m1.c.p1.v = m2.c.p1.v;
// m1.c.p2.v = m3.c.p2.v;
// m1.c.p2.v = m2.c.p2.v;
// m1.c.p3.v = m3.c.p3.v;
// m1.c.p3.v = m2.c.p3.v;
// m3.c.p1.i + m2.c.p1.i + m1.c.p1.i = 0.0;
// m3.c.p2.i + m2.c.p2.i + m1.c.p2.i = 0.0;
// m3.c.p3.i + m2.c.p3.i + m1.c.p3.i = 0.0;
// m3.c.p2.i = 0.0;
// m3.c.p3.i = 0.0;
// m1.ground1.p.i - m1.c.p1.i = 0.0;
// m1.ground2.p.i - m1.c.p2.i = 0.0;
// m1.ground3.p.i - m1.c.p3.i = 0.0;
// m2.ground1.p.i - m2.c.p1.i = 0.0;
// m2.ground2.p.i - m2.c.p2.i = 0.0;
// m2.ground3.p.i - m2.c.p3.i = 0.0;
// m3.ground1.p.i - m3.c.p1.i = 0.0;
// m1.ground1.p.v = 0.0;
// m1.ground2.p.v = 0.0;
// m1.ground3.p.v = 0.0;
// m2.ground1.p.v = 0.0;
// m2.ground2.p.v = 0.0;
// m2.ground3.p.v = 0.0;
// m3.ground1.p.v = 0.0;
// end ExpandableConnector13;
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -450,6 +450,7 @@ ExpandableConnector9.mo \
ExpandableConnector10.mo \
ExpandableConnector11.mo \
ExpandableConnector12.mo \
ExpandableConnector13.mo \
ExpandableConnectorComplex1.mo \
ExpandableConnectorFlow1.mo \
ExpandableConnectorFlow2.mo \
Expand Down

0 comments on commit b77f6ab

Please sign in to comment.