{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Second virial coefficient calculation of a Kern-Frenkel patch using Mayer-Sampling\n",
    "\n",
    "\n",
    "Here, we reproduce the virial coefficient of a Kern-Frenkel patchy particle and compare to Eq. 8 of https://doi.org/10.1063/1.1569473"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Usage: /home/user/feasst/build/bin/fst < file.txt\n",
      "FEASST version 0.25.13\n",
      "MonteCarlo\n",
      "Configuration add_num_janus_particles=2 centers_site_type=0 cubic_side_length=100 first_particle_index=0 group=first,centers particle_type=janus:/feasst/plugin/patch/particle/janus.txt\n",
      "Potential Model=HardSphere group=centers\n",
      "Potential Model=SquareWell VisitModel=VisitModel VisitModelInner=VisitModelInnerPatch group=centers\n",
      "RefPotential Model=HardSphere group=centers ref=hs\n",
      "ThermoParams beta=0.1\n",
      "MayerSampling\n",
      "TrialTranslate group=first new_only=true ref=hs tunable_param=1\n",
      "TrialRotate new_only=true ref=hs tunable_param=40\n",
      "Log output_file=patch.csv trials_per_write=10000.0\n",
      "MoviePatch output_file=patch.xyz trials_per_write=10000.0\n",
      "CriteriaWriter output_file=patch_b2.txt trials_per_write=10000.0\n",
      "Tune\n",
      "Run num_trials=1e5\n",
      "# Initializing random number generator with seed: 1749647244\n",
      "Remove name=Tune\n",
      "Run num_trials=1e6\n",
      "MayerSampling\n",
      "Run num_trials=1e7\n",
      " \n",
      " exit: 0\n"
     ]
    }
   ],
   "source": [
    "params={\"beta\": 0.1, \"trials_per\": 1e4} # 1/K\n",
    "\n",
    "script=\"\"\"\n",
    "MonteCarlo\n",
    "Configuration cubic_side_length=100 particle_type=janus:/feasst/plugin/patch/particle/janus.txt add_num_janus_particles=2 \\\n",
    "    group=first,centers first_particle_index=0 centers_site_type=0\n",
    "Potential Model=HardSphere group=centers\n",
    "Potential Model=SquareWell VisitModel=VisitModel VisitModelInner=VisitModelInnerPatch group=centers\n",
    "RefPotential ref=hs Model=HardSphere group=centers\n",
    "ThermoParams beta={beta}\n",
    "MayerSampling\n",
    "TrialTranslate new_only=true ref=hs tunable_param=1 group=first\n",
    "TrialRotate new_only=true ref=hs tunable_param=40\n",
    "Let [write]=trials_per_write={trials_per} output_file=patch\n",
    "Log [write].csv\n",
    "MoviePatch [write].xyz\n",
    "CriteriaWriter [write]_b2.txt\n",
    "\n",
    "# tune trial parameters\n",
    "Tune\n",
    "Run num_trials=1e5\n",
    "Remove name=Tune\n",
    "\n",
    "# equilibrate\n",
    "Run num_trials=1e6\n",
    "\n",
    "# production\n",
    "MayerSampling\n",
    "Run num_trials=1e7\n",
    "\"\"\".format(**params)\n",
    "\n",
    "with open('script3.txt', 'w') as file: file.write(script)\n",
    "import subprocess\n",
    "syscode = subprocess.call(\"../../../build/bin/fst < script3.txt > script3.log\", shell=True, executable='/bin/bash')\n",
    "with open('script3.log', 'r') as file: print(file.read(), '\\n', 'exit:', syscode)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'second_virial_ratio': 0.93762, 'second_virial_ratio_block_stdev': 0.000762112}\n",
      "0.93762\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "import numpy as np\n",
    "b2_reduced = 1-0.5**2*(1.5**3-1)*(math.exp(params['beta'])-1)\n",
    "with open(\"patch_b2.txt\") as f:\n",
    "    firstline = f.readline().rstrip()\n",
    "    b2=eval(firstline)\n",
    "    print(b2)\n",
    "print(b2['second_virial_ratio'])\n",
    "assert np.abs(b2_reduced - b2['second_virial_ratio']) < 10*b2['second_virial_ratio_block_stdev']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Did this tutorial work as expected? Did you find any inconsistencies or have any comments? Please [contact](../../../CONTACT.rst) us. Any feedback is appreciated!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
