(git:0de0cc2)
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-2024 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 ! **************************************************************************************************
13  USE cell_types, ONLY: cell_type
14  USE cp_control_types, ONLY: dft_control_type
15  USE cp_files, ONLY: close_file,&
16  open_file
17  USE gaussian_gridlevels, ONLY: gridlevel_info_type
19  section_vals_type,&
21  USE kinds, ONLY: default_string_length,&
22  dp
23  USE pw_env_types, ONLY: pw_env_get,&
24  pw_env_type
25  USE pw_methods, ONLY: pw_integrate_function
26  USE pw_types, ONLY: pw_c1d_gs_type,&
27  pw_r3d_rs_type
28  USE qs_environment_types, ONLY: get_qs_env,&
29  qs_environment_type
30  USE qs_rho_types, ONLY: qs_rho_get,&
31  qs_rho_type
32  USE realspace_grid_types, ONLY: realspace_grid_desc_p_type,&
33  realspace_grid_type,&
38 #include "./base/base_uses.f90"
39 
40  IMPLICIT NONE
41 
42  PRIVATE
43 
44  CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_external_density'
45 
46  PUBLIC :: external_read_density
47 
48 CONTAINS
49 
50 ! **************************************************************************************************
51 !> \brief Computes the external density on the grid
52 !> \param qs_env ...
53 !> \date 03.2011
54 !> \author D. Varsano
55 ! **************************************************************************************************
56  SUBROUTINE external_read_density(qs_env)
57 
58  TYPE(qs_environment_type), POINTER :: qs_env
59 
60  CHARACTER(len=*), PARAMETER :: routinen = 'external_read_density'
61 
62  CHARACTER(LEN=default_string_length) :: filename
63  INTEGER :: extunit, handle, i, igrid_level, j, k, &
64  nat, ndum, tag
65  INTEGER, DIMENSION(3) :: lbounds, lbounds_local, npoints, &
66  npoints_local, ubounds, ubounds_local
67  REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: buffer
68  REAL(kind=dp), DIMENSION(3) :: dr, rdum
69  REAL(kind=dp), DIMENSION(:), POINTER :: tot_rho_r_ext
70  TYPE(cell_type), POINTER :: cell
71  TYPE(dft_control_type), POINTER :: dft_control
72  TYPE(gridlevel_info_type), POINTER :: gridlevel_info
73  TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_ext_g
74  TYPE(pw_env_type), POINTER :: pw_env
75  TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_ext_r
76  TYPE(qs_rho_type), POINTER :: rho_external
77  TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
78  POINTER :: rs_descs
79  TYPE(realspace_grid_type), ALLOCATABLE, &
80  DIMENSION(:) :: rs_rho_ext
81  TYPE(section_vals_type), POINTER :: ext_den_section, input
82 
83  CALL timeset(routinen, handle)
84  NULLIFY (cell, input, ext_den_section, rs_descs, dft_control)
85  NULLIFY (rho_ext_r, rho_ext_g, tot_rho_r_ext)
86 
87  CALL get_qs_env(qs_env, &
88  cell=cell, &
89  rho_external=rho_external, &
90  input=input, &
91  pw_env=pw_env, &
92  dft_control=dft_control)
93 
94  IF (dft_control%apply_external_density) THEN
95  CALL qs_rho_get(rho_external, &
96  rho_r=rho_ext_r, &
97  rho_g=rho_ext_g, &
98  tot_rho_r=tot_rho_r_ext)
99 
100  gridlevel_info => pw_env%gridlevel_info
101 
102  CALL pw_env_get(pw_env, rs_descs=rs_descs)
103 
104  ALLOCATE (rs_rho_ext(gridlevel_info%ngrid_levels))
105 
106  DO igrid_level = 1, gridlevel_info%ngrid_levels
107  CALL rs_grid_create(rs_rho_ext(igrid_level), &
108  rs_descs(igrid_level)%rs_desc)
109  CALL rs_grid_zero(rs_rho_ext(igrid_level))
110  END DO
111 
112  igrid_level = igrid_level - 1
113 
114  ext_den_section => section_vals_get_subs_vals(input, "DFT%EXTERNAL_DENSITY")
115  CALL section_vals_val_get(ext_den_section, "FILE_DENSITY", c_val=filename)
116 
117  tag = 1
118  associate(gid => rho_ext_r(1)%pw_grid%para%group, my_rank => rho_ext_r(1)%pw_grid%para%my_pos, &
119  num_pe => rho_ext_r(1)%pw_grid%para%group_size)
120 
121  IF (dft_control%read_external_density) THEN
122 
123  DO i = 1, 3
124  dr(i) = rs_descs(igrid_level)%rs_desc%dh(i, i)
125  END DO
126  npoints = rs_descs(igrid_level)%rs_desc%npts
127  lbounds = rs_descs(igrid_level)%rs_desc%lb
128  ubounds = rs_descs(igrid_level)%rs_desc%ub
129 
130  npoints_local = rho_ext_r(1)%pw_grid%npts_local
131  lbounds_local = rho_ext_r(1)%pw_grid%bounds_local(1, :)
132  ubounds_local = rho_ext_r(1)%pw_grid%bounds_local(2, :)
133 
134  ALLOCATE (buffer(lbounds_local(3):ubounds_local(3)))
135 
136  IF (my_rank == 0) THEN
137  WRITE (*, fmt="(/,/,T2,A)") "INITIALIZING ZMP CONSTRAINED DENSITY METHOD"
138  WRITE (*, fmt="(/,(T3,A,T51,A30))") "ZMP| Reading the target density: ", filename
139 
140  CALL open_file(file_name=filename, &
141  file_status="OLD", &
142  file_form="FORMATTED", &
143  file_action="READ", &
144  unit_number=extunit)
145 
146  DO i = 1, 2
147  READ (extunit, *)
148  END DO
149  READ (extunit, *) nat, rdum
150  DO i = 1, 3
151  READ (extunit, *) ndum, rdum
152  IF (ndum /= npoints(i) .OR. (abs(rdum(i) - dr(i)) > 1e-4)) THEN
153  WRITE (*, *) "ZMP | ERROR! | CUBE FILE NOT COINCIDENT WITH INTERNAL GRID ", i
154  WRITE (*, *) "ZMP | ", ndum, " DIFFERS FROM ", npoints(i)
155  WRITE (*, *) "ZMP | ", rdum, " DIFFERS FROM ", dr(i)
156  END IF
157  END DO
158  DO i = 1, nat
159  READ (extunit, *)
160  END DO
161  END IF
162 
163  DO i = lbounds(1), ubounds(1)
164  DO j = lbounds(2), ubounds(2)
165  IF (my_rank .EQ. 0) THEN
166  READ (extunit, *) (buffer(k), k=lbounds(3), ubounds(3))
167  END IF
168  CALL gid%bcast(buffer(lbounds(3):ubounds(3)), 0)
169 
170  IF ((lbounds_local(1) .LE. i) .AND. (i .LE. ubounds_local(1)) .AND. (lbounds_local(2) .LE. j) &
171  .AND. (j .LE. ubounds_local(2))) THEN
172  rs_rho_ext(igrid_level)%r(i, j, lbounds(3):ubounds(3)) = buffer(lbounds(3):ubounds(3))
173  END IF
174 
175  END DO
176  END DO
177  IF (my_rank == 0) CALL close_file(unit_number=extunit)
178  END IF
179 
180  CALL density_rs2pw(pw_env, rs_rho_ext, rho=rho_ext_r(1), rho_gspace=rho_ext_g(1))
181  DO igrid_level = 1, SIZE(rs_rho_ext)
182  CALL rs_grid_release(rs_rho_ext(igrid_level))
183  END DO
184  tot_rho_r_ext(1) = pw_integrate_function(rho_ext_r(1), isign=1)
185  IF (my_rank == 0) THEN
186  WRITE (*, fmt="(T3,A,T61,F20.10)") "ZMP| Total external charge: ", &
187  tot_rho_r_ext(1)
188  END IF
189  DEALLOCATE (buffer, rs_rho_ext)
190  CALL gid%sync()
191  END associate
192  END IF
193 
194  CALL timestop(handle)
195 
196  END SUBROUTINE external_read_density
197 
198 END MODULE qs_external_density
199 
Handles all functions related to the CELL.
Definition: cell_types.F:15
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:308
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:119
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
Definition: pw_env_types.F:14
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
Definition: pw_env_types.F:113
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, 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_nonbond, sab_almo, sab_kp, sab_kp_nosym, 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, 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, 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, ecoul_1c, rho0_s_rs, rho0_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, 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, rhs)
Get the QUICKSTEP environment.
Routines to handle an external density The external density can be generic and is provided by user in...
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...
Definition: qs_rho_types.F:18
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...
Definition: qs_rho_types.F:229
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...