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 : LOGICAL :: diis_state_valid = .FALSE.
44 : REAL(KIND=dp) :: last_objective = 0.0_dp
45 : ! Total coefficient weight assigned to all entries older than the current raw Fock.
46 : REAL(KIND=dp) :: last_old_fock_weight = 0.0_dp
47 : REAL(KIND=dp) :: diis_weight = 0.0_dp
48 : TYPE(dbcsr_p_type), DIMENSION(:, :, :), POINTER :: density => NULL()
49 : TYPE(dbcsr_p_type), DIMENSION(:, :, :), POINTER :: fock => NULL()
50 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: combined_fock => NULL()
51 : INTEGER, DIMENSION(:), ALLOCATABLE :: generation
52 : REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: pf_metric
53 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: coefficients, state_energy
54 : END TYPE qs_scf_subspace_buffer_type
55 :
56 : CONTAINS
57 :
58 : ! **************************************************************************************************
59 : !> \brief Initialize SCF subspace history metadata.
60 : !> \param buffer Buffer to initialize.
61 : !> \param nbuffer Maximum number of paired history entries.
62 : ! **************************************************************************************************
63 32 : PURE SUBROUTINE qs_scf_subspace_buffer_create(buffer, nbuffer)
64 :
65 : TYPE(qs_scf_subspace_buffer_type), INTENT(OUT) :: buffer
66 : INTEGER, INTENT(IN) :: nbuffer
67 :
68 32 : buffer%nbuffer = MAX(nbuffer, 0)
69 : buffer%ncall = 0
70 : buffer%nstored = 0
71 : buffer%last_status = 0
72 : buffer%last_restart = .FALSE.
73 : buffer%use_combined_fock = .FALSE.
74 : buffer%diis_state_valid = .FALSE.
75 : buffer%last_objective = 0.0_dp
76 : buffer%last_old_fock_weight = 0.0_dp
77 : buffer%diis_weight = 0.0_dp
78 : NULLIFY (buffer%density, buffer%fock, buffer%combined_fock)
79 :
80 32 : END SUBROUTINE qs_scf_subspace_buffer_create
81 :
82 : ! **************************************************************************************************
83 : !> \brief Clear logical history while retaining allocated matrix storage.
84 : !> \param buffer Buffer to clear.
85 : ! **************************************************************************************************
86 16 : PURE SUBROUTINE qs_scf_subspace_buffer_clear(buffer)
87 :
88 : TYPE(qs_scf_subspace_buffer_type), INTENT(INOUT) :: buffer
89 :
90 16 : buffer%ncall = 0
91 16 : buffer%nstored = 0
92 16 : buffer%last_status = 0
93 16 : buffer%last_restart = .FALSE.
94 16 : buffer%use_combined_fock = .FALSE.
95 16 : buffer%diis_state_valid = .FALSE.
96 16 : buffer%last_objective = 0.0_dp
97 16 : buffer%last_old_fock_weight = 0.0_dp
98 16 : buffer%diis_weight = 0.0_dp
99 16 : IF (ALLOCATED(buffer%generation)) buffer%generation = 0
100 16 : IF (ALLOCATED(buffer%pf_metric)) buffer%pf_metric = 0.0_dp
101 16 : IF (ALLOCATED(buffer%coefficients)) buffer%coefficients = 0.0_dp
102 16 : IF (ALLOCATED(buffer%state_energy)) buffer%state_energy = HUGE(1.0_dp)
103 :
104 16 : END SUBROUTINE qs_scf_subspace_buffer_clear
105 :
106 : ! **************************************************************************************************
107 : !> \brief Release all matrices and scalar storage owned by a subspace buffer.
108 : !> \param buffer Buffer to release.
109 : ! **************************************************************************************************
110 16 : SUBROUTINE qs_scf_subspace_buffer_release(buffer)
111 :
112 : TYPE(qs_scf_subspace_buffer_type), INTENT(INOUT) :: buffer
113 :
114 : INTEGER :: icell, islot, ispin
115 :
116 16 : IF (ASSOCIATED(buffer%density)) THEN
117 644 : DO icell = 1, SIZE(buffer%density, 3)
118 1756 : DO ispin = 1, SIZE(buffer%density, 2)
119 19532 : DO islot = 1, SIZE(buffer%density, 1)
120 18904 : IF (ASSOCIATED(buffer%density(islot, ispin, icell)%matrix)) THEN
121 17792 : CALL dbcsr_deallocate_matrix(buffer%density(islot, ispin, icell)%matrix)
122 : END IF
123 : END DO
124 : END DO
125 : END DO
126 16 : DEALLOCATE (buffer%density)
127 : END IF
128 :
129 16 : IF (ASSOCIATED(buffer%fock)) THEN
130 644 : DO icell = 1, SIZE(buffer%fock, 3)
131 1756 : DO ispin = 1, SIZE(buffer%fock, 2)
132 19532 : DO islot = 1, SIZE(buffer%fock, 1)
133 18904 : IF (ASSOCIATED(buffer%fock(islot, ispin, icell)%matrix)) THEN
134 17792 : CALL dbcsr_deallocate_matrix(buffer%fock(islot, ispin, icell)%matrix)
135 : END IF
136 : END DO
137 : END DO
138 : END DO
139 16 : DEALLOCATE (buffer%fock)
140 : END IF
141 :
142 16 : IF (ASSOCIATED(buffer%combined_fock)) THEN
143 644 : DO icell = 1, SIZE(buffer%combined_fock, 2)
144 1756 : DO ispin = 1, SIZE(buffer%combined_fock, 1)
145 1740 : IF (ASSOCIATED(buffer%combined_fock(ispin, icell)%matrix)) THEN
146 1112 : CALL dbcsr_deallocate_matrix(buffer%combined_fock(ispin, icell)%matrix)
147 : END IF
148 : END DO
149 : END DO
150 16 : DEALLOCATE (buffer%combined_fock)
151 : END IF
152 :
153 16 : IF (ALLOCATED(buffer%generation)) DEALLOCATE (buffer%generation)
154 16 : IF (ALLOCATED(buffer%pf_metric)) DEALLOCATE (buffer%pf_metric)
155 16 : IF (ALLOCATED(buffer%coefficients)) DEALLOCATE (buffer%coefficients)
156 16 : IF (ALLOCATED(buffer%state_energy)) DEALLOCATE (buffer%state_energy)
157 16 : CALL qs_scf_subspace_buffer_create(buffer, 0)
158 :
159 16 : END SUBROUTINE qs_scf_subspace_buffer_release
160 :
161 0 : END MODULE qs_scf_subspace_types
|