{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Test :class:`Waveprop` class\n\nDefine a :class:`BarSingle` bar and use it with :class:`Waveprop` to compute\nelastic wave propagation in simple test cases.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# sphinx_gallery_thumbnail_number = 5\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom elwaspatid import Waveprop, BarSingle, trapezeWave"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define material and geometrical parameters\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "E = 201e9  # Young modulus [Pa]\nrho = 7800  # Density [kg/m3]\nd = 0.020  # diameter [m]\nk = 2.4  # diamters ratio [-]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define the incident wave vector\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "incw = np.zeros(80)  # incident wave\nincw[0:20] = 1e3  # >0 means traction pulse"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the bars\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dx = 0.01  # length of an elementary Segment [m]\nn = 50  # number of Segments [-]\nD = np.ones(n) * d  # diameters of the Segments\nbb = BarSingle(dx, D, E, rho)  # constant section bar\nD2 = np.hstack((np.ones(n)*d, np.ones(n)*d*k))  # section change \nb2 = BarSingle(dx, D2, E, rho)  # cross-section increase\nb3 = BarSingle(dx, D2[::-1], E, rho)  # cross-section reduction\n\n# Visualize the bar:\nbb.plot()  # constant cross-section and constant impedance\nb2.plot()  # cross-section and impedance increase"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Free-free uniform bar\nIncident pulse reflects on both end of the bar endlessly.\nThe force at both ends of the bar is null. Traction reflects as compression.\nCompression pulse reflects as traction.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "test = Waveprop(bb, incw, nstep=2*len(incw), left='free', right='free')\ntest.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "It is possible to plot cuts of the space-time diagram, at a given time `t`\nor at a given position `x`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "test.plotcut(x=bb.x[int(n/2)])\ntest.plotcut(t=bb.dt*len(incw)/2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Additional diagrams are also available\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "test.plot(typ='dir-D')  # Wave direction (dir) and Displacement (D)\ntest.plot(typ='sig-eps')  # Stress (sig) and Strain (eps)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Free-fixed uniform bar\nLeft end is free, right end is fixed:\n\n- compression relfects as compression on fixed end;\n- then, compression reflects as traction on free end;\n- and finally traction reflects as traction on fixed end.\n\nNote that velocity and displacement of the right end are null.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "test = Waveprop(bb, incw, nstep=3*len(incw), left='free', right='fixed')\ntest.plot()\ntest.plot(typ='D')  #  Displacement (D)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Infinite-infinite uniform bar\nInfinite end amounts to anechoic condition: no reflecion of elastic wave.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "testf = Waveprop(bb, incw, nstep=100, left='infinite', right='infinite')\ntestf.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Free-free bar with section increase\nThe traction pulse reflects as traction on section increase.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "testa = Waveprop(b2, incw, nstep=170, left='free', right='free')\ntesta.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Free-free bar with section reduction\nThe traction pulse reflects as compression on the section reduction.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "testd = Waveprop(b3, incw, nstep=170, left='free', right='free')\ntestd.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Whatever pulse input is possible\nTrapeze\n^^^^^^^\nFor exemple, define a trapeze pulse shape and propagate it in a bar with \nconstant section. Right end is ``free`` so the traction wave is reflected as\na compression wave. Left end is ``infinite`` so no reflecion occur.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "trap = trapezeWave(plateau=5, rise=5)\ntestt = Waveprop(bb, trap, nstep=120, left='infinite', right='free')\ntestt.plot()\ntestt.plotcut(x=0.2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### And why not a sine pulse?\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sine = np.sin(2*np.pi*np.linspace(0, 1, num=40))\nbar = BarSingle(dx, np.ones(30)*d, E, rho)\ntests = Waveprop(bar, sine, nstep=3*len(sine), left='infinite', right='free')\ntests.plot()\ntests.plotcut(x=0.15)\ntests.plotcut(x=0.20)\n\nplt.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.6.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}