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.111152e+02     1.661285e+00
 * time: 0.3345329761505127
     1     1.048701e+01     9.043060e-01
 * time: 1.0256619453430176
     2    -1.213179e+01     1.025418e+00
 * time: 1.6999070644378662
     3    -3.390544e+01     7.707066e-01
 * time: 2.7058329582214355
     4    -4.745632e+01     5.575479e-01
 * time: 3.714892864227295
     5    -5.687422e+01     1.801465e-01
 * time: 4.737499952316284
     6    -5.977292e+01     1.264083e-01
 * time: 5.4094438552856445
     7    -6.090587e+01     6.593513e-02
 * time: 6.086449861526489
     8    -6.137022e+01     4.570971e-02
 * time: 6.775820970535278
     9    -6.161677e+01     4.627294e-02
 * time: 7.45754599571228
    10    -6.182497e+01     3.131107e-02
 * time: 8.155931949615479
    11    -6.196782e+01     1.922990e-02
 * time: 8.847249031066895
    12    -6.204260e+01     1.753987e-02
 * time: 9.524410009384155
    13    -6.209826e+01     1.476741e-02
 * time: 10.21449589729309
    14    -6.213677e+01     1.278514e-02
 * time: 10.877980947494507
    15    -6.216462e+01     1.142130e-02
 * time: 11.55464506149292
    16    -6.218278e+01     8.887459e-03
 * time: 12.242568016052246
    17    -6.219519e+01     1.007190e-02
 * time: 12.943018913269043
    18    -6.220393e+01     9.624012e-03
 * time: 13.626991987228394
    19    -6.221031e+01     8.792004e-03
 * time: 14.29285192489624
    20    -6.221599e+01     7.562795e-03
 * time: 14.965039014816284
    21    -6.222215e+01     7.404275e-03
 * time: 15.648388862609863
    22    -6.222888e+01     7.891627e-03
 * time: 16.329011917114258
    23    -6.223599e+01     6.176170e-03
 * time: 17.02313494682312
    24    -6.224293e+01     5.322894e-03
 * time: 17.712029933929443
    25    -6.224872e+01     4.875030e-03
 * time: 18.386679887771606
    26    -6.225281e+01     4.631014e-03
 * time: 19.076564073562622
    27    -6.225576e+01     3.412833e-03
 * time: 19.756484031677246
    28    -6.225769e+01     2.809919e-03
 * time: 20.43790292739868
    29    -6.225902e+01     2.293947e-03
 * time: 21.11301589012146
    30    -6.225995e+01     1.955225e-03
 * time: 21.798824071884155
    31    -6.226061e+01     1.542357e-03
 * time: 22.478134870529175
    32    -6.226106e+01     1.080892e-03
 * time: 23.14909791946411
    33    -6.226135e+01     8.797984e-04
 * time: 23.823495864868164
    34    -6.226152e+01     7.058776e-04
 * time: 24.500053882598877
    35    -6.226160e+01     5.415384e-04
 * time: 25.186769008636475
    36    -6.226163e+01     3.184913e-04
 * time: 25.86358904838562
    37    -6.226165e+01     2.140161e-04
 * time: 26.54715394973755
    38    -6.226166e+01     1.415091e-04
 * time: 27.21821904182434
    39    -6.226166e+01     1.253974e-04
 * time: 27.889484882354736
    40    -6.226166e+01     1.063355e-04
 * time: 28.552640914916992
    41    -6.226166e+01     8.763636e-05
 * time: 29.24816107749939
    42    -6.226166e+01     6.046202e-05
 * time: 29.926748037338257
    43    -6.226167e+01     5.400411e-05
 * time: 30.603137016296387
    44    -6.226167e+01     4.241988e-05
 * time: 31.298162937164307
    45    -6.226167e+01     3.143870e-05
 * time: 31.97456407546997
    46    -6.226167e+01     2.632254e-05
 * time: 32.64623403549194
    47    -6.226167e+01     2.026863e-05
 * time: 33.32402801513672
    48    -6.226167e+01     1.552964e-05
 * time: 33.994884967803955
    49    -6.226167e+01     8.936208e-06
 * time: 34.67085886001587
    50    -6.226167e+01     7.852934e-06
 * time: 35.36610698699951
    51    -6.226167e+01     6.026399e-06
 * time: 36.069419860839844
    52    -6.226167e+01     4.743195e-06
 * time: 36.75545406341553
    53    -6.226167e+01     3.295405e-06
 * time: 37.4454300403595
    54    -6.226167e+01     3.465368e-06
 * time: 38.124664068222046
    55    -6.226167e+01     2.577085e-06
 * time: 38.80343508720398
scfres.energies
Energy breakdown:
    Kinetic             25.7671070
    AtomicLocal         -18.8557650
    AtomicNonlocal      14.8522618
    Ewald               -67.1831486
    PspCorrection       -2.3569765
    Hartree             4.8485368 
    Xc                  -19.3336819

    total               -62.261666460339