{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "organizational-exclusion",
   "metadata": {},
   "source": [
    "# F3C potential test\n",
    "In this example, the potential energy between two sites is compared to the analytical value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "split-sculpture",
   "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 particle_type=f3c:../../../plugin/models/test/data/f3c_o.txt xyz_file=two.xyz\n",
      "Potential Asc=1 Model=F3C\n",
      "ThermoParams beta=1000000\n",
      "Metropolis\n",
      "Log clear_file=true max_precision=true output_file=f3c_o.csv\n",
      "Run num_trials=1\n",
      "#Warn 0 [/feasst/plugin/monte_carlo/src/monte_carlo.cpp:802] No Trials to attempt.\n",
      "# 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 particle_type=f3c:../../../plugin/models/test/data/f3c_h.txt xyz_file=two.xyz\n",
      "Potential Asc=1 Model=F3C\n",
      "ThermoParams beta=1000000\n",
      "Metropolis\n",
      "Log clear_file=true max_precision=true output_file=f3c_h.csv\n",
      "Run num_trials=1\n",
      "#Warn 0 [/feasst/plugin/monte_carlo/src/monte_carlo.cpp:802] No Trials to attempt.\n",
      "# 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_f3c_h_particles=1 add_num_f3c_o_particles=1 particle_type=f3c_o:../../../plugin/models/test/data/f3c_o.txt,f3c_h:../../../plugin/models/test/data/f3c_h.txt xyz_file=two.xyz\n",
      "# Renamed Site name:0->1 for particle_type:1 in:../../../plugin/models/test/data/f3c_h.txt\n",
      "# Renamed Site type:0->1 for particle_type:1 in:../../../plugin/models/test/data/f3c_h.txt\n",
      "Potential Asc=1 Model=F3C\n",
      "ThermoParams beta=1000000\n",
      "Metropolis\n",
      "Log clear_file=true max_precision=true output_file=f3c_h.csv\n",
      "Run num_trials=1\n",
      "#Warn 0 [/feasst/plugin/monte_carlo/src/monte_carlo.cpp:802] No Trials to attempt.\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "import sys\n",
    "import subprocess\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "def run_fst(params):\n",
    "    \"\"\" Run a feasst simulation with the given parameters \"\"\"\n",
    "    with open(\"launch0.txt\", \"w\") as myfile: myfile.write(\"\"\"\n",
    "MonteCarlo\n",
    "Configuration {config_params}\n",
    "Potential Model=F3C Asc=1\n",
    "ThermoParams beta=1000000\n",
    "Metropolis\n",
    "Log output_file={log}.csv max_precision=true clear_file=true\n",
    "Run num_trials=1\n",
    "\"\"\".format(**params))\n",
    "    syscode = subprocess.call(\"feasst < launch0.txt\", shell=True, executable='/bin/bash')\n",
    "    if syscode > 0: sys.exit(1)\n",
    "\n",
    "def write_xyz(params):\n",
    "    \"\"\" Write an xyz file\"\"\"\n",
    "    with open(\"two.xyz\", \"w\") as myfile: myfile.write(\n",
    "\"\"\"2\n",
    "-1 20 20 20\n",
    "0 0 0 0\n",
    "1 0 0 {displacement}\"\"\".format(**params))\n",
    "\n",
    "\"\"\"Test the F3C potential against analytical calculation of two oyxgen sites\"\"\"\n",
    "params = {\"displacement\": 5, \"log\": \"f3c_o\",\n",
    "          \"config_params\": \"particle_type=f3c:../../../plugin/models/test/data/f3c_o.txt xyz_file=two.xyz\"}\n",
    "write_xyz(params)\n",
    "run_fst(params)\n",
    "df = pd.read_csv(params['log']+'.csv')\n",
    "assert 2 == df['num_particles_f3c'][0]\n",
    "assert np.abs(-0.041567117565916176 + 46.71010086004122 - df['F3C'][0]) < 12\n",
    "\n",
    "\"\"\"Test the LJ potential against analytical calculation of two hydrogen sites\"\"\"\n",
    "params = {\"displacement\": 5, \"log\": \"f3c_h\",\n",
    "          \"config_params\": \"particle_type=f3c:../../../plugin/models/test/data/f3c_h.txt xyz_file=two.xyz\"}\n",
    "write_xyz(params)\n",
    "run_fst(params)\n",
    "df = pd.read_csv(params['log']+'.csv')\n",
    "assert 2 == df['num_particles_f3c'][0]\n",
    "assert np.abs(-6.377176514562533e-07 + 11.677525215010306 - df['F3C'][0]) < 12\n",
    "\n",
    "\"\"\"Test the LJ potential against analytical calculation of an oxygen and hydrogen site\"\"\"\n",
    "params = {\"displacement\": 5, \"log\": \"f3c_h\",\n",
    "          \"config_params\": \"particle_type=f3c_o:../../../plugin/models/test/data/f3c_o.txt,f3c_h:../../../plugin/models/test/data/f3c_h.txt add_num_f3c_o_particles=1 add_num_f3c_h_particles=1 xyz_file=two.xyz\"}\n",
    "write_xyz(params)\n",
    "run_fst(params)\n",
    "df = pd.read_csv(params['log']+'.csv')\n",
    "assert 1 == df['num_particles_f3c_o'][0]\n",
    "assert 1 == df['num_particles_f3c_h'][0]\n",
    "assert np.abs(-0.0006260058345921262 + -23.35505043002061 - df['F3C'][0]) < 12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "numeric-tattoo",
   "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": 5
}
