0

The following construct generates an error saying that there are too many equations.

model Model1

model myBlock
input Modelica.Blocks.Interfaces.RealInput u(start=1e99);
output Modelica.Blocks.Interfaces.RealOutput y;
parameter Real p=1 "Parameter";
equation
            u=min(u,p);
            u=y;
end myBlock;

    myBlock b1; 
    myBlock b2 (p=0.5);
    myBlock b3;
    input Modelica.Blocks.Interfaces.RealInput u;
    output Modelica.Blocks.Interfaces.RealOutput y;
equation
    connect(y,b1.u);
    connect(b1.y,b2.u);
    connect(b2.y,b3.u);
    connect(b3.y,u);
end Model1;

The idea is to get the same minimum value for u and y in all the "myBlock" components. However the assignement for u (=min...) seems to be obligatory valid for all components and cannot be calculate one after the other.

Any idea how I can solve this problem? Thanks.

Geronimo
  • 46
  • 3

1 Answers1

1

I don't entirely understand the intent of this model. It is not clear to me whether you expect to find the minimum of all u values over time or just the current value of u vs. p? If the former, then that isn't what min is for. If the latter, then you are on the right track...

Assuming the latter, then your main misconception seems to be about the semantics of equations. It looks to me like you are assuming that you start with one value of u. Then you "reassign" that value to be the min(u,p). Then, finally, you assign y to this "reassigned" value of u.

All that would be fine if you had an algorithm section in your model. This is because an algorithm section has assignment semantics (this is the "imperative" semantic in most programming languages).

But you have an equation section. The thing about an equation section is that the statements you wrote are equations. The important thing about equations is that they always apply. So in you model you have stated that u=min(u,p). This is not "overwriting" the value of u, it is stating an additional mathematical constraint. It is a bit like trying to solve the equation x=x*x. If you put this equation in your model it is the equivalent of saying x=0 because that is the only value of x that can satisfy the equation.

So the error about too many equations comes because if you look at your myBlock model, it has one input and one output. That means it has only one "unknown" to compute and therefore requires only one equation. You've given two equations (one too many).

I think what you want is:

model myBlock
  input Modelica.Blocks.Interfaces.RealInput u;
  output Modelica.Blocks.Interfaces.RealOutput y;
  parameter Real p;
equation
  y = min(u,p);
end myBlock;

This will "limit" the output, y, to be no less than p. Is that what you wanted?

Alternatively, you could write your model with an algorithm section and get something like this:

model myBlock
  input Modelica.Blocks.Interfaces.RealInput u;
  output Modelica.Blocks.Interfaces.RealOutput y;
  parameter Real p;
algorithm
  u := min(u,p) "This is assignment";
  y := u;
end myBlock;

Note the behavior should be the same (although I confess I didn't test either model).

I hope that helps.

Michael Tiller
  • 9,291
  • 3
  • 26
  • 41
  • Thank you Michael, using an algorithm section instead of an equation one help eliminate the error. However it did not solve my problem. – Geronimo Aug 19 '12 at 13:31
  • What I am trying to do is to calculate the the max possible flow in each component of a pipe (valves, fittings,..) depending on the pressure difference between inlet and outlet. The overall flow will then be the least of all the individual maximum flows. – Geronimo Aug 19 '12 at 13:44
  • You will say that it is not how Modelica is meant to work, but I'm struggling now for several weeks trying to understand the classical solution, with no avail. – Geronimo Aug 19 '12 at 13:47
  • Back to the initial question, a good solution would be a statement like: u<=p in the equation section, but that don't seem to exist.(?) – Geronimo Aug 19 '12 at 13:56
  • Hmmm...Does this help: Imagine you have computed 5 potential flows: u1, u2, u3, u4 and u5. You want the actual flow, u, to be the minimum of all of those. You could say `u=min({u1,u2,u3,u4,u5})`. Does that help? I'm still not sure I understand your goal. Instead of commenting on my solution, I suggest you update your question (or open a new question with your broader question). – Michael Tiller Aug 19 '12 at 16:59
  • sure your solution would help, but it is not helpful if you want to develop a library where you can add arbitrary components without having to write an extra equation for the whole model. I'll nevertheless follow your advice and rewrite the question. – Geronimo Aug 19 '12 at 19:03
  • If I understand your last comment correctly, you want your approach to be robust to the number of possible flows. So it should automatically consider all flow paths and then choose the largest value. Is that the issue? There are ways to do that as well by using vectors of connectors. – Michael Tiller Aug 25 '12 at 13:31