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 Set of routines handling the localization for molecular properties
10 : ! **************************************************************************************************
11 : MODULE molecular_moments
12 : USE atomic_kind_types, ONLY: atomic_kind_type,&
13 : get_atomic_kind
14 : USE cell_types, ONLY: cell_type,&
15 : pbc
16 : USE cp_control_types, ONLY: dft_control_type
17 : USE cp_dbcsr_api, ONLY: dbcsr_copy,&
18 : dbcsr_deallocate_matrix,&
19 : dbcsr_p_type,&
20 : dbcsr_set
21 : USE cp_dbcsr_operations, ONLY: cp_dbcsr_sm_fm_multiply
22 : USE cp_fm_basic_linalg, ONLY: cp_fm_schur_product
23 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
24 : cp_fm_struct_release,&
25 : cp_fm_struct_type
26 : USE cp_fm_types, ONLY: cp_fm_create,&
27 : cp_fm_get_info,&
28 : cp_fm_release,&
29 : cp_fm_to_fm,&
30 : cp_fm_type
31 : USE cp_log_handling, ONLY: cp_get_default_logger,&
32 : cp_logger_type
33 : USE cp_output_handling, ONLY: cp_print_key_finished_output,&
34 : cp_print_key_unit_nr
35 : USE distribution_1d_types, ONLY: distribution_1d_type
36 : USE input_constants, ONLY: use_mom_ref_com
37 : USE input_section_types, ONLY: section_vals_type,&
38 : section_vals_val_get
39 : USE kinds, ONLY: dp
40 : USE message_passing, ONLY: mp_para_env_type
41 : USE molecule_kind_types, ONLY: get_molecule_kind,&
42 : molecule_kind_type
43 : USE molecule_types, ONLY: molecule_type
44 : USE moments_utils, ONLY: get_reference_point
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: loc_coeff_index,&
53 : qs_loc_env_type
54 : USE qs_moments, ONLY: build_local_moment_matrix
55 : #include "./base/base_uses.f90"
56 :
57 : IMPLICIT NONE
58 :
59 : PRIVATE
60 :
61 : ! *** Public ***
62 : PUBLIC :: calculate_molecular_moments
63 :
64 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'molecular_moments'
65 :
66 : CONTAINS
67 :
68 : ! **************************************************************************************************
69 : !> \brief Calculates electrical molecular moments using local operators r-r_ref
70 : !> r_ref: center of mass of the molecule
71 : !> Output is in atomic units
72 : !> \param qs_env the qs_env in which the qs_env lives
73 : !> \param qs_loc_env ...
74 : !> \param mo_local ...
75 : !> \param loc_print_key ...
76 : !> \param molecule_set ...
77 : ! **************************************************************************************************
78 2 : SUBROUTINE calculate_molecular_moments(qs_env, qs_loc_env, mo_local, loc_print_key, molecule_set)
79 : TYPE(qs_environment_type), POINTER :: qs_env
80 : TYPE(qs_loc_env_type), INTENT(IN) :: qs_loc_env
81 : TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: mo_local
82 : TYPE(section_vals_type), POINTER :: loc_print_key
83 : TYPE(molecule_type), POINTER :: molecule_set(:)
84 :
85 : INTEGER :: akind, first_atom, i, iatom, ikind, imol, imol_now, iounit, iproc, ispin, j, lx, &
86 : ly, lz, molkind, n, n1, n2, natom, ncol_global, ncol_local, nm, nmol, nmols, norder, &
87 : nrow_global, nrow_local, ns, nspins
88 2 : INTEGER, ALLOCATABLE, DIMENSION(:) :: states
89 : INTEGER, DIMENSION(2) :: nstates
90 : LOGICAL :: floating, ghost
91 : REAL(KIND=dp) :: zeff, zmom, zwfc
92 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: charge_set
93 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: moment_set
94 : REAL(KIND=dp), DIMENSION(3) :: rcc, ria
95 2 : REAL(KIND=dp), DIMENSION(:), POINTER :: ref_point
96 : TYPE(atomic_kind_type), POINTER :: atomic_kind
97 : TYPE(cell_type), POINTER :: cell
98 : TYPE(cp_fm_struct_type), POINTER :: fm_struct
99 : TYPE(cp_fm_type) :: momv, mvector, omvector
100 : TYPE(cp_logger_type), POINTER :: logger
101 2 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s, moments
102 : TYPE(dft_control_type), POINTER :: dft_control
103 : TYPE(distribution_1d_type), POINTER :: local_molecules
104 : TYPE(molecule_kind_type), POINTER :: molecule_kind
105 : TYPE(mp_para_env_type), POINTER :: para_env
106 2 : TYPE(particle_type), POINTER :: particle_set(:)
107 2 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
108 :
109 4 : logger => cp_get_default_logger()
110 :
111 2 : CALL get_qs_env(qs_env, dft_control=dft_control)
112 2 : nspins = dft_control%nspins
113 2 : zwfc = 3.0_dp - REAL(nspins, KIND=dp)
114 :
115 2 : CALL section_vals_val_get(loc_print_key, "MOLECULAR_MOMENTS%ORDER", i_val=norder)
116 2 : CPASSERT(norder >= 0)
117 2 : nm = ncoset(norder) - 1
118 :
119 2 : CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, cell=cell)
120 2 : particle_set => qs_loc_env%particle_set
121 2 : para_env => qs_loc_env%para_env
122 2 : local_molecules => qs_loc_env%local_molecules
123 2 : molkind = SIZE(local_molecules%n_el)
124 2 : nmols = SIZE(molecule_set)
125 12 : ALLOCATE (charge_set(nmols), moment_set(nm, nmols))
126 2 : charge_set = 0.0_dp
127 2 : moment_set = 0.0_dp
128 :
129 2 : IF (norder > 0) THEN
130 2 : CALL get_qs_env(qs_env, matrix_s=matrix_s)
131 6 : DO imol = 1, SIZE(molecule_set)
132 4 : molecule_kind => molecule_set(imol)%molecule_kind
133 4 : first_atom = molecule_set(imol)%first_atom
134 4 : CALL get_molecule_kind(molecule_kind=molecule_kind, natom=natom)
135 : ! Get reference point for this molecule
136 : CALL get_reference_point(rcc, qs_env=qs_env, reference=use_mom_ref_com, &
137 : ref_point=ref_point, ifirst=first_atom, &
138 4 : ilast=first_atom + natom - 1)
139 48 : ALLOCATE (moments(nm))
140 40 : DO i = 1, nm
141 36 : ALLOCATE (moments(i)%matrix)
142 36 : CALL dbcsr_copy(moments(i)%matrix, matrix_s(1)%matrix, 'MOM MAT')
143 40 : CALL dbcsr_set(moments(i)%matrix, 0.0_dp)
144 : END DO
145 : !
146 4 : CALL build_local_moment_matrix(qs_env, moments, norder, rcc)
147 : !
148 12 : DO ispin = 1, nspins
149 8 : IF (ASSOCIATED(molecule_set(imol)%lmi)) THEN
150 4 : nstates(1) = molecule_set(imol)%lmi(ispin)%nstates
151 : ELSE
152 4 : nstates(1) = 0
153 : END IF
154 8 : nstates(2) = para_env%mepos
155 8 : CALL para_env%maxloc(nstates)
156 8 : IF (nstates(1) == 0) CYCLE
157 8 : ns = nstates(1)
158 8 : iproc = nstates(2)
159 24 : ALLOCATE (states(ns))
160 8 : IF (iproc == para_env%mepos) THEN
161 20 : states(:) = molecule_set(imol)%lmi(ispin)%states(:)
162 : ELSE
163 4 : states(:) = 0
164 : END IF
165 8 : CALL para_env%bcast(states, iproc)
166 : ! assemble local states for this molecule
167 : ! in RTP mo_local holds the real and imaginary parts of each spin
168 : ASSOCIATE (mo_localized => mo_local(loc_coeff_index(SIZE(mo_local), nspins, ispin)))
169 8 : CALL cp_fm_get_info(mo_localized, ncol_global=ncol_global, nrow_global=nrow_global)
170 : CALL cp_fm_struct_create(fm_struct, nrow_global=nrow_global, ncol_global=ns, &
171 : para_env=mo_localized%matrix_struct%para_env, &
172 8 : context=mo_localized%matrix_struct%context)
173 8 : CALL cp_fm_create(mvector, fm_struct, name="mvector")
174 8 : CALL cp_fm_create(omvector, fm_struct, name="omvector")
175 8 : CALL cp_fm_create(momv, fm_struct, name="omvector")
176 8 : CALL cp_fm_struct_release(fm_struct)
177 8 : CALL cp_fm_get_info(momv, ncol_local=ncol_local, nrow_local=nrow_local)
178 : !
179 40 : DO i = 1, ns
180 40 : CALL cp_fm_to_fm(mo_localized, mvector, 1, states(i), i)
181 : END DO
182 : END ASSOCIATE
183 80 : DO i = 1, nm
184 72 : CALL cp_dbcsr_sm_fm_multiply(moments(i)%matrix, mvector, omvector, ns)
185 72 : CALL cp_fm_schur_product(mvector, omvector, momv)
186 : moment_set(i, imol) = moment_set(i, imol) - &
187 2096 : zwfc*SUM(momv%local_data(1:nrow_local, 1:ncol_local))
188 : END DO
189 : !
190 8 : CALL cp_fm_release(mvector)
191 8 : CALL cp_fm_release(omvector)
192 8 : CALL cp_fm_release(momv)
193 36 : DEALLOCATE (states)
194 : END DO
195 40 : DO i = 1, nm
196 40 : CALL dbcsr_deallocate_matrix(moments(i)%matrix)
197 : END DO
198 10 : DEALLOCATE (moments)
199 : END DO
200 : END IF
201 : !
202 6 : DO ikind = 1, molkind ! loop over different molecules
203 4 : nmol = SIZE(local_molecules%list(ikind)%array)
204 8 : DO imol = 1, nmol ! all the molecules of the kind
205 2 : imol_now = local_molecules%list(ikind)%array(imol) ! index in the global array
206 2 : molecule_kind => molecule_set(imol_now)%molecule_kind
207 2 : first_atom = molecule_set(imol_now)%first_atom
208 2 : CALL get_molecule_kind(molecule_kind=molecule_kind, natom=natom)
209 : ! Get reference point for this molecule
210 : CALL get_reference_point(rcc, qs_env=qs_env, reference=use_mom_ref_com, &
211 : ref_point=ref_point, ifirst=first_atom, &
212 2 : ilast=first_atom + natom - 1)
213 : ! charge
214 8 : DO iatom = 1, natom
215 6 : i = first_atom + iatom - 1
216 6 : atomic_kind => particle_set(i)%atomic_kind
217 6 : CALL get_atomic_kind(atomic_kind, kind_number=akind)
218 6 : CALL get_qs_kind(qs_kind_set(akind), ghost=ghost, floating=floating)
219 14 : IF (.NOT. ghost .AND. .NOT. floating) THEN
220 6 : CALL get_qs_kind(qs_kind_set(akind), core_charge=zeff)
221 6 : charge_set(imol_now) = charge_set(imol_now) + zeff
222 : END IF
223 : END DO
224 6 : DO ispin = 1, nspins
225 6 : IF (ASSOCIATED(molecule_set(imol_now)%lmi(ispin)%states)) THEN
226 4 : ns = SIZE(molecule_set(imol_now)%lmi(ispin)%states)
227 4 : charge_set(imol_now) = charge_set(imol_now) - zwfc*ns
228 : END IF
229 : END DO
230 : !
231 8 : IF (norder > 0) THEN
232 : ! nuclear contribution
233 20 : DO i = 1, nm
234 18 : lx = indco(1, i + 1)
235 18 : ly = indco(2, i + 1)
236 18 : lz = indco(3, i + 1)
237 74 : DO iatom = 1, natom
238 54 : j = first_atom + iatom - 1
239 54 : atomic_kind => particle_set(j)%atomic_kind
240 54 : CALL get_atomic_kind(atomic_kind, kind_number=akind)
241 54 : CALL get_qs_kind(qs_kind_set(akind), ghost=ghost, floating=floating)
242 126 : IF (.NOT. ghost .AND. .NOT. floating) THEN
243 54 : CALL get_qs_kind(qs_kind_set(akind), core_charge=zeff)
244 216 : ria = particle_set(j)%r - rcc
245 216 : ria = pbc(ria, cell)
246 54 : zmom = zeff
247 54 : IF (lx /= 0) zmom = zmom*ria(1)**lx
248 54 : IF (ly /= 0) zmom = zmom*ria(2)**ly
249 54 : IF (lz /= 0) zmom = zmom*ria(3)**lz
250 54 : moment_set(i, imol_now) = moment_set(i, imol_now) + zmom
251 : END IF
252 : END DO
253 : END DO
254 : END IF
255 : END DO
256 : END DO
257 2 : CALL para_env%sum(moment_set)
258 2 : CALL para_env%sum(charge_set)
259 :
260 : iounit = cp_print_key_unit_nr(logger, loc_print_key, "MOLECULAR_MOMENTS", &
261 2 : extension=".MolMom", middle_name="MOLECULAR_MOMENTS")
262 2 : IF (iounit > 0) THEN
263 3 : DO i = 1, SIZE(charge_set)
264 2 : WRITE (UNIT=iounit, FMT='(A,I6,A,F12.6)') " # molecule nr:", i, " Charge:", charge_set(I)
265 7 : DO n = 1, norder
266 4 : n1 = ncoset(n - 1)
267 4 : n2 = ncoset(n) - 1
268 6 : WRITE (UNIT=iounit, FMT='(T4,A,I2,10(T16,6F12.6))') "Order:", n, moment_set(n1:n2, i)
269 : END DO
270 : END DO
271 : END IF
272 : CALL cp_print_key_finished_output(iounit, logger, loc_print_key, &
273 2 : "MOLECULAR_MOMENTS")
274 :
275 2 : DEALLOCATE (charge_set, moment_set)
276 :
277 6 : END SUBROUTINE calculate_molecular_moments
278 : !------------------------------------------------------------------------------
279 :
280 : END MODULE molecular_moments
|