|
|
@@ -0,0 +1,221 @@
|
|
|
+{
|
|
|
+ "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": [
|
|
|
+ ""
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "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": [
|
|
|
+ {
|
|
|
+ "ename": "ModuleNotFoundError",
|
|
|
+ "evalue": "No module named 'BondGraphTools'",
|
|
|
+ "output_type": "error",
|
|
|
+ "traceback": [
|
|
|
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
|
|
+ "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
|
|
|
+ "\u001b[1;32m<ipython-input-1-65591fcf6218>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mBondGraphTools\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mbgt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
|
+ "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'BondGraphTools'"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "source": [
|
|
|
+ "import BondGraphTools as bgt"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "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": null,
|
|
|
+ "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": null,
|
|
|
+ "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": null,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "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": [
|
|
|
+ "\n",
|
|
|
+ "\n",
|
|
|
+ "The dampening constant should be 1."
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "\n",
|
|
|
+ "#>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",
|
|
|
+ "\n",
|
|
|
+ "#<\n"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "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
|
|
|
+}
|