Skip to content

Commit

Permalink
Improve handling of expandable connectors (#10540)
Browse files Browse the repository at this point in the history
- Split complex components into parts when augmenting an expandable
  connector.
  • Loading branch information
perost committed Apr 13, 2023
1 parent d055608 commit 1e295d1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
1 change: 0 additions & 1 deletion .CI/compliance.failures
Expand Up @@ -44,7 +44,6 @@ ModelicaCompliance.Connections.Declarations.OperatorRecordMissingAddition
ModelicaCompliance.Connections.Declarations.OperatorRecordMissingNegation
ModelicaCompliance.Connections.Declarations.OperatorRecordMissingZero
ModelicaCompliance.Connections.Expandable.AugmentArray
ModelicaCompliance.Connections.Expandable.AugmentComplex
ModelicaCompliance.Connections.Expandable.MissingNonInput
ModelicaCompliance.Connections.Expandable.PotentiallyPresentMod
ModelicaCompliance.Connections.Expandable.UnknownArrayDimensions
Expand Down
34 changes: 27 additions & 7 deletions OMCompiler/Compiler/NFFrontEnd/NFExpandableConnectors.mo
Expand Up @@ -361,7 +361,6 @@ protected
ClassTree cls_tree;
Component comp;
list<InstNode> nodes = {};
Variable var;
Type ty;
ComplexType complex_ty;
algorithm
Expand Down Expand Up @@ -397,12 +396,7 @@ algorithm
nodes := node :: nodes;
ty := c.ty;
elem_name := ComponentRef.prefixCref(node, ty, {}, exp_name);
// TODO: This needs more work, the new connector might be a complex connector.
var := Variable.VARIABLE(elem_name, ty, NFBinding.EMPTY_BINDING,
Visibility.PUBLIC, NFAttributes.DEFAULT_ATTR, {}, {},
SOME(SCode.COMMENT(NONE(), SOME("virtual variable in expandable connector"))),
ElementSource.getInfo(c.source), NFBackendExtension.DUMMY_BACKEND_INFO);
vars := var :: vars;
vars := createVirtualVariables(elem_name, ty, ElementSource.getInfo(c.source), vars);
else
comp_node := InstNode.resolveInner(comp_node);

Expand Down Expand Up @@ -430,6 +424,32 @@ algorithm
InstNode.componentApply(exp_node, Component.setType, ty);
end augmentExpandableConnector;

function createVirtualVariables
input ComponentRef connectorName;
input Type connectorType;
input SourceInfo info;
input output list<Variable> vars;
protected
Variable var;
array<InstNode> comps;
ComponentRef name;
Type ty;
algorithm
if Type.isComplex(connectorType) then
for comp in Type.complexComponents(connectorType) loop
ty := InstNode.getType(comp);
name := ComponentRef.prefixCref(comp, ty, {}, connectorName);
vars := createVirtualVariables(name, ty, info, vars);
end for;
else
var := Variable.VARIABLE(connectorName, connectorType, NFBinding.EMPTY_BINDING,
Visibility.PUBLIC, NFAttributes.DEFAULT_ATTR, {}, {},
SOME(SCode.COMMENT(NONE(), SOME("virtual variable in expandable connector"))),
info, NFBackendExtension.DUMMY_BACKEND_INFO);
vars := var :: vars;
end if;
end createVirtualVariables;

function updateUndeclaredConnection
input Connection conn;
input output list<Connection> conns;
Expand Down
7 changes: 7 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFType.mo
Expand Up @@ -462,6 +462,13 @@ public
COMPLEX(cls = node) := ty;
end complexNode;

function complexComponents
input Type ty;
output array<InstNode> comps;
algorithm
comps := ClassTree.getComponents(Class.classTree(InstNode.getClass(complexNode(ty))));
end complexComponents;

function isConnector
input Type ty;
output Boolean isConnector;
Expand Down
@@ -0,0 +1,46 @@
// name: ExpandableConnectorComplex1
// keywords: expandable connector
// status: correct
// cflags: -d=newInst
//
// Checks that augmenting an expandable connector with a connector works.
//

expandable connector Bus
end Bus;

connector PositivePin
Real v;
flow Real i;
end PositivePin;

model M
parameter Real A = 1;
parameter Real w = 1;
PositivePin a;
equation
a.v = A*sin(w*time);
end M;

model ExpandableConnectorComplex1
Bus bus;
M m;
equation
connect(m.a, bus.a);
end ExpandableConnectorComplex1;

// Result:
// class ExpandableConnectorComplex1
// Real bus.a.i "virtual variable in expandable connector";
// Real bus.a.v "virtual variable in expandable connector";
// parameter Real m.A = 1.0;
// parameter Real m.w = 1.0;
// Real m.a.v;
// Real m.a.i;
// equation
// m.a.v = bus.a.v;
// bus.a.i = 0.0;
// m.a.i - bus.a.i = 0.0;
// m.a.v = m.A * sin(m.w * time);
// end ExpandableConnectorComplex1;
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -425,6 +425,7 @@ ExpandableConnector6.mo \
ExpandableConnector7.mo \
ExpandableConnector8.mo \
ExpandableConnector9.mo \
ExpandableConnectorComplex1.mo \
ExpandableConnectorFlow1.mo \
ExpandableConnectorFlow2.mo \
ExpandableConnectorNonDecl1.mo \
Expand Down

0 comments on commit 1e295d1

Please sign in to comment.