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.0553974705 -0.05 -0.653 5.0 459ms
2 -224.1925227060 -0.86 -0.46 -0.553 1.6 208ms
3 -224.2168571478 -1.61 -1.69 -0.485 2.8 254ms
4 -224.2169566076 -4.00 -2.47 -0.465 1.0 221ms
5 -224.2169704074 -4.86 -2.64 -0.446 2.1 203ms
6 -224.2169792568 -5.05 -2.73 -0.409 1.4 194ms
7 -224.2169955112 -4.79 -2.79 -0.299 1.5 197ms
8 -224.2170091361 -4.87 -2.76 -0.131 2.1 212ms
9 -224.2170127232 -5.45 -2.93 -0.042 1.6 247ms
10 -224.2170133797 -6.18 -3.28 -0.024 1.0 188ms
11 -224.2170136499 -6.57 -3.76 -0.005 1.5 229ms
12 -224.2170136762 -7.58 -4.70 -0.001 1.8 208ms
13 -224.2170136772 -9.00 -5.01 0.000 3.0 243ms
14 -224.2170136773 -10.16 -5.76 -0.000 1.4 227ms
15 -224.2170136773 -11.10 -6.09 -0.000 2.8 231ms
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.324270040393008,
"PspCorrection": 7.03000636467455,
"Ewald": -171.88999401525265,
"total": -224.21701367726652,
"Entropy": -0.032846808501659754,
"Kinetic": 74.11040508725443,
"AtomicLocal": -139.80050621901236,
"Hartree": 31.601690814592676,
"Xc": -20.911498860628498
}
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