Creating supercells with pymatgen

The Pymatgen python library allows to setup solid-state calculations using a flexible set of classes as well as an API to an online data base of structures. Its Structure and Lattice objects are directly supported by the DFTK load_atoms and load_lattice functions, such that DFTK may be readily used to run calculation on systems defined in pymatgen. Using the pymatgen_structure function a conversion from DFTK to pymatgen structures is also possible. In the following we use this to create a silicon supercell and find its LDA ground state using direct minimisation.

First we setup the silicon lattice in DFTK.

using DFTK

a = 10.263141334305942  # Lattice constant in Bohr
lattice = a / 2 .* [[0 1 1.]; [1 0 1.]; [1 1 0.]]
Si = ElementPsp(:Si, psp=load_psp("hgh/lda/Si-q4"))
atoms = [Si => [ones(3)/8, -ones(3)/8]];

Next we make a [2, 2, 2] supercell using pymatgen

pystruct = pymatgen_structure(lattice, atoms)
pystruct.make_supercell([2, 2, 2])
lattice = load_lattice(pystruct)
atoms = [Si => [s.frac_coords for s in pystruct.sites]];

Setup an LDA model and discretize using a single kpoint and a small Ecut of 5 Hartree.

model = model_LDA(lattice, atoms)
basis = PlaneWaveBasis(model, 5, kgrid=(1, 1, 1))
PlaneWaveBasis (Ecut=5.0, 1 kpoints)

Find the ground state using direct minimisation (always using SCF is boring ...)

scfres = direct_minimization(basis, tol=1e-5);
Iter     Function value   Gradient norm
     0     1.123366e+02     1.565098e+00
 * time: 0.28290486335754395
     1     1.093661e+01     9.513280e-01
 * time: 0.8457469940185547
     2    -1.253519e+01     1.016672e+00
 * time: 1.4555068016052246
     3    -3.461850e+01     7.483797e-01
 * time: 2.346151828765869
     4    -4.833723e+01     5.380835e-01
 * time: 3.2231249809265137
     5    -5.703854e+01     2.028171e-01
 * time: 4.034320831298828
     6    -5.972338e+01     1.454944e-01
 * time: 4.611511945724487
     7    -6.085272e+01     5.667090e-02
 * time: 5.164144992828369
     8    -6.132942e+01     4.788186e-02
 * time: 5.726039886474609
     9    -6.161307e+01     5.616116e-02
 * time: 6.299672842025757
    10    -6.183134e+01     4.458863e-02
 * time: 6.865570783615112
    11    -6.199527e+01     2.599839e-02
 * time: 7.423525810241699
    12    -6.207665e+01     2.011376e-02
 * time: 7.981431007385254
    13    -6.213698e+01     1.621922e-02
 * time: 8.532658815383911
    14    -6.218007e+01     1.629478e-02
 * time: 9.107432842254639
    15    -6.220578e+01     1.282179e-02
 * time: 9.674598932266235
    16    -6.222147e+01     9.892797e-03
 * time: 10.231048822402954
    17    -6.223193e+01     7.681708e-03
 * time: 10.785382986068726
    18    -6.223989e+01     6.520534e-03
 * time: 11.351523876190186
    19    -6.224526e+01     5.379430e-03
 * time: 11.93134593963623
    20    -6.224954e+01     4.518465e-03
 * time: 12.484625816345215
    21    -6.225329e+01     4.883914e-03
 * time: 13.054414987564087
    22    -6.225648e+01     4.374242e-03
 * time: 13.600202798843384
    23    -6.225885e+01     2.892240e-03
 * time: 14.182824850082397
    24    -6.226027e+01     1.911585e-03
 * time: 14.755788803100586
    25    -6.226099e+01     1.534687e-03
 * time: 15.325362920761108
    26    -6.226135e+01     1.031536e-03
 * time: 15.880291938781738
    27    -6.226150e+01     6.089073e-04
 * time: 16.428684949874878
    28    -6.226157e+01     5.059989e-04
 * time: 17.058578968048096
    29    -6.226160e+01     4.023921e-04
 * time: 17.647005796432495
    30    -6.226162e+01     3.638191e-04
 * time: 18.210349798202515
    31    -6.226164e+01     3.130687e-04
 * time: 18.772292852401733
    32    -6.226165e+01     2.516051e-04
 * time: 19.352329969406128
    33    -6.226165e+01     1.971401e-04
 * time: 19.969337940216064
    34    -6.226166e+01     1.388178e-04
 * time: 20.54590392112732
    35    -6.226166e+01     1.317134e-04
 * time: 21.11669087409973
    36    -6.226166e+01     9.075770e-05
 * time: 21.699984788894653
    37    -6.226167e+01     6.353830e-05
 * time: 22.31446886062622
    38    -6.226167e+01     4.585746e-05
 * time: 22.90895390510559
    39    -6.226167e+01     2.998991e-05
 * time: 23.502071857452393
    40    -6.226167e+01     2.158711e-05
 * time: 24.059324979782104
    41    -6.226167e+01     1.673575e-05
 * time: 24.617875814437866
    42    -6.226167e+01     1.151810e-05
 * time: 25.224156856536865
    43    -6.226167e+01     9.102588e-06
 * time: 25.78610396385193
    44    -6.226167e+01     7.360428e-06
 * time: 26.329620838165283
    45    -6.226167e+01     5.497171e-06
 * time: 26.87767791748047
    46    -6.226167e+01     5.023337e-06
 * time: 27.46343684196472
    47    -6.226167e+01     5.083763e-06
 * time: 28.030648946762085
    48    -6.226167e+01     3.924887e-06
 * time: 28.605808973312378
    49    -6.226167e+01     3.005953e-06
 * time: 29.160786867141724
scfres.energies
Energy breakdown:
    Kinetic             25.7671065
    AtomicLocal         -18.8557691
    AtomicNonlocal      14.8522661
    Ewald               -67.1831486
    PspCorrection       -2.3569765
    Hartree             4.8485371 
    Xc                  -19.3336820

    total               -62.261666461219