(git:cd590b0)
Loading...
Searching...
No Matches
qs_external_density.F
Go to the documentation of this file.
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!> \brief Routines to handle an external density
9!> The external density can be generic and is provided by user input
10!> \author D. Varsano
11! **************************************************************************************************
14 USE cp_files, ONLY: close_file,&
18 USE hfx_types, ONLY: hfx_type
22 USE kinds, ONLY: default_string_length,&
23 dp
24 USE pw_env_types, ONLY: pw_env_get,&
27 USE pw_types, ONLY: pw_c1d_gs_type,&
31 USE qs_rho_types, ONLY: qs_rho_get,&
40#include "./base/base_uses.f90"
41
42 IMPLICIT NONE
43
44 PRIVATE
45
46 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_external_density'
47
49
50CONTAINS
51
52! **************************************************************************************************
53!> \brief Read a cube density for one-time use as the first SCF input density
54!> \param qs_env QS environment
55!> \param filename cube filename
56! **************************************************************************************************
57 SUBROUTINE read_scf_guess_density(qs_env, filename)
58 TYPE(qs_environment_type), POINTER :: qs_env
59 CHARACTER(LEN=*), INTENT(IN) :: filename
60
61 TYPE(dft_control_type), POINTER :: dft_control
62 TYPE(hfx_type), DIMENSION(:, :), POINTER :: x_data
63 TYPE(qs_rho_type), POINTER :: rho
64
65 NULLIFY (dft_control, rho, x_data)
66
67 IF (len_trim(filename) == 0) THEN
68 cpabort("SCF_GUESS EXTERNAL_DENSITY requires a cube filename")
69 END IF
70
71 CALL get_qs_env(qs_env, dft_control=dft_control, rho=rho, x_data=x_data)
72 IF (.NOT. dft_control%qs_control%gpw) THEN
73 cpabort("SCF_GUESS EXTERNAL_DENSITY currently supports GPW only")
74 END IF
75 IF (dft_control%nspins /= 1) THEN
76 cpabort("SCF_GUESS EXTERNAL_DENSITY currently requires a spin-restricted calculation")
77 END IF
78 IF (dft_control%use_kinetic_energy_density) THEN
79 cpabort("SCF_GUESS EXTERNAL_DENSITY does not provide a kinetic-energy density")
80 END IF
81 IF (dft_control%drho_by_collocation) THEN
82 cpabort("SCF_GUESS EXTERNAL_DENSITY is incompatible with DRHO_BY_COLLOCATION")
83 END IF
84 IF (dft_control%dft_plus_u) THEN
85 cpabort("SCF_GUESS EXTERNAL_DENSITY currently does not support DFT+U")
86 END IF
87 IF (ASSOCIATED(x_data)) THEN
88 cpabort("SCF_GUESS EXTERNAL_DENSITY does not support HFX")
89 END IF
90 IF (dft_control%apply_external_density) THEN
91 cpabort("SCF_GUESS EXTERNAL_DENSITY cannot be combined with DFT%EXTERNAL_DENSITY")
92 END IF
93
94 CALL read_cube_density(qs_env, rho, trim(filename), total_density_sign=-1, &
95 source_label="SCF GUESS")
96 CALL qs_rho_set(rho, rho_r_valid=.true., rho_g_valid=.true., &
97 drho_r_valid=.false., drho_g_valid=.false., &
98 tau_r_valid=.false., tau_g_valid=.false., soft_valid=.false.)
99
100 END SUBROUTINE read_scf_guess_density
101
102! **************************************************************************************************
103!> \brief Computes the external density on the grid
104!> \param qs_env ...
105!> \date 03.2011
106!> \author D. Varsano
107! **************************************************************************************************
108 SUBROUTINE external_read_density(qs_env)
109
110 TYPE(qs_environment_type), POINTER :: qs_env
111
112 CHARACTER(len=*), PARAMETER :: routinen = 'external_read_density'
113
114 CHARACTER(LEN=default_string_length) :: filename
115 INTEGER :: handle
116 TYPE(dft_control_type), POINTER :: dft_control
117 TYPE(qs_rho_type), POINTER :: rho_external
118 TYPE(section_vals_type), POINTER :: ext_den_section, input
119
120 CALL timeset(routinen, handle)
121 NULLIFY (input, ext_den_section, dft_control, rho_external)
122
123 CALL get_qs_env(qs_env, &
124 rho_external=rho_external, &
125 input=input, &
126 dft_control=dft_control)
127
128 IF (dft_control%apply_external_density .AND. dft_control%read_external_density) THEN
129 ext_den_section => section_vals_get_subs_vals(input, "DFT%EXTERNAL_DENSITY")
130 CALL section_vals_val_get(ext_den_section, "FILE_DENSITY", c_val=filename)
131 CALL read_cube_density(qs_env, rho_external, trim(filename), &
132 total_density_sign=1, source_label="ZMP")
133 END IF
134
135 CALL timestop(handle)
136
137 END SUBROUTINE external_read_density
138
139! **************************************************************************************************
140!> \brief Read an electron density from a Gaussian cube file into a QS density grid
141!> \param qs_env ...
142!> \param rho_target target density structure
143!> \param filename cube filename
144!> \param total_density_sign sign used for rho_target%tot_rho_r
145!> \param source_label label used in output
146! **************************************************************************************************
147 SUBROUTINE read_cube_density(qs_env, rho_target, filename, total_density_sign, source_label)
148 TYPE(qs_environment_type), POINTER :: qs_env
149 TYPE(qs_rho_type), INTENT(INOUT) :: rho_target
150 CHARACTER(LEN=*), INTENT(IN) :: filename
151 INTEGER, INTENT(IN) :: total_density_sign
152 CHARACTER(LEN=*), INTENT(IN) :: source_label
153
154 CHARACTER(len=*), PARAMETER :: routinen = 'read_cube_density'
155
156 INTEGER :: extunit, handle, i, igrid_level, j, k, &
157 nat, ndum, output_unit
158 INTEGER, DIMENSION(3) :: lbounds, lbounds_local, npoints, &
159 ubounds, ubounds_local
160 LOGICAL :: grid_mismatch
161 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: buffer
162 REAL(kind=dp), DIMENSION(3) :: cube_origin, voxel
163 REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_r
164 TYPE(gridlevel_info_type), POINTER :: gridlevel_info
165 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
166 TYPE(pw_env_type), POINTER :: pw_env
167 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
168 TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
169 POINTER :: rs_descs
170 TYPE(realspace_grid_type), ALLOCATABLE, &
171 DIMENSION(:) :: rs_rho
172
173 CALL timeset(routinen, handle)
174 NULLIFY (pw_env, rho_r, rho_g, tot_rho_r, rs_descs)
175 output_unit = cp_logger_get_default_io_unit()
176
177 IF (total_density_sign /= -1 .AND. total_density_sign /= 1) THEN
178 cpabort("total_density_sign has to be -1 or 1")
179 END IF
180
181 CALL get_qs_env(qs_env, pw_env=pw_env)
182 CALL qs_rho_get(rho_target, rho_r=rho_r, rho_g=rho_g, tot_rho_r=tot_rho_r)
183 gridlevel_info => pw_env%gridlevel_info
184 CALL pw_env_get(pw_env, rs_descs=rs_descs)
185
186 ALLOCATE (rs_rho(gridlevel_info%ngrid_levels))
187 DO igrid_level = 1, gridlevel_info%ngrid_levels
188 CALL rs_grid_create(rs_rho(igrid_level), rs_descs(igrid_level)%rs_desc)
189 CALL rs_grid_zero(rs_rho(igrid_level))
190 END DO
191 igrid_level = gridlevel_info%ngrid_levels
192
193 npoints = rs_descs(igrid_level)%rs_desc%npts
194 lbounds = rs_descs(igrid_level)%rs_desc%lb
195 ubounds = rs_descs(igrid_level)%rs_desc%ub
196 lbounds_local = rho_r(1)%pw_grid%bounds_local(1, :)
197 ubounds_local = rho_r(1)%pw_grid%bounds_local(2, :)
198 ALLOCATE (buffer(lbounds(3):ubounds(3)))
199
200 associate(gid => rho_r(1)%pw_grid%para%group, &
201 my_rank => rho_r(1)%pw_grid%para%group%mepos)
202 grid_mismatch = .false.
203 IF (my_rank == 0) THEN
204 WRITE (output_unit, fmt="(/,T3,A,A)") &
205 trim(source_label)//"| Reading electron density: ", trim(filename)
206 CALL open_file(file_name=filename, file_status="OLD", file_form="FORMATTED", &
207 file_action="READ", unit_number=extunit)
208
209 READ (extunit, *)
210 READ (extunit, *)
211 READ (extunit, *) nat, cube_origin
212 IF (maxval(abs(cube_origin)) > 1.0e-4_dp) THEN
213 grid_mismatch = .true.
214 WRITE (output_unit, fmt="(T3,A,3ES16.8)") trim(source_label)// &
215 "| Cube origin is not the CP2K grid origin: ", cube_origin
216 END IF
217 IF (nat < 0) THEN
218 grid_mismatch = .true.
219 WRITE (output_unit, fmt="(T3,A)") trim(source_label)// &
220 "| Multi-orbital cube files are not supported"
221 END IF
222 DO i = 1, 3
223 READ (extunit, *) ndum, voxel
224 IF (ndum /= npoints(i) .OR. &
225 maxval(abs(voxel - rs_descs(igrid_level)%rs_desc%dh(:, i))) > 1.0e-4_dp) THEN
226 grid_mismatch = .true.
227 WRITE (output_unit, fmt="(T3,A,I0)") trim(source_label)// &
228 "| Cube grid does not coincide with CP2K grid along axis ", i
229 WRITE (output_unit, fmt="(T3,A,I0,A,I0)") trim(source_label)//"| Grid points: ", &
230 ndum, " instead of ", npoints(i)
231 WRITE (output_unit, fmt="(T3,A,3ES16.8)") trim(source_label)//"| Cube vector: ", voxel
232 WRITE (output_unit, fmt="(T3,A,3ES16.8)") trim(source_label)//"| CP2K vector: ", &
233 rs_descs(igrid_level)%rs_desc%dh(:, i)
234 END IF
235 END DO
236 DO i = 1, abs(nat)
237 READ (extunit, *)
238 END DO
239 END IF
240
241 CALL gid%bcast(grid_mismatch, 0)
242 IF (grid_mismatch) THEN
243 IF (my_rank == 0) CALL close_file(unit_number=extunit)
244 cpabort("Cube density grid is incompatible with the CP2K real-space grid")
245 END IF
246
247 DO i = lbounds(1), ubounds(1)
248 DO j = lbounds(2), ubounds(2)
249 IF (my_rank == 0) THEN
250 READ (extunit, *) (buffer(k), k=lbounds(3), ubounds(3))
251 END IF
252 CALL gid%bcast(buffer, 0)
253 IF ((lbounds_local(1) <= i) .AND. (i <= ubounds_local(1)) .AND. &
254 (lbounds_local(2) <= j) .AND. (j <= ubounds_local(2))) THEN
255 rs_rho(igrid_level)%r(i, j, lbounds(3):ubounds(3)) = buffer
256 END IF
257 END DO
258 END DO
259 IF (my_rank == 0) CALL close_file(unit_number=extunit)
260
261 CALL density_rs2pw(pw_env, rs_rho, rho=rho_r(1), rho_gspace=rho_g(1))
262 tot_rho_r(1) = pw_integrate_function(rho_r(1), isign=total_density_sign)
263 IF (my_rank == 0) THEN
264 WRITE (output_unit, fmt="(T3,A,T61,F20.10)") trim(source_label)// &
265 "| Integrated electron density:", real(total_density_sign, dp)*tot_rho_r(1)
266 END IF
267 CALL gid%sync()
268 END associate
269
270 DO igrid_level = 1, SIZE(rs_rho)
271 CALL rs_grid_release(rs_rho(igrid_level))
272 END DO
273 DEALLOCATE (buffer, rs_rho)
274
275 CALL timestop(handle)
276
277 END SUBROUTINE read_cube_density
278
279END MODULE qs_external_density
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
Utility routines to open and close files. Tracking of preconnections.
Definition cp_files.F:16
subroutine, public open_file(file_name, file_status, file_form, file_action, file_position, file_pad, unit_number, debug, skip_get_unit_number, file_access)
Opens the requested file using a free unit number.
Definition cp_files.F:322
subroutine, public close_file(unit_number, file_status, keep_preconnection)
Close an open file given by its logical unit number. Optionally, keep the file and unit preconnected.
Definition cp_files.F:122
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
Types and set/get functions for HFX.
Definition hfx_types.F:16
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
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, mimic, 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_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, 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, matrix_vhxc, 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, xcint_weights, 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, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_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, harris_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, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Routines to handle an external density The external density can be generic and is provided by user in...
subroutine, public read_cube_density(qs_env, rho_target, filename, total_density_sign, source_label)
Read an electron density from a Gaussian cube file into a QS density grid.
subroutine, public read_scf_guess_density(qs_env, filename)
Read a cube density for one-time use as the first SCF input density.
subroutine, public external_read_density(qs_env)
Computes the external density on the grid.
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_set(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
subroutine, public rs_grid_create(rs, desc)
...
subroutine, public rs_grid_release(rs_grid)
releases the given rs grid (see doc/ReferenceCounting.html)
subroutine, public rs_grid_zero(rs)
Initialize grid to zero.
Transfers densities from PW to RS grids and potentials from PW to RS.
subroutine, public density_rs2pw(pw_env, rs_rho, rho, rho_gspace)
given partial densities on the realspace multigrids, computes the full density on the plane wave grid...
stores some data used in construction of Kohn-Sham matrix
Definition hfx_types.F:514
contained for different pw related things
keeps the density in various representations, keeping track of which ones are valid.