(git:ccc2433)
mixed_environment_utils.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 Util mixed_environment
10 !> \author Teodoro Laino [tlaino] - 02.2011
11 ! **************************************************************************************************
13 
15  get_results,&
16  put_results,&
18  USE cp_result_types, ONLY: cp_result_p_type,&
19  cp_result_type
22  section_vals_type,&
24  USE kinds, ONLY: default_string_length,&
25  dp
26  USE mixed_energy_types, ONLY: mixed_force_type
27  USE particle_list_types, ONLY: particle_list_type
28  USE virial_types, ONLY: virial_p_type,&
29  virial_type,&
31 #include "./base/base_uses.f90"
32 
33  IMPLICIT NONE
34 
35  PRIVATE
36 
37  CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mixed_environment_utils'
38 
39  PUBLIC :: mixed_map_forces, &
41 
42 CONTAINS
43 
44 ! **************************************************************************************************
45 !> \brief Maps forces between the different force_eval sections/environments
46 !> \param particles_mix ...
47 !> \param virial_mix ...
48 !> \param results_mix ...
49 !> \param global_forces ...
50 !> \param virials ...
51 !> \param results ...
52 !> \param factor ...
53 !> \param iforce_eval ...
54 !> \param nforce_eval ...
55 !> \param map_index ...
56 !> \param mapping_section ...
57 !> \param overwrite ...
58 !> \author Teodoro Laino - University of Zurich [tlaino] - 05.2007
59 ! **************************************************************************************************
60  SUBROUTINE mixed_map_forces(particles_mix, virial_mix, results_mix, global_forces, &
61  virials, results, factor, iforce_eval, nforce_eval, map_index, &
62  mapping_section, overwrite)
63 
64  TYPE(particle_list_type), POINTER :: particles_mix
65  TYPE(virial_type), POINTER :: virial_mix
66  TYPE(cp_result_type), POINTER :: results_mix
67  TYPE(mixed_force_type), DIMENSION(:), POINTER :: global_forces
68  TYPE(virial_p_type), DIMENSION(:), POINTER :: virials
69  TYPE(cp_result_p_type), DIMENSION(:), POINTER :: results
70  REAL(kind=dp), INTENT(IN) :: factor
71  INTEGER, INTENT(IN) :: iforce_eval, nforce_eval
72  INTEGER, DIMENSION(:), POINTER :: map_index
73  TYPE(section_vals_type), POINTER :: mapping_section
74  LOGICAL, INTENT(IN) :: overwrite
75 
76  CHARACTER(LEN=default_string_length) :: description
77  INTEGER :: iparticle, jparticle, natom, nres
78  LOGICAL :: dip_exists
79  REAL(kind=dp), DIMENSION(3) :: dip_mix, dip_tmp
80 
81 ! Get Mapping index array
82 
83  natom = SIZE(global_forces(iforce_eval)%forces, 2)
84  CALL get_subsys_map_index(mapping_section, natom, iforce_eval, nforce_eval, map_index)
85  DO iparticle = 1, natom
86  jparticle = map_index(iparticle)
87  IF (overwrite) THEN
88  particles_mix%els(jparticle)%f(:) = factor*global_forces(iforce_eval)%forces(:, iparticle)
89  ELSE
90  particles_mix%els(jparticle)%f(:) = particles_mix%els(jparticle)%f(:) + &
91  factor*global_forces(iforce_eval)%forces(:, iparticle)
92  END IF
93  END DO
94  ! Mixing Virial
95  IF (virial_mix%pv_availability) THEN
96  IF (overwrite) CALL zero_virial(virial_mix, reset=.false.)
97  virial_mix%pv_total = virial_mix%pv_total + factor*virials(iforce_eval)%virial%pv_total
98  virial_mix%pv_kinetic = virial_mix%pv_kinetic + factor*virials(iforce_eval)%virial%pv_kinetic
99  virial_mix%pv_virial = virial_mix%pv_virial + factor*virials(iforce_eval)%virial%pv_virial
100  virial_mix%pv_xc = virial_mix%pv_xc + factor*virials(iforce_eval)%virial%pv_xc
101  virial_mix%pv_fock_4c = virial_mix%pv_fock_4c + factor*virials(iforce_eval)%virial%pv_fock_4c
102  virial_mix%pv_constraint = virial_mix%pv_constraint + factor*virials(iforce_eval)%virial%pv_constraint
103  END IF
104  ! Deallocate map_index array
105  IF (ASSOCIATED(map_index)) THEN
106  DEALLOCATE (map_index)
107  END IF
108 
109  ! Collect Requested Results info
110  description = '[DIPOLE]'
111  IF (overwrite) CALL cp_results_erase(results_mix)
112 
113  dip_exists = test_for_result(results=results(iforce_eval)%results, description=description)
114  IF (dip_exists) THEN
115  CALL get_results(results=results_mix, description=description, n_rep=nres)
116  cpassert(nres <= 1)
117  dip_mix = 0.0_dp
118  IF (nres == 1) CALL get_results(results=results_mix, description=description, values=dip_mix)
119  CALL get_results(results=results(iforce_eval)%results, description=description, n_rep=nres)
120  CALL get_results(results=results(iforce_eval)%results, description=description, &
121  values=dip_tmp, nval=nres)
122  dip_mix = dip_mix + factor*dip_tmp
123  CALL cp_results_erase(results=results_mix, description=description)
124  CALL put_results(results=results_mix, description=description, values=dip_mix)
125  END IF
126 
127  END SUBROUTINE mixed_map_forces
128 
129 ! **************************************************************************************************
130 !> \brief performs mapping of the subsystems of different force_eval
131 !> \param mapping_section ...
132 !> \param natom ...
133 !> \param iforce_eval ...
134 !> \param nforce_eval ...
135 !> \param map_index ...
136 !> \param force_eval_embed ...
137 !> \author Teodoro Laino - University of Zurich [tlaino] - 05.2007
138 ! **************************************************************************************************
139  SUBROUTINE get_subsys_map_index(mapping_section, natom, iforce_eval, nforce_eval, map_index, &
140  force_eval_embed)
141 
142  TYPE(section_vals_type), POINTER :: mapping_section
143  INTEGER, INTENT(IN) :: natom, iforce_eval, nforce_eval
144  INTEGER, DIMENSION(:), POINTER :: map_index
145  LOGICAL, OPTIONAL :: force_eval_embed
146 
147  INTEGER :: i, iatom, ival, j, jval, k, n_rep, &
148  n_rep_loc, n_rep_map, n_rep_sys, tmp
149  INTEGER, DIMENSION(:), POINTER :: index_glo, index_loc, list
150  LOGICAL :: check, explicit
151  TYPE(section_vals_type), POINTER :: fragments_loc, fragments_sys, &
152  map_force_ev, map_full_sys
153 
154  cpassert(.NOT. ASSOCIATED(map_index))
155  ALLOCATE (map_index(natom))
156  CALL section_vals_get(mapping_section, explicit=explicit)
157  IF (.NOT. explicit) THEN
158  ! Standard Mapping.. subsys are assumed to have the same structure
159  DO i = 1, natom
160  map_index(i) = i
161  END DO
162  ELSE
163  ! Mapping systems with different structures
164  IF (.NOT. PRESENT(force_eval_embed)) THEN
165  map_full_sys => section_vals_get_subs_vals(mapping_section, "FORCE_EVAL_MIXED")
166  ELSE
167  map_full_sys => section_vals_get_subs_vals(mapping_section, "FORCE_EVAL_EMBED")
168  END IF
169  map_force_ev => section_vals_get_subs_vals(mapping_section, "FORCE_EVAL")
170  CALL section_vals_get(map_full_sys, explicit=explicit)
171  cpassert(explicit)
172  CALL section_vals_get(map_force_ev, explicit=explicit, n_repetition=n_rep)
173  cpassert(explicit)
174  cpassert(n_rep == nforce_eval)
175  DO i = 1, n_rep
176  CALL section_vals_val_get(map_force_ev, "_SECTION_PARAMETERS_", i_rep_section=i, i_val=ival)
177  IF (ival == iforce_eval) EXIT
178  END DO
179  cpassert(i <= nforce_eval)
180  mark_used(nforce_eval)
181  fragments_sys => section_vals_get_subs_vals(map_full_sys, "FRAGMENT")
182  fragments_loc => section_vals_get_subs_vals(map_force_ev, "FRAGMENT", i_rep_section=i)
183  !Perform few check on the structure of the input mapping section. as provided by the user
184  CALL section_vals_get(fragments_loc, n_repetition=n_rep_loc)
185  CALL section_vals_get(fragments_sys, explicit=explicit, n_repetition=n_rep_sys)
186  cpassert(explicit)
187  cpassert(n_rep_sys >= n_rep_loc)
188  IF (n_rep_loc == 0) THEN
189  NULLIFY (list)
190  ! We expect an easier syntax in this case..
191  CALL section_vals_val_get(map_force_ev, "DEFINE_FRAGMENTS", i_rep_section=i, n_rep_val=n_rep_map)
192  check = (n_rep_map /= 0)
193  cpassert(check)
194  CALL section_vals_val_get(map_force_ev, "DEFINE_FRAGMENTS", i_rep_section=i, i_vals=list)
195  cpassert(SIZE(list) > 0)
196  iatom = 0
197  DO i = 1, SIZE(list)
198  jval = list(i)
199  DO j = 1, n_rep_sys
200  CALL section_vals_val_get(fragments_sys, "_SECTION_PARAMETERS_", i_rep_section=j, i_val=tmp)
201  IF (tmp == jval) EXIT
202  END DO
203  CALL section_vals_val_get(fragments_sys, "_DEFAULT_KEYWORD_", i_rep_section=j, i_vals=index_glo)
204  DO k = 0, index_glo(2) - index_glo(1)
205  iatom = iatom + 1
206  cpassert(iatom <= natom)
207  map_index(iatom) = index_glo(1) + k
208  END DO
209  END DO
210  check = (iatom == natom)
211  cpassert(check)
212  ELSE
213  ! General syntax..
214  !Loop over the fragment of the force_eval
215  DO i = 1, n_rep_loc
216  CALL section_vals_val_get(fragments_loc, "_SECTION_PARAMETERS_", i_rep_section=i, i_val=ival)
217  CALL section_vals_val_get(fragments_loc, "MAP", i_rep_section=i, i_val=jval)
218  ! Index corresponding to the mixed_force_eval fragment
219  DO j = 1, n_rep_sys
220  CALL section_vals_val_get(fragments_sys, "_SECTION_PARAMETERS_", i_rep_section=j, i_val=tmp)
221  IF (tmp == jval) EXIT
222  END DO
223  cpassert(j <= n_rep_sys)
224  CALL section_vals_val_get(fragments_loc, "_DEFAULT_KEYWORD_", i_rep_section=i, i_vals=index_loc)
225  CALL section_vals_val_get(fragments_sys, "_DEFAULT_KEYWORD_", i_rep_section=j, i_vals=index_glo)
226  check = ((index_loc(2) - index_loc(1)) == (index_glo(2) - index_glo(1)))
227  cpassert(check)
228  ! Now let's build the real mapping
229  DO k = 0, index_loc(2) - index_loc(1)
230  map_index(index_loc(1) + k) = index_glo(1) + k
231  END DO
232  END DO
233  END IF
234  END IF
235 
236  END SUBROUTINE get_subsys_map_index
237 
238 END MODULE mixed_environment_utils
set of type/routines to handle the storage of results in force_envs
subroutine, public cp_results_erase(results, description, nval)
erase a part of result_list
logical function, public test_for_result(results, description)
test for a certain result in the result_list
set of type/routines to handle the storage of results in force_envs
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_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
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
An array-based list which grows on demand. When the internal array is full, a new array of twice the ...
Definition: list.F:24
Util mixed_environment.
subroutine, public get_subsys_map_index(mapping_section, natom, iforce_eval, nforce_eval, map_index, force_eval_embed)
performs mapping of the subsystems of different force_eval
subroutine, public mixed_map_forces(particles_mix, virial_mix, results_mix, global_forces, virials, results, factor, iforce_eval, nforce_eval, map_index, mapping_section, overwrite)
Maps forces between the different force_eval sections/environments.
represent a simple array based list of the given type
subroutine, public zero_virial(virial, reset)
...
Definition: virial_types.F:123