Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2025 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! *****************************************************************************
9 : !> \brief qs_environment methods that use many other modules
10 : !> \par History
11 : !> 09.2002 created [fawzi]
12 : !> - local atom distribution (25.06.2003,MK)
13 : !> \author Fawzi Mohamed
14 : ! *****************************************************************************
15 : MODULE qs_update_s_mstruct
16 : USE cp_control_types, ONLY: dft_control_type
17 : USE cp_ddapc_types, ONLY: cp_ddapc_release
18 : USE cp_ddapc_util, ONLY: cp_ddapc_init
19 : USE input_constants, ONLY: do_ppl_analytic,&
20 : do_ppl_grid,&
21 : kg_tnadd_embed,&
22 : kg_tnadd_embed_ri
23 : USE kinds, ONLY: default_string_length
24 : USE pw_methods, ONLY: pw_transfer
25 : USE pw_types, ONLY: pw_c1d_gs_type,&
26 : pw_r3d_rs_type
27 : USE qs_collocate_density, ONLY: calculate_ppl_grid,&
28 : calculate_rho_core,&
29 : calculate_rho_nlcc
30 : USE qs_environment_types, ONLY: get_qs_env,&
31 : qs_environment_type
32 : USE qs_ks_types, ONLY: get_ks_env,&
33 : qs_ks_did_change,&
34 : qs_ks_env_type,&
35 : set_ks_env
36 : USE qs_rho_methods, ONLY: qs_rho_rebuild
37 : USE qs_rho_types, ONLY: qs_rho_type
38 : USE qs_scf_types, ONLY: scf_env_did_change
39 : USE task_list_methods, ONLY: generate_qs_task_list
40 : USE task_list_types, ONLY: allocate_task_list,&
41 : deallocate_task_list,&
42 : task_list_type
43 : #include "./base/base_uses.f90"
44 :
45 : IMPLICIT NONE
46 : PRIVATE
47 :
48 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
49 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_update_s_mstruct'
50 :
51 : PUBLIC :: qs_env_update_s_mstruct
52 : !***
53 : CONTAINS
54 :
55 : ! *****************************************************************************
56 : !> \brief updates the s_mstruct to reflect the new overlap structure,
57 : !> and also updates rho_core distribution.
58 : !> Should be called after the atoms have moved and the new overlap
59 : !> has been calculated.
60 : !> \param qs_env the environment to update
61 : !> \par History
62 : !> 07.2002 created [fawzi]
63 : !> \author Fawzi Mohamed
64 : ! **************************************************************************************************
65 21868 : SUBROUTINE qs_env_update_s_mstruct(qs_env)
66 : TYPE(qs_environment_type), POINTER :: qs_env
67 :
68 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_env_update_s_mstruct'
69 :
70 : INTEGER :: handle
71 : LOGICAL :: do_ppl
72 : TYPE(dft_control_type), POINTER :: dft_control
73 : TYPE(pw_c1d_gs_type), POINTER :: rho_core, rho_nlcc_g
74 : TYPE(pw_r3d_rs_type), POINTER :: rho_nlcc, vppl
75 :
76 21868 : CALL timeset(routineN, handle)
77 :
78 21868 : CPASSERT(ASSOCIATED(qs_env))
79 :
80 21868 : NULLIFY (dft_control)
81 : CALL get_qs_env(qs_env, &
82 21868 : dft_control=dft_control)
83 :
84 : ! *** updates rho core ***
85 21868 : NULLIFY (rho_core)
86 21868 : CALL get_qs_env(qs_env, rho_core=rho_core)
87 21868 : IF (dft_control%qs_control%gapw) THEN
88 1462 : qs_env%qs_charges%total_rho_core_rspace = qs_env%local_rho_set%rhoz_tot
89 : ! Initial CNEO quantum nuclear charge density is a simple Zeff sum.
90 : ! Later it will be calculated from numerical integration during SCF.
91 1462 : qs_env%qs_charges%total_rho1_hard_nuc = qs_env%local_rho_set%rhoz_cneo_tot
92 1462 : IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
93 168 : CPASSERT(ASSOCIATED(rho_core))
94 : CALL calculate_rho_core(rho_core, &
95 168 : qs_env%qs_charges%total_rho_core_rspace, qs_env, only_nopaw=.TRUE.)
96 : ELSE
97 1294 : IF (ASSOCIATED(rho_core)) THEN
98 0 : CALL rho_core%release()
99 0 : DEALLOCATE (rho_core)
100 : END IF
101 : END IF
102 : ! force analytic ppl calculation
103 1462 : dft_control%qs_control%do_ppl_method = do_ppl_analytic
104 20406 : ELSE IF (dft_control%qs_control%semi_empirical) THEN
105 : !??
106 16070 : ELSE IF (dft_control%qs_control%dftb) THEN
107 : !??
108 13972 : ELSE IF (dft_control%qs_control%xtb) THEN
109 : !??
110 : ELSE
111 9288 : CPASSERT(ASSOCIATED(rho_core))
112 : CALL calculate_rho_core(rho_core, &
113 9288 : qs_env%qs_charges%total_rho_core_rspace, qs_env)
114 : END IF
115 :
116 : ! calculate local pseudopotential on grid
117 21868 : do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
118 21868 : IF (do_ppl) THEN
119 12 : NULLIFY (vppl)
120 12 : CALL get_qs_env(qs_env, vppl=vppl)
121 12 : CPASSERT(ASSOCIATED(vppl))
122 12 : CALL calculate_ppl_grid(vppl, qs_env)
123 : END IF
124 :
125 : ! compute the rho_nlcc
126 21868 : NULLIFY (rho_nlcc, rho_nlcc_g)
127 21868 : CALL get_qs_env(qs_env, rho_nlcc=rho_nlcc, rho_nlcc_g=rho_nlcc_g)
128 21868 : IF (ASSOCIATED(rho_nlcc)) THEN
129 36 : CALL calculate_rho_nlcc(rho_nlcc, qs_env)
130 36 : CALL pw_transfer(rho_nlcc, rho_nlcc_g)
131 : END IF
132 :
133 : ! allocates and creates the task_list
134 21868 : CALL qs_create_task_list(qs_env)
135 :
136 : ! *** environment for ddapc ***
137 21868 : IF (ASSOCIATED(qs_env%cp_ddapc_env)) THEN
138 128 : CALL cp_ddapc_release(qs_env%cp_ddapc_env)
139 128 : DEALLOCATE (qs_env%cp_ddapc_env)
140 : END IF
141 21868 : CALL cp_ddapc_init(qs_env)
142 :
143 : ! *** tell ks_env ***
144 21868 : CALL qs_ks_did_change(qs_env%ks_env, s_mstruct_changed=.TRUE.)
145 :
146 : ! *** Updates rho structure ***
147 21868 : CALL qs_env_rebuild_rho(qs_env=qs_env)
148 :
149 : ! *** tell scf_env ***
150 21868 : IF (ASSOCIATED(qs_env%scf_env)) THEN
151 14056 : CALL scf_env_did_change(qs_env%scf_env)
152 : END IF
153 :
154 21868 : CALL timestop(handle)
155 :
156 21868 : END SUBROUTINE qs_env_update_s_mstruct
157 :
158 : ! *****************************************************************************
159 : !> \brief ...
160 : !> \param qs_env ...
161 : ! **************************************************************************************************
162 21868 : SUBROUTINE qs_create_task_list(qs_env)
163 : TYPE(qs_environment_type), POINTER :: qs_env
164 :
165 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_create_task_list'
166 :
167 : CHARACTER(LEN=default_string_length) :: basis_type
168 : INTEGER :: handle, isub
169 : LOGICAL :: skip_load_balance_distributed, soft_valid
170 : TYPE(dft_control_type), POINTER :: dft_control
171 : TYPE(qs_ks_env_type), POINTER :: ks_env
172 : TYPE(task_list_type), POINTER :: task_list
173 :
174 21868 : CALL timeset(routineN, handle)
175 21868 : NULLIFY (ks_env, dft_control)
176 21868 : CALL get_qs_env(qs_env, ks_env=ks_env, dft_control=dft_control)
177 :
178 21868 : soft_valid = (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc)
179 21868 : skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
180 21868 : IF (.NOT. (dft_control%qs_control%semi_empirical &
181 : .OR. dft_control%qs_control%xtb &
182 : .OR. dft_control%qs_control%dftb)) THEN
183 : ! generate task lists (non-soft)
184 10750 : IF (.NOT. dft_control%qs_control%gapw) THEN
185 9288 : CALL get_ks_env(ks_env, task_list=task_list)
186 9288 : IF (.NOT. ASSOCIATED(task_list)) THEN
187 4360 : CALL allocate_task_list(task_list)
188 4360 : CALL set_ks_env(ks_env, task_list=task_list)
189 : END IF
190 : CALL generate_qs_task_list(ks_env, task_list, basis_type="ORB", &
191 : reorder_rs_grid_ranks=.TRUE., &
192 9288 : skip_load_balance_distributed=skip_load_balance_distributed)
193 : END IF
194 : ! generate the soft task list
195 10750 : IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
196 1746 : CALL get_ks_env(ks_env, task_list_soft=task_list)
197 1746 : IF (.NOT. ASSOCIATED(task_list)) THEN
198 996 : CALL allocate_task_list(task_list)
199 996 : CALL set_ks_env(ks_env, task_list_soft=task_list)
200 : END IF
201 : CALL generate_qs_task_list(ks_env, task_list, basis_type="ORB_SOFT", &
202 : reorder_rs_grid_ranks=.TRUE., &
203 1746 : skip_load_balance_distributed=skip_load_balance_distributed)
204 : END IF
205 : END IF
206 :
207 21868 : IF (dft_control%qs_control%do_kg) THEN
208 :
209 112 : IF (qs_env%kg_env%tnadd_method == kg_tnadd_embed .OR. &
210 : qs_env%kg_env%tnadd_method == kg_tnadd_embed_ri) THEN
211 :
212 82 : IF (ASSOCIATED(qs_env%kg_env%subset)) THEN
213 252 : DO isub = 1, qs_env%kg_env%nsubsets
214 170 : IF (ASSOCIATED(qs_env%kg_env%subset(isub)%task_list)) &
215 146 : CALL deallocate_task_list(qs_env%kg_env%subset(isub)%task_list)
216 : END DO
217 : ELSE
218 0 : ALLOCATE (qs_env%kg_env%subset(qs_env%kg_env%nsubsets))
219 : END IF
220 :
221 82 : IF (soft_valid) THEN
222 0 : basis_type = "ORB_SOFT"
223 : ELSE
224 82 : basis_type = "ORB"
225 : END IF
226 :
227 252 : DO isub = 1, qs_env%kg_env%nsubsets
228 170 : CALL allocate_task_list(qs_env%kg_env%subset(isub)%task_list)
229 : ! generate the subset task list from the neighborlist
230 : CALL generate_qs_task_list(ks_env, qs_env%kg_env%subset(isub)%task_list, &
231 : basis_type=basis_type, &
232 : reorder_rs_grid_ranks=.FALSE., &
233 : skip_load_balance_distributed=skip_load_balance_distributed, &
234 252 : sab_orb_external=qs_env%kg_env%subset(isub)%sab_orb)
235 : END DO
236 :
237 : END IF
238 :
239 : END IF
240 :
241 21868 : CALL timestop(handle)
242 :
243 21868 : END SUBROUTINE qs_create_task_list
244 :
245 : ! *****************************************************************************
246 : !> \brief rebuilds the rho structure, making sure that everything is allocated
247 : !> and has the right size
248 : !> \param qs_env the environment in which rho should be rebuilt
249 : !> \param rebuild_ao if it is necessary to rebuild rho_ao. Defaults to true.
250 : !> \param rebuild_grids if it in necessary to rebuild rho_r and rho_g.
251 : !> Defaults to false.
252 : !> \par History
253 : !> 10.2002 created [fawzi]
254 : !> \author Fawzi Mohamed
255 : !> \note
256 : !> needs updated pw pools, s_mstruct and h.
257 : !> The use of p to keep the structure of h (needed for the forces)
258 : !> is ugly and should be removed.
259 : !> If necessary rho is created from scratch.
260 : ! **************************************************************************************************
261 21868 : SUBROUTINE qs_env_rebuild_rho(qs_env, rebuild_ao, rebuild_grids)
262 : TYPE(qs_environment_type), POINTER :: qs_env
263 : LOGICAL, INTENT(in), OPTIONAL :: rebuild_ao, rebuild_grids
264 :
265 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_env_rebuild_rho'
266 :
267 : INTEGER :: handle
268 : LOGICAL :: do_admm, gapw_xc
269 : TYPE(dft_control_type), POINTER :: dft_control
270 : TYPE(qs_rho_type), POINTER :: rho, rho_external, rho_xc
271 :
272 21868 : NULLIFY (rho)
273 21868 : CALL timeset(routineN, handle)
274 :
275 : CALL get_qs_env(qs_env, &
276 : dft_control=dft_control, &
277 : rho=rho, &
278 : rho_xc=rho_xc, &
279 21868 : rho_external=rho_external)
280 :
281 21868 : gapw_xc = dft_control%qs_control%gapw_xc
282 21868 : do_admm = dft_control%do_admm
283 : CALL qs_rho_rebuild(rho, qs_env=qs_env, &
284 21868 : rebuild_ao=rebuild_ao, rebuild_grids=rebuild_grids)
285 :
286 21868 : IF (gapw_xc) THEN
287 : CALL qs_rho_rebuild(rho_xc, qs_env=qs_env, &
288 284 : rebuild_ao=rebuild_ao, rebuild_grids=rebuild_grids)
289 : END IF
290 :
291 : ! ZMP rebuilding external density
292 21868 : IF (dft_control%apply_external_density) THEN
293 : CALL qs_rho_rebuild(rho_external, qs_env=qs_env, &
294 0 : rebuild_grids=rebuild_grids)
295 0 : dft_control%read_external_density = .TRUE.
296 : END IF
297 :
298 21868 : CALL timestop(handle)
299 :
300 21868 : END SUBROUTINE qs_env_rebuild_rho
301 :
302 : END MODULE qs_update_s_mstruct
|