Buildings.Utilities.IO.Files

Reports package

Information

Package with models for writing data to files.

Extends from Modelica.Icons.Package (Icon for standard packages).

Package Content

Name Description
Buildings.Utilities.IO.Files.CSVWriter CSVWriter Model for writing results to a .csv file
Buildings.Utilities.IO.Files.CombiTimeTableWriter CombiTimeTableWriter Model for writing results to a format that is readable by the model CombiTimeTable
Buildings.Utilities.IO.Files.JSONWriter JSONWriter Model for writing results to a json file
Buildings.Utilities.IO.Files.Printer Printer Model that prints values to a file
Buildings.Utilities.IO.Files.WeeklySchedule WeeklySchedule Weekly schedule
Buildings.Utilities.IO.Files.Examples Examples Examples package
Buildings.Utilities.IO.Files.BaseClasses BaseClasses Package with base classes for Buildings.Utilities.IO.Files

Buildings.Utilities.IO.Files.CSVWriter Buildings.Utilities.IO.Files.CSVWriter

Model for writing results to a .csv file

Buildings.Utilities.IO.Files.CSVWriter

Information

This model samples the model inputs u and saves them to a .csv file, which can be read using e.g. Excel or Python.

Typical use and important parameters

The parameter nin defines the number of variables that are stored. In Dymola, this parameter is updated automatically when inputs are connected to the component.

The parameter fileName defines to what file name the results are saved. The file is in the current working directory, unless an absolute path is provided.

The parameter samplePeriod defines every how many seconds the inputs are saved to the file.

Options

The parameter delimiter can be used to choose a custom separator.

By default the first line of the csv file consists of the file header. The column names can be defined using parameter headerNames. The header can be removed by setting writeHeader=false

Dynamics

This model samples the outputs at an equidistant interval and hence disregards the simulation tool output interval settings.

Extends from Buildings.Utilities.IO.Files.BaseClasses.FileWriter (Partial model for writing results to a .csv file).

Parameters

TypeNameDefaultDescription
StringfileNamegetInstanceName() + ".csv"File name, including extension
TimesamplePeriod Sample period: equidistant interval for which the inputs are saved [s]
Advanced
Stringdelimiter"\t"Delimiter for csv file
BooleanwriteHeadertrue=true, to write header with variable names, otherwise no header will be written
StringheaderNames[nin]{"col" + String(i) for i in ...Header names, indices by default
IntegersignificantDigits6Number of significant digits that are used for converting inputs into string format

Connectors

TypeNameDescription
input RealVectorInputu[nin]Variables that are saved

Modelica definition

model CSVWriter "Model for writing results to a .csv file" extends Buildings.Utilities.IO.Files.BaseClasses.FileWriter( final isCombiTimeTable=false); initial algorithm if writeHeader then str := str + "time" + delimiter; for i in 1:nin-1 loop str := str + headerNames[i] + delimiter; if mod(i+1,10)==0 then // write out buffer every 10 entries to avoid overflow writeLine(filWri, str, 1); str:=""; end if; end for; str := str + headerNames[nin] + "\n"; writeLine(filWri, str, 1); end if; end CSVWriter;

Buildings.Utilities.IO.Files.CombiTimeTableWriter Buildings.Utilities.IO.Files.CombiTimeTableWriter

Model for writing results to a format that is readable by the model CombiTimeTable

Buildings.Utilities.IO.Files.CombiTimeTableWriter

Information

This model samples the model inputs u and saves them to a .csv file, which can be read by a Modelica.Blocks.Sources.CombiTimeTable. The Modelica.Blocks.Sources.CombiTimeTable must then have tableName="csv", as illustrated in the example Buildings.Utilities.IO.Files.Examples.CSVReader.

Typical use and important parameters

The parameter nin defines the number of variables that are stored. In Dymola, this parameter is updated automatically when inputs are connected to the component.

The parameter fileName defines to what file name the results are saved. The file is in the current working directory, unless an absolute path is provided.

The parameter samplePeriod defines every how many seconds the inputs are saved to the file.

Options

By default the first line of the csv file consists of the file header. The column names can be defined using parameter headerNames or the header can be removed by setting writeHeader=false

Dynamics

This model samples the outputs at an equidistant interval and hence disregards the simulation tool output interval settings.

Extends from Buildings.Utilities.IO.Files.BaseClasses.FileWriter (Partial model for writing results to a .csv file).

Parameters

TypeNameDefaultDescription
StringfileNamegetInstanceName() + ".csv"File name, including extension
TimesamplePeriod Sample period: equidistant interval for which the inputs are saved [s]
Advanced
Stringdelimiter"\t"Delimiter for csv file
BooleanwriteHeadertrue=true, to write header with variable names, otherwise no header will be written
StringheaderNames[nin]{"col" + String(i) for i in ...Header names, indices by default
IntegersignificantDigits6Number of significant digits that are used for converting inputs into string format

Connectors

TypeNameDescription
input RealVectorInputu[nin]Variables that are saved

Modelica definition

model CombiTimeTableWriter "Model for writing results to a format that is readable by the model CombiTimeTable" extends Buildings.Utilities.IO.Files.BaseClasses.FileWriter( final delimiter="\t", final isCombiTimeTable=true); initial algorithm if writeHeader then str :="# time" + delimiter; for i in 1:nin-1 loop str :=str + headerNames[i] + delimiter; if mod(i+1,10)==0 then // write out buffer every 10 entries to avoid overflow writeLine(filWri, str, 1); str:=""; end if; end for; str :=str + headerNames[nin] + "\n"; writeLine(filWri, str, 1); end if; end CombiTimeTableWriter;

Buildings.Utilities.IO.Files.JSONWriter Buildings.Utilities.IO.Files.JSONWriter

Model for writing results to a json file

Buildings.Utilities.IO.Files.JSONWriter

Information

This model samples the model inputs u and saves them to a json file.

Typical use and important parameters

The parameter nin defines the number of variables that are stored. In Dymola, this parameter is updated automatically when inputs are connected to the component.

The parameter fileName defines to what file name the results are saved. The file is in the current working directory, unless an absolute path is provided.

The parameter keyNames defines the key names that are used to store the json values corresponding to the inputs u.

Dynamics

This model samples its inputs at the time defined by the parameter outputTime and writes them to the file fileName. The model has the following options:

Extends from Modelica.Blocks.Icons.DiscreteBlock (Graphical layout of discrete block component icon).

Parameters

TypeNameDefaultDescription
StringfileNamegetInstanceName() + ".json"File name, including extension
StringvarKeys[nin]{"key" + String(i) for i in ...Key names, indices by default
OutputTimeoutputTimeBuildings.Utilities.IO.Files...Time when results are written to file
TimecustomTime0Custom time when results are stored, used if outputTime=Custom only [s]

Connectors

TypeNameDescription
input RealVectorInputu[nin]Variables that are saved

Modelica definition

model JSONWriter "Model for writing results to a json file" extends Modelica.Blocks.Icons.DiscreteBlock; parameter Integer nin "Number of inputs"; parameter String fileName = getInstanceName() + ".json" "File name, including extension"; parameter String[nin] varKeys = {"key" + String(i) for i in 1:nin} "Key names, indices by default"; parameter Buildings.Utilities.IO.Files.BaseClasses.OutputTime outputTime= Buildings.Utilities.IO.Files.BaseClasses.OutputTime.Terminal "Time when results are written to file"; parameter Modelica.Units.SI.Time customTime=0 "Custom time when results are stored, used if outputTime=Custom only"; Modelica.Blocks.Interfaces.RealVectorInput[nin] u "Variables that are saved"; protected parameter String insNam = getInstanceName() "Instance name"; Buildings.Utilities.IO.Files.BaseClasses.JSONWriterObject jsonWri= Buildings.Utilities.IO.Files.BaseClasses.JSONWriterObject( insNam, fileName, outputTime==Buildings.Utilities.IO.Files.BaseClasses.OutputTime.Terminal, varKeys) "File writer object"; equation if outputTime==Buildings.Utilities.IO.Files.BaseClasses.OutputTime.Terminal then Buildings.Utilities.IO.Files.BaseClasses.cacheVals(jsonWri, u); end if; if outputTime==Buildings.Utilities.IO.Files.BaseClasses.OutputTime.Initial then when initial() then Buildings.Utilities.IO.Files.BaseClasses.writeJSON(jsonWri, u); end when; end if; if outputTime==Buildings.Utilities.IO.Files.BaseClasses.OutputTime.Custom then when time>=customTime then Buildings.Utilities.IO.Files.BaseClasses.writeJSON(jsonWri, u); end when; end if; end JSONWriter;

Buildings.Utilities.IO.Files.Printer Buildings.Utilities.IO.Files.Printer

Model that prints values to a file

Buildings.Utilities.IO.Files.Printer

Information

This model prints to a file or the terminal at a fixed sample interval.

The parameter configuration controls the printing as follows:

configurationconfiguration
1 print at sample times only
2 print at sample times and at end of simulation
3 print at end of simulation only

Extends from Modelica.Blocks.Interfaces.DiscreteBlock (Base class of discrete control blocks).

Parameters

TypeNameDefaultDescription
TimesamplePeriod Sample period of component [s]
TimestartTime0First sample time instant [s]
Stringheader""Header to be printed
StringfileName""File name (empty string is the terminal)
Integernin1Number of inputs
Integerconfiguration1Index for treating final report (see documentation)
IntegerminimumLength1Minimum length of result string
IntegersignificantDigits16Number of significant digits

Connectors

TypeNameDescription
input RealInputx[nin]Value to be printed

Modelica definition

model Printer "Model that prints values to a file" extends Modelica.Blocks.Interfaces.DiscreteBlock( firstTrigger( start=false, fixed=true)); parameter String header="" "Header to be printed"; parameter String fileName="" "File name (empty string is the terminal)"; parameter Integer nin=1 "Number of inputs"; parameter Integer configuration = 1 "Index for treating final report (see documentation)"; parameter Integer minimumLength = 1 "Minimum length of result string"; parameter Integer significantDigits = 16 "Number of significant digits"; Modelica.Blocks.Interfaces.RealInput x[nin] "Value to be printed"; initial algorithm if (fileName <> "") then Modelica.Utilities.Files.removeFile(fileName); end if; Modelica.Utilities.Streams.print(fileName=fileName, string=header); equation if configuration < 3 then when {sampleTrigger, initial()} then Buildings.Utilities.IO.Files.BaseClasses.printRealArray( x=x, fileName=fileName, minimumLength=minimumLength, significantDigits=significantDigits); end when; end if; when terminal() then if configuration >= 2 then Buildings.Utilities.IO.Files.BaseClasses.printRealArray( x=x, fileName=fileName, minimumLength=minimumLength, significantDigits=significantDigits); end if; end when; end Printer;

Buildings.Utilities.IO.Files.WeeklySchedule Buildings.Utilities.IO.Files.WeeklySchedule

Weekly schedule

Buildings.Utilities.IO.Files.WeeklySchedule

Information

This model interprets a schedule file and performs a weekly, cyclic extrapolation on the source data. An example for a schedule entry is

double tab1(3,2)
# For week-day, output 1 between 7:00 and 19:00, and 0 otherwise
mon,tue,wed,thu,fri:7:00:00  1
mon,tue,wed,thu,fri:19:00:00  0
# For week-end, output always 0
sat,sun:0:00:00 0

The first row must start with double tab1 and be followed by the number of rows (excluding comments) and number of columns, whereas the time column is considered the first column.
Lines that start with '#' are comments.
The list of week-days is separated by a comma, and ends with a colon, after which the time format for the start is listed in the format hour:minute:second. Week-day, hour, minute and second are optional fields, but if one of these fields is used, the fields coming before it in the sequence (week-day, hour, minute, second) must be used too.

By default, schedules are read from the parameter data but optionally schedules can be read from a file. The parameter columns is used to specify which columns of the table should be produced at the output y. The first column is time, hence for the above example, set columns = {2}.

See Buildings/Resources/Data/schedule.txt for an example of the supported file format.

Extends from Modelica.Blocks.Icons.DiscreteBlock (Graphical layout of discrete block component icon).

Parameters

TypeNameDefaultDescription
Integercolumns[:]{2}Columns of the schedule to be produced at the output y. First column is time, hence value must be 2 or larger
Timet_offset0Timestamp that corresponds to midnight from Sunday to Monday [s]
Data source
BooleantableOnFilefalse= true, if table is defined on file; false if defined through parameter data
StringfileName""Filename
Stringdata"double tab1(3,2) # For week...String data with weekly schedule

Connectors

TypeNameDescription
output RealOutputy[n_columns]Schedule values

Modelica definition

model WeeklySchedule "Weekly schedule" extends Modelica.Blocks.Icons.DiscreteBlock; parameter Integer[:] columns = {2} "Columns of the schedule to be produced at the output y. First column is time, hence value must be 2 or larger"; parameter Boolean tableOnFile=false "= true, if table is defined on file; false if defined through parameter data"; parameter String fileName = "" "Filename"; parameter String data = "double tab1(3,2) # For week-day, output 1 between 7:00 and 19:00, and 0 otherwise mon,tue,wed,thu,fri:7:00:00 1 mon,tue,wed,thu,fri:19:00:00 0 # For week-end, output always 0. Note that this line is redundant in this example. sat,sun:0:00:00 0" "String data with weekly schedule"; parameter Modelica.Units.SI.Time t_offset=0 "Timestamp that corresponds to midnight from Sunday to Monday"; Modelica.Blocks.Interfaces.RealOutput[n_columns] y= {getCalendarValue(cal, iCol-1, time) for iCol in columns} "Schedule values"; protected Buildings.Utilities.IO.Files.BaseClasses.WeeklyScheduleObject cal= Buildings.Utilities.IO.Files.BaseClasses.WeeklyScheduleObject(tableOnFile, sourceName, t_offset, data) "Schedule object"; final parameter Integer n_columns = size(columns,1) "Number of columns"; parameter String sourceName = if tableOnFile then fileName else getInstanceName() +".data"; pure function getCalendarValue "Returns the interpolated (zero order hold) value" extends Modelica.Icons.Function; input Buildings.Utilities.IO.Files.BaseClasses.WeeklyScheduleObject ID "Pointer to file writer object"; input Integer iCol "Column index"; input Real timeIn "Time for look-up"; output Real y "Schedule value"; external "C" y=getScheduleValue(ID, iCol, timeIn); end getCalendarValue; end WeeklySchedule;