I would like to enforce that the user cannot connect an input to an input. I expected the code below to give a compile-time error but it gives no error. How can I fix this?
Another issue is the package-global compile-time constant C. It is sort of a parameter, and it should be provided by the user of the package. How should this be implemented in Modelica?
package Pkg
constant Integer C=3;
connector Connector
Real x[C];
end Connector;
connector InConn = input Connector;
connector OutConn = output Connector;
class Base
InConn[:] inlet;
OutConn[:] outlet;
end Base;
class A
extends Base;
redeclare InConn[1] inlet;
redeclare OutConn[1] outlet;
end A;
end Pkg;
model Test
import Pkg.*;
A p;
A q;
equation
connect(p.inlet[1], q.inlet[1]);
end Test;
There are a couple of problems here. The main one is that your redeclarations in
Aare not correct. They should be modifications on theextendsclause. But also note they are not even necessary since they don’t actually change anything. Specifying sizes should be done through parameters.Similarly, the constant really needs to be a parameter of your
Connectordefinition. The Modelica compiler should throw an error if you connect two connectors with different sizes (specifically, it should generate an assertion on the values of any parameters in the connection set).I don’t have a Modelica compiler installed on this machine, but I suggest you try this and see if this works better for you:
Hopefully that will get things into a state where the compiler will generate the correct error. The semantics of Modelica are such that connection of two inputs should trigger an error (in fact, that is the fundamental restriction of input and output connectors).