{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Workflow automation tools\n", "=========================\n", "\n", "Exercise 3 - Running several simulations without recompilation\n", "--------------------------------------------------------------\n", "\n", "This page shows how to start a series of simulations using the [BuildingsPy](http://simulationresearch.lbl.gov/modelica/buildingspy/) Python package.\n", "\n", "In addition to this package, you need the [Annex60 Modelica library](https://github.com/iea-annex60/modelica-annex60) to run it. You can simply copy and paste the code below and run it in your Python environment, or open this notebook in the Jupyter viewer.\n", "\n", "Before running the code, the `ppath` variable should point to the location of the Annex 60 library on your drive." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "#==============================================================================\n", "# Loading the model file\n", "#==============================================================================\n", "\n", "# Set the path to the Buildings library on your disk\n", "ppath = 'D:\\\\path_to_the_modelica_library_on_your_drive'\n", "# Set the path to the model inside the Buildings library\n", "model = 'Annex60.Controls.Continuous.Examples.PIDHysteresis'\n", "\n", "#==============================================================================\n", "# Translate the first model\n", "#==============================================================================\n", "\n", "import buildingspy.simulate.Simulator as si\n", "s = si.Simulator(model, 'dymola', packagePath = ppath)\n", "\n", "s.setSolver('dassl')\n", "s.showGUI(False)\n", "s.setStopTime(86400)\n", "s.setTimeOut(60)\n", "s.translate()\n", "\n", "#==============================================================================\n", "# Run a series of simulations with different integrator times\n", "#==============================================================================\n", "\n", "from copy import deepcopy\n", "Ti_list = [150, 600, 1200, 2400]\n", "\n", "for Ti, i in zip(Ti_list, range(len(Ti_list))):\n", " \n", " s_new = deepcopy(s)\n", " \n", " outputFileName = 'case'+str(i)\n", " s_new.setOutputDirectory(outputFileName)\n", " \n", " s_new.addParameters({'con.Ti': Ti})\n", " \n", " s_new.printModelAndTime()\n", " s_new.simulate_translated()\n", " \n", "#==============================================================================\n", "# Read and plot results\n", "#==============================================================================\n", "\n", "from buildingspy.io.outputfile import Reader\n", "import os\n", "import shutil\n", "import matplotlib.pyplot as plt\n", "\n", "plt.figure()\n", "for Ti, i in zip(Ti_list, range(len(Ti_list))):\n", " \n", " outputFileName = 'case'+str(i)\n", " r = Reader(os.path.join(outputFileName,'PIDHysteresis.mat'), 'dymola')\n", " \n", " (t, T) = r.values('temSen.T')\n", " (t, y) = r.values('con.y')\n", " \n", " label_Ti = 'Ti = %0.1f ' % Ti\n", " plt.plot(t/3600, T-273.15, linewidth = 1.5, label = label_Ti)\n", " \n", " shutil.rmtree(outputFileName)\n", "\n", "plt.xlabel('Time (h)')\n", "plt.ylabel('T (C)')\n", "plt.legend(loc = 'upper left', fontsize = 12)\n", "\n", "plt.savefig('ex3_plot.png')\n", "\n", "#==============================================================================\n", "# Delete translated directory (optional)\n", "#==============================================================================\n", "\n", "s.deleteTranslateDirectory()\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }