{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Second virial coefficient calculation of TraPPE ethane using Mayer-Sampling\n",
    "\n",
    "\n",
    "Here, we reproduce the virial coefficient of TraPPE ethane\n",
    "\n",
    "Table 6 of https://doi.org/10.1021/jp972543+ reports -73 +/- 1 ml/mol at 423.2K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Usage: feasst < file.txt\n",
      "# For more information, use the command \"feasst-menu\"\n",
      "# Exit with ctrl-c\n",
      "FEASST version 0.25.19\n",
      "MonteCarlo\n",
      "Configuration add_num_ethane_particles=2 cubic_side_length=1e100 cutoff=1e4 first_particle_index=0 group=first particle_type=ethane:ethane_with_ref.txt\n",
      "Potential Model=LennardJones\n",
      "RefPotential Model=HardSphere ref=hs sigmaC1=4 sigmaC2=0\n",
      "ThermoParams beta=0.0023629489603024575\n",
      "MayerSampling\n",
      "TrialTranslate group=first new_only=true ref=hs tunable_param=0.1\n",
      "TrialRotate new_only=true ref=hs tunable_param=40\n",
      "Log output_file=ethane.csv trials_per_write=1e5\n",
      "Movie output_file=ethane.xyz trials_per_write=1e5\n",
      "CriteriaWriter output_file=ethane_b2.csv trials_per_write=1e5\n",
      "Tune\n",
      "Run num_trials=1e5\n",
      "# Initializing random number generator with seed: 1781198411\n",
      "Remove name=Tune\n",
      "Run num_trials=1e6\n",
      "MayerSampling\n",
      "Run num_trials=1e7\n",
      " \n",
      " exit: 0\n"
     ]
    }
   ],
   "source": [
    "params={\"beta\": 1./423.2} # 1/K\n",
    "\n",
    "script=\"\"\"\n",
    "MonteCarlo\n",
    "Configuration cubic_side_length=1e100 particle_type=ethane:ethane_with_ref.txt \\\n",
    "    add_num_ethane_particles=2 cutoff=1e4 \\\n",
    "    group=first first_particle_index=0\n",
    "Potential Model=LennardJones\n",
    "RefPotential ref=hs Model=HardSphere sigmaC1=4 sigmaC2=0\n",
    "ThermoParams beta={beta}\n",
    "MayerSampling\n",
    "TrialTranslate new_only=true ref=hs tunable_param=0.1 group=first\n",
    "TrialRotate new_only=true ref=hs tunable_param=40\n",
    "Let [write]=trials_per_write=1e5 output_file=ethane\n",
    "Log [write].csv\n",
    "Movie [write].xyz\n",
    "CriteriaWriter [write]_b2.csv\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('script2.txt', 'w') as file: file.write(script)\n",
    "import subprocess\n",
    "syscode = subprocess.call(\"feasst < script2.txt > script2.log\", shell=True, executable='/bin/bash')\n",
    "with open('script2.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.913015, 'second_virial_ratio_block_stdev': 0.00493166}\n",
      "b22(L/mol) -60.72691679776316\n",
      "b22_block_stdev(L/mol) 0.32801707145540504\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "import numpy as np\n",
    "with open(\"ethane_b2.csv\") as f:\n",
    "    firstline = f.readline().rstrip()\n",
    "    #print(firstline)\n",
    "    b2=eval(firstline)\n",
    "    print(b2)\n",
    "    b2hs = 2./3.*math.pi*3.75**3  # A^3\n",
    "    b2hs *= 1e-30*1e3*1e3*6.02214076E+23 # L/mol\n",
    "    print('b22(L/mol)', b2hs*b2['second_virial_ratio'])\n",
    "    print('b22_block_stdev(L/mol)', b2hs*b2['second_virial_ratio_block_stdev'])\n",
    "    assert np.abs(-63 - b2hs*b2['second_virial_ratio']) < 20*b2hs*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
}
