{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualize modulated structures" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "962bf063b664449192a3b90ce346efe0", "version_major": 2, "version_minor": 0 }, "text/plain": [] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from pathlib import Path\n", "\n", "import numpy as np\n", "import phonopy\n", "from phonopy import Phonopy\n", "from phonopy.interface.vasp import write_vasp\n", "from pymatgen.core import Structure\n", "from pymatgen.io.phonopy import get_pmg_structure, get_phonopy_structure\n", "\n", "from spgrep_modulation.modulation import Modulation\n", "from spgrep_modulation.visualize import viewer, get_local_and_ghost_sites" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "path = Path().resolve().parent.parent / \"tests\" / \"phonopy_mp-2998.yaml.xz\"\n", "ph = phonopy.load(path)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "qpoint = [0.5, 0, 0] # X point\n", "dimension = [2, 2, 2]\n", "\n", "md = Modulation.with_supercell_and_symmetry_search(\n", " dynamical_matrix=ph.dynamical_matrix,\n", " supercell_matrix=dimension,\n", " qpoint=qpoint,\n", " factor=ph.unit_conversion_factor,\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Frequency (THz): -4.88\n", "Irrep shape: (16, 2, 2)\n" ] } ], "source": [ "# Degenerated imaginary mode\n", "frequency_index = 0\n", "dim = md.eigenspaces[frequency_index][2].shape[1]\n", "print(f\"Frequency (THz): {md.eigvals_to_frequencies(md.eigenspaces[frequency_index][0]):.2f}\")\n", "print(f\"Irrep shape: {md.eigenspaces[frequency_index][2].shape}\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def get_modulated_cells(md: Modulation, frequency_index, idx, max_amplitude, num):\n", " argument = 0\n", " symmetry_adapted_eigvec = md.eigenspaces[frequency_index][1][idx]\n", " amplitudes = np.linspace(-max_amplitude, max_amplitude, num=num, endpoint=True)\n", " modulated_cells = []\n", " for amplitude in amplitudes:\n", " modulation = md._get_displacements(symmetry_adapted_eigvec, amplitude, argument=0)\n", " scaled_modulation = amplitude / np.max(np.abs(modulation)) * modulation\n", " modulated = md.apply_modulation_to_supercell(scaled_modulation)\n", " modulated_cells.append(modulated)\n", " return amplitudes, modulated_cells\n", "\n", "\n", "def save_modulated_cells(modulated_cells, root_dir: str):\n", " root = Path(root_dir)\n", " root.mkdir(exist_ok=True)\n", "\n", " num = len(modulated_cells)\n", " # shift list to start with non-modulated cell\n", " shifted = modulated_cells[-num // 2 :] + modulated_cells[: num // 2]\n", " for i, cell in enumerate(shifted):\n", " structure = get_pmg_structure(cell)\n", " structure_with_ghosts = Structure.from_sites(get_local_and_ghost_sites(structure))\n", " cell_with_ghosts = get_phonopy_structure(structure_with_ghosts)\n", "\n", " write_vasp(root / f\"POSCAR-{i:03d}\", cell_with_ghosts)\n", " write_vasp(root / f\"POSCAR-{2 * num - 1 - i:03d}\", cell_with_ghosts)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "amplitude = 1.0\n", "num = 16\n", "for idx in range(dim):\n", " _, modulated_cells = get_modulated_cells(md, frequency_index, idx, amplitude, num=num)\n", " save_modulated_cells(modulated_cells, f\"disp-{idx}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.4 ('spgrep')", "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.10.4" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "88c77d0dd5409a9a505b149d1d516cb944fb2ee79f549abf35dc2e7bd6c12498" } } }, "nbformat": 4, "nbformat_minor": 2 }