{ "cells": [ { "cell_type": "markdown", "id": "c4478ee5-edc2-4069-b339-b12e42e4ac7c", "metadata": {}, "source": [ "The ASE .xyz format is a convenient format for storing single point properties of a collection of structures. It is commonly used and supported by academic machine learning potential projects, so you may find it useful.\n", "\n", "Here, we will initialize/save a ResultsImporter object from/to ASE .xyz format. \n", "\n", "For advanced usage you can also work directly with lists of ASE Atoms, and the ParAMS ``JobCollection`` and ``DataSet`` classes. See the documentation for ``params_to_ase``, and ``ase_to_params``." ] }, { "cell_type": "code", "execution_count": 1, "id": "df986d72-f26b-4a3c-b82a-f9bcdaf2da7d", "metadata": {}, "outputs": [], "source": [ "from scm.params import ResultsImporter\n", "import os" ] }, { "cell_type": "markdown", "id": "c9dc5d3b-823a-42a5-bc85-916b480c8293", "metadata": {}, "source": [ "## One-liner conversion from ParAMS .yaml to ASE .xyz\n", "\n", "Here we use the examples directory that already contains files in the ParAMS .yaml format:" ] }, { "cell_type": "code", "execution_count": 2, "id": "d5b25a3c-c765-41dc-bfe5-cf486264e587", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[PosixPath('/home/hellstrom/adfhome/scripting/scm/params/doc/source/examples/convert_ase/training_set.xyz'), PosixPath('/home/hellstrom/adfhome/scripting/scm/params/doc/source/examples/convert_ase/validation_set.xyz')]\n" ] } ], "source": [ "yaml_dir = os.path.expandvars(\"$AMSHOME/scripting/scm/params/examples/DataSets/LiquidAr_32Atoms_100Frames\")\n", "\n", "# put training_set.xyz, validation_set.xyz in current working directory\n", "xyz_target_dir = os.getcwd()\n", "\n", "xyz_files = ResultsImporter.from_yaml(yaml_dir).store_ase(xyz_target_dir, format=\"extxyz\")\n", "\n", "print(xyz_files)" ] }, { "cell_type": "markdown", "id": "39068ef8-c530-4466-8c7c-3ad1bb374bd0", "metadata": {}, "source": [ "## One-liner conversion from ASE .xyz to ParAMS .yaml" ] }, { "cell_type": "code", "execution_count": 12, "id": "50ca1d04-3c0d-4232-ad16-52b2d2bb47a6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['yaml_ref_data/job_collection.yaml', 'yaml_ref_data/results_importer_settings.yaml', 'yaml_ref_data/training_set.yaml', 'yaml_ref_data/validation_set.yaml']\n" ] } ], "source": [ "xyz_dir = os.getcwd()\n", "\n", "# put job_collection.yaml etc. in a folder yaml_ref_data\n", "yaml_target_dir = \"yaml_ref_data\"\n", "\n", "yaml_files = ResultsImporter.from_ase(f\"{xyz_dir}/training_set.xyz\", f\"{xyz_dir}/validation_set.xyz\").store(\n", " yaml_target_dir, backup=False\n", ")\n", "\n", "print(yaml_files)" ] }, { "cell_type": "markdown", "id": "1fd2c480-660a-4e2d-bb7e-1f1f00321582", "metadata": {}, "source": [ "## More about ASE .xyz format\n", "\n", "Let's look at the first few lines of the ASE .xyz files:" ] }, { "cell_type": "code", "execution_count": 4, "id": "632b8317-6fed-45df-a162-c5fb4216338c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- first 5 lines of /home/hellstrom/adfhome/scripting/scm/params/doc/source/examples/convert_ase/training_set.xyz ---\n", "32\n", "Lattice=\"10.52 0.0 0.0 0.0 10.52 0.0 0.0 0.0 10.52\" Properties=species:S:1:pos:R:3:forces:R:3 nAtoms=32.0 energy_weight=1.0 forces_weights=1.0 energy=-5354.689144659606 pbc=\"T T T\"\n", "Ar 5.22816630 -0.09999816 7.90156704 0.00827748 0.00316181 -0.00109192\n", "Ar 5.36838670 0.11217830 2.61448917 -0.00296065 -0.01538116 0.00091621\n", "Ar 5.47472123 5.39235886 7.71471647 -0.02373655 -0.02892664 0.00741117\n", "\n", "--- first 5 lines of /home/hellstrom/adfhome/scripting/scm/params/doc/source/examples/convert_ase/validation_set.xyz ---\n", "32\n", "Lattice=\"10.52 0.0 0.0 0.0 10.52 0.0 0.0 0.0 10.52\" Properties=species:S:1:pos:R:3:forces:R:3 nAtoms=32.0 energy_weight=1.0 forces_weights=1.0 energy=-5354.752060382252 pbc=\"T T T\"\n", "Ar 5.26000000 0.00000000 7.89000000 -0.00000013 0.00000037 -0.00000037\n", "Ar 5.26000000 0.00000000 2.63000000 -0.00000013 0.00000037 0.00000013\n", "Ar 5.26000000 5.26000000 7.89000000 -0.00000013 -0.00000013 -0.00000037\n", "\n" ] } ], "source": [ "for file in xyz_files:\n", " print(f\"--- first 5 lines of {file} ---\")\n", " with open(file) as f:\n", " print(\"\".join(f.readlines()[:5]))" ] }, { "cell_type": "markdown", "id": "c7e9f791-c024-4490-850c-78a99756869b", "metadata": {}, "source": [ "To read the ASE format, use ``ase.io.read(\"filename.xyz\", \":\")`` to get a list of ASE Atoms. For more details, see the ASE documentation." ] }, { "cell_type": "code", "execution_count": 5, "id": "99439a54-4965-4e31-9490-fd9f6cf65568", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First structure:\n", "Atoms(symbols='Ar32', pbc=True, cell=[10.52, 10.52, 10.52], forces=..., calculator=SinglePointCalculator(...))\n", "Energy: -5354.689144659606\n" ] } ], "source": [ "import ase.io\n", "\n", "list_of_ase_atoms = ase.io.read(xyz_files[0], \":\") # files[0] == \"training_set.xyz\"\n", "atoms = list_of_ase_atoms[0]\n", "print(\"First structure:\")\n", "print(atoms)\n", "print(f\"Energy: {atoms.get_potential_energy()}\")" ] }, { "cell_type": "markdown", "id": "b168b70e-0932-4cc1-ab0c-9c89e988eed1", "metadata": {}, "source": [ "### More about ParAMS ResultsImporter initialized from ASE .xyz" ] }, { "cell_type": "code", "execution_count": 6, "id": "b38eecf5-0d2b-4db9-ab3f-9b910241ca32", "metadata": {}, "outputs": [], "source": [ "ri = ResultsImporter.from_ase(f\"{xyz_dir}/training_set.xyz\", f\"{xyz_dir}/validation_set.xyz\")" ] }, { "cell_type": "markdown", "id": "f1dd0e9a-18e9-432a-97c7-7b29ae61a98f", "metadata": {}, "source": [ "The structure is stored in the job collection:" ] }, { "cell_type": "code", "execution_count": 7, "id": "e2ac1518-7814-4255-9fa6-55806a64f37f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ID: training_set0001\n", "ReferenceEngineID: None\n", "AMSInput: |\n", " properties\n", " gradients yes\n", " End\n", " system\n", " Atoms\n", " Ar 5.2281663000 -0.0999981600 7.9015670400 \n", " Ar 5.3683867000 0.1121783000 2.6144891700 \n", " Ar 5.4747212300 5.3923588600 7.7147164700 \n", " Ar 5.0988082900 5.4349847800 2.7366203700 \n", " Ar 7.9476603300 7.6156524900 8.0580968000 \n", " Ar 8.0131968700 8.0720767000 2.6497474100 \n", " Ar 7.8281713200 2.4490447200 7.8676092300 \n", " Ar 8.0186369800 2.4216024300 2.7003118400 \n", " Ar 2.6755162100 7.8807576800 7.8447710500 \n", " Ar 2.5484574000 7.8109744500 2.4632325200 \n", " Ar 2.5454749200 2.7008728300 7.8508382500 \n", " Ar 2.8017327000 2.4629101300 2.6664112100 \n", " Ar 7.8474635500 -0.0973865700 0.0171454400 \n", " Ar 7.8797231100 0.0391989100 5.0441430600 \n", " Ar 7.8726237500 5.3153487100 -0.2533762600 \n", " Ar 7.9047282900 5.2404472900 5.0502125000 \n", " Ar 2.9247115500 -0.2226033900 0.1389427600 \n", " Ar 2.6890580100 0.1416854000 5.4569524000 \n", " Ar 2.7625038100 5.4126025300 -0.0144396700 \n", " Ar 2.4993060000 5.1786998200 5.3291454800 \n", " Ar 0.2830784700 7.9023484600 0.0274749500 \n", " Ar -0.1270171800 8.0412894300 5.3905843300 \n", " Ar -0.2138571200 2.5109171800 0.0077376600 \n", " Ar -0.1896415200 2.7934612700 5.3088064400 \n", " Ar 5.1592133300 7.9576884700 0.0444941600 \n", " Ar 5.0858600300 7.7810555100 5.3596734400 \n", " Ar 5.2313185300 2.6624672300 -0.0623109300 \n", " Ar 5.1917182600 2.6762513100 5.0453924000 \n", " Ar -0.0610246500 0.0066595600 7.9748565400 \n", " Ar 0.0874271200 -0.0476604500 2.7572524100 \n", " Ar -0.0099775900 5.2074930100 7.9102005200 \n", " Ar -0.1261450000 5.4866211100 2.6387010300 \n", " End\n", " Lattice\n", " 10.5200000000 0.0000000000 0.0000000000\n", " 0.0000000000 10.5200000000 0.0000000000\n", " 0.0000000000 0.0000000000 10.5200000000\n", " End\n", " End\n", " task singlepoint\n", "\n" ] } ], "source": [ "for name in ri.job_collection:\n", " print(f\"ID: {name}\")\n", " print(ri.job_collection[name])\n", " break" ] }, { "cell_type": "markdown", "id": "8ee7777d-f2b8-4185-9dce-98f6982fa66d", "metadata": {}, "source": [ "The reference values are stored in the data sets:" ] }, { "cell_type": "code", "execution_count": 8, "id": "a32808a4-dbd8-438d-9d7d-73b3726106e8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "---\n", "Expression: energy('training_set0001')\n", "Weight: 1.0\n", "ReferenceValue: -5354.689144659606\n", "Unit: eV, 27.211386245988\n", "\n" ] } ], "source": [ "# print the first training set entry\n", "for ds_entry in ri.get_data_set(\"training_set\"):\n", " print(ds_entry)\n", " break" ] }, { "cell_type": "code", "execution_count": 9, "id": "df5f0ab9-0ebd-40dd-b916-e774c8be926f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "---\n", "Expression: energy('validation_set0001')\n", "Weight: 1.0\n", "ReferenceValue: -5354.752060382252\n", "Unit: eV, 27.211386245988\n", "\n" ] } ], "source": [ "# print the first validation set entry\n", "for ds_entry in ri.get_data_set(\"validation_set\"):\n", " print(ds_entry)\n", " break" ] }, { "cell_type": "markdown", "id": "b9ae739a-9568-4a61-a11e-1603b784c83c", "metadata": {}, "source": [ "When you initialize a ResultsImporter using ``from_ase``, it will automatically set the ResultsImporter units to the ASE units. If you use the results importer to import new data, the data will be stored in the ASE units (eV, eV/angstrom, etc.)." ] }, { "cell_type": "code", "execution_count": 11, "id": "8df9dae7-a198-4ec0-aec2-2365cd69c6f2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ri.settings['units']['energy']=('eV', 27.211386245988)\n", "ri.settings['units']['forces']=('eV/angstrom', 51.422067476325886)\n" ] } ], "source": [ "print(f\"{ri.settings['units']['energy']=}\")\n", "print(f\"{ri.settings['units']['forces']=}\")" ] } ], "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }