{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "organizational-exclusion",
   "metadata": {},
   "source": [
    "# Lennard Jones Alpha potential test\n",
    "In this example, the [LennardJonesAlpha](../doc/LennardJonesAlpha_arguments.rst) potential energy between two particles is compared to the analytical value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "split-sculpture",
   "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 delta_sigma=0.5 particle_type=lj:/feasst/plugin/models/particle/ljdelta.txt xyz_file=two.xyz\n",
      "Potential Model=LennardJonesAlpha alpha=12\n",
      "ThermoParams beta=1000000\n",
      "Metropolis\n",
      "Log clear_file=true max_precision=true output_file=lj.csv\n",
      "Run num_trials=1\n",
      "#Warn 0 [plugin/monte_carlo/src/monte_carlo.cpp:786] 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",
    "# Run a feasst simulation with the given parameters\n",
    "def run_fst(params):\n",
    "    with open(\"launch0.txt\", \"w\") as myfile: myfile.write(\"\"\"\n",
    "MonteCarlo\n",
    "Configuration {config_params}\n",
    "Potential Model=LennardJonesAlpha alpha=12\n",
    "ThermoParams beta=1000000\n",
    "Metropolis\n",
    "Log output_file=lj.csv max_precision=true clear_file=true\n",
    "Run num_trials=1\n",
    "\"\"\".format(**params))\n",
    "    syscode = subprocess.call(\"../../../build/bin/fst < launch0.txt\", shell=True, executable='/bin/bash')\n",
    "    if syscode > 0: sys.exit(1)\n",
    "\n",
    "\"\"\"Test the LJ potential against analytical calculation of two particles\"\"\"\n",
    "params = {\"displacement\": 1.2345}\n",
    "with open(\"two.xyz\", \"w\") as myfile: myfile.write(\n",
    "\"\"\"2\n",
    "-1 8 8 8\n",
    "0 0 0 0\n",
    "1 0 0 {displacement}\"\"\".format(**params))\n",
    "run_fst({\"config_params\": \"particle_type=lj:/feasst/plugin/models/particle/ljdelta.txt xyz_file=two.xyz delta_sigma=0.5\"})\n",
    "df = pd.read_csv('lj.csv')\n",
    "assert 2 == df['num_particles_lj'][0]\n",
    "\n",
    "# compute the expected analytical LJ energy\n",
    "enlj = 4*((params[\"displacement\"]+0.5)**(-24) - (params[\"displacement\"]+0.5)**(-12))\n",
    "\n",
    "# Compare the analytical results with the FEASST computed energies.\n",
    "assert np.abs(enlj - df['LennardJonesAlpha'][0]) < 10**-12\n",
    "assert np.abs(enlj - df['energy'][0]) < 10**-12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7752ff68-1287-41ca-be73-33f775d4fe6c",
   "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 particle_type=lj:/feasst/plugin/models/particle/ljlambda.txt xyz_file=two.xyz\n",
      "Potential Model=LennardJonesAlpha alpha=12\n",
      "ThermoParams beta=1000000\n",
      "Metropolis\n",
      "Log clear_file=true max_precision=true output_file=lj.csv\n",
      "Run num_trials=1\n",
      "#Warn 0 [plugin/monte_carlo/src/monte_carlo.cpp:786] No Trials to attempt.\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Similar to above, but with the lambda parameter\"\"\"\n",
    "params = {\"displacement\": 1.2345}\n",
    "with open(\"two.xyz\", \"w\") as myfile: myfile.write(\n",
    "\"\"\"2\n",
    "-1 8 8 8\n",
    "0 0 0 0\n",
    "1 0 0 {displacement}\"\"\".format(**params))\n",
    "run_fst({\"config_params\": \"particle_type=lj:/feasst/plugin/models/particle/ljlambda.txt xyz_file=two.xyz\"})\n",
    "df = pd.read_csv('lj.csv')\n",
    "assert 2 == df['num_particles_lj'][0]\n",
    "\n",
    "# compute the expected analytical LJ energy\n",
    "enlj = 2*((params[\"displacement\"]+0.5)**(-24) - (params[\"displacement\"]+0.5)**(-12))\n",
    "\n",
    "# Compare the analytical results with the FEASST computed energies.\n",
    "assert np.abs(enlj - df['LennardJonesAlpha'][0]) < 10**-12\n",
    "assert np.abs(enlj - df['energy'][0]) < 10**-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
}
