{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "ModelicaRes Advanced Topics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This IPython notebook demonstrates some of the advanced features and use cases of [ModelicaRes](http://kdavies4.github.io/ModelicaRes)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Table of contents:**\n", "- [Sankey diagrams](#Sankey-diagrams)\n", "- [Testing simulations based on criteria](#Testing-simulations-based-on-criteria)\n", "- [Indexing lists of simulations](#Indexing-lists-of-simulations)\n", "- [Speed considerations](#Speed-considerations)\n", "- [Contributing](#Contributing)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we'll load the [ModelicaRes](http://kdavies4.github.io/ModelicaRes) classes we'll need:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from modelicares import SimRes, SimResList" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "and some standard modules and settings for this IPython notebook:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from pandas import DataFrame\n", "%matplotlib inline\n", "%precision 3" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "u'%.3f'" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Sankey diagrams" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[SimRes](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimRes) has a built-in method ([sankey](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimRes.sankey)) to produce Sankey diagrams. We'll plot subfigures with the Sankey diagrams of [ThreeTanks](https://build.openmodelica.org/Documentation/Modelica.Fluid.Examples.Tanks.ThreeTanks.html) at several times over the simulation:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sim = SimRes('ThreeTanks.mat')\n", "sim.sankey(title=\"Sankey Diagrams of Modelica.Fluid.Examples.Tanks.ThreeTanks\",\n", " times=[0, 50, 100, 150], n_rows=2, format='%.1f ',\n", " names=['tank1.ports[1].m_flow', 'tank2.ports[1].m_flow',\n", " 'tank3.ports[1].m_flow'],\n", " labels=['Tank 1', 'Tank 2', 'Tank 3'],\n", " orientations=[-1, 0, 1],\n", " scale=0.1, margin=6, offset=1.5,\n", " pathlengths=2, trunklength=10);" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "reduce() of empty sequence with no initial value", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msim\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSimRes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ThreeTanks.mat'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m sim.sankey(title=\"Sankey Diagrams of Modelica.Fluid.Examples.Tanks.ThreeTanks\",\n\u001b[1;32m 3\u001b[0m \u001b[0mtimes\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m50\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m150\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_rows\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'%.1f '\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m names=['tank1.ports[1].m_flow', 'tank2.ports[1].m_flow',\n\u001b[1;32m 5\u001b[0m 'tank3.ports[1].m_flow'],\n", "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/ModelicaRes-0.12.2_96_gcc11e6e_dirty-py2.7.egg/modelicares/simres.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, fname, constants_only, tool)\u001b[0m\n\u001b[1;32m 931\u001b[0m '(\"%s\").' % (tool,\n\u001b[1;32m 932\u001b[0m '\", \"'.join(list(readerdict))))\n\u001b[0;32m--> 933\u001b[0;31m \u001b[0mvariables\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconstants_only\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 934\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvariables\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 935\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/ModelicaRes-0.12.2_96_gcc11e6e_dirty-py2.7.egg/modelicares/_io/dymola.pyc\u001b[0m in \u001b[0;36mreadsim\u001b[0;34m(fname, constants_only)\u001b[0m\n\u001b[1;32m 348\u001b[0m for description, [data_set, sign_col] in zip(data['description'],\n\u001b[1;32m 349\u001b[0m data['dataInfo'][:, 0:2]):\n\u001b[0;32m--> 350\u001b[0;31m \u001b[0munit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdisplay_unit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdescription\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mparse_description\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdescription\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 351\u001b[0m \u001b[0mnegated\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msign_col\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 352\u001b[0m \u001b[0mtraj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrajectories\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mdata_set\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/ModelicaRes-0.12.2_96_gcc11e6e_dirty-py2.7.egg/modelicares/_io/dymola.pyc\u001b[0m in \u001b[0;36mparse_description\u001b[0;34m(description)\u001b[0m\n\u001b[1;32m 305\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 306\u001b[0m \u001b[0mdisplay_unit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdisplayUnit\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdisplayUnit\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0munit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 307\u001b[0;31m \u001b[0munit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mU\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_units\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mExponents\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0munit\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# TODO: skip if units included\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 308\u001b[0m \u001b[0mdescription\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdescription\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 309\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mPY2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/natu-0.1.0_b_7_gfb200b7_dirty-py2.7.egg/natu/core.pyc\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **factors)\u001b[0m\n\u001b[1;32m 1378\u001b[0m \u001b[0mfactors\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mUnitExponents\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1379\u001b[0m \u001b[0mfactors\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mbase\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0mexp\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mbase\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexp\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mfactors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1380\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfactors\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1381\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1382\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mload_ini\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfiles\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: reduce() of empty sequence with no initial value" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unfortunately, the formatting arguments (`scale`, `margin`, `offset`, `pathlengths`, and `trunklength`) usually require manual adjustment." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Testing simulations based on criteria" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As demonstrated in the tutorial, [ModelicaRes](http://kdavies4.github.io/ModelicaRes) has a special class for lists of simulation results. We'll use it to load a group of files by wildcard: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims = SimResList('*.mat', '*/*/*.mat')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see which simulations are in the list:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(sims)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The directory contained some linearization results, but they were excluded automatically." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's see which variables are available:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims.names" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Only time is available! [names](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimResList.names)() only returns the variables that the simulations have in common, and there are no others. We'll address that by filtering the list of simulations. To do so, we'll first introduce the concept of a test condition. We'll create a test that checks if a simulation has a variable named \"L.L\" (indicating that an inductor \"L\" is at the base of the model):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "has_inductor = lambda sim: 'L.L' in sim" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see if each simulation passes the test:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "[has_inductor(sim) for sim in sims]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There's actually another way to access the same information: [unique_names](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimResList.unique_names)(). It returns a dictionary of the variable names that aren't in all of the simulations. The value of each entry is a Boolean list indicating if the variable is in each simulation. For example, we can do:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims.unique_names['L.L']" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Anyway, let's go ahead and filter our simulation list to those that have the inductor: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims = SimResList(filter(has_inductor, sims))\n", "print(sims)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The [ThreeTanks](https://build.openmodelica.org/Documentation/Modelica.Fluid.Examples.Tanks.ThreeTanks.html) example has been removed. Now the [names](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimResList.names) method returns so many variables that we'll use a pattern to limit it:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims.find('L*v')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, we could have excluded the [ThreeTanks](https://build.openmodelica.org/Documentation/Modelica.Fluid.Examples.Tanks.ThreeTanks.html) example in the first place, but there may be situations where we have a directory of simulation results that we need to filter. We could have also used a test condition that looks at the values of constants, e.g., " ] }, { "cell_type": "code", "collapsed": false, "input": [ "has_small_inductance = lambda sim: 'L.L' in sim and sim['L.L'].value() < 14" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This idea isn't limited to Boolean test functions. We could just as easily map a cost function to a list of simulations." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Indexing lists of simulations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As mentioned in the [tutorial](http://nbviewer.ipython.org/github/kdavies4/ModelicaRes/blob/release/examples/advanced.ipynb), the get item method can be used on a [SimResList](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimResList) of simulations to retrieve an attribute across all of the simulations, e.g., " ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims['L.L'].value()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, the method is overloaded so that it can still be used to index a simulation from the list of simulations:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims[0]['L.L'].value()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, the first index was for the simulation result (a [SimRes](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimRes) instance) and the second was for the variable within the result." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The [in operator](https://docs.python.org/2/library/operator.html#operator.__contains__) is also overloaded. It can be used to check if a variable is in all of the simulations:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'L.L' in sims" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or if a simulation is in the list of simulations:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims[0] in sims" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, it is not appropriate to overload some Python [list methods](https://docs.python.org/2/tutorial/datastructures.html) in this context. Those that are not overloaded (e.g.,\n", "[insert](https://docs.python.org/2/tutorial/datastructures.html),\n", "[remove](https://docs.python.org/2/tutorial/datastructures.html),\n", "[pop](https://docs.python.org/2/tutorial/datastructures.html),\n", "[index](https://docs.python.org/2/tutorial/datastructures.html), and\n", "[count](https://docs.python.org/2/tutorial/datastructures.html)) only accept integer indices or the actual list elements ([SimRes](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimRes) instances) as applicable, not variable names." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is possible to use slices to extract a [SimResList](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimResList) with selected simulations:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(sims[:2])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare this to the last `print(sims)` in the [previous section](#Testing-simulations-based-on-criteria)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the call method is not available for a list of simulations (only for a single simulation; see [\\_\\_call\\_\\_](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimRes.__call__)). The return value would be too confusing. However, it's possible to retrieve information from multiple variables manually, e.g.," ] }, { "cell_type": "code", "collapsed": false, "input": [ "{\"Final value of \" + name: sims[name].FV() for name in ['C1.v', 'C2.v', 'L.v']}" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we iterated over the variables. For a single simulation we could do this automatically:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sims[0](['C1.v', 'C2.v', 'L.v']).FV()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where these values form the first \"column\" of the previous dictionary." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Speed considerations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the [tutorial](http://nbviewer.ipython.org/github/kdavies4/ModelicaRes/blob/release/examples/advanced.ipynb), we saw two ways to access variables: the get item method (square brackets) and the call method (parentheses). The get item method allows access to only one variable at a time but is a little faster:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sim = SimRes('ChuaCircuit.mat')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "timeit sim['L.i'].values()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100000 loops, best of 3: 7.35 \u00b5s per loop\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "timeit sim(['L.i']).values()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100000 loops, best of 3: 10.1 \u00b5s per loop\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both of these approaches consist of two steps: accessing the variable (`sim['L.i']` or `sim('L.i')`) and reading the values. The first step takes almost as much time as the second, but it only needs to be performed once. If we need to access a variable or a group of variables multiple times (to get their values, units, times, etc.), it's best to assign a variable to the entry or entries:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Li = sim['L.i']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the first step out of the way, we can now retrieve information more quickly:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "timeit Li.values()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100000 loops, best of 3: 6.87 \u00b5s per loop\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same holds for the call method. We'll access the voltages of all of the components in the [ChuaCircuit](http://reference.wolfram.com/system-modeler/libraries/Modelica/Modelica_Electrical_Analog_Examples_ChuaCircuit.html):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "voltages = sim.find('^[^.]*.v$', re=True)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running both steps at once " ] }, { "cell_type": "code", "collapsed": false, "input": [ "timeit sim(voltages).values()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10000 loops, best of 3: 50.3 \u00b5s per loop\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "takes longer than the second step alone:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "v = sim(voltages)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "timeit v.values()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10000 loops, best of 3: 44.6 \u00b5s per loop\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "As shown in the plot below, the simulation file loading time is fairly quick -- about one fifth of a second for a 3 MB file. The `constants_only` initialization option of [SimRes](http://kdavies4.github.io/ModelicaRes/modelicares.simres.html#modelicares.simres.SimRes) saves about 15 to 25% of the load time and may be useful if it is only necessary to read parameters." ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = DataFrame({'file size / B':[22273, 34990, 43027, 278277, 347461, \n", " 2332811, 3088223],\n", " 'load time / ms': [4.24, 2.97, 2.43, 13.6, 36.4, \n", " 68.6, 197]})\n", "plt.plot(d['file size / B']/1e6, d['load time / ms'], 'o--')\n", "plt.title(\"File loading time using ModelicaRes\\n\"\n", " \"Samsung ATIV Book 8, Intel Core i7-3635QM, Ubuntu 14.04\")\n", "plt.xlabel('File size / MB')\n", "plt.ylabel('Loading time / ms')\n", "plt.grid(True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There appears to be about a 60% memory overhead associated with the data. A 278.3 kB file took approximately 4.5 GB of system memory when loaded 10,000 times:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "4.5e9/(1e4*278.3e3) - 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Contributing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you've seen the main features of [ModelicaRes](http://kdavies4.github.io/ModelicaRes) besides the [exps](http://kdavies4.github.io/ModelicaRes/modelicares.exps.html) module (tools to help set up and run simulation experiments). If there is a compelling use case or feature you'd like to see added, please consider developing it yourself and sharing it by a pull request to the ``master`` branch of the [GitHub repository](https://github.com/kdavies4/ModelicaRes). The [ModelicaRes](http://kdavies4.github.io/ModelicaRes) source code is well documented and organized to allow expansion." ] } ], "metadata": {} } ] }