# Running simulations¶

This module contains the class Simulator that can be used to run Modelica simulations using Dymola.

class buildingspy.simulate.Simulator.Simulator(modelName, simulator, outputDirectory=u'.', packagePath=None)

Class to simulate a Modelica model.

Parameters: modelName – The name of the Modelica model. simulator – The simulation engine. Currently, the only supported value is dymola. outputDirectory – An optional output directory. packagePath – An optional path where the Modelica package.mo file is located.

If the parameter outputDirectory is specified, then the output files and log files will be moved to this directory when the simulation is completed. Outputs from the python functions will be written to outputDirectory/BuildingsPy.log.

If the parameter packagePath is specified, the Simulator will copy this directory and all its subdirectories to a temporary directory when running the simulations.

Note

Up to version 1.4, the environmental variable MODELICAPATH has been used as the default value. This has been changed as MODELICAPATH can have multiple entries in which case it is not clear what entry should be used.

addModelModifier(modelModifier)

Parameters: dictionary – A model modifier.
Usage: Type
>>> from buildingspy.simulate.Simulator import Simulator
>>> s=Simulator("myPackage.myModel", "dymola", packagePath="buildingspy/tests/MyModelicaLibrary")
>>> s.addModelModifier('redeclare package MediumA = Buildings.Media.IdealGases.SimpleAir')


