{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bond Graph Modelling\n",
    "\n",
    "## Exercise 1: Mass-spring System\n",
    "\n",
    "In this exercise, you will build a bond graph model of a Mass-Spring System, illustrated below."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![MS](resources/mass-spring.png \"MS\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The $x(t)$ denotes the position of the mass over time (in meters), and $v(t) = x'(t)$ its velocity (in meters per second). \n",
    "$x'(t)$ denotes the time derivative of $x(t)$.\n",
    "\n",
    "\n",
    "The constant $m$ denotes the mass of the mass (in kg).\n",
    "\n",
    "Force $f_e(t)$ denotes an external force exerted on the mass.\n",
    "\n",
    "The spring, when relaxed, that is, when $x(t)=0$, does not exert any force on the mass. However, when the spring is displaced by $x(t)$ meters, it exerts the force of $cx(t)$ newtons on the mass, where $c$ denotes the spring stiffness constant.\n",
    "\n",
    "Complete the following code to construct the bond graph model of the MS system."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import BondGraphTools as bgt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create model\n",
    "model = bgt.new(name=\"MS\")\n",
    "\n",
    "# TODO: Create spring element\n",
    "spring = None\n",
    "#>Solution\n",
    "spring = bgt.new(\"C\", name=\"spring\", value=1.0)\n",
    "#<\n",
    "\n",
    "# TODO: Create mass element\n",
    "mass = None\n",
    "#>Solution\n",
    "mass = bgt.new(\"I\", name=\"mass\", value=1.0)\n",
    "#<\n",
    "\n",
    "# Create force element. Default value is a constant force of 1N.\n",
    "fe = bgt.new(\"Se\", name=\"fe\", value=1.0)\n",
    "\n",
    "# Create law\n",
    "law = bgt.new(\"1\")\n",
    "\n",
    "# Add all elements to the model.\n",
    "bgt.add(model, spring, mass, fe, law)\n",
    "\n",
    "# TODO: Complete the connections. Notice the direction.\n",
    "bgt.connect(law, spring)\n",
    "#>Solution\n",
    "bgt.connect(law, mass)\n",
    "bgt.connect(fe, law)\n",
    "#<"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Draw the model. May need to be run more than once to produce effect.\n",
    "bgt.draw(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run a simulation\n",
    "timespan = [0, 30]\n",
    "\n",
    "# Initial state [position, velocity]\n",
    "x0 = [0, 0]\n",
    "\n",
    "t, x = bgt.simulate(model, timespan=timespan, x0=x0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33344 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot results\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.figure(1)\n",
    "plt.plot(t,x, label=['position', 'velocity'])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2: Add a Damper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this exercise, use Bond Graphs to model a Mass-Spring-Damper system, as illustrated below."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![MSD](resources/mass-spring-damper.png \"MSD\")\n",
    "\n",
    "The dampening constant should be 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28280 characters]\n",
      "text/plain": [
       "<Figure size 960x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#>Solution\n",
    "\n",
    "model = bgt.new(name=\"MS\")\n",
    "spring = bgt.new(\"C\", name=\"spring\", value=1.0)\n",
    "damper = bgt.new(\"R\", name=\"damper\", value=1.0)\n",
    "mass = bgt.new(\"I\", name=\"mass\", value=1.0)\n",
    "fe = bgt.new(\"Se\", name=\"fe\", value=1.0)\n",
    "law = bgt.new(\"1\")\n",
    "bgt.add(model, spring, mass, damper, fe, law)\n",
    "bgt.connect(law, spring)\n",
    "bgt.connect(law, mass)\n",
    "bgt.connect(law, damper)\n",
    "bgt.connect(fe, law)\n",
    "# bgt.draw(model)\n",
    "\n",
    "#<"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12872 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "timespan = [0, 30]\n",
    "x0 = [0, 0]\n",
    "t, x = bgt.simulate(model, timespan=timespan, x0=x0)\n",
    "\n",
    "plt.figure(1)\n",
    "plt.plot(t,x, label=['position', 'velocity'])\n",
    "plt.show()"
   ]
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
