{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Multicore ARF Computation\n\nIn this example we show how multiprocessing can be used to improving\ncomputation speed for ARF plots.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "For computationally expensive models, i.e. models that include numerical\nintegration the OSAFT library provides the option for computing the ARF\nusing multiprocessing. If a solution class is added to an ``ARFPlot``\ninstance using ``add_solutions`` the parameter ``multicore`` can be set to\n``True``. Now the computation of the ARF for each point of the plot will be\ncomputed in a new process that will run in parallel.\n\nThe models used in the example below should probably not be used with the\n``multicore`` option, since they are simple and evaluate very fast. For\nsimple models multiprocessing will not lead to faster evaluation.\n\n\n**Important**\n\nIn order for multiprocessing to work you need to run your code in your main\nfile inside the ``if __name__ == '__main__':`` clause as shown below.\nCheck the\n[multiprocessing documentation](https://docs.python.org/3/library/multiprocessing.html) for more\ninformation.\n\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\n\ndef main():\n\n    king = osaft.king1934.ARF(\n        f=1e6,\n        R_0=1e-6,\n        rho_s=1020,\n        rho_f=997,\n        c_f=1498,\n        p_0=1e5,\n        wave_type=osaft.WaveType.STANDING,\n        position=osaft.pi / 4,\n    )\n\n    yosioka = osaft.yosioka1955.ARF(\n        f=1e6,\n        R_0=1e-6,\n        rho_s=1020,\n        c_s=2350,\n        rho_f=997,\n        c_f=1498,\n        p_0=1e5,\n        wave_type=osaft.WaveType.STANDING,\n        position=osaft.pi / 4,\n    )\n\n    gorkov = osaft.gorkov1962.ARF(\n        f=1e6,\n        R_0=1e-6,\n        rho_s=1020,\n        c_s=2350,\n        rho_f=997,\n        c_f=1498,\n        p_0=1e5,\n        wave_type=osaft.WaveType.STANDING,\n        position=osaft.pi / 4,\n    )\n\n    plot = osaft.ARFPlot(\"R_0\", np.linspace(1e-6, 20e-6))\n\n    # Solutions added using multiprocessing\n    plot.add_solutions(king, yosioka, multicore=True)\n\n    # Solutions added without multiprocessing\n    plot.add_solutions(gorkov)\n\n    plot.plot_solutions()\n\n    plt.show()\n\n\nif __name__ == \"__main__\":\n    main()"
      ]
    }
  ],
  "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
}