0

I am trying to write a establish a Dymola python interface where a .csv file is read and given in the CombiTimeTable in Dymola. But I do not find any help to do that. Even with Dymola Script/Command I wasn't able to give in my csv file and a filename.

Does anyone have any idea how would i be able to do so?

P.S. I have also tried the ExternData package.

I have used

Modelica.Blocks.Sources.CombiTimeTable CombiTimeTable(tableOnFile=True, tableName="",fileName="")

And

innerparameter ExternData

Update

Found a alternative approach of doing it with simulateExtendedModel() as:

dymola.simulateExtendedModel("Model",startTime=0, stopTime=1, numberOfIntervals=0, outputInterval=300,
initialNames={"CombiTimeTable.table"}, initialValues={DataFiles.readCSVmatrix("Z:\your.csv")}); 

Still struggling with the errors but in the combiTimeTable it's possible to write a .csv in the table, whereas .txt and .mat under tableOnFile.

Rb8685
  • 1
  • 1
  • `CombiTimeTable` does not support reading csv files, only text files in a very special format. Just check the documentation of the class for details. `ExternData` shows how to read csv files in `ExternData.Examples.CSVTest`. – marco Feb 15 '23 at 09:04

1 Answers1

0

i have myself encountered a similar issue some time ago.

If you look at the documentation CombiTimeTable Documentation you can see that you can provide a .txt file as a source for the time table.

Here is a bit of python code used to process a csv into a suitable file (it may not be the best way, but it works perfectly for me). I am using the python pandas library :

import pandas as pd

"""Creating a dataframe by reading from your source csv file"""
data = pd.read_csv("Path/To/Your/SourceFile.csv")

"""Creating the entry file for the combi time table"""
f = open (file="Path/To/Dymola/Source.txt" ,mode="w+")
"""Writing whatever Dymola needs to read the file (Check the CombiTimeTable documentation for more informations) """
f.write("#1 \n")
f.write("double tab1("+str(len(data.axes[0]))+","+str(len(data.axes[1]))+")   # comment line" +"\n")
f.close()
"""Using the to_csv method by pointing to an existing txt file, and using the 'a' (append) mode will cause the values from the dataframe to be next to the existing text"""
data.to_csv("Path/To/Dymola/Source.txt",mode ="a",sep = '\t', index = None, header=False)

Here is an example of what it returns me : Example of a generated CombiTimeTable source file

Some precisions :

  • I am assuming that your data from your csv have headers.
  • The first column in your csv contains time points (in my example they represent minutes).

Dymola side : Here is how you can setup the CombiTimeTable (based on my example) : CombiTimeTable setup example

I hope that you'll find this answer helpful.

Merle
  • 1
  • 2
  • Thank you for the response! Yes I checked the Documentation and rewrote as a text file. I will try your method. Found a diferent way of doing it with SimulateExtendedModel() as: dymola.simulateExtendedModel("Model",startTime=0, stopTime=1, numberOfIntervals=0, outputInterval=300, initialNames={"CombiTimeTable.table"}, initialValues={DataFiles.readCSVmatrix("Z:\your.csv")}); Still struggling with it's errors but in the combiTimeTable it's possible to write a .csv in the table, whereas .txt and .mat under tableOnFile. – Rb8685 Feb 17 '23 at 12:46