#2309 closed defect (worksforme)
Cannot read numbers from a file
Reported by: | paul | Owned by: | Martin Sjölund |
---|---|---|---|
Priority: | high | Milestone: | 1.12.0 |
Component: | Code Generation | Version: | trunk |
Keywords: | Cc: | Willi Braun |
Description (last modified by )
With OMEdit i wanted to execute the following model:
model ReadNumber import Streams = Modelica.Utilities.Streams; import Strings = Modelica.Utilities.Strings; constant String filename = "<pathtofile>/numberfile.txt"; Integer lineno = 1; String line; Boolean eof; Real value; algorithm (line, eof) := Streams.readLine( filename, lineno ); value := Strings.scanReal( line ); end ReadNumber;
The content of numberfile.txt in the path <pathtofile> is
1.1 2.1 3.1
OMEdit apparently exectutes all steps including simulation. There is no error message. However, the plot shows no variables to be displayed, as if no simulation had been done.
I am using OpenModelica 1.9.0 beta4+dev (r16769). The two original files have been attached.
Attachments (4)
Change History (29)
by , 11 years ago
Attachment: | ReadNumber-br.mo added |
---|
by , 11 years ago
Attachment: | numberfile.txt added |
---|
comment:1 by , 11 years ago
Component: | Backend → OMEdit |
---|---|
Owner: | changed from | to
by , 11 years ago
comment:2 by , 11 years ago
It even works fine for me also. Check the attached screenshot "plot.png".
comment:3 by , 11 years ago
Description: | modified (diff) |
---|
comment:4 by , 11 years ago
After reading your inputs i tried it again, and it worked fine for me too, this time. I cannot reconstruct yesterday's behavior, which is mysterious to me. Sorry about that, but thank you both, anyway.
Then i tried what i wanted to do originally, namely reading in an array of numbers. This model (attached as ReadArray-br.mo; it works with the same numberfile.txt) gives an error message, which is:
Translation 13:11:28 CodegenC.tpl 6162:9-6162:9 Template error: writeLhsCref UNHANDLED: a[lineno] = tmp1.c1
I don't know what that wants to say, and i cannot figure out what's wrong. Could you please try it out and give me a hint?
by , 11 years ago
Attachment: | ReadArray-br.mo added |
---|
comment:5 by , 11 years ago
That is probably not a helpful message for you as user :-)
You can avoid this missing case in our code generation with the following workaround:
model ReadArray import Streams = Modelica.Utilities.Streams; import Strings = Modelica.Utilities.Strings; constant String filename = "numberfile.txt"; constant Integer alength = 3; Real[alength] a; String line; Boolean eof; protected Real temp; algorithm for i in 1:alength loop (line, eof):= Streams.readLine(filename, i); temp := Strings.scanReal(line); a[i] := temp; end for; end ReadArray;
comment:6 by , 11 years ago
Cc: | added |
---|
I don't know why its not working. I am adding Willi in the CC list so he can have look at it. For the time being you can use the following code,
model ReadArray import Streams = Modelica.Utilities.Streams; import Strings = Modelica.Utilities.Strings; constant String filename = "<pathtofile>/numberfile.txt"; constant Integer alength = 3; Real[alength] a; Real temp; String line; Boolean eof; algorithm for lineno in 1:alength loop (line, eof):= Streams.readLine( filename, lineno ); temp:=Strings.scanReal( line ); a[lineno]:= temp; end for; end ReadArray;
It would be more better if you use something like this,
while not eof loop // read here end while;
comment:7 by , 11 years ago
Component: | OMEdit → Backend |
---|---|
Owner: | changed from | to
comment:8 by , 11 years ago
Component: | Backend → Code Generation |
---|---|
Owner: | changed from | to
follow-up: 10 comment:9 by , 11 years ago
Ok, the workaround helps, thanks.
As to a while-loop, is there a reason to prefer it over a for-loop if we knew that the number of lines in the file is fixed?---But, actually, we are approaching my original intent: i wanted to read in items from a file whose number is not fixed. So i need a while-loop. Therefore i checked a modification of the model above which just replaces the for-loop by a while-loop (and introduces an explicit variable for indexing the lines):
model ReadArray import Streams = Modelica.Utilities.Streams; import Strings = Modelica.Utilities.Strings; constant String filename = "numberfile.txt"; constant Integer alength = 3; Real[alength] a; String line; Boolean eof; Integer lineno(start=1); protected Real temp; algorithm while not eof loop (line, eof):= Streams.readLine( filename, lineno ); temp:= Strings.scanReal( line ); a[lineno]:= temp; lineno:= lineno + 1; end while; end ReadArray;
The translation seems to be ok, but, unfortunately, now the executable crashes.
The second thing i need is an array with an unspecified dimension. I checked it with a model that leaves just the dimension unspecified (and still uses a for-loop):
model ReadArray import Streams = Modelica.Utilities.Streams; import Strings = Modelica.Utilities.Strings; constant String filename = "Test/numberfile.txt"; constant Integer alength = 3; Real[:] a; String line; Boolean eof; protected Real temp; algorithm for lineno in 1:alength loop (line, eof):= Streams.readLine( filename, lineno ); temp:= Strings.scanReal( line ); a[lineno]:= temp; end for; end ReadArray;
Here i get:
"Translation 16:46:45 0:0-0:0 Interner Fehler Transformation Module PFPlusExt index Reduction Method Pantelites failed!"
and
Symbolic 16:46:45 0:0-0:0 Too few equations, under-determined system. The model has 3 equation(s) and 4 variable(s).
but i can't tell how OM counts.
I would be happy to find a way to read in an unspecified number of items from a file in a sensible manner. Any hint is welcome.
follow-up: 11 comment:10 by , 11 years ago
Replying to anonymous:
The second thing i need is an array with an unspecified dimension. I checked it with a model that leaves just the dimension unspecified (and still uses a for-loop):
That will not work. The Modelica Compiler needs to know the number of variables and equations.
follow-up: 12 comment:11 by , 11 years ago
Replying to lochel:
Replying to anonymous:
The second thing i need is an array with an unspecified dimension. I checked it with a model that leaves just the dimension unspecified (and still uses a for-loop):
That will not work. The Modelica Compiler needs to know the number of variables and equations.
Ok, that's what i was afraid of. But how is this to be done in Modelica? In other languages there is something like lists, and they can be constructed dynamically, without knowing in advance how many members they will finally have. So what's the Modelica way to do that?
(It would be fine to have this array as a parameter. But its members, and the information on the number of the members shall come from an external data file).
comment:12 by , 11 years ago
Replying to paul:
Ok, that's what i was afraid of. But how is this to be done in Modelica? In other languages there is something like lists, and they can be constructed dynamically, without knowing in advance how many members they will finally have. So what's the Modelica way to do that?
(It would be fine to have this array as a parameter. But its members, and the information on the number of the members shall come from an external data file).
It is ok to do that in Modelica. But it has to be done in functions (local variables are not part of the global equation/variable count).
comment:14 by , 10 years ago
Milestone: | 1.9.1 → 1.9.2 |
---|
This ticket was not closed for 1.9.1, which has now been released. It was batch modified for milestone 1.9.2 (but maybe an empty milestone was more appropriate; feel free to change it).
comment:15 by , 10 years ago
Milestone: | 1.9.2 → 1.9.3 |
---|
Milestone changed to 1.9.3 since 1.9.2 was released.
comment:20 by , 8 years ago
Milestone: | 1.11.0 → 1.12.0 |
---|
Milestone moved to 1.12.0 due to 1.11.0 already being released.
comment:21 by , 7 years ago
Milestone: | 1.12.0 → Future |
---|
The milestone of this ticket has been reassigned to "Future".
If you think the issue is still valid and relevant for you, please select milestone 1.13.0 for back-end, code generation and run-time issues, or 2.0.0 for front-end issues.
If you are aware that the problem is no longer present, please select the milestone corresponding to the version of OMC you used to check that, and set the status to "worksforme".
In both cases, a short informative comment would be welcome.
comment:22 by , 7 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
follow-up: 25 comment:23 by , 7 years ago
Milestone: | Future → 1.12.0 |
---|---|
Resolution: | wontfix |
Status: | closed → reopened |
@casella: Never close a ticket with milestone set to future. Set it to empty if anything.
Anyway, the model works for me now (comment:9 fails on line 4 because of no error-checking, but a for-loop 1:3 works):
model ReadArray import Streams = Modelica.Utilities.Streams; import Strings = Modelica.Utilities.Strings; constant String filename = "numberfile.txt"; constant Integer alength = 3; Real[alength] a; String line; Boolean eof; protected Real temp; algorithm for lineno in 1:3 loop print(String(lineno)+"\n"); (line, eof):= Streams.readLine( filename, lineno ); temp:= Strings.scanReal( line ); a[lineno]:= temp; lineno:= lineno + 1; end for; end ReadArray;
comment:24 by , 7 years ago
Resolution: | → worksforme |
---|---|
Status: | reopened → closed |
comment:25 by , 7 years ago
Replying to sjoelund.se:
@casella: Never close a ticket with milestone set to future. Set it to empty if anything.
Sure, sorry, I messed up with that.
I tested your model with OMNotebook and OMEdit. It is working fine with both tools.
using Windows and OpenModelica 1.9.0 beta4+dev (r16751)