Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Data types for the ADIIS SCF subspace accelerator.
10 : ! **************************************************************************************************
11 : MODULE qs_scf_subspace_types
12 :
13 : USE cp_dbcsr_api, ONLY: dbcsr_deallocate_matrix,&
14 : dbcsr_p_type
15 : USE kinds, ONLY: dp
16 : #include "./base/base_uses.f90"
17 :
18 : IMPLICIT NONE
19 :
20 : PRIVATE
21 :
22 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf_subspace_types'
23 :
24 : PUBLIC :: qs_scf_subspace_buffer_type, &
25 : qs_scf_subspace_buffer_clear, &
26 : qs_scf_subspace_buffer_create, &
27 : qs_scf_subspace_buffer_release
28 :
29 : ! **************************************************************************************************
30 : !> \brief History buffer holding strictly paired P and F[P] SCF states.
31 : !>
32 : !> Matrix history is stored in the real-space cell representation. This is
33 : !> the representation of the density that actually built the current KS
34 : !> matrix, and therefore applies to both Gamma-point and k-point runs.
35 : ! **************************************************************************************************
36 : TYPE qs_scf_subspace_buffer_type
37 : INTEGER :: nbuffer = 0
38 : INTEGER :: ncall = 0
39 : INTEGER :: nstored = 0
40 : INTEGER :: last_status = 0
41 : LOGICAL :: last_restart = .FALSE.
42 : LOGICAL :: use_combined_fock = .FALSE.
43 : REAL(KIND=dp) :: last_objective = 0.0_dp
44 : ! Total coefficient weight assigned to all entries older than the current raw Fock.
45 : REAL(KIND=dp) :: last_old_fock_weight = 0.0_dp
46 : REAL(KIND=dp) :: diis_weight = 0.0_dp
47 : TYPE(dbcsr_p_type), DIMENSION(:, :, :), POINTER :: density => NULL()
48 : TYPE(dbcsr_p_type), DIMENSION(:, :, :), POINTER :: fock => NULL()
49 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: combined_fock => NULL()
50 : INTEGER, DIMENSION(:), ALLOCATABLE :: generation
51 : REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: pf_metric
52 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: coefficients, state_energy
53 : END TYPE qs_scf_subspace_buffer_type
54 :
55 : CONTAINS
56 :
57 : ! **************************************************************************************************
58 : !> \brief Initialize SCF subspace history metadata.
59 : !> \param buffer Buffer to initialize.
60 : !> \param nbuffer Maximum number of paired history entries.
61 : ! **************************************************************************************************
62 24 : PURE SUBROUTINE qs_scf_subspace_buffer_create(buffer, nbuffer)
63 :
64 : TYPE(qs_scf_subspace_buffer_type), INTENT(OUT) :: buffer
65 : INTEGER, INTENT(IN) :: nbuffer
66 :
67 24 : buffer%nbuffer = MAX(nbuffer, 0)
68 : buffer%ncall = 0
69 : buffer%nstored = 0
70 : buffer%last_status = 0
71 : buffer%last_restart = .FALSE.
72 : buffer%use_combined_fock = .FALSE.
73 : buffer%last_objective = 0.0_dp
74 : buffer%last_old_fock_weight = 0.0_dp
75 : buffer%diis_weight = 0.0_dp
76 : NULLIFY (buffer%density, buffer%fock, buffer%combined_fock)
77 :
78 24 : END SUBROUTINE qs_scf_subspace_buffer_create
79 :
80 : ! **************************************************************************************************
81 : !> \brief Clear logical history while retaining allocated matrix storage.
82 : !> \param buffer Buffer to clear.
83 : ! **************************************************************************************************
84 12 : PURE SUBROUTINE qs_scf_subspace_buffer_clear(buffer)
85 :
86 : TYPE(qs_scf_subspace_buffer_type), INTENT(INOUT) :: buffer
87 :
88 12 : buffer%ncall = 0
89 12 : buffer%nstored = 0
90 12 : buffer%last_status = 0
91 12 : buffer%last_restart = .FALSE.
92 12 : buffer%use_combined_fock = .FALSE.
93 12 : buffer%last_objective = 0.0_dp
94 12 : buffer%last_old_fock_weight = 0.0_dp
95 12 : buffer%diis_weight = 0.0_dp
96 12 : IF (ALLOCATED(buffer%generation)) buffer%generation = 0
97 12 : IF (ALLOCATED(buffer%pf_metric)) buffer%pf_metric = 0.0_dp
98 12 : IF (ALLOCATED(buffer%coefficients)) buffer%coefficients = 0.0_dp
99 12 : IF (ALLOCATED(buffer%state_energy)) buffer%state_energy = HUGE(1.0_dp)
100 :
101 12 : END SUBROUTINE qs_scf_subspace_buffer_clear
102 :
103 : ! **************************************************************************************************
104 : !> \brief Release all matrices and scalar storage owned by a subspace buffer.
105 : !> \param buffer Buffer to release.
106 : ! **************************************************************************************************
107 12 : SUBROUTINE qs_scf_subspace_buffer_release(buffer)
108 :
109 : TYPE(qs_scf_subspace_buffer_type), INTENT(INOUT) :: buffer
110 :
111 : INTEGER :: icell, islot, ispin
112 :
113 12 : IF (ASSOCIATED(buffer%density)) THEN
114 566 : DO icell = 1, SIZE(buffer%density, 3)
115 1602 : DO ispin = 1, SIZE(buffer%density, 2)
116 18166 : DO islot = 1, SIZE(buffer%density, 1)
117 17612 : IF (ASSOCIATED(buffer%density(islot, ispin, icell)%matrix)) THEN
118 16576 : CALL dbcsr_deallocate_matrix(buffer%density(islot, ispin, icell)%matrix)
119 : END IF
120 : END DO
121 : END DO
122 : END DO
123 12 : DEALLOCATE (buffer%density)
124 : END IF
125 :
126 12 : IF (ASSOCIATED(buffer%fock)) THEN
127 566 : DO icell = 1, SIZE(buffer%fock, 3)
128 1602 : DO ispin = 1, SIZE(buffer%fock, 2)
129 18166 : DO islot = 1, SIZE(buffer%fock, 1)
130 17612 : IF (ASSOCIATED(buffer%fock(islot, ispin, icell)%matrix)) THEN
131 16576 : CALL dbcsr_deallocate_matrix(buffer%fock(islot, ispin, icell)%matrix)
132 : END IF
133 : END DO
134 : END DO
135 : END DO
136 12 : DEALLOCATE (buffer%fock)
137 : END IF
138 :
139 12 : IF (ASSOCIATED(buffer%combined_fock)) THEN
140 566 : DO icell = 1, SIZE(buffer%combined_fock, 2)
141 1602 : DO ispin = 1, SIZE(buffer%combined_fock, 1)
142 1590 : IF (ASSOCIATED(buffer%combined_fock(ispin, icell)%matrix)) THEN
143 1036 : CALL dbcsr_deallocate_matrix(buffer%combined_fock(ispin, icell)%matrix)
144 : END IF
145 : END DO
146 : END DO
147 12 : DEALLOCATE (buffer%combined_fock)
148 : END IF
149 :
150 12 : IF (ALLOCATED(buffer%generation)) DEALLOCATE (buffer%generation)
151 12 : IF (ALLOCATED(buffer%pf_metric)) DEALLOCATE (buffer%pf_metric)
152 12 : IF (ALLOCATED(buffer%coefficients)) DEALLOCATE (buffer%coefficients)
153 12 : IF (ALLOCATED(buffer%state_energy)) DEALLOCATE (buffer%state_energy)
154 12 : CALL qs_scf_subspace_buffer_create(buffer, 0)
155 :
156 12 : END SUBROUTINE qs_scf_subspace_buffer_release
157 :
158 0 : END MODULE qs_scf_subspace_types
|