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 Interface for the force calculations
10 : !> \par History
11 : !> cjm, FEB-20-2001: pass variable box_ref
12 : !> cjm, SEPT-12-2002: major reorganization
13 : !> fawzi, APR-12-2003: introduced force_env
14 : !> cjm, FEB-27-2006: no more box_change
15 : !> MK, Nov. 2010: new interfaces added and others were updated
16 : !> \author CJM & JGH
17 : ! **************************************************************************************************
18 : MODULE force_env_types
19 : USE cell_types, ONLY: cell_type
20 : USE cp_log_handling, ONLY: cp_add_default_logger,&
21 : cp_logger_type,&
22 : cp_rm_default_logger
23 : USE cp_subsys_types, ONLY: cp_subsys_get,&
24 : cp_subsys_type,&
25 : pack_subsys_particles
26 : USE eip_environment_types, ONLY: eip_env_get,&
27 : eip_env_release,&
28 : eip_environment_type
29 : USE embed_types, ONLY: embed_env_release,&
30 : embed_env_type,&
31 : get_embed_env
32 : USE fist_energy_types, ONLY: fist_energy_type
33 : USE fist_environment_types, ONLY: fist_env_get,&
34 : fist_env_release,&
35 : fist_environment_type
36 : USE fp_types, ONLY: fp_env_release,&
37 : fp_type
38 : USE global_types, ONLY: global_environment_type,&
39 : globenv_release
40 : USE input_section_types, ONLY: section_vals_get,&
41 : section_vals_release,&
42 : section_vals_retain,&
43 : section_vals_type,&
44 : section_vals_val_get
45 : USE ipi_environment_types, ONLY: ipi_env_get,&
46 : ipi_env_release,&
47 : ipi_environment_type
48 : USE ipi_server, ONLY: shutdown_server
49 : USE kinds, ONLY: dp
50 : USE message_passing, ONLY: mp_para_env_release,&
51 : mp_para_env_type
52 : USE metadynamics_types, ONLY: meta_env_release,&
53 : meta_env_type
54 : USE mixed_energy_types, ONLY: mixed_energy_type
55 : USE mixed_environment_types, ONLY: get_mixed_env,&
56 : mixed_env_release,&
57 : mixed_environment_type
58 : USE nnp_environment_types, ONLY: nnp_env_get,&
59 : nnp_env_release,&
60 : nnp_type
61 : USE pwdft_environment_types, ONLY: pwdft_energy_type,&
62 : pwdft_env_get,&
63 : pwdft_env_release,&
64 : pwdft_environment_type
65 : USE qmmm_types, ONLY: qmmm_env_get,&
66 : qmmm_env_release,&
67 : qmmm_env_type
68 : USE qmmmx_types, ONLY: qmmmx_env_get,&
69 : qmmmx_env_release,&
70 : qmmmx_env_type
71 : USE qs_energy_types, ONLY: qs_energy_type
72 : USE qs_environment_types, ONLY: get_qs_env,&
73 : qs_env_release,&
74 : qs_environment_type
75 : #include "./base/base_uses.f90"
76 :
77 : IMPLICIT NONE
78 :
79 : PRIVATE
80 :
81 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'force_env_types'
82 :
83 : INTEGER, PARAMETER, PUBLIC :: use_fist_force = 501, &
84 : use_qs_force = 502, &
85 : use_qmmm = 503, &
86 : use_qmmmx = 504, &
87 : use_eip_force = 505, &
88 : use_mixed_force = 506, &
89 : use_embed = 507, &
90 : use_pwdft_force = 508, &
91 : use_nnp_force = 509, &
92 : use_ipi = 510
93 :
94 : CHARACTER(LEN=10), DIMENSION(501:510), PARAMETER, PUBLIC :: &
95 : use_prog_name = [ &
96 : "FIST ", &
97 : "QS ", &
98 : "QMMM ", &
99 : "QMMMX ", &
100 : "EIP ", &
101 : "MIXED ", &
102 : "EMBED ", &
103 : "SIRIUS", &
104 : "NNP ", &
105 : "IPI "]
106 :
107 : PUBLIC :: force_env_type, &
108 : force_env_p_type
109 :
110 : PUBLIC :: force_env_retain, &
111 : force_env_release, &
112 : force_env_get, &
113 : force_env_get_natom, &
114 : force_env_get_nparticle, &
115 : force_env_get_frc, &
116 : force_env_get_pos, &
117 : force_env_get_vel, &
118 : force_env_set, &
119 : multiple_fe_list
120 :
121 : ! **************************************************************************************************
122 : !> \brief wrapper to abstract the force evaluation of the various methods
123 : !> \param ref_count reference count (see doc/ReferenceCounting.html)
124 : !> \param in_use which method is in use
125 : !> \param fist_env the fist environment (allocated only if fist is in use)
126 : !> \param qs_env qs_env (activated only if quickstep is in use)
127 : !> \param globenv the globenv to have the input that generated this force_env
128 : !> \param para_env the parallel environment that contains all the parallel
129 : !> environment of the fragments
130 : !> \param meta_env the metadynamics environment, allocated if there is
131 : !> metadynamics
132 : !> \param fp_env the flexible partitioning environment
133 : !> read-only attributes (get them *only* through force_env_get):
134 : !> \param subsys the fragments that build up the actual system.
135 : !> \param cell the cell of the actual system
136 : !> \note
137 : !> as always direct manipulation of these attributes can have very
138 : !> bad effects. In this case it can be quite bad and the variables
139 : !> might not be up to date. You are warned, use only the get method...
140 : !> \par History
141 : !> 04.2003 created [fawzi]
142 : !> 07.2003 tried to adapt to multiple mpi groups
143 : !> \author fawzi
144 : ! **************************************************************************************************
145 : TYPE force_env_type
146 : INTEGER :: ref_count = 0, in_use = 0, method_name_id = 0
147 : REAL(KIND=dp) :: additional_potential = 0.0_dp
148 : TYPE(fist_environment_type), POINTER :: fist_env => NULL()
149 : TYPE(meta_env_type), POINTER :: meta_env => NULL()
150 : TYPE(fp_type), POINTER :: fp_env => NULL()
151 : TYPE(qs_environment_type), POINTER :: qs_env => NULL()
152 : TYPE(eip_environment_type), POINTER :: eip_env => NULL()
153 : TYPE(pwdft_environment_type), POINTER :: pwdft_env => NULL()
154 : TYPE(global_environment_type), POINTER :: globenv => NULL()
155 : TYPE(mp_para_env_type), POINTER :: para_env => NULL()
156 : TYPE(force_env_p_type), DIMENSION(:), POINTER :: sub_force_env => NULL()
157 : TYPE(qmmm_env_type), POINTER :: qmmm_env => NULL()
158 : TYPE(qmmmx_env_type), POINTER :: qmmmx_env => NULL()
159 : TYPE(mixed_environment_type), POINTER :: mixed_env => NULL()
160 : TYPE(nnp_type), POINTER :: nnp_env => NULL()
161 : TYPE(embed_env_type), POINTER :: embed_env => NULL()
162 : TYPE(ipi_environment_type), POINTER :: ipi_env => NULL()
163 : TYPE(section_vals_type), POINTER :: force_env_section => NULL()
164 : TYPE(section_vals_type), POINTER :: root_section => NULL()
165 : END TYPE force_env_type
166 :
167 : ! **************************************************************************************************
168 : !> \brief allows for the creation of an array of force_env
169 : !> \param force_env a force environment (see above)
170 : !> \note
171 : !> added by MJM for MC swap moves
172 : !> \author MJM
173 : ! **************************************************************************************************
174 : TYPE force_env_p_type
175 : TYPE(force_env_type), POINTER :: force_env => NULL()
176 : END TYPE force_env_p_type
177 :
178 : CONTAINS
179 :
180 : ! **************************************************************************************************
181 : !> \brief retains the given force env
182 : !> \param force_env the force environment to retain
183 : !> \par History
184 : !> 04.2003 created [fawzi]
185 : !> \author fawzi
186 : !> \note
187 : !> see doc/ReferenceCounting.html
188 : ! **************************************************************************************************
189 13691 : SUBROUTINE force_env_retain(force_env)
190 : TYPE(force_env_type), POINTER :: force_env
191 :
192 13691 : CPASSERT(ASSOCIATED(force_env))
193 13691 : CPASSERT(force_env%ref_count > 0)
194 13691 : force_env%ref_count = force_env%ref_count + 1
195 13691 : END SUBROUTINE force_env_retain
196 :
197 : ! **************************************************************************************************
198 : !> \brief releases the given force env
199 : !> \param force_env the force environment to release
200 : !> \par History
201 : !> 04.2003 created [fawzi]
202 : !> \author fawzi
203 : !> \note
204 : !> see doc/ReferenceCounting.html
205 : ! **************************************************************************************************
206 24578 : RECURSIVE SUBROUTINE force_env_release(force_env)
207 : TYPE(force_env_type), POINTER :: force_env
208 :
209 : INTEGER :: i, my_group
210 : TYPE(cp_logger_type), POINTER :: my_logger
211 :
212 24578 : IF (ASSOCIATED(force_env)) THEN
213 24578 : CPASSERT(force_env%ref_count > 0)
214 24578 : force_env%ref_count = force_env%ref_count - 1
215 24578 : IF (force_env%ref_count == 0) THEN
216 : ! Deallocate SUB_FORCE_ENV
217 10887 : IF (ASSOCIATED(force_env%sub_force_env)) THEN
218 578 : DO i = 1, SIZE(force_env%sub_force_env)
219 410 : IF (.NOT. ASSOCIATED(force_env%sub_force_env(i)%force_env)) CYCLE
220 : ! Use the proper logger to deallocate..
221 324 : IF (force_env%in_use == use_mixed_force) THEN
222 218 : my_group = force_env%mixed_env%group_distribution(force_env%para_env%mepos)
223 218 : my_logger => force_env%mixed_env%sub_logger(my_group + 1)%p
224 218 : CALL cp_add_default_logger(my_logger)
225 : END IF
226 : ! The same for embedding
227 324 : IF (force_env%in_use == use_embed) THEN
228 96 : my_group = force_env%embed_env%group_distribution(force_env%para_env%mepos)
229 96 : my_logger => force_env%embed_env%sub_logger(my_group + 1)%p
230 96 : CALL cp_add_default_logger(my_logger)
231 : END IF
232 324 : CALL force_env_release(force_env%sub_force_env(i)%force_env)
233 324 : IF (force_env%in_use == use_mixed_force) THEN
234 218 : CALL cp_rm_default_logger()
235 : END IF
236 492 : IF (force_env%in_use == use_embed) THEN
237 96 : CALL cp_rm_default_logger()
238 : END IF
239 : END DO
240 168 : DEALLOCATE (force_env%sub_force_env)
241 : END IF
242 :
243 13130 : SELECT CASE (force_env%in_use)
244 : CASE (use_fist_force)
245 2243 : CALL fist_env_release(force_env%fist_env)
246 2243 : DEALLOCATE (force_env%fist_env)
247 : CASE (use_qs_force)
248 8108 : CALL qs_env_release(force_env%qs_env)
249 8108 : DEALLOCATE (force_env%qs_env)
250 : CASE (use_eip_force)
251 8 : CALL eip_env_release(force_env%eip_env)
252 8 : DEALLOCATE (force_env%eip_env)
253 : CASE (use_pwdft_force)
254 20 : CALL pwdft_env_release(force_env%pwdft_env)
255 20 : DEALLOCATE (force_env%pwdft_env)
256 : CASE (use_mixed_force)
257 136 : CALL mixed_env_release(force_env%mixed_env)
258 136 : DEALLOCATE (force_env%mixed_env)
259 : CASE (use_nnp_force)
260 14 : CALL nnp_env_release(force_env%nnp_env)
261 14 : DEALLOCATE (force_env%nnp_env)
262 : CASE (use_embed)
263 24 : CALL embed_env_release(force_env%embed_env)
264 24 : DEALLOCATE (force_env%embed_env)
265 : CASE (use_ipi)
266 0 : CALL shutdown_server(force_env%ipi_env)
267 0 : CALL ipi_env_release(force_env%ipi_env)
268 10887 : DEALLOCATE (force_env%ipi_env)
269 : END SELECT
270 10887 : CALL globenv_release(force_env%globenv)
271 10887 : CALL mp_para_env_release(force_env%para_env)
272 : ! Not deallocated
273 10887 : CPASSERT(.NOT. ASSOCIATED(force_env%fist_env))
274 10887 : CPASSERT(.NOT. ASSOCIATED(force_env%qs_env))
275 10887 : CPASSERT(.NOT. ASSOCIATED(force_env%eip_env))
276 10887 : CPASSERT(.NOT. ASSOCIATED(force_env%pwdft_env))
277 10887 : CPASSERT(.NOT. ASSOCIATED(force_env%mixed_env))
278 10887 : CPASSERT(.NOT. ASSOCIATED(force_env%nnp_env))
279 10887 : CPASSERT(.NOT. ASSOCIATED(force_env%embed_env))
280 10887 : CPASSERT(.NOT. ASSOCIATED(force_env%ipi_env))
281 10887 : IF (ASSOCIATED(force_env%meta_env)) THEN
282 154 : CALL meta_env_release(force_env%meta_env)
283 154 : DEALLOCATE (force_env%meta_env)
284 : END IF
285 10887 : IF (ASSOCIATED(force_env%fp_env)) THEN
286 10563 : CALL fp_env_release(force_env%fp_env)
287 10563 : DEALLOCATE (force_env%fp_env)
288 : END IF
289 10887 : IF (ASSOCIATED(force_env%qmmm_env)) THEN
290 326 : CALL qmmm_env_release(force_env%qmmm_env)
291 326 : DEALLOCATE (force_env%qmmm_env)
292 : END IF
293 10887 : IF (ASSOCIATED(force_env%qmmmx_env)) THEN
294 8 : CALL qmmmx_env_release(force_env%qmmmx_env)
295 8 : DEALLOCATE (force_env%qmmmx_env)
296 : END IF
297 10887 : CALL section_vals_release(force_env%force_env_section)
298 10887 : CALL section_vals_release(force_env%root_section)
299 10887 : DEALLOCATE (force_env)
300 : END IF
301 : END IF
302 24578 : NULLIFY (force_env)
303 24578 : END SUBROUTINE force_env_release
304 :
305 : ! **************************************************************************************************
306 : !> \brief returns various attributes about the force environment
307 : !> \param force_env the force environment you what informations about
308 : !> \param in_use ...
309 : !> \param fist_env ...
310 : !> \param qs_env ...
311 : !> \param meta_env ...
312 : !> \param fp_env ...
313 : !> \param subsys ...
314 : !> \param para_env ...
315 : !> \param potential_energy ...
316 : !> \param additional_potential ...
317 : !> \param kinetic_energy ...
318 : !> \param harmonic_shell ...
319 : !> \param kinetic_shell ...
320 : !> \param cell ...
321 : !> \param sub_force_env ...
322 : !> \param qmmm_env ...
323 : !> \param qmmmx_env ...
324 : !> \param eip_env ...
325 : !> \param pwdft_env ...
326 : !> \param globenv ...
327 : !> \param input ...
328 : !> \param force_env_section ...
329 : !> \param method_name_id ...
330 : !> \param root_section ...
331 : !> \param mixed_env ...
332 : !> \param nnp_env ...
333 : !> \param embed_env ...
334 : !> \param ipi_env ...
335 : !> \par History
336 : !> 04.2003 created [fawzi]
337 : !> \author fawzi
338 : ! **************************************************************************************************
339 2178107 : RECURSIVE SUBROUTINE force_env_get(force_env, in_use, fist_env, qs_env, &
340 : meta_env, fp_env, subsys, para_env, potential_energy, additional_potential, &
341 : kinetic_energy, harmonic_shell, kinetic_shell, cell, sub_force_env, &
342 : qmmm_env, qmmmx_env, eip_env, pwdft_env, globenv, input, force_env_section, &
343 : method_name_id, root_section, mixed_env, nnp_env, embed_env, ipi_env)
344 : TYPE(force_env_type), INTENT(IN) :: force_env
345 : INTEGER, INTENT(out), OPTIONAL :: in_use
346 : TYPE(fist_environment_type), OPTIONAL, POINTER :: fist_env
347 : TYPE(qs_environment_type), OPTIONAL, POINTER :: qs_env
348 : TYPE(meta_env_type), OPTIONAL, POINTER :: meta_env
349 : TYPE(fp_type), OPTIONAL, POINTER :: fp_env
350 : TYPE(cp_subsys_type), OPTIONAL, POINTER :: subsys
351 : TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env
352 : REAL(KIND=dp), INTENT(OUT), OPTIONAL :: potential_energy, additional_potential, &
353 : kinetic_energy, harmonic_shell, &
354 : kinetic_shell
355 : TYPE(cell_type), OPTIONAL, POINTER :: cell
356 : TYPE(force_env_p_type), DIMENSION(:), OPTIONAL, &
357 : POINTER :: sub_force_env
358 : TYPE(qmmm_env_type), OPTIONAL, POINTER :: qmmm_env
359 : TYPE(qmmmx_env_type), OPTIONAL, POINTER :: qmmmx_env
360 : TYPE(eip_environment_type), OPTIONAL, POINTER :: eip_env
361 : TYPE(pwdft_environment_type), OPTIONAL, POINTER :: pwdft_env
362 : TYPE(global_environment_type), OPTIONAL, POINTER :: globenv
363 : TYPE(section_vals_type), OPTIONAL, POINTER :: input, force_env_section
364 : INTEGER, INTENT(out), OPTIONAL :: method_name_id
365 : TYPE(section_vals_type), OPTIONAL, POINTER :: root_section
366 : TYPE(mixed_environment_type), OPTIONAL, POINTER :: mixed_env
367 : TYPE(nnp_type), OPTIONAL, POINTER :: nnp_env
368 : TYPE(embed_env_type), OPTIONAL, POINTER :: embed_env
369 : TYPE(ipi_environment_type), OPTIONAL, POINTER :: ipi_env
370 :
371 : REAL(KIND=dp) :: eip_kinetic_energy, eip_potential_energy
372 : TYPE(cp_subsys_type), POINTER :: subsys_tmp
373 : TYPE(fist_energy_type), POINTER :: thermo
374 : TYPE(mixed_energy_type), POINTER :: mixed_energy
375 : TYPE(pwdft_energy_type), POINTER :: pwdft_energy
376 : TYPE(qs_energy_type), POINTER :: qs_energy
377 :
378 2178107 : NULLIFY (subsys_tmp)
379 :
380 2178107 : CPASSERT(force_env%ref_count > 0)
381 :
382 2573502 : SELECT CASE (force_env%in_use)
383 : CASE (use_qs_force)
384 395395 : CPASSERT(ASSOCIATED(force_env%qs_env))
385 395395 : CPASSERT(.NOT. PRESENT(fist_env))
386 395395 : CPASSERT(.NOT. PRESENT(eip_env))
387 395395 : CPASSERT(.NOT. PRESENT(pwdft_env))
388 395395 : CPASSERT(.NOT. PRESENT(ipi_env))
389 : CALL get_qs_env(force_env%qs_env, &
390 : energy=qs_energy, &
391 : input=input, &
392 395395 : cp_subsys=subsys)
393 395395 : IF (PRESENT(potential_energy)) potential_energy = qs_energy%total
394 395395 : CPASSERT(.NOT. PRESENT(kinetic_energy))
395 : CASE (use_fist_force)
396 1695509 : CPASSERT(ASSOCIATED(force_env%fist_env))
397 1695509 : CPASSERT(.NOT. PRESENT(input))
398 : CALL fist_env_get(force_env%fist_env, &
399 : thermo=thermo, &
400 1695509 : subsys=subsys)
401 1695509 : IF (PRESENT(potential_energy)) potential_energy = thermo%pot
402 1695509 : IF (PRESENT(kinetic_energy)) kinetic_energy = thermo%kin
403 1695509 : IF (PRESENT(kinetic_shell)) kinetic_shell = thermo%kin_shell
404 1695509 : IF (PRESENT(harmonic_shell)) harmonic_shell = thermo%harm_shell
405 : CASE (use_eip_force)
406 2416 : CPASSERT(ASSOCIATED(force_env%eip_env))
407 2416 : CPASSERT(.NOT. PRESENT(qs_env))
408 2416 : CPASSERT(.NOT. PRESENT(fist_env))
409 2416 : CPASSERT(.NOT. PRESENT(ipi_env))
410 : CALL eip_env_get(force_env%eip_env, &
411 : eip_potential_energy=eip_potential_energy, &
412 : eip_kinetic_energy=eip_kinetic_energy, &
413 2416 : subsys=subsys)
414 2416 : IF (PRESENT(potential_energy)) THEN
415 176 : potential_energy = eip_potential_energy
416 : END IF
417 2416 : IF (PRESENT(kinetic_energy)) kinetic_energy = eip_kinetic_energy
418 0 : CPASSERT(.NOT. PRESENT(kinetic_energy))
419 : CASE (use_pwdft_force)
420 220 : CPASSERT(ASSOCIATED(force_env%pwdft_env))
421 220 : CPASSERT(.NOT. PRESENT(qs_env))
422 220 : CPASSERT(.NOT. PRESENT(fist_env))
423 220 : CPASSERT(.NOT. PRESENT(ipi_env))
424 220 : CALL pwdft_env_get(force_env%pwdft_env, energy=pwdft_energy)
425 220 : CALL pwdft_env_get(force_env%pwdft_env, cp_subsys=subsys)
426 220 : IF (PRESENT(potential_energy)) potential_energy = pwdft_energy%etotal
427 220 : CPASSERT(.NOT. PRESENT(kinetic_energy))
428 : CASE (use_qmmm)
429 : CALL qmmm_env_get(force_env%qmmm_env, &
430 : subsys=subsys, &
431 : potential_energy=potential_energy, &
432 63196 : kinetic_energy=kinetic_energy)
433 : CASE (use_qmmmx)
434 : CALL qmmmx_env_get(force_env%qmmmx_env, &
435 : subsys=subsys, &
436 : potential_energy=potential_energy, &
437 16785 : kinetic_energy=kinetic_energy)
438 : CASE (use_mixed_force)
439 15227 : CPASSERT(ASSOCIATED(force_env%mixed_env))
440 15227 : CPASSERT(.NOT. PRESENT(input))
441 : CALL get_mixed_env(force_env%mixed_env, &
442 : mixed_energy=mixed_energy, &
443 15227 : subsys=subsys)
444 15227 : IF (PRESENT(potential_energy)) potential_energy = mixed_energy%pot
445 15227 : IF (PRESENT(kinetic_energy)) kinetic_energy = mixed_energy%kin
446 : ! In embedding we only have potential energies (electronic energies)
447 : CASE (use_embed)
448 338 : CPASSERT(ASSOCIATED(force_env%embed_env))
449 338 : CPASSERT(.NOT. PRESENT(input))
450 : CALL get_embed_env(force_env%embed_env, &
451 : pot_energy=potential_energy, &
452 338 : subsys=subsys)
453 : CASE (use_nnp_force)
454 5806 : CPASSERT(ASSOCIATED(force_env%nnp_env))
455 5806 : CPASSERT(.NOT. PRESENT(ipi_env))
456 : CALL nnp_env_get(force_env%nnp_env, &
457 : nnp_potential_energy=potential_energy, &
458 5806 : subsys=subsys)
459 5806 : CPASSERT(.NOT. PRESENT(kinetic_energy))
460 : CASE (use_ipi)
461 : CALL ipi_env_get(force_env%ipi_env, &
462 : ipi_energy=potential_energy, &
463 0 : subsys=subsys)
464 : CASE DEFAULT
465 2186329 : CPABORT("unknown in_use flag value ")
466 : END SELECT
467 :
468 2178107 : IF (PRESENT(force_env_section)) force_env_section => force_env%force_env_section
469 2178107 : IF (PRESENT(in_use)) in_use = force_env%in_use
470 2178107 : IF (PRESENT(method_name_id)) method_name_id = force_env%method_name_id
471 2178107 : IF (PRESENT(fist_env)) THEN
472 14 : fist_env => force_env%fist_env
473 : END IF
474 2178107 : IF (PRESENT(qs_env)) THEN
475 26407 : qs_env => force_env%qs_env
476 : END IF
477 2178107 : IF (PRESENT(eip_env)) THEN
478 0 : eip_env => force_env%eip_env
479 : END IF
480 2178107 : IF (PRESENT(pwdft_env)) THEN
481 0 : pwdft_env => force_env%pwdft_env
482 : END IF
483 2178107 : IF (PRESENT(nnp_env)) THEN
484 0 : nnp_env => force_env%nnp_env
485 : END IF
486 2178107 : IF (PRESENT(ipi_env)) THEN
487 0 : ipi_env => force_env%ipi_env
488 : END IF
489 2178107 : IF (PRESENT(para_env)) para_env => force_env%para_env
490 : ! adjust the total energy for the metadynamics
491 2178107 : IF (ASSOCIATED(force_env%meta_env)) THEN
492 430664 : IF (PRESENT(potential_energy)) THEN
493 : potential_energy = potential_energy + &
494 : force_env%meta_env%epot_s + &
495 : force_env%meta_env%epot_walls + &
496 27824 : force_env%meta_env%hills_env%energy
497 : END IF
498 430664 : IF (PRESENT(kinetic_energy)) THEN
499 0 : kinetic_energy = kinetic_energy + force_env%meta_env%ekin_s
500 : END IF
501 : END IF
502 : ! adjust the total energy for the flexible partitioning
503 2178107 : IF (ASSOCIATED(force_env%fp_env) .AND. PRESENT(potential_energy)) THEN
504 203882 : IF (force_env%fp_env%use_fp) THEN
505 244 : potential_energy = potential_energy + force_env%fp_env%energy
506 : END IF
507 : END IF
508 210342 : IF (PRESENT(potential_energy)) THEN
509 205426 : potential_energy = potential_energy + force_env%additional_potential
510 : END IF
511 2178107 : IF (PRESENT(additional_potential)) THEN
512 102625 : additional_potential = force_env%additional_potential
513 : END IF
514 2178107 : IF (PRESENT(cell)) THEN
515 507293 : CALL force_env_get(force_env, subsys=subsys_tmp)
516 507293 : CALL cp_subsys_get(subsys_tmp, cell=cell)
517 : END IF
518 2178107 : IF (PRESENT(fp_env)) fp_env => force_env%fp_env
519 2178107 : IF (PRESENT(meta_env)) meta_env => force_env%meta_env
520 2178107 : IF (PRESENT(sub_force_env)) sub_force_env => force_env%sub_force_env
521 2178107 : IF (PRESENT(qmmm_env)) qmmm_env => force_env%qmmm_env
522 2178107 : IF (PRESENT(qmmmx_env)) qmmmx_env => force_env%qmmmx_env
523 2178107 : IF (PRESENT(mixed_env)) mixed_env => force_env%mixed_env
524 2178107 : IF (PRESENT(embed_env)) embed_env => force_env%embed_env
525 2178107 : IF (PRESENT(ipi_env)) ipi_env => force_env%ipi_env
526 2178107 : IF (PRESENT(globenv)) globenv => force_env%globenv
527 2178107 : IF (PRESENT(root_section)) root_section => force_env%root_section
528 :
529 2178107 : END SUBROUTINE force_env_get
530 :
531 : ! **************************************************************************************************
532 : !> \brief returns the number of atoms
533 : !> \param force_env the force_env you what information about
534 : !> \return the number of atoms
535 : !> \date 22.11.2010 updated (MK)
536 : !> \author fawzi
537 : ! **************************************************************************************************
538 232442 : FUNCTION force_env_get_natom(force_env) RESULT(n_atom)
539 :
540 : TYPE(force_env_type), INTENT(IN) :: force_env
541 : INTEGER :: n_atom
542 :
543 : TYPE(cp_subsys_type), POINTER :: subsys
544 :
545 : n_atom = 0
546 116221 : NULLIFY (subsys)
547 116221 : CALL force_env_get(force_env, subsys=subsys)
548 116221 : CALL cp_subsys_get(subsys, natom=n_atom)
549 :
550 116221 : END FUNCTION force_env_get_natom
551 :
552 : ! **************************************************************************************************
553 : !> \brief returns the number of particles in a force environment
554 : !> \param force_env the force_env you what information about
555 : !> \return the number of particles
556 : !> \date 22.11.2010 (MK)
557 : !> \author Matthias Krack
558 : ! **************************************************************************************************
559 43216 : FUNCTION force_env_get_nparticle(force_env) RESULT(n_particle)
560 :
561 : TYPE(force_env_type), INTENT(IN) :: force_env
562 : INTEGER :: n_particle
563 :
564 : TYPE(cp_subsys_type), POINTER :: subsys
565 :
566 : n_particle = 0
567 21608 : NULLIFY (subsys)
568 21608 : CALL force_env_get(force_env, subsys=subsys)
569 21608 : CALL cp_subsys_get(subsys, nparticle=n_particle)
570 :
571 21608 : END FUNCTION force_env_get_nparticle
572 :
573 : ! **************************************************************************************************
574 : !> \brief returns the particle forces in a dimension(*) array
575 : !> \param force_env the force_env you want to get the forces
576 : !> \param frc the array of the forces
577 : !> \param n ...
578 : !> \date 22.11.2010 Creation
579 : !> \author Matthias Krack
580 : ! **************************************************************************************************
581 9676 : SUBROUTINE force_env_get_frc(force_env, frc, n)
582 :
583 : TYPE(force_env_type), INTENT(IN) :: force_env
584 : REAL(KIND=dp), DIMENSION(*), INTENT(OUT) :: frc
585 : INTEGER, INTENT(IN) :: n
586 :
587 : CHARACTER(LEN=*), PARAMETER :: routineN = 'force_env_get_frc'
588 :
589 : INTEGER :: handle
590 : TYPE(cp_subsys_type), POINTER :: subsys
591 :
592 9676 : CALL timeset(routineN, handle)
593 9676 : CPASSERT(force_env%ref_count > 0)
594 9676 : CALL force_env_get(force_env, subsys=subsys)
595 9676 : CALL pack_subsys_particles(subsys=subsys, f=frc(1:n))
596 9676 : CALL timestop(handle)
597 :
598 9676 : END SUBROUTINE force_env_get_frc
599 :
600 : ! **************************************************************************************************
601 : !> \brief returns the particle positions in a dimension(*) array
602 : !> \param force_env the force_env you want to get the positions
603 : !> \param pos the array of the positions
604 : !> \param n ...
605 : !> \date 22.11.2010 updated (MK)
606 : !> \author fawzi
607 : ! **************************************************************************************************
608 346 : SUBROUTINE force_env_get_pos(force_env, pos, n)
609 :
610 : TYPE(force_env_type), INTENT(IN) :: force_env
611 : REAL(kind=dp), DIMENSION(*), INTENT(OUT) :: pos
612 : INTEGER, INTENT(IN) :: n
613 :
614 : CHARACTER(LEN=*), PARAMETER :: routineN = 'force_env_get_pos'
615 :
616 : INTEGER :: handle
617 : TYPE(cp_subsys_type), POINTER :: subsys
618 :
619 346 : CALL timeset(routineN, handle)
620 346 : CPASSERT(force_env%ref_count > 0)
621 346 : CALL force_env_get(force_env, subsys=subsys)
622 346 : CALL pack_subsys_particles(subsys=subsys, r=pos(1:n))
623 346 : CALL timestop(handle)
624 :
625 346 : END SUBROUTINE force_env_get_pos
626 :
627 : ! **************************************************************************************************
628 : !> \brief returns the particle velocities in a dimension(*) array
629 : !> \param force_env the force_env you want to get the velocities
630 : !> \param vel the array of the velocities
631 : !> \param n ...
632 : !> \date 22.11.2010 Creation (MK)
633 : !> \author Matthias Krack
634 : ! **************************************************************************************************
635 0 : SUBROUTINE force_env_get_vel(force_env, vel, n)
636 :
637 : TYPE(force_env_type), INTENT(IN) :: force_env
638 : REAL(KIND=dp), DIMENSION(*), INTENT(OUT) :: vel
639 : INTEGER, INTENT(IN) :: n
640 :
641 : CHARACTER(LEN=*), PARAMETER :: routineN = 'force_env_get_vel'
642 :
643 : INTEGER :: handle
644 : TYPE(cp_subsys_type), POINTER :: subsys
645 :
646 0 : CALL timeset(routineN, handle)
647 0 : CPASSERT(force_env%ref_count > 0)
648 0 : CALL force_env_get(force_env, subsys=subsys)
649 0 : CALL pack_subsys_particles(subsys=subsys, v=vel(1:n))
650 0 : CALL timestop(handle)
651 :
652 0 : END SUBROUTINE force_env_get_vel
653 :
654 : ! **************************************************************************************************
655 : !> \brief changes some attributes of the force_env
656 : !> \param force_env the force environment where the cell should be changed
657 : !> \param meta_env the new meta environment
658 : !> \param fp_env ...
659 : !> \param force_env_section ...
660 : !> \param method_name_id ...
661 : !> \param additional_potential ...
662 : !> \par History
663 : !> 09.2003 created [fawzi]
664 : !> \author Fawzi Mohamed
665 : ! **************************************************************************************************
666 226886 : SUBROUTINE force_env_set(force_env, meta_env, fp_env, force_env_section, &
667 : method_name_id, additional_potential)
668 :
669 : TYPE(force_env_type), INTENT(INOUT) :: force_env
670 : TYPE(meta_env_type), OPTIONAL, POINTER :: meta_env
671 : TYPE(fp_type), OPTIONAL, POINTER :: fp_env
672 : TYPE(section_vals_type), OPTIONAL, POINTER :: force_env_section
673 : INTEGER, OPTIONAL :: method_name_id
674 : REAL(KIND=dp), INTENT(IN), OPTIONAL :: additional_potential
675 :
676 226886 : CPASSERT(force_env%ref_count > 0)
677 226886 : IF (PRESENT(meta_env)) THEN
678 10563 : IF (ASSOCIATED(force_env%meta_env)) THEN
679 0 : CALL meta_env_release(force_env%meta_env)
680 0 : DEALLOCATE (force_env%meta_env)
681 : END IF
682 10563 : force_env%meta_env => meta_env
683 : END IF
684 226886 : IF (PRESENT(fp_env)) THEN
685 10563 : IF (ASSOCIATED(force_env%fp_env)) CALL fp_env_release(force_env%fp_env)
686 10563 : force_env%fp_env => fp_env
687 : END IF
688 226886 : IF (PRESENT(force_env_section)) THEN
689 0 : IF (ASSOCIATED(force_env_section)) THEN
690 0 : CALL section_vals_retain(force_env_section)
691 0 : CALL section_vals_release(force_env%force_env_section)
692 0 : force_env%force_env_section => force_env_section
693 : END IF
694 : END IF
695 226886 : IF (PRESENT(additional_potential)) THEN
696 205760 : force_env%additional_potential = additional_potential
697 : END IF
698 226886 : IF (PRESENT(method_name_id)) THEN
699 0 : force_env%method_name_id = method_name_id
700 : END IF
701 :
702 226886 : END SUBROUTINE force_env_set
703 :
704 : ! **************************************************************************************************
705 : !> \brief returns the order of the multiple force_env
706 : !> \param force_env_sections ...
707 : !> \param root_section ...
708 : !> \param i_force_eval ...
709 : !> \param nforce_eval ...
710 : !> \author teo
711 : ! **************************************************************************************************
712 29760 : SUBROUTINE multiple_fe_list(force_env_sections, root_section, i_force_eval, nforce_eval)
713 :
714 : TYPE(section_vals_type), INTENT(IN) :: force_env_sections, root_section
715 : INTEGER, DIMENSION(:), POINTER :: i_force_eval
716 : INTEGER :: nforce_eval
717 :
718 : INTEGER :: iforce_eval, main_force_eval
719 29760 : INTEGER, DIMENSION(:), POINTER :: my_i_force_eval
720 :
721 : ! Let's treat the case of Multiple force_eval
722 :
723 29760 : CALL section_vals_get(force_env_sections, n_repetition=nforce_eval)
724 : CALL section_vals_val_get(root_section, "MULTIPLE_FORCE_EVALS%FORCE_EVAL_ORDER", &
725 29760 : i_vals=my_i_force_eval)
726 89021 : ALLOCATE (i_force_eval(nforce_eval))
727 29760 : IF (nforce_eval > 0) THEN
728 29501 : IF (nforce_eval == SIZE(my_i_force_eval)) THEN
729 117190 : i_force_eval = my_i_force_eval
730 : ELSE
731 : ! The difference in the amount of defined force_env MUST be one..
732 270 : CPASSERT(nforce_eval - SIZE(my_i_force_eval) == 1)
733 280 : DO iforce_eval = 1, nforce_eval
734 955 : IF (ANY(my_i_force_eval == iforce_eval)) CYCLE
735 : main_force_eval = iforce_eval
736 10 : EXIT
737 : END DO
738 270 : i_force_eval(1) = main_force_eval
739 1880 : i_force_eval(2:nforce_eval) = my_i_force_eval
740 : END IF
741 : END IF
742 :
743 29760 : END SUBROUTINE multiple_fe_list
744 :
745 0 : END MODULE force_env_types
|