Opened 9 years ago

Closed 4 years ago

#3514 closed discussion (invalid)

(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?

Attachments (1)

TestFile.mo (3.0 KB) - added by Christian Kral <dr.christian.kral@…> 9 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 Changed 9 years ago by lochel

I think that this is a Modelica issue. What about an algorithm section? There you could define a certain order of the statements.

Changed 9 years ago by Christian Kral <dr.christian.kral@…>

comment:2 Changed 9 years ago by sjoelund.se

There is no defined order in an equation section, so the behavior is correct but unexpected. Use algorithm sections for writing statements where execution order matters.

comment:3 Changed 4 years ago by perost

  • Milestone changed from Future to never
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.