{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Make your own custom analysis\n",
    "\n",
    "In this example, a user can follow the instructions in /feasst/plugin/example/include/analyze_example.h to make their own custom analysis.\n",
    "In some cases, it is preferable to implement the analysis within FEASST rather than analyze stored configurations in order to limit the number of configurations that need to be saved to disk.\n",
    "\n",
    "In this relatively trivial example, the geometric center of all (PBC wrapped) sites is computed for an ideal gas fluid.\n",
    "The PBCs are centered on the origin, so the result should be the origin, within statistical error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import subprocess\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "def run_fst():\n",
    "    with open(\"launch2.txt\", \"w\") as myfile: myfile.write(\"\"\"\n",
    "MonteCarlo\n",
    "Configuration particle_type=fluid:/feasst/particle/lj_new.txt cubic_side_length=8 add_num_fluid_particles=100\n",
    "Potential Model=IdealGas\n",
    "ThermoParams beta=1\n",
    "Metropolis trials_per_cycle=1e2\n",
    "TrialTranslate tunable_param=4\n",
    "AnalyzeExample trials_per_write=1e3 output_file=ig_center.csv start_after_cycle=1\n",
    "Run num_trials=1e5\"\"\")\n",
    "    syscode = subprocess.call(\"../../../build/bin/fst < launch2.txt\", shell=True, executable='/bin/bash')\n",
    "    if syscode > 0: sys.exit(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the test and check the energy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "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_fluid_particles=100 cubic_side_length=8 particle_type=fluid:/feasst/particle/lj_new.txt\n",
      "Potential Model=IdealGas\n",
      "ThermoParams beta=1\n",
      "Metropolis trials_per_cycle=1e2\n",
      "TrialTranslate tunable_param=4\n",
      "AnalyzeExample output_file=ig_center.csv start_after_cycle=1 trials_per_write=1e3\n",
      "Run num_trials=1e5\n",
      "# Initializing random number generator with seed: 1749043370\n"
     ]
    }
   ],
   "source": [
    "run_fst()\n",
    "df = pd.read_csv('ig_center.csv')\n",
    "df['center_is_origin'] = abs(df['average']) < 3*df['block_stdev']\n",
    "assert df['center_is_origin'].all()"
   ]
  },
  {
   "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
}
