Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#3515 closed discussion (duplicate)

(Wrong) order of writing text into a file

Reported by: Christian Kral <dr.christian.kral@…> Owned by: somebody
Priority: normal Milestone: never
Component: *unknown* Version:
Keywords: Cc:

Description

When writing data to files in a

when terminal()
end when

section, I ran into some issues. Consider the following three examples:

Example 1

model TestWrite1
  Real x(start = 1, fixed = true);
  Real y(start = 1, fixed = true);
  Real z(start = 1, fixed = true);
  parameter String fileName = "output.dat";
equation
  der(x) = 1;
  der(y) = x;
  der(z) = y;
  when terminal() then
    Modelica.Utilities.Files.removeFile(fileName);
    Modelica.Utilities.Streams.print("x = " + String(x), fileName);
    Modelica.Utilities.Streams.print("y = " + String(y), fileName);
    Modelica.Utilities.Streams.print("z = " + String(z), fileName);
  end when;
end TestWrite1;

When the simulation is terminated, the output file is deleted first. Then the terminal values of the three variables are written to the file. However, the order the written parameters is: z,y,x (not x,y,z as intended and expected)

Example 2

model TestWrite2
  Real x(start = 1, fixed = true);
  Real y(start = 1, fixed = true);
  Real z(start = 1, fixed = true);
  parameter String fileName = "output.dat";
equation
  der(x) = 1;
  der(y) = x;
  der(z) = y;
  when terminal() then
    writeRealParameter(fileName, "x", x, append = false);
    writeRealParameter(fileName, "y", y, append = true);
    writeRealParameter(fileName, "z", z, append = true);
  end when;
end TestWrite2;

In example 2 an axiliary function is used (listed below) to write the terminal values of the variables to a file. In this function append indicates whether the outputs should be added to the existing file when append==true, or a new file shall be created when append==false. Considering the order of execution of file writing statements of example 1:

  • z is written firstly (appended to existing file)
  • y is written secondly (appended to existing file)
  • x is written at the end (deleting the existing file first)

So finally, only x appears in the file when the simulation terminates. This behavior is not strictly logical to me.

Additional function writeRealParameter

function writeRealParameter "Writing real parameter to file"
  input String fileName "Name of file";
  input String name "Name of parameter";
  input Real data "Actual value of parameter";
  input Boolean append = false "Append data to file";
algorithm
  if not append then
    Modelica.Utilities.Files.removeFile(fileName);
  end if;
  Modelica.Utilities.Streams.print(name + " = " + String(data), fileName);
end writeRealParameter;

Example 3

model TestWrite3
  Real x(start = 1, fixed = true);
  Real y(start = 1, fixed = true);
  Real z(start = 1, fixed = true);
  parameter String fileName = "output.dat";
equation
  der(x) = 1;
  der(y) = x;
  der(z) = y;
  when terminal() then
    writeRealParameter(fileName, "z", z, append = true);
    writeRealParameter(fileName, "y", y, append = true);
    writeRealParameter(fileName, "x", x, append = false);
  end when;
end TestWrite3;

In example 3 the order of writing parameters is changed so that the three terminal values are written to the output file in the intended order. The order of variables is x,y,z even though the order of statements is z,y,x and one would expect that the only x is written to file from looking at the order of writeRealParameter statements.

Discussion

The question here is whether there is an order of how the statements in a when clause are executed. I could not find anything in the Modelica Specification 3.2 rev 2, but maybe I missed it.

I wonder whether this is an issue of OpenModelica or the Modelica specification. In case it is a Modelica specification issue it may make sense that the order of executed statements should be the same as written in the code. Otherwise one could not write an output data file without encapsulating the writing function into an algorithm.

I understand that there is a workaround of this issue by deleting the file in an initial algorithm. Writing the data to a file in given order then has to be performed by an algorithm executed at the termination of the simulation. Saying this, it may make sense to introduce something like a terminal equation section in Modelica.

Any comments?

Change History (2)

comment:1 by Christian Kral <dr.christian.kral@…>, 9 years ago

Resolution: duplicate
Status: newclosed

Created twice accidentally in #3514

comment:2 by Dietmar Winkler, 9 years ago

Milestone: Futurenever

Sorting away the closed as invalid, won't fix and duplicate tickets from Future.

Note: See TracTickets for help on using tickets.