{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Magnetic Structure Generation as Input for Initial DFT Calculations\n", "\n", "_Author: Shivam Beniwal_\n", "\n", "Date: 26 August 2021" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "## Introduction\n", "\n", "This notebook demonstrates a helpful method from the pymatgen magnetism analyzer module to add magnetic information to a structure as an input to a DFT run where the outcome of the calculation is dependent on the initial magnetic structure, and/or requires some magnetic moment as starting point. This can be extremely useful in case of newly predicted structures." ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "# Uncomment the subsequent lines in this cell to install dependencies for Google Colab.\n", "# !pip install pymatgen==2022.7.19\n", "from __future__ import annotations" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "from pymatgen.analysis.magnetism.analyzer import CollinearMagneticStructureAnalyzer\n", "from pymatgen.ext.matproj import MPRester\n", "from pymatgen.symmetry.analyzer import SpacegroupAnalyzer\n", "from pymatgen.util.testing import PymatgenTest" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Full Formula (Li4 Fe4 P4 O16)\n", "Reduced Formula: LiFePO4\n", "abc : 4.744800 6.065770 10.410370\n", "angles: 90.501790 90.000190 90.003620\n", "pbc : True True True\n", "Sites (28)\n", " # SP a b c\n", "--- ---- ------- ------- -------\n", " 0 Li 1e-05 0.99999 0.99999\n", " 1 Li 0.99999 0.5 1e-05\n", " 2 Li 0.49999 0.99999 0.5\n", " 3 Li 0.50002 0.5 0.5\n", " 4 Fe 0.52507 0.25344 0.21884\n", " 5 Fe 0.02508 0.74654 0.28116\n", " 6 Fe 0.97497 0.25346 0.71884\n", " 7 Fe 0.47493 0.74653 0.78116\n", " 8 P 0.58205 0.75169 0.09444\n", " 9 P 0.08207 0.2483 0.40556\n", " 10 P 0.91794 0.75174 0.59443\n", " 11 P 0.41793 0.24828 0.90557\n", " 12 O 0.29156 0.25112 0.04317\n", " 13 O 0.25851 0.75043 0.09622\n", " 14 O 0.71346 0.95594 0.1658\n", " 15 O 0.71627 0.5486 0.16562\n", " 16 O 0.21629 0.4514 0.33438\n", " 17 O 0.21345 0.04406 0.33419\n", " 18 O 0.75852 0.24955 0.40378\n", " 19 O 0.79157 0.7489 0.45682\n", " 20 O 0.20845 0.2511 0.54316\n", " 21 O 0.24149 0.75046 0.59622\n", " 22 O 0.78658 0.95602 0.66577\n", " 23 O 0.78367 0.54867 0.66564\n", " 24 O 0.28366 0.45133 0.83436\n", " 25 O 0.28658 0.04399 0.83423\n", " 26 O 0.74149 0.24958 0.90378\n", " 27 O 0.70842 0.74889 0.95684\n" ] } ], "source": [ "# Specify location of CIF on your device\n", "structure = PymatgenTest.get_structure(\"LiFePO4\")\n", "print(structure)" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## Add magmoms to initial structure\n", "\n", "Here we define magnetic moments of individual species present in the structure, if not already present. Refer to pymatgen [docs](https://pymatgen.org/pymatgen.analysis.magnetism.analyzer.html) for more information on options available for the argument overwrite_magmom_mode. Here we add magmoms for all sites in the structure irrespective of input structure, suitable for a spin-polarized (a.k.a 'magnetic') calculation.\n", "\n", "This is particularly interesting in case of either attempting a ferromagnetic calculation or an antiferromagnetic calculation." ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "magmom = CollinearMagneticStructureAnalyzer(\n", " structure, overwrite_magmom_mode=\"replace_all_if_undefined\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "# Assume an initial ferromagnetic order\n", "fm_structure = magmom.get_ferromagnetic_structure(make_primitive=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Full Formula (Li4 Fe4 P4 O16)\n", "Reduced Formula: LiFePO4\n", "abc : 4.744800 6.065770 10.410370\n", "angles: 90.501790 90.000190 90.003620\n", "pbc : True True True\n", "Sites (28)\n", " # SP a b c magmom\n", "--- ---- ------- ------- ------- --------\n", " 0 Li 1e-05 0.99999 0.99999 0\n", " 1 Li 0.99999 0.5 1e-05 0\n", " 2 Li 0.49999 0.99999 0.5 0\n", " 3 Li 0.50002 0.5 0.5 0\n", " 4 Fe 0.52507 0.25344 0.21884 5\n", " 5 Fe 0.02508 0.74654 0.28116 5\n", " 6 Fe 0.97497 0.25346 0.71884 5\n", " 7 Fe 0.47493 0.74653 0.78116 5\n", " 8 P 0.58205 0.75169 0.09444 0\n", " 9 P 0.08207 0.2483 0.40556 0\n", " 10 P 0.91794 0.75174 0.59443 0\n", " 11 P 0.41793 0.24828 0.90557 0\n", " 12 O 0.29156 0.25112 0.04317 0\n", " 13 O 0.25851 0.75043 0.09622 0\n", " 14 O 0.71346 0.95594 0.1658 0\n", " 15 O 0.71627 0.5486 0.16562 0\n", " 16 O 0.21629 0.4514 0.33438 0\n", " 17 O 0.21345 0.04406 0.33419 0\n", " 18 O 0.75852 0.24955 0.40378 0\n", " 19 O 0.79157 0.7489 0.45682 0\n", " 20 O 0.20845 0.2511 0.54316 0\n", " 21 O 0.24149 0.75046 0.59622 0\n", " 22 O 0.78658 0.95602 0.66577 0\n", " 23 O 0.78367 0.54867 0.66564 0\n", " 24 O 0.28366 0.45133 0.83436 0\n", " 25 O 0.28658 0.04399 0.83423 0\n", " 26 O 0.74149 0.24958 0.90378 0\n", " 27 O 0.70842 0.74889 0.95684 0\n" ] } ], "source": [ "print(fm_structure)" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ordering.FM\n" ] } ], "source": [ "order = magmom.ordering # Useful if magnetic order is unknown or not user-defined\n", "print(order)" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "## Get space group information" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2/m'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spa = SpacegroupAnalyzer(structure)\n", "spa.get_point_group_symbol()" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'P2_1/c'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spa.get_space_group_symbol()" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"# generated using pymatgen\\ndata_LiFePO4\\n_symmetry_space_group_name_H-M 'P 1'\\n_cell_length_a 4.74480000\\n_cell_length_b 6.06577000\\n_cell_length_c 10.41037000\\n_cell_angle_alpha 90.50179000\\n_cell_angle_beta 90.00019000\\n_cell_angle_gamma 90.00362000\\n_symmetry_Int_Tables_number 1\\n_chemical_formula_structural LiFePO4\\n_chemical_formula_sum 'Li4 Fe4 P4 O16'\\n_cell_volume 299.60796771\\n_cell_formula_units_Z 4\\nloop_\\n _symmetry_equiv_pos_site_id\\n _symmetry_equiv_pos_as_xyz\\n 1 'x, y, z'\\nloop_\\n _atom_site_type_symbol\\n _atom_site_label\\n _atom_site_symmetry_multiplicity\\n _atom_site_fract_x\\n _atom_site_fract_y\\n _atom_site_fract_z\\n _atom_site_occupancy\\n Li Li0 1 0.00001000 0.99999000 0.99999000 1.0\\n Li Li1 1 0.99999000 0.50000000 0.00001000 1.0\\n Li Li2 1 0.49999000 0.99999000 0.50000000 1.0\\n Li Li3 1 0.50002000 0.50000000 0.50000000 1.0\\n Fe Fe4 1 0.52507000 0.25344000 0.21884000 1.0\\n Fe Fe5 1 0.02508000 0.74654000 0.28116000 1.0\\n Fe Fe6 1 0.97497000 0.25346000 0.71884000 1.0\\n Fe Fe7 1 0.47493000 0.74653000 0.78116000 1.0\\n P P8 1 0.58205000 0.75169000 0.09444000 1.0\\n P P9 1 0.08207000 0.24830000 0.40556000 1.0\\n P P10 1 0.91794000 0.75174000 0.59443000 1.0\\n P P11 1 0.41793000 0.24828000 0.90557000 1.0\\n O O12 1 0.29156000 0.25112000 0.04317000 1.0\\n O O13 1 0.25851000 0.75043000 0.09622000 1.0\\n O O14 1 0.71346000 0.95594000 0.16580000 1.0\\n O O15 1 0.71627000 0.54860000 0.16562000 1.0\\n O O16 1 0.21629000 0.45140000 0.33438000 1.0\\n O O17 1 0.21345000 0.04406000 0.33419000 1.0\\n O O18 1 0.75852000 0.24955000 0.40378000 1.0\\n O O19 1 0.79157000 0.74890000 0.45682000 1.0\\n O O20 1 0.20845000 0.25110000 0.54316000 1.0\\n O O21 1 0.24149000 0.75046000 0.59622000 1.0\\n O O22 1 0.78658000 0.95602000 0.66577000 1.0\\n O O23 1 0.78367000 0.54867000 0.66564000 1.0\\n O O24 1 0.28366000 0.45133000 0.83436000 1.0\\n O O25 1 0.28658000 0.04399000 0.83423000 1.0\\n O O26 1 0.74149000 0.24958000 0.90378000 1.0\\n O O27 1 0.70842000 0.74889000 0.95684000 1.0\\nloop_\\n _atom_site_moment_label\\n _atom_site_moment_crystalaxis_x\\n _atom_site_moment_crystalaxis_y\\n _atom_site_moment_crystalaxis_z\\n Fe4 -5.00000000 0.00000000 0.00000000\\n Fe5 -5.00000000 0.00000000 0.00000000\\n Fe6 -5.00000000 0.00000000 0.00000000\\n Fe7 -5.00000000 0.00000000 0.00000000\\n\"" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fm_structure.to(filename=\"lfp.mcif\") # Save the structure in magCIF format." ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Full Formula (Li4 Fe4 P4 O16)\n", "Reduced Formula: LiFePO4\n", "abc : 4.744800 6.065770 10.410370\n", "angles: 90.501790 90.000190 90.003620\n", "pbc : True True True\n", "Sites (28)\n", " # SP a b c\n", "--- --------- ------- ------- -------\n", " 0 Li,spin=0 1e-05 0.99999 0.99999\n", " 1 Li,spin=0 0.99999 0.5 1e-05\n", " 2 Li,spin=0 0.49999 0.99999 0.5\n", " 3 Li,spin=0 0.50002 0.5 0.5\n", " 4 Fe,spin=5 0.52507 0.25344 0.21884\n", " 5 Fe,spin=5 0.02508 0.74654 0.28116\n", " 6 Fe,spin=5 0.97497 0.25346 0.71884\n", " 7 Fe,spin=5 0.47493 0.74653 0.78116\n", " 8 P,spin=0 0.58205 0.75169 0.09444\n", " 9 P,spin=0 0.08207 0.2483 0.40556\n", " 10 P,spin=0 0.91794 0.75174 0.59443\n", " 11 P,spin=0 0.41793 0.24828 0.90557\n", " 12 O,spin=0 0.29156 0.25112 0.04317\n", " 13 O,spin=0 0.25851 0.75043 0.09622\n", " 14 O,spin=0 0.71346 0.95594 0.1658\n", " 15 O,spin=0 0.71627 0.5486 0.16562\n", " 16 O,spin=0 0.21629 0.4514 0.33438\n", " 17 O,spin=0 0.21345 0.04406 0.33419\n", " 18 O,spin=0 0.75852 0.24955 0.40378\n", " 19 O,spin=0 0.79157 0.7489 0.45682\n", " 20 O,spin=0 0.20845 0.2511 0.54316\n", " 21 O,spin=0 0.24149 0.75046 0.59622\n", " 22 O,spin=0 0.78658 0.95602 0.66577\n", " 23 O,spin=0 0.78367 0.54867 0.66564\n", " 24 O,spin=0 0.28366 0.45133 0.83436\n", " 25 O,spin=0 0.28658 0.04399 0.83423\n", " 26 O,spin=0 0.74149 0.24958 0.90378\n", " 27 O,spin=0 0.70842 0.74889 0.95684\n" ] } ], "source": [ "spn_structure = (\n", " magmom.get_structure_with_spin()\n", ") # Returns spin decorated values in structure instead of magmom site properties\n", "print(spn_structure)" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "The above structure is saved as a magCIF with .mcif extension. This can be converted back to a CIF with relevant magnetic information associated with each site. OpenBabel does this easily, on command line write-\n", "```\n", "obabel -imcif lfp.mcif -ocif -O lfp.cif \n", "```" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## Analyze magnetic moment present in a calculated structure using MAPI\n", "\n", "In some cases, it might be useful to analyze magnetic behavior of a strucure from the Materials Project database. " ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "31adbcae7a3b46f6af0607be98587806", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Retrieving MaterialsDoc documents: 0%| | 0/1 [00:00" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mgmmnt.ordering" ] } ], "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" }, "vscode": { "interpreter": { "hash": "8022b3e932e045c760cb4633b91dd1cb8bc60d104ca9808334cbd1645adbe837" } } }, "nbformat": 4, "nbformat_minor": 5 }