0

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;
Ali
  • 56,466
  • 29
  • 168
  • 265

1 Answers1

2

There are a couple of problems here. The main one is that your redeclarations in A are not correct. They should be modifications on the extends clause. 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 Connector definition. 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:

package Pkg

  connector Connector
    parameter Integer C=3;
    Real x[C];
  end Connector;

  connector InConn  = input  Connector;
  connector OutConn = output Connector;

  class Base
    parameter Integer ni;
    parameter Integer no;
    InConn[ni]  inlet;
    OutConn[no] outlet;
  end Base;

  class A
    extends Base(ni=1, no=1);
  end A;

end Pkg;

model Test
  import Pkg.*;
  A p;
  A q;
equation
  connect(p.inlet[1], q.inlet[1]);
end Test;

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).

Michael Tiller
  • 9,291
  • 3
  • 26
  • 41
  • Many thanks, it fixes my problems! As for the messed up connection, I get a failed assert and that is enough. – Ali Apr 28 '12 at 10:13
  • Ooops, not quite. The classes also need access to the package-global `C` and those classes have no Connectors. So moving the `C` inside the connector class breaks everything. I may come up with another question. – Ali Apr 30 '12 at 09:23
  • You can still make a package level constant and then initialize the parameters in the connectors with the package level constant. But why would a component that cannot access your `x` array need to know its size? I think there is a more fundamental architectural issue here as well. – Michael Tiller May 01 '12 at 07:02
  • It is very possible that there are design flaws. It is difficult to elaborate. The best explanation I can give you at the moment is the following. There are equations in which the user has to iterate over the range 1:C. These equations are supplied by the user of the package, outside of any connector and blocks. The only thing the classes and connectors have to know is the C. Apart from that the classes and connectors of the package are decoupled from the user-defined equations. – Ali May 01 '12 at 16:53
  • If you have the time and patience, could you please help me with [another question](http://stackoverflow.com/q/10380792/341970)? It is an example for user supplied, non-default equations, although this issue with the `C` is not in it. – Ali May 01 '12 at 16:55
  • Actually, I wrote up a complete answer to that and forgot to post it. I just pressed the "post" button so you should see an answer to that question as well now. – Michael Tiller May 02 '12 at 07:48