Line data Source code
1 : !--------------------------------------------------------------------------------------------------! 2 : ! CP2K: A general program to perform molecular dynamics simulations ! 3 : ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> ! 4 : ! ! 5 : ! SPDX-License-Identifier: GPL-2.0-or-later ! 6 : !--------------------------------------------------------------------------------------------------! 7 : 8 : ! ************************************************************************************************** 9 : !> \brief Types used by the PAO machinery 10 : !> \author Ole Schuett 11 : ! ************************************************************************************************** 12 : MODULE pao_types 13 : USE cp_dbcsr_api, ONLY: dbcsr_distribution_release,& 14 : dbcsr_distribution_type,& 15 : dbcsr_release,& 16 : dbcsr_type 17 : USE kinds, ONLY: default_path_length,& 18 : default_string_length,& 19 : dp 20 : USE linesearch, ONLY: linesearch_type 21 : #include "./base/base_uses.f90" 22 : 23 : IMPLICIT NONE 24 : 25 : PRIVATE 26 : 27 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'pao_types' 28 : 29 : PUBLIC :: pao_env_type, training_matrix_type, pao_finalize 30 : 31 : TYPE filename_type 32 : CHARACTER(LEN=default_path_length) :: fn = "" 33 : END TYPE filename_type 34 : 35 : ! ************************************************************************************************** 36 : !> \brief PAO machine learning data for one atomic kind 37 : !> \var kindname name of atomic kind 38 : !> \var inputs training points 39 : !> \var outputs training points 40 : !> \var prior constant prior which is added to prediction 41 : !> \var NN trained neural network 42 : !> \var GP trained gaussian process 43 : ! ************************************************************************************************** 44 : TYPE training_matrix_type 45 : CHARACTER(LEN=default_string_length) :: kindname = "" 46 : REAL(dp), DIMENSION(:, :), ALLOCATABLE :: inputs 47 : REAL(dp), DIMENSION(:, :), ALLOCATABLE :: outputs 48 : REAL(dp), DIMENSION(:), ALLOCATABLE :: prior 49 : REAL(dp), DIMENSION(:, :, :), ALLOCATABLE :: NN ! Neural Network 50 : REAL(dp), DIMENSION(:, :), ALLOCATABLE :: GP ! Gaussian Process 51 : END TYPE training_matrix_type 52 : 53 : ! ************************************************************************************************** 54 : !> \brief The PAO environment type 55 : !> \var eps_pao parsed input parameter 56 : !> \var cg_reset_limit parsed input parameter 57 : !> \var mixing parsed input parameter 58 : !> \var regularization parsed input parameter 59 : !> \var penalty_dist parsed input parameter 60 : !> \var penalty_strength parsed input parameter 61 : !> \var check_unitary_tol parsed input parameter 62 : !> \var check_grad_tol parsed input parameter 63 : !> \var num_grad_eps parsed input parameter 64 : !> \var eps_pgf parsed input parameter 65 : !> \var linpot_precon_delta parsed input parameter 66 : !> \var linpot_init_delta parsed input parameter 67 : !> \var linpot_regu_delta parsed input parameter 68 : !> \var linpot_regu_strength parsed input parameter 69 : !> \var num_grad_order parsed input parameter 70 : !> \var max_pao parsed input parameter 71 : !> \var max_cycles parsed input parameter 72 : !> \var write_cycles parsed input parameter 73 : !> \var parameterization parsed input parameter 74 : !> \var optimizer parsed input parameter 75 : !> \var cg_init_steps parsed input parameter 76 : !> \var preopt_dm_file parsed input parameter 77 : !> \var restart_file parsed input parameter 78 : !> \var ml_training_set parsed input parameter 79 : !> \var ml_method parsed input parameter 80 : !> \var ml_prior parsed input parameter 81 : !> \var ml_descriptor parsed input parameter 82 : !> \var ml_tolerance parsed input parameter 83 : !> \var gp_noise_var parsed input parameter 84 : !> \var gp_scale parsed input parameter 85 : !> \var precondition parsed input parameter 86 : !> \var iw output unit for pao in general 87 : !> \var iw_atoms output unit for one line summary for each atom 88 : !> \var iw_gap output unit for gap of the fock matrix 89 : !> \var iw_fockev output unit for eigenvalues of the fock matrix 90 : !> \var iw_opt output unit for pao optimizer 91 : !> \var iw_mlvar output unit for variances of machine learning predictions 92 : !> \var iw_mldata output unit for dumping training data used for machine learning 93 : !> \var istep counts pao iterations, ie. number of pao energy evaluations 94 : !> \var energy_prev energy of previous pao step 95 : !> \var step_start_time timestamp of when current pao step started 96 : !> \var norm_G frobenius-norm of matrix_G or matrix_G_preconed 97 : !> \var linesearch holds linesearch state 98 : !> \var matrix_X_ready set when matrix_X is initialized 99 : !> \var matrix_P_ready set when density matrix is initialized 100 : !> \var constants_ready set when stuff, which does not depend of atomic positions is ready 101 : !> \var need_initial_scf set when the initial density matrix is not self-consistend 102 : !> \var matrix_X parameters of pao basis, which eventually determine matrix_U. Uses diag_distribution. 103 : !> \var matrix_U0 constant pre-rotation which serves as initial guess for exp-parametrization. Uses diag_distribution. 104 : !> \var matrix_H0 Diagonal blocks of core hamiltonian, uses diag_distribution 105 : !> \var matrix_Y selector matrix which translates between primary and pao basis. 106 : !> basically a block diagonal "rectangular identity matrix". Uses s_matrix-distribution. 107 : !> \var matrix_N diagonal matrix filled with 1/sqrt(S) from primary overlap matrix. Uses s_matrix-distribution. 108 : !> \var matrix_N_diag copy of matrix_N using diag_distribution 109 : !> \var matrix_N_inv diagonal matrix filled with sqrt(S) from primary overlap matrix. Uses s_matrix-distribution. 110 : !> \var matrix_N_inv_diag copy of matrix_N_inv using diag_distribution 111 : !> \var matrix_X_orig copy made of matrix_X at beginning of optimization cylce, used for mixing. Uses diag_distribution. 112 : !> \var matrix_G derivative of pao-energy wrt to matrix_X, ie. the pao-gradient. Uses diag_distribution. 113 : !> \var matrix_G_prev copy of gradient from previous step, used for conjugate gradient method. Uses diag_distribution. 114 : !> \var matrix_D Current line-search direction, used for conjugate gradient method. Uses diag_distribution. 115 : !> \var matrix_D_preconed Current line-search direction with preconditioner applied. 116 : !> This copy is keept, because application of inverse preconditioner 117 : !> introduces too much numeric noise. Uses diag_distribution. 118 : !> \var matrix_V_terms Potential terms, used by linpot and gth parametrization, Uses diag_distribution. 119 : !> \var matrix_BFGS Approximate inverse hessian, used by BFGS optimizer, Uses diag_distribution. 120 : !> \var matrix_precon preconditioner, uses diag_distribution. 121 : !> \var matrix_precon_inv inverse of matrix_precon, uses diag_distribution. 122 : !> \var matrix_R Rgularization, uses diag_distribution 123 : !> \var ml_training_matrices holds training data and trained machine learning model 124 : !> \var diag_distribution DBCSR distribution to spreads diagonal blocks evenly across ranks 125 : ! ************************************************************************************************** 126 : TYPE pao_env_type 127 : ! input values 128 : REAL(KIND=dp) :: eps_pao = 0.0_dp 129 : REAL(KIND=dp) :: cg_reset_limit = 0.1_dp 130 : REAL(KIND=dp) :: mixing = 0.0_dp 131 : REAL(KIND=dp) :: regularization = 0.0_dp 132 : REAL(KIND=dp) :: penalty_dist = 0.0_dp 133 : REAL(KIND=dp) :: penalty_strength = 0.0_dp 134 : REAL(KIND=dp) :: check_unitary_tol = 0.0_dp 135 : REAL(KIND=dp) :: check_grad_tol = 0.0_dp 136 : REAL(KIND=dp) :: num_grad_eps = 0.0_dp 137 : REAL(KIND=dp) :: eps_pgf = 0.0_dp 138 : REAL(KIND=dp) :: linpot_precon_delta = 0.0_dp 139 : REAL(KIND=dp) :: linpot_init_delta = 0.0_dp 140 : REAL(KIND=dp) :: linpot_regu_delta = 0.0_dp 141 : REAL(KIND=dp) :: linpot_regu_strength = 0.0_dp 142 : INTEGER :: num_grad_order = -1 143 : INTEGER :: max_pao = -1 144 : INTEGER :: max_cycles = -1 145 : INTEGER :: write_cycles = -1 146 : INTEGER :: parameterization = -1 147 : INTEGER :: optimizer = -1 148 : INTEGER :: cg_init_steps = -1 149 : LOGICAL :: precondition = .FALSE. 150 : CHARACTER(LEN=default_path_length) :: preopt_dm_file = "" 151 : CHARACTER(LEN=default_path_length) :: restart_file = "" 152 : TYPE(filename_type), DIMENSION(:), ALLOCATABLE :: ml_training_set 153 : 154 : INTEGER :: ml_method = -1 155 : INTEGER :: ml_prior = -1 156 : INTEGER :: ml_descriptor = -1 157 : REAL(KIND=dp) :: ml_tolerance = 0.0_dp 158 : REAL(KIND=dp) :: gp_noise_var = 0.0_dp 159 : REAL(KIND=dp) :: gp_scale = 0.0_dp 160 : 161 : ! output units 162 : INTEGER :: iw = -1 163 : INTEGER :: iw_atoms = -1 164 : INTEGER :: iw_gap = -1 165 : INTEGER :: iw_fockev = -1 166 : INTEGER :: iw_opt = -1 167 : INTEGER :: iw_mlvar = -1 168 : INTEGER :: iw_mldata = -1 169 : 170 : ! state variable 171 : INTEGER :: istep = -1 172 : REAL(KIND=dp) :: energy_prev = 0.0_dp 173 : REAL(KIND=dp) :: step_start_time = 0.0_dp 174 : REAL(KIND=dp) :: norm_G = 0.0_dp 175 : TYPE(linesearch_type) :: linesearch = linesearch_type() 176 : LOGICAL :: matrix_X_ready = .FALSE. 177 : LOGICAL :: matrix_P_ready = .FALSE. 178 : LOGICAL :: constants_ready = .FALSE. 179 : LOGICAL :: need_initial_scf = .FALSE. 180 : 181 : ! matrices 182 : TYPE(dbcsr_type) :: matrix_X 183 : TYPE(dbcsr_type) :: matrix_U0 184 : TYPE(dbcsr_type) :: matrix_H0 185 : TYPE(dbcsr_type) :: matrix_Y 186 : TYPE(dbcsr_type) :: matrix_N 187 : TYPE(dbcsr_type) :: matrix_N_diag 188 : TYPE(dbcsr_type) :: matrix_N_inv 189 : TYPE(dbcsr_type) :: matrix_N_inv_diag 190 : TYPE(dbcsr_type) :: matrix_X_orig 191 : TYPE(dbcsr_type) :: matrix_G 192 : TYPE(dbcsr_type) :: matrix_G_prev 193 : TYPE(dbcsr_type) :: matrix_D 194 : TYPE(dbcsr_type) :: matrix_D_preconed 195 : TYPE(dbcsr_type) :: matrix_V_terms 196 : TYPE(dbcsr_type) :: matrix_BFGS 197 : TYPE(dbcsr_type) :: matrix_precon 198 : TYPE(dbcsr_type) :: matrix_precon_inv 199 : TYPE(dbcsr_type) :: matrix_R 200 : 201 : TYPE(training_matrix_type), ALLOCATABLE, & 202 : DIMENSION(:) :: ml_training_matrices 203 : 204 : TYPE(dbcsr_distribution_type) :: diag_distribution 205 : END TYPE 206 : 207 : CONTAINS 208 : 209 : ! ************************************************************************************************** 210 : !> \brief Finalize the PAO environment 211 : !> \param pao ... 212 : ! ************************************************************************************************** 213 94 : SUBROUTINE pao_finalize(pao) 214 : TYPE(pao_env_type) :: pao 215 : 216 : CHARACTER(len=*), PARAMETER :: routineN = 'pao_finalize' 217 : 218 : INTEGER :: handle 219 : 220 94 : CALL timeset(routineN, handle) 221 : 222 94 : CALL dbcsr_release(pao%matrix_X) 223 94 : CALL dbcsr_release(pao%matrix_Y) 224 94 : CALL dbcsr_release(pao%matrix_N) 225 94 : CALL dbcsr_release(pao%matrix_N_diag) 226 94 : CALL dbcsr_release(pao%matrix_N_inv) 227 94 : CALL dbcsr_release(pao%matrix_N_inv_diag) 228 94 : CALL dbcsr_release(pao%matrix_H0) 229 : 230 94 : DEALLOCATE (pao%ml_training_set) 231 94 : IF (ALLOCATED(pao%ml_training_matrices)) & 232 38 : DEALLOCATE (pao%ml_training_matrices) 233 : 234 94 : CALL dbcsr_distribution_release(pao%diag_distribution) 235 : 236 : !TODO: should finish printkey 237 : 238 94 : CALL timestop(handle) 239 : 240 94 : END SUBROUTINE pao_finalize 241 : 242 0 : END MODULE pao_types