{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Compare :class:`ElasticImpact` and :class:`WP2`\nIn the case of the impact of an elastic striker on a bar, compare the analytical\nresult given by :class:`ElasticImpact` and the numerical result computed with\n:class:`WP2`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom elwaspatid import WP2, BarSet, ElasticImpact"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define material parameters\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "E = 201e9  # Young modulus [Pa]\nrho = 7800  # Density [kg/m3]\nd1 = 0.02  # bar/stricker diameter [m]\nd2 = 0.03  # bar/stricker diameter [m]\nL = 1.  # length of the striker [m]\nVo = 5.  # intial striker velocity [m/s]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Analytical result with :class:`ElasticImpact`\nThree possible cases:\n\n1. striker and bar have the same diameter\n2. striker smaller than bar\n3. striker larger than bar\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "time = np.linspace(-10e-6, 10e-3, num=1000)\n\nEI = ElasticImpact(E, rho, d1, L, Vo)\nEI.computeImpact(time)  # time array must be given to the method\nEI.plotForce('compare', label='ds=db')\n\nEJ = ElasticImpact(E, rho, d=[d1, d2])\nEJ.computeImpact(time)\nEJ.plotForce('compare', label='ds<db')\n\nEK = ElasticImpact(E, rho, d=[d2, d1])\nEK.computeImpact(time)\nEK.plotForce('compare', label='ds>db')\n_ = plt.legend()\n\nEK.plotRn()  # plot amplitude of successive steps"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Numerical result with :class:`WP2`\nBar configuration: one striker and one bar at rest\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "striker = BarSet([E, E], [rho, rho], [L, .2], [d2, d1], nmin=6)\ntestk = WP2(striker, nstep=400, left='free', right='infinite', Vinit=Vo)\ntestk.plot('striker')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get force at at impacted side of the bar\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "f1, v1, x1, ind1 = testk.getSignal(x=0, iseg=1, plot=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Compare analytical and numerical solutions\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure()\nplt.plot(testk.time, f1, '-', label='num')\nplt.plot(time, -EK.force, '--', label='ana')\nplt.legend()\nplt.xlim(xmax=2.7e-3)\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
}