{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Phonon computations\n",
    "\n",
    "This is a quick sketch how to run a simple phonon calculation using DFTK.\n",
    "First we run an SCF calculation."
   ],
   "metadata": {}
  },
  {
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n     Energy            log10(ΔE)   log10(Δρ)   Diag   Δtime\n",
      "---   ---------------   ---------   ---------   ----   ------\n",
      "  1   -7.916435700389                   -0.69    4.8    508ms\n",
      "  2   -7.921206001831       -2.32       -1.52    1.0   50.3ms\n",
      "  3   -7.921405819539       -3.70       -2.49    1.4   55.1ms\n",
      "  4   -7.921440921267       -4.45       -2.83    2.5   71.5ms\n",
      "  5   -7.921441656914       -6.13       -3.03    1.1   53.1ms\n",
      "  6   -7.921442008158       -6.45       -4.53    1.0   57.1ms\n",
      "  7   -7.921442021682       -7.87       -4.54    2.9   85.9ms\n",
      "  8   -7.921442022123       -9.36       -5.25    1.0   58.8ms\n",
      "  9   -7.921442022143      -10.69       -6.03    1.6   62.0ms\n",
      " 10   -7.921442022144      -12.16       -6.07    2.1   88.5ms\n",
      " 11   -7.921442022144      -12.71       -6.60    1.0   51.4ms\n",
      " 12   -7.921442022144      -13.97       -7.03    1.2   53.6ms\n",
      " 13   -7.921442022144      -14.75       -7.70    1.1   52.8ms\n",
      " 14   -7.921442022144   +  -14.57       -8.12    1.8    108ms\n"
     ]
    }
   ],
   "cell_type": "code",
   "source": [
    "using AtomsBuilder\n",
    "using DFTK\n",
    "using PseudoPotentialData\n",
    "\n",
    "pseudopotentials = PseudoFamily(\"cp2k.nc.sr.lda.v0_1.semicore.gth\")\n",
    "model  = model_DFT(bulk(:Si); pseudopotentials, functionals=LDA())\n",
    "basis  = PlaneWaveBasis(model; Ecut=10, kgrid=[4, 4, 4])\n",
    "scfres = self_consistent_field(basis, tol=1e-8);\n",
    "nothing  # hide"
   ],
   "metadata": {},
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "Next we compute the phonon modes at the q-point `[1/4, 1/4, 1/4]`."
   ],
   "metadata": {}
  },
  {
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "152.797460 seconds (159.01 M allocations: 104.604 GiB, 7.43% gc time, 25.39% compilation time)\n"
     ]
    }
   ],
   "cell_type": "code",
   "source": [
    "scfres = DFTK.unfold_bz(scfres)\n",
    "phret_q0 = @time DFTK.phonon_modes(scfres; q=[0.25, 0.25, 0.25]);\n",
    "nothing  # hide"
   ],
   "metadata": {},
   "execution_count": 2
  },
  {
   "cell_type": "markdown",
   "source": [
    "These are the final phonon frequencies:"
   ],
   "metadata": {}
  },
  {
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "6-element Vector{Float64}:\n -0.0034981553613516723\n -0.0029774511663139334\n -0.0029774511660595397\n  0.004302230638734129\n  0.004302230638938137\n  0.00435320176869754"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "cell_type": "code",
   "source": [
    "phret_q0.frequencies"
   ],
   "metadata": {},
   "execution_count": 3
  }
 ],
 "nbformat_minor": 3,
 "metadata": {
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.11.4"
  },
  "kernelspec": {
   "name": "julia-1.11",
   "display_name": "Julia 1.11.4",
   "language": "julia"
  }
 },
 "nbformat": 4
}