{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "unknown-specific",
   "metadata": {},
   "source": [
    "# Modify a checkpoint file during restarting to run longer\n",
    "\n",
    "In this example, the first simulation generates a checkpoint file. The second simulation runs from the checkpoint file after modification of the number of cycles to complete a simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "split-sculpture",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Usage: feasst < file.txt\n",
      "# For more information, use the command \"feasst-menu\"\n",
      "FEASST version 0.25.19\n",
      "MonteCarlo\n",
      "Checkpoint checkpoint_file=lj.fst\n",
      "RandomMT19937 seed=1572362164\n",
      "# Initializing random number generator with seed: 1572362164\n",
      "Configuration cubic_side_length=55.0321208149104 particle_type=lj:/feasst/particle/lj_new.txt\n",
      "Potential Model=LennardJones VisitModel=VisitModelCell\n",
      "Potential VisitModel=LongRangeCorrections\n",
      "ThermoParams beta=1.111111111111111 chemical_potential=-1\n",
      "Metropolis\n",
      "TrialTranslate tunable_param=2\n",
      "Tune\n",
      "CheckEnergy decimal_places=6 trials_per_update=1e4\n",
      "TrialAdd particle_type=lj weight=2\n",
      "Run until_num_particles=500\n",
      "Remove name=TrialAdd\n",
      "Run num_trials=1e5\n",
      "Remove name=Tune\n",
      "Log clear_file=true output_file=lj.csv trials_per_write=1e4\n",
      "Movie output_file=lj.xyz trials_per_write=1e4\n",
      "Energy output_file=lj_en.csv trials_per_write=1e4\n",
      "Metropolis cycles_to_complete=1e2 trials_per_cycle=1e4\n",
      "Run until=complete\n",
      "WriteCheckpoint\n",
      "# Usage: feasst < file.txt\n",
      "# For more information, use the command \"feasst-menu\"\n",
      "FEASST version 0.25.19\n",
      "Restart checkpoint_file=lj.fst\n",
      "# Restarting from file: lj.fst\n",
      "Metropolis cycles_to_complete=2e8 trials_per_cycle=1e8\n",
      "Checkpoint checkpoint_file=lj2.fst num_hours_terminate=0.01\n",
      "Run until=complete\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "terminate called after throwing an instance of 'feasst::CustomException'\n",
      "  what():  Throw on proc 0 : #Fatal error0 /feasst/plugin/utils/include/checkpoint.h:88: Terminating because Checkpoint has reached the user input num_hours_terminate: 0.01. Detect this termination in Bash shell using \"$? != 0\"\n",
      "/bin/bash: line 1: 33594 Aborted                 feasst < continue.txt\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Usage: feasst < file.txt\n",
      "# For more information, use the command \"feasst-menu\"\n",
      "FEASST version 0.25.19\n",
      "Restart checkpoint_file=lj2.fst clear_previous_arguments=true\n",
      "# Restarting from file: lj2.fst\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import pandas as pd\n",
    "import subprocess\n",
    "\n",
    "# Run a feasst simulation with the given parameters\n",
    "def run_fst():\n",
    "    with open(\"launch00.txt\", \"w\") as myfile: myfile.write(\"\"\"\n",
    "MonteCarlo\n",
    "Checkpoint checkpoint_file=lj.fst\n",
    "RandomMT19937 seed=1572362164\n",
    "Configuration cubic_side_length=55.0321208149104 particle_type=lj:/feasst/particle/lj_new.txt\n",
    "Potential Model=LennardJones VisitModel=VisitModelCell\n",
    "Potential VisitModel=LongRangeCorrections\n",
    "ThermoParams beta=1.111111111111111 chemical_potential=-1\n",
    "Metropolis\n",
    "TrialTranslate tunable_param=2\n",
    "Tune\n",
    "CheckEnergy trials_per_update=1e4 decimal_places=6\n",
    "TrialAdd weight=2 particle_type=lj\n",
    "Run until_num_particles=500\n",
    "Remove name=TrialAdd\n",
    "Run num_trials=1e5\n",
    "Remove name=Tune\n",
    "Let [write]=trials_per_write=1e4 output_file=lj\n",
    "Log [write].csv clear_file=true\n",
    "Movie [write].xyz\n",
    "Energy [write]_en.csv\n",
    "Metropolis trials_per_cycle=1e4 cycles_to_complete=1e2\n",
    "Run until=complete\n",
    "WriteCheckpoint\n",
    "\"\"\")\n",
    "    syscode = subprocess.call(\"feasst < launch00.txt\", shell=True, executable='/bin/bash')\n",
    "    if syscode > 0: sys.exit(1)\n",
    "\n",
    "run_fst()\n",
    "df = pd.read_csv('lj.csv')\n",
    "assert len(df) == 101\n",
    "\n",
    "# Write a second run script that restarts the simulation and adds a lot more trials. Too many. Try to clear those on a second restart.\n",
    "with open('continue.txt', 'w') as myfile: myfile.write(\"\"\"Restart checkpoint_file=lj.fst\n",
    "Metropolis trials_per_cycle=1e8 cycles_to_complete=2e8\n",
    "Checkpoint checkpoint_file=lj2.fst num_hours_terminate=0.01\n",
    "Run until=complete\n",
    "WriteCheckpoint\n",
    "\"\"\")\n",
    "syscode = subprocess.call(\"feasst < continue.txt\", shell=True, executable='/bin/bash')\n",
    "\n",
    "# Write a third run script that restarts the simulation and the clears the previous arguments. Ask for a reasonable number of trials.\n",
    "with open('continue2.txt', 'w') as myfile: myfile.write(\"\"\"Restart checkpoint_file=lj2.fst clear_previous_arguments=true\n",
    "Metropolis trials_per_cycle=1e4 cycles_to_complete=1e1\n",
    "Run until=complete\n",
    "WriteCheckpoint\n",
    "\"\"\")\n",
    "syscode = subprocess.call(\"feasst < continue2.txt\", shell=True, executable='/bin/bash')\n",
    "\n",
    "df = pd.read_csv('lj.csv')\n",
    "assert len(df) > 202"
   ]
  },
  {
   "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
}