This method adds a model modifier. The modifier is added to the list of model parameters. For example, the above statement would yield the command simulateModel(myPackage.myModel(redeclare package MediumA = Buildings.Media.IdealGases.SimpleAir), startTime=...

addParameters(dictionary)

Adds parameter declarations to the simulator.

Parameters: dictionary – A dictionary with the parameter values
Usage: Type
>>> from buildingspy.simulate.Simulator import Simulator
>>> s=Simulator("myPackage.myModel", "dymola", packagePath="buildingspy/tests/MyModelicaLibrary")
>>> s.addParameters({'PID.k': 1.0, 'valve.m_flow_nominal' : 0.1})


This will add the three parameters PID.k, valve.m_flow_nominal and PID.t to the list of model parameters.

For parameters that are arrays, use a syntax such as
>>> from buildingspy.simulate.Simulator import Simulator
>>> s = Simulator("MyModelicaLibrary.Examples.Constants", "dymola", packagePath="buildingspy/tests/MyModelicaLibrary")
>>> s.addParameters({'const2.k' : [[1.1, 1.2], [2.1, 2.2], [3.1, 3.2]]})


Do not use curly brackets for the values of parameters, such as s.addParameters({'const1.k' : {2, 3}}) as Python converts this entry to {'const1.k': set([2, 3])}.

addPostProcessingStatement(command)

Adds a post-processing statement to the simulation script.

Parameters: statement – A script statement.

This will execute command after the simulation, and before the log file is written.

addPreProcessingStatement(command)

Adds a pre-processing statement to the simulation script.

Parameters: command – A script statement.
Usage: Type
>>> from buildingspy.simulate.Simulator import Simulator
>>> s=Simulator("myPackage.myModel", "dymola", packagePath="buildingspy/tests/MyModelicaLibrary")


This will execute the two statements after the openModel and before the simulateModel statement.

deleteLogFiles()

Deletes the log files of the Python simulator, e.g. the files BuildingsPy.log, run.mos and simulator.log.

deleteOutputFiles()

Deletes the output files of the simulator.

deleteSimulateDirectory()

Deletes the simulate directory. Can be called when simulation failed.

deleteTranslateDirectory()

Deletes the translate directory. Called after simulate_translated

exitSimulator(exitAfterSimulation=True)

This function allows avoiding that the simulator terminates.

Parameters: exit – Set to False to avoid the simulator from terminating after the simulation.

This function is useful during debugging, as it allows to keep the simulator open after the simulation in order to inspect results or log messages.

getOutputDirectory()

Returns the name of the output directory.

Returns: The name of the output directory.
getPackagePath()

Returns the path of the directory containing the Modelica package.

Returns: The path of the Modelica package directory.
getParameters()

Returns a list of parameters as (key, value)-tuples.

Returns: A list of parameters as (key, value)-tuples.
Usage: Type
>>> from buildingspy.simulate.Simulator import Simulator
>>> s=Simulator("myPackage.myModel", "dymola", packagePath="buildingspy/tests/MyModelicaLibrary")
>>> s.addParameters({'PID.k': 1.0, 'valve.m_flow_nominal' : 0.1})
>>> s.getParameters()
[(u'valve.m_flow_nominal', 0.1), (u'PID.k', 1.0)]

getSimulatorSettings()

Returns a list of settings for the parameter as (key, value)-tuples.

Returns: A list of parameters (key, value) pairs, as 2-tuples.

This method is deprecated. Use getParameters() instead.

printModelAndTime()

Prints the current time and the model name to the standard output.

This method may be used to print logging information.

setNumberOfIntervals(n)

Sets the number of output intervals.

Parameters: n – The number of output intervals.

The default is unspecified, which defaults by Dymola to 500.

setOutputDirectory(outputDirectory)

Sets the name of the output directory.

Returns: The name of the output directory.
setPackagePath(packagePath)

Set the path specified by packagePath.

Parameters: packagePath – The path where the Modelica package to be loaded is located.

It first checks whether the path exists and whether it is a directory. If both conditions are satisfied, the path is set. Otherwise, a ValueError is raised.

setResultFile(resultFile)

Sets the name of the result file (without extension).

Parameters: resultFile – The name of the result file (without extension).
setSolver(solver)

Sets the solver.

Parameters: solver – The name of the solver.

setStartTime(t0)

Sets the start time.

Parameters: t0 – The start time of the simulation in seconds.

The default start time is 0.

setStopTime(t1)

Sets the start time.

Parameters: t1 – The stop time of the simulation in seconds.

The default stop time is 1.

setTimeOut(sec)

Sets the time out after which the simulation will be killed.

Parameters: sec – The time out after which the simulation will be killed.

The default value is -1, which means that the simulation will never be killed.

setTolerance(eps)

Sets the solver tolerance.

Parameters: eps – The solver tolerance.

The default solver tolerance is 1E-6.

showGUI(show=True)

Call this function to show the GUI of the simulator.

By default, the simulator runs without GUI

showProgressBar(show=True)

Enables or disables the progress bar.

Parameters: show – Set to false to disable the progress bar.

If this function is not called, then a progress bar will be shown as the simulation runs.

simulate()

Simulates the model.

This method
1. Deletes dymola output files
2. Copies the current directory, or the directory specified by the packagePath parameter of the constructor, to a temporary directory.
3. Writes a Modelica script to the temporary directory.
4. Starts the Modelica simulation environment from the temporary directory.
5. Translates and simulates the model.
6. Closes the Modelica simulation environment.
7. Copies output files and deletes the temporary directory.

This method requires that the directory that contains the executable dymola is on the system PATH variable. If it is not found, the function returns with an error message.

simulate_translated()

Simulates a translated model or a copy of it, which is especially useful for a large amount of simulations of the same model. This method is usually called after the translate() command.

This method
1. Deletes dymola output files
2. Copies the temporary translate directory to a temporary directory.
3. Writes a Modelica script to the temporary directory.
4. Starts the Modelica simulation environment from the temporary directory.
5. Simulates the translated model.
6. Closes the Modelica simulation environment.
7. Copies output files and deletes the temporary directory.
Usage: Type
>>> from buildingspy.simulate.Simulator import Simulator
>>> s = Simulator("MyModelicaLibrary.MyModel", "dymola", packagePath="buildingspy/tests/MyModelicaLibrary")
>>> s.addModelModifier("redeclare Modelica.Blocks.Sources.Step source(offset=-0.1, height=1.1, startTime=0.5)")
>>> s.setSolver("dassl")
>>> s.translate()
>>> s.simulate_translated()
>>> s.deleteTranslateDirectory()

translate()

Translates the model. Usually followed by the command simulate_translated().

This method
1. Deletes dymola output files
2. Copies the current directory, or the directory specified by the packagePath parameter of the constructor, to a temporary directory.
3. Writes a Modelica script to the temporary directory.
4. Starts the Modelica simulation environment from the temporary directory.
5. Translates the model.
6. Closes the Modelica simulation environment.
7. Keeps translated output files in the temporary directory.

This method requires that the directory that contains the executable dymola is on the system PATH variable. If it is not found, the function returns with an error message.