(git:b279b6b)
molecular_moments.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 ! **************************************************************************************************
9 !> \brief Set of routines handling the localization for molecular properties
10 ! **************************************************************************************************
12  USE atomic_kind_types, ONLY: atomic_kind_type,&
14  USE cell_types, ONLY: cell_type,&
15  pbc
16  USE cp_control_types, ONLY: dft_control_type
21  cp_fm_struct_type
22  USE cp_fm_types, ONLY: cp_fm_create,&
24  cp_fm_release,&
25  cp_fm_to_fm,&
26  cp_fm_type
28  cp_logger_type
31  USE dbcsr_api, ONLY: dbcsr_copy,&
32  dbcsr_deallocate_matrix,&
33  dbcsr_p_type,&
34  dbcsr_set
35  USE distribution_1d_types, ONLY: distribution_1d_type
37  USE input_section_types, ONLY: section_vals_type,&
39  USE kinds, ONLY: dp
40  USE message_passing, ONLY: mp_para_env_type
42  molecule_kind_type
43  USE molecule_types, ONLY: molecule_type
45  USE orbital_pointers, ONLY: indco,&
46  ncoset
47  USE particle_types, ONLY: particle_type
48  USE qs_environment_types, ONLY: get_qs_env,&
49  qs_environment_type
50  USE qs_kind_types, ONLY: get_qs_kind,&
51  qs_kind_type
52  USE qs_loc_types, ONLY: qs_loc_env_type
54 #include "./base/base_uses.f90"
55 
56  IMPLICIT NONE
57 
58  PRIVATE
59 
60  ! *** Public ***
62 
63  CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'molecular_moments'
64 
65 CONTAINS
66 
67 ! **************************************************************************************************
68 !> \brief Calculates electrical molecular moments using local operators r-r_ref
69 !> r_ref: center of mass of the molecule
70 !> Output is in atomic units
71 !> \param qs_env the qs_env in which the qs_env lives
72 !> \param qs_loc_env ...
73 !> \param mo_local ...
74 !> \param loc_print_key ...
75 !> \param molecule_set ...
76 ! **************************************************************************************************
77  SUBROUTINE calculate_molecular_moments(qs_env, qs_loc_env, mo_local, loc_print_key, molecule_set)
78  TYPE(qs_environment_type), POINTER :: qs_env
79  TYPE(qs_loc_env_type), INTENT(IN) :: qs_loc_env
80  TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: mo_local
81  TYPE(section_vals_type), POINTER :: loc_print_key
82  TYPE(molecule_type), POINTER :: molecule_set(:)
83 
84  INTEGER :: akind, first_atom, i, iatom, ikind, imol, imol_now, iounit, iproc, ispin, j, lx, &
85  ly, lz, molkind, n, n1, n2, natom, ncol_global, nm, nmol, nmols, norder, nrow_global, ns, &
86  nspins
87  INTEGER, ALLOCATABLE, DIMENSION(:) :: states
88  INTEGER, DIMENSION(2) :: nstates
89  LOGICAL :: floating, ghost
90  REAL(kind=dp) :: zeff, zmom, zwfc
91  REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: charge_set
92  REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: moment_set
93  REAL(kind=dp), DIMENSION(3) :: rcc, ria
94  REAL(kind=dp), DIMENSION(:), POINTER :: ref_point
95  TYPE(atomic_kind_type), POINTER :: atomic_kind
96  TYPE(cell_type), POINTER :: cell
97  TYPE(cp_fm_struct_type), POINTER :: fm_struct
98  TYPE(cp_fm_type) :: momv, mvector, omvector
99  TYPE(cp_logger_type), POINTER :: logger
100  TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s, moments
101  TYPE(dft_control_type), POINTER :: dft_control
102  TYPE(distribution_1d_type), POINTER :: local_molecules
103  TYPE(molecule_kind_type), POINTER :: molecule_kind
104  TYPE(mp_para_env_type), POINTER :: para_env
105  TYPE(particle_type), POINTER :: particle_set(:)
106  TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
107 
108  logger => cp_get_default_logger()
109 
110  CALL get_qs_env(qs_env, dft_control=dft_control)
111  nspins = dft_control%nspins
112  zwfc = 3.0_dp - real(nspins, kind=dp)
113 
114  CALL section_vals_val_get(loc_print_key, "MOLECULAR_MOMENTS%ORDER", i_val=norder)
115  cpassert(norder >= 0)
116  nm = ncoset(norder) - 1
117 
118  CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, cell=cell)
119  particle_set => qs_loc_env%particle_set
120  para_env => qs_loc_env%para_env
121  local_molecules => qs_loc_env%local_molecules
122  molkind = SIZE(local_molecules%n_el)
123  nmols = SIZE(molecule_set)
124  ALLOCATE (charge_set(nmols), moment_set(nm, nmols))
125  charge_set = 0.0_dp
126  moment_set = 0.0_dp
127 
128  IF (norder > 0) THEN
129  CALL get_qs_env(qs_env, matrix_s=matrix_s)
130  DO imol = 1, SIZE(molecule_set)
131  molecule_kind => molecule_set(imol)%molecule_kind
132  first_atom = molecule_set(imol)%first_atom
133  CALL get_molecule_kind(molecule_kind=molecule_kind, natom=natom)
134  ! Get reference point for this molecule
135  CALL get_reference_point(rcc, qs_env=qs_env, reference=use_mom_ref_com, &
136  ref_point=ref_point, ifirst=first_atom, &
137  ilast=first_atom + natom - 1)
138  ALLOCATE (moments(nm))
139  DO i = 1, nm
140  ALLOCATE (moments(i)%matrix)
141  CALL dbcsr_copy(moments(i)%matrix, matrix_s(1)%matrix, 'MOM MAT')
142  CALL dbcsr_set(moments(i)%matrix, 0.0_dp)
143  END DO
144  !
145  CALL build_local_moment_matrix(qs_env, moments, norder, rcc)
146  !
147  DO ispin = 1, nspins
148  IF (ASSOCIATED(molecule_set(imol)%lmi)) THEN
149  nstates(1) = molecule_set(imol)%lmi(ispin)%nstates
150  ELSE
151  nstates(1) = 0
152  END IF
153  nstates(2) = para_env%mepos
154  CALL para_env%maxloc(nstates)
155  IF (nstates(1) == 0) cycle
156  ns = nstates(1)
157  iproc = nstates(2)
158  ALLOCATE (states(ns))
159  IF (iproc == para_env%mepos) THEN
160  states(:) = molecule_set(imol)%lmi(ispin)%states(:)
161  ELSE
162  states(:) = 0
163  END IF
164  CALL para_env%bcast(states, iproc)
165  ! assemble local states for this molecule
166  associate(mo_localized => mo_local(ispin))
167  CALL cp_fm_get_info(mo_localized, ncol_global=ncol_global, nrow_global=nrow_global)
168  CALL cp_fm_struct_create(fm_struct, nrow_global=nrow_global, ncol_global=ns, &
169  para_env=mo_localized%matrix_struct%para_env, &
170  context=mo_localized%matrix_struct%context)
171  CALL cp_fm_create(mvector, fm_struct, name="mvector")
172  CALL cp_fm_create(omvector, fm_struct, name="omvector")
173  CALL cp_fm_create(momv, fm_struct, name="omvector")
174  CALL cp_fm_struct_release(fm_struct)
175  !
176  DO i = 1, ns
177  CALL cp_fm_to_fm(mo_localized, mvector, 1, states(i), i)
178  END DO
179  END associate
180  DO i = 1, nm
181  CALL cp_dbcsr_sm_fm_multiply(moments(i)%matrix, mvector, omvector, ns)
182  CALL cp_fm_schur_product(mvector, omvector, momv)
183  moment_set(i, imol) = moment_set(i, imol) - zwfc*sum(momv%local_data)
184  END DO
185  !
186  CALL cp_fm_release(mvector)
187  CALL cp_fm_release(omvector)
188  CALL cp_fm_release(momv)
189  DEALLOCATE (states)
190  END DO
191  DO i = 1, nm
192  CALL dbcsr_deallocate_matrix(moments(i)%matrix)
193  END DO
194  DEALLOCATE (moments)
195  END DO
196  END IF
197  !
198  DO ikind = 1, molkind ! loop over different molecules
199  nmol = SIZE(local_molecules%list(ikind)%array)
200  DO imol = 1, nmol ! all the molecules of the kind
201  imol_now = local_molecules%list(ikind)%array(imol) ! index in the global array
202  molecule_kind => molecule_set(imol_now)%molecule_kind
203  first_atom = molecule_set(imol_now)%first_atom
204  CALL get_molecule_kind(molecule_kind=molecule_kind, natom=natom)
205  ! Get reference point for this molecule
206  CALL get_reference_point(rcc, qs_env=qs_env, reference=use_mom_ref_com, &
207  ref_point=ref_point, ifirst=first_atom, &
208  ilast=first_atom + natom - 1)
209  ! charge
210  DO iatom = 1, natom
211  i = first_atom + iatom - 1
212  atomic_kind => particle_set(i)%atomic_kind
213  CALL get_atomic_kind(atomic_kind, kind_number=akind)
214  CALL get_qs_kind(qs_kind_set(akind), ghost=ghost, floating=floating)
215  IF (.NOT. ghost .AND. .NOT. floating) THEN
216  CALL get_qs_kind(qs_kind_set(akind), core_charge=zeff)
217  charge_set(imol_now) = charge_set(imol_now) + zeff
218  END IF
219  END DO
220  DO ispin = 1, nspins
221  IF (ASSOCIATED(molecule_set(imol_now)%lmi(ispin)%states)) THEN
222  ns = SIZE(molecule_set(imol_now)%lmi(ispin)%states)
223  charge_set(imol_now) = charge_set(imol_now) - zwfc*ns
224  END IF
225  END DO
226  !
227  IF (norder > 0) THEN
228  ! nuclear contribution
229  DO i = 1, nm
230  lx = indco(1, i + 1)
231  ly = indco(2, i + 1)
232  lz = indco(3, i + 1)
233  DO iatom = 1, natom
234  j = first_atom + iatom - 1
235  atomic_kind => particle_set(j)%atomic_kind
236  CALL get_atomic_kind(atomic_kind, kind_number=akind)
237  CALL get_qs_kind(qs_kind_set(akind), ghost=ghost, floating=floating)
238  IF (.NOT. ghost .AND. .NOT. floating) THEN
239  CALL get_qs_kind(qs_kind_set(akind), core_charge=zeff)
240  ria = particle_set(j)%r - rcc
241  ria = pbc(ria, cell)
242  zmom = zeff
243  IF (lx /= 0) zmom = zmom*ria(1)**lx
244  IF (ly /= 0) zmom = zmom*ria(2)**ly
245  IF (lz /= 0) zmom = zmom*ria(3)**lz
246  moment_set(i, imol_now) = moment_set(i, imol_now) + zmom
247  END IF
248  END DO
249  END DO
250  END IF
251  END DO
252  END DO
253  CALL para_env%sum(moment_set)
254  CALL para_env%sum(charge_set)
255 
256  iounit = cp_print_key_unit_nr(logger, loc_print_key, "MOLECULAR_MOMENTS", &
257  extension=".MolMom", middle_name="MOLECULAR_MOMENTS")
258  IF (iounit > 0) THEN
259  DO i = 1, SIZE(charge_set)
260  WRITE (unit=iounit, fmt='(A,I6,A,F12.6)') " # molecule nr:", i, " Charge:", charge_set(i)
261  DO n = 1, norder
262  n1 = ncoset(n - 1)
263  n2 = ncoset(n) - 1
264  WRITE (unit=iounit, fmt='(T4,A,I2,10(T16,6F12.6))') "Order:", n, moment_set(n1:n2, i)
265  END DO
266  END DO
267  END IF
268  CALL cp_print_key_finished_output(iounit, logger, loc_print_key, &
269  "MOLECULAR_MOMENTS")
270 
271  DEALLOCATE (charge_set, moment_set)
272 
273  END SUBROUTINE calculate_molecular_moments
274  !------------------------------------------------------------------------------
275 
276 END MODULE molecular_moments
277 
subroutine pbc(r, r_pbc, s, s_pbc, a, b, c, alpha, beta, gamma, debug, info, pbc0, h, hinv)
...
Definition: dumpdcd.F:1203
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind(atomic_kind, fist_potential, element_symbol, name, mass, kind_number, natom, atom_list, rcov, rvdw, z, qeff, apol, cpol, mm_radius, shell, shell_active, damping)
Get attributes of an atomic kind.
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...
DBCSR operations in CP2K.
subroutine, public cp_dbcsr_sm_fm_multiply(matrix, fm_in, fm_out, ncol, alpha, beta)
multiply a dbcsr with a fm matrix
basic linear algebra operations for full matrices
subroutine, public cp_fm_schur_product(matrix_a, matrix_b, matrix_c)
computes the schur product of two matrices c_ij = a_ij * b_ij
represent the structure of a full matrix
Definition: cp_fm_struct.F:14
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
Definition: cp_fm_struct.F:125
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
Definition: cp_fm_struct.F:320
represent a full matrix distributed on many processors
Definition: cp_fm_types.F:15
subroutine, public cp_fm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, nrow_locals, ncol_locals, matrix_struct, para_env)
returns all kind of information about the full matrix
Definition: cp_fm_types.F:1016
subroutine, public cp_fm_create(matrix, matrix_struct, name, use_sp)
creates a new full matrix with the given structure
Definition: cp_fm_types.F:167
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
stores a lists of integer that are local to a processor. The idea is that these integers represent ob...
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public use_mom_ref_com
objects that represent the structure of input sections and the data contained in an input section
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
Interface to the message passing library MPI.
Set of routines handling the localization for molecular properties.
subroutine, public calculate_molecular_moments(qs_env, qs_loc_env, mo_local, loc_print_key, molecule_set)
Calculates electrical molecular moments using local operators r-r_ref r_ref: center of mass of the mo...
Define the molecule kind structure types and the corresponding functionality.
subroutine, public get_molecule_kind(molecule_kind, atom_list, bond_list, bend_list, ub_list, impr_list, opbend_list, colv_list, fixd_list, g3x3_list, g4x6_list, vsite_list, torsion_list, shell_list, name, mass, charge, kind_number, natom, nbend, nbond, nub, nimpr, nopbend, nconstraint, nconstraint_fixd, nfixd, ncolv, ng3x3, ng4x6, nvsite, nfixd_restraint, ng3x3_restraint, ng4x6_restraint, nvsite_restraint, nrestraints, nmolecule, nsgf, nshell, ntorsion, molecule_list, nelectron, nelectron_alpha, nelectron_beta, bond_kind_set, bend_kind_set, ub_kind_set, impr_kind_set, opbend_kind_set, torsion_kind_set, molname_generated)
Get informations about a molecule kind.
Define the data structure for the molecule information.
Calculates the moment integrals <a|r^m|b>
Definition: moments_utils.F:15
subroutine, public get_reference_point(rpoint, drpoint, qs_env, fist_env, reference, ref_point, ifirst, ilast)
...
Definition: moments_utils.F:61
Provides Cartesian and spherical orbital pointers and indices.
integer, dimension(:), allocatable, public ncoset
integer, dimension(:, :), allocatable, public indco
Define the data structure for the particle information.
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.
Define the quickstep kind type and their sub types.
Definition: qs_kind_types.F:23
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_r3d_rs_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, U_of_dft_plus_u, J_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, J0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, init_u_ramping_each_scf, reltmat, ghost, floating, name, element_symbol, pao_basis_size, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
New version of the module for the localization of the molecular orbitals This should be able to use d...
Definition: qs_loc_types.F:25
Calculates the moment integrals <a|r^m|b> and <a|r x d/dr|b>
Definition: qs_moments.F:14
subroutine, public build_local_moment_matrix(qs_env, moments, nmoments, ref_point, ref_points, basis_type)
...
Definition: qs_moments.F:558