#2525 closed defect (fixed)
Issue with for loops
Reported by: | Owned by: | Martin Sjölund | |
---|---|---|---|
Priority: | high | Milestone: | 1.9.1 |
Component: | Frontend | Version: | trunk |
Keywords: | Cc: |
Description (last modified by )
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 , 11 years ago
Component: | Backend → Frontend |
---|---|
Description: | modified (diff) |
Owner: | changed from | to
comment:2 by , 11 years ago
comment:3 by , 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 , 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 , 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 , 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 , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:8 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:9 by , 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.
The problem seems to be the initial equation, which is moved to the non-initial section in the flattened code.