{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Boundary clusters in a disk\n\nA classical mean-field Vicsek model in a bounded disk domain.  \n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First of all, some standard imports. \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport sys\nimport time\nimport torch\nimport numpy as np \nfrom matplotlib import pyplot as plt\nfrom sisyphe.display import display_kinetic_particles\n\n\nuse_cuda = torch.cuda.is_available()\ndtype = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set the parameters and create an instance of the Vicsek model. \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import sisyphe.models as models\n\nN = 1000000\nL = 10.\ndt = .01\n\nnu = 3\nsigma = 1.\nkappa = nu/sigma\n\nR = .1\nc = 1.\n\ncenter = torch.tensor([L/2,L/2]).type(dtype).reshape((1,2))\nradius = L/2\npos = L*torch.rand((N,2)).type(dtype)\nout = ((pos-center)**2).sum(1) > radius**2\nwhile out.sum()>0:\n    pos[out,:] = L*torch.rand((out.sum(),2)).type(dtype)\n    out = ((pos-center)**2).sum(1) > radius**2\nvel = torch.randn(N,2).type(dtype)\nvel = vel/torch.norm(vel,dim=1).reshape((N,1))\n\nsimu=models.Vicsek(pos=pos,vel=vel,\n             v=c,\n             sigma=sigma,nu=nu,\n             interaction_radius=R,\n             box_size=L,\n             boundary_conditions='spherical',\n             variant = {\"name\" : \"max_kappa\", \"parameters\" : {\"kappa_max\" : 10.}},\n             options = {},\n             numerical_scheme='projection',\n             dt=dt,\n             block_sparse_reduction=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set the block sparse parameters to their optimal value. \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fastest, nb_cells, average_simu_time, simulation_time = simu.best_blocksparse_parameters(40,100)\n\nplt.plot(nb_cells,average_simu_time)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run the simulation and plot the particles. \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# sphinx_gallery_thumbnail_number = -1\n\nframes = [0, 10, 40, 70, 100, 150, 200, 250, 300]\n\ns = time.time()\nit, op = display_kinetic_particles(simu, frames, N_dispmax=100000)\ne = time.time()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Print the total simulation time and the average time per iteration. \n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print('Total time: '+str(e-s)+' seconds')\nprint('Average time per iteration: '+str((e-s)/simu.iteration)+' seconds')"
      ]
    }
  ],
  "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.8.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}