{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Workflow automation tools\n", "=========================\n", "\n", "Running a single simulation with PyFMI\n", "--------------------------------------\n", "\n", "This page shows how to compile and run a Modelica model in Python, using the FMI standard.\n", "\n", "The [PyFMI](https://pypi.python.org/pypi/PyFMI) package is required, which is only compatible with Python 2.7. The easiest way for a functional PyFMI framework is to install the [JModelica platform](http://jmodelica.org/) and to run Python scripts from its IPython or pylab console. The JModelica installer includes a Python 2.7 distribution, which you can use in addition to an eventual other one.\n", "\n", "This exercise requires the [Annex60 Modelica library](https://github.com/iea-annex60/modelica-annex60). Before running the code, the `ppath` variable below should point to the location of this library on your drive." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#==============================================================================\n", "# Compile the FMU from the .mo file\n", "#==============================================================================\n", "from pymodelica import compile_fmu\n", "ppath = 'C:\\\\path_to_the_annex60_library_on_your_drive'\n", "model_name = 'Annex60.Controls.Continuous.Examples.PIDHysteresis'\n", "fmu1 = compile_fmu(model_name, ppath)\n", "\n", "\n", "#==============================================================================\n", "# Loading the FMU\n", "#==============================================================================\n", "# Without JModelica, you can skip the part above and load a .fmu file that\n", "# has been generated by another simulator\n", "\n", "from pyfmi import load_fmu\n", "PID = load_fmu('Buildings_Controls_Continuous_Examples_PIDHysteresis.fmu')\n", "\n", "# Choice of the time discretisation\n", "tStart = 0\n", "tStop = 3600*24\n", "\n", "# Simulation\n", "Tset_init = PID.get('TSet.k')\n", "PID.set('TSet.k', 273.15 + 40)\n", "PID_res = PID.simulate(tStart, tStop)\n", "\n", "# Extract output values from the dictionary PID_res\n", "t = PID_res['time']\n", "T = PID_res['temSen.T']\n", "y = PID_res['con.y']\n", "\n", "#==============================================================================\n", "# Plotting results\n", "#==============================================================================\n", "\n", "import matplotlib.pyplot as plt\n", "fig = plt.figure()\n", "\n", "ax = fig.add_subplot(211) \n", "ax.plot(t/3600, T-273.15, '-k', linewidth = 1.5, label = 'T')\n", "ax.set_xlabel('Time (h)')\n", "ax.set_ylabel('Temperature (C)')\n", "ax.legend()\n", "\n", "ax = fig.add_subplot(212)\n", "ax.plot(t/3600, y, '-b', linewidth = 1.5, label = 'y')\n", "ax.set_xlabel('Time (h)')\n", "ax.set_ylabel('y')\n", "ax.legend(loc = 'lower right')\n", "\n", "plt.savefig('JModelica_Ex1_plot.png')" ] } ], "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 }