{ "cells": [ { "cell_type": "markdown", "source": [ "# AtomsBase integration" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "[AtomsBase.jl](https://github.com/JuliaMolSim/AtomsBase.jl) is a common interface\n", "for representing atomic structures in Julia. DFTK directly supports using such\n", "structures to run a calculation as is demonstrated here." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using DFTK" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "## Feeding an AtomsBase AbstractSystem to DFTK\n", "In this example we construct a silicon system using the `ase.build.bulk` routine\n", "from the [atomistic simulation environment](https://wiki.fysik.dtu.dk/ase/index.html)\n", "(ASE), which is exposed by [ASEconvert](https://github.com/mfherbst/ASEconvert.jl)\n", "as an AtomsBase `AbstractSystem`." ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "FlexibleSystem(Si₂, periodic = TTT):\n bounding_box : [ 0 2.715 2.715;\n 2.715 0 2.715;\n 2.715 2.715 0]u\"Å\"\n\n Atom(Si, [ 0, 0, 0]u\"Å\")\n Atom(Si, [ 1.3575, 1.3575, 1.3575]u\"Å\")\n\n \n \n \n \n Si \n \n Si \n \n \n \n \n" }, "metadata": {}, "execution_count": 2 } ], "cell_type": "code", "source": [ "# Construct bulk system and convert to an AbstractSystem\n", "using ASEconvert\n", "system_ase = ase.build.bulk(\"Si\")\n", "system = pyconvert(AbstractSystem, system_ase)" ], "metadata": {}, "execution_count": 2 }, { "cell_type": "markdown", "source": [ "To use an AbstractSystem in DFTK, we attach pseudopotentials, construct a DFT model,\n", "discretise and solve:" ], "metadata": {} }, { "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n Energy log10(ΔE) log10(Δρ) Diag Δtime\n", "--- --------------- --------- --------- ---- ------\n", " 1 -7.921707077037 -0.69 5.6 \n", " 2 -7.926168133060 -2.35 -1.22 1.0 252ms\n", " 3 -7.926837334351 -3.17 -2.37 1.6 235ms\n", " 4 -7.926861518265 -4.62 -3.03 3.0 262ms\n", " 5 -7.926861642237 -6.91 -3.38 1.9 248ms\n", " 6 -7.926861669582 -7.56 -3.78 1.9 219ms\n", " 7 -7.926861680503 -7.96 -4.34 1.2 203ms\n", " 8 -7.926861681759 -8.90 -4.85 2.1 228ms\n", " 9 -7.926861681859 -10.00 -5.23 1.9 246ms\n", " 10 -7.926861681871 -10.91 -5.86 1.8 219ms\n", " 11 -7.926861681873 -11.83 -6.98 2.0 234ms\n", " 12 -7.926861681873 -13.56 -7.34 3.5 284ms\n", " 13 -7.926861681873 -14.35 -8.26 1.2 207ms\n" ] } ], "cell_type": "code", "source": [ "system = attach_psp(system; Si=\"hgh/lda/si-q4\")\n", "\n", "model = model_LDA(system; temperature=1e-3)\n", "basis = PlaneWaveBasis(model; Ecut=15, kgrid=[4, 4, 4])\n", "scfres = self_consistent_field(basis, tol=1e-8);" ], "metadata": {}, "execution_count": 3 }, { "cell_type": "markdown", "source": [ "If we did not want to use ASE we could of course use any other package\n", "which yields an AbstractSystem object. This includes:" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Reading a system using AtomsIO" ], "metadata": {} }, { "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n Energy log10(ΔE) log10(Δρ) Diag Δtime\n", "--- --------------- --------- --------- ---- ------\n", " 1 -7.921703754529 -0.69 5.8 \n", " 2 -7.926164546407 -2.35 -1.22 1.0 213ms\n", " 3 -7.926836478339 -3.17 -2.37 1.6 232ms\n", " 4 -7.926861454194 -4.60 -3.00 2.8 293ms\n", " 5 -7.926861630589 -6.75 -3.33 1.9 221ms\n", " 6 -7.926861665512 -7.46 -3.70 1.8 218ms\n", " 7 -7.926861680459 -7.83 -4.34 1.2 208ms\n", " 8 -7.926861681789 -8.88 -4.89 2.0 254ms\n", " 9 -7.926861681865 -10.12 -5.36 1.9 223ms\n", " 10 -7.926861681872 -11.16 -6.00 2.0 227ms\n", " 11 -7.926861681873 -12.12 -6.86 2.2 240ms\n", " 12 -7.926861681873 -13.59 -7.40 2.5 260ms\n", " 13 -7.926861681873 -14.75 -8.11 1.9 221ms\n" ] } ], "cell_type": "code", "source": [ "using AtomsIO\n", "\n", "# Read a file using [AtomsIO](https://github.com/mfherbst/AtomsIO.jl),\n", "# which directly yields an AbstractSystem.\n", "system = load_system(\"Si.extxyz\")\n", "\n", "# Now run the LDA calculation:\n", "system = attach_psp(system; Si=\"hgh/lda/si-q4\")\n", "model = model_LDA(system; temperature=1e-3)\n", "basis = PlaneWaveBasis(model; Ecut=15, kgrid=[4, 4, 4])\n", "scfres = self_consistent_field(basis, tol=1e-8);" ], "metadata": {}, "execution_count": 4 }, { "cell_type": "markdown", "source": [ "The same could be achieved using [ExtXYZ](https://github.com/libAtoms/ExtXYZ.jl)\n", "by `system = Atoms(read_frame(\"Si.extxyz\"))`,\n", "since the `ExtXYZ.Atoms` object is directly AtomsBase-compatible." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Directly setting up a system in AtomsBase" ], "metadata": {} }, { "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n Energy log10(ΔE) log10(Δρ) Diag Δtime\n", "--- --------------- --------- --------- ---- ------\n", " 1 -7.921713359642 -0.69 5.9 \n", " 2 -7.926165461257 -2.35 -1.22 1.0 205ms\n", " 3 -7.926840189044 -3.17 -2.37 1.9 268ms\n", " 4 -7.926864919612 -4.61 -3.01 2.6 252ms\n", " 5 -7.926865039504 -6.92 -3.33 1.6 204ms\n", " 6 -7.926865077386 -7.42 -3.71 1.6 208ms\n", " 7 -7.926865091851 -7.84 -4.42 1.0 185ms\n" ] } ], "cell_type": "code", "source": [ "using AtomsBase\n", "using Unitful\n", "using UnitfulAtomic\n", "\n", "# Construct a system in the AtomsBase world\n", "a = 10.26u\"bohr\" # Silicon lattice constant\n", "lattice = a / 2 * [[0, 1, 1.], # Lattice as vector of vectors\n", " [1, 0, 1.],\n", " [1, 1, 0.]]\n", "atoms = [:Si => ones(3)/8, :Si => -ones(3)/8]\n", "system = periodic_system(atoms, lattice; fractional=true)\n", "\n", "# Now run the LDA calculation:\n", "system = attach_psp(system; Si=\"hgh/lda/si-q4\")\n", "model = model_LDA(system; temperature=1e-3)\n", "basis = PlaneWaveBasis(model; Ecut=15, kgrid=[4, 4, 4])\n", "scfres = self_consistent_field(basis, tol=1e-4);" ], "metadata": {}, "execution_count": 5 }, { "cell_type": "markdown", "source": [ "## Obtaining an AbstractSystem from DFTK data" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "At any point we can also get back the DFTK model as an\n", "AtomsBase-compatible `AbstractSystem`:" ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "FlexibleSystem(Si₂, periodic = TTT):\n bounding_box : [ 0 5.13 5.13;\n 5.13 0 5.13;\n 5.13 5.13 0]u\"a₀\"\n\n Atom(Si, [ 1.2825, 1.2825, 1.2825]u\"a₀\")\n Atom(Si, [ -1.2825, -1.2825, -1.2825]u\"a₀\")\n\n \n \n \n \n Si \n \n Si \n \n \n \n \n" }, "metadata": {}, "execution_count": 6 } ], "cell_type": "code", "source": [ "second_system = atomic_system(model)" ], "metadata": {}, "execution_count": 6 }, { "cell_type": "markdown", "source": [ "Similarly DFTK offers a method to the `atomic_system` and `periodic_system` functions\n", "(from AtomsBase), which enable a seamless conversion of the usual data structures for\n", "setting up DFTK calculations into an `AbstractSystem`:" ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "FlexibleSystem(Si₂, periodic = TTT):\n bounding_box : [ 0 5.13155 5.13155;\n 5.13155 0 5.13155;\n 5.13155 5.13155 0]u\"a₀\"\n\n Atom(Si, [ 1.28289, 1.28289, 1.28289]u\"a₀\")\n Atom(Si, [-1.28289, -1.28289, -1.28289]u\"a₀\")\n\n \n \n \n \n Si \n \n Si \n \n \n \n \n" }, "metadata": {}, "execution_count": 7 } ], "cell_type": "code", "source": [ "lattice = 5.431u\"Å\" / 2 * [[0 1 1.];\n", " [1 0 1.];\n", " [1 1 0.]];\n", "Si = ElementPsp(:Si, psp=load_psp(\"hgh/lda/Si-q4\"))\n", "atoms = [Si, Si]\n", "positions = [ones(3)/8, -ones(3)/8]\n", "\n", "third_system = atomic_system(lattice, atoms, positions)" ], "metadata": {}, "execution_count": 7 } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.9.0" }, "kernelspec": { "name": "julia-1.9", "display_name": "Julia 1.9.0", "language": "julia" } }, "nbformat": 4 }