{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Make your own custom Action\n",
    "\n",
    "In this example, a user can follow the instructions in /feasst/plugin/example/include/action_example.h to make their own custom Action.\n",
    "\n",
    "In this relatively trivial example, the newly implemented Action forces an Analyze or Modify to write to file."
   ]
  },
  {
   "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(\"launch3.txt\", \"w\") as myfile: myfile.write(\"\"\"\n",
    "MonteCarlo\n",
    "Configuration particle_type=fluid:/feasst/particle/lj_new.txt cubic_side_length=8\n",
    "Potential Model=IdealGas\n",
    "ThermoParams beta=1 chemical_potential=1\n",
    "FlatHistogram Macrostate=MacrostateNumParticles width=1 max=4 min=0 Bias=TransitionMatrix min_sweeps=2\n",
    "TrialTransfer particle_type=fluid\n",
    "CriteriaUpdater trials_per_update=1e2\n",
    "Let [write]=trials_per_write=1e2 output_file=action_ex\n",
    "Energy [write]_en.csv multistate=true\n",
    "CriteriaWriter [write]_crit.csv\n",
    "PairDistribution [write]_gr.csv\n",
    "Run until=complete\n",
    "ActionExample analyze_name=CriteriaWriter\n",
    "ActionExample analyze_name=Energy\n",
    "ActionExample modify_name=PairDistribution\"\"\")\n",
    "    syscode = subprocess.call(\"../../../build/bin/fst < launch3.txt\", shell=True, executable='/bin/bash')\n",
    "    if syscode > 0: sys.exit(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check that, with \"ActionExample analyze_name CriteriaWriter\", the number of sweeps in the writen file is equal to the number of sweeps requested (i.e., 2)."
   ]
  },
  {
   "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 cubic_side_length=8 particle_type=fluid:/feasst/particle/lj_new.txt\n",
      "Potential Model=IdealGas\n",
      "ThermoParams beta=1 chemical_potential=1\n",
      "FlatHistogram Bias=TransitionMatrix Macrostate=MacrostateNumParticles max=4 min=0 min_sweeps=2 width=1\n",
      "TrialTransfer particle_type=fluid\n",
      "CriteriaUpdater trials_per_update=1e2\n",
      "Energy multistate=true output_file=action_ex_en.csv trials_per_write=1e2\n",
      "CriteriaWriter output_file=action_ex_crit.csv trials_per_write=1e2\n",
      "PairDistribution output_file=action_ex_gr.csv trials_per_write=1e2\n",
      "Run until=complete\n",
      "# Initializing random number generator with seed: 1749043512\n",
      "ActionExample analyze_name=CriteriaWriter\n",
      "ActionExample analyze_name=Energy\n",
      "ActionExample modify_name=PairDistribution\n",
      "{'num_sweeps': 2, 'soft_min': 0, 'soft_max': 4}\n"
     ]
    }
   ],
   "source": [
    "run_fst()\n",
    "file1 = open('action_ex_crit.csv', 'r')\n",
    "lines = file1.readlines()\n",
    "file1.close()\n",
    "exec('iprm={' + lines[0][1:]+'}', globals())\n",
    "print(iprm)\n",
    "assert iprm['num_sweeps'] == 2"
   ]
  },
  {
   "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
}
