Input and output formats

This section provides an overview of the input and output formats supported by DFTK, usually via integration with a third-party library.

Reading / writing files supported by AtomsIO

AtomsIO is a Julia package which supports reading / writing atomistic structures from / to a large range of file formats. Supported formats include Crystallographic Information Framework (CIF), XYZ and extxyz files, ASE / Gromacs / LAMMPS / Amber trajectory files or input files of various other codes (e.g. Quantum Espresso, VASP, ABINIT, CASTEP, …). The full list of formats is is available in the AtomsIO documentation.

The AtomsIO functionality is split into two packages. The main package, AtomsIO itself, only depends on packages, which are registered in the Julia General package registry. In contrast AtomsIOPython extends AtomsIO by parsers depending on python packages, which are automatically managed via PythonCall. While it thus provides the full set of supported IO formats, this also adds additional practical complications, so some users may choose not to use AtomsIOPython.

As an example we start the calculation of a simple antiferromagnetic iron crystal using a Quantum-Espresso input file, Fe_afm.pwi. For more details about calculations on magnetic systems using collinear spin, see Collinear spin and magnetic systems.

First we parse the Quantum Espresso input file using AtomsIO, which reads the lattice, atomic positions and initial magnetisation from the input file and returns it as an AtomsBase AbstractSystem, the JuliaMolSim community standard for representing atomic systems.

using AtomsIO        # Use Julia-only IO parsers
using AtomsIOPython  # Use python-based IO parsers (e.g. ASE)
system = load_system("Fe_afm.pwi")
FlexibleSystem(Fe₂, periodic = TTT):
    bounding_box      : [ 2.86814        0        0;
                                0  2.86814        0;
                                0        0  2.86814]u"Å"

    Atom(Fe, [       0,        0,        0]u"Å")
    Atom(Fe, [ 1.43407,  1.43407,  1.43407]u"Å")

      .------.  
     /|      |  
    * |      |  
    | | Fe   |  
    | .------.  
    |/      /   
    Fe-----*    

Next we attach pseudopotential information, since currently the parser is not yet capable to read this information from the file.

using DFTK
system = attach_psp(system, Fe="hgh/pbe/fe-q16.hgh")
FlexibleSystem(Fe₂, periodic = TTT):
    bounding_box      : [ 2.86814        0        0;
                                0  2.86814        0;
                                0        0  2.86814]u"Å"

    Atom(Fe, [       0,        0,        0]u"Å")
    Atom(Fe, [ 1.43407,  1.43407,  1.43407]u"Å")

      .------.  
     /|      |  
    * |      |  
    | | Fe   |  
    | .------.  
    |/      /   
    Fe-----*    

Finally we make use of DFTK's AtomsBase integration to run the calculation.

model = model_DFT(system; functionals=LDA(), temperature=0.01)
basis = PlaneWaveBasis(model; Ecut=10, kgrid=(2, 2, 2))
ρ0 = guess_density(basis, system)
scfres = self_consistent_field(basis, ρ=ρ0);
n     Energy            log10(ΔE)   log10(Δρ)   Magnet   Diag   Δtime
---   ---------------   ---------   ---------   ------   ----   ------
  1   -224.0552267772                   -0.05   -0.653    5.1    348ms
  2   -224.1924590371       -0.86       -0.46   -0.549    1.6    241ms
  3   -224.2168569947       -1.61       -1.69   -0.495    2.6    246ms
  4   -224.2169553649       -4.01       -2.47   -0.471    1.0    157ms
  5   -224.2169696550       -4.84       -2.64   -0.451    2.2    185ms
  6   -224.2169781689       -5.07       -2.72   -0.415    1.5    171ms
  7   -224.2169975587       -4.71       -2.75   -0.277    1.5    223ms
  8   -224.2170092703       -4.93       -2.79   -0.131    2.0    222ms
  9   -224.2170130087       -5.43       -2.97   -0.025    1.8    191ms
 10   -224.2170134558       -6.35       -3.23   -0.013    1.2    164ms
 11   -224.2170136436       -6.73       -3.65    0.001    1.2    169ms
 12   -224.2170136741       -7.52       -4.35    0.000    1.2    167ms
 13   -224.2170136772       -8.51       -5.01   -0.000    2.2    267ms
 14   -224.2170136772      -10.19       -5.03   -0.000    2.4    218ms
 15   -224.2170136773      -10.21       -5.55   -0.000    1.0    157ms
 16   -224.2170136773      -11.20       -5.93   -0.000    1.8    181ms
 17   -224.2170136773      -12.94       -6.43    0.000    2.0    194ms
