Opened 11 years ago

Closed 7 years ago

Last modified 7 years ago

#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 Lennart Ochel)

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)

ReadNumber-br.mo (369 bytes ) - added by anonymous 11 years ago.
numberfile.txt (13 bytes ) - added by anonymous 11 years ago.
plot.png (172.7 KB ) - added by Adeel Asghar 11 years ago.
ReadArray-br.mo (430 bytes ) - added by anonymous 11 years ago.

Download all attachments as: .zip

Change History (29)

by anonymous, 11 years ago

Attachment: ReadNumber-br.mo added

by anonymous, 11 years ago

Attachment: numberfile.txt added

comment:1 by Lennart Ochel, 11 years ago

Component: BackendOMEdit
Owner: changed from probably noone to Adeel Asghar

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)

Last edited 11 years ago by Lennart Ochel (previous) (diff)

by Adeel Asghar, 11 years ago

Attachment: plot.png added

comment:2 by Adeel Asghar, 11 years ago

It even works fine for me also. Check the attached screenshot "plot.png".

comment:3 by Lennart Ochel, 11 years ago

Description: modified (diff)

comment:4 by paul, 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 anonymous, 11 years ago

Attachment: ReadArray-br.mo added

comment:5 by Lennart Ochel, 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;
Last edited 11 years ago by Lennart Ochel (previous) (diff)

comment:6 by Adeel Asghar, 11 years ago

Cc: Willi Braun 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 Adeel Asghar, 11 years ago

Component: OMEditBackend
Owner: changed from Adeel Asghar to probably noone

comment:8 by Lennart Ochel, 11 years ago

Component: BackendCode Generation
Owner: changed from probably noone to Martin Sjölund

comment:9 by anonymous, 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.

Last edited 11 years ago by Lennart Ochel (previous) (diff)

in reply to:  9 ; comment:10 by Lennart Ochel, 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.

in reply to:  10 ; comment:11 by paul, 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).

in reply to:  11 comment:12 by Martin Sjölund, 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:13 by Martin Sjölund, 11 years ago

Milestone: 1.9.01.9.1

Postponed until 1.9.1

comment:14 by Martin Sjölund, 10 years ago

Milestone: 1.9.11.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 Martin Sjölund, 10 years ago

Milestone: 1.9.21.9.3

Milestone changed to 1.9.3 since 1.9.2 was released.

comment:16 by Martin Sjölund, 9 years ago

Milestone: 1.9.31.9.4

Moved to new milestone 1.9.4

comment:17 by Martin Sjölund, 9 years ago

Milestone: 1.9.41.9.5

Milestone pushed to 1.9.5

comment:18 by Martin Sjölund, 9 years ago

Milestone: 1.9.51.10.0

Milestone renamed

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

Milestone: 1.10.01.11.0

Ticket retargeted after milestone closed

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

Milestone: 1.11.01.12.0

Milestone moved to 1.12.0 due to 1.11.0 already being released.

comment:21 by Francesco Casella, 7 years ago

Milestone: 1.12.0Future

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 Francesco Casella, 7 years ago

Resolution: wontfix
Status: newclosed

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

Milestone: Future1.12.0
Resolution: wontfix
Status: closedreopened

@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 Martin Sjölund, 7 years ago

Resolution: worksforme
Status: reopenedclosed

in reply to:  23 comment:25 by Francesco Casella, 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.

Note: See TracTickets for help on using tickets.