{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Possible Numerical Problems\n\nAs with an numerical software, it is possible that the numerics run into\nproblems for certain set of parameters. Luckily, for the usual range of\nparameters relevant in acoustofluidics there seem to be few problems in OSAFT.\nIn our automated tests, we test every model in this relevant parameter range.\nNevertheless, it is still possible to run into numerical problems. In this\nexample, we show when these problems could arise and how they can be dealt\nwith.\n\nIn this example we investigate a heavy particle in a highly viscous\nfluid. We are using the model ``doinikov1994rigid``. For high\nviscosities ( $\\eta_f \\approx 0.5 \\mathrm{Pa s}$) and very small\nparticles ($R_0= 500\\mathrm{nm}$) this solution can become unstable.\nWe are comparing the general solution of this model with the long\nwavelength approximation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom matplotlib import pyplot as plt\n\nimport osaft\n\ngeneral = osaft.doinikov1994rigid.ARF(\n    f=1e5,\n    R_0=5e-7,\n    rho_s=5e4,\n    rho_f=1e3,\n    c_f=1500,\n    eta_f=1e-3,\n    zeta_f=0,\n    p_0=1e5,\n    wave_type=osaft.WaveType.STANDING,\n    position=osaft.pi / 4,\n)\n\napprox = general.copy()\napprox.long_wavelength = True\n\ngeneral.name = \"General\"\napprox.name = \"Long Wavelength\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The ARF on the particle is computed for different values of viscosity. The\ngeneral solution becomes unstable as we pass a viscosity of\n$\\eta_f \\approx 0.1 \\mathrm{Pa s}$ the solution becomes unstable.\nIn order for multiprocessing to work you need to run your code inside the\n``if __name__ == '__main__':`` clause as shown below.\nCheck the\n`multiprocessing example\n<sphx_glr_examples_tutorial_example_multicore.py>`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "if __name__ == \"__main__\":\n\n    arf_plot = osaft.ARFPlot(\"eta_f\", np.logspace(-3, -0.5, num=16))\n    arf_plot.add_solutions(approx, multicore=False)\n    arf_plot.add_solutions(general, multicore=True)\n\n    fig, ax = arf_plot.plot_solutions(plot_method=plt.semilogx, marker=\"o\")\n    ax.set_xlabel(\"$\\\\eta_f$ $\\\\mathrm{[Pa s]}$\")\n    plt.show()\n\n    print(f\"{approx.eta_f = }\")\n    print(f\"{abs(approx.x):.3e} << 1\")\n    print(f\"{abs(approx.x):.3e} << {abs(approx.x_v):.3e}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "If one runs into numerical issues, the easiest remedy is to choose a\ndifferent model or a different approximation of a model. In this case here\nthe ``long_wavelength`` solution seems to be more suitable. In particular,\nsince the assumption of a long wavelength, i.e.\n$x \\ll 1$, $x \\ll x_v$, still holds. We can confirm this\neasily using the OSAFT library.\n\n"
      ]
    }
  ],
  "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
}