Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#2525 closed defect (fixed)

Issue with for loops

Reported by: michael.tiller@… Owned by: Martin Sjölund
Priority: high Milestone: 1.9.1
Component: Frontend Version: trunk
Keywords: Cc:

Description (last modified by Martin Sjölund)

The following model doesn't simulate in OM but does run in Dymola. Martin Sjölund says it's a bug. :-) Here is the code:

model Rod_ForLoop "Modeling heat conduction in a rod using a for loop"
  type Temperature=Real(unit="K", min=0);
  type ConvectionCoefficient=Real(unit="W/K", min=0);
  type ConductionCoefficient=Real(unit="W.m-1.K-1", min=0);
  type Mass=Real(unit="kg", min=0);
  type SpecificHeat=Real(unit="J/(K.kg)", min=0);
  type Density=Real(unit="kg/m3", min=0);
  type Area=Real(unit="m2");
  type Volume=Real(unit="m3");
  type Length=Real(unit="m", min=0);
  type Radius=Real(unit="m", min=0);

  constant Real pi = 3.14159;

  parameter Integer n=10;
  parameter Length L=1.0;
  parameter Radius R=0.1;
  parameter Density rho=2.0;
  parameter ConvectionCoefficient h=2.0;
  parameter ConductionCoefficient k=10;
  parameter SpecificHeat C=10.0;
  parameter Temperature Tamb=300 "Ambient temperature";

  parameter Area A = pi*R^2;
  parameter Volume V = A*L/n;

  Temperature T[n];
initial equation
  T = linspace(200,300,n);
equation
  rho*V*C*der(T[1]) = -h*(T[1]-Tamb)-k*A/(L/n)*(T[1]-T[2]);
  for i in 2:(n-1) loop
    rho*V*C*der(T[i]) = -k*(L/n)*(T[i]-T[i-1])-k*A/(L/n)*(T[i]-T[i+1]);
  end for;
  rho*V*C*der(T[end]) = -h*(T[end]-Tamb)-k*A/(L/n)*(T[end]-T[end-1]);
end Rod_ForLoop;

Change History (9)

comment:1 by Martin Sjölund, 11 years ago

Component: BackendFrontend
Description: modified (diff)
Owner: changed from probably noone to somebody

comment:2 by Martin Sjölund, 11 years ago

The problem seems to be the initial equation, which is moved to the non-initial section in the flattened code.

comment:3 by Adam Dershowitz <dersh@…>, 11 years ago

Changing it from n to a constant allows it to work:

  T = linspace(200,300,10);

Perhaps it is this bug cropping up again:
https://trac.openmodelica.org/OpenModelica/ticket/2027

comment:4 by anonymous, 11 years ago

Thanks for pointing out the workaround. But since this is an example for my book, I can't really use a workaround. I need to demonstrate the proper way to do this (which is, clearly, using the parameter).

comment:5 by Adam Dershowitz <dersh@…>, 11 years ago

Sure. I really intended this more to help localize the bug, then to really "solve the problem".
Hopefully, this will help to get rid of the actual bug.
Does this mean a new version of your book is coming out? That would be great.

comment:6 by anonymous, 11 years ago

An entirely new book actually. And I'm using OpenModelica to generate results for all the plots and perhaps even more than that. So you may see more bug reports from me in the coming weeks. :-)

comment:7 by Martin Sjölund, 11 years ago

Owner: changed from somebody to Martin Sjölund
Status: newassigned

comment:8 by Martin Sjölund, 11 years ago

Resolution: fixed
Status: assignedclosed

Fixed in r18560, with testcase.

comment:9 by Adam Dershowitz <dersh@…>, 11 years ago

For some reason, the results look very different after r18561, then they did with my work around and a slightly older version (18599).
I don't know which ones are "correct". It could be that my workaround just allowed it to run, but was then doing a bad simulation.
I just wanted to point it out, to ask if the new results match what is expected. My guess is that the new results look more reasonable then the old one.

Note: See TracTickets for help on using tickets.