{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "***Note: this is the svgBondGraph.ipynb notebook. The\n", "PDF version \"The svgBondGraph module\"\n", "is available [here](svgBondGraph.pdf).***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "Bond graphs can be represented graphically in scalar vector graphics (SGV) format. Such representations can be generated ab initio using an editor such as xfig or inkscape, or can be generated from legacy MTT files in fig format. This document describes how bond graphs in svg format can be converted to __[BondGraphTools](http://pypi.org/project/BondGraphTools/)__ format." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Converting from fig to svg\n", "Legacy bond graph graphical representation generated by xfig are .fig files.\n", ".fig graphics can be converted to .svg graphics in various ways including via inkscape. However, fig2dev provides a simple conversion approach:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "!fig2dev -Lsvg RC_abg.fig > RC_abg.svg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This gives the svg representation:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "r\n", "\n", "c\n", "\n", "Bond graph\n", "\n", "Schematic\n", "\n", "v\n", "\n", "1\n", "\n", "i\n", "\n", "1\n", "\n", "i\n", "\n", "2\n", "\n", "v\n", "\n", "2\n", "\n", "v\n", "\n", "2\n", "\n", "i\n", "\n", "2\n", "\n", "i\n", "\n", "1\n", "\n", "v\n", "\n", "1\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "0\n", "\n", "R:r\n", "\n", "C:c\n", "\n", "Sf:i2\n", "\n", "Se:v1\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import IPython.display as disp\n", "disp.SVG('RC_abg.svg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Converting from graphical to computational representation\n", "SVG files are in XML format and, as such, can be parsed using python modules such as **lxml** and **svgpathtools**.\n", "**svgBondGraph** provides the tools to convert bond graphs in svg format to bond graphs in __[BondGraphTools](http://pypi.org/project/BondGraphTools/)__ format." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import svgBondGraph as sbg\n", "sbg.model('RC_abg.svg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This generates the file ABCDE_abg.py which can be imported as usual. (Note that, by default, the BG itself is distinguished from decoration by colour: the default includes black and red)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import BondGraphTools as bgt\n", "import sympy as sp\n", "\n", "def model():\n", " \"\"\" Acausal bond graph RC_abg.py\n", " Created by svgBondGraph at Tue Dec 18 11:03:34 2018 from RC_abg.svg\n", "\n", " Usage:\n", " import RC_abg; model = RC_abg.model()\n", " \"\"\"\n", "\n", " model = bgt.new(name=\"RC\")\n", "\n", " ## Junction 0:MTT1\n", " MTT1 = bgt.new('0')\n", "\n", " ## Junction 1:MTT0\n", " MTT0 = bgt.new('1')\n", "\n", " ## Component C:c\n", " c = sp.symbols('c')\n", " c = bgt.new('C',name='c',value={'C':c})\n", "\n", " ## Component R:r\n", " r = sp.symbols('r')\n", " r = bgt.new('R',name='r',value={'r':r})\n", "\n", " ## Component Se:v1\n", " v1 = bgt.new('Se',name='v1')\n", "\n", " ## Component Sf:i2\n", " i2 = bgt.new('Sf',name='i2')\n", "\n", " ## Component list\n", " components = (\n", " MTT1,\n", " MTT0,\n", " c,\n", " r,\n", " v1,\n", " i2 \n", " )\n", "\n", " bgt.add(model, *components)\n", "\n", " ## Bonds\n", " bgt.connect(v1,MTT0)\n", " bgt.connect(MTT0,MTT1)\n", " bgt.connect(MTT0,r)\n", " bgt.connect(MTT1,c)\n", " bgt.connect(MTT1,i2)\n", "\n", " return model\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ABCDE_abg.py can be imported and analysed using BondGraphTools.\n", "For example:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on module RC_abg:\n", "\n", "NAME\n", " RC_abg\n", "\n", "FUNCTIONS\n", " model()\n", " Acausal bond graph RC_abg.py\n", " Created by svgBondGraph at Sun Oct 27 18:48:02 2019 from RC_abg.svg\n", " \n", " Usage:\n", " import RC_abg; model = RC_abg.model()\n", "\n", "FILE\n", " /home/peterg/WORK/Research/SystemsBiology/Notes/2018/BGtools/Notebooks/RC_abg.py\n", "\n", "\n" ] }, { "data": { "text/plain": [ "[dx_0 - u_1 - u_0/r + x_0/(c*r)]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import RC_abg; model = RC_abg.model(); help(RC_abg)\n", "model.constitutive_relations\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'u_0': (SS: v1, 'e'), 'u_1': (SS: i2, 'f')}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.control_vars" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can, for example, be simulated for numerical parameter values." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A biomolecular example." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "!fig2dev -Lsvg ABCDE_abg.fig > ABCDE_abg.svg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This gives the svg representation:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "0\n", "\n", "0\n", "\n", "1\n", "\n", "1\n", "\n", "1\n", "\n", "1\n", "\n", "Ce:E3\n", "\n", "Ce:E4\n", "\n", "Ce:E1\n", "\n", "Ce:A\n", "\n", "Ce:B\n", "\n", "Ce:C\n", "\n", "Ce:D\n", "\n", "Re:r2\n", "\n", "Re:r4\n", "\n", "Ce:E2\n", "\n", "Re:r1\n", "\n", "Re:r3\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "disp.SVG('ABCDE_abg.svg')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Converting one-port r2 to two-port\n", "Converting one-port r4 to two-port\n", "Converting one-port r1 to two-port\n", "Converting one-port r3 to two-port\n" ] } ], "source": [ "sbg.model('ABCDE_abg.svg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This generates ABCDE_abg.py which can be imported as usual and is then available for, for example, stoichiometric analysis." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import ABCDE_abg\n", "ABCDE = ABCDE_abg.model()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'x_0': (C: A, 'q_0'),\n", " 'x_1': (C: B, 'q_0'),\n", " 'x_2': (C: C, 'q_0'),\n", " 'x_3': (C: D, 'q_0'),\n", " 'x_4': (C: E1, 'q_0'),\n", " 'x_5': (C: E2, 'q_0'),\n", " 'x_6': (C: E3, 'q_0'),\n", " 'x_7': (C: E4, 'q_0')}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ABCDE.state_vars" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[K_A*K_E1*kappa_r1*x_0*x_4 - K_E2*kappa_r1*x_5 + dx_0,\n", " K_B*K_E2*kappa_r2*x_1*x_5 - K_E3*kappa_r2*x_6 + dx_1,\n", " K_C*K_E4*kappa_r3*x_2*x_7 - K_E3*kappa_r3*x_6 + dx_2,\n", " K_D*K_E1*kappa_r4*x_3*x_4 - K_E4*kappa_r4*x_7 + dx_3,\n", " K_A*K_E1*kappa_r1*x_0*x_4 + K_D*K_E1*kappa_r4*x_3*x_4 - K_E2*kappa_r1*x_5 - K_E4*kappa_r4*x_7 + dx_4,\n", " -K_A*K_E1*kappa_r1*x_0*x_4 + K_B*K_E2*kappa_r2*x_1*x_5 + K_E2*kappa_r1*x_5 - K_E3*kappa_r2*x_6 + dx_5,\n", " -K_B*K_E2*kappa_r2*x_1*x_5 - K_C*K_E4*kappa_r3*x_2*x_7 + K_E3*kappa_r2*x_6 + K_E3*kappa_r3*x_6 + dx_6,\n", " K_C*K_E4*kappa_r3*x_2*x_7 - K_D*K_E1*kappa_r4*x_3*x_4 - K_E3*kappa_r3*x_6 + K_E4*kappa_r4*x_7 + dx_7]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ABCDE.constitutive_relations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }