-
Notifications
You must be signed in to change notification settings - Fork 1
/
admm_dm_methods.F
549 lines (448 loc) · 25.3 KB
/
admm_dm_methods.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
!--------------------------------------------------------------------------------------------------!
! 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 Contains ADMM methods which only require the density matrix
!> \par History
!> 11.2014 created [Ole Schuett]
!> \author Ole Schuett
! **************************************************************************************************
MODULE admm_dm_methods
USE admm_dm_types, ONLY: admm_dm_type,&
mcweeny_history_type
USE admm_types, ONLY: get_admm_env
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_frobenius_norm, dbcsr_get_block_p, &
dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, dbcsr_p_type, dbcsr_release, &
dbcsr_scale, dbcsr_set, dbcsr_type
USE cp_dbcsr_operations, ONLY: dbcsr_deallocate_matrix_set
USE cp_log_handling, ONLY: cp_logger_get_default_unit_nr
USE input_constants, ONLY: do_admm_basis_projection,&
do_admm_blocked_projection
USE iterate_matrix, ONLY: invert_Hotelling
USE kinds, ONLY: dp
USE pw_types, ONLY: pw_c1d_gs_type,&
pw_r3d_rs_type
USE qs_collocate_density, ONLY: calculate_rho_elec
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_ks_types, ONLY: qs_ks_env_type
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_set,&
qs_rho_type
USE task_list_types, ONLY: task_list_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
PUBLIC :: admm_dm_calc_rho_aux, admm_dm_merge_ks_matrix
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'admm_dm_methods'
CONTAINS
! **************************************************************************************************
!> \brief Entry methods: Calculates auxiliary density matrix from primary one.
!> \param qs_env ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE admm_dm_calc_rho_aux(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'admm_dm_calc_rho_aux'
INTEGER :: handle
TYPE(admm_dm_type), POINTER :: admm_dm
NULLIFY (admm_dm)
CALL timeset(routineN, handle)
CALL get_admm_env(qs_env%admm_env, admm_dm=admm_dm)
SELECT CASE (admm_dm%method)
CASE (do_admm_basis_projection)
CALL map_dm_projection(qs_env)
CASE (do_admm_blocked_projection)
CALL map_dm_blocked(qs_env)
CASE DEFAULT
CPABORT("admm_dm_calc_rho_aux: unknown method")
END SELECT
IF (admm_dm%purify) &
CALL purify_mcweeny(qs_env)
CALL update_rho_aux(qs_env)
CALL timestop(handle)
END SUBROUTINE admm_dm_calc_rho_aux
! **************************************************************************************************
!> \brief Entry methods: Merges auxiliary Kohn-Sham matrix into primary one.
!> \param qs_env ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE admm_dm_merge_ks_matrix(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'admm_dm_merge_ks_matrix'
INTEGER :: handle
TYPE(admm_dm_type), POINTER :: admm_dm
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks_merge
CALL timeset(routineN, handle)
NULLIFY (admm_dm, matrix_ks_merge)
CALL get_admm_env(qs_env%admm_env, admm_dm=admm_dm)
IF (admm_dm%purify) THEN
CALL revert_purify_mcweeny(qs_env, matrix_ks_merge)
ELSE
CALL get_admm_env(qs_env%admm_env, matrix_ks_aux_fit=matrix_ks_merge)
END IF
SELECT CASE (admm_dm%method)
CASE (do_admm_basis_projection)
CALL merge_dm_projection(qs_env, matrix_ks_merge)
CASE (do_admm_blocked_projection)
CALL merge_dm_blocked(qs_env, matrix_ks_merge)
CASE DEFAULT
CPABORT("admm_dm_merge_ks_matrix: unknown method")
END SELECT
IF (admm_dm%purify) &
CALL dbcsr_deallocate_matrix_set(matrix_ks_merge)
CALL timestop(handle)
END SUBROUTINE admm_dm_merge_ks_matrix
! **************************************************************************************************
!> \brief Calculates auxiliary density matrix via basis projection.
!> \param qs_env ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE map_dm_projection(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER :: ispin
LOGICAL :: s_mstruct_changed
REAL(KIND=dp) :: threshold
TYPE(admm_dm_type), POINTER :: admm_dm
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s_aux, matrix_s_mixed, rho_ao, &
rho_ao_aux
TYPE(dbcsr_type) :: matrix_s_aux_inv, matrix_tmp
TYPE(dft_control_type), POINTER :: dft_control
TYPE(qs_rho_type), POINTER :: rho, rho_aux
NULLIFY (dft_control, admm_dm, matrix_s_aux, matrix_s_mixed, rho, rho_aux)
NULLIFY (rho_ao, rho_ao_aux)
CALL get_qs_env(qs_env, dft_control=dft_control, s_mstruct_changed=s_mstruct_changed, rho=rho)
CALL get_admm_env(qs_env%admm_env, matrix_s_aux_fit=matrix_s_aux, rho_aux_fit=rho_aux, &
matrix_s_aux_fit_vs_orb=matrix_s_mixed, admm_dm=admm_dm)
CALL qs_rho_get(rho, rho_ao=rho_ao)
CALL qs_rho_get(rho_aux, rho_ao=rho_ao_aux)
IF (s_mstruct_changed) THEN
! Calculate A = S_aux^(-1) * S_mixed
CALL dbcsr_create(matrix_s_aux_inv, template=matrix_s_aux(1)%matrix, matrix_type="N")
threshold = MAX(admm_dm%eps_filter, 1.0e-12_dp)
CALL invert_Hotelling(matrix_s_aux_inv, matrix_s_aux(1)%matrix, threshold)
IF (.NOT. ASSOCIATED(admm_dm%matrix_A)) THEN
ALLOCATE (admm_dm%matrix_A)
CALL dbcsr_create(admm_dm%matrix_A, template=matrix_s_mixed(1)%matrix, matrix_type="N")
END IF
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_s_aux_inv, matrix_s_mixed(1)%matrix, &
0.0_dp, admm_dm%matrix_A)
CALL dbcsr_release(matrix_s_aux_inv)
END IF
! Calculate P_aux = A * P * A^T
CALL dbcsr_create(matrix_tmp, template=admm_dm%matrix_A)
DO ispin = 1, dft_control%nspins
CALL dbcsr_multiply("N", "N", 1.0_dp, admm_dm%matrix_A, rho_ao(ispin)%matrix, &
0.0_dp, matrix_tmp)
CALL dbcsr_multiply("N", "T", 1.0_dp, matrix_tmp, admm_dm%matrix_A, &
0.0_dp, rho_ao_aux(ispin)%matrix)
END DO
CALL dbcsr_release(matrix_tmp)
END SUBROUTINE map_dm_projection
! **************************************************************************************************
!> \brief Calculates auxiliary density matrix via blocking.
!> \param qs_env ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE map_dm_blocked(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER :: blk, iatom, ispin, jatom
LOGICAL :: found
REAL(dp), DIMENSION(:, :), POINTER :: sparse_block, sparse_block_aux
TYPE(admm_dm_type), POINTER :: admm_dm
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao, rho_ao_aux
TYPE(dft_control_type), POINTER :: dft_control
TYPE(qs_rho_type), POINTER :: rho, rho_aux
NULLIFY (dft_control, admm_dm, rho, rho_aux, rho_ao, rho_ao_aux)
CALL get_qs_env(qs_env, dft_control=dft_control, rho=rho)
CALL get_admm_env(qs_env%admm_env, rho_aux_fit=rho_aux, admm_dm=admm_dm)
CALL qs_rho_get(rho, rho_ao=rho_ao)
CALL qs_rho_get(rho_aux, rho_ao=rho_ao_aux)
! ** set blocked density matrix to 0
DO ispin = 1, dft_control%nspins
CALL dbcsr_set(rho_ao_aux(ispin)%matrix, 0.0_dp)
! ** now loop through the list and copy corresponding blocks
CALL dbcsr_iterator_start(iter, rho_ao(ispin)%matrix)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, iatom, jatom, sparse_block, blk)
IF (admm_dm%block_map(iatom, jatom) == 1) THEN
CALL dbcsr_get_block_p(rho_ao_aux(ispin)%matrix, &
row=iatom, col=jatom, BLOCK=sparse_block_aux, found=found)
IF (found) &
sparse_block_aux = sparse_block
END IF
END DO
CALL dbcsr_iterator_stop(iter)
END DO
END SUBROUTINE map_dm_blocked
! **************************************************************************************************
!> \brief Call calculate_rho_elec() for auxiliary density
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE update_rho_aux(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER :: ispin
REAL(KIND=dp), DIMENSION(:), POINTER :: tot_rho_r_aux
TYPE(admm_dm_type), POINTER :: admm_dm
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao_aux
TYPE(dft_control_type), POINTER :: dft_control
TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g_aux
TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r_aux
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_rho_type), POINTER :: rho_aux
TYPE(task_list_type), POINTER :: task_list_aux_fit
NULLIFY (dft_control, admm_dm, rho_aux, rho_ao_aux, rho_r_aux, rho_g_aux, tot_rho_r_aux, &
task_list_aux_fit, ks_env)
CALL get_qs_env(qs_env, ks_env=ks_env, dft_control=dft_control)
CALL get_admm_env(qs_env%admm_env, task_list_aux_fit=task_list_aux_fit, rho_aux_fit=rho_aux, &
admm_dm=admm_dm)
CALL qs_rho_get(rho_aux, &
rho_ao=rho_ao_aux, &
rho_r=rho_r_aux, &
rho_g=rho_g_aux, &
tot_rho_r=tot_rho_r_aux)
DO ispin = 1, dft_control%nspins
CALL calculate_rho_elec(ks_env=ks_env, &
matrix_p=rho_ao_aux(ispin)%matrix, &
rho=rho_r_aux(ispin), &
rho_gspace=rho_g_aux(ispin), &
total_rho=tot_rho_r_aux(ispin), &
soft_valid=.FALSE., &
basis_type="AUX_FIT", &
task_list_external=task_list_aux_fit)
END DO
CALL qs_rho_set(rho_aux, rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
END SUBROUTINE update_rho_aux
! **************************************************************************************************
!> \brief Merges auxiliary Kohn-Sham matrix via basis projection.
!> \param qs_env ...
!> \param matrix_ks_merge Input: The KS matrix to be merged
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE merge_dm_projection(qs_env, matrix_ks_merge)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks_merge
INTEGER :: ispin
TYPE(admm_dm_type), POINTER :: admm_dm
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks
TYPE(dbcsr_type) :: matrix_tmp
TYPE(dft_control_type), POINTER :: dft_control
NULLIFY (admm_dm, dft_control, matrix_ks)
CALL get_qs_env(qs_env, dft_control=dft_control, matrix_ks=matrix_ks)
CALL get_admm_env(qs_env%admm_env, admm_dm=admm_dm)
! Calculate K += A^T * K_aux * A
CALL dbcsr_create(matrix_tmp, template=admm_dm%matrix_A, matrix_type="N")
DO ispin = 1, dft_control%nspins
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_ks_merge(ispin)%matrix, admm_dm%matrix_A, &
0.0_dp, matrix_tmp)
CALL dbcsr_multiply("T", "N", 1.0_dp, admm_dm%matrix_A, matrix_tmp, &
1.0_dp, matrix_ks(ispin)%matrix)
END DO
CALL dbcsr_release(matrix_tmp)
END SUBROUTINE merge_dm_projection
! **************************************************************************************************
!> \brief Merges auxiliary Kohn-Sham matrix via blocking.
!> \param qs_env ...
!> \param matrix_ks_merge Input: The KS matrix to be merged
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE merge_dm_blocked(qs_env, matrix_ks_merge)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks_merge
INTEGER :: blk, iatom, ispin, jatom
REAL(dp), DIMENSION(:, :), POINTER :: sparse_block
TYPE(admm_dm_type), POINTER :: admm_dm
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks
TYPE(dft_control_type), POINTER :: dft_control
NULLIFY (admm_dm, dft_control, matrix_ks)
CALL get_qs_env(qs_env, dft_control=dft_control, matrix_ks=matrix_ks)
CALL get_admm_env(qs_env%admm_env, admm_dm=admm_dm)
DO ispin = 1, dft_control%nspins
CALL dbcsr_iterator_start(iter, matrix_ks_merge(ispin)%matrix)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, iatom, jatom, sparse_block, blk)
IF (admm_dm%block_map(iatom, jatom) == 0) &
sparse_block = 0.0_dp
END DO
CALL dbcsr_iterator_stop(iter)
CALL dbcsr_add(matrix_ks(ispin)%matrix, matrix_ks_merge(ispin)%matrix, 1.0_dp, 1.0_dp)
END DO
END SUBROUTINE merge_dm_blocked
! **************************************************************************************************
!> \brief Apply McWeeny purification to auxiliary density matrix
!> \param qs_env ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE purify_mcweeny(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'purify_mcweeny'
INTEGER :: handle, ispin, istep, nspins, unit_nr
REAL(KIND=dp) :: frob_norm
TYPE(admm_dm_type), POINTER :: admm_dm
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s_aux_fit, rho_ao_aux
TYPE(dbcsr_type) :: matrix_ps, matrix_psp, matrix_test
TYPE(dbcsr_type), POINTER :: matrix_p, matrix_s
TYPE(dft_control_type), POINTER :: dft_control
TYPE(mcweeny_history_type), POINTER :: history, new_hist_entry
TYPE(qs_rho_type), POINTER :: rho_aux_fit
CALL timeset(routineN, handle)
NULLIFY (dft_control, admm_dm, matrix_s_aux_fit, rho_aux_fit, new_hist_entry, &
matrix_p, matrix_s, rho_ao_aux)
unit_nr = cp_logger_get_default_unit_nr()
CALL get_qs_env(qs_env, dft_control=dft_control)
CALL get_admm_env(qs_env%admm_env, matrix_s_aux_fit=matrix_s_aux_fit, &
rho_aux_fit=rho_aux_fit, admm_dm=admm_dm)
CALL qs_rho_get(rho_aux_fit, rho_ao=rho_ao_aux)
matrix_p => rho_ao_aux(1)%matrix
CALL dbcsr_create(matrix_PS, template=matrix_p, matrix_type="N")
CALL dbcsr_create(matrix_PSP, template=matrix_p, matrix_type="S")
CALL dbcsr_create(matrix_test, template=matrix_p, matrix_type="S")
nspins = dft_control%nspins
DO ispin = 1, nspins
matrix_p => rho_ao_aux(ispin)%matrix
matrix_s => matrix_s_aux_fit(1)%matrix
history => admm_dm%mcweeny_history(ispin)%p
IF (ASSOCIATED(history)) CPABORT("purify_dm_mcweeny: history already associated")
IF (nspins == 1) CALL dbcsr_scale(matrix_p, 0.5_dp)
DO istep = 1, admm_dm%mcweeny_max_steps
! allocate new element in linked list
ALLOCATE (new_hist_entry)
new_hist_entry%next => history
history => new_hist_entry
history%count = istep
NULLIFY (new_hist_entry)
CALL dbcsr_create(history%m, template=matrix_p, matrix_type="N")
CALL dbcsr_copy(history%m, matrix_p, name="P from McWeeny")
! calc PS and PSP
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_p, matrix_s, &
0.0_dp, matrix_ps)
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_ps, matrix_p, &
0.0_dp, matrix_psp)
!test convergence
CALL dbcsr_copy(matrix_test, matrix_psp)
CALL dbcsr_add(matrix_test, matrix_p, 1.0_dp, -1.0_dp)
frob_norm = dbcsr_frobenius_norm(matrix_test)
IF (unit_nr > 0) WRITE (unit_nr, '(t3,a,i5,a,f16.8)') "McWeeny-Step", istep, &
": Deviation of idempotency", frob_norm
IF (frob_norm < 1000_dp*admm_dm%eps_filter .AND. istep > 1) EXIT
! build next P matrix
CALL dbcsr_copy(matrix_p, matrix_PSP, name="P from McWeeny")
CALL dbcsr_multiply("N", "N", -2.0_dp, matrix_PS, matrix_PSP, &
3.0_dp, matrix_p)
END DO
admm_dm%mcweeny_history(ispin)%p => history
IF (nspins == 1) CALL dbcsr_scale(matrix_p, 2.0_dp)
END DO
! clean up
CALL dbcsr_release(matrix_PS)
CALL dbcsr_release(matrix_PSP)
CALL dbcsr_release(matrix_test)
CALL timestop(handle)
END SUBROUTINE purify_mcweeny
! **************************************************************************************************
!> \brief Prepare auxiliary KS-matrix for merge using reverse McWeeny
!> \param qs_env ...
!> \param matrix_ks_merge Output: The KS matrix for the merge
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE revert_purify_mcweeny(qs_env, matrix_ks_merge)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks_merge
CHARACTER(LEN=*), PARAMETER :: routineN = 'revert_purify_mcweeny'
INTEGER :: handle, ispin, nspins, unit_nr
TYPE(admm_dm_type), POINTER :: admm_dm
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_ks_aux_fit, &
matrix_s_aux_fit, &
matrix_s_aux_fit_vs_orb
TYPE(dbcsr_type), POINTER :: matrix_k
TYPE(dft_control_type), POINTER :: dft_control
TYPE(mcweeny_history_type), POINTER :: history_curr, history_next
CALL timeset(routineN, handle)
unit_nr = cp_logger_get_default_unit_nr()
NULLIFY (admm_dm, dft_control, matrix_ks, matrix_ks_aux_fit, &
matrix_s_aux_fit, matrix_s_aux_fit_vs_orb, &
history_next, history_curr, matrix_k)
CALL get_qs_env(qs_env, dft_control=dft_control, matrix_ks=matrix_ks)
CALL get_admm_env(qs_env%admm_env, matrix_s_aux_fit=matrix_s_aux_fit, admm_dm=admm_dm, &
matrix_s_aux_fit_vs_orb=matrix_s_aux_fit_vs_orb, matrix_ks_aux_fit=matrix_ks_aux_fit)
nspins = dft_control%nspins
ALLOCATE (matrix_ks_merge(nspins))
DO ispin = 1, nspins
ALLOCATE (matrix_ks_merge(ispin)%matrix)
matrix_k => matrix_ks_merge(ispin)%matrix
CALL dbcsr_copy(matrix_k, matrix_ks_aux_fit(ispin)%matrix, name="K")
history_curr => admm_dm%mcweeny_history(ispin)%p
NULLIFY (admm_dm%mcweeny_history(ispin)%p)
! reverse McWeeny iteration
DO WHILE (ASSOCIATED(history_curr))
IF (unit_nr > 0) WRITE (unit_nr, '(t3,a,i5)') "Reverse McWeeny-Step ", history_curr%count
CALL reverse_mcweeny_step(matrix_k=matrix_k, &
matrix_s=matrix_s_aux_fit(1)%matrix, &
matrix_p=history_curr%m)
CALL dbcsr_release(history_curr%m)
history_next => history_curr%next
DEALLOCATE (history_curr)
history_curr => history_next
NULLIFY (history_next)
END DO
END DO
! clean up
CALL timestop(handle)
END SUBROUTINE revert_purify_mcweeny
! **************************************************************************************************
!> \brief Multiply matrix_k with partial derivative of McWeeny by reversing it.
!> \param matrix_k ...
!> \param matrix_s ...
!> \param matrix_p ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE reverse_mcweeny_step(matrix_k, matrix_s, matrix_p)
TYPE(dbcsr_type) :: matrix_k, matrix_s, matrix_p
CHARACTER(LEN=*), PARAMETER :: routineN = 'reverse_mcweeny_step'
INTEGER :: handle
TYPE(dbcsr_type) :: matrix_ps, matrix_sp, matrix_sum, &
matrix_tmp
CALL timeset(routineN, handle)
CALL dbcsr_create(matrix_ps, template=matrix_p, matrix_type="N")
CALL dbcsr_create(matrix_sp, template=matrix_p, matrix_type="N")
CALL dbcsr_create(matrix_tmp, template=matrix_p, matrix_type="N")
CALL dbcsr_create(matrix_sum, template=matrix_p, matrix_type="N")
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_p, matrix_s, &
0.0_dp, matrix_ps)
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_s, matrix_p, &
0.0_dp, matrix_sp)
!TODO: can we exploid more symmetry?
CALL dbcsr_multiply("N", "N", 3.0_dp, matrix_k, matrix_ps, &
0.0_dp, matrix_sum)
CALL dbcsr_multiply("N", "N", 3.0_dp, matrix_sp, matrix_k, &
1.0_dp, matrix_sum)
!matrix_tmp = KPS
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_k, matrix_ps, &
0.0_dp, matrix_tmp)
CALL dbcsr_multiply("N", "N", -2.0_dp, matrix_tmp, matrix_ps, &
1.0_dp, matrix_sum)
CALL dbcsr_multiply("N", "N", -2.0_dp, matrix_sp, matrix_tmp, &
1.0_dp, matrix_sum)
!matrix_tmp = SPK
CALL dbcsr_multiply("N", "N", 1.0_dp, matrix_sp, matrix_k, &
0.0_dp, matrix_tmp)
CALL dbcsr_multiply("N", "N", -2.0_dp, matrix_sp, matrix_tmp, &
1.0_dp, matrix_sum)
! overwrite matrix_k
CALL dbcsr_copy(matrix_k, matrix_sum, name="K from reverse McWeeny")
! clean up
CALL dbcsr_release(matrix_sum)
CALL dbcsr_release(matrix_tmp)
CALL dbcsr_release(matrix_ps)
CALL dbcsr_release(matrix_sp)
CALL timestop(handle)
END SUBROUTINE reverse_mcweeny_step
END MODULE admm_dm_methods