{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "87bbf79d",
   "metadata": {},
   "source": [
    "# Run FEASST as server with Python client\n",
    "\n",
    "In this example, run feasst in server mode and use a Python client to send text interface commands."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "46b407e9",
   "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",
      "# initializing server on localhost:54321\n",
      "# Initializing random number generator with seed: 1781200110\n"
     ]
    }
   ],
   "source": [
    "import multiprocessing\n",
    "import subprocess\n",
    "import time\n",
    "import socket\n",
    "\n",
    "PORT = 54321\n",
    "BUFFER_SIZE = 1000\n",
    "\n",
    "def server():\n",
    "    subprocess.call('echo \"Server port '+str(PORT)+' buffer_size '+str(BUFFER_SIZE)+'\" | feasst', shell=True, executable='/bin/bash')\n",
    "\n",
    "def client():\n",
    "    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "    sock.connect((\"localhost\", PORT))\n",
    "    for line in ['MonteCarlo',\n",
    "                 'Configuration cubic_side_length=8 particle_type=fluid:/feasst/particle/lj.txt',\n",
    "                 'Potential Model=LennardJones',\n",
    "                 'ThermoParams beta=1 chemical_potential=1',\n",
    "                 'Metropolis',\n",
    "                 'TrialTranslate',\n",
    "                 'TrialAdd particle_type=fluid',\n",
    "                 'Run until_num_particles=20',\n",
    "                 'Remove name=TrialAdd',\n",
    "                 'Log output_file=lj.csv clear_file=true',\n",
    "                 'Run num_trials=10']:\n",
    "        sock.send(bytes(line, 'utf-8'))\n",
    "        message = sock.recv(BUFFER_SIZE)\n",
    "        #print(str(message))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    proc1 = multiprocessing.Process(target=server, args=())\n",
    "    proc2 = multiprocessing.Process(target=client, args=())\n",
    "    proc1.start()\n",
    "    time.sleep(1)\n",
    "    proc2.start()\n",
    "    proc1.join()\n",
    "    proc2.join()\n",
    "    import pandas as pd\n",
    "    df = pd.read_csv('lj.csv')\n",
    "    assert len(df)==10  # Check if simulation printed output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fcec3a9",
   "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
}
