{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "08ee4c09-7fb5-4ce1-a7a3-5c5c52a6d4d5", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ase import Atoms, Atom\n", "from ase.io import read, write\n", "from ase.data import chemical_symbols, covalent_radii, vdw_alvarez\n", "from ase.parallel import paropen as open\n", "from gpaw import GPAW, PW, FermiDirac, LCAO\n", "from gpaw import Davidson\n", "from gpaw import Mixer, MixerSum, MixerDif\n", "from gpaw.directmin.etdm_lcao import LCAOETDM\n", "from gpaw.cdft.cdft import CDFT\n", "from pathlib import Path\n", "import os\n", "import numpy as np\n", "from pymatgen.core import Element\n", "from tqdm.auto import tqdm\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "id": "90887faa-1601-4c4c-9c44-d16731471d7f", "metadata": { "tags": [] }, "outputs": [], "source": [ "\n", "magnetism = 'NM'\n", "\n", "for symbol in tqdm(chemical_symbols):\n", " \n", " s = set([symbol])\n", " \n", " if 'X' in s:\n", " continue\n", " \n", " try:\n", " atom = Atom(symbol)\n", " rmin = covalent_radii[atom.number] * 2 * 0.6\n", " rvdw = vdw_alvarez.vdw_radii[atom.number] if atom.number < len(vdw_alvarez.vdw_radii) else np.nan \n", " rmax = 3.1 * rvdw if not np.isnan(rvdw) else 6\n", " rstep = 0.2 #if rmin < 1 else 0.4\n", "\n", " a = 2 * rmax\n", "\n", " npts = int((rmax - rmin)/rstep)\n", "\n", " rs = np.linspace(rmin, rmax, npts)\n", " e = np.zeros_like(rs)\n", "\n", " da = symbol + symbol\n", "\n", " out_dir = Path(str(da + f\"_{magnetism}\"))\n", "\n", " os.makedirs(out_dir, exist_ok=True)\n", "\n", " skip = 0\n", " \n", " element = Element(symbol)\n", " \n", " try:\n", " m = element.valence[1]\n", " if element.valence == (0, 2):\n", " m = 0\n", " except:\n", " m = 0\n", " \n", " \n", " r = rs[0]\n", " \n", " positions = [\n", " [a/2-r/2, a/2, a/2],\n", " [a/2+r/2, a/2, a/2],\n", " ]\n", " \n", " if magnetism == 'FM':\n", " if m == 0:\n", " continue\n", " magmoms = [m, m]\n", " elif magnetism == 'AFM':\n", " if m == 0:\n", " continue\n", " magmoms = [m, -m]\n", " elif magnetism == 'NM':\n", " magmoms = [0, 0]\n", " \n", " traj_fpath = out_dir / \"traj.extxyz\"\n", "\n", " if traj_fpath.exists():\n", " traj = read(traj_fpath, index=\":\")\n", " skip = len(traj)\n", " atoms = traj[-1]\n", " else:\n", " # Create the unit cell with two atoms\n", " atoms = Atoms(\n", " da, \n", " positions=positions,\n", " magmoms=magmoms,\n", " cell=[a, a+0.001, a+0.002], \n", " pbc=True\n", " )\n", " \n", " print(atoms)\n", " \n", " restart_fpath = out_dir / 'restart.gpw'\n", "\n", " calc = GPAW(\n", " mode=PW(1000),\n", " xc='PBE',\n", " spinpol=True,\n", " # basis='dzp'\n", " basis='szp(dzp)',\n", " # h=0.25,\n", " # nbands=0 if element.is_noble_gas else '110%',\n", " hund=False,\n", " mixer=MixerDif(0.01, 1, 1) if element.is_transition_metal else MixerDif(0.25, 3, 10),\n", " eigensolver='cg', #'rmm-diis', #Davidson(3), # This solver can parallelize over bands Davidson(3), #\n", " occupations=FermiDirac(0.0, fixmagmom=False), # if not element.is_metal else FermiDirac(0.2, fixmagmom=False),\n", " # eigensolver=LCAOETDM(),\n", " # # searchdir_algo={'name': 'l-bfgs-p', 'memory': 10}),\n", " # occupations={'name': 'fixed-uniform'},\n", " # mixer={'backend': 'no-mixing'},\n", " # nbands='nao',\n", " symmetry={'point_group': False},\n", " txt=out_dir / 'out.txt',\n", " convergence={\n", " 'eigenstates': 1e-5,\n", " 'density': 5e-3,\n", " 'energy': 5e-4,\n", " # 'bands': 4\n", " },\n", " # {'energy': 0.0005, # eV / electron\n", " # 'density': 1.0e-4, # electrons / electron\n", " # 'eigenstates': 4.0e-8, # eV^2 / electron\n", " # 'bands': 'occupied'}\n", " )\n", " # calc.attach(calc.write, 10, restart_fpath, mode='all')\n", "\n", " atoms.calc = calc\n", " \n", " # cdft = CDFT(calc=calc, atoms=atoms, spinspin_regions= \n", " # atoms.calc = cdft\n", "\n", " for i, r in enumerate(tqdm(np.flip(rs))):\n", "\n", " if i < skip:\n", " continue\n", "\n", " positions = [\n", " [a/2-r/2, a/2, a/2],\n", " [a/2+r/2, a/2, a/2],\n", " ]\n", " \n", " # if i > 0: \n", " # magmoms = atoms.get_magnetic_moments()\n", " # m = min(abs(magmoms[0])*1.2, m)\n", " # magmoms = magmoms*m/np.abs(magmoms)\n", " \n", " atoms.set_initial_magnetic_moments(magmoms)\n", " \n", " atoms.set_positions(positions)\n", "\n", " e[i] = atoms.get_potential_energy()\n", " \n", " atoms.calc.results.update({\n", " \"forces\": atoms.get_forces()\n", " })\n", "\n", " write(traj_fpath, atoms, append=\"a\")\n", " except Exception as e:\n", " print(e)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a0ac2c09-370b-4fdd-bf74-ea5c4ade0215", "metadata": {}, "outputs": [], "source": [ "\n", "\n", "df = pd.DataFrame(columns=['name', 'method', 'R', 'E', 'F', 'S^2'])\n", "\n", "\n", "\n", "for symbol in tqdm(chemical_symbols):\n", " \n", " for magnetism in ['AFM', 'FM', 'NM']:\n", " \n", " da = symbol + symbol\n", "\n", " # out_dir = Path(da)\n", " out_dir = Path(str(da + f\"_{magnetism}\"))\n", "\n", " traj_fpath = out_dir / \"traj.extxyz\"\n", "\n", " if traj_fpath.exists():\n", " traj = read(traj_fpath, index=\":\")\n", " else:\n", " continue\n", "\n", " Rs, Es, Fs, S2s = [], [], [], []\n", " for atoms in traj:\n", "\n", " vec = atoms.positions[1] - atoms.positions[0]\n", " r = np.linalg.norm(vec)\n", " e = atoms.get_potential_energy()\n", " # f = np.inner(vec/r, atoms.get_forces()[1])\n", " # s2 = np.mean(np.power(atoms.get_magnetic_moments(), 2))\n", "\n", " Rs.append(r)\n", " Es.append(e)\n", " # Fs.append(f)\n", " # S2s.append(s2)\n", "\n", " data = {\n", " 'name': da,\n", " 'method': f'GGA-PBE (GPAW): {magnetism}',\n", " 'R': Rs,\n", " 'E': Es,\n", " 'F': Fs,\n", " 'S^2': S2s\n", " }\n", "\n", " df = pd.concat([df, pd.DataFrame([data])], ignore_index=True)\n", "\n", "json_fpath = 'homonuclear-diatomics.json'\n", "\n", "df.to_json(json_fpath, orient='records') " ] }, { "cell_type": "code", "execution_count": null, "id": "5d4a7312-a619-411f-9c6f-36c40cd47a34", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "mlip-arena", "language": "python", "name": "mlip-arena" }, "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.8" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }