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 performs geometry optimization
10 : !> \par History
11 : !> none
12 : ! **************************************************************************************************
13 : MODULE geo_opt
14 :
15 : USE bfgs_optimizer, ONLY: geoopt_bfgs
16 : USE cg_optimizer, ONLY: geoopt_cg
17 : USE cp_lbfgs_geo, ONLY: geoopt_lbfgs
18 : USE cp_log_handling, ONLY: cp_get_default_logger,&
19 : cp_logger_type
20 : USE cp_output_handling, ONLY: cp_add_iter_level,&
21 : cp_iterate,&
22 : cp_rm_iter_level
23 : USE force_env_types, ONLY: force_env_type
24 : USE global_types, ONLY: global_environment_type
25 : USE gopt_f_methods, ONLY: gopt_f_create_x0
26 : USE gopt_f_types, ONLY: gopt_f_create,&
27 : gopt_f_release,&
28 : gopt_f_type
29 : USE gopt_param_types, ONLY: gopt_param_read,&
30 : gopt_param_type
31 : USE input_constants, ONLY: default_bfgs_method_id,&
32 : default_cg_method_id,&
33 : default_lbfgs_method_id
34 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
35 : section_vals_type,&
36 : section_vals_val_get,&
37 : section_vals_val_set
38 : USE kinds, ONLY: dp
39 : #include "../base/base_uses.f90"
40 :
41 : IMPLICIT NONE
42 : PRIVATE
43 :
44 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'geo_opt'
45 :
46 : PUBLIC :: cp_geo_opt, cp_rot_opt
47 :
48 : CONTAINS
49 :
50 : ! **************************************************************************************************
51 : !> \brief Main driver to perform geometry optimization
52 : !> \param force_env ...
53 : !> \param globenv ...
54 : !> \param eval_opt_geo ...
55 : !> \param rm_restart_info ...
56 : ! **************************************************************************************************
57 5262 : SUBROUTINE cp_geo_opt(force_env, globenv, eval_opt_geo, rm_restart_info)
58 :
59 : TYPE(force_env_type), POINTER :: force_env
60 : TYPE(global_environment_type), POINTER :: globenv
61 : LOGICAL, INTENT(IN), OPTIONAL :: eval_opt_geo, rm_restart_info
62 :
63 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_geo_opt'
64 :
65 : INTEGER :: handle, step_start_val
66 : LOGICAL :: my_rm_restart_info
67 1754 : REAL(KIND=dp), DIMENSION(:), POINTER :: x0
68 : TYPE(cp_logger_type), POINTER :: logger
69 : TYPE(gopt_f_type), POINTER :: gopt_env
70 : TYPE(gopt_param_type), POINTER :: gopt_param
71 : TYPE(section_vals_type), POINTER :: geo_section, root_section
72 :
73 1754 : CALL timeset(routineN, handle)
74 1754 : logger => cp_get_default_logger()
75 1754 : CPASSERT(ASSOCIATED(force_env))
76 1754 : CPASSERT(ASSOCIATED(globenv))
77 1754 : NULLIFY (gopt_param, gopt_env, x0)
78 1754 : root_section => force_env%root_section
79 1754 : geo_section => section_vals_get_subs_vals(root_section, "MOTION%GEO_OPT")
80 :
81 1754 : ALLOCATE (gopt_param)
82 1754 : CALL gopt_param_read(gopt_param, geo_section)
83 : CALL gopt_f_create(gopt_env, gopt_param, force_env=force_env, globenv=globenv, &
84 1754 : geo_opt_section=geo_section, eval_opt_geo=eval_opt_geo)
85 1754 : CALL gopt_f_create_x0(gopt_env, x0)
86 :
87 1754 : CALL section_vals_val_get(geo_section, "STEP_START_VAL", i_val=step_start_val)
88 1754 : CALL cp_add_iter_level(logger%iter_info, "GEO_OPT")
89 1754 : CALL cp_iterate(logger%iter_info, iter_nr=step_start_val)
90 : CALL cp_geo_opt_low(force_env, globenv, gopt_param, gopt_env, &
91 1754 : geo_section, x0)
92 1754 : CALL cp_rm_iter_level(logger%iter_info, "GEO_OPT")
93 :
94 : ! Reset counter for next iteration, unless rm_restart_info==.FALSE.
95 1754 : my_rm_restart_info = .TRUE.
96 1754 : IF (PRESENT(rm_restart_info)) my_rm_restart_info = rm_restart_info
97 48 : IF (my_rm_restart_info) &
98 1706 : CALL section_vals_val_set(geo_section, "STEP_START_VAL", i_val=0)
99 :
100 1754 : DEALLOCATE (x0)
101 1754 : CALL gopt_f_release(gopt_env)
102 1754 : DEALLOCATE (gopt_param)
103 1754 : CALL timestop(handle)
104 :
105 1754 : END SUBROUTINE cp_geo_opt
106 :
107 : ! **************************************************************************************************
108 : !> \brief Main driver to perform rotation optimization for Dimer
109 : !> \param gopt_env ...
110 : !> \param x0 ...
111 : !> \param gopt_param ...
112 : !> \param geo_section ...
113 : ! **************************************************************************************************
114 264 : SUBROUTINE cp_rot_opt(gopt_env, x0, gopt_param, geo_section)
115 : TYPE(gopt_f_type), POINTER :: gopt_env
116 : REAL(KIND=dp), DIMENSION(:), POINTER :: x0
117 : TYPE(gopt_param_type), POINTER :: gopt_param
118 : TYPE(section_vals_type), POINTER :: geo_section
119 :
120 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_rot_opt'
121 :
122 : INTEGER :: handle, step_start_val
123 : TYPE(cp_logger_type), POINTER :: logger
124 :
125 132 : CALL timeset(routineN, handle)
126 132 : logger => cp_get_default_logger()
127 132 : CPASSERT(ASSOCIATED(gopt_env))
128 132 : CPASSERT(ASSOCIATED(gopt_env%force_env))
129 132 : CPASSERT(ASSOCIATED(gopt_env%globenv))
130 :
131 132 : CALL section_vals_val_get(geo_section, "STEP_START_VAL", i_val=step_start_val)
132 132 : CALL cp_add_iter_level(logger%iter_info, "ROT_OPT")
133 132 : CALL cp_iterate(logger%iter_info, iter_nr=step_start_val)
134 : CALL cp_geo_opt_low(gopt_env%force_env, gopt_env%globenv, gopt_param, gopt_env, &
135 132 : geo_section, x0)
136 132 : CALL cp_rm_iter_level(logger%iter_info, "ROT_OPT")
137 :
138 : ! Reset counter for next iteration
139 132 : CALL section_vals_val_set(geo_section, "STEP_START_VAL", i_val=0)
140 132 : CALL timestop(handle)
141 :
142 132 : END SUBROUTINE cp_rot_opt
143 :
144 : ! **************************************************************************************************
145 : !> \brief call to low level geometry optimizers
146 : !> \param force_env ...
147 : !> \param globenv ...
148 : !> \param gopt_param ...
149 : !> \param gopt_env ...
150 : !> \param geo_section ...
151 : !> \param x0 ...
152 : ! **************************************************************************************************
153 1886 : SUBROUTINE cp_geo_opt_low(force_env, globenv, gopt_param, gopt_env, &
154 : geo_section, x0)
155 : TYPE(force_env_type), POINTER :: force_env
156 : TYPE(global_environment_type), POINTER :: globenv
157 : TYPE(gopt_param_type), POINTER :: gopt_param
158 : TYPE(gopt_f_type), POINTER :: gopt_env
159 : TYPE(section_vals_type), POINTER :: geo_section
160 : REAL(KIND=dp), DIMENSION(:), POINTER :: x0
161 :
162 1886 : CPASSERT(ASSOCIATED(force_env))
163 1886 : CPASSERT(ASSOCIATED(globenv))
164 1886 : CPASSERT(ASSOCIATED(gopt_param))
165 1886 : CPASSERT(ASSOCIATED(gopt_env))
166 1886 : CPASSERT(ASSOCIATED(x0))
167 1886 : CPASSERT(ASSOCIATED(geo_section))
168 :
169 3014 : SELECT CASE (gopt_param%method_id)
170 : CASE (default_bfgs_method_id)
171 : CALL geoopt_bfgs(force_env, gopt_param, globenv, &
172 1128 : geo_section, gopt_env, x0)
173 : CASE (default_lbfgs_method_id)
174 : CALL geoopt_lbfgs(force_env, gopt_param, globenv, &
175 346 : geo_section, gopt_env, x0)
176 : CASE (default_cg_method_id)
177 : CALL geoopt_cg(force_env, gopt_param, globenv, &
178 412 : geo_section, gopt_env, x0)
179 : CASE DEFAULT
180 1886 : CPABORT("")
181 : END SELECT
182 :
183 1886 : END SUBROUTINE cp_geo_opt_low
184 :
185 : END MODULE geo_opt
|