Examples — BuildingsPy documentation

### Navigation

- index
- modules |
- next |
- previous |
- Home > BuildingsPy

# Examples

## Running a Simulation

This module provides an example that illustrates the use of the python to run a Modelica simulation.

The module `buildingspy.simulate.Simulator` that can be used to automate running simulations. For example, to run the model `Buildings.Controls.Continuous.Examples.PIDHysteresis.mo` with controller parameters `con.eOn = 1` and `con.eOn = 5`, use the following commands:

    from multiprocessing import Pool
    import buildingspy.simulate.Simulator as si

    # Function to set common parameters and to run the simulation
    def simulateCase(s):
        ''' Set common parameters and run a simulation.

        :param s: A simulator object.

        '''
        s.setStopTime(86400)
        # Kill the process if it does not finish in 1 minute
        s.setTimeOut(60)
        s.showProgressBar(False)
        s.printModelAndTime()
        s.simulate()

    def main():
        ''' Main method that configures and runs all simulations
        '''
        # Build list of cases to run
        li = []
        # First model
        model = 'Buildings.Controls.Continuous.Examples.PIDHysteresis'
        s = si.Simulator(model, 'dymola', 'case1')
        s.addParameters({'con.eOn': 0.1})
        li.append(s)
        # second model
        s = si.Simulator(model, 'dymola', 'case2')
        s.addParameters({'con.eOn': 1})
        li.append(s)

        # Run all cases in parallel
        po = Pool()
        po.map(simulateCase, li)

    # Main function
    if __name__ == '__main__':
        main()

This will run the two test cases and store the results in the directories `case1` and `case2`.

## Plotting of Time Series

This module provides an example that illustrates the use of the python to plot results from a Dymola simulation. See also the class `buildingspy.io.postprocess.Plotter` for more advanced plotting.

The file `plotResult.py` illustrates how to plot results from a Dymola output file. To run the example, proceed as follows:

> 1. Open a terminal or dos-shell.

> 2. Set the PYTHONPATH environment variables to the directory that contains ``buildingspy`` as a subdirectory, such as

>        cd buildingspy/examples/dymola
>        export PYTHONPATH=${PYTHONPATH}:../../..

> 3. Type

>        python plotResult.py

This will execute the script `plotResult.py`, which contains the following instructions:

    from buildingspy.io.outputfile import Reader
    import matplotlib.pyplot as plt
    import os

    # Optionally, change fonts to use LaTeX fonts
    #from matplotlib import rc
    #rc('text', usetex=True)
    #rc('font', family='serif')

    # Read results
    ofr1=Reader(os.path.join("buildingspy", "examples", "dymola", "case1", "PIDHysteresis.mat"), "dymola")
    ofr2=Reader(os.path.join("buildingspy", "examples", "dymola", "case2", "PIDHysteresis.mat"), "dymola")
    (time1, T1) = ofr1.values("cap.T")
    (time1, y1) = ofr1.values("con.y")
    (time2, T2) = ofr2.values("cap.T")
    (time2, y2) = ofr2.values("con.y")

    # Plot figure
    fig = plt.figure()
    ax = fig.add_subplot(211)

    ax.plot(time1/3600, T1-273.15, 'r', label='$T_1$')
    ax.plot(time2/3600, T2-273.15, 'b', label='$T_2$')
    ax.set_xlabel('time [h]')
    ax.set_ylabel('temperature [$^\circ$C]')
    ax.set_xticks(range(25))
    ax.set_xlim([0, 24])
    ax.legend()
    ax.grid(True)

    ax = fig.add_subplot(212)
    ax.plot(time1/3600, y1, 'r', label='$y_1$')
    ax.plot(time2/3600, y2, 'b', label='$y_2$')
    ax.set_xlabel('time [h]')
    ax.set_ylabel('y [-]')
    ax.set_xticks(range(25))
    ax.set_xlim([0, 24])
    ax.legend()
    ax.grid(True)

    # Save figure to file
    plt.savefig('plot.pdf')
    plt.savefig('plot.png')

    # To show the plot on the screen, uncomment the line below
    #plt.show()

The script generates the following plot:

### Navigation

- index
- modules |
- next |
- previous |
- Home > BuildingsPy