DFTK data formats are not yet fully matured

The data format in which DFTK saves data as well as the general interface of the load_scfres and save_scfres pair of functions are not yet fully matured. They have become fairly stable, but we reserve the right to change them incompatibly between DFTK versions (including patch versions) at this point.

Writing VTK files for visualization

For visualizing the density or the Kohn-Sham orbitals DFTK supports storing the result of an SCF calculations in the form of VTK files. These can afterwards be visualized using tools such as paraview. Using this feature requires the WriteVTK.jl Julia package.

using WriteVTK
save_scfres("iron_afm.vts", scfres; save_ψ=true);

This will save the iron calculation above into the file iron_afm.vts, using save_ψ=true to also include the KS orbitals.

Parsable data-export using json

Many structures in DFTK support the (unexported) todict function, which returns a simplified dictionary representation of the data.

DFTK.todict(scfres.energies)
Dict{String, Float64} with 9 entries:
  "AtomicNonlocal" => -4.32427
  "PspCorrection"  => 7.03001
  "Ewald"          => -171.89
  "total"          => -224.217
  "Entropy"        => -0.0328468
  "Kinetic"        => 74.1104
  "AtomicLocal"    => -139.801
  "Hartree"        => 31.6017
  "Xc"             => -20.9115

This in turn can be easily written to disk using a JSON library. Currently we integrate most closely with JSON3, which is thus recommended.

using JSON3
open("iron_afm_energies.json", "w") do io
    JSON3.pretty(io, DFTK.todict(scfres.energies))
end
println(read("iron_afm_energies.json", String))
{
    "AtomicNonlocal": -4.324270075281979,
    "PspCorrection": 7.03000636467455,
    "Ewald": -171.88999401525265,
    "total": -224.21701367728474,
    "Entropy": -0.03284680397899815,
    "Kinetic": 74.11040836959609,
    "AtomicLocal": -139.8005116927611,
    "Hartree": 31.60169341412248,
    "Xc": -20.911499238403163
}

Once JSON3 is loaded, additionally a convenience function for saving a summary of scfres objects using save_scfres is available:

using JSON3
save_scfres("iron_afm.json", scfres)

Similarly a summary of the band data (occupations, eigenvalues, εF, etc.) for post-processing can be dumped using save_bands:

save_bands("iron_afm_scfres.json", scfres)

Notably this function works both for the results obtained by self_consistent_field as well as compute_bands:

bands = compute_bands(scfres, kline_density=10)
save_bands("iron_afm_bands.json", bands)
┌ Warning: The provided cell is a supercell: the returned k-path is the standard k-path of the associated primitive cell in the basis of the supercell reciprocal lattice.
  cell =
   Spglib.SpglibCell{Float64, Float64, Int64, Float64}
    lattice:
      5.41999997388764  0.0  0.0
      0.0  5.41999997388764  0.0
      0.0  0.0  5.41999997388764
    2 atomic positions:
      0.0  0.0  0.0
      0.5  0.5  0.5
    2 atoms:
      1  1
    2 magmoms:
      0.0  0.0

@ BrillouinSpglibExt ~/.julia/packages/Brillouin/h7lol/ext/BrillouinSpglibExt.jl:28

Writing and reading JLD2 files

The full state of a DFTK self-consistent field calculation can be stored on disk in form of an JLD2.jl file. This file can be read from other Julia scripts as well as other external codes supporting the HDF5 file format (since the JLD2 format is based on HDF5). This includes notably h5py to read DFTK output from python.

using JLD2
save_scfres("iron_afm.jld2", scfres)

Saving such JLD2 files supports some options, such as save_ψ=false, which avoids saving the Bloch waves (much faster and smaller files). Notice that JLD2 files can also be used with save_bands.

Since such JLD2 can also be read by DFTK to start or continue a calculation, these can also be used for checkpointing or for transferring results to a different computer. See Saving SCF results on disk and SCF checkpoints for details.

(Cleanup files generated by this notebook.)

rm.(["iron_afm.vts", "iron_afm.jld2",
     "iron_afm.json", "iron_afm_energies.json", "iron_afm_scfres.json",
     "iron_afm_bands.json"])
6-element Vector{Nothing}:
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing