Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Read and use data from txt file

Read and use data from txt file

Hi all,
Let's say I have a simple model: y = a*t+b
Where:
t is the time
a is a variable that is read from a txt file, which depend on time.
b is a fixed parameter

model simple
  constant Real b=1;
  Real a,y
equation
  y = a*time+b;
end simple;

now I have the txt file looking like:
t      a
0     1.0
5     1.1
11   1.0
17   0.9
...

I would like to launch a simulation taking the values from txt file (with linear interpolation between values for a continuous time simulation).

Is there a example on this topic somewhere ?

Thanks in advance,
Sam

Re: Read and use data from txt file

Use external C or Modelica.Blocks.Tables and be prepared to swear a lot

Re: Read and use data from txt file

Hi there,

although this topic is a year ago, this might help someone.
I did swear a lot about TimeTable and CombiTable when reading a file with multiple columns.
Everytime I only got the all timesteps for first item y[1], but not the rest of them, y[2:n]

Important is
1. to define the parameter "columns" with either the number of values you want to read, or a real big number to read all of them
2. if youf don't want interpolation, but the real values, you have to set simulation parameters starttime=1, stoptime <= number of time steps in your data files

My example data file "data01.txt" :

Code:

#1 

double tab1(6,3)   # comment line
0 0 0
1 2 5
2 4 7
3 5 8
4 6 9
5 7 10

My Model

Code:

model TestCombiTable

    Modelica.Blocks.Sources.CombiTimeTable combiTimeTable(
    tableOnFile=true,
    fileName="c:/data01.txt",
    tableName="tab1",
    columns=2:1000  // put a big number at the end here
    )
end TestCombiTable;

Simulation using e.g. OMNotbook:

Code:

loadModel(Modelica);

simulate(TestCombiTable,startTime=1,stopTime=6)

Check the values e.g. OMNotebook

Code:

val(TestCombiTable.y[2],{1:6})

Result:
{{5.0,7.0,8.0,9.0,10.0,11.0}}

Maybe this helps someone else a bit of heavy swearing.
Greetings,
Uwe

Re: Read and use data from txt file

how to use external C or Modelica.Blocks for reading the external txt data

There are 0 guests and 0 other users also viewing this topic
You are here: