-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_dos.F
350 lines (312 loc) · 15 KB
/
qs_dos.F
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Calculation and writing of density of states
!> \par History
!> -
!> \author JGH
! **************************************************************************************************
MODULE qs_dos
USE cp_control_types, ONLY: dft_control_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_io_unit,&
cp_logger_type
USE cp_output_handling, ONLY: cp_p_file,&
cp_print_key_finished_output,&
cp_print_key_should_output,&
cp_print_key_unit_nr
USE input_section_types, ONLY: section_vals_type,&
section_vals_val_get
USE kinds, ONLY: default_string_length,&
dp
USE kpoint_types, ONLY: kpoint_type
USE message_passing, ONLY: mp_para_env_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_dos'
PUBLIC :: calculate_dos, calculate_dos_kp
! **************************************************************************************************
CONTAINS
! **************************************************************************************************
!> \brief Compute and write density of states
!> \param mos ...
!> \param dft_section ...
!> \date 26.02.2008
!> \par History:
!> \author JGH
!> \version 1.0
! **************************************************************************************************
SUBROUTINE calculate_dos(mos, dft_section)
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
TYPE(section_vals_type), POINTER :: dft_section
CHARACTER(len=*), PARAMETER :: routineN = 'calculate_dos'
CHARACTER(LEN=20) :: fmtstr_data
CHARACTER(LEN=default_string_length) :: my_act, my_pos
INTEGER :: handle, i, iounit, ispin, iterstep, iv, &
iw, ndigits, nhist, nmo(2), nspins
LOGICAL :: append, ionode, should_output
REAL(KIND=dp) :: de, e1, e2, e_fermi(2), emax, emin, eval
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: ehist, hist, occval
REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues, occupation_numbers
TYPE(cp_logger_type), POINTER :: logger
TYPE(mo_set_type), POINTER :: mo_set
NULLIFY (logger)
logger => cp_get_default_logger()
ionode = logger%para_env%is_source()
should_output = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
"PRINT%DOS"), cp_p_file)
iounit = cp_logger_get_default_io_unit(logger)
IF ((.NOT. should_output)) RETURN
CALL timeset(routineN, handle)
iterstep = logger%iter_info%iteration(logger%iter_info%n_rlevel)
IF (iounit > 0) WRITE (UNIT=iounit, FMT='(/,(T3,A,T61,I10))') &
" Calculate DOS at iteration step ", iterstep
CALL section_vals_val_get(dft_section, "PRINT%DOS%DELTA_E", r_val=de)
CALL section_vals_val_get(dft_section, "PRINT%PDOS%APPEND", l_val=append)
CALL section_vals_val_get(dft_section, "PRINT%DOS%NDIGITS", i_val=ndigits)
IF (append .AND. iterstep > 1) THEN
my_pos = "APPEND"
ELSE
my_pos = "REWIND"
END IF
ndigits = MIN(MAX(ndigits, 1), 10)
emin = 1.e10_dp
emax = -1.e10_dp
nspins = SIZE(mos)
nmo(:) = 0
DO ispin = 1, nspins
mo_set => mos(ispin)
CALL get_mo_set(mo_set=mo_set, nmo=nmo(ispin), mu=e_fermi(ispin))
eigenvalues => mo_set%eigenvalues
e1 = MINVAL(eigenvalues(1:nmo(ispin)))
e2 = MAXVAL(eigenvalues(1:nmo(ispin)))
emin = MIN(emin, e1)
emax = MAX(emax, e2)
END DO
IF (de > 0.0_dp) THEN
nhist = NINT((emax - emin)/de) + 1
ALLOCATE (hist(nhist, nspins), occval(nhist, nspins), ehist(nhist, nspins))
hist = 0.0_dp
occval = 0.0_dp
ehist = 0.0_dp
DO ispin = 1, nspins
mo_set => mos(ispin)
occupation_numbers => mo_set%occupation_numbers
eigenvalues => mo_set%eigenvalues
DO i = 1, nmo(ispin)
eval = eigenvalues(i) - emin
iv = NINT(eval/de) + 1
CPASSERT((iv > 0) .AND. (iv <= nhist))
hist(iv, ispin) = hist(iv, ispin) + 1.0_dp
occval(iv, ispin) = occval(iv, ispin) + occupation_numbers(i)
END DO
hist(:, ispin) = hist(:, ispin)/REAL(nmo(ispin), KIND=dp)
END DO
DO i = 1, nhist
ehist(i, 1:nspins) = emin + (i - 1)*de
END DO
ELSE
nhist = MAXVAL(nmo)
ALLOCATE (hist(nhist, nspins), occval(nhist, nspins), ehist(nhist, nspins))
hist = 0.0_dp
occval = 0.0_dp
ehist = 0.0_dp
DO ispin = 1, nspins
mo_set => mos(ispin)
occupation_numbers => mo_set%occupation_numbers
eigenvalues => mo_set%eigenvalues
DO i = 1, nmo(ispin)
ehist(i, ispin) = eigenvalues(i)
hist(i, ispin) = 1.0_dp
occval(i, ispin) = occupation_numbers(i)
END DO
hist(:, ispin) = hist(:, ispin)/REAL(nmo(ispin), KIND=dp)
END DO
END IF
my_act = "WRITE"
iw = cp_print_key_unit_nr(logger, dft_section, "PRINT%DOS", &
extension=".dos", file_position=my_pos, file_action=my_act, &
file_form="FORMATTED")
IF (iw > 0) THEN
IF (nspins == 2) THEN
WRITE (UNIT=iw, FMT="(T2,A,I0,A,2F12.6)") &
"# DOS at iteration step i = ", iterstep, ", E_Fermi[a.u.] = ", e_fermi(1:2)
WRITE (UNIT=iw, FMT="(T2,A, A)") "# Energy[a.u.] Alpha_Density Occupation", &
" Energy[a.u.] Beta_Density Occupation"
! (2(F15.8,2F15.ndigits))
WRITE (UNIT=fmtstr_data, FMT="(A,I0,A)") "(2(F15.8,2F15.", ndigits, "))"
ELSE
WRITE (UNIT=iw, FMT="(T2,A,I0,A,F12.6)") &
"# DOS at iteration step i = ", iterstep, ", E_Fermi[a.u.] = ", e_fermi(1)
WRITE (UNIT=iw, FMT="(T2,A)") "# Energy[a.u.] Density Occupation"
! (F15.8,2F15.ndigits)
WRITE (UNIT=fmtstr_data, FMT="(A,I0,A)") "(F15.8,2F15.", ndigits, ")"
END IF
DO i = 1, nhist
IF (nspins == 2) THEN
e1 = ehist(i, 1)
e2 = ehist(i, 2)
! fmtstr_data == "(2(F15.8,2F15.xx))"
WRITE (UNIT=iw, FMT=fmtstr_data) e1, hist(i, 1), occval(i, 1), &
e2, hist(i, 2), occval(i, 2)
ELSE
eval = ehist(i, 1)
! fmtstr_data == "(F15.8,2F15.xx)"
WRITE (UNIT=iw, FMT=fmtstr_data) eval, hist(i, 1), occval(i, 1)
END IF
END DO
END IF
CALL cp_print_key_finished_output(iw, logger, dft_section, "PRINT%DOS")
DEALLOCATE (hist, occval, ehist)
CALL timestop(handle)
END SUBROUTINE calculate_dos
! **************************************************************************************************
!> \brief Compute and write density of states (kpoints)
!> \param kpoints ...
!> \param qs_env ...
!> \param dft_section ...
!> \date 26.02.2008
!> \par History:
!> \author JGH
!> \version 1.0
! **************************************************************************************************
SUBROUTINE calculate_dos_kp(kpoints, qs_env, dft_section)
TYPE(kpoint_type), POINTER :: kpoints
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(section_vals_type), POINTER :: dft_section
CHARACTER(len=*), PARAMETER :: routineN = 'calculate_dos_kp'
CHARACTER(LEN=16) :: fmtstr_data
CHARACTER(LEN=default_string_length) :: my_act, my_pos
INTEGER :: handle, i, ik, iounit, ispin, iterstep, &
iv, iw, ndigits, nhist, nmo(2), &
nmo_kp, nspins
LOGICAL :: append, ionode, should_output
REAL(KIND=dp) :: de, e1, e2, emax, emin, eval, wkp
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: ehist, hist, occval
REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues, occupation_numbers
TYPE(cp_logger_type), POINTER :: logger
TYPE(dft_control_type), POINTER :: dft_control
TYPE(mo_set_type), DIMENSION(:, :), POINTER :: mos
TYPE(mo_set_type), POINTER :: mo_set
TYPE(mp_para_env_type), POINTER :: para_env
NULLIFY (logger)
logger => cp_get_default_logger()
ionode = logger%para_env%is_source()
should_output = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
"PRINT%DOS"), cp_p_file)
iounit = cp_logger_get_default_io_unit(logger)
IF ((.NOT. should_output)) RETURN
CALL timeset(routineN, handle)
iterstep = logger%iter_info%iteration(logger%iter_info%n_rlevel)
IF (iounit > 0) WRITE (UNIT=iounit, FMT='(/,(T3,A,T61,I10))') &
" Calculate DOS at iteration step ", iterstep
CALL section_vals_val_get(dft_section, "PRINT%DOS%DELTA_E", r_val=de)
CALL section_vals_val_get(dft_section, "PRINT%DOS%APPEND", l_val=append)
CALL section_vals_val_get(dft_section, "PRINT%DOS%NDIGITS", i_val=ndigits)
! ensure a lower value for the histogram width
de = MAX(de, 0.00001_dp)
IF (append .AND. iterstep > 1) THEN
my_pos = "APPEND"
ELSE
my_pos = "REWIND"
END IF
ndigits = MIN(MAX(ndigits, 1), 10)
CALL get_qs_env(qs_env, dft_control=dft_control)
nspins = dft_control%nspins
para_env => kpoints%para_env_inter_kp
emin = 1.e10_dp
emax = -1.e10_dp
nmo(:) = 0
IF (kpoints%nkp /= 0) THEN
DO ik = 1, SIZE(kpoints%kp_env)
mos => kpoints%kp_env(ik)%kpoint_env%mos
CPASSERT(ASSOCIATED(mos))
DO ispin = 1, nspins
mo_set => mos(1, ispin)
CALL get_mo_set(mo_set=mo_set, nmo=nmo_kp)
eigenvalues => mo_set%eigenvalues
e1 = MINVAL(eigenvalues(1:nmo_kp))
e2 = MAXVAL(eigenvalues(1:nmo_kp))
emin = MIN(emin, e1)
emax = MAX(emax, e2)
nmo(ispin) = MAX(nmo(ispin), nmo_kp)
END DO
END DO
END IF
CALL para_env%min(emin)
CALL para_env%max(emax)
CALL para_env%max(nmo)
nhist = NINT((emax - emin)/de) + 1
ALLOCATE (hist(nhist, nspins), occval(nhist, nspins), ehist(nhist, nspins))
hist = 0.0_dp
occval = 0.0_dp
ehist = 0.0_dp
IF (kpoints%nkp /= 0) THEN
DO ik = 1, SIZE(kpoints%kp_env)
mos => kpoints%kp_env(ik)%kpoint_env%mos
wkp = kpoints%kp_env(ik)%kpoint_env%wkp
DO ispin = 1, nspins
mo_set => mos(1, ispin)
occupation_numbers => mo_set%occupation_numbers
eigenvalues => mo_set%eigenvalues
DO i = 1, nmo(ispin)
eval = eigenvalues(i) - emin
iv = NINT(eval/de) + 1
CPASSERT((iv > 0) .AND. (iv <= nhist))
hist(iv, ispin) = hist(iv, ispin) + wkp
occval(iv, ispin) = occval(iv, ispin) + wkp*occupation_numbers(i)
END DO
END DO
END DO
END IF
CALL para_env%sum(hist)
CALL para_env%sum(occval)
DO ispin = 1, nspins
hist(:, ispin) = hist(:, ispin)/REAL(nmo(ispin), KIND=dp)
END DO
DO i = 1, nhist
ehist(i, 1:nspins) = emin + (i - 1)*de
END DO
my_act = "WRITE"
iw = cp_print_key_unit_nr(logger, dft_section, "PRINT%DOS", &
extension=".dos", file_position=my_pos, file_action=my_act, &
file_form="FORMATTED")
IF (iw > 0) THEN
IF (nspins == 2) THEN
WRITE (UNIT=iw, FMT="(T2,A,I0)") "# DOS at iteration step i = ", iterstep
WRITE (UNIT=iw, FMT="(T2,A,A)") "# Energy[a.u.] Alpha_Density Occupation", &
" Beta_Density Occupation"
! (F15.8,4F15.ndigits)
WRITE (UNIT=fmtstr_data, FMT="(A,I0,A)") "(F15.8,4F15.", ndigits, ")"
ELSE
WRITE (UNIT=iw, FMT="(T2,A,I0)") "# DOS at iteration step i = ", iterstep
WRITE (UNIT=iw, FMT="(T2,A)") "# Energy[a.u.] Density Occupation"
! (F15.8,2F15.ndigits)
WRITE (UNIT=fmtstr_data, FMT="(A,I0,A)") "(F15.8,2F15.", ndigits, ")"
END IF
DO i = 1, nhist
eval = emin + (i - 1)*de
IF (nspins == 2) THEN
! fmtstr_data == "(F15.8,4F15.xx)"
WRITE (UNIT=iw, FMT=fmtstr_data) eval, hist(i, 1), occval(i, 1), &
hist(i, 2), occval(i, 2)
ELSE
! fmtstr_data == "(F15.8,2F15.xx)"
WRITE (UNIT=iw, FMT=fmtstr_data) eval, hist(i, 1), occval(i, 1)
END IF
END DO
END IF
CALL cp_print_key_finished_output(iw, logger, dft_section, "PRINT%DOS")
DEALLOCATE (hist, occval)
CALL timestop(handle)
END SUBROUTINE calculate_dos_kp
END MODULE qs_dos