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 :
8 : ! **************************************************************************************************
9 : !> \brief contains a functional that calculates the energy and its derivatives
10 : !> for the geometry optimizer
11 : !> \par History
12 : !> 01.2008 - Luca Bellucci and Teodoro Laino - Generalizing for Dimer Method.
13 : !> 03.2008 - Teodoro Laino [tlaino] - University of Zurich - Cell Optimization
14 : ! **************************************************************************************************
15 : MODULE gopt_f_types
16 : USE cell_opt_types, ONLY: cell_opt_env_create,&
17 : cell_opt_env_release,&
18 : cell_opt_env_type
19 : USE cp_subsys_types, ONLY: cp_subsys_get,&
20 : cp_subsys_type
21 : USE dimer_types, ONLY: dimer_env_create,&
22 : dimer_env_release,&
23 : dimer_env_retain,&
24 : dimer_env_type
25 : USE force_env_types, ONLY: force_env_get,&
26 : force_env_release,&
27 : force_env_retain,&
28 : force_env_type
29 : USE global_types, ONLY: global_environment_type
30 : USE gopt_param_types, ONLY: gopt_param_read,&
31 : gopt_param_type
32 : USE input_constants, ONLY: default_cell_method_id,&
33 : default_dimer_method_id,&
34 : default_minimization_method_id,&
35 : default_shellcore_method_id,&
36 : default_ts_method_id
37 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
38 : section_vals_type,&
39 : section_vals_val_get
40 : USE kinds, ONLY: default_string_length,&
41 : dp
42 : USE particle_list_types, ONLY: particle_list_type
43 : USE space_groups_types, ONLY: release_spgr_type,&
44 : spgr_type
45 : #include "../base/base_uses.f90"
46 :
47 : IMPLICIT NONE
48 : PRIVATE
49 :
50 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
51 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gopt_f_types'
52 :
53 : PUBLIC :: gopt_f_type, gopt_f_create, gopt_f_retain, gopt_f_release
54 :
55 : ! **************************************************************************************************
56 : !> \brief calculates the potential energy of a system, and its derivatives
57 : !> \par History
58 : !> none
59 : ! **************************************************************************************************
60 : TYPE gopt_f_type
61 : INTEGER :: ref_count = 0
62 : INTEGER :: type_id = default_cell_method_id
63 : INTEGER :: ts_method_id = 0
64 : INTEGER :: shellcore_method_id = 0
65 : LOGICAL :: dimer_rotation = .FALSE., do_line_search = .FALSE., eval_opt_geo = .FALSE.
66 : CHARACTER(LEN=default_string_length) :: label = "", tag = ""
67 : TYPE(force_env_type), POINTER :: force_env => NULL()
68 : TYPE(global_environment_type), POINTER :: globenv => NULL()
69 : ! Motion section must be references only for IO of the MOTION%PRINT..
70 : TYPE(section_vals_type), POINTER :: motion_section => NULL(), geo_section => NULL()
71 : TYPE(dimer_env_type), POINTER :: dimer_env => NULL()
72 : TYPE(gopt_f_type), POINTER :: gopt_dimer_env => NULL()
73 : TYPE(gopt_param_type), POINTER :: gopt_dimer_param => NULL()
74 : TYPE(cell_opt_env_type), POINTER :: cell_env => NULL()
75 : TYPE(spgr_type), POINTER :: spgr => NULL()
76 : PROCEDURE(gopt_f_dimer_rotation_optimizer), POINTER, NOPASS :: dimer_rotation_optimizer => NULL()
77 : REAL(KIND=dp), DIMENSION(3, 3) :: h_ref = 0.0_dp
78 : LOGICAL :: require_consistent_energy_force = .FALSE.
79 : END TYPE gopt_f_type
80 :
81 : ABSTRACT INTERFACE
82 : ! **************************************************************************************************
83 : !> \brief ...
84 : !> \param gopt_env ...
85 : !> \param x0 ...
86 : !> \param gopt_param ...
87 : !> \param geo_section ...
88 : ! **************************************************************************************************
89 : SUBROUTINE gopt_f_dimer_rotation_optimizer(gopt_env, x0, gopt_param, geo_section)
90 : IMPORT :: dp, gopt_f_type, gopt_param_type, section_vals_type
91 : TYPE(gopt_f_type), POINTER :: gopt_env
92 : REAL(KIND=dp), DIMENSION(:), POINTER :: x0
93 : TYPE(gopt_param_type), POINTER :: gopt_param
94 : TYPE(section_vals_type), POINTER :: geo_section
95 : END SUBROUTINE gopt_f_dimer_rotation_optimizer
96 : END INTERFACE
97 :
98 : CONTAINS
99 :
100 : ! **************************************************************************************************
101 : !> \brief ...
102 : !> \param gopt_env the geometry optimization environment to be created
103 : !> force_env:
104 : !> \param gopt_param ...
105 : !> \param force_env ...
106 : !> \param globenv ...
107 : !> \param geo_opt_section ...
108 : !> \param eval_opt_geo ...
109 : !> \par History
110 : !> none
111 : ! **************************************************************************************************
112 1157 : RECURSIVE SUBROUTINE gopt_f_create(gopt_env, gopt_param, force_env, globenv, geo_opt_section, &
113 : eval_opt_geo)
114 :
115 : TYPE(gopt_f_type), POINTER :: gopt_env
116 : TYPE(gopt_param_type), POINTER :: gopt_param
117 : TYPE(force_env_type), POINTER :: force_env
118 : TYPE(global_environment_type), POINTER :: globenv
119 : TYPE(section_vals_type), POINTER :: geo_opt_section
120 : LOGICAL, INTENT(IN), OPTIONAL :: eval_opt_geo
121 :
122 : INTEGER :: nshell
123 : TYPE(cp_subsys_type), POINTER :: subsys
124 : TYPE(particle_list_type), POINTER :: particles, shell_particles
125 : TYPE(section_vals_type), POINTER :: dimer_section, rot_opt_section
126 :
127 1157 : CPASSERT(.NOT. ASSOCIATED(gopt_env))
128 15041 : ALLOCATE (gopt_env)
129 1157 : nshell = 0
130 :
131 1157 : NULLIFY (gopt_env%dimer_env, gopt_env%gopt_dimer_env, gopt_env%gopt_dimer_param, gopt_env%cell_env, gopt_env%spgr)
132 1157 : gopt_env%ref_count = 1
133 1157 : gopt_env%dimer_rotation = .FALSE.
134 1157 : gopt_env%do_line_search = .FALSE.
135 5785 : ALLOCATE (gopt_env%spgr)
136 : ! This routine reads the space-group flags once. The optimizer
137 : ! drivers consume spgr%* only.
138 : CALL section_vals_val_get(geo_opt_section, "KEEP_SPACE_GROUP", &
139 1157 : l_val=gopt_env%spgr%keep_space_group)
140 : CALL section_vals_val_get(geo_opt_section, "SHOW_SPACE_GROUP", &
141 1157 : l_val=gopt_env%spgr%show_space_group)
142 1157 : CALL force_env_retain(force_env)
143 1157 : gopt_env%force_env => force_env
144 1157 : gopt_env%motion_section => section_vals_get_subs_vals(force_env%root_section, "MOTION")
145 1157 : gopt_env%geo_section => geo_opt_section
146 1157 : gopt_env%globenv => globenv
147 1157 : gopt_env%eval_opt_geo = .TRUE.
148 1157 : IF (PRESENT(eval_opt_geo)) gopt_env%eval_opt_geo = eval_opt_geo
149 1157 : gopt_env%require_consistent_energy_force = .TRUE.
150 :
151 1157 : CALL force_env_get(force_env, subsys=subsys)
152 1157 : gopt_env%type_id = gopt_param%type_id
153 2060 : SELECT CASE (gopt_env%type_id)
154 : CASE (default_ts_method_id, default_minimization_method_id)
155 : CALL cp_subsys_get(subsys, &
156 : particles=particles, &
157 903 : shell_particles=shell_particles)
158 903 : IF (ASSOCIATED(shell_particles)) nshell = shell_particles%n_els
159 : ! The same number of shell and core particles is assumed
160 903 : gopt_env%label = "GEO_OPT"
161 903 : gopt_env%tag = "GEOMETRY"
162 1173 : SELECT CASE (gopt_param%type_id)
163 : CASE (default_ts_method_id)
164 36 : gopt_env%ts_method_id = gopt_param%ts_method_id
165 903 : SELECT CASE (gopt_param%ts_method_id)
166 : CASE (default_dimer_method_id)
167 : ! For the Dimer method we use the same framework of geometry optimizers
168 : ! already defined for cp2k..
169 18 : dimer_section => section_vals_get_subs_vals(geo_opt_section, "TRANSITION_STATE%DIMER")
170 18 : CALL dimer_env_create(gopt_env%dimer_env, subsys, globenv, dimer_section, force_env)
171 :
172 : ! Setup the GEO_OPT environment for the rotation of the Dimer
173 18 : rot_opt_section => section_vals_get_subs_vals(dimer_section, "ROT_OPT")
174 18 : ALLOCATE (gopt_env%gopt_dimer_param)
175 : CALL gopt_param_read(gopt_env%gopt_dimer_param, rot_opt_section, &
176 18 : type_id=default_minimization_method_id)
177 18 : gopt_env%gopt_dimer_param%type_id = default_ts_method_id
178 :
179 : CALL gopt_f_create(gopt_env%gopt_dimer_env, gopt_env%gopt_dimer_param, force_env=force_env, &
180 18 : globenv=globenv, geo_opt_section=rot_opt_section, eval_opt_geo=eval_opt_geo)
181 18 : CALL dimer_env_retain(gopt_env%dimer_env)
182 18 : gopt_env%gopt_dimer_env%dimer_env => gopt_env%dimer_env
183 18 : gopt_env%gopt_dimer_env%label = "ROT_OPT"
184 54 : gopt_env%gopt_dimer_env%dimer_rotation = .TRUE.
185 : END SELECT
186 : END SELECT
187 : CASE (default_cell_method_id)
188 234 : gopt_env%label = "CELL_OPT"
189 234 : gopt_env%tag = " CELL "
190 9360 : ALLOCATE (gopt_env%cell_env)
191 234 : CALL cell_opt_env_create(gopt_env%cell_env, force_env, gopt_env%geo_section)
192 : CASE (default_shellcore_method_id)
193 20 : gopt_env%label = "SHELL_OPT"
194 20 : gopt_env%tag = " SHELL-CORE "
195 1157 : gopt_env%shellcore_method_id = gopt_param%shellcore_method_id
196 : END SELECT
197 1157 : END SUBROUTINE gopt_f_create
198 :
199 : ! **************************************************************************************************
200 : !> \brief ...
201 : !> \param gopt_env the geometry optimization environment to retain
202 : !> \par History
203 : !> none
204 : ! **************************************************************************************************
205 104 : SUBROUTINE gopt_f_retain(gopt_env)
206 : TYPE(gopt_f_type), POINTER :: gopt_env
207 :
208 104 : CPASSERT(ASSOCIATED(gopt_env))
209 104 : CPASSERT(gopt_env%ref_count > 0)
210 104 : gopt_env%ref_count = gopt_env%ref_count + 1
211 104 : END SUBROUTINE gopt_f_retain
212 :
213 : ! **************************************************************************************************
214 : !> \brief ...
215 : !> \param gopt_env the geometry optimization environment to release
216 : !> \par History
217 : !> none
218 : ! **************************************************************************************************
219 2400 : RECURSIVE SUBROUTINE gopt_f_release(gopt_env)
220 : TYPE(gopt_f_type), POINTER :: gopt_env
221 :
222 2400 : IF (ASSOCIATED(gopt_env)) THEN
223 1261 : CPASSERT(gopt_env%ref_count > 0)
224 1261 : gopt_env%ref_count = gopt_env%ref_count - 1
225 1261 : IF (gopt_env%ref_count == 0) THEN
226 1157 : CALL force_env_release(gopt_env%force_env)
227 : NULLIFY (gopt_env%force_env, &
228 1157 : gopt_env%globenv, &
229 1157 : gopt_env%motion_section, &
230 1157 : gopt_env%geo_section)
231 1157 : IF (ASSOCIATED(gopt_env%cell_env)) THEN
232 234 : CALL cell_opt_env_release(gopt_env%cell_env)
233 234 : DEALLOCATE (gopt_env%cell_env)
234 : END IF
235 1157 : CALL dimer_env_release(gopt_env%dimer_env)
236 1157 : CALL gopt_f_release(gopt_env%gopt_dimer_env)
237 1157 : IF (ASSOCIATED(gopt_env%gopt_dimer_param)) DEALLOCATE (gopt_env%gopt_dimer_param)
238 1157 : CALL release_spgr_type(gopt_env%spgr)
239 1157 : DEALLOCATE (gopt_env)
240 : END IF
241 : END IF
242 2400 : END SUBROUTINE gopt_f_release
243 :
244 0 : END MODULE gopt_f_types
|