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 Module containing a MiMiC communicator class
10 : !> \par History
11 : !> 05.2025 Created [AA]
12 : !> \author Andrej Antalik
13 : ! **************************************************************************************************
14 :
15 : MODULE mimic_communicator
16 :
17 : USE atomic_kind_list_types, ONLY: atomic_kind_list_type
18 : USE atomic_kind_types, ONLY: get_atomic_kind
19 : USE cp_control_types, ONLY: dft_control_type
20 : USE cp_result_methods, ONLY: get_results
21 : USE cp_result_types, ONLY: cp_result_type
22 : USE cp_subsys_types, ONLY: cp_subsys_get,&
23 : cp_subsys_type
24 : USE cp_units, ONLY: cp_unit_from_cp2k
25 : USE force_env_types, ONLY: force_env_get,&
26 : force_env_type
27 : USE kinds, ONLY: default_string_length,&
28 : dp
29 : USE mcl_api, ONLY: mcl_finalize,&
30 : mcl_get_api_version,&
31 : mcl_get_program_id,&
32 : mcl_is_initialized,&
33 : mcl_receive,&
34 : mcl_send
35 : USE mcl_requests, ONLY: MCL_DATA,&
36 : MCL_LENGTH,&
37 : MCL_REQUEST,&
38 : MCL_RUNTYPE_QM_RS_GRID
39 : USE message_passing, ONLY: mp_para_env_type
40 : USE particle_list_types, ONLY: particle_list_type
41 : USE pw_env_types, ONLY: pw_env_get,&
42 : pw_env_type
43 : USE pw_pool_types, ONLY: pw_pool_type
44 : USE pw_types, ONLY: pw_r3d_rs_type
45 : USE qs_energy_types, ONLY: qs_energy_type
46 : USE qs_environment_types, ONLY: get_qs_env,&
47 : qs_environment_type,&
48 : set_qs_env
49 : USE qs_kind_types, ONLY: get_qs_kind,&
50 : qs_kind_type
51 : USE qs_ks_types, ONLY: qs_ks_env_type,&
52 : set_ks_env
53 : USE qs_rho_types, ONLY: qs_rho_get,&
54 : qs_rho_type
55 : #include "../base/base_uses.f90"
56 :
57 : IMPLICIT NONE
58 :
59 : PRIVATE
60 :
61 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mimic_communicator'
62 :
63 : ! **************************************************************************************************
64 : !> \brief MiMiC communicator class that facilitates MiMiC client-server data exchange
65 : !> \par History
66 : !> 05.2025 Created [AA]
67 : ! **************************************************************************************************
68 : TYPE, PUBLIC :: mimic_communicator_type
69 : PRIVATE
70 : !> communication
71 : TYPE(mp_para_env_type), POINTER :: para_env => Null()
72 : LOGICAL :: is_ionode = .FALSE.
73 : INTEGER :: mcl_server = 0, &
74 : client_id = -1
75 : !> CP2K data
76 : TYPE(force_env_type), POINTER :: force_env => Null()
77 : TYPE(pw_pool_type), POINTER :: pw_info => Null()
78 : TYPE(particle_list_type), POINTER :: atoms => Null()
79 : TYPE(atomic_kind_list_type), POINTER :: kinds => Null()
80 : TYPE(qs_energy_type), POINTER :: energy => Null()
81 : TYPE(pw_r3d_rs_type), POINTER :: potential => Null()
82 : TYPE(qs_rho_type), POINTER :: density => Null()
83 : INTEGER :: n_atoms = -1, &
84 : n_kinds = -1, &
85 : n_spins = -1
86 : INTEGER, DIMENSION(:, :), ALLOCATABLE :: npts_pproc
87 : !> beginning index of the local buffer in the global buffer diminished by 1
88 : INTEGER, DIMENSION(:), ALLOCATABLE :: lb_pproc
89 :
90 : CONTAINS
91 :
92 : PROCEDURE :: initialize
93 : PROCEDURE :: finalize
94 : PROCEDURE :: receive_request
95 : PROCEDURE :: send_value
96 : PROCEDURE :: send_client_info
97 : PROCEDURE :: send_atom_info
98 : PROCEDURE :: send_kind_info
99 : PROCEDURE :: send_box_info
100 : PROCEDURE :: send_result
101 : PROCEDURE :: send_grid_coordinates
102 : PROCEDURE :: send_density
103 : PROCEDURE :: send_forces
104 : PROCEDURE :: send_positions
105 : PROCEDURE :: receive_positions
106 : PROCEDURE :: receive_potential
107 :
108 : END TYPE mimic_communicator_type
109 :
110 : CONTAINS
111 :
112 : ! **************************************************************************************************
113 : !> \brief Initialize the communicator by loading data and saving pointers to relevant data
114 : !> \param this ...
115 : !> \param force_env ...
116 : !> \par History
117 : !> 05.2025 Created [AA]
118 : ! **************************************************************************************************
119 0 : SUBROUTINE initialize(this, force_env)
120 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
121 : TYPE(force_env_type), TARGET :: force_env
122 :
123 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':initialize'
124 :
125 : TYPE(cp_subsys_type), POINTER :: subsys
126 : TYPE(dft_control_type), POINTER :: dft_control
127 : TYPE(pw_env_type), POINTER :: pw_env
128 : TYPE(qs_environment_type), POINTER :: qs_env
129 : TYPE(qs_ks_env_type), POINTER :: ks_env
130 : INTEGER :: handle
131 : LOGICAL :: mcl_initialized
132 :
133 0 : CALL timeset(routineN, handle)
134 :
135 0 : CALL mcl_is_initialized(mcl_initialized)
136 0 : IF (.NOT. mcl_initialized) THEN
137 : CALL cp_abort(__LOCATION__, "MiMiC run requested, but MCL is not initialized. "// &
138 0 : "Launch CP2K as a client in an MPMD run or set MCL_COMM_MODE.")
139 : END IF
140 :
141 0 : CALL mcl_get_program_id(this%client_id)
142 :
143 0 : NULLIFY (subsys, qs_env, ks_env, pw_env)
144 0 : this%force_env => force_env
145 0 : CALL force_env_get(this%force_env, subsys=subsys, para_env=this%para_env, qs_env=qs_env)
146 : CALL cp_subsys_get(subsys, natom=this%n_atoms, particles=this%atoms, &
147 0 : nkind=this%n_kinds, atomic_kinds=this%kinds)
148 : CALL get_qs_env(qs_env, energy=this%energy, vee=this%potential, rho=this%density, &
149 0 : dft_control=dft_control, ks_env=ks_env, pw_env=pw_env)
150 0 : CALL pw_env_get(pw_env, auxbas_pw_pool=this%pw_info)
151 :
152 0 : this%is_ionode = this%para_env%is_source()
153 :
154 0 : ALLOCATE (this%npts_pproc(3, 0:this%para_env%num_pe - 1), source=0)
155 0 : this%npts_pproc(:, this%para_env%mepos) = this%pw_info%pw_grid%npts_local
156 0 : CALL this%para_env%sum(this%npts_pproc)
157 :
158 0 : ALLOCATE (this%lb_pproc(0:this%para_env%num_pe - 1), source=0)
159 : this%lb_pproc(this%para_env%mepos) = this%pw_info%pw_grid%bounds_local(1, 1) &
160 0 : - this%pw_info%pw_grid%bounds(1, 1)
161 0 : CALL this%para_env%sum(this%lb_pproc)
162 :
163 0 : this%n_spins = dft_control%nspins
164 :
165 0 : CALL set_qs_env(qs_env, mimic=.TRUE.)
166 0 : dft_control%apply_external_potential = .TRUE.
167 0 : dft_control%eval_external_potential = .FALSE.
168 :
169 : ! allocate external electrostatic potential
170 0 : IF (ASSOCIATED(this%potential)) THEN
171 0 : CALL this%potential%release()
172 0 : DEALLOCATE (this%potential)
173 : END IF
174 0 : ALLOCATE (this%potential)
175 0 : CALL this%pw_info%create_pw(this%potential)
176 0 : CALL set_ks_env(ks_env, vee=this%potential)
177 :
178 0 : CALL timestop(handle)
179 :
180 0 : END SUBROUTINE initialize
181 :
182 : ! **************************************************************************************************
183 : !> \brief Finalize the simulation by deallocating memory
184 : !> \param this ...
185 : ! **************************************************************************************************
186 0 : SUBROUTINE finalize(this)
187 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
188 :
189 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':finalize'
190 :
191 : INTEGER :: handle
192 :
193 0 : CALL timeset(routineN, handle)
194 :
195 0 : CALL this%para_env%sync()
196 :
197 0 : CALL mcl_finalize()
198 :
199 0 : CALL timestop(handle)
200 :
201 0 : END SUBROUTINE finalize
202 :
203 : ! **************************************************************************************************
204 : !> \brief Receive a request from the server
205 : !> \param this ...
206 : !> \return ...
207 : ! **************************************************************************************************
208 0 : FUNCTION receive_request(this) RESULT(request)
209 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
210 : INTEGER :: request
211 :
212 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':receive_request'
213 :
214 : INTEGER :: handle
215 :
216 0 : CALL timeset(routineN, handle)
217 :
218 0 : request = -1
219 0 : CALL mcl_receive(request, 1, MCL_REQUEST, this%mcl_server)
220 0 : CALL this%para_env%bcast(request)
221 :
222 0 : CALL timestop(handle)
223 :
224 0 : END FUNCTION receive_request
225 :
226 : ! **************************************************************************************************
227 : !> \brief Send the specified single value data to the server
228 : !> \param this ...
229 : !> \param option word corresponding to available options
230 : !> \note Several values hardcoded for now
231 : ! **************************************************************************************************
232 0 : SUBROUTINE send_value(this, option)
233 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
234 : CHARACTER(LEN=*) :: option
235 :
236 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_value'
237 :
238 : REAL(dp) :: energy
239 : INTEGER :: handle
240 :
241 0 : CALL timeset(routineN, handle)
242 :
243 0 : SELECT CASE (option)
244 : CASE ("num_atoms", "num_atoms_in_fragments")
245 0 : CALL mcl_send(this%n_atoms, 1, MCL_DATA, this%mcl_server)
246 : CASE ("num_kinds")
247 0 : CALL mcl_send(this%n_kinds, 1, MCL_DATA, this%mcl_server)
248 : CASE ("num_fragments")
249 0 : CALL mcl_send(1, 1, MCL_DATA, this%mcl_server)
250 : CASE ("num_bonds") ! later use to communicate constraints
251 0 : CALL mcl_send(0, 1, MCL_DATA, this%mcl_server)
252 : CASE ("num_angles") ! later use to communicate constraints
253 0 : CALL mcl_send(0, 1, MCL_DATA, this%mcl_server)
254 : CASE ("energy")
255 0 : energy = this%energy%total - this%energy%ee
256 0 : CALL mcl_send(energy, 1, MCL_DATA, this%mcl_server)
257 : CASE DEFAULT
258 0 : CPABORT("The value chosen in "//routineN//" is not implemented.")
259 : END SELECT
260 :
261 0 : CALL timestop(handle)
262 :
263 0 : END SUBROUTINE send_value
264 :
265 : ! **************************************************************************************************
266 : !> \brief Send the specified information about the client to the server
267 : !> \param this ...
268 : !> \param option word corresponding to available options
269 : ! **************************************************************************************************
270 0 : SUBROUTINE send_client_info(this, option)
271 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
272 : CHARACTER(LEN=*) :: option
273 :
274 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_client_info'
275 :
276 : CHARACTER(LEN=*), PARAMETER :: client_name = "CP2K"
277 : INTEGER, DIMENSION(3) :: api_version
278 : INTEGER :: handle, length
279 :
280 0 : CALL timeset(routineN, handle)
281 :
282 0 : SELECT CASE (option)
283 : CASE ("id")
284 0 : CALL mcl_send(this%client_id, 1, MCL_DATA, this%mcl_server)
285 : CASE ("name")
286 0 : length = LEN(client_name)
287 0 : CALL mcl_send(length, 1, MCL_LENGTH, this%mcl_server)
288 0 : CALL mcl_send(client_name, length, MCL_DATA, this%mcl_server)
289 : CASE ("run_type")
290 0 : CALL mcl_send(MCL_RUNTYPE_QM_RS_GRID, 1, MCL_DATA, this%mcl_server)
291 : CASE ("api_version")
292 0 : CALL mcl_get_api_version(api_version)
293 0 : CALL mcl_send(api_version, 3, MCL_DATA, this%mcl_server)
294 : CASE DEFAULT
295 0 : CPABORT("The value chosen in "//routineN//" is not implemented.")
296 : END SELECT
297 :
298 0 : CALL timestop(handle)
299 :
300 0 : END SUBROUTINE send_client_info
301 :
302 : ! **************************************************************************************************
303 : !> \brief Send the specified data for each atom to the server
304 : !> \param this ...
305 : !> \param option word corresponding to available options
306 : ! **************************************************************************************************
307 0 : SUBROUTINE send_atom_info(this, option)
308 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
309 : CHARACTER(LEN=*) :: option
310 :
311 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_atom_info'
312 :
313 0 : INTEGER, DIMENSION(:), ALLOCATABLE :: buffer
314 : INTEGER :: handle, i
315 :
316 0 : CALL timeset(routineN, handle)
317 :
318 0 : ALLOCATE (buffer(this%n_atoms))
319 : SELECT CASE (option)
320 : CASE ("kinds")
321 0 : DO i = 1, this%n_atoms
322 0 : buffer(i) = this%atoms%els(i)%atomic_kind%kind_number
323 : END DO
324 : CASE ("ids")
325 0 : DO i = 1, this%n_atoms
326 0 : buffer(i) = this%atoms%els(i)%atom_index
327 : END DO
328 : CASE DEFAULT
329 0 : CPABORT("The value chosen in "//routineN//" is not implemented.")
330 : END SELECT
331 0 : CALL mcl_send(buffer, SIZE(buffer), MCL_DATA, this%mcl_server)
332 :
333 0 : CALL timestop(handle)
334 :
335 0 : END SUBROUTINE send_atom_info
336 :
337 : ! **************************************************************************************************
338 : !> \brief Send the specified data for each kind to the server
339 : !> \param this ...
340 : !> \param option word corresponding to available options
341 : ! **************************************************************************************************
342 0 : SUBROUTINE send_kind_info(this, option)
343 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
344 : CHARACTER(LEN=*) :: option
345 :
346 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_kind_info'
347 :
348 : TYPE(qs_environment_type), POINTER :: qs_env
349 0 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kinds
350 0 : REAL(dp), DIMENSION(:), ALLOCATABLE :: buffer_dp
351 0 : INTEGER, DIMENSION(:), ALLOCATABLE :: buffer_i
352 0 : CHARACTER(LEN=:), ALLOCATABLE :: labels
353 : CHARACTER(LEN=default_string_length) :: label
354 : INTEGER :: handle, length, i
355 :
356 0 : CALL timeset(routineN, handle)
357 :
358 0 : SELECT CASE (option)
359 : CASE ("labels")
360 0 : ALLOCATE (CHARACTER(30*this%n_kinds) :: labels)
361 0 : labels = ""
362 0 : DO i = 1, this%n_kinds
363 0 : CALL get_atomic_kind(this%kinds%els(i), name=label)
364 0 : labels = TRIM(labels)//TRIM(label)//","
365 : END DO
366 0 : length = LEN(TRIM(labels)) - 1
367 0 : CALL mcl_send(length, 1, MCL_LENGTH, this%mcl_server)
368 0 : CALL mcl_send(labels, length, MCL_DATA, this%mcl_server)
369 : CASE ("elements")
370 0 : ALLOCATE (buffer_i(this%n_kinds))
371 0 : DO i = 1, this%n_kinds
372 0 : CALL get_atomic_kind(this%kinds%els(i), z=buffer_i(i))
373 : END DO
374 0 : CALL mcl_send(buffer_i, SIZE(buffer_i), MCL_DATA, this%mcl_server)
375 : CASE ("masses")
376 0 : ALLOCATE (buffer_dp(this%n_kinds))
377 0 : DO i = 1, this%n_kinds
378 0 : buffer_dp(i) = cp_unit_from_cp2k(this%kinds%els(i)%mass, "AMU")
379 : END DO
380 0 : CALL mcl_send(buffer_dp, SIZE(buffer_dp), MCL_DATA, this%mcl_server)
381 : CASE ("nuclear_charges")
382 0 : NULLIFY (qs_env, qs_kinds)
383 0 : CALL force_env_get(this%force_env, qs_env=qs_env)
384 0 : CALL get_qs_env(qs_env, qs_kind_set=qs_kinds)
385 0 : ALLOCATE (buffer_dp(this%n_kinds))
386 0 : DO i = 1, this%n_kinds
387 0 : CALL get_qs_kind(qs_kinds(i), zeff=buffer_dp(i))
388 : END DO
389 0 : CALL mcl_send(buffer_dp, SIZE(buffer_dp), MCL_DATA, this%mcl_server)
390 : CASE DEFAULT
391 0 : CPABORT("The value chosen in "//routineN//" is not implemented.")
392 : END SELECT
393 :
394 0 : CALL timestop(handle)
395 :
396 0 : END SUBROUTINE send_kind_info
397 :
398 : ! **************************************************************************************************
399 : !> \brief Send the specified box information to the server
400 : !> \param this ...
401 : !> \param option word corresponding to available options
402 : ! **************************************************************************************************
403 0 : SUBROUTINE send_box_info(this, option)
404 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
405 : CHARACTER(LEN=*) :: option
406 :
407 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_box_info'
408 :
409 : INTEGER, DIMENSION(3) :: npts_glob
410 : REAL(dp), DIMENSION(3) :: origin
411 : REAL(dp), DIMENSION(9) :: box_vectors
412 : INTEGER :: handle, i
413 :
414 0 : CALL timeset(routineN, handle)
415 :
416 0 : npts_glob = this%pw_info%pw_grid%npts
417 :
418 0 : SELECT CASE (option)
419 : CASE ("num_gridpoints")
420 0 : CALL mcl_send(npts_glob, 3, MCL_DATA, this%mcl_server)
421 : CASE ("origin")
422 0 : origin = 0.0_dp
423 0 : CALL mcl_send(origin, 3, MCL_DATA, this%mcl_server)
424 : CASE ("box_vectors")
425 0 : box_vectors = [(this%pw_info%pw_grid%dh(:, i)*REAL(npts_glob(i), dp), i=1, 3)]
426 0 : CALL mcl_send(box_vectors, 9, MCL_DATA, this%mcl_server)
427 : CASE DEFAULT
428 0 : CPABORT("The value chosen in "//routineN//" is not implemented.")
429 : END SELECT
430 :
431 0 : CALL timestop(handle)
432 :
433 0 : END SUBROUTINE send_box_info
434 :
435 : ! **************************************************************************************************
436 : !> \brief Send the specified result to the server
437 : !> \param this ...
438 : !> \param option word corresponding to available options
439 : ! **************************************************************************************************
440 0 : SUBROUTINE send_result(this, option)
441 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
442 : CHARACTER(LEN=*) :: option
443 :
444 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_result'
445 :
446 : TYPE(qs_environment_type), POINTER :: qs_env
447 : TYPE(cp_result_type), POINTER :: results
448 : CHARACTER(LEN=default_string_length) :: description
449 0 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: buffer
450 : INTEGER :: handle
451 :
452 0 : CALL timeset(routineN, handle)
453 :
454 0 : NULLIFY (qs_env, results)
455 0 : CALL force_env_get(this%force_env, qs_env=qs_env)
456 0 : CALL get_qs_env(qs_env, results=results)
457 :
458 0 : SELECT CASE (option)
459 : CASE ("hirshfeld_charges")
460 0 : description = "[HIRSHFELD-CHARGES]"
461 0 : ALLOCATE (buffer(this%n_atoms), source=0.0_dp)
462 0 : CALL get_results(results, description, buffer)
463 0 : CALL mcl_send(buffer, SIZE(buffer), MCL_DATA, this%mcl_server)
464 : CASE DEFAULT
465 0 : CPABORT("The value chosen in "//routineN//" is not implemented.")
466 : END SELECT
467 :
468 0 : CALL timestop(handle)
469 :
470 0 : END SUBROUTINE send_result
471 :
472 : ! **************************************************************************************************
473 : !> \brief Send grid point coordinates to the server
474 : !> \param this ...
475 : ! **************************************************************************************************
476 0 : SUBROUTINE send_grid_coordinates(this)
477 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
478 :
479 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_grid_coordinates'
480 :
481 : INTEGER, DIMENSION(3) :: npts_glob, npts, lb_glob, lb, ub
482 : REAL(dp), DIMENSION(3) :: origin
483 : REAL(dp), DIMENSION(3, 3) :: box_vectors
484 0 : REAL(dp), DIMENSION(:, :), ALLOCATABLE :: coords
485 : INTEGER :: handle, i, j, k, offset
486 :
487 0 : CALL timeset(routineN, handle)
488 :
489 : origin = 0.0_dp
490 0 : box_vectors = this%pw_info%pw_grid%dh
491 : ! number of grid points
492 0 : npts_glob = this%pw_info%pw_grid%npts
493 0 : npts = this%pw_info%pw_grid%npts_local
494 : ! bounds
495 0 : lb_glob = this%pw_info%pw_grid%bounds(1, :)
496 0 : lb = this%pw_info%pw_grid%bounds_local(1, :)
497 0 : ub = this%pw_info%pw_grid%bounds_local(2, :)
498 :
499 0 : ALLOCATE (coords(3, PRODUCT(npts_glob)), source=0.0_dp)
500 0 : offset = (lb(1) - lb_glob(1))*PRODUCT(npts(2:))
501 0 : DO k = lb(3), ub(3)
502 0 : DO j = lb(2), ub(2)
503 0 : DO i = lb(1), ub(1)
504 0 : offset = offset + 1
505 : coords(:, offset) = origin + box_vectors(:, 1)*REAL(i - lb_glob(1), dp) &
506 : + box_vectors(:, 2)*REAL(j - lb_glob(2), dp) &
507 0 : + box_vectors(:, 3)*REAL(k - lb_glob(3), dp)
508 : END DO
509 : END DO
510 : END DO
511 0 : CALL this%para_env%sum(coords)
512 :
513 0 : CALL mcl_send(coords, SIZE(coords), MCL_DATA, this%mcl_server)
514 :
515 0 : CALL timestop(handle)
516 :
517 0 : END SUBROUTINE send_grid_coordinates
518 :
519 : ! **************************************************************************************************
520 : !> \brief Receive external potential from the server
521 : !> \param this ...
522 : ! **************************************************************************************************
523 0 : SUBROUTINE receive_potential(this)
524 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
525 :
526 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':receive_potential'
527 :
528 : INTEGER, DIMENSION(3) :: npts, lb, ub
529 0 : REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET :: buffer
530 : REAL(dp), DIMENSION(:), ALLOCATABLE :: buffer_loc
531 0 : REAL(dp), DIMENSION(:), POINTER :: buffer_p
532 : INTEGER :: i, j, k, i_proc, offset
533 : INTEGER :: handle, length, tag
534 :
535 0 : CALL timeset(routineN, handle)
536 :
537 0 : NULLIFY (buffer_p)
538 0 : npts = this%pw_info%pw_grid%npts_local
539 0 : lb = this%pw_info%pw_grid%bounds_local(1, :)
540 0 : ub = this%pw_info%pw_grid%bounds_local(2, :)
541 0 : ALLOCATE (buffer_loc(PRODUCT(npts)))
542 :
543 0 : tag = 1
544 :
545 0 : IF (this%is_ionode) THEN
546 0 : ALLOCATE (buffer(PRODUCT(this%pw_info%pw_grid%npts)))
547 : ! receive potential at the IO process
548 0 : CALL mcl_receive(buffer, SIZE(buffer), MCL_DATA, this%mcl_server)
549 : ! distribute across processes
550 0 : DO i_proc = 0, this%para_env%num_pe - 1
551 0 : length = PRODUCT(this%npts_pproc(:, i_proc))
552 0 : offset = this%lb_pproc(i_proc)*PRODUCT(npts(2:)) + 1
553 0 : buffer_p => buffer(offset:offset + length - 1)
554 0 : IF (i_proc /= this%para_env%source) THEN
555 0 : i = i_proc
556 0 : CALL this%para_env%send(buffer_p, i, tag)
557 : ELSE
558 0 : buffer_loc(:) = buffer_p
559 : END IF
560 : END DO
561 : ELSE
562 0 : CALL this%para_env%recv(buffer_loc, this%para_env%source, tag)
563 : END IF
564 :
565 : ! set the potential
566 0 : offset = 0
567 0 : DO k = lb(3), ub(3)
568 0 : DO j = lb(2), ub(2)
569 0 : DO i = lb(1), ub(1)
570 0 : offset = offset + 1
571 0 : this%potential%array(i, j, k) = -buffer_loc(offset)
572 : END DO
573 : END DO
574 : END DO
575 :
576 0 : CALL timestop(handle)
577 :
578 0 : END SUBROUTINE receive_potential
579 :
580 : ! **************************************************************************************************
581 : !> \brief Send electron density to the server
582 : !> \param this ...
583 : ! **************************************************************************************************
584 0 : SUBROUTINE send_density(this)
585 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
586 :
587 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_density'
588 :
589 : INTEGER, DIMENSION(3) :: npts, lb, ub
590 0 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho
591 0 : REAL(dp), DIMENSION(:), ALLOCATABLE, TARGET :: buffer
592 : REAL(dp), DIMENSION(:), ALLOCATABLE :: buffer_loc
593 0 : REAL(dp), DIMENSION(:), POINTER :: buffer_p
594 : INTEGER :: i_spin, i_proc, i, j, k, offset
595 : INTEGER :: handle, length, tag
596 :
597 0 : CALL timeset(routineN, handle)
598 :
599 0 : NULLIFY (rho, buffer_p)
600 0 : CALL qs_rho_get(this%density, rho_r=rho)
601 0 : npts = this%pw_info%pw_grid%npts_local
602 0 : lb = this%pw_info%pw_grid%bounds_local(1, :)
603 0 : ub = this%pw_info%pw_grid%bounds_local(2, :)
604 0 : ALLOCATE (buffer_loc(PRODUCT(npts)))
605 :
606 : ! gather density values
607 0 : buffer_loc = 0.0_dp
608 0 : DO i_spin = 1, this%n_spins
609 0 : offset = 0
610 0 : DO k = lb(3), ub(3)
611 0 : DO j = lb(2), ub(2)
612 0 : DO i = lb(1), ub(1)
613 0 : offset = offset + 1
614 0 : buffer_loc(offset) = buffer_loc(offset) + rho(i_spin)%array(i, j, k)
615 : END DO
616 : END DO
617 : END DO
618 : END DO
619 :
620 0 : tag = 1
621 :
622 0 : IF (.NOT. this%is_ionode) THEN
623 0 : CALL this%para_env%send(buffer_loc, this%para_env%source, tag)
624 : ELSE
625 0 : ALLOCATE (buffer(PRODUCT(this%pw_info%pw_grid%npts)))
626 : ! collect from the processes at the IO process
627 0 : DO i_proc = 0, this%para_env%num_pe - 1
628 0 : length = PRODUCT(this%npts_pproc(:, i_proc))
629 0 : offset = this%lb_pproc(i_proc)*PRODUCT(npts(2:)) + 1
630 0 : buffer_p => buffer(offset:offset + length - 1)
631 0 : IF (i_proc /= this%para_env%source) THEN
632 0 : i = i_proc
633 0 : CALL this%para_env%recv(buffer_p, i, tag)
634 : ELSE
635 0 : buffer_p = buffer_loc
636 : END IF
637 : END DO
638 : ! send the density
639 0 : CALL mcl_send(buffer, SIZE(buffer), MCL_DATA, this%mcl_server)
640 : END IF
641 :
642 0 : CALL timestop(handle)
643 :
644 0 : END SUBROUTINE send_density
645 :
646 : ! **************************************************************************************************
647 : !> \brief Send positions of all atoms to the server
648 : !> \param this ...
649 : ! **************************************************************************************************
650 0 : SUBROUTINE send_positions(this)
651 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
652 :
653 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_positions'
654 :
655 0 : REAL(dp), DIMENSION(:, :), ALLOCATABLE :: buffer
656 : INTEGER :: handle, i_atom
657 :
658 0 : CALL timeset(routineN, handle)
659 :
660 0 : ALLOCATE (buffer(3, this%n_atoms))
661 0 : DO i_atom = 1, this%n_atoms
662 0 : buffer(:, i_atom) = this%atoms%els(i_atom)%r
663 : END DO
664 0 : CALL mcl_send(buffer, SIZE(buffer), MCL_DATA, this%mcl_server)
665 :
666 0 : CALL timestop(handle)
667 :
668 0 : END SUBROUTINE send_positions
669 :
670 : ! **************************************************************************************************
671 : !> \brief Receive positions of all atoms from the server
672 : !> \param this ...
673 : ! **************************************************************************************************
674 0 : SUBROUTINE receive_positions(this)
675 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
676 :
677 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':receive_positions'
678 :
679 0 : REAL(dp), DIMENSION(:, :), ALLOCATABLE :: buffer
680 : INTEGER :: handle, i_atom
681 :
682 0 : CALL timeset(routineN, handle)
683 :
684 0 : ALLOCATE (buffer(3, this%n_atoms))
685 0 : CALL mcl_receive(buffer, SIZE(buffer), MCL_DATA, this%mcl_server)
686 0 : CALL this%para_env%bcast(buffer)
687 0 : DO i_atom = 1, this%n_atoms
688 0 : this%atoms%els(i_atom)%r = buffer(:, i_atom)
689 : END DO
690 :
691 0 : CALL timestop(handle)
692 :
693 0 : END SUBROUTINE receive_positions
694 :
695 : ! **************************************************************************************************
696 : !> \brief Send QM forces of all atoms to the server
697 : !> \param this ...
698 : ! **************************************************************************************************
699 0 : SUBROUTINE send_forces(this)
700 : CLASS(mimic_communicator_type), INTENT(INOUT) :: this
701 :
702 : CHARACTER(LEN=*), PARAMETER :: routineN = moduleN//':send_forces'
703 :
704 0 : REAL(dp), DIMENSION(:, :), ALLOCATABLE :: buffer
705 : INTEGER :: handle, i_atom
706 :
707 0 : CALL timeset(routineN, handle)
708 :
709 0 : ALLOCATE (buffer(3, this%n_atoms))
710 0 : DO i_atom = 1, this%n_atoms
711 0 : buffer(:, i_atom) = this%atoms%els(i_atom)%f
712 : END DO
713 0 : CALL mcl_send(buffer, SIZE(buffer), MCL_DATA, this%mcl_server)
714 :
715 0 : CALL timestop(handle)
716 :
717 0 : END SUBROUTINE send_forces
718 :
719 0 : END MODULE mimic_communicator
|