generated from usnistgov/opensource-repo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
DFToutMod.jl
330 lines (259 loc) · 9.58 KB
/
DFToutMod.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include("Crystal.jl")
"""
module DFToutMod
Deal with energy, force, stress, band structure, etc, from a DFT calculation
Generic to different DFT codes.
"""
module DFToutMod
#using LinearAlgebra
using ..CrystalMod:crystal
using ..CrystalMod:makecrys
using ..CrystalMod:orbital_index
using Printf
using ..ThreeBodyTB:convert_energy
using ..ThreeBodyTB:convert_force
using ..ThreeBodyTB:convert_stress
using ..ThreeBodyTB:global_energy_units
##using Formatting
export dftout
export makedftout
#export test
export test2
#export crystal
#export makecrys
#export generate_supercell
#export generate_random
#export write_poscar
#export write_efs
#holds data
"""
mutable struct bandstructure
Band structure. Has
- `nbnd::Int` Number of bands
- `nks::Int` Number of k-points
- `nelec::Float64` Number of electrons
- `nspin::Int64` Number of spins (2 for spin-polarized (magnetic), 1 non-sp)
- `efermi::Float64` Fermi energy
- `kpts::Array{Float64,2}` List of k-points, `nks × 3` array in BZ crystal units.
- `kweights::Array{Float64,1}` Weights of k-points (`nks`).
- `kgrid::Array{Int,1}` Equivalent gamma centered Monkhorst-Pack k-grid dimensions, if applies.
- `eigs::Array{Float64,3}` Eigenvalues. `nks × nbnd × nspin`
"""
mutable struct bandstructure
nbnd::Int
nks::Int
nelec::Float64
nspin::Int64
efermi::Float64
kpts::Array{Float64,2}
kweights::Array{Float64,1}
kgrid::Array{Int,1}
eigs::Array{Float64,3}
end
"""
mutable struct dftout
DFT output struct. Has
- `crys::crystal` crystal structure
- `energy::Float64` the actual DFT energy, depends on pseudopotentials.
- `energy_smear::Float64` smearing energy
- `forces::Array{Float64,2}` forces Ryd / a.u.
- `stress::Array{Float64,2}` stress Ryd / (a.u.)^3
- `bandstruct::bandstructure` See bandstructure struct
- `hasband::Bool` does this object have a band structure, usually `true`
- `hasham::Bool` not used, always `false`
- `prefix::String` A string has a name used to find output files.
- `outdir::String` Directly loaded from
- `tot_charge::Float64` If charge of unit cell is nonzero
- `atomize_energy::Float64` Atomization energy, relative to non-spin-polarized atoms.
- `nspin::Int64` number of spins (1 = non-sp, 2= spin-polarized)
- `mag_tot::Float64` Total magnetization = sum_i mag_i
- `mag_abs::Float64` Absolute magnetization = sum_i | mag_i |
"""
mutable struct dftout
crys::crystal
energy::Float64
energy_smear::Float64
forces::Array{Float64,2}
stress::Array{Float64,2}
bandstruct::bandstructure
hasband::Bool
hasham::Bool
prefix::String
outdir::String
tot_charge::Float64
atomize_energy::Float64
nspin::Int64
mag_tot::Float64
mag_abs::Float64
end
"""
function get_atomize_energy(d::dftout)
Return atomization energy in current energy units
"""
function get_atomize_energy(d::dftout)
return convert_energy(d.atomize_energy)
end
#printing
Base.show(io::IO, d::dftout) = begin
# println(io)
# for i in 1:3
# @printf(io, "A%.1i= % .5f % .5f % .5f\n", i, d.crys.A[i,1], d.crys.A[i,2], d.crys.A[i,3])
# end
println(io, d.crys)
println(io)
println("Energy (direct from dft) ", d.energy, " Ryd")
println(io)
println(io,"STRUCTURE =====================| FORCES =================")
println(io)
forces = convert_force(d.forces)
for i in 1:d.crys.nat
@printf(io, "%-3s % .5f % .5f % .5f | % .5f % .5f % .5f\n", d.crys.types[i], d.crys.coords[i,1], d.crys.coords[i,2], d.crys.coords[i,3], forces[i,1], forces[i,2], forces[i,3])
end
println(io,)
println(io,"=========================================================")
println(io)
println(io,"STRESS ==================")
stress = convert_stress(d.stress)
@printf(io, "\n")
for i in 1:3
@printf(io, "% .5f % .5f % .5f\n", stress[i,1], stress[i,2], stress[i,3])
end
println(io,)
println(io, "Atomization energy (no spin reference): ", convert_energy(d.atomize_energy), " $global_energy_units ")
println(io,"=========================")
if d.nspin == 2
hasspin = true
else
hasspin = false
end
println(io," Has bandstructure: ", d.hasband,"; Has hamiltonian: ", d.hasham, "; has spin: ", hasspin, "; tot_charge: ", d.tot_charge)
if hasspin
@printf(io, " Net magnetization: % .5f ; Absolute magnetization: % .5f \n", d.mag_tot, d.mag_abs)
end
println(io)
end
#print bandstructure
Base.show(io::IO, d::bandstructure) = begin
println(io,"nbnd = ", d.nbnd, "; nkpts = ", d.nks, "; nspin = ", d.nspin)
println(io,"nelec = ", d.nelec, "; efermi = ", convert_energy(d.efermi))
println(io)
# pst = "{:d} {: f} {: f} {: f} , {: f}, "
# pst = "%-3s % .5f % .5f % .5f , % .5f, "
# println(pst)
function prt(spin)
for i = 1:min(d.nks, 200)
t = @sprintf("%-3s", i)
k = @sprintf(" [ % .5f % .5f % .5f ]", d.kpts[i,1],d.kpts[i,2],d.kpts[i,3] )
kw = @sprintf(" % .5f ", d.kweights[i])
v = " "
for j = 1:d.nbnd
v = v * @sprintf(" % .5f",convert_energy.(d.eigs[i,j, spin]))
end
println(io,t,kw,k,v)
end
if d.nks > 200
println(io, "... [truncated]")
end
println(io)
end
if d.nspin == 1
prt(1)
else
println(io, "SPIN UP")
prt(1)
println(io, "-------")
println(io, "SPIN DN")
prt(2)
println(io, "-------")
end
end
"""
function makebs(nelec::Number, efermi::Number, kpoints, kweights,kgrid, vals)
Constructor for `bandstructure`.
"""
function makebs(nelec::Number, efermi::Number, kpoints, kweights,kgrid, vals; nspin=1)
nks = size(kpoints,1)
nbnd = size(vals,2)
if nspin == 2 && length(size(vals)) != 3
println("makebs eigenvalues not consistent with nspin $npin vs ", size(vals))
end
if length(size(vals)) == 2 #need to add dummy spin axis in non-sp case
v = zeros(nks, nbnd, 1)
v[:,:,1] = vals[:,:]
vals = v
end
try
kpoints = convert(Array{Float64,2},kpoints)
kweights = convert(Array{Float64,1}, kweights)
vals = convert(Array{Float64,3}, vals)
catch
println(typeof(kpoints), typeof(kweights), typeof(vals))
error("error make bs type conversion")
end
if nks != size(kweights,1) || nks != size(vals,1)
error("kpoints kweights eigs don't match")
end
if nspin != size(vals)[3]
println("ERROR creating band structure nspin $nspin size vals ", size(vals))
end
K2 = zeros(Float64,size(kpoints))
for i in 1:nks
K2[i,:] = Int64.(round.(kpoints[i,:] * 100000000))/100000000
end
return bandstructure(nbnd, nks, nelec, nspin, efermi, K2, kweights, kgrid, vals)
end
function make_empty_bs(;nspin=1)
return bandstructure(1, 1, 1, nspin, 0.0, zeros(1,3), zeros(3), [1, 1, 1], zeros(1,1,1))
end
"""
function makedftout(crys::crystal, energy::Number, energy_smear::Number, forces, stress, bandstruct=missing; prefix="PREFIX", outdir="TMPDIR", tot_charge=0.0)
Constructor for dftout. Usually called by function that loads DFT output files, not called directly.
"""
function makedftout(crys::crystal, energy::Number, energy_smear::Number, forces, stress, bandstruct=missing; nspin=1, mag_tot = 0.0, mag_abs = 0.0, prefix="PREFIX", outdir="TMPDIR", tot_charge=0.0)
"""
Creates a struct with the desired data
"""
try
forces = convert(Array{Float64,2}, forces)
stress = convert(Array{Float64,2}, stress)
energy = convert(Float64, energy)
energy_smear = convert(Float64, energy_smear)
catch
println(typeof(energy), typeof(forces), typeof(stress))
error("Error converting types")
return
end
nat = crys.nat
if (nat,3) != size(forces)
println( forces)
error("Error forces")
return
end
if size(stress) != (3,3)
println( stress)
error("Error stress", size(stress))
return
end
ind2orb, orb2ind, etotal_atoms, nval = orbital_index(crys)
atomize_energy = energy - etotal_atoms
if ismissing(bandstruct)
return dftout(crys, energy, energy_smear, forces, stress, make_empty_bs() , false, false, prefix, outdir, tot_charge, atomize_energy, nspin, mag_tot, mag_abs) #, missing, False, False)
else
return dftout(crys, energy, energy_smear,forces, stress, bandstruct, true, false, prefix, outdir, tot_charge, atomize_energy, nspin, mag_tot, mag_abs) #, missing, False, False)
end
end
#alterate version, will make the crys for you.
"""
function makedftout(A, pos, types, energy::Number,energy_smear::Number, forces, stress, bandstruct=missing; prefix="PREFIX", outdir="TMPDIR", tot_charge=0.0)
"""
function makedftout(A, pos, types, energy::Number,energy_smear::Number, forces, stress, bandstruct=missing; prefix="PREFIX", outdir="TMPDIR", tot_charge=0.0, nspin = 1, mag_tot = 0.0, mag_abs = 0.0)
c = makecrys(A,pos,types, units="Bohr")
if ismissing(bandstruct)
return makedftout(c, energy, energy_smear, forces,stress, prefix=prefix, outdir=outdir, tot_charge=tot_charge, nspin=nspin, mag_tot= mag_tot, mag_abs = mag_abs)
else
return makedftout(c, energy, energy_smear, forces,stress, bandstruct,prefix=prefix, outdir=outdir, tot_charge=tot_charge, nspin=nspin, mag_tot= mag_tot, mag_abs = mag_abs)
end
end
end #end module
#using .DFToutMod
#Base.:(==)(x::dftout, y::dftout) = (x.crys == y.crys && x.energy == y.energy && x.forces == y.forces && x.stress ==y.stress)