{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d4042ccc-59c7-48a2-bbd0-0d4804c8dee8",
   "metadata": {},
   "source": [
    "# Run a Template Job\n",
    "\n",
    "This notebook demonstrates how to run a customizable job using a template.\n",
    "\n",
    "## Imports\n",
    "\n",
    "As in every notebook we make, first import the various packages needed for the rest of the notebook function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e4618ffa-130c-4982-bc71-9dc4790e3397",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from webmo import WebMOREST\n",
    "from webmo.util import xyz_from_name\n",
    "from webmo.gui import JupyterGUI"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96682f6c-6bce-462e-98d2-54066e572285",
   "metadata": {},
   "source": [
    "## User Setup\n",
    "\n",
    "Set up the user-supplied information that defines the job."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f4add38f-5515-4d26-832b-b421e7c3f2ef",
   "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",
    "# name of the molecule to run a job on\n",
    "molecule = \"methane\"\n",
    "\n",
    "# job type to run\n",
    "job_type = \"Optimize + Vib Freq\" "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb1ccc52-4b1a-461c-b3f5-10e49862ee15",
   "metadata": {},
   "source": [
    "## Geometry\n",
    "\n",
    "Grab the 3D geometry from PubChem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "170ea260-3711-4390-93f7-5239cdd5ed39",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "geom = xyz_from_name(molecule)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e627a47-4250-4c6c-b503-81dee83623e0",
   "metadata": {},
   "source": [
    "## REST session\n",
    "\n",
    "Open a WebMO REST session."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b52bf972-da93-4a2a-bf5a-7fb628c8e576",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter WebMO password for user smith: ········\n"
     ]
    }
   ],
   "source": [
    "rest = WebMOREST(URL, username=username)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d528df35-7cee-4078-9c6b-f663b48ab908",
   "metadata": {},
   "source": [
    "## Template\n",
    "\n",
    "Create the job template and submit it using the new GUI module."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b16b504c-7b3f-45f3-a7e1-7a879079403e",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "gui = JupyterGUI(rest.get_templates(\"gaussian\")[job_type],\n",
    "                query_vars=[\n",
    "                    \"jobName\",\n",
    "                    \"basisSet\",\n",
    "                    \"theory\",\n",
    "                    \"multiplicity\",\n",
    "                    \"charge\",\n",
    "                ],\n",
    "                additional_vars={\n",
    "                    \"geometry\":geom\n",
    "                })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "845a2878-dd84-48b9-bcb5-4f0b1db8bd95",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "14dc97d5f4c04c8d8614d0f6782ca0c8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Text(value='Title', description='jobName')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "37169955c6b641b8b42fc9cf180a9af7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='basisSet', index=3, options={'Accurate: 6-311+G(2d,p)': '6-311+G(2d,p)', 'cc-pVTZ': 'cc-…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d1a694fe1a99425a90d7a5d543ef0605",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='theory', options={'Hartree-Fock': 'HF', 'UFF Mechanics': 'UFF', 'APF-D': 'APFD', 'PM6': …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7a966790ceaf4052a9f1f03fb945d4f3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='multiplicity', index=2, options={'Triplet': '3', 'Quartet': '4', 'Singlet': '1', 'Quinte…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "21bd7db3e73f4ed0a407e463b981c0df",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Text(value='0', description='charge')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "gui.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6037c6ee-7abe-41f8-8ae9-7e2bb22149c2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The job that will be submitted:\n",
      "#N MP2/3-21G OPT FREQ \n",
      "\n",
      "Methane Opt Freq\n",
      "\n",
      "0 1\n",
      "C\t0.000000\t0.000000\t0.000000\n",
      "H\t0.554100\t0.799600\t0.496500\n",
      "H\t0.683300\t-0.813400\t-0.253600\n",
      "H\t-0.778200\t-0.373500\t0.669200\n",
      "H\t-0.459300\t0.387400\t-0.912100\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "job = rest.generate_input(rest.get_templates(\"gaussian\")[job_type], gui.get_variables())\n",
    "\n",
    "print(\"The job that will be submitted:\")\n",
    "print(job)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "463b2b04-3f1b-422c-ab33-89b3e0d4542c",
   "metadata": {},
   "source": [
    "Submit the job through that REST session and wait for it to complete."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c8097303-ab66-4325-a091-782cbd0a06cf",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "n = rest.submit_job(gui.get_variables()['jobName'], job, \"gaussian\")\n",
    "rest.wait_for_job(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "386be75a-70be-4f19-85c6-fb96efae3fc4",
   "metadata": {},
   "source": [
    "After the job is finished, get some of its output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a47a3fe-7c40-4c6b-9bce-93e172639e54",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Entering Gaussian System, Link 0=/share/apps/g16/g16\n",
      " Initial command:\n",
      " /share/apps/g16/l1.exe \"/scratch/webmo-1316028/40/Gau-990953.inp\" -scrdir=\"/scratch/webmo-1316028/40/\"\n",
      " Entering Link 1 = /share/apps/g16/l1.exe PID=    990954.\n",
      "  \n",
      " Copyright (c) 1988-2019, Gaussian, Inc.  All Rights Reserved.\n",
      "  \n",
      " This is part of the Gaussian(R) 16 program.  It is based on\n",
      " the Gaussian(R) 09 system (copyrigh\n"
     ]
    }
   ],
   "source": [
    "print(rest.get_job_output(n)[:400])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7446dd9-739c-4049-95f7-3b6080a8a2f3",
   "metadata": {},
   "source": [
    "We can also get numeric results through the WebMO library, such as the energy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "89c96c8c-6dee-42d3-b685-49e6d709f5fc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Energy: -39.9766423976 Hartree\n"
     ]
    }
   ],
   "source": [
    "results = rest.get_job_results(n)\n",
    "energy = results['properties']['rhf_energy']['value']\n",
    "units = results['properties']['rhf_energy']['units']\n",
    "print(\"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
}
