(git:e7e05ae)
optimize_basis.F
Go to the documentation of this file.
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 !--------------------------------------------------------------------------------------------------!
8  USE cp_blacs_env, ONLY: cp_blacs_env_type
12  cp_fm_struct_type
13  USE cp_fm_types, ONLY: cp_fm_release,&
14  cp_fm_type
17  cp_logger_type
18  USE dbcsr_api, ONLY: dbcsr_p_type
19  USE f77_interface, ONLY: create_force_env,&
24  f_env_type
25  USE force_env_types, ONLY: force_env_get,&
26  force_env_type
27  USE input_cp2k_read, ONLY: empty_initial_variables,&
29  USE input_section_types, ONLY: section_type,&
31  section_vals_type
32  USE kinds, ONLY: default_path_length,&
33  dp
34  USE machine, ONLY: m_chdir,&
35  m_getcwd,&
37  USE message_passing, ONLY: mp_comm_type,&
39  mp_para_env_type
48  USE optimize_basis_types, ONLY: basis_optimization_type,&
50  subset_type
54  USE powell, ONLY: powell_optimize
55  USE qs_environment_types, ONLY: get_qs_env,&
57  qs_environment_type
58  USE qs_kind_types, ONLY: get_qs_kind_set,&
59  qs_kind_type
60  USE qs_ks_types, ONLY: get_ks_env,&
61  qs_ks_env_type,&
63  USE qs_mo_types, ONLY: allocate_mo_set,&
65  get_mo_set,&
66  init_mo_set,&
67  mo_set_type
68  USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type,&
72 #include "./base/base_uses.f90"
73 
74  IMPLICIT NONE
75  PRIVATE
76 
77  PUBLIC :: run_optimize_basis
78 
79  CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'optimize_basis'
80 
81 CONTAINS
82 
83 ! **************************************************************************************************
84 !> \brief main entry point for methods aimed at optimizing basis sets
85 !> \param input_declaration ...
86 !> \param root_section ...
87 !> \param para_env ...
88 !> \author Florian Schiffmann
89 ! **************************************************************************************************
90  SUBROUTINE run_optimize_basis(input_declaration, root_section, para_env)
91  TYPE(section_type), POINTER :: input_declaration
92  TYPE(section_vals_type), POINTER :: root_section
93  TYPE(mp_para_env_type), POINTER :: para_env
94 
95  CHARACTER(len=*), PARAMETER :: routinen = 'run_optimize_basis'
96 
97  INTEGER :: handle
98  TYPE(basis_optimization_type) :: opt_bas
99 
100  CALL timeset(routinen, handle)
101 
102  CALL optimize_basis_init_read_input(opt_bas, root_section, para_env)
103 
104  CALL driver_para_opt_basis(opt_bas, input_declaration, para_env)
105 
107  CALL timestop(handle)
108 
109  END SUBROUTINE run_optimize_basis
110 
111 ! **************************************************************************************************
112 !> \brief driver routine for the parallel part of the method
113 !> \param opt_bas ...
114 !> \param input_declaration ...
115 !> \param para_env ...
116 !> \author Florian Schiffmann
117 ! **************************************************************************************************
118 
119  SUBROUTINE driver_para_opt_basis(opt_bas, input_declaration, para_env)
120  TYPE(basis_optimization_type) :: opt_bas
121  TYPE(section_type), POINTER :: input_declaration
122  TYPE(mp_para_env_type), POINTER :: para_env
123 
124  CHARACTER(len=*), PARAMETER :: routinen = 'driver_para_opt_basis'
125 
126  INTEGER :: handle, n_groups_created
127  TYPE(mp_comm_type) :: opt_group
128  INTEGER, DIMENSION(:), POINTER :: group_distribution_p
129  INTEGER, DIMENSION(0:para_env%num_pe-1), TARGET :: group_distribution
130 
131  CALL timeset(routinen, handle)
132  group_distribution_p => group_distribution
133  CALL opt_group%from_split(para_env, n_groups_created, group_distribution_p, &
134  n_subgroups=SIZE(opt_bas%group_partition), group_partition=opt_bas%group_partition)
135  opt_bas%opt_id = group_distribution(para_env%mepos) + 1
136  opt_bas%n_groups_created = n_groups_created
137  ALLOCATE (opt_bas%sub_sources(0:para_env%num_pe - 1))
138 
139  CALL driver_optimization_para_low(opt_bas, input_declaration, para_env, opt_group)
140 
141  CALL opt_group%free()
142  CALL timestop(handle)
143 
144  END SUBROUTINE driver_para_opt_basis
145 
146 ! **************************************************************************************************
147 !> \brief low level optimization routine includes initialization of the subsytems
148 !> powell optimizer and deallocation of the various force envs
149 !> \param opt_bas ...
150 !> \param input_declaration ...
151 !> \param para_env_top ...
152 !> \param mpi_comm_opt ...
153 !> \author Florian Schiffmann
154 ! **************************************************************************************************
155 
156  SUBROUTINE driver_optimization_para_low(opt_bas, input_declaration, para_env_top, mpi_comm_opt)
157  TYPE(basis_optimization_type) :: opt_bas
158  TYPE(section_type), POINTER :: input_declaration
159  TYPE(mp_para_env_type), POINTER :: para_env_top
160  TYPE(mp_comm_type), INTENT(IN) :: mpi_comm_opt
161 
162  CHARACTER(len=*), PARAMETER :: routinen = 'driver_optimization_para_low'
163 
164  INTEGER :: handle, icalc, iopt, is, mp_id, stat
165  INTEGER, ALLOCATABLE, DIMENSION(:) :: f_env_id
166  LOGICAL :: write_basis
167  REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: tot_time
168  TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: matrix_s_inv
169  TYPE(f_env_type), POINTER :: f_env
170  TYPE(mp_para_env_type), POINTER :: para_env
171 
172  NULLIFY (f_env)
173 
174  CALL timeset(routinen, handle)
175 
176  ! ====== initialize the f_env and precompute some matrices =====
177  mp_id = opt_bas%opt_id
178  NULLIFY (para_env, f_env)
179  ALLOCATE (f_env_id(SIZE(opt_bas%comp_group(mp_id)%member_list)))
180  ALLOCATE (tot_time(opt_bas%ncombinations*opt_bas%ntraining_sets))
181  ALLOCATE (matrix_s_inv(SIZE(opt_bas%comp_group(mp_id)%member_list)))
182 
183  ALLOCATE (para_env)
184  para_env = mpi_comm_opt
185 
186  is = -1
187  IF (para_env%is_source()) is = para_env_top%mepos
188  CALL para_env_top%allgather(is, opt_bas%sub_sources)
189 
190  CALL init_training_force_envs(opt_bas, f_env_id, input_declaration, matrix_s_inv, para_env, mpi_comm_opt)
191 
192  CALL init_free_vars(opt_bas)
193  tot_time = 0.0_dp
194 
195  ! ======= The real optimization loop =======
196  DO iopt = 0, opt_bas%powell_param%maxfun
197  CALL compute_residuum_vectors(opt_bas, f_env_id, matrix_s_inv, tot_time, &
198  para_env_top, para_env, iopt)
199  IF (para_env_top%is_source()) &
200  CALL powell_optimize(opt_bas%powell_param%nvar, opt_bas%x_opt, opt_bas%powell_param)
201  CALL para_env_top%bcast(opt_bas%powell_param%state)
202  CALL para_env_top%bcast(opt_bas%x_opt)
203  CALL update_free_vars(opt_bas)
204  write_basis = mod(iopt, opt_bas%write_frequency) == 0
205  CALL update_derived_basis_sets(opt_bas, write_basis, opt_bas%output_basis_file, &
206  para_env_top)
207  IF (opt_bas%powell_param%state == -1) EXIT
208  END DO
209 
210  ! ======= Update the basis set and print the final basis =======
211  IF (para_env_top%is_source()) THEN
212  opt_bas%powell_param%state = 8
213  CALL powell_optimize(opt_bas%powell_param%nvar, opt_bas%x_opt, opt_bas%powell_param)
214  END IF
215 
216  CALL para_env_top%bcast(opt_bas%x_opt)
217  CALL update_free_vars(opt_bas)
218  CALL update_derived_basis_sets(opt_bas, .true., opt_bas%output_basis_file, &
219  para_env_top)
220 
221  ! ====== get rid of the f_env again =====
222 
223  DO icalc = SIZE(opt_bas%comp_group(mp_id)%member_list), 1, -1
224  CALL f_env_get_from_id(f_env_id(icalc), f_env)
225  CALL destroy_force_env(f_env_id(icalc), stat)
226  END DO
227  DEALLOCATE (f_env_id); DEALLOCATE (tot_time)
228  CALL cp_fm_release(matrix_s_inv)
229  CALL mp_para_env_release(para_env)
230  CALL timestop(handle)
231 
232  END SUBROUTINE driver_optimization_para_low
233 
234 ! **************************************************************************************************
235 !> \brief compute all ingredients for powell optimizer. Rho_diff,
236 !> condition number, energy,... for all ttraining sets in
237 !> the computational group
238 !> \param opt_bas ...
239 !> \param f_env_id ...
240 !> \param matrix_S_inv ...
241 !> \param tot_time ...
242 !> \param para_env_top ...
243 !> \param para_env ...
244 !> \param iopt ...
245 ! **************************************************************************************************
246 
247  SUBROUTINE compute_residuum_vectors(opt_bas, f_env_id, matrix_S_inv, tot_time, &
248  para_env_top, para_env, iopt)
249  TYPE(basis_optimization_type) :: opt_bas
250  INTEGER, ALLOCATABLE, DIMENSION(:) :: f_env_id
251  TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: matrix_s_inv
252  REAL(kind=dp), DIMENSION(:) :: tot_time
253  TYPE(mp_para_env_type), POINTER :: para_env_top, para_env
254  INTEGER :: iopt
255 
256  CHARACTER(len=*), PARAMETER :: routinen = 'compute_residuum_vectors'
257 
258  CHARACTER(len=8) :: basis_type
259  INTEGER :: bas_id, handle, icalc, icomb, ispin, &
260  mp_id, my_id, nao, ncalc, nelectron, &
261  nmo, nspins, set_id
262  REAL(kind=dp) :: flexible_electron_count, maxocc, n_el_f
263  REAL(kind=dp), DIMENSION(:), POINTER :: cond_vec, energy, f_vec, my_time, &
264  start_time
265  REAL(kind=dp), DIMENSION(:, :), POINTER :: gdata
266  TYPE(cp_fm_struct_type), POINTER :: fm_struct
267  TYPE(cp_fm_type), POINTER :: mo_coeff
268  TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s_aux, matrix_s_aux_orb
269  TYPE(f_env_type), POINTER :: f_env
270  TYPE(force_env_type), POINTER :: force_env
271  TYPE(mo_set_type), ALLOCATABLE, DIMENSION(:) :: mos_aux
272  TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
273  TYPE(neighbor_list_set_p_type), DIMENSION(:), &
274  POINTER :: sab_aux, sab_aux_orb
275  TYPE(qs_environment_type), POINTER :: qs_env
276  TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
277  TYPE(qs_ks_env_type), POINTER :: ks_env
278 
279  CALL timeset(routinen, handle)
280 
281  basis_type = "AUX_OPT"
282  !
283  ncalc = opt_bas%ncombinations*opt_bas%ntraining_sets
284  ALLOCATE (gdata(ncalc, 4))
285  f_vec => gdata(:, 1)
286  my_time => gdata(:, 2)
287  cond_vec => gdata(:, 3)
288  energy => gdata(:, 4)
289  !
290  f_vec = 0.0_dp; cond_vec = 0.0_dp; my_time = 0.0_dp; energy = 0.0_dp
291  mp_id = opt_bas%opt_id
292  ALLOCATE (start_time(SIZE(opt_bas%comp_group(mp_id)%member_list)))
293  !
294  DO icalc = 1, SIZE(opt_bas%comp_group(mp_id)%member_list)
295  my_id = opt_bas%comp_group(mp_id)%member_list(icalc) + 1
296  ! setup timings
297  start_time(icalc) = m_walltime()
298 
299  NULLIFY (matrix_s_aux_orb, matrix_s_aux)
300  CALL get_set_and_basis_id(opt_bas%comp_group(mp_id)%member_list(icalc), opt_bas, set_id, bas_id)
301  CALL f_env_get_from_id(f_env_id(icalc), f_env)
302  force_env => f_env%force_env
303  CALL force_env_get(force_env, qs_env=qs_env)
304  CALL get_qs_env(qs_env, ks_env=ks_env)
305  CALL update_basis_set(opt_bas, bas_id, basis_type, qs_env)
306  NULLIFY (sab_aux, sab_aux_orb)
307  CALL optbas_build_neighborlist(qs_env, sab_aux, sab_aux_orb, basis_type)
308  CALL build_overlap_matrix(ks_env, matrix_s=matrix_s_aux, &
309  basis_type_a=basis_type, &
310  basis_type_b=basis_type, &
311  sab_nl=sab_aux)
312  CALL build_overlap_matrix(ks_env, matrix_s=matrix_s_aux_orb, &
313  basis_type_a=basis_type, &
314  basis_type_b="ORB", &
315  sab_nl=sab_aux_orb)
316  CALL release_neighbor_list_sets(sab_aux)
317  CALL release_neighbor_list_sets(sab_aux_orb)
318  CALL get_qs_env(qs_env, mos=mos, matrix_ks=matrix_ks)
319 
320  nspins = SIZE(mos)
321  ALLOCATE (mos_aux(nspins))
322  CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set)
323  CALL get_qs_kind_set(qs_kind_set, nsgf=nao, basis_type=basis_type)
324  DO ispin = 1, nspins
325  CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, maxocc=maxocc, nelectron=nelectron, &
326  n_el_f=n_el_f, nmo=nmo, flexible_electron_count=flexible_electron_count)
327  CALL cp_fm_struct_create(fm_struct, nrow_global=nao, ncol_global=nmo, &
328  context=mo_coeff%matrix_struct%context, &
329  para_env=mo_coeff%matrix_struct%para_env)
330  CALL allocate_mo_set(mos_aux(ispin), nao, nmo, nelectron, &
331  n_el_f, maxocc, flexible_electron_count)
332  CALL init_mo_set(mo_set=mos_aux(ispin), fm_struct=fm_struct, name="MO_AUX")
333  CALL cp_fm_struct_release(fm_struct)
334  END DO
335 
336  CALL fit_mo_coeffs(matrix_s_aux, matrix_s_aux_orb, mos, mos_aux)
337  CALL evaluate_optvals(mos, mos_aux, matrix_ks, matrix_s_aux_orb(1)%matrix, &
338  matrix_s_aux(1)%matrix, matrix_s_inv(icalc), &
339  f_vec(my_id), energy(my_id), cond_vec(my_id))
340 
341  DO ispin = 1, nspins
342  CALL deallocate_mo_set(mos_aux(ispin))
343  END DO
344  DEALLOCATE (mos_aux)
345  IF (ASSOCIATED(matrix_s_aux)) CALL dbcsr_deallocate_matrix_set(matrix_s_aux)
346  IF (ASSOCIATED(matrix_s_aux_orb)) CALL dbcsr_deallocate_matrix_set(matrix_s_aux_orb)
347 
348  my_time(my_id) = m_walltime() - start_time(icalc)
349  END DO
350 
351  DEALLOCATE (start_time)
352 
353  IF (.NOT. para_env%is_source()) THEN
354  f_vec = 0.0_dp; cond_vec = 0.0_dp; my_time = 0.0_dp; energy = 0.0_dp
355  END IF
356  ! collect date from all subgroup ionodes on the main ionode
357  CALL para_env_top%sum(gdata)
358 
359  opt_bas%powell_param%f = 0.0_dp
360  IF (para_env_top%is_source()) THEN
361  DO icalc = 1, SIZE(f_vec)
362  icomb = mod(icalc - 1, opt_bas%ncombinations)
363  opt_bas%powell_param%f = opt_bas%powell_param%f + &
364  (f_vec(icalc) + energy(icalc))*opt_bas%fval_weight(icomb)
365  IF (opt_bas%use_condition_number) &
366  opt_bas%powell_param%f = opt_bas%powell_param%f + &
367  log(cond_vec(icalc))*opt_bas%condition_weight(icomb)
368  END DO
369  ELSE
370  f_vec = 0.0_dp; cond_vec = 0.0_dp; my_time = 0.0_dp; energy = 0.0_dp
371  END IF
372  CALL para_env_top%bcast(opt_bas%powell_param%f)
373 
374  ! output info if required
375  CALL output_opt_info(f_vec, cond_vec, my_time, tot_time, opt_bas, iopt, para_env_top)
376  DEALLOCATE (gdata)
377 
378  CALL para_env_top%sync()
379 
380  CALL timestop(handle)
381 
382  END SUBROUTINE compute_residuum_vectors
383 
384 ! **************************************************************************************************
385 !> \brief create the force_envs for every input in the computational group
386 !> \param opt_bas ...
387 !> \param f_env_id ...
388 !> \param input_declaration ...
389 !> \param matrix_s_inv ...
390 !> \param para_env ...
391 !> \param mpi_comm_opt ...
392 ! **************************************************************************************************
393 
394  SUBROUTINE init_training_force_envs(opt_bas, f_env_id, input_declaration, matrix_s_inv, para_env, mpi_comm_opt)
395 
396  TYPE(basis_optimization_type) :: opt_bas
397  INTEGER, ALLOCATABLE, DIMENSION(:) :: f_env_id
398  TYPE(section_type), POINTER :: input_declaration
399  TYPE(cp_fm_type), DIMENSION(:), INTENT(OUT) :: matrix_s_inv
400  TYPE(mp_para_env_type), POINTER :: para_env
401  TYPE(mp_comm_type) :: mpi_comm_opt
402 
403  CHARACTER(len=*), PARAMETER :: routinen = 'init_training_force_envs'
404 
405  CHARACTER(len=default_path_length) :: main_dir
406  INTEGER :: bas_id, handle, icalc, ierr, mp_id, &
407  set_id, stat
408  TYPE(cp_blacs_env_type), POINTER :: blacs_env
409  TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
410  TYPE(f_env_type), POINTER :: f_env
411  TYPE(force_env_type), POINTER :: force_env
412  TYPE(neighbor_list_set_p_type), DIMENSION(:), &
413  POINTER :: sab_orb
414  TYPE(qs_environment_type), POINTER :: qs_env
415  TYPE(qs_ks_env_type), POINTER :: ks_env
416  TYPE(section_vals_type), POINTER :: input_file
417 
418  CALL timeset(routinen, handle)
419 
420  NULLIFY (matrix_s, blacs_env, ks_env)
421 
422  mp_id = opt_bas%opt_id
423  CALL m_getcwd(main_dir)
424 
425  ! ======= Create f_env for all calculations in MPI group =======
426  DO icalc = 1, SIZE(opt_bas%comp_group(mp_id)%member_list)
427  NULLIFY (input_file)
428  ! parse the input of the training sets
429  CALL get_set_and_basis_id(opt_bas%comp_group(mp_id)%member_list(icalc), opt_bas, set_id, bas_id)
430  CALL m_chdir(trim(opt_bas%training_dir(set_id)), ierr)
431  IF (ierr /= 0) THEN
432  CALL cp_abort(__location__, &
433  "Could not change to directory <"//trim(opt_bas%training_dir(set_id))//">")
434  END IF
435  input_file => read_input(input_declaration, &
436  opt_bas%training_input(set_id), &
437  initial_variables=empty_initial_variables, &
438  para_env=para_env)
439 
440  CALL modify_input_settings(opt_bas, bas_id, input_file)
441  CALL create_force_env(f_env_id(icalc), &
442  input_declaration=input_declaration, &
443  input_path=opt_bas%training_input(set_id), &
444  input=input_file, &
445  output_path="scrap_information", &
446  mpi_comm=mpi_comm_opt, &
447  ierr=stat)
448 
449  ! some weirdness with the default stacks defaults have to be addded to get the
450  ! correct default program name this causes trouble with the timer stack if kept
451  CALL f_env_add_defaults(f_env_id(icalc), f_env)
452  force_env => f_env%force_env
453  CALL force_env_get(force_env, qs_env=qs_env)
454  CALL allocate_mo_sets(qs_env)
455  CALL f_env_rm_defaults(f_env, stat)
456  CALL get_qs_env(qs_env, ks_env=ks_env)
457  CALL build_qs_neighbor_lists(qs_env, para_env, molecular=.false., &
458  force_env_section=qs_env%input)
459  CALL get_ks_env(ks_env, &
460  matrix_s=matrix_s, &
461  sab_orb=sab_orb)
462  CALL build_overlap_matrix(ks_env, matrix_s=matrix_s, &
463  matrix_name="OVERLAP", &
464  basis_type_a="ORB", &
465  basis_type_b="ORB", &
466  sab_nl=sab_orb)
467  CALL set_ks_env(ks_env, matrix_s=matrix_s)
468  CALL get_qs_env(qs_env, matrix_s=matrix_s, blacs_env=blacs_env)
469  CALL calculate_overlap_inverse(matrix_s(1)%matrix, matrix_s_inv(icalc), &
470  para_env, blacs_env)
471  CALL calculate_ks_matrix(qs_env)
472 
473  CALL section_vals_release(input_file)
474 
475  CALL qs_env_part_release(qs_env)
476 
477  CALL m_chdir(trim(adjustl(main_dir)), ierr)
478  END DO
479 
480  CALL timestop(handle)
481 
482  END SUBROUTINE init_training_force_envs
483 
484 ! **************************************************************************************************
485 !> \brief variable update from the powell vector for all sets
486 !> \param opt_bas ...
487 !> \author Florian Schiffmann
488 ! **************************************************************************************************
489 
490  SUBROUTINE update_free_vars(opt_bas)
491  TYPE(basis_optimization_type) :: opt_bas
492 
493  CHARACTER(len=*), PARAMETER :: routinen = 'update_free_vars'
494 
495  INTEGER :: handle, ikind, iset, ix
496 
497  CALL timeset(routinen, handle)
498  ix = 0
499  DO ikind = 1, opt_bas%nkind
500  DO iset = 1, opt_bas%kind_basis(ikind)%flex_basis(0)%nsets
501  CALL update_subset_freevars(opt_bas%kind_basis(ikind)%flex_basis(0)%subset(iset), ix, opt_bas%x_opt)
502  END DO
503  END DO
504  CALL timestop(handle)
505 
506  END SUBROUTINE update_free_vars
507 
508 ! **************************************************************************************************
509 !> \brief low level update for the basis sets. Exponents are transformed according to constraint
510 !> \param subset ...
511 !> \param ix ...
512 !> \param x ...
513 !> \author Florian Schiffmann
514 ! **************************************************************************************************
515 
516  SUBROUTINE update_subset_freevars(subset, ix, x)
517  TYPE(subset_type) :: subset
518  INTEGER :: ix
519  REAL(kind=dp), DIMENSION(:) :: x
520 
521  CHARACTER(len=*), PARAMETER :: routinen = 'update_subset_freevars'
522 
523  INTEGER :: handle, icon1, icon2, icont, iexp, il, &
524  istart
525  REAL(kind=dp) :: fermi_f, gs_scale
526 
527  CALL timeset(routinen, handle)
528  DO iexp = 1, subset%nexp
529  IF (subset%opt_exps(iexp)) THEN
530  ix = ix + 1
531  subset%exps(iexp) = abs(x(ix))
532  IF (subset%exp_has_const(iexp)) THEN
533  !use a fermi function to keep exponents in a given range around their initial value
534  fermi_f = 1.0_dp/(exp((x(ix) - 1.0_dp)/0.5_dp) + 1.0_dp)
535  subset%exps(iexp) = (2.0_dp*fermi_f - 1.0_dp)*subset%exp_const(iexp)%var_fac*subset%exp_const(iexp)%init + &
536  subset%exp_const(iexp)%init
537  ELSE
538 
539  END IF
540  END IF
541  DO icont = 1, subset%ncon_tot
542  IF (subset%opt_coeff(iexp, icont)) THEN
543  ix = ix + 1
544  subset%coeff(iexp, icont) = x(ix)
545  END IF
546  END DO
547  END DO
548 
549  ! orthonormalize contraction coefficients using gram schmidt
550  istart = 1
551  DO il = 1, subset%nl
552  DO icon1 = istart, istart + subset%l(il) - 2
553  DO icon2 = icon1 + 1, istart + subset%l(il) - 1
554  gs_scale = dot_product(subset%coeff(:, icon2), subset%coeff(:, icon1))/ &
555  dot_product(subset%coeff(:, icon1), subset%coeff(:, icon1))
556  subset%coeff(:, icon2) = subset%coeff(:, icon2) - gs_scale*subset%coeff(:, icon1)
557  END DO
558  END DO
559  istart = istart + subset%l(il)
560  END DO
561 
562  DO icon1 = 1, subset%ncon_tot
563  subset%coeff(:, icon1) = subset%coeff(:, icon1)/ &
564  sqrt(dot_product(subset%coeff(:, icon1), subset%coeff(:, icon1)))
565  END DO
566  CALL timestop(handle)
567 
568  END SUBROUTINE update_subset_freevars
569 
570 ! **************************************************************************************************
571 !> \brief variable initialization for the powell vector for all sets
572 !> \param opt_bas ...
573 !> \author Florian Schiffmann
574 ! **************************************************************************************************
575 
576  SUBROUTINE init_free_vars(opt_bas)
577  TYPE(basis_optimization_type) :: opt_bas
578 
579  CHARACTER(len=*), PARAMETER :: routinen = 'init_free_vars'
580 
581  INTEGER :: handle, ikind, iset, ix
582 
583  CALL timeset(routinen, handle)
584  ix = 0
585  DO ikind = 1, opt_bas%nkind
586  DO iset = 1, opt_bas%kind_basis(ikind)%flex_basis(0)%nsets
587  CALL init_subset_freevars(opt_bas%kind_basis(ikind)%flex_basis(0)%subset(iset), ix, opt_bas%x_opt)
588  END DO
589  END DO
590  CALL timestop(handle)
591 
592  END SUBROUTINE init_free_vars
593 
594 ! **************************************************************************************************
595 !> \brief variable initialization for the powell vector from low level informations
596 !> constraint exponents will be mapped on a fermi function
597 !> \param subset ...
598 !> \param ix ...
599 !> \param x ...
600 !> \author Florian Schiffmann
601 ! **************************************************************************************************
602 
603  SUBROUTINE init_subset_freevars(subset, ix, x)
604  TYPE(subset_type) :: subset
605  INTEGER :: ix
606  REAL(kind=dp), DIMENSION(:) :: x
607 
608  CHARACTER(len=*), PARAMETER :: routinen = 'init_subset_freevars'
609 
610  INTEGER :: handle, icont, iexp
611  REAL(kind=dp) :: fract
612 
613  CALL timeset(routinen, handle)
614 
615  DO iexp = 1, subset%nexp
616  IF (subset%opt_exps(iexp)) THEN
617  ix = ix + 1
618  x(ix) = subset%exps(iexp)
619  IF (subset%exp_has_const(iexp)) THEN
620  IF (subset%exp_const(iexp)%const_type == 0) THEN
621  fract = 1.0_dp + (subset%exps(iexp) - subset%exp_const(iexp)%init)/ &
622  (subset%exp_const(iexp)%init*subset%exp_const(iexp)%var_fac)
623  x(ix) = 0.5_dp*log((2.0_dp/fract - 1.0_dp)) + 1.0_dp
624  END IF
625  IF (subset%exp_const(iexp)%const_type == 1) THEN
626  x(ix) = 1.0_dp
627  END IF
628  END IF
629  END IF
630  DO icont = 1, subset%ncon_tot
631  IF (subset%opt_coeff(iexp, icont)) THEN
632  ix = ix + 1
633  x(ix) = subset%coeff(iexp, icont)
634  END IF
635  END DO
636  END DO
637  CALL timestop(handle)
638 
639  END SUBROUTINE init_subset_freevars
640 
641 ! **************************************************************************************************
642 !> \brief commuticates all info to the master and assembles the output
643 !> \param f_vec ...
644 !> \param cond_vec ...
645 !> \param my_time ...
646 !> \param tot_time ...
647 !> \param opt_bas ...
648 !> \param iopt ...
649 !> \param para_env_top ...
650 !> \author Florian Schiffmann
651 ! **************************************************************************************************
652 
653  SUBROUTINE output_opt_info(f_vec, cond_vec, my_time, tot_time, opt_bas, iopt, para_env_top)
654  REAL(kind=dp), DIMENSION(:) :: f_vec, cond_vec, my_time, tot_time
655  TYPE(basis_optimization_type) :: opt_bas
656  INTEGER :: iopt
657  TYPE(mp_para_env_type), POINTER :: para_env_top
658 
659  CHARACTER(len=*), PARAMETER :: routinen = 'output_opt_info'
660 
661  INTEGER :: handle, ibasis, icalc, iset, unit_nr
662  TYPE(cp_logger_type), POINTER :: logger
663 
664  CALL timeset(routinen, handle)
665  logger => cp_get_default_logger()
666 
667  tot_time = tot_time + my_time
668 
669  unit_nr = -1
670  IF (para_env_top%is_source() .AND. (mod(iopt, opt_bas%write_frequency) == 0 .OR. iopt == opt_bas%powell_param%maxfun)) &
671  unit_nr = cp_logger_get_default_unit_nr(logger)
672 
673  IF (unit_nr .GT. 0) THEN
674  WRITE (unit_nr, '(1X,A,I8)') "BASOPT| Information at iteration number:", iopt
675  WRITE (unit_nr, '(1X,A)') "BASOPT| Training set | Combination | Rho difference | Condition num. | Time"
676  WRITE (unit_nr, '(1X,A)') "BASOPT| -----------------------------------------------------------------------"
677  icalc = 0
678  DO iset = 1, opt_bas%ntraining_sets
679  DO ibasis = 1, opt_bas%ncombinations
680  icalc = icalc + 1
681  WRITE (unit_nr, '(1X,A,2(5X,I3,5X,A),2(1X,E14.8,1X,A),1X,F8.1)') &
682  'BASOPT| ', iset, "|", ibasis, "|", f_vec(icalc), "|", cond_vec(icalc), "|", tot_time(icalc)
683  END DO
684  END DO
685  WRITE (unit_nr, '(1X,A)') "BASOPT| -----------------------------------------------------------------------"
686  WRITE (unit_nr, '(1X,A,E14.8)') "BASOPT| Total residuum value: ", opt_bas%powell_param%f
687  WRITE (unit_nr, '(A)') ""
688  END IF
689  CALL timestop(handle)
690  END SUBROUTINE output_opt_info
691 
692 END MODULE optimize_basis
693 
void update_basis_set(const int nkinds, const grid_basis_set **const basis_sets, grid_context *data)
methods related to the blacs parallel environment
Definition: cp_blacs_env.F:15
DBCSR operations in CP2K.
represent the structure of a full matrix
Definition: cp_fm_struct.F:14
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
Definition: cp_fm_struct.F:125
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
Definition: cp_fm_struct.F:320
represent a full matrix distributed on many processors
Definition: cp_fm_types.F:15
various routines to log and control the output. The idea is that decisions about where to log should ...
recursive integer function, public cp_logger_get_default_unit_nr(logger, local, skip_not_ionode)
asks the default unit number of the given logger. try to use cp_logger_get_unit_nr
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
interface to use cp2k as library
Definition: f77_interface.F:20
recursive subroutine, public destroy_force_env(env_id, ierr, q_finalize)
deallocates the force_env with the given id
subroutine, public f_env_get_from_id(f_env_id, f_env)
...
subroutine, public f_env_add_defaults(f_env_id, f_env, handle)
adds the default environments of the f_env to the stack of the defaults, and returns a new error and ...
recursive subroutine, public create_force_env(new_env_id, input_declaration, input_path, output_path, mpi_comm, output_unit, owns_out_unit, input, ierr, work_dir, initial_variables)
creates a new force environment using the given input, and writing the output to the given output uni...
subroutine, public f_env_rm_defaults(f_env, ierr, handle)
removes the default environments of the f_env to the stack of the defaults, and sets ierr accordingly...
Interface for the force calculations.
recursive subroutine, public force_env_get(force_env, in_use, fist_env, qs_env, meta_env, fp_env, subsys, para_env, potential_energy, additional_potential, kinetic_energy, harmonic_shell, kinetic_shell, cell, sub_force_env, qmmm_env, qmmmx_env, eip_env, pwdft_env, globenv, input, force_env_section, method_name_id, root_section, mixed_env, nnp_env, embed_env)
returns various attributes about the force environment
parse cp2k input files
type(section_vals_type) function, pointer, public read_input(input_declaration, file_path, initial_variables, para_env)
reads the cp2k input from the given filepath and returns a section_vals containing the input
objects that represent the structure of input sections and the data contained in an input section
recursive subroutine, public section_vals_release(section_vals)
releases the given object
Defines the basic variable types.
Definition: kinds.F:23
integer, parameter, public dp
Definition: kinds.F:34
integer, parameter, public default_path_length
Definition: kinds.F:58
Machine interface based on Fortran 2003 and POSIX.
Definition: machine.F:17
subroutine, public m_getcwd(curdir)
...
Definition: machine.F:507
subroutine, public m_chdir(dir, ierror)
...
Definition: machine.F:536
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition: machine.F:123
Interface to the message passing library MPI.
subroutine, public mp_para_env_release(para_env)
releases the para object (to be called when you don't want anymore the shared copy of this object)
subroutine, public modify_input_settings(basis_optimization, bas_id, input_file)
change settings in the training input files to initialize all needed structures and adjust settings t...
subroutine, public calculate_ks_matrix(qs_env)
...
subroutine, public calculate_overlap_inverse(matrix_s, matrix_s_inv, para_env, context)
...
subroutine, public allocate_mo_sets(qs_env)
...
subroutine, public optbas_build_neighborlist(qs_env, sab_aux, sab_aux_orb, basis_type)
rebuilds neighborlist for absis sets
subroutine, public fit_mo_coeffs(saux, sauxorb, mos, mosaux)
...
subroutine, public evaluate_optvals(mos, mos_aux_fit, matrix_ks, Q, Snew, S_inv_orb, fval, energy, S_cond_number)
...
subroutine, public deallocate_basis_optimization_type(opt_bas)
Deallocate everything which was allocated before. Note not all arrays are used depending on the type ...
subroutine, public get_set_and_basis_id(calc_id, opt_bas, set_id, bas_id)
returns a mapping from the calculation id to the trainings set id and basis combination id
subroutine, public optimize_basis_init_read_input(opt_bas, root_section, para_env)
initialize all parts of the optimization type and read input settings
subroutine, public update_derived_basis_sets(opt_bas, write_it, output_file, para_env)
Regenerate the basis sets from reference 0 after an update from the optimizer to reference was perfor...
subroutine, public run_optimize_basis(input_declaration, root_section, para_env)
main entry point for methods aimed at optimizing basis sets
Definition: powell.F:9
subroutine, public powell_optimize(n, x, optstate)
...
Definition: powell.F:55
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_RI_aux_kp, matrix_s, matrix_s_RI_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, ecoul_1c, rho0_s_rs, rho0_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, WannierCentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, rhs)
Get the QUICKSTEP environment.
subroutine, public qs_env_part_release(qs_env)
releases part of the given qs_env in order to save memory
Define the quickstep kind type and their sub types.
Definition: qs_kind_types.F:23
subroutine, public get_qs_kind_set(qs_kind_set, all_potential_present, tnadd_potential_present, gth_potential_present, sgp_potential_present, paw_atom_present, dft_plus_u_atom_present, maxcgf, maxsgf, maxco, maxco_proj, maxgtops, maxlgto, maxlprj, maxnset, maxsgf_set, ncgf, npgf, nset, nsgf, nshell, maxpol, maxlppl, maxlppnl, maxppnl, nelectron, maxder, max_ngrid_rad, max_sph_harm, maxg_iso_not0, lmax_rho0, basis_rcut, basis_type, total_zeff_corr)
Get attributes of an atomic kind set.
subroutine, public get_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_RI_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_RI_aux_kp, matrix_ks_im_kp, rho, rho_xc, vppl, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, task_list, task_list_soft, kpoints, do_kpoints, atomic_kind_set, qs_kind_set, cell, cell_ref, use_ref_cell, particle_set, energy, force, local_particles, local_molecules, molecule_kind_set, molecule_set, subsys, cp_subsys, virial, results, atprop, nkind, natom, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env, nelectron_total, nelectron_spin)
...
Definition: qs_ks_types.F:330
subroutine, public set_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_RI_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_RI_aux_kp, matrix_ks_im_kp, vppl, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, kpoints, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, task_list, task_list_soft, subsys, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env)
...
Definition: qs_ks_types.F:567
Definition and initialisation of the mo data type.
Definition: qs_mo_types.F:22
subroutine, public allocate_mo_set(mo_set, nao, nmo, nelectron, n_el_f, maxocc, flexible_electron_count)
Allocates a mo set and partially initializes it (nao,nmo,nelectron, and flexible_electron_count are v...
Definition: qs_mo_types.F:206
subroutine, public deallocate_mo_set(mo_set)
Deallocate a wavefunction data structure.
Definition: qs_mo_types.F:352
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kTS, mu, flexible_electron_count)
Get the components of a MO set data structure.
Definition: qs_mo_types.F:397
subroutine, public init_mo_set(mo_set, fm_pool, fm_ref, fm_struct, name)
initializes an allocated mo_set. eigenvalues, mo_coeff, occupation_numbers are valid only after this ...
Definition: qs_mo_types.F:245
Define the neighbor list data types and the corresponding functionality.
subroutine, public release_neighbor_list_sets(nlists)
releases an array of neighbor_list_sets
Generate the atomic neighbor lists.
subroutine, public build_qs_neighbor_lists(qs_env, para_env, molecular, force_env_section)
Build all the required neighbor lists for Quickstep.
Calculation of overlap matrix, its derivatives and forces.
Definition: qs_overlap.F:19
subroutine, public build_overlap_matrix(ks_env, matrix_s, matrixkp_s, matrix_name, nderivative, basis_type_a, basis_type_b, sab_nl, calculate_forces, matrix_p, matrixkp_p)
Calculation of the overlap matrix over Cartesian Gaussian functions.
Definition: qs_overlap.F:120