Buildings.Utilities.IO.Files.BaseClasses

Package with base classes for Buildings.Utilities.IO.Files

Information

This package contains base classes that are used to construct the models in Buildings.Utilities.IO.Files.

Extends from Modelica.Icons.BasesPackage (Icon for packages containing base classes).

Package Content

Name Description
Buildings.Utilities.IO.Files.BaseClasses.FileWriter FileWriter Partial model for writing results to a .csv file
Buildings.Utilities.IO.Files.BaseClasses.FileWriterObject FileWriterObject Class used to ensure that each CSV writer writes to a unique file
Buildings.Utilities.IO.Files.BaseClasses.JSONWriterObject JSONWriterObject Class used to ensure that each JSON writer writes to a unique file
Buildings.Utilities.IO.Files.BaseClasses.OutputTime OutputTime Time when results are outputted
Buildings.Utilities.IO.Files.BaseClasses.WeeklyScheduleObject WeeklyScheduleObject Class that loads a weekly schedule
Buildings.Utilities.IO.Files.BaseClasses.cacheVals cacheVals Function for caching results such that they can be written at destruction
Buildings.Utilities.IO.Files.BaseClasses.printRealArray printRealArray Print string to terminal or file
Buildings.Utilities.IO.Files.BaseClasses.writeJSON writeJSON Write a vector of Real variables to a JSON file

Types and constants

  type OutputTime = enumeration(
      Initial "Output results at initial() time",
      Terminal "Output results at terminal() time",
      Custom "Output results at custom time value")
      "Time when results are outputted";

Buildings.Utilities.IO.Files.BaseClasses.FileWriter Buildings.Utilities.IO.Files.BaseClasses.FileWriter

Partial model for writing results to a .csv file

Buildings.Utilities.IO.Files.BaseClasses.FileWriter

Information

Base class for a file writer. See extending classes.

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

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 FileWriter "Partial model for writing results to a .csv file" extends Modelica.Blocks.Icons.DiscreteBlock; parameter Integer nin "Number of inputs"; parameter String fileName = getInstanceName() + ".csv" "File name, including extension"; parameter Modelica.Units.SI.Time samplePeriod "Sample period: equidistant interval for which the inputs are saved"; parameter String delimiter = "\t" "Delimiter for csv file"; parameter Boolean writeHeader = true "=true, to write header with variable names, otherwise no header will be written"; parameter String[nin] headerNames = {"col"+String(i) for i in 1:nin} "Header names, indices by default"; parameter Integer significantDigits(min=1,max=15) = 6 "Number of significant digits that are used for converting inputs into string format"; Modelica.Blocks.Interfaces.RealVectorInput[nin] u "Variables that are saved"; protected parameter Boolean isCombiTimeTable = false "=true, if CombiTimeTable header should be prepended upon destruction"; parameter Modelica.Units.SI.Time t0(fixed=false) "First sample time instant"; parameter String insNam = getInstanceName() "Instance name"; Buildings.Utilities.IO.Files.BaseClasses.FileWriterObject filWri= Buildings.Utilities.IO.Files.BaseClasses.FileWriterObject( insNam, fileName, nin+1, isCombiTimeTable) "File writer object"; discrete String str "Intermediate variable for constructing a single line"; output Boolean sampleTrigger "True, if sample time instant"; function writeLine "Prepend a string to an existing text file" extends Modelica.Icons.Function; input Buildings.Utilities.IO.Files.BaseClasses.FileWriterObject id "ID of the file writer"; input String string "Written string"; input Integer isMetaData "=1, if line should not be included for row count of combiTimeTable"; external"C" writeLine(id, string, isMetaData); end writeLine; initial equation t0 = time; equation sampleTrigger = sample(t0, samplePeriod); algorithm when sampleTrigger then str :=String(time,significantDigits=significantDigits) + delimiter; for i in 1:nin-1 loop str :=str + String(u[i],significantDigits=significantDigits) + delimiter; if mod(i+1,10)==0 then // write out buffer every 10 entries to avoid overflow writeLine(filWri, str, 1); // not adding this one to row count str:=""; end if; end for; str :=str + String(u[nin],significantDigits=significantDigits) + "\n"; writeLine(filWri, str, 0); end when; end FileWriter;

Buildings.Utilities.IO.Files.BaseClasses.FileWriterObject

Class used to ensure that each CSV writer writes to a unique file

Information

Class derived from ExternalObject having two local external function definition, named destructor and constructor respectively.

Extends from ExternalObject.

Modelica definition

class FileWriterObject "Class used to ensure that each CSV writer writes to a unique file" extends ExternalObject; function constructor "Construct an extendable array that can be used to store double valuesCreate empty file" extends Modelica.Icons.Function; input String instanceName "Instance name of the file write"; input String fileName "Name of the file, including extension"; input Integer numColumns "Number of columns that are written to file"; input Boolean isCombiTimeTable "Flag to indicate whether combiTimeTable header should be prepended upon destruction"; output FileWriterObject fileWriter "Pointer to the file writer"; external"C" fileWriter = fileWriterInit(instanceName, fileName, numColumns, isCombiTimeTable); end constructor; function destructor "Release storage and close the external object" input FileWriterObject fileWriter "Pointer to file writer object"; external "C" fileWriterFree(fileWriter); end destructor; end FileWriterObject;

Buildings.Utilities.IO.Files.BaseClasses.JSONWriterObject

Class used to ensure that each JSON writer writes to a unique file

Information

Class derived from ExternalObject having two local external function definition, named destructor and constructor respectively.

Extends from ExternalObject.

Modelica definition

class JSONWriterObject "Class used to ensure that each JSON writer writes to a unique file" extends ExternalObject; function constructor "Verify whether a file writer with the same path exists and cache variable keys" extends Modelica.Icons.Function; input String instanceName "Instance name of the file write"; input String fileName "Name of the file, including extension"; input Boolean dumpAtDestruction "=true, to write cached values at destruction"; input String[:] varKeys "Keys for values that are written to file"; output JSONWriterObject jsonWriter "Pointer to the file writer"; external"C" jsonWriter = jsonWriterInit(instanceName, fileName, dumpAtDestruction, size(varKeys,1), varKeys); end constructor; function destructor "Release storage and close the external object, write data if needed" input JSONWriterObject jsonWriter "Pointer to file writer object"; external "C" jsonWriterFree(jsonWriter); end destructor; end JSONWriterObject;

Buildings.Utilities.IO.Files.BaseClasses.OutputTime

Time when results are outputted

Information

Enumeration for when output files are written.

Modelica definition

type OutputTime = enumeration( Initial "Output results at initial() time", Terminal "Output results at terminal() time", Custom "Output results at custom time value") "Time when results are outputted";

Buildings.Utilities.IO.Files.BaseClasses.WeeklyScheduleObject

Class that loads a weekly schedule

Information

Class definition for weekly schedule object used by Buildings.Utilities.IO.Files.WeeklySchedule.

Extends from ExternalObject.

Modelica definition

class WeeklyScheduleObject "Class that loads a weekly schedule" extends ExternalObject; impure function constructor "Creates an object for the weekly schedule" extends Modelica.Icons.Function; input Boolean tableOnFile "Table is on file"; input String sourceName "Data source"; input Real t_offset "When time=t_offset, the time is assumed to be monday at midnight"; input String data "Data, when tableOnFile=false"; output WeeklyScheduleObject weeklySchedule "Pointer to the weekly schedule"; external"C" weeklySchedule = weeklyScheduleInit(tableOnFile, sourceName, t_offset, data); end constructor; pure function destructor "Release storage and close the external object, write data if needed" input WeeklyScheduleObject weeklySchedule "Pointer to file writer object"; external "C" weeklyScheduleFree(weeklySchedule); end destructor; end WeeklyScheduleObject;

Buildings.Utilities.IO.Files.BaseClasses.cacheVals

Function for caching results such that they can be written at destruction

Information

Function for writing data to cache such that the results can be written at destruction.

Inputs

TypeNameDefaultDescription
JSONWriterObjectID Json writer object id
RealvarVals[:] Variable values

Modelica definition

function cacheVals "Function for caching results such that they can be written at destruction" input Buildings.Utilities.IO.Files.BaseClasses.JSONWriterObject ID "Json writer object id"; input Real[:] varVals "Variable values"; external "C" cacheVals(ID, varVals, size(varVals,1)); end cacheVals;

Buildings.Utilities.IO.Files.BaseClasses.printRealArray Buildings.Utilities.IO.Files.BaseClasses.printRealArray

Print string to terminal or file

Information

Function that prints a real array to an output file.

Extends from Modelica.Icons.Function (Icon for functions).

Inputs

TypeNameDefaultDescription
Realx[:] Input to be printed
StringfileName""File where to print (empty string is the terminal)
IntegerminimumLength1Minimum width of result
IntegersignificantDigits6Number of significant digits

Outputs

TypeNameDescription
StringoutStrString to be printed

Modelica definition

function printRealArray "Print string to terminal or file" extends Modelica.Icons.Function; input Real[:] x "Input to be printed"; input String fileName="" "File where to print (empty string is the terminal)"; input Integer minimumLength = 1 "Minimum width of result"; input Integer significantDigits = 6 "Number of significant digits"; output String outStr="" "String to be printed"; algorithm for i in 1:size(x,1) loop outStr :=outStr + " " + String(x[i], minimumLength=minimumLength, significantDigits=significantDigits); end for; Modelica.Utilities.Streams.print(string=outStr, fileName=fileName); end printRealArray;

Buildings.Utilities.IO.Files.BaseClasses.writeJSON

Write a vector of Real variables to a JSON file

Information

Function for writing data to a JSON file.

Inputs

TypeNameDefaultDescription
JSONWriterObjectID JSON writer object id
RealvarVals[:] Variable values

Modelica definition

function writeJSON "Write a vector of Real variables to a JSON file" input Buildings.Utilities.IO.Files.BaseClasses.JSONWriterObject ID "JSON writer object id"; input Real[:] varVals "Variable values"; external "C" writeJson(ID, varVals, size(varVals,1)); end writeJSON;