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 : MODULE optimize_basis_types
8 :
9 : USE kinds, ONLY: default_path_length,&
10 : default_string_length,&
11 : dp
12 : USE powell, ONLY: opt_state_type
13 : #include "./base/base_uses.f90"
14 :
15 : IMPLICIT NONE
16 : PRIVATE
17 :
18 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'optimize_basis_types'
19 :
20 : PUBLIC :: basis_optimization_type, subset_type, flex_basis_type, &
21 : derived_basis_info, deallocate_basis_optimization_type, &
22 : method_mo_fit_occ, method_mo_fit_occ_virtual
23 :
24 : INTEGER, PARAMETER :: method_mo_fit_occ = 1
25 : INTEGER, PARAMETER :: method_mo_fit_occ_virtual = 2
26 :
27 : ! constraint information for a single constraing. boundary is translateed into a fermi function
28 : ! like setting as for variational limited case
29 : TYPE exp_constraint_type
30 : INTEGER :: const_type = -1
31 : REAL(KIND=dp) :: llim = -1.0_dp, ulim = -1.0_dp
32 : REAL(KIND=dp) :: init = -1.0_dp, var_fac = -1.0_dp
33 : END TYPE exp_constraint_type
34 :
35 : ! Subset of a basis+ additional information on what to optimize.
36 : ! *_x_ind maps to the index in the optimization vector
37 : ! opt_* logical whether quantity ahould be optimized
38 : ! *_const information for exponents used to constrain them
39 : TYPE subset_type
40 : INTEGER :: lmin = -1, lmax = -1, nexp = -1
41 : INTEGER :: n = -1, ncon_tot = -1, nl = -1
42 : INTEGER, DIMENSION(:), ALLOCATABLE :: l
43 : REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE :: coeff
44 : LOGICAL, DIMENSION(:, :), ALLOCATABLE :: opt_coeff
45 : INTEGER, DIMENSION(:, :), ALLOCATABLE :: coeff_x_ind
46 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: exps
47 : LOGICAL, DIMENSION(:), ALLOCATABLE :: opt_exps
48 : INTEGER, DIMENSION(:), ALLOCATABLE :: exp_x_ind
49 : LOGICAL, DIMENSION(:), ALLOCATABLE :: exp_has_const
50 : TYPE(exp_constraint_type), DIMENSION(:), &
51 : ALLOCATABLE :: exp_const
52 : END TYPE subset_type
53 :
54 : ! Top level information for basis sets+ vector subset with the real information
55 : TYPE flex_basis_type
56 : CHARACTER(LEN=default_string_length) :: basis_name = ""
57 : INTEGER :: nopt = -1
58 : INTEGER :: nsets = -1
59 : TYPE(subset_type), DIMENSION(:), ALLOCATABLE :: subset
60 : END TYPE flex_basis_type
61 :
62 : ! information for optimization: whether coeff has to be optimized or not
63 : TYPE use_contr_type
64 : LOGICAL, DIMENSION(:), ALLOCATABLE :: in_use
65 : END TYPE use_contr_type
66 :
67 : ! information about how to generate the derived basis sets
68 : TYPE derived_basis_info
69 : CHARACTER(LEN=default_string_length) :: basis_name = ""
70 : INTEGER :: reference_set = -1
71 : INTEGER, DIMENSION(:, :), ALLOCATABLE :: remove_contr
72 : INTEGER :: nsets = -1, ncontr = -1
73 : INTEGER, DIMENSION(:), ALLOCATABLE :: remove_set
74 : LOGICAL, DIMENSION(:), ALLOCATABLE :: in_use_set
75 : TYPE(use_contr_type), DIMENSION(:), ALLOCATABLE :: use_contr
76 : END TYPE derived_basis_info
77 :
78 : ! some usual stuff for basis information and an info type containing the
79 : ! the translated input on how to genrate the derived basis sets
80 : ! a flexible basis type for every derived basis
81 : ! ATTENTION: both vectors go from 0:nbasis_deriv. entry 0 is the one specified
82 : ! in the template basis file
83 : TYPE kind_basis_type
84 : CHARACTER(LEN=default_string_length) :: basis_name = ""
85 : CHARACTER(LEN=default_string_length) :: element = ""
86 : INTEGER :: nbasis_deriv = -1
87 : TYPE(derived_basis_info), DIMENSION(:), &
88 : ALLOCATABLE :: deriv_info
89 : TYPE(flex_basis_type), DIMENSION(:), ALLOCATABLE :: flex_basis
90 : END TYPE kind_basis_type
91 :
92 : ! vector of length nparallel_groups containing the id's of the calculations in the group
93 : TYPE comp_group_type
94 : INTEGER, DIMENSION(:), ALLOCATABLE :: member_list
95 : END TYPE comp_group_type
96 :
97 : ! **************************************************************************************************
98 : !> \brief type containing all information needed for basis matching
99 : !> \author Florian Schiffmann
100 : ! **************************************************************************************************
101 : TYPE basis_optimization_type
102 : TYPE(comp_group_type), DIMENSION(:), ALLOCATABLE :: comp_group
103 : INTEGER :: ntraining_sets = -1
104 : INTEGER :: ncombinations = -1
105 : LOGICAL :: use_condition_number = .FALSE.
106 : INTEGER, DIMENSION(:), POINTER :: group_partition => NULL()
107 : INTEGER :: n_groups_created = -1
108 : INTEGER, DIMENSION(:), ALLOCATABLE :: sub_sources
109 : INTEGER, DIMENSION(:, :), ALLOCATABLE :: combination
110 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: fval_weight
111 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: condition_weight
112 : INTEGER :: method = method_mo_fit_occ
113 : REAL(KIND=dp) :: occupied_weight = 1.0_dp
114 : REAL(KIND=dp) :: virtual_weight = 1.0_dp
115 : REAL(KIND=dp) :: empty_overlap_weight = 1.0_dp
116 : REAL(KIND=dp) :: gap_weight = 1.0E-3_dp
117 : REAL(KIND=dp) :: coefficient_weight = 1.0E-3_dp
118 : REAL(KIND=dp) :: gap_energy_scale = 0.0_dp
119 : REAL(KIND=dp) :: virtual_energy_cutoff = 0.0_dp
120 : REAL(KIND=dp) :: virtual_energy_smoothing = 0.0_dp
121 : LOGICAL :: quiet_output = .FALSE.
122 : INTEGER :: nkind = -1
123 : INTEGER :: write_frequency = -1
124 : INTEGER :: nbasis_deriv_types = -1
125 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: x_opt
126 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: x_initial
127 : TYPE(opt_state_type) :: powell_param = opt_state_type()
128 : CHARACTER(LEN=default_path_length), DIMENSION(:), ALLOCATABLE :: training_input
129 : CHARACTER(LEN=default_path_length), DIMENSION(:), ALLOCATABLE :: training_dir
130 : CHARACTER(LEN=default_path_length) :: work_basis_file = ""
131 : CHARACTER(LEN=default_path_length) :: output_basis_file = ""
132 : CHARACTER(LEN=default_path_length) :: template_basis_file = ""
133 : TYPE(kind_basis_type), DIMENSION(:), ALLOCATABLE :: kind_basis
134 : INTEGER :: opt_id = -1
135 : END TYPE basis_optimization_type
136 :
137 : CONTAINS
138 :
139 : ! **************************************************************************************************
140 : !> \brief Deallocate everything which was allocated before.
141 : !> Note not all arrays are used depending on the type of basis
142 : !> i.e derived or reference basis set
143 : !> \param opt_bas ...
144 : !> \author Florian Schiffmann
145 : ! **************************************************************************************************
146 :
147 8 : SUBROUTINE deallocate_basis_optimization_type(opt_bas)
148 : TYPE(basis_optimization_type) :: opt_bas
149 :
150 : INTEGER :: igroup, ikind
151 :
152 8 : IF (ASSOCIATED(opt_bas%group_partition)) DEALLOCATE (opt_bas%group_partition)
153 8 : IF (ALLOCATED(opt_bas%sub_sources)) DEALLOCATE (opt_bas%sub_sources)
154 8 : IF (ALLOCATED(opt_bas%combination)) DEALLOCATE (opt_bas%combination)
155 8 : IF (ALLOCATED(opt_bas%x_opt)) DEALLOCATE (opt_bas%x_opt)
156 8 : IF (ALLOCATED(opt_bas%x_initial)) DEALLOCATE (opt_bas%x_initial)
157 8 : IF (ALLOCATED(opt_bas%training_input)) DEALLOCATE (opt_bas%training_input)
158 8 : IF (ALLOCATED(opt_bas%training_dir)) DEALLOCATE (opt_bas%training_dir)
159 8 : IF (ALLOCATED(opt_bas%fval_weight)) DEALLOCATE (opt_bas%fval_weight)
160 8 : IF (ALLOCATED(opt_bas%condition_weight)) DEALLOCATE (opt_bas%condition_weight)
161 :
162 8 : IF (ALLOCATED(opt_bas%comp_group)) THEN
163 20 : DO igroup = 1, SIZE(opt_bas%comp_group)
164 20 : IF (ALLOCATED(opt_bas%comp_group(igroup)%member_list)) DEALLOCATE (opt_bas%comp_group(igroup)%member_list)
165 : END DO
166 20 : DEALLOCATE (opt_bas%comp_group)
167 : END IF
168 :
169 8 : IF (ALLOCATED(opt_bas%kind_basis)) THEN
170 24 : DO ikind = 1, SIZE(opt_bas%kind_basis)
171 24 : CALL deallocate_kind_basis(opt_bas%kind_basis(ikind))
172 : END DO
173 24 : DEALLOCATE (opt_bas%kind_basis)
174 : END IF
175 :
176 8 : END SUBROUTINE deallocate_basis_optimization_type
177 :
178 : ! **************************************************************************************************
179 : !> \brief Some more deallocation of the subtypes of optimize_absis type
180 : !> \param kind ...
181 : !> \author Florian Schiffmann
182 : ! **************************************************************************************************
183 :
184 16 : SUBROUTINE deallocate_kind_basis(kind)
185 : TYPE(kind_basis_type) :: kind
186 :
187 : INTEGER :: ibasis, icont, iinfo, iset
188 :
189 16 : IF (ALLOCATED(kind%deriv_info)) THEN
190 48 : DO iinfo = 0, SIZE(kind%deriv_info) - 1
191 32 : IF (ALLOCATED(kind%deriv_info(iinfo)%remove_contr)) DEALLOCATE (kind%deriv_info(iinfo)%remove_contr)
192 32 : IF (ALLOCATED(kind%deriv_info(iinfo)%remove_set)) DEALLOCATE (kind%deriv_info(iinfo)%remove_set)
193 32 : IF (ALLOCATED(kind%deriv_info(iinfo)%in_use_set)) DEALLOCATE (kind%deriv_info(iinfo)%in_use_set)
194 48 : IF (ALLOCATED(kind%deriv_info(iinfo)%use_contr)) THEN
195 64 : DO icont = 1, SIZE(kind%deriv_info(iinfo)%use_contr)
196 64 : IF (ALLOCATED(kind%deriv_info(iinfo)%use_contr(icont)%in_use)) THEN
197 32 : DEALLOCATE (kind%deriv_info(iinfo)%use_contr(icont)%in_use)
198 : END IF
199 : END DO
200 64 : DEALLOCATE (kind%deriv_info(iinfo)%use_contr)
201 : END IF
202 : END DO
203 48 : DEALLOCATE (kind%deriv_info)
204 : END IF
205 :
206 16 : IF (ALLOCATED(kind%flex_basis)) THEN
207 48 : DO ibasis = 0, SIZE(kind%flex_basis) - 1
208 48 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset)) THEN
209 64 : DO iset = 1, SIZE(kind%flex_basis(ibasis)%subset)
210 32 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset(iset)%l)) THEN
211 32 : DEALLOCATE (kind%flex_basis(ibasis)%subset(iset)%l)
212 : END IF
213 32 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset(iset)%coeff)) THEN
214 32 : DEALLOCATE (kind%flex_basis(ibasis)%subset(iset)%coeff)
215 : END IF
216 32 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset(iset)%opt_coeff)) THEN
217 16 : DEALLOCATE (kind%flex_basis(ibasis)%subset(iset)%opt_coeff)
218 : END IF
219 32 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset(iset)%coeff_x_ind)) THEN
220 16 : DEALLOCATE (kind%flex_basis(ibasis)%subset(iset)%coeff_x_ind)
221 : END IF
222 32 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset(iset)%exps)) THEN
223 32 : DEALLOCATE (kind%flex_basis(ibasis)%subset(iset)%exps)
224 : END IF
225 32 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset(iset)%opt_exps)) THEN
226 16 : DEALLOCATE (kind%flex_basis(ibasis)%subset(iset)%opt_exps)
227 : END IF
228 32 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset(iset)%exp_x_ind)) THEN
229 16 : DEALLOCATE (kind%flex_basis(ibasis)%subset(iset)%exp_x_ind)
230 : END IF
231 64 : IF (ALLOCATED(kind%flex_basis(ibasis)%subset(iset)%exp_const)) THEN
232 16 : DEALLOCATE (kind%flex_basis(ibasis)%subset(iset)%exp_const)
233 : END IF
234 : END DO
235 64 : DEALLOCATE (kind%flex_basis(ibasis)%subset)
236 : END IF
237 : END DO
238 48 : DEALLOCATE (kind%flex_basis)
239 : END IF
240 :
241 16 : END SUBROUTINE deallocate_kind_basis
242 :
243 0 : END MODULE optimize_basis_types
|