{ "cells": [ { "cell_type": "markdown", "id": "7fb27b941602401d91542211134fc71a", "metadata": {}, "source": [ "## Convert from XYZ/OUTCAR to RKF\n", "This example starts from an external multi-frame XYZ trajectory, converts it to `ams.rkf`, and enriches it with molecule and bond information by running a replay job.\n" ] }, { "cell_type": "markdown", "id": "acae54e37e7d407bbb7b55eff062a284", "metadata": {}, "source": [ "### Initial Imports\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "9a63283cbaf04dbcab1f6479b197f3a8", "metadata": {}, "outputs": [], "source": [ "import os\n", "import subprocess\n", "from pathlib import Path\n", "\n", "from scm.plams import AMSJob, Settings, delete_job, file_to_traj, traj_to_rkf" ] }, { "cell_type": "markdown", "id": "8dd0d8092fe74a7c96281538738b07e2", "metadata": {}, "source": [ "### Define XYZ trajectory input\n" ] }, { "cell_type": "markdown", "id": "26f6eb25-2da9-4ca2-8bc4-23a8f887f4f9", "metadata": {}, "source": [ "We first write a very short example .xyz file:" ] }, { "cell_type": "code", "execution_count": 2, "id": "72eea5119410473aa328ad9291626812", "metadata": {}, "outputs": [], "source": [ "xyz_trajectory = \"\"\"6\n", "frame 1\n", "O 2.01504900 2.16272700 2.99552700\n", "H 2.96307600 2.41195100 2.99244800\n", "H 1.42963800 2.95106300 2.98957900\n", "O 0.95320300 1.47810200 1.19941600\n", "H 1.00821200 2.41101200 0.90355900\n", "H 1.76505900 0.97420300 0.97316100\n", "6\n", "frame 2\n", "O 2.01505786 2.16226811 2.99837195\n", "H 2.95676994 2.39860472 2.98680480\n", "H 1.42248605 2.96034695 2.99335021\n", "O 0.95325699 1.47877442 1.19649020\n", "H 1.01375745 2.41610845 0.89913436\n", "H 1.77197403 0.96978002 0.98074075\n", "6\n", "frame 3\n", "O 2.01512004 2.16202221 3.00146369\n", "H 2.94874909 2.38589811 2.98026036\n", "H 1.41907239 2.96804981 2.99718816\n", "O 0.95316789 1.47931163 1.19345976\n", "H 1.02092464 2.41742729 0.89396582\n", "H 1.77666876 0.96884156 0.98764296\n", "6\n", "frame 4\n", "O 2.01516130 2.16199995 3.00479257\n", "H 2.93983749 2.37412231 2.97284825\n", "H 1.41975474 2.97371643 3.00104071\n", "O 0.95294917 1.47971792 1.19032629\n", "H 1.02967864 2.41481091 0.88814023\n", "H 1.77896035 0.97147237 0.99392690\"\"\"\n", "\n", "xyz_input = Path(\"water_frames.xyz\")\n", "xyz_input.write_text(xyz_trajectory + \"\\n\");" ] }, { "cell_type": "markdown", "id": "8edb47106e1a46a883d545849b8ab81b", "metadata": {}, "source": [ "### Convert to XYZ to RKF" ] }, { "cell_type": "markdown", "id": "1d941154-fe6d-461a-a860-9312b705e485", "metadata": {}, "source": [ "We then convert the .xyz file to a .rkf via an intermediate .traj file:" ] }, { "cell_type": "code", "execution_count": 3, "id": "f101a6a0-a023-413a-a27a-7b000951d5a1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[2.0151613 , 2.16199995, 3.00479257],\n", " [2.93983749, 2.37412231, 2.97284825],\n", " [1.41975474, 2.97371643, 3.00104071],\n", " [0.95294917, 1.47971792, 1.19032629],\n", " [1.02967864, 2.41481091, 0.88814023],\n", " [1.77896035, 0.97147237, 0.9939269 ]]),\n", " Cell([0.0, 0.0, 0.0]))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rkf_output = \"water_frames.rkf\"\n", "\n", "traj_temp = Path(\"water_frames.traj\")\n", "file_to_traj(str(xyz_input), str(traj_temp))\n", "traj_to_rkf(str(traj_temp), rkf_output, task=\"moleculardynamics\", timestep=0.5)" ] }, { "cell_type": "markdown", "id": "8cec5f77-aba9-49b2-90e1-c4999617111c", "metadata": {}, "source": [ "### Run Replay Job" ] }, { "cell_type": "markdown", "id": "35a5b61d-8b8a-4628-99b1-895ea1fde88c", "metadata": {}, "source": [ "Finally, we add molecule and bonding information by running a replay job:" ] }, { "cell_type": "code", "execution_count": 4, "id": "53664559-7bbb-42a2-8280-764b1a0ba7d1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[02.04|10:09:39] JOB replay_for_bond_guessing STARTED\n", "[02.04|10:09:39] JOB replay_for_bond_guessing RUNNING\n", "[02.04|10:09:40] JOB replay_for_bond_guessing FINISHED\n", "[02.04|10:09:40] JOB replay_for_bond_guessing SUCCESSFUL\n" ] } ], "source": [ "replay_settings = Settings()\n", "replay_settings.input.ams.task = \"replay\"\n", "replay_settings.input.lennardjones\n", "replay_settings.input.ams.replay.file = os.path.abspath(rkf_output)\n", "replay_settings.input.ams.properties.molecules = \"yes\"\n", "replay_settings.input.ams.properties.bondorders = \"yes\"\n", "replay_settings.runscript.nproc = 1\n", "\n", "replay_job = AMSJob(settings=replay_settings, name=\"replay_for_bond_guessing\")\n", "replay_results = replay_job.run()\n", "if not replay_results.ok():\n", " raise RuntimeError(\"Replay job failed\")\n", "\n", "cpkf = Path(os.path.expandvars(\"$AMSBIN/cpkf\"))\n", "if not cpkf.exists():\n", " raise FileNotFoundError(f\"Could not find cpkf at {cpkf}\")\n", "\n", "subprocess.run(\n", " [\"sh\", str(cpkf), replay_results.rkfpath(), rkf_output, \"History\", \"Molecules\"],\n", " check=True,\n", ")\n", "delete_job(replay_job)\n", "traj_temp.unlink(missing_ok=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "a48285d2-95e0-47fa-93a1-e69078fab1de", "metadata": {}, "outputs": [], "source": [] } ], "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.8.16" } }, "nbformat": 4, "nbformat_minor": 5 }