{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Fluid: Acoustic velocity plots with arrows\n\nThis example shows how to plot the acoustic velocity and also add arrows within\nIn this example we illustrate how we can use arrow plots to illustrate both\nmagnitude and direction of the acoustic particle velocity.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In this example we study a glass sphere in a viscous fluid.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from matplotlib import pyplot as plt\n\nimport osaft\n\n# --------\n# Geometry\n# --------\n# Radius\nR_0 = 50e-6  # [m]\n\n# -------------------\n# Properties of Glass\n# -------------------\n# Speed of sound\nc_1_glass = 4_521  # [m/s]\nc_2_glass = 2_226  # [m/s]\n# Density\nrho_glass = 2_240  # [kg/m^3]\n\n# ---------------------------\n# Properties of Viscous Fluid\n# ---------------------------\n# Speed of sound\nc_oil = 1_400  # [m/s]\n# Density\nrho_oil = 900  # [kg/m^3]\n# Viscosity\neta_oil = 0.04  # [Pa s]\nzeta_oil = 0  # [Pa s]\n\n# --------------------------------\n# Properties of the Acoustic Field\n# --------------------------------\n# Frequency\nf = 10e5  # [Hz]\n# Pressure\np_0 = 1e5  # [Pa]\n# Wave type\nwave_type = osaft.WaveType.STANDING\n# Position of the particle in the field\nposition = osaft.pi / 4  # [rad]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here, we are given the primary and the secondary wave speed of\nglass only. The model ``doinikov2021viscous``, however, takes the Young's\nmodulus and the Poisson's ratio as input parameters. We therefore have to\nconvert the properties. The ``ElasticSolid`` class offers such conversion\nmethods.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "E_s = osaft.ElasticSolid.E_from_wave_speed(c_1_glass, c_2_glass, rho_glass)\nnu_s = osaft.ElasticSolid.nu_from_wave_speed(c_1_glass, c_2_glass)\n\nsol = osaft.doinikov2021viscous.ScatteringField(\n    f=f,\n    R_0=R_0,\n    rho_s=rho_glass,\n    E_s=E_s,\n    nu_s=nu_s,\n    rho_f=rho_oil,\n    c_f=c_oil,\n    eta_f=eta_oil,\n    zeta_f=zeta_oil,\n    p_0=p_0,\n    wave_type=wave_type,\n    position=position,\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The next step is to initialise the plotter for the fluid.\nThe option ``n_quiver_points`` let's use decide how many arrows along the\nz-direction and x-direction shall be plotted. If you chose the plotting\noption ``symmetric=False`` then the arrows along the x-direction will be\nhalved.\nIt is set in the constructor of the plotter, because the solution for the\nquiver arrows is stored within the class and it depends on the number of\npoints. A change of this parameter will invoke new computation of the quiver\ndirections. Per default it is set to ``n_quiver_points=21``. It is advisable\nto use an odd number if you like to change the parameter because this ensures\nthat have a point at z=0, x=0.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plotter = osaft.FluidScatteringPlot(sol, r_max=3 * sol.R_0, cmap=\"Oranges\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's start by simply plotting the acoustic velocity for ``phase=0``. In\norder to also show the arrows within the plot we need to add the option\n``quiver_color='____'``. Per default ``quiver_color = None`` such that no\narrows will be overlayed.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plotter.plot_velocity(quiver_color=\"black\", symmetric=False)\nax.set_title(\"Acoustic Velocity Magnitude + Quiver\")\nfig.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can also have a look at first four modes and look how the velocity\nbehaves for each of them individually.\nWe start by creating a ``plt.subplots(...)`` with two rows and two columns.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, Axes = plt.subplots(2, 2, figsize=(12, 10))\n\n# Now we loop over each of the ``plt.Axes`` within the ``Axes`` array and\n# depict a different mode.\n\nfor i, ax in enumerate(Axes.flatten()):\n    plotter.plot_velocity(mode=i, ax=ax, quiver_color=\"black\")\n    ax.set_title(f\"Mode #{i}\")\n\nfig.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The ``quiver_color=...`` option is also available for the\n``plotter.plot_velocity_evolution(quiver_color=...)``. Let's check first if\nthe incident acoustic field is depicted right. Therefore, we set\n``incident=True`` and ``scattered=False``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plotter.plot_velocity_evolution(\n    quiver_color=\"black\",\n    mode=None,\n    incident=True,\n    scattered=False,\n    symmetric=False,\n    layout=(3, 3),\n    figsize=(12, 12),\n)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And also for ``plotter.animate_velocity(quiver_color=...)``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "anim = plotter.animate_velocity(\n    frames=100,\n    interval=50,\n    quiver=True,\n    quiver_color=\"black\",\n    mode=None,\n    incident=False,\n    scattered=True,\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.9.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}