{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0effec24-cd5d-4535-931b-1b054822a43a",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Run a Simple Job\n",
    "\n",
    "This notebook walks you through the process of submitting a WebMO job using Python and Jupyter. \n",
    "\n",
    "## Setup\n",
    "\n",
    "Import the WebMO Python library and check its version."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "436cf933-3f7b-4483-88eb-8685f3588345",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WebMO Library Version: 1.4.1\n"
     ]
    }
   ],
   "source": [
    "import webmo\n",
    "print(f\"WebMO Library Version: {webmo.__version__}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b24d1b0-3c52-41cb-b504-a6a9ae242532",
   "metadata": {},
   "source": [
    "Set up all the user-defied values needed to run the job."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0ad7c4b2-511a-4ff4-8b46-eb09bdc0888d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "## BOOKKEEPING PARAMETERS\n",
    "# the URL of your WebMO instance\n",
    "URL = \"https://server.university.edu/~webmo/cgi-bin/webmo/rest.cgi\"\n",
    "\n",
    "# your username on the above WebMO instance\n",
    "username = \"smith\"\n",
    "\n",
    "## JOB PARAMETERS\n",
    "# title of the submitted WebMO job\n",
    "title = \"methane\"\n",
    "\n",
    "# Should we wait for the job to finish? True/False. If True, will wait for the \n",
    "# job (possibly for a while) and return output. If False, will not wait for the\n",
    "# job or display output.\n",
    "wait = True\n",
    "\n",
    "# engine to run the job\n",
    "engine = \"orca\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0eec399-565b-4edc-bdd0-c3d1592a83cb",
   "metadata": {},
   "source": [
    "Start the REST session:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "47aeaeba-c590-49f6-a426-3807f51a45ae",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter WebMO password for user smith: ········\n"
     ]
    }
   ],
   "source": [
    "rest = webmo.WebMOREST(URL, username=username)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a3235fb-877a-4485-aef8-fb751460d003",
   "metadata": {},
   "source": [
    "## Define the Job\n",
    "\n",
    "Define the input file for WebMO to run using the engine set above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d1fed414-0e7a-4774-ba58-c16314d811c8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# job file goes here!\n",
    "job = \"\"\"\n",
    "#\n",
    "# CH4\n",
    "#\n",
    "\n",
    "! HF 6-31G(d) \n",
    "\n",
    "* internal 0 1\n",
    "C 0 0 0 0.0000000 0.0000000 0.0000000\n",
    "H 1 0 0 1.0900000 0.0000000 0.0000000\n",
    "H 1 2 0 1.0900000 109.47122 0.0000000\n",
    "H 1 2 3 1.0900000 109.47122 120.00000\n",
    "H 1 2 3 1.0900000 109.47122 -120.00000\n",
    "*\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bd62593-239b-45c3-8415-3e642d8cc2b2",
   "metadata": {},
   "source": [
    "## Run the Job\n",
    "\n",
    "Run the job and optionally wait for it to finish."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b1b0497e-26e9-4e1e-a448-e477a51312e0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "n = rest.submit_job(title, job, engine)\n",
    "\n",
    "if wait:\n",
    "    rest.wait_for_job(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d3ac6b5-bb34-4731-b4fb-7497dff22da5",
   "metadata": {},
   "source": [
    "## Display Results\n",
    "\n",
    "If we have waited for the job to finish, grab a basic result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "86ca11a8-875e-4222-911d-6dab71713c52",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "job 39 Energy: -40.194732276435 Hartree\n"
     ]
    }
   ],
   "source": [
    "if wait:\n",
    "    results = rest.get_job_results(n)\n",
    "    energy = results['properties']['final_single_point_energy']['value']\n",
    "    units = results['properties']['final_single_point_energy']['units']\n",
    "    print(\"job\", n, \"Energy: {} {}\".format(energy, units))"
   ]
  }
 ],
 "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
