1!--------------------------------------------------------------------------------------------------!
2!   CP2K: A general program to perform molecular dynamics simulations                              !
3!   Copyright (C) 2000 - 2019  CP2K developers group                                               !
4!--------------------------------------------------------------------------------------------------!
5
6! **************************************************************************************************
7!> \brief Types needed for a linear scaling quickstep SCF run based on the density
8!>        matrix
9!> \par History
10!>       2010.10 created [Joost VandeVondele]
11!> \author Joost VandeVondele
12! **************************************************************************************************
13MODULE dm_ls_scf_types
14   USE cp_para_env,                     ONLY: cp_para_env_release
15   USE cp_para_types,                   ONLY: cp_para_env_type
16   USE dbcsr_api,                       ONLY: dbcsr_release,&
17                                              dbcsr_type
18   USE input_constants,                 ONLY: ls_cluster_atomic,&
19                                              ls_cluster_molecular
20   USE input_section_types,             ONLY: section_vals_release,&
21                                              section_vals_type
22   USE kinds,                           ONLY: dp
23   USE pao_types,                       ONLY: pao_env_type,&
24                                              pao_finalize
25   USE pexsi_types,                     ONLY: lib_pexsi_env,&
26                                              lib_pexsi_finalize
27   USE qs_density_mixing_types,         ONLY: mixing_storage_release,&
28                                              mixing_storage_type
29#include "./base/base_uses.f90"
30
31   IMPLICIT NONE
32
33   PRIVATE
34
35   CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'dm_ls_scf_types'
36
37   PUBLIC :: ls_scf_env_type, ls_mstruct_type, ls_cluster_atomic, ls_cluster_molecular, &
38             ls_scf_curvy_type
39
40   TYPE ls_mstruct_type
41      INTEGER :: cluster_type
42      LOGICAL :: single_precision
43      LOGICAL :: do_pao
44      INTEGER, DIMENSION(:), ALLOCATABLE :: atom_to_molecule
45      TYPE(dbcsr_type)                :: matrix_A
46      TYPE(dbcsr_type)                :: matrix_B
47   END TYPE
48
49   TYPE ls_mat_history_type
50      INTEGER :: istore, nstore
51      TYPE(dbcsr_type), DIMENSION(:, :), ALLOCATABLE :: matrix
52   END TYPE
53
54   TYPE ls_scf_curvy_type
55      TYPE(dbcsr_type), DIMENSION(:), ALLOCATABLE :: matrix_dp
56      TYPE(dbcsr_type), DIMENSION(:), ALLOCATABLE :: matrix_p
57      TYPE(dbcsr_type), DIMENSION(:, :), ALLOCATABLE :: matrix_psave
58      TYPE(dbcsr_type), DIMENSION(:, :), ALLOCATABLE :: matrix_BCH
59      REAL(KIND=dp)                                    :: step_size(2)
60      REAL(KIND=dp)                                    :: shift(2)
61      REAL(KIND=dp)                                    :: cg_denom(2)
62      REAL(KIND=dp)                                    :: cg_numer(2)
63      REAL(KIND=dp), DIMENSION(6)                      :: energies
64      INTEGER                                          :: line_search_step
65      INTEGER                                          :: BCH_saved(2)
66      LOGICAL                                          :: double_step_size
67      LOGICAL                                          :: fix_shift(2)
68
69      INTEGER                                          :: line_search_type
70      INTEGER                                          :: n_bch_hist
71      REAL(KIND=dp)                                    :: scale_filter
72      REAL(KIND=dp)                                    :: filter_factor
73      REAL(KIND=dp)                                    :: min_shift
74      REAL(KIND=dp)                                    :: min_filter
75   END TYPE
76
77   TYPE chebyshev_type
78      LOGICAL :: compute_chebyshev
79      INTEGER :: n_chebyshev
80      INTEGER :: n_gridpoint_dos
81      REAL(KIND=dp), DIMENSION(:), POINTER :: min_energy => NULL()
82      REAL(KIND=dp), DIMENSION(:), POINTER :: max_energy => NULL()
83      TYPE(section_vals_type), POINTER :: print_key_dos => NULL()
84      TYPE(section_vals_type), POINTER :: print_key_cube => NULL()
85   END TYPE
86
87   TYPE ls_scf_env_type
88      INTEGER               :: nspins, natoms
89      INTEGER               :: nelectron_total
90      INTEGER, DIMENSION(2) :: nelectron_spin
91      REAL(KIND=dp), DIMENSION(2) ::  mu_spin
92      REAL(KIND=dp), DIMENSION(2) ::  homo_spin
93      REAL(KIND=dp), DIMENSION(2) ::  lumo_spin
94
95      TYPE(ls_mat_history_type) :: scf_history
96      INTEGER :: extrapolation_order
97
98      LOGICAL :: has_unit_metric
99
100      LOGICAL :: curvy_steps
101      INTEGER :: s_preconditioner_type
102      INTEGER :: s_inversion_type
103      INTEGER :: purification_method
104      INTEGER :: sign_method
105      INTEGER :: sign_order
106      INTEGER :: s_sqrt_method
107      INTEGER :: s_sqrt_order
108
109      LOGICAL               :: needs_s_inv, has_s_preconditioner, fixed_mu, dynamic_threshold, check_s_inv
110      LOGICAL               :: restart_read, restart_write, non_monotonic
111      REAL(KIND=dp)         :: eps_filter, eps_scf
112
113      REAL(KIND=dp)         :: eps_lanczos
114      INTEGER               :: max_iter_lanczos
115
116      REAL(KIND=dp)         :: mixing_fraction
117      INTEGER               :: max_scf
118      LOGICAL               :: ls_diis
119      INTEGER               :: iter_ini_diis
120      INTEGER               :: nmixing, max_diis
121      REAL(KIND=dp)         :: eps_diis
122      REAL(KIND=dp)         :: energy_init
123
124      TYPE(dbcsr_type)   :: matrix_s_inv
125      TYPE(dbcsr_type)   :: matrix_s
126      TYPE(dbcsr_type)   :: matrix_bs_sqrt, matrix_bs_sqrt_inv
127      TYPE(dbcsr_type)   :: matrix_s_sqrt, matrix_s_sqrt_inv
128      TYPE(dbcsr_type), DIMENSION(:), ALLOCATABLE :: matrix_ks
129      TYPE(dbcsr_type), DIMENSION(:), ALLOCATABLE :: matrix_p
130
131      LOGICAL  :: report_all_sparsities, perform_mu_scan, use_s_sqrt
132
133      TYPE(ls_mstruct_type) :: ls_mstruct
134      TYPE(ls_scf_curvy_type) :: curvy_data
135
136      TYPE(chebyshev_type) :: chebyshev
137
138      LOGICAL :: do_rho_mixing
139      INTEGER :: density_mixing_method
140      TYPE(mixing_storage_type), POINTER :: mixing_store
141
142      LOGICAL :: do_transport
143      LOGICAL :: do_pexsi
144
145      LOGICAL :: calculate_forces
146
147      TYPE(lib_pexsi_env) :: pexsi
148
149      TYPE(cp_para_env_type), POINTER :: para_env
150      LOGICAL                 :: do_pao = .FALSE.
151      TYPE(pao_env_type)      :: pao_env
152   END TYPE ls_scf_env_type
153
154   PUBLIC :: ls_scf_release
155
156CONTAINS
157
158! **************************************************************************************************
159!> \brief release the LS type.
160!> \param ls_scf_env ...
161!> \par History
162!>       2012.11 created [Joost VandeVondele]
163!> \author Joost VandeVondele
164! **************************************************************************************************
165   SUBROUTINE ls_scf_release(ls_scf_env)
166      TYPE(ls_scf_env_type), POINTER                     :: ls_scf_env
167
168      CHARACTER(len=*), PARAMETER :: routineN = 'ls_scf_release', routineP = moduleN//':'//routineN
169
170      INTEGER                                            :: handle, ispin, istore
171
172      CALL timeset(routineN, handle)
173
174      CALL cp_para_env_release(ls_scf_env%para_env)
175
176      DEALLOCATE (ls_scf_env%ls_mstruct%atom_to_molecule)
177
178      ! set up the buffer for the history of matrices
179      DO istore = 1, MIN(ls_scf_env%scf_history%istore, ls_scf_env%scf_history%nstore)
180         DO ispin = 1, SIZE(ls_scf_env%scf_history%matrix, 1)
181            CALL dbcsr_release(ls_scf_env%scf_history%matrix(ispin, istore))
182         ENDDO
183      ENDDO
184      DEALLOCATE (ls_scf_env%scf_history%matrix)
185
186      IF (ALLOCATED(ls_scf_env%matrix_p)) THEN
187         DO ispin = 1, SIZE(ls_scf_env%matrix_p)
188            CALL dbcsr_release(ls_scf_env%matrix_p(ispin))
189         ENDDO
190         DEALLOCATE (ls_scf_env%matrix_p)
191      END IF
192
193      IF (ASSOCIATED(ls_scf_env%chebyshev%print_key_dos)) &
194         CALL section_vals_release(ls_scf_env%chebyshev%print_key_dos)
195      IF (ASSOCIATED(ls_scf_env%chebyshev%print_key_cube)) &
196         CALL section_vals_release(ls_scf_env%chebyshev%print_key_cube)
197      IF (ASSOCIATED(ls_scf_env%chebyshev%min_energy)) THEN
198         DEALLOCATE (ls_scf_env%chebyshev%min_energy)
199      ENDIF
200      IF (ASSOCIATED(ls_scf_env%chebyshev%max_energy)) THEN
201         DEALLOCATE (ls_scf_env%chebyshev%max_energy)
202      ENDIF
203
204      IF (ASSOCIATED(ls_scf_env%mixing_store)) THEN
205         CALL mixing_storage_release(ls_scf_env%mixing_store)
206      ENDIF
207
208      IF (ls_scf_env%do_pexsi) THEN
209         CALL lib_pexsi_finalize(ls_scf_env%pexsi)
210      ENDIF
211
212      IF (ls_scf_env%do_pao) &
213         CALL pao_finalize(ls_scf_env%pao_env)
214
215      DEALLOCATE (ls_scf_env)
216
217      CALL timestop(handle)
218
219   END SUBROUTINE ls_scf_release
220
221END MODULE dm_ls_scf_types
222
223