{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Second virial coefficient calculation of a Trimer using Mayer-Sampling\n",
    "\n",
    "\n",
    "Here, we reproduce the virial coefficient of a trimer at the Boyle temperature as reported Table 1 of the following manuscript for $L=0.4\\sigma$\n",
    "\n",
    "https://dx.doi.org/10.1063/1.4918557"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Usage: /home/hwh/feasst/build/bin/fst < file.txt\n",
      "FEASST version 0.25.16\n",
      "MonteCarlo\n",
      "Configuration add_num_trimer_particles=2 cubic_side_length=1e100 cutoffA_R=1.122462048309373 cutoffR_R=1.122462048309373 first_particle_index=0 group=first particle_type=trimer:/feasst/particle/trimer.txt\n",
      "Potential Model=LennardJonesForceShift\n",
      "RefPotential Model=HardSphere cutoff=0 cutoffA=1 ref=hs sigma=0 sigmaA=1 sigmaR=0\n",
      "ThermoParams beta=2.2988505747126435\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=trimer.txt trials_per_write=1e4\n",
      "Movie output_file=trimer.xyz trials_per_write=1e4\n",
      "CriteriaWriter output_file=trimer_b2.csv trials_per_write=1e4\n",
      "Run num_trials=1e6\n",
      "# Initializing random number generator with seed: 1769185324\n",
      " \n",
      " exit: 0\n"
     ]
    }
   ],
   "source": [
    "params={\"beta\": 1./0.435, \"rwca\": 2.**(1./6.)}\n",
    "\n",
    "script=\"\"\"\n",
    "MonteCarlo\n",
    "Configuration cubic_side_length=1e100 particle_type=trimer:/feasst/particle/trimer.txt \\\n",
    "    add_num_trimer_particles=2 cutoffA_R={rwca} cutoffR_R={rwca} \\\n",
    "    group=first first_particle_index=0\n",
    "Potential Model=LennardJonesForceShift\n",
    "RefPotential ref=hs Model=HardSphere sigma=0 sigmaA=1 sigmaR=0 cutoff=0 cutoffA=1\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=1e4 output_file=trimer\n",
    "Log [write].txt\n",
    "Movie [write].xyz\n",
    "CriteriaWriter [write]_b2.csv\n",
    "Run num_trials 1e6\n",
    "\"\"\".format(**params)\n",
    "\n",
    "with open('script1.txt', 'w') as file: file.write(script)\n",
    "import subprocess\n",
    "syscode = subprocess.call(\"../../../build/bin/fst < script1.txt > script1.log\", shell=True, executable='/bin/bash')\n",
    "with open('script1.log', 'r') as file: print(file.read(), '\\n', 'exit:', syscode)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'second_virial_ratio': 0.020557, 'second_virial_ratio_block_stdev': 0.0372921}\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "import numpy as np\n",
    "with open(\"trimer_b2.csv\") as f:\n",
    "    firstline = f.readline().rstrip()\n",
    "    #print(firstline)\n",
    "    b2=eval(firstline)\n",
    "    #b2={\"second_virial_ratio\": -0.0166853, \"second_virial_ratio_block_stdev\": 0.0159632}\n",
    "    print(b2)\n",
    "# print(mayer.mayer().str())\n",
    "    assert np.abs(b2['second_virial_ratio']) < 4*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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
