{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Pressure Plot\n\nThis examples shows you how to plot the pressure field.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As always we start off by importing the necessary Python modules. For our\nexample we are going to need the osaft library, and the third party package\nMatplotlib.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from matplotlib import pyplot as plt\n\nimport osaft"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The next step is to define the properties for our example,\nthese include the material properties, the properties of the acoustic field\nand the radius. We always assume SI-units.\n\nThe wave type is set using the ``osaft.WaveType`` enum. Currently, there are\ntwo options:\n``osaft.WaveType.STANDING`` and ``osaft.WaveType.TRAVELLING`` for a plane\nstanding wave and a plane travelling wave, respectively.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# --------\n# Geometry\n# --------\n# Radius\nR_0 = 5e-6  # [m]\n\n# -----------------\n# Properties of copper\n# -----------------\n# Density\nrho_cu = 8960  # [kg/m^3]\n# Young's modulus\nE_cu = 130e9  # [Pa]\n# Possion ratio\nnu_cu = 0.34  # [-]\n\n# -------------------\n# Properties of Water\n# -------------------\n# Speed of sound\nc_w = 1_498  # [m/s]\n# Density\nrho_w = 997  # [kg/m^3]\n\n# --------------------------------\n# Properties of the Acoustic Field\n# --------------------------------\n# Frequency\nf = 5e5  # [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": [
        "Once all properties are defined we can initialize the solution instances for\nthe scattering field. In this case we are using the model Hasegawa (1969).\nHowever, you can also chose any other model that has the scattering\nimplemented.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sol = osaft.hasegawa1969.ScatteringField(\n    f=f,\n    R_0=R_0,\n    rho_s=rho_cu,\n    E_s=E_cu,\n    nu_s=nu_cu,\n    rho_f=rho_w,\n    c_f=c_w,\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\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plotter = osaft.FluidScatteringPlot(sol, r_max=5 * sol.R_0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In the first step we want to check if the set input pressure of ``p_0=1e5``\nmatches with the calculated incident pressure field. Since we are in a\nstanding wave field and will look at the time zero. We set the position to a\nmultiple of ``osaft.pi`` such that we are the pressure maximum or minimum\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sol.position = 0\n\nfig, _ = plotter.plot_pressure(\n    inst=True,\n    tripcolor=False,\n    incident=True,\n    scattered=False,\n)\nfig.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Alternatively, we can also look in an evolution plot how the pressure field\nevolves for different phase values. The default layout is 3x3 and can be\nadjusted with the option ``layout=(n_row, n_cols)``. Additionally we increase\nthe figure size with ``figsize=(...)`` to enlarge the plots.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# sphinx_gallery_thumbnail_number = 2\n\nplotter.plot_pressure_evolution(\n    inst=True,\n    tripcolor=False,\n    layout=(5, 5),\n    figsize=(10, 8),\n    incident=True,\n    scattered=False,\n)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now lets see what the magnitude of the scattered field is\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plotter.plot_pressure_evolution(\n    inst=True,\n    tripcolor=False,\n    layout=(5, 5),\n    figsize=(10, 8),\n    incident=False,\n    scattered=True,\n)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We might be also wondering how it looks for a travelling wave. Additionally,\nwe can change the colormap. Note here, that we set the attribute\n``plotter.div_cmap`` since the data that will be plotted will contain\npositive and negative values. Also, it is useful to have a diverging colormap\nthat is NOT white in the center because the scatterer is depicted white in\nour plots.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sol.wave_type = osaft.WaveType.TRAVELLING\nplotter.div_cmap = \"RdYlBu\"\n\nplotter.plot_pressure_evolution(\n    inst=True,\n    tripcolor=False,\n    layout=(5, 5),\n    figsize=(10, 8),\n    incident=False,\n    scattered=True,\n)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Lastly we want to animate the pressure. Keep in mind that now the wave type\nis travelling\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "anim = plotter.animate_pressure(scattered=True, incident=False)\nanim.resume()\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
}