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 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 accint_weights_forces, ONLY: weight_function_pwgrid
17 : USE cp_control_types, ONLY: dft_control_type
18 : USE cp_ddapc_types, ONLY: cp_ddapc_release
19 : USE cp_ddapc_util, ONLY: cp_ddapc_init
20 : USE input_constants, ONLY: do_ppl_analytic,&
21 : do_ppl_grid,&
22 : kg_tnadd_embed,&
23 : kg_tnadd_embed_ri
24 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
25 : section_vals_type,&
26 : section_vals_val_get
27 : USE kinds, ONLY: default_string_length,&
28 : dp
29 : USE pw_methods, ONLY: pw_transfer
30 : USE pw_types, ONLY: pw_c1d_gs_type,&
31 : pw_r3d_rs_type
32 : USE qs_collocate_density, ONLY: calculate_ppl_grid,&
33 : calculate_rho_core,&
34 : calculate_rho_nlcc
35 : USE qs_environment_types, ONLY: get_qs_env,&
36 : qs_environment_type,&
37 : set_qs_env
38 : USE qs_ks_types, ONLY: get_ks_env,&
39 : qs_ks_did_change,&
40 : qs_ks_env_type,&
41 : set_ks_env
42 : USE qs_rho_methods, ONLY: qs_rho_rebuild
43 : USE qs_rho_types, ONLY: qs_rho_create,&
44 : qs_rho_type
45 : USE qs_scf_types, ONLY: scf_env_did_change
46 : USE skala_gpw_functional, ONLY: get_gauxc_section,&
47 : native_skala_gapw_composite_direct_ao,&
48 : native_skala_gapw_composite_reference,&
49 : skala_gapw_representation
50 : USE task_list_methods, ONLY: generate_qs_task_list
51 : USE task_list_types, ONLY: allocate_task_list,&
52 : deallocate_task_list,&
53 : task_list_type
54 : USE xc_input_constants, ONLY: skala_gapw_paw_one_center
55 : #include "./base/base_uses.f90"
56 :
57 : IMPLICIT NONE
58 : PRIVATE
59 :
60 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
61 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_update_s_mstruct'
62 :
63 : PUBLIC :: qs_env_update_s_mstruct
64 :
65 : ! *****************************************************************************
66 :
67 : CONTAINS
68 :
69 : ! *****************************************************************************
70 : !> \brief updates the s_mstruct to reflect the new overlap structure,
71 : !> and also updates rho_core distribution.
72 : !> Should be called after the atoms have moved and the new overlap
73 : !> has been calculated.
74 : !> \param qs_env the environment to update
75 : !> \par History
76 : !> 07.2002 created [fawzi]
77 : !> \author Fawzi Mohamed
78 : ! **************************************************************************************************
79 29230 : SUBROUTINE qs_env_update_s_mstruct(qs_env)
80 : TYPE(qs_environment_type), POINTER :: qs_env
81 :
82 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_env_update_s_mstruct'
83 :
84 : INTEGER :: handle, nk
85 : LOGICAL :: do_ppl
86 29230 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: cw
87 : TYPE(dft_control_type), POINTER :: dft_control
88 : TYPE(pw_c1d_gs_type), POINTER :: rho_core, rho_nlcc_g
89 : TYPE(pw_r3d_rs_type), POINTER :: rho_nlcc, vppl, xcint_weights
90 :
91 29230 : CALL timeset(routineN, handle)
92 :
93 29230 : CPASSERT(ASSOCIATED(qs_env))
94 :
95 29230 : NULLIFY (dft_control)
96 : CALL get_qs_env(qs_env, &
97 29230 : dft_control=dft_control)
98 :
99 : ! *** updates rho core ***
100 29230 : NULLIFY (rho_core)
101 29230 : CALL get_qs_env(qs_env, rho_core=rho_core)
102 29230 : IF (dft_control%qs_control%gapw) THEN
103 2260 : qs_env%qs_charges%total_rho_core_rspace = qs_env%local_rho_set%rhoz_tot
104 : ! Initial CNEO quantum nuclear charge density is a simple Zeff sum.
105 : ! Later it will be calculated from numerical integration during SCF.
106 2260 : qs_env%qs_charges%total_rho1_hard_nuc = qs_env%local_rho_set%rhoz_cneo_tot
107 2260 : IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
108 294 : CPASSERT(ASSOCIATED(rho_core))
109 : CALL calculate_rho_core(rho_core, &
110 294 : qs_env%qs_charges%total_rho_core_rspace, qs_env, only_nopaw=.TRUE.)
111 : ELSE
112 1966 : IF (ASSOCIATED(rho_core)) THEN
113 0 : CALL rho_core%release()
114 0 : DEALLOCATE (rho_core)
115 : END IF
116 : END IF
117 : ! force analytic ppl calculation
118 2260 : dft_control%qs_control%do_ppl_method = do_ppl_analytic
119 26970 : ELSE IF (dft_control%qs_control%semi_empirical) THEN
120 : !??
121 22612 : ELSE IF (dft_control%qs_control%dftb) THEN
122 : !??
123 19192 : ELSE IF (dft_control%qs_control%xtb) THEN
124 : !??
125 : ELSE
126 10924 : CPASSERT(ASSOCIATED(rho_core))
127 : CALL calculate_rho_core(rho_core, &
128 10924 : qs_env%qs_charges%total_rho_core_rspace, qs_env)
129 : END IF
130 :
131 : ! calculate local pseudopotential on grid
132 29230 : do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
133 29230 : IF (do_ppl) THEN
134 12 : NULLIFY (vppl)
135 12 : CALL get_qs_env(qs_env, vppl=vppl)
136 12 : CPASSERT(ASSOCIATED(vppl))
137 12 : CALL calculate_ppl_grid(vppl, qs_env)
138 : END IF
139 :
140 : ! compute the rho_nlcc
141 29230 : NULLIFY (rho_nlcc, rho_nlcc_g)
142 29230 : CALL get_qs_env(qs_env, rho_nlcc=rho_nlcc, rho_nlcc_g=rho_nlcc_g)
143 29230 : IF (ASSOCIATED(rho_nlcc)) THEN
144 136 : CALL calculate_rho_nlcc(rho_nlcc, qs_env)
145 136 : CALL pw_transfer(rho_nlcc, rho_nlcc_g)
146 : END IF
147 :
148 : ! compute the xcint_weights
149 29230 : IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
150 2650 : IF (dft_control%qs_control%gapw_control%accurate_xcint) THEN
151 670 : CALL get_qs_env(qs_env, xcint_weights=xcint_weights, nkind=nk)
152 2010 : ALLOCATE (cw(nk))
153 1978 : cw = 1.0_dp
154 : CALL weight_function_pwgrid(xcint_weights, qs_env, &
155 : dft_control%qs_control%gapw_control%oweights, &
156 670 : dft_control%qs_control%gapw_control%aw, cw)
157 33514741 : xcint_weights%array = 1.0_dp + xcint_weights%array
158 670 : DEALLOCATE (cw)
159 : END IF
160 : END IF
161 :
162 : ! allocates and creates the task_list
163 29230 : CALL qs_create_task_list(qs_env)
164 :
165 : ! *** environment for ddapc ***
166 29230 : IF (ASSOCIATED(qs_env%cp_ddapc_env)) THEN
167 138 : CALL cp_ddapc_release(qs_env%cp_ddapc_env)
168 138 : DEALLOCATE (qs_env%cp_ddapc_env)
169 : END IF
170 29230 : CALL cp_ddapc_init(qs_env)
171 :
172 : ! *** tell ks_env ***
173 29230 : CALL qs_ks_did_change(qs_env%ks_env, s_mstruct_changed=.TRUE.)
174 :
175 : ! *** Updates rho structure ***
176 29230 : CALL qs_env_rebuild_rho(qs_env=qs_env)
177 :
178 : ! *** tell scf_env ***
179 29230 : IF (ASSOCIATED(qs_env%scf_env)) THEN
180 19672 : CALL scf_env_did_change(qs_env%scf_env)
181 : END IF
182 :
183 29230 : CALL timestop(handle)
184 :
185 29230 : END SUBROUTINE qs_env_update_s_mstruct
186 :
187 : ! *****************************************************************************
188 : !> \brief ...
189 : !> \param qs_env ...
190 : ! **************************************************************************************************
191 29230 : SUBROUTINE qs_create_task_list(qs_env)
192 : TYPE(qs_environment_type), POINTER :: qs_env
193 :
194 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_create_task_list'
195 :
196 : CHARACTER(LEN=default_string_length) :: basis_type
197 : INTEGER :: handle, isub
198 : LOGICAL :: composite_direct_ao, composite_reference, native_grid_diagnostics, &
199 : paw_one_center, skip_load_balance_distributed, soft_valid
200 : TYPE(dft_control_type), POINTER :: dft_control
201 : TYPE(qs_ks_env_type), POINTER :: ks_env
202 : TYPE(section_vals_type), POINTER :: gauxc_section, input, xc_section
203 : TYPE(task_list_type), POINTER :: task_list
204 :
205 29230 : CALL timeset(routineN, handle)
206 29230 : NULLIFY (ks_env, dft_control, gauxc_section, input, xc_section)
207 29230 : CALL get_qs_env(qs_env, ks_env=ks_env, dft_control=dft_control, input=input)
208 :
209 29230 : soft_valid = (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc)
210 29230 : xc_section => section_vals_get_subs_vals(input, "DFT%XC")
211 29230 : composite_reference = native_skala_gapw_composite_reference(xc_section)
212 : composite_direct_ao = composite_reference .AND. &
213 29230 : native_skala_gapw_composite_direct_ao(xc_section)
214 29230 : paw_one_center = skala_gapw_representation(xc_section) == skala_gapw_paw_one_center
215 29230 : native_grid_diagnostics = .FALSE.
216 29230 : gauxc_section => get_gauxc_section(xc_section)
217 29230 : IF (ASSOCIATED(gauxc_section)) THEN
218 : CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_DIAGNOSTICS", &
219 194 : l_val=native_grid_diagnostics)
220 : END IF
221 29230 : skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
222 29230 : IF (.NOT. (dft_control%qs_control%semi_empirical &
223 : .OR. dft_control%qs_control%xtb &
224 : .OR. dft_control%qs_control%dftb)) THEN
225 : ! generate task lists (non-soft)
226 13184 : IF (.NOT. dft_control%qs_control%gapw .OR. composite_direct_ao .OR. &
227 : ((composite_reference .OR. paw_one_center) .AND. native_grid_diagnostics)) THEN
228 10934 : CALL get_ks_env(ks_env, task_list=task_list)
229 10934 : IF (.NOT. ASSOCIATED(task_list)) THEN
230 5292 : CALL allocate_task_list(task_list)
231 5292 : CALL set_ks_env(ks_env, task_list=task_list)
232 : END IF
233 : CALL generate_qs_task_list(ks_env, task_list, basis_type="ORB", &
234 : reorder_rs_grid_ranks=.TRUE., &
235 10934 : skip_load_balance_distributed=skip_load_balance_distributed)
236 : END IF
237 : ! generate the soft task list
238 13184 : IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
239 2650 : CALL get_ks_env(ks_env, task_list_soft=task_list)
240 2650 : IF (.NOT. ASSOCIATED(task_list)) THEN
241 1458 : CALL allocate_task_list(task_list)
242 1458 : CALL set_ks_env(ks_env, task_list_soft=task_list)
243 : END IF
244 : CALL generate_qs_task_list(ks_env, task_list, basis_type="ORB_SOFT", &
245 : reorder_rs_grid_ranks=.TRUE., &
246 2650 : skip_load_balance_distributed=skip_load_balance_distributed)
247 : END IF
248 : END IF
249 :
250 29230 : IF (dft_control%qs_control%do_kg) THEN
251 :
252 128 : IF (qs_env%kg_env%tnadd_method == kg_tnadd_embed .OR. &
253 : qs_env%kg_env%tnadd_method == kg_tnadd_embed_ri) THEN
254 :
255 94 : IF (ASSOCIATED(qs_env%kg_env%subset)) THEN
256 296 : DO isub = 1, qs_env%kg_env%nsubsets
257 296 : IF (ASSOCIATED(qs_env%kg_env%subset(isub)%task_list)) THEN
258 64 : CALL deallocate_task_list(qs_env%kg_env%subset(isub)%task_list)
259 : END IF
260 : END DO
261 : ELSE
262 0 : ALLOCATE (qs_env%kg_env%subset(qs_env%kg_env%nsubsets))
263 : END IF
264 :
265 94 : IF (soft_valid) THEN
266 12 : basis_type = "ORB_SOFT"
267 : ELSE
268 82 : basis_type = "ORB"
269 : END IF
270 :
271 296 : DO isub = 1, qs_env%kg_env%nsubsets
272 202 : CALL allocate_task_list(qs_env%kg_env%subset(isub)%task_list)
273 : ! generate the subset task list from the neighborlist
274 : CALL generate_qs_task_list(ks_env, qs_env%kg_env%subset(isub)%task_list, &
275 : basis_type=basis_type, &
276 : reorder_rs_grid_ranks=.FALSE., &
277 : skip_load_balance_distributed=skip_load_balance_distributed, &
278 296 : sab_orb_external=qs_env%kg_env%subset(isub)%sab_orb)
279 : END DO
280 :
281 : END IF
282 :
283 : END IF
284 :
285 29230 : CALL timestop(handle)
286 :
287 29230 : END SUBROUTINE qs_create_task_list
288 :
289 : ! *****************************************************************************
290 : !> \brief rebuilds the rho structure, making sure that everything is allocated
291 : !> and has the right size
292 : !> \param qs_env the environment in which rho should be rebuilt
293 : !> \param rebuild_ao if it is necessary to rebuild rho_ao. Defaults to true.
294 : !> \param rebuild_grids if it in necessary to rebuild rho_r and rho_g.
295 : !> Defaults to false.
296 : !> \par History
297 : !> 10.2002 created [fawzi]
298 : !> \author Fawzi Mohamed
299 : !> \note
300 : !> needs updated pw pools, s_mstruct and h.
301 : !> The use of p to keep the structure of h (needed for the forces)
302 : !> is ugly and should be removed.
303 : !> If necessary rho is created from scratch.
304 : ! **************************************************************************************************
305 29230 : SUBROUTINE qs_env_rebuild_rho(qs_env, rebuild_ao, rebuild_grids)
306 : TYPE(qs_environment_type), POINTER :: qs_env
307 : LOGICAL, INTENT(in), OPTIONAL :: rebuild_ao, rebuild_grids
308 :
309 : CHARACTER(len=*), PARAMETER :: routineN = 'qs_env_rebuild_rho'
310 :
311 : INTEGER :: handle
312 : LOGICAL :: do_admm, gapw_xc
313 : TYPE(dft_control_type), POINTER :: dft_control
314 : TYPE(qs_rho_type), POINTER :: rho, rho_external, rho_xc
315 :
316 29230 : NULLIFY (rho)
317 29230 : CALL timeset(routineN, handle)
318 :
319 : CALL get_qs_env(qs_env, &
320 : dft_control=dft_control, &
321 : rho=rho, &
322 : rho_xc=rho_xc, &
323 29230 : rho_external=rho_external)
324 :
325 29230 : gapw_xc = dft_control%qs_control%gapw_xc
326 29230 : do_admm = dft_control%do_admm
327 : CALL qs_rho_rebuild(rho, qs_env=qs_env, &
328 29230 : rebuild_ao=rebuild_ao, rebuild_grids=rebuild_grids)
329 :
330 29230 : IF (gapw_xc) THEN
331 : CALL qs_rho_rebuild(rho_xc, qs_env=qs_env, &
332 390 : rebuild_ao=rebuild_ao, rebuild_grids=rebuild_grids)
333 : END IF
334 :
335 : ! ZMP rebuilding external density
336 29230 : IF (dft_control%apply_external_density) THEN
337 4 : IF (.NOT. ASSOCIATED(rho_external)) THEN
338 4 : ALLOCATE (rho_external)
339 4 : CALL qs_rho_create(rho_external)
340 4 : CALL set_qs_env(qs_env, rho_external=rho_external)
341 : END IF
342 : CALL qs_rho_rebuild(rho_external, qs_env=qs_env, &
343 4 : rebuild_grids=rebuild_grids)
344 4 : dft_control%read_external_density = .TRUE.
345 : END IF
346 :
347 29230 : CALL timestop(handle)
348 :
349 29230 : END SUBROUTINE qs_env_rebuild_rho
350 :
351 : END MODULE qs_update_s_mstruct
|