28#include "./base/base_uses.f90"
34 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'skala_gpw_features'
35 REAL(KIND=
dp),
PARAMETER,
PRIVATE :: layout_tol = 1.0e-12_dp
36 INTEGER,
PARAMETER,
PRIVATE :: ndynamic_per_point = 10, nrks_dynamic_per_point = 5, &
37 nstatic_per_point = 5, ngrad_per_point = 10
40 REAL(kind=
dp),
PARAMETER,
PRIVATE :: smooth_partition_eps = 1.0e-12_dp
41 REAL(kind=
dp),
PARAMETER,
PRIVATE :: smooth_partition_taper_end = &
42 10.0_dp*smooth_partition_eps
43 INTEGER,
PARAMETER,
PRIVATE :: periodic_partition_image_shell = 1
53 TYPE skala_gpw_layout_cache_type
54 INTEGER :: chunk_atom_begin = 1, chunk_atom_end = 0, &
55 chunk_feature_begin = 1, &
56 chunk_feature_count = 0, chunk_natom = 0, &
57 natom = 0, nflat = 0, nflat_local = 0, &
58 npoint = 0, nproc = 0, &
60 INTEGER,
DIMENSION(2, 3) :: bo = 0, bounds = 0
61 INTEGER,
DIMENSION(3) :: npts = 0
62 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: dynamic_counts, dynamic_displs, &
63 chunk_feature_counts, chunk_feature_displs, &
64 chunk_grad_counts, chunk_grad_displs, &
65 feature_counts, feature_displs, &
66 feature_source_points, global_to_feature, &
67 local_feature_counts, local_feature_offsets, &
68 local_feature_points, local_feature_rows, &
69 chunk_return_positions, route_chunk_offsets, &
70 route_chunk_rows, route_row_offsets, &
71 route_row_positions, route_send_local_rows, &
72 route_point_recv_counts, &
73 route_point_recv_displs, &
74 route_point_send_counts, &
75 route_point_send_displs
76 INTEGER,
ALLOCATABLE,
DIMENSION(:, :, :) :: feature_index
77 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:) :: atomic_grid_sizes, chunk_atomic_grid_sizes, &
79 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:) :: local_feature_indices
80 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:, :) :: atomic_grid_size_bound_shape, &
81 chunk_atomic_grid_size_bound_shape
109 REAL(kind=
dp) :: dvol = 0.0_dp, weight_sum = 0.0_dp, &
110 weight_sumsq = 0.0_dp
111 REAL(kind=
dp),
DIMENSION(3, 3) :: cell_hmat = 0.0_dp, dh = 0.0_dp
112 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: atomic_grid_weights, chunk_atomic_grid_weights, &
113 chunk_grid_weights, grid_weights
114 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: atom_coords, chunk_coarse_0_atomic_coords, &
115 chunk_grid_coords, coarse_0_atomic_coords, &
117 LOGICAL :: active = .false., has_weights = .false., &
118 chunk_dynamic_input_views_active = .false., &
119 chunk_dynamic_tensors_active = .false., &
120 chunk_inputs_active = .false., &
121 chunk_inputs_use_collapsed_rks = .false., &
122 chunk_static_tensors_active = .false., &
123 dynamic_tensors_active = .false., &
124 inputs_active = .false., &
125 static_tensors_active = .false.
126 END TYPE skala_gpw_layout_cache_type
129 INTEGER :: chunk_feature_count = 0, nflat = 0, &
146 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: chunk_grad_counts, chunk_grad_displs, &
147 local_feature_counts, local_feature_offsets, &
149 INTEGER,
POINTER,
DIMENSION(:) :: route_point_recv_counts => null(), &
150 route_point_recv_displs => null(), &
151 route_point_send_counts => null(), &
152 route_point_send_displs => null(), &
153 route_chunk_offsets => null(), &
154 route_chunk_rows => null(), &
155 route_row_offsets => null(), &
156 route_row_positions => null(), &
157 route_send_local_rows => null()
158 INTEGER,
ALLOCATABLE,
DIMENSION(:, :, :) :: feature_index
159 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:) :: atomic_grid_sizes
160 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:, :) :: atomic_grid_size_bound_shape
161 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: atomic_grid_weights, grid_weights
162 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: chunk_density, chunk_kin, &
163 coarse_0_atomic_coords, density, &
165 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: chunk_grad, grad
166 REAL(kind=
dp) :: electron_count = 0.0_dp, &
167 grid_weight_sum = 0.0_dp, &
168 kinetic_integral = 0.0_dp, &
170 LOGICAL :: active = .false., owns_coordinate_tensor = .false., &
171 owns_grid_coordinate_tensor = .false., &
172 owns_weight_tensors = .false., &
173 owns_dynamic_tensors = .true., &
174 owns_inputs = .true., &
175 owns_static_tensors = .true., &
176 uses_atom_chunk_routing = .false., &
177 uses_atom_chunks = .false., &
178 uses_collapsed_rks_dynamic = .false.
201 requires_grad, weights, requires_coordinate_grad, &
202 requires_stress_grad, use_atom_chunks, route_atom_chunks, &
209 LOGICAL,
INTENT(IN),
OPTIONAL :: requires_grad
211 LOGICAL,
INTENT(IN),
OPTIONAL :: requires_coordinate_grad, &
212 requires_stress_grad, use_atom_chunks, &
214 INTEGER,
INTENT(IN),
OPTIONAL :: atom_partition
216 INTEGER :: handle, i, ipt, ispin, j, k, local_row, my_atom_partition, &
217 ndynamic_local_per_point, nflat, nflat_local, nspins, phase_handle, real_base, row
218 INTEGER,
DIMENSION(2, 3) :: bo
219 LOGICAL :: collapse_spin_dynamics, my_requires_coordinate_grad, my_requires_grad, &
220 my_requires_stress_grad, my_route_atom_chunks, my_use_atom_chunks, &
221 use_atom_chunk_protocol, use_atom_chunk_routing
222 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: global_dynamic, local_dynamic
223 REAL(kind=
dp),
DIMENSION(:, :, :),
POINTER :: rho, rhoa, rhob, tau_a, tau_b, tau_total
227 CALL timeset(
"skala_gpw_feature_build", handle)
229 my_requires_grad = .false.
230 IF (
PRESENT(requires_grad)) my_requires_grad = requires_grad
231 my_requires_coordinate_grad = .false.
232 IF (
PRESENT(requires_coordinate_grad))
THEN
233 my_requires_coordinate_grad = requires_coordinate_grad
235 my_requires_stress_grad = .false.
236 IF (
PRESENT(requires_stress_grad)) my_requires_stress_grad = requires_stress_grad
237 my_use_atom_chunks = .false.
238 IF (
PRESENT(use_atom_chunks)) my_use_atom_chunks = use_atom_chunks
239 my_route_atom_chunks = .false.
240 IF (
PRESENT(route_atom_chunks)) my_route_atom_chunks = route_atom_chunks
242 IF (
PRESENT(atom_partition)) my_atom_partition = atom_partition
245 CALL cp_abort(__location__,
"Unknown native SKALA atom-partition mode.")
247 cpassert(
ASSOCIATED(cell))
248 cpassert(
ASSOCIATED(particle_set))
249 cpassert(
SIZE(rho_r) == 1 .OR.
SIZE(rho_r) == 2)
250 cpassert(
ASSOCIATED(rho_r(1)%pw_grid))
251 pw_grid => rho_r(1)%pw_grid
254 bo = pw_grid%bounds_local
255 nflat_local = pw_grid%ngpts_local
257 CALL timeset(
"skala_gpw_pre_release", phase_handle)
259 CALL timestop(phase_handle)
261 use_atom_chunk_protocol = my_use_atom_chunks .AND. &
262 .NOT. (my_requires_coordinate_grad .OR. my_requires_stress_grad)
263 use_atom_chunk_routing = use_atom_chunk_protocol .AND. my_route_atom_chunks
264 CALL timeset(
"skala_gpw_layout_cache", phase_handle)
265 CALL ensure_layout_cache(pw_grid, particle_set, cell, weights, my_atom_partition, &
266 needs_full_static_tensors=.NOT. use_atom_chunk_protocol)
267 CALL timestop(phase_handle)
269 collapse_spin_dynamics = nspins == 1 .AND. use_atom_chunk_routing
270 ndynamic_local_per_point = ndynamic_per_point
271 IF (collapse_spin_dynamics) ndynamic_local_per_point = nrks_dynamic_per_point
272 ALLOCATE (local_dynamic(ndynamic_local_per_point*nflat_local))
273 local_dynamic = 0.0_dp
275 CALL timeset(
"skala_gpw_pack_local", phase_handle)
276 IF (nspins == 1)
THEN
279 CALL xc_rho_set_get(rho_set, rhoa=rhoa, rhob=rhob, drhoa=drhoa, drhob=drhob, &
280 tau_a=tau_a, tau_b=tau_b)
284 DO k = bo(1, 3), bo(2, 3)
285 DO j = bo(1, 2), bo(2, 2)
286 DO i = bo(1, 1), bo(2, 1)
287 local_row = local_row + 1
288 real_base = ndynamic_local_per_point*(local_row - 1)
290 IF (nspins == 1)
THEN
291 IF (collapse_spin_dynamics)
THEN
292 local_dynamic(real_base + 1) = 0.5_dp*rho(i, j, k)
293 local_dynamic(real_base + 2) = 0.5_dp*drho(1)%array(i, j, k)
294 local_dynamic(real_base + 3) = 0.5_dp*drho(2)%array(i, j, k)
295 local_dynamic(real_base + 4) = 0.5_dp*drho(3)%array(i, j, k)
296 local_dynamic(real_base + 5) = 0.5_dp*tau_total(i, j, k)
298 local_dynamic(real_base + 1) = 0.5_dp*rho(i, j, k)
299 local_dynamic(real_base + 2) = 0.5_dp*rho(i, j, k)
301 local_dynamic(real_base + 2 + 3*(ispin - 1) + 1) = &
302 0.5_dp*drho(1)%array(i, j, k)
303 local_dynamic(real_base + 2 + 3*(ispin - 1) + 2) = &
304 0.5_dp*drho(2)%array(i, j, k)
305 local_dynamic(real_base + 2 + 3*(ispin - 1) + 3) = &
306 0.5_dp*drho(3)%array(i, j, k)
307 local_dynamic(real_base + 8 + ispin) = 0.5_dp*tau_total(i, j, k)
311 local_dynamic(real_base + 1) = rhoa(i, j, k)
312 local_dynamic(real_base + 2) = rhob(i, j, k)
313 local_dynamic(real_base + 3) = drhoa(1)%array(i, j, k)
314 local_dynamic(real_base + 4) = drhoa(2)%array(i, j, k)
315 local_dynamic(real_base + 5) = drhoa(3)%array(i, j, k)
316 local_dynamic(real_base + 6) = drhob(1)%array(i, j, k)
317 local_dynamic(real_base + 7) = drhob(2)%array(i, j, k)
318 local_dynamic(real_base + 8) = drhob(3)%array(i, j, k)
319 local_dynamic(real_base + 9) = tau_a(i, j, k)
320 local_dynamic(real_base + 10) = tau_b(i, j, k)
325 CALL timestop(phase_handle)
327 CALL timeset(
"skala_gpw_copy_layout", phase_handle)
328 CALL copy_cached_layout(features, use_atom_chunk_routing, &
329 my_requires_coordinate_grad .OR. my_requires_stress_grad, &
330 my_requires_stress_grad .OR. &
332 (my_requires_coordinate_grad .OR. my_requires_stress_grad)))
333 CALL timestop(phase_handle)
335 IF (use_atom_chunk_routing)
THEN
336 CALL timeset(
"skala_gpw_route_dyn", phase_handle)
337 CALL route_atom_chunk_dynamics(features, local_dynamic, pw_grid%para%group, &
338 collapse_spin_dynamics)
339 features%uses_atom_chunk_routing = .true.
340 features%uses_atom_chunks = .true.
341 CALL timestop(phase_handle)
343 ALLOCATE (global_dynamic(ndynamic_per_point*
cached_layout%npoint))
344 CALL timeset(
"skala_gpw_allgatherv", phase_handle)
345 CALL pw_grid%para%group%allgatherv(local_dynamic, global_dynamic, &
348 CALL timestop(phase_handle)
350 CALL timeset(
"skala_gpw_reorder_dyn", phase_handle)
351 ALLOCATE (features%density(nflat, 2), features%grad(nflat, 3, 2), &
352 features%kin(nflat, 2))
353 features%density = 0.0_dp
354 features%grad = 0.0_dp
355 features%kin = 0.0_dp
359 real_base = ndynamic_per_point*(ipt - 1)
360 features%density(row, :) = global_dynamic(real_base + 1:real_base + 2)
361 features%grad(row, 1, 1) = global_dynamic(real_base + 3)
362 features%grad(row, 2, 1) = global_dynamic(real_base + 4)
363 features%grad(row, 3, 1) = global_dynamic(real_base + 5)
364 features%grad(row, 1, 2) = global_dynamic(real_base + 6)
365 features%grad(row, 2, 2) = global_dynamic(real_base + 7)
366 features%grad(row, 3, 2) = global_dynamic(real_base + 8)
367 features%kin(row, :) = global_dynamic(real_base + 9:real_base + 10)
369 CALL timestop(phase_handle)
372 CALL timeset(
"skala_gpw_feature_sums", phase_handle)
373 IF (features%uses_atom_chunks)
THEN
374 features%electron_count = 0.0_dp
375 features%kinetic_integral = 0.0_dp
376 features%spin_moment = 0.0_dp
377 IF (features%chunk_feature_count > 0)
THEN
378 IF (features%uses_collapsed_rks_dynamic)
THEN
379 features%electron_count = sum(2.0_dp*features%chunk_density(:, 1)* &
381 features%kinetic_integral = sum(2.0_dp*features%chunk_kin(:, 1)* &
384 features%electron_count = sum((features%chunk_density(:, 1) + &
385 features%chunk_density(:, 2))* &
387 features%kinetic_integral = sum((features%chunk_kin(:, 1) + &
388 features%chunk_kin(:, 2))* &
390 features%spin_moment = sum((features%chunk_density(:, 1) - &
391 features%chunk_density(:, 2))* &
395 CALL pw_grid%para%group%sum(features%electron_count)
396 CALL pw_grid%para%group%sum(features%kinetic_integral)
397 CALL pw_grid%para%group%sum(features%spin_moment)
399 features%electron_count = sum((features%density(:, 1) + features%density(:, 2))* &
400 features%grid_weights)
401 features%kinetic_integral = sum((features%kin(:, 1) + features%kin(:, 2))* &
402 features%grid_weights)
403 features%spin_moment = sum((features%density(:, 1) - features%density(:, 2))* &
404 features%grid_weights)
406 IF (use_atom_chunk_routing)
THEN
409 features%grid_weight_sum = sum(features%grid_weights)
411 CALL timestop(phase_handle)
413 CALL timeset(
"skala_gpw_tensor_update", phase_handle)
414 IF (use_atom_chunk_protocol .AND. .NOT. features%uses_atom_chunks)
THEN
415 IF (features%chunk_feature_count > 0)
CALL extract_atom_chunk_dynamics(features)
416 features%uses_atom_chunks = .true.
418 IF (.NOT. features%uses_atom_chunks .OR. features%chunk_feature_count > 0)
THEN
419 CALL add_feature_tensors(features, my_requires_grad, my_requires_coordinate_grad, &
420 my_requires_stress_grad, &
421 features%uses_atom_chunks, &
422 requires_weight_grad= &
424 (my_requires_coordinate_grad .OR. my_requires_stress_grad)))
427 features%owns_coordinate_tensor = .false.
428 features%owns_grid_coordinate_tensor = .false.
429 features%owns_weight_tensors = .false.
430 features%owns_dynamic_tensors = .false.
431 features%owns_inputs = .false.
432 features%owns_static_tensors = .false.
434 CALL timestop(phase_handle)
435 features%active = .true.
437 IF (
ALLOCATED(global_dynamic))
DEALLOCATE (global_dynamic)
438 DEALLOCATE (local_dynamic)
439 CALL timestop(handle)
452 SUBROUTINE ensure_layout_cache(pw_grid, particle_set, cell, weights, atom_partition, &
453 needs_full_static_tensors)
458 INTEGER,
INTENT(IN),
OPTIONAL :: atom_partition
459 LOGICAL,
INTENT(IN) :: needs_full_static_tensors
461 INTEGER :: my_atom_partition, phase_handle
462 LOGICAL :: cache_matches
465 IF (
PRESENT(atom_partition)) my_atom_partition = atom_partition
466 IF (
PRESENT(weights))
THEN
467 CALL timeset(
"skala_gpw_layout_match", phase_handle)
468 cache_matches = layout_cache_matches(pw_grid, particle_set, cell, weights, &
470 CALL timestop(phase_handle)
472 CALL timeset(
"skala_gpw_layout_match", phase_handle)
473 cache_matches = layout_cache_matches(pw_grid, particle_set, cell, &
474 atom_partition=my_atom_partition)
475 CALL timestop(phase_handle)
478 IF (cache_matches)
THEN
479 IF (needs_full_static_tensors .AND. .NOT.
cached_layout%static_tensors_active)
THEN
480 CALL timeset(
"skala_gpw_layout_tensors", phase_handle)
482 CALL timestop(phase_handle)
487 CALL timeset(
"skala_gpw_layout_rebuild", phase_handle)
488 IF (
PRESENT(weights))
THEN
489 CALL rebuild_layout_cache(pw_grid, particle_set, cell, weights, my_atom_partition, &
490 needs_full_static_tensors)
492 CALL rebuild_layout_cache(pw_grid, particle_set, cell, &
493 atom_partition=my_atom_partition, &
494 needs_full_static_tensors=needs_full_static_tensors)
496 CALL timestop(phase_handle)
498 END SUBROUTINE ensure_layout_cache
509 FUNCTION layout_cache_matches(pw_grid, particle_set, cell, weights, atom_partition)
RESULT(matches)
514 INTEGER,
INTENT(IN),
OPTIONAL :: atom_partition
517 INTEGER :: iatom, my_atom_partition
518 LOGICAL :: weights_match
521 IF (
PRESENT(atom_partition)) my_atom_partition = atom_partition
524 IF (
cached_layout%atom_partition /= my_atom_partition)
RETURN
531 IF (abs(
cached_layout%dvol - pw_grid%dvol) > layout_tol)
RETURN
532 IF (any(abs(
cached_layout%dh - pw_grid%dh) > layout_tol))
RETURN
533 IF (any(abs(
cached_layout%cell_hmat - cell%hmat) > layout_tol))
RETURN
536 DO iatom = 1,
SIZE(particle_set)
537 IF (any(abs(
cached_layout%atom_coords(:, iatom) - particle_set(iatom)%r) > layout_tol))
RETURN
540 IF (
PRESENT(weights))
THEN
541 weights_match = layout_weights_match(pw_grid, weights)
543 weights_match = layout_weights_match(pw_grid)
545 IF (.NOT. weights_match)
RETURN
549 END FUNCTION layout_cache_matches
557 FUNCTION layout_weights_match(pw_grid, weights)
RESULT(matches)
562 LOGICAL :: has_weights
563 REAL(kind=
dp) :: weight_sum, weight_sumsq
567 IF (
PRESENT(weights))
THEN
568 CALL weights_signature(weights, has_weights, weight_sum, weight_sumsq)
570 CALL weights_signature(has_weights=has_weights, weight_sum=weight_sum, &
571 weight_sumsq=weight_sumsq)
575 IF (abs(
cached_layout%weight_sum - weight_sum) > layout_tol)
RETURN
576 IF (abs(
cached_layout%weight_sumsq - weight_sumsq) > layout_tol)
RETURN
580 END FUNCTION layout_weights_match
591 SUBROUTINE rebuild_layout_cache(pw_grid, particle_set, cell, weights, atom_partition, &
592 needs_full_static_tensors)
597 INTEGER,
INTENT(IN),
OPTIONAL :: atom_partition
598 LOGICAL,
INTENT(IN) :: needs_full_static_tensors
600 INTEGER :: feature_local, feature_slot, i, iatom, ipt, j, jatom, k, local_feature, &
601 local_row, max_grid_size, max_local_features, my_atom_partition, natom, nfeature_local, &
602 nflat, nflat_local, npoint, nproc, nx, ny, owner, pe, pe_index, phase_handle, row, &
603 source_global, source_local, static_base
604 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: atom_offset, atom_position, chunk_atom_begin, &
605 chunk_atom_end, cursor, feature_counts, feature_displs, global_owner, &
606 global_source_points, local_feature_counts_tmp, local_owner, local_source_global, &
607 local_source_points, point_counts, point_displs, static_counts, static_displs
608 INTEGER,
DIMENSION(2, 3) :: bo
609 LOGICAL :: has_weights, nonperiodic
610 REAL(kind=
dp) :: atomic_base_weight, base_weight, &
611 included_sum, partition_weight, &
612 weight_sum, weight_sumsq
613 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: distances, global_static, local_static, &
615 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: atom_coords_pbc, pair_distances, &
616 partition_atom_coords
617 REAL(kind=
dp),
DIMENSION(3) :: grid_point
622 IF (
PRESENT(atom_partition)) my_atom_partition = atom_partition
623 natom =
SIZE(particle_set)
624 bo = pw_grid%bounds_local
625 nflat_local = pw_grid%ngpts_local
626 nproc = pw_grid%para%group%num_pe
627 pe_index = pw_grid%para%group%mepos + 1
629 IF (
PRESENT(weights))
THEN
630 CALL weights_signature(weights, has_weights, weight_sum, weight_sumsq)
632 CALL weights_signature(has_weights=has_weights, weight_sum=weight_sum, &
633 weight_sumsq=weight_sumsq)
636 max_local_features = nflat_local
638 max_local_features = nflat_local*natom
640 ALLOCATE (local_owner(max_local_features), &
641 local_source_points(max_local_features), &
642 local_static(nstatic_per_point*max_local_features), &
643 local_feature_counts_tmp(nflat_local), feature_counts(nproc), &
644 feature_displs(nproc), point_counts(nproc), point_displs(nproc), &
645 static_counts(nproc), static_displs(nproc), atom_coords_pbc(3, natom))
650 local_feature_counts_tmp = 0
652 atom_coords_pbc(:, iatom) =
pbc(particle_set(iatom)%r, cell, positive_range=.true.)
654 nonperiodic = all(cell%perd == 0)
656 ALLOCATE (pair_distances(natom, natom))
657 pair_distances = 0.0_dp
658 DO iatom = 1, natom - 1
659 DO jatom = iatom + 1, natom
660 pair_distances(iatom, jatom) = &
661 sqrt(sum((atom_coords_pbc(:, iatom) - atom_coords_pbc(:, jatom))**2))
662 pair_distances(jatom, iatom) = pair_distances(iatom, jatom)
666 ALLOCATE (pair_distances(0, 0))
669 CALL timeset(
"skala_gpw_layout_local", phase_handle)
670 nx = bo(2, 1) - bo(1, 1) + 1
671 ny = bo(2, 2) - bo(1, 2) + 1
673 nfeature_local = nflat_local
678 DO k = bo(1, 3), bo(2, 3)
679 DO j = bo(1, 2), bo(2, 2)
680 DO i = bo(1, 1), bo(2, 1)
681 local_row = i - bo(1, 1) + 1 + &
682 nx*(j - bo(1, 2) + ny*(k - bo(1, 3)))
683 grid_point = grid_coordinate(pw_grid, [i, j, k])
684 atomic_base_weight = pw_grid%dvol
685 base_weight = pw_grid%dvol
686 IF (
PRESENT(weights))
THEN
687 IF (
ASSOCIATED(weights)) base_weight = base_weight*weights%array(i, j, k)
690 owner = nearest_atom(grid_point, atom_coords_pbc, cell)
691 local_feature_counts_tmp(local_row) = 1
692 local_owner(local_row) = owner
693 local_source_points(local_row) = local_row
694 static_base = nstatic_per_point*(local_row - 1)
695 local_static(static_base + 1:static_base + 3) = grid_point
696 local_static(static_base + 4) = base_weight
697 local_static(static_base + 5) = atomic_base_weight
711 ALLOCATE (distances(natom), partition_atom_coords(3, natom), partition_weights(natom))
713 DO k = bo(1, 3), bo(2, 3)
714 DO j = bo(1, 2), bo(2, 2)
715 DO i = bo(1, 1), bo(2, 1)
716 local_row = i - bo(1, 1) + 1 + &
717 nx*(j - bo(1, 2) + ny*(k - bo(1, 3)))
718 grid_point = grid_coordinate(pw_grid, [i, j, k])
719 atomic_base_weight = pw_grid%dvol
720 base_weight = pw_grid%dvol
721 IF (
PRESENT(weights))
THEN
722 IF (
ASSOCIATED(weights)) base_weight = base_weight*weights%array(i, j, k)
725 IF (nonperiodic)
THEN
727 partition_weights, partition_atom_coords, distances, &
731 partition_weights, partition_atom_coords, distances)
733 included_sum = sum(partition_weights, mask=partition_weights > smooth_partition_eps)
734 IF (included_sum <= 0.0_dp)
THEN
735 owner = nearest_atom(grid_point, atom_coords_pbc, cell)
736 partition_weights = 0.0_dp
737 partition_weights(owner) = 1.0_dp
738 included_sum = 1.0_dp
742 IF (partition_weights(iatom) <= smooth_partition_eps) cycle
743 partition_weight = partition_weights(iatom)/included_sum
744 local_feature = local_feature + 1
745 feature_slot = natom*(local_row - 1) + local_feature
746 local_owner(feature_slot) = iatom
747 local_source_points(feature_slot) = local_row
748 static_base = nstatic_per_point*(feature_slot - 1)
749 local_static(static_base + 1:static_base + 3) = grid_point
750 local_static(static_base + 4) = base_weight*partition_weight
751 local_static(static_base + 5) = atomic_base_weight* &
755 local_feature_counts_tmp(local_row) = local_feature
760 DEALLOCATE (distances, partition_atom_coords, partition_weights)
765 DO local_row = 1, nflat_local
766 cpassert(local_feature_counts_tmp(local_row) > 0)
767 DO local_feature = 1, local_feature_counts_tmp(local_row)
768 nfeature_local = nfeature_local + 1
769 feature_slot = natom*(local_row - 1) + local_feature
770 IF (feature_slot == nfeature_local) cycle
771 local_owner(nfeature_local) = local_owner(feature_slot)
772 local_source_points(nfeature_local) = local_source_points(feature_slot)
773 local_static(nstatic_per_point*(nfeature_local - 1) + 1: &
774 nstatic_per_point*nfeature_local) = &
775 local_static(nstatic_per_point*(feature_slot - 1) + 1: &
776 nstatic_per_point*feature_slot)
780 CALL timestop(phase_handle)
784 CALL timeset(
"skala_gpw_layout_gather", phase_handle)
785 CALL pw_grid%para%group%allgather(nflat_local, point_counts)
786 CALL counts_to_displs(point_counts, point_displs)
787 npoint = sum(point_counts)
788 CALL pw_grid%para%group%allgather(nfeature_local, feature_counts)
789 CALL counts_to_displs(feature_counts, feature_displs)
791 static_counts(pe) = nstatic_per_point*feature_counts(pe)
792 static_displs(pe) = nstatic_per_point*feature_displs(pe)
794 nflat = sum(feature_counts)
795 ALLOCATE (global_owner(nflat), global_source_points(nflat), &
796 global_static(nstatic_per_point*nflat), local_source_global(nfeature_local))
797 DO feature_local = 1, nfeature_local
798 local_source_global(feature_local) = point_displs(pe_index) + local_source_points(feature_local)
800 CALL pw_grid%para%group%allgatherv(local_owner(1:nfeature_local), global_owner, feature_counts, &
802 CALL pw_grid%para%group%allgatherv(local_source_global, global_source_points, feature_counts, &
804 CALL pw_grid%para%group%allgatherv(local_static(1:nstatic_per_point*nfeature_local), &
805 global_static, static_counts, &
807 CALL timestop(phase_handle)
824 cached_layout%local_feature_indices(nfeature_local), atom_offset(natom + 1), &
825 atom_position(natom), chunk_atom_begin(nproc), chunk_atom_end(nproc), &
829 cached_layout%dynamic_counts(:) = ndynamic_per_point*point_counts
830 cached_layout%dynamic_displs(:) = ndynamic_per_point*point_displs
833 cached_layout%local_feature_counts(:) = local_feature_counts_tmp
835 DO local_row = 1, nflat_local
840 cursor(:) =
cached_layout%local_feature_offsets(1:nflat_local)
842 CALL timeset(
"skala_gpw_layout_atom_sort", phase_handle)
849 atom_offset(iatom + 1) = atom_offset(iatom) + int(
cached_layout%atomic_grid_sizes(iatom))
852 atom_position(iatom) = atom_offset(iatom)
855 CALL build_atom_chunks(
cached_layout%atomic_grid_sizes, atom_offset, nproc, &
856 chunk_atom_begin, chunk_atom_end, &
871 cached_layout%atomic_grid_size_bound_shape(0, max_grid_size), &
880 cached_layout%coarse_0_atomic_coords(:, iatom) = atom_coords_pbc(:, iatom)
884 owner = global_owner(ipt)
885 row = atom_position(owner)
886 atom_position(owner) = atom_position(owner) + 1
887 source_global = global_source_points(ipt)
889 IF (
cached_layout%global_to_feature(source_global) == 0)
THEN
892 static_base = nstatic_per_point*(ipt - 1)
893 cached_layout%grid_coords(:, row) = global_static(static_base + 1:static_base + 3)
894 cached_layout%grid_weights(row) = global_static(static_base + 4)
895 cached_layout%atomic_grid_weights(row) = global_static(static_base + 5)
896 source_local = source_global - point_displs(pe_index)
897 IF (source_local >= 1 .AND. source_local <= nflat_local)
THEN
898 feature_local = cursor(source_local)
899 cursor(source_local) = cursor(source_local) + 1
901 cached_layout%local_feature_points(feature_local) = source_local
908 DO k = bo(1, 3), bo(2, 3)
909 DO j = bo(1, 2), bo(2, 2)
910 DO i = bo(1, 1), bo(2, 1)
917 DO feature_local = 1, nfeature_local
921 CALL timestop(phase_handle)
922 CALL timeset(
"skala_gpw_layout_chunk_routes", phase_handle)
926 CALL timestop(phase_handle)
943 CALL timeset(
"skala_gpw_layout_tensors", phase_handle)
944 IF (needs_full_static_tensors)
CALL build_full_static_layout_tensors(
cached_layout)
948 CALL timestop(phase_handle)
951 DEALLOCATE (atom_coords_pbc, atom_offset, atom_position, &
952 chunk_atom_begin, chunk_atom_end, cursor, feature_counts, feature_displs, &
953 global_owner, global_source_points, global_static, local_feature_counts_tmp, &
954 local_owner, local_source_global, local_source_points, &
955 local_static, pair_distances, point_counts, point_displs, static_counts, &
958 END SUBROUTINE rebuild_layout_cache
964 SUBROUTINE build_full_static_layout_tensors(cache)
965 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
967 cpassert(.NOT. cache%static_tensors_active)
980 cache%atomic_grid_size_bound_shape)
987 CALL torch_dict_insert(cache%static_inputs,
"grid_weights", cache%grid_weights_t)
989 cache%atomic_grid_weights_t)
991 cache%atomic_grid_sizes_t)
993 cache%atomic_grid_size_bound_shape_t)
994 cache%static_tensors_active = .true.
996 END SUBROUTINE build_full_static_layout_tensors
1002 SUBROUTINE build_chunk_static_layout_tensors(cache)
1003 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
1005 cpassert(.NOT. cache%chunk_static_tensors_active)
1011 cache%chunk_atomic_grid_weights)
1014 cache%chunk_atomic_grid_sizes)
1017 cache%chunk_coarse_0_atomic_coords)
1020 cache%chunk_atomic_grid_size_bound_shape)
1027 cache%chunk_grid_coords_t)
1029 cache%chunk_grid_weights_t)
1031 cache%chunk_atomic_grid_weights_t)
1033 cache%chunk_atomic_grid_sizes_t)
1034 CALL torch_dict_insert(cache%chunk_static_inputs,
"atomic_grid_size_bound_shape", &
1035 cache%chunk_atomic_grid_size_bound_shape_t)
1036 cache%chunk_static_tensors_active = .true.
1038 END SUBROUTINE build_chunk_static_layout_tensors
1047 SUBROUTINE copy_cached_layout(features, use_atom_chunk_routing, needs_coordinate_array, &
1048 needs_grid_coordinate_array)
1050 LOGICAL,
INTENT(IN) :: use_atom_chunk_routing, &
1051 needs_coordinate_array, &
1052 needs_grid_coordinate_array
1056 IF (.NOT. use_atom_chunk_routing)
THEN
1057 ALLOCATE (features%feature_index(lbound(
cached_layout%feature_index, 1): &
1064 ALLOCATE (features%local_feature_counts(
cached_layout%nflat_local), &
1065 features%local_feature_offsets(
cached_layout%nflat_local + 1), &
1066 features%local_feature_rows(
SIZE(
cached_layout%local_feature_rows)))
1068 features%feature_index(:, :, :) =
cached_layout%feature_index
1070 features%local_feature_counts(:) =
cached_layout%local_feature_counts
1071 features%local_feature_offsets(:) =
cached_layout%local_feature_offsets
1072 features%local_feature_rows(:) =
cached_layout%local_feature_rows
1076 features%chunk_feature_count =
cached_layout%chunk_feature_count
1079 features%atomic_grid_sizes(:) =
cached_layout%atomic_grid_sizes
1080 IF (needs_grid_coordinate_array)
THEN
1082 ALLOCATE (features%atomic_grid_weights(
cached_layout%nflat))
1084 features%atomic_grid_weights(:) =
cached_layout%atomic_grid_weights
1086 ALLOCATE (features%chunk_grad_counts(
cached_layout%nproc), &
1088 features%chunk_grad_counts(:) =
cached_layout%chunk_grad_counts
1089 features%chunk_grad_displs(:) =
cached_layout%chunk_grad_displs
1090 IF (use_atom_chunk_routing)
THEN
1091 features%route_point_recv_counts =>
cached_layout%route_point_recv_counts
1092 features%route_point_recv_displs =>
cached_layout%route_point_recv_displs
1093 features%route_point_send_counts =>
cached_layout%route_point_send_counts
1094 features%route_point_send_displs =>
cached_layout%route_point_send_displs
1095 features%route_chunk_offsets =>
cached_layout%route_chunk_offsets
1097 features%route_row_offsets =>
cached_layout%route_row_offsets
1098 features%route_row_positions =>
cached_layout%route_row_positions
1099 features%route_send_local_rows =>
cached_layout%route_send_local_rows
1101 IF (needs_coordinate_array)
THEN
1102 ALLOCATE (features%coarse_0_atomic_coords(3,
cached_layout%natom))
1103 features%coarse_0_atomic_coords(:, :) =
cached_layout%coarse_0_atomic_coords
1106 END SUBROUTINE copy_cached_layout
1118 SUBROUTINE build_atom_chunks(atomic_grid_sizes, atom_offset, nproc, chunk_atom_begin, &
1119 chunk_atom_end, chunk_feature_counts, chunk_feature_displs)
1120 INTEGER(KIND=int_8),
DIMENSION(:),
INTENT(IN) :: atomic_grid_sizes
1121 INTEGER,
DIMENSION(:),
INTENT(IN) :: atom_offset
1122 INTEGER,
INTENT(IN) :: nproc
1123 INTEGER,
DIMENSION(:),
INTENT(OUT) :: chunk_atom_begin, chunk_atom_end, &
1124 chunk_feature_counts, &
1125 chunk_feature_displs
1127 INTEGER :: best_limit, count, displ, end_atom, lower_limit, max_end_atom, midpoint, natom, &
1128 next_atom, next_count, pe, ranks_left, target_chunks, total_count, upper_limit
1130 natom =
SIZE(atomic_grid_sizes)
1131 chunk_atom_begin = natom + 1
1132 chunk_atom_end = natom
1133 chunk_feature_counts = 0
1134 chunk_feature_displs = 0
1135 IF (natom == 0)
RETURN
1137 target_chunks = min(nproc, natom)
1138 total_count = atom_offset(natom + 1) - 1
1139 lower_limit = maxval(int(atomic_grid_sizes))
1140 lower_limit = max(lower_limit, (total_count + target_chunks - 1)/target_chunks)
1141 upper_limit = total_count
1142 best_limit = upper_limit
1143 DO WHILE (lower_limit <= upper_limit)
1144 midpoint = (lower_limit + upper_limit)/2
1145 IF (atom_chunks_fit_limit(atomic_grid_sizes, midpoint, target_chunks))
THEN
1146 best_limit = midpoint
1147 upper_limit = midpoint - 1
1149 lower_limit = midpoint + 1
1156 chunk_feature_displs(pe) = displ
1157 IF (pe > target_chunks .OR. next_atom > natom) cycle
1159 ranks_left = target_chunks - pe + 1
1160 chunk_atom_begin(pe) = next_atom
1161 max_end_atom = natom - ranks_left + 1
1162 end_atom = next_atom
1163 count = int(atomic_grid_sizes(end_atom))
1164 DO WHILE (end_atom < max_end_atom)
1165 next_count = count + int(atomic_grid_sizes(end_atom + 1))
1166 IF (next_count > best_limit)
EXIT
1167 end_atom = end_atom + 1
1171 chunk_atom_end(pe) = end_atom
1172 chunk_feature_counts(pe) = atom_offset(end_atom + 1) - atom_offset(next_atom)
1173 displ = displ + chunk_feature_counts(pe)
1174 next_atom = end_atom + 1
1177 cpassert(displ == atom_offset(natom + 1) - 1)
1179 END SUBROUTINE build_atom_chunks
1188 FUNCTION atom_chunks_fit_limit(atomic_grid_sizes, limit, nchunks)
RESULT(fits)
1189 INTEGER(KIND=int_8),
DIMENSION(:),
INTENT(IN) :: atomic_grid_sizes
1190 INTEGER,
INTENT(IN) :: limit, nchunks
1193 INTEGER :: atom_count, chunk_count, iatom, &
1197 IF (
SIZE(atomic_grid_sizes) == 0)
THEN
1204 DO iatom = 1,
SIZE(atomic_grid_sizes)
1205 atom_count = int(atomic_grid_sizes(iatom))
1206 IF (atom_count > limit)
RETURN
1207 IF (chunk_count + atom_count > limit)
THEN
1208 used_chunks = used_chunks + 1
1209 chunk_count = atom_count
1211 chunk_count = chunk_count + atom_count
1214 fits = used_chunks <= nchunks
1216 END FUNCTION atom_chunks_fit_limit
1225 FUNCTION feature_row_chunk_owner(row, counts, displs)
RESULT(owner)
1226 INTEGER,
INTENT(IN) :: row
1227 INTEGER,
DIMENSION(:),
INTENT(IN) :: counts, displs
1233 DO pe = 1,
SIZE(counts)
1234 IF (row > displs(pe) .AND. row <= displs(pe) + counts(pe))
THEN
1240 END FUNCTION feature_row_chunk_owner
1247 SUBROUTINE counts_to_displs(counts, displs)
1248 INTEGER,
DIMENSION(:),
INTENT(IN) :: counts
1249 INTEGER,
DIMENSION(:),
INTENT(OUT) :: displs
1254 DO pe = 2,
SIZE(counts)
1255 displs(pe) = displs(pe - 1) + counts(pe - 1)
1258 END SUBROUTINE counts_to_displs
1266 SUBROUTINE build_atom_chunk_routes(cache, local_to_global, group)
1267 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
1268 INTEGER,
DIMENSION(:),
INTENT(IN) :: local_to_global
1272 INTEGER :: chunk_row, dest, feature_begin, feature_end, &
1273 feature_pos, local_feature, local_row, meta_pos, &
1274 nflat_local, nproc, nrecv, nsend, pe, point_pos, &
1275 route_index, row, row_position
1276 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: chunk_cursor, chunk_route_counts, &
1277 feature_cursor, feature_recv_counts, &
1278 feature_recv_displs, feature_send_counts, &
1279 feature_send_displs, meta_recv_counts, &
1280 meta_recv_displs, meta_send_counts, &
1281 meta_send_displs, recv_meta, route_cursor, &
1282 route_feature_dest, route_last_row, &
1283 route_last_position, send_meta
1285 nflat_local =
SIZE(cache%local_feature_offsets) - 1
1286 nproc =
SIZE(cache%route_point_send_counts)
1287 ALLOCATE (route_feature_dest(
SIZE(local_to_global)), &
1288 feature_send_counts(nproc), feature_send_displs(nproc), &
1289 feature_recv_counts(nproc), feature_recv_displs(nproc), &
1290 route_last_row(nproc), route_last_position(nproc))
1291 cache%route_point_send_counts = 0
1292 feature_send_counts = 0
1293 route_feature_dest = 0
1296 DO local_row = 1, nflat_local
1297 feature_begin = cache%local_feature_offsets(local_row)
1298 feature_end = cache%local_feature_offsets(local_row + 1) - 1
1299 DO local_feature = feature_begin, feature_end
1300 dest = feature_row_chunk_owner(local_to_global(local_feature), &
1301 cache%chunk_feature_counts, &
1302 cache%chunk_feature_displs)
1304 route_feature_dest(local_feature) = dest
1305 feature_send_counts(dest) = feature_send_counts(dest) + 1
1306 IF (route_last_row(dest) /= local_row)
THEN
1307 cache%route_point_send_counts(dest) = cache%route_point_send_counts(dest) + 1
1308 route_last_row(dest) = local_row
1312 CALL counts_to_displs(cache%route_point_send_counts, cache%route_point_send_displs)
1313 CALL counts_to_displs(feature_send_counts, feature_send_displs)
1314 CALL group%alltoall(cache%route_point_send_counts, cache%route_point_recv_counts, 1)
1315 CALL counts_to_displs(cache%route_point_recv_counts, cache%route_point_recv_displs)
1316 CALL group%alltoall(feature_send_counts, feature_recv_counts, 1)
1317 CALL counts_to_displs(feature_recv_counts, feature_recv_displs)
1319 nsend = sum(cache%route_point_send_counts)
1320 nrecv = sum(cache%route_point_recv_counts)
1321 ALLOCATE (cache%route_send_local_rows(nsend), &
1322 cache%route_row_offsets(nflat_local + 1), &
1323 cache%route_row_positions(nsend), &
1324 route_cursor(nproc), feature_cursor(nproc), &
1325 send_meta(2*
SIZE(local_to_global)), &
1326 recv_meta(2*cache%chunk_feature_count), &
1327 meta_send_counts(nproc), meta_send_displs(nproc), &
1328 meta_recv_counts(nproc), meta_recv_displs(nproc))
1329 cache%route_send_local_rows = 0
1330 cache%route_row_positions = 0
1332 route_cursor(:) = cache%route_point_send_displs + 1
1333 feature_cursor(:) = feature_send_displs + 1
1335 route_last_position = 0
1336 cache%route_row_offsets(1) = 1
1338 DO local_row = 1, nflat_local
1339 feature_begin = cache%local_feature_offsets(local_row)
1340 feature_end = cache%local_feature_offsets(local_row + 1) - 1
1341 DO local_feature = feature_begin, feature_end
1342 dest = route_feature_dest(local_feature)
1343 IF (route_last_row(dest) /= local_row)
THEN
1344 point_pos = route_cursor(dest)
1345 route_cursor(dest) = route_cursor(dest) + 1
1346 route_last_row(dest) = local_row
1347 route_last_position(dest) = point_pos
1348 cache%route_send_local_rows(point_pos) = local_row
1349 cache%route_row_positions(row_position) = point_pos
1350 row_position = row_position + 1
1352 point_pos = route_last_position(dest)
1354 feature_pos = feature_cursor(dest)
1355 feature_cursor(dest) = feature_cursor(dest) + 1
1356 send_meta(2*feature_pos - 1) = local_to_global(local_feature)
1357 send_meta(2*feature_pos) = point_pos - cache%route_point_send_displs(dest)
1359 cache%route_row_offsets(local_row + 1) = row_position
1361 cpassert(row_position == nsend + 1)
1364 meta_send_counts(:) = 2*feature_send_counts
1365 meta_send_displs(:) = 2*feature_send_displs
1366 meta_recv_counts(:) = 2*feature_recv_counts
1367 meta_recv_displs(:) = 2*feature_recv_displs
1368 CALL group%alltoall(send_meta, meta_send_counts, meta_send_displs, recv_meta, &
1369 meta_recv_counts, meta_recv_displs)
1372 ALLOCATE (cache%chunk_return_positions(cache%chunk_feature_count), &
1373 cache%route_chunk_offsets(nrecv + 1), &
1374 cache%route_chunk_rows(cache%chunk_feature_count), &
1375 chunk_route_counts(nrecv), chunk_cursor(nrecv))
1376 cache%chunk_return_positions = 0
1377 cache%route_chunk_rows = 0
1378 chunk_route_counts = 0
1380 DO feature_pos = feature_recv_displs(pe) + 1, &
1381 feature_recv_displs(pe) + feature_recv_counts(pe)
1382 meta_pos = 2*feature_pos
1383 row = recv_meta(meta_pos - 1)
1384 route_index = recv_meta(meta_pos)
1385 point_pos = cache%route_point_recv_displs(pe) + route_index
1386 cpassert(point_pos >= 1 .AND. point_pos <= nrecv)
1387 chunk_row = row - cache%chunk_feature_begin + 1
1388 cpassert(chunk_row >= 1 .AND. chunk_row <= cache%chunk_feature_count)
1389 cache%chunk_return_positions(chunk_row) = point_pos
1390 chunk_route_counts(point_pos) = chunk_route_counts(point_pos) + 1
1393 cache%route_chunk_offsets(1) = 1
1394 DO point_pos = 1, nrecv
1395 cache%route_chunk_offsets(point_pos + 1) = &
1396 cache%route_chunk_offsets(point_pos) + chunk_route_counts(point_pos)
1398 chunk_cursor(:) = cache%route_chunk_offsets(1:nrecv)
1400 DO feature_pos = feature_recv_displs(pe) + 1, &
1401 feature_recv_displs(pe) + feature_recv_counts(pe)
1402 meta_pos = 2*feature_pos
1403 row = recv_meta(meta_pos - 1)
1404 chunk_row = row - cache%chunk_feature_begin + 1
1405 point_pos = cache%chunk_return_positions(chunk_row)
1406 cache%route_chunk_rows(chunk_cursor(point_pos)) = chunk_row
1407 chunk_cursor(point_pos) = chunk_cursor(point_pos) + 1
1411 cpassert(sum(feature_send_counts) ==
SIZE(local_to_global))
1412 cpassert(sum(feature_recv_counts) == cache%chunk_feature_count)
1413 cpassert(sum(cache%route_point_send_counts) == nsend)
1414 cpassert(sum(cache%route_point_recv_counts) == nrecv)
1415 cpassert(all(cache%chunk_return_positions > 0))
1416 cpassert(all(cache%route_chunk_rows > 0))
1417 cpassert(all(cache%route_row_positions > 0))
1418 cpassert(all(cache%route_send_local_rows > 0))
1420 DEALLOCATE (chunk_cursor, chunk_route_counts, feature_cursor, feature_recv_counts, &
1421 feature_recv_displs, feature_send_counts, feature_send_displs, &
1422 meta_recv_counts, meta_recv_displs, meta_send_counts, meta_send_displs, &
1423 recv_meta, route_cursor, route_feature_dest, route_last_position, route_last_row, &
1426 END SUBROUTINE build_atom_chunk_routes
1432 SUBROUTINE build_atom_chunk_layout(cache)
1433 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
1435 INTEGER :: irow, max_grid_size, row_begin, row_end
1437 IF (cache%chunk_feature_count <= 0 .OR. cache%chunk_natom <= 0)
RETURN
1439 row_begin = cache%chunk_feature_begin
1440 row_end = row_begin + cache%chunk_feature_count - 1
1441 ALLOCATE (cache%chunk_grid_coords(3, cache%chunk_feature_count), &
1442 cache%chunk_grid_weights(cache%chunk_feature_count), &
1443 cache%chunk_atomic_grid_weights(cache%chunk_feature_count), &
1444 cache%chunk_atomic_grid_sizes(cache%chunk_natom), &
1445 cache%chunk_coarse_0_atomic_coords(3, cache%chunk_natom), &
1446 cache%chunk_feature_indices(cache%chunk_feature_count))
1447 cache%chunk_grid_coords(:, :) = cache%grid_coords(:, row_begin:row_end)
1448 cache%chunk_grid_weights(:) = cache%grid_weights(row_begin:row_end)
1449 cache%chunk_atomic_grid_weights(:) = cache%atomic_grid_weights(row_begin:row_end)
1450 cache%chunk_atomic_grid_sizes(:) = &
1451 cache%atomic_grid_sizes(cache%chunk_atom_begin:cache%chunk_atom_end)
1452 cache%chunk_coarse_0_atomic_coords(:, :) = &
1453 cache%coarse_0_atomic_coords(:, cache%chunk_atom_begin:cache%chunk_atom_end)
1455 max_grid_size = maxval(int(cache%chunk_atomic_grid_sizes))
1456 ALLOCATE (cache%chunk_atomic_grid_size_bound_shape(0, max_grid_size))
1457 cache%chunk_atomic_grid_size_bound_shape = 0_int_8
1458 DO irow = 1, cache%chunk_feature_count
1459 cache%chunk_feature_indices(irow) = int(irow - 1, kind=
int_8)
1462 END SUBROUTINE build_atom_chunk_layout
1471 SUBROUTINE route_atom_chunk_dynamics(features, local_dynamic, group, collapse_spin_dynamics)
1473 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: local_dynamic
1476 LOGICAL,
INTENT(IN) :: collapse_spin_dynamics
1478 INTEGER :: chunk_row, dyn_base, local_row, &
1479 ndynamic_route_per_point, nrecv, nsend, &
1481 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: recv_counts, recv_displs, send_counts, send_displs
1482 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: recv_dynamic, send_dynamic
1486 cpassert(nsend == sum(
cached_layout%route_point_send_counts))
1487 ndynamic_route_per_point = ndynamic_per_point
1488 IF (collapse_spin_dynamics) ndynamic_route_per_point = nrks_dynamic_per_point
1490 ALLOCATE (send_dynamic(max(1, ndynamic_route_per_point*nsend)), &
1491 recv_dynamic(max(1, ndynamic_route_per_point*nrecv)), &
1495 send_counts(:) = ndynamic_route_per_point*
cached_layout%route_point_send_counts
1496 send_displs(:) = ndynamic_route_per_point*
cached_layout%route_point_send_displs
1497 recv_counts(:) = ndynamic_route_per_point*
cached_layout%route_point_recv_counts
1498 recv_displs(:) = ndynamic_route_per_point*
cached_layout%route_point_recv_displs
1502 DO point_pos = 1, nsend
1503 dyn_base = ndynamic_route_per_point*(point_pos - 1)
1505 src_base = ndynamic_route_per_point*(local_row - 1)
1506 send_dynamic(dyn_base + 1:dyn_base + ndynamic_route_per_point) = &
1507 local_dynamic(src_base + 1:src_base + ndynamic_route_per_point)
1511 CALL group%alltoall(send_dynamic, send_counts, send_displs, recv_dynamic, recv_counts, &
1514 features%uses_collapsed_rks_dynamic = collapse_spin_dynamics
1516 IF (collapse_spin_dynamics)
THEN
1517 ALLOCATE (features%chunk_density(
cached_layout%chunk_feature_count, 1), &
1518 features%chunk_grad(
cached_layout%chunk_feature_count, 3, 1), &
1521 ALLOCATE (features%chunk_density(
cached_layout%chunk_feature_count, 2), &
1522 features%chunk_grad(
cached_layout%chunk_feature_count, 3, 2), &
1531 cpassert(point_pos >= 1 .AND. point_pos <= nrecv)
1532 dyn_base = ndynamic_route_per_point*(point_pos - 1)
1533 IF (collapse_spin_dynamics)
THEN
1534 features%chunk_density(chunk_row, 1) = recv_dynamic(dyn_base + 1)
1535 features%chunk_grad(chunk_row, 1, 1) = recv_dynamic(dyn_base + 2)
1536 features%chunk_grad(chunk_row, 2, 1) = recv_dynamic(dyn_base + 3)
1537 features%chunk_grad(chunk_row, 3, 1) = recv_dynamic(dyn_base + 4)
1538 features%chunk_kin(chunk_row, 1) = recv_dynamic(dyn_base + 5)
1540 features%chunk_density(chunk_row, :) = recv_dynamic(dyn_base + 1:dyn_base + 2)
1541 features%chunk_grad(chunk_row, 1, 1) = recv_dynamic(dyn_base + 3)
1542 features%chunk_grad(chunk_row, 2, 1) = recv_dynamic(dyn_base + 4)
1543 features%chunk_grad(chunk_row, 3, 1) = recv_dynamic(dyn_base + 5)
1544 features%chunk_grad(chunk_row, 1, 2) = recv_dynamic(dyn_base + 6)
1545 features%chunk_grad(chunk_row, 2, 2) = recv_dynamic(dyn_base + 7)
1546 features%chunk_grad(chunk_row, 3, 2) = recv_dynamic(dyn_base + 8)
1547 features%chunk_kin(chunk_row, :) = recv_dynamic(dyn_base + 9:dyn_base + 10)
1553 DEALLOCATE (recv_counts, recv_displs, recv_dynamic, send_counts, send_displs, send_dynamic)
1555 END SUBROUTINE route_atom_chunk_dynamics
1561 SUBROUTINE extract_atom_chunk_dynamics(features)
1564 INTEGER :: row_begin, row_end
1569 ALLOCATE (features%chunk_density(
cached_layout%chunk_feature_count, 2), &
1570 features%chunk_grad(
cached_layout%chunk_feature_count, 3, 2), &
1572 features%chunk_density(:, :) = features%density(row_begin:row_end, :)
1573 features%chunk_grad(:, :, :) = features%grad(row_begin:row_end, :, :)
1574 features%chunk_kin(:, :) = features%kin(row_begin:row_end, :)
1576 END SUBROUTINE extract_atom_chunk_dynamics
1585 SUBROUTINE weights_signature(weights, has_weights, weight_sum, weight_sumsq)
1587 LOGICAL,
INTENT(OUT) :: has_weights
1588 REAL(kind=
dp),
INTENT(OUT) :: weight_sum, weight_sumsq
1590 has_weights = .false.
1592 weight_sumsq = 0.0_dp
1593 IF (
PRESENT(weights))
THEN
1594 IF (
ASSOCIATED(weights))
THEN
1595 has_weights = .true.
1596 weight_sum = sum(weights%array)
1597 weight_sumsq = sum(weights%array*weights%array)
1601 END SUBROUTINE weights_signature
1607 SUBROUTINE release_layout_cache(cache)
1608 TYPE(skala_gpw_layout_cache_type),
INTENT(INOUT) :: cache
1610 IF (cache%inputs_active)
THEN
1612 cache%inputs_active = .false.
1615 IF (cache%chunk_inputs_active)
THEN
1617 cache%chunk_inputs_active = .false.
1620 IF (cache%dynamic_tensors_active)
THEN
1624 cache%dynamic_tensors_active = .false.
1627 IF (cache%chunk_dynamic_tensors_active)
THEN
1628 IF (cache%chunk_dynamic_input_views_active)
THEN
1632 cache%chunk_dynamic_input_views_active = .false.
1637 cache%chunk_dynamic_tensors_active = .false.
1640 IF (cache%static_tensors_active)
THEN
1649 cache%static_tensors_active = .false.
1652 IF (cache%chunk_static_tensors_active)
THEN
1661 cache%chunk_static_tensors_active = .false.
1664 IF (
ALLOCATED(cache%chunk_feature_counts))
DEALLOCATE (cache%chunk_feature_counts)
1665 IF (
ALLOCATED(cache%chunk_feature_displs))
DEALLOCATE (cache%chunk_feature_displs)
1666 IF (
ALLOCATED(cache%chunk_grad_counts))
DEALLOCATE (cache%chunk_grad_counts)
1667 IF (
ALLOCATED(cache%chunk_grad_displs))
DEALLOCATE (cache%chunk_grad_displs)
1668 IF (
ALLOCATED(cache%chunk_return_positions))
DEALLOCATE (cache%chunk_return_positions)
1669 IF (
ALLOCATED(cache%route_chunk_offsets))
DEALLOCATE (cache%route_chunk_offsets)
1670 IF (
ALLOCATED(cache%route_chunk_rows))
DEALLOCATE (cache%route_chunk_rows)
1671 IF (
ALLOCATED(cache%route_point_recv_counts))
DEALLOCATE (cache%route_point_recv_counts)
1672 IF (
ALLOCATED(cache%route_point_recv_displs))
DEALLOCATE (cache%route_point_recv_displs)
1673 IF (
ALLOCATED(cache%route_point_send_counts))
DEALLOCATE (cache%route_point_send_counts)
1674 IF (
ALLOCATED(cache%route_point_send_displs))
DEALLOCATE (cache%route_point_send_displs)
1675 IF (
ALLOCATED(cache%route_row_offsets))
DEALLOCATE (cache%route_row_offsets)
1676 IF (
ALLOCATED(cache%route_row_positions))
DEALLOCATE (cache%route_row_positions)
1677 IF (
ALLOCATED(cache%route_send_local_rows))
DEALLOCATE (cache%route_send_local_rows)
1678 IF (
ALLOCATED(cache%dynamic_counts))
DEALLOCATE (cache%dynamic_counts)
1679 IF (
ALLOCATED(cache%dynamic_displs))
DEALLOCATE (cache%dynamic_displs)
1680 IF (
ALLOCATED(cache%feature_counts))
DEALLOCATE (cache%feature_counts)
1681 IF (
ALLOCATED(cache%feature_displs))
DEALLOCATE (cache%feature_displs)
1682 IF (
ALLOCATED(cache%feature_source_points))
DEALLOCATE (cache%feature_source_points)
1683 IF (
ALLOCATED(cache%global_to_feature))
DEALLOCATE (cache%global_to_feature)
1684 IF (
ALLOCATED(cache%feature_index))
DEALLOCATE (cache%feature_index)
1685 IF (
ALLOCATED(cache%atomic_grid_sizes))
DEALLOCATE (cache%atomic_grid_sizes)
1686 IF (
ALLOCATED(cache%chunk_atomic_grid_sizes))
DEALLOCATE (cache%chunk_atomic_grid_sizes)
1687 IF (
ALLOCATED(cache%chunk_feature_indices))
DEALLOCATE (cache%chunk_feature_indices)
1688 IF (
ALLOCATED(cache%local_feature_counts))
DEALLOCATE (cache%local_feature_counts)
1689 IF (
ALLOCATED(cache%local_feature_indices))
DEALLOCATE (cache%local_feature_indices)
1690 IF (
ALLOCATED(cache%local_feature_offsets))
DEALLOCATE (cache%local_feature_offsets)
1691 IF (
ALLOCATED(cache%local_feature_points))
DEALLOCATE (cache%local_feature_points)
1692 IF (
ALLOCATED(cache%local_feature_rows))
DEALLOCATE (cache%local_feature_rows)
1693 IF (
ALLOCATED(cache%atomic_grid_size_bound_shape))
THEN
1694 DEALLOCATE (cache%atomic_grid_size_bound_shape)
1696 IF (
ALLOCATED(cache%chunk_atomic_grid_size_bound_shape))
THEN
1697 DEALLOCATE (cache%chunk_atomic_grid_size_bound_shape)
1699 IF (
ALLOCATED(cache%atomic_grid_weights))
DEALLOCATE (cache%atomic_grid_weights)
1700 IF (
ALLOCATED(cache%chunk_atomic_grid_weights))
DEALLOCATE (cache%chunk_atomic_grid_weights)
1701 IF (
ALLOCATED(cache%chunk_grid_weights))
DEALLOCATE (cache%chunk_grid_weights)
1702 IF (
ALLOCATED(cache%grid_weights))
DEALLOCATE (cache%grid_weights)
1703 IF (
ALLOCATED(cache%atom_coords))
DEALLOCATE (cache%atom_coords)
1704 IF (
ALLOCATED(cache%chunk_coarse_0_atomic_coords))
THEN
1705 DEALLOCATE (cache%chunk_coarse_0_atomic_coords)
1707 IF (
ALLOCATED(cache%coarse_0_atomic_coords))
DEALLOCATE (cache%coarse_0_atomic_coords)
1708 IF (
ALLOCATED(cache%chunk_grid_coords))
DEALLOCATE (cache%chunk_grid_coords)
1709 IF (
ALLOCATED(cache%grid_coords))
DEALLOCATE (cache%grid_coords)
1711 cache%chunk_atom_begin = 1
1712 cache%chunk_atom_end = 0
1713 cache%chunk_feature_begin = 1
1714 cache%chunk_feature_count = 0
1715 cache%chunk_natom = 0
1718 cache%nflat_local = 0
1726 cache%weight_sum = 0.0_dp
1727 cache%weight_sumsq = 0.0_dp
1728 cache%cell_hmat = 0.0_dp
1730 cache%active = .false.
1731 cache%has_weights = .false.
1732 cache%chunk_dynamic_tensors_active = .false.
1733 cache%chunk_dynamic_input_views_active = .false.
1734 cache%chunk_inputs_active = .false.
1735 cache%chunk_inputs_use_collapsed_rks = .false.
1736 cache%chunk_static_tensors_active = .false.
1737 cache%dynamic_tensors_active = .false.
1738 cache%inputs_active = .false.
1739 cache%static_tensors_active = .false.
1741 END SUBROUTINE release_layout_cache
1750 IF (features%active)
THEN
1751 IF (features%owns_dynamic_tensors)
THEN
1752 IF (features%uses_collapsed_rks_dynamic)
THEN
1761 IF (features%owns_static_tensors)
THEN
1768 IF (features%owns_grid_coordinate_tensor)
THEN
1771 IF (features%owns_weight_tensors)
THEN
1775 IF (features%owns_static_tensors .OR. features%owns_coordinate_tensor)
THEN
1779 features%active = .false.
1780 features%owns_coordinate_tensor = .false.
1781 features%owns_grid_coordinate_tensor = .false.
1782 features%owns_weight_tensors = .false.
1783 features%owns_dynamic_tensors = .true.
1784 features%owns_inputs = .true.
1785 features%owns_static_tensors = .true.
1786 features%uses_atom_chunk_routing = .false.
1787 features%uses_atom_chunks = .false.
1788 features%uses_collapsed_rks_dynamic = .false.
1791 IF (
ALLOCATED(features%chunk_density))
DEALLOCATE (features%chunk_density)
1792 IF (
ALLOCATED(features%chunk_grad))
DEALLOCATE (features%chunk_grad)
1793 IF (
ALLOCATED(features%chunk_kin))
DEALLOCATE (features%chunk_kin)
1794 IF (
ALLOCATED(features%density))
DEALLOCATE (features%density)
1795 IF (
ALLOCATED(features%grad))
DEALLOCATE (features%grad)
1796 IF (
ALLOCATED(features%kin))
DEALLOCATE (features%kin)
1797 IF (
ALLOCATED(features%chunk_grad_counts))
DEALLOCATE (features%chunk_grad_counts)
1798 IF (
ALLOCATED(features%chunk_grad_displs))
DEALLOCATE (features%chunk_grad_displs)
1799 NULLIFY (features%route_point_recv_counts, features%route_point_recv_displs, &
1800 features%route_point_send_counts, features%route_point_send_displs, &
1801 features%route_chunk_offsets, features%route_chunk_rows, &
1802 features%route_row_offsets, features%route_row_positions, &
1803 features%route_send_local_rows)
1804 IF (
ALLOCATED(features%feature_index))
DEALLOCATE (features%feature_index)
1805 IF (
ALLOCATED(features%local_feature_counts))
DEALLOCATE (features%local_feature_counts)
1806 IF (
ALLOCATED(features%local_feature_offsets))
DEALLOCATE (features%local_feature_offsets)
1807 IF (
ALLOCATED(features%local_feature_rows))
DEALLOCATE (features%local_feature_rows)
1808 IF (
ALLOCATED(features%grid_coords))
DEALLOCATE (features%grid_coords)
1809 IF (
ALLOCATED(features%grid_weights))
DEALLOCATE (features%grid_weights)
1810 IF (
ALLOCATED(features%atomic_grid_weights))
DEALLOCATE (features%atomic_grid_weights)
1811 IF (
ALLOCATED(features%atomic_grid_sizes))
DEALLOCATE (features%atomic_grid_sizes)
1812 IF (
ALLOCATED(features%coarse_0_atomic_coords))
DEALLOCATE (features%coarse_0_atomic_coords)
1813 IF (
ALLOCATED(features%atomic_grid_size_bound_shape))
THEN
1814 DEALLOCATE (features%atomic_grid_size_bound_shape)
1816 features%chunk_feature_count = 0
1818 features%nflat_local = 0
1820 features%uses_atom_chunk_routing = .false.
1821 features%uses_collapsed_rks_dynamic = .false.
1831 INTEGER,
INTENT(IN) :: max_rows
1832 INTEGER :: nsubchunks
1834 INTEGER :: atom_rows, iatom, rows
1839 IF (max_rows <= 0)
THEN
1846 atom_rows = int(
cached_layout%chunk_atomic_grid_sizes(iatom))
1847 IF (rows > 0 .AND. rows + atom_rows > max_rows)
THEN
1848 nsubchunks = nsubchunks + 1
1851 rows = rows + atom_rows
1853 IF (rows > 0) nsubchunks = nsubchunks + 1
1854 nsubchunks = max(1, nsubchunks)
1868 INTEGER,
INTENT(IN) :: max_rows
1869 INTEGER,
ALLOCATABLE,
DIMENSION(:),
INTENT(OUT) :: atom_begin, atom_count, row_begin, &
1872 INTEGER :: atom_begin_tmp, atom_count_tmp, atom_rows, iatom, insert_at, nsubchunks, &
1873 row_begin_tmp, row_count_tmp, rows, subchunk
1874 INTEGER(KIND=int_8) :: padded_rows_tmp
1875 INTEGER(KIND=int_8),
ALLOCATABLE,
DIMENSION(:) :: padded_rows
1878 ALLOCATE (atom_begin(nsubchunks), atom_count(nsubchunks), row_begin(nsubchunks), &
1879 row_count(nsubchunks))
1880 IF (nsubchunks == 0)
RETURN
1890 IF (max_rows <= 0)
THEN
1899 atom_begin(subchunk) = 1
1900 row_begin(subchunk) = 1
1903 atom_rows = int(
cached_layout%chunk_atomic_grid_sizes(iatom))
1904 IF (rows > 0 .AND. rows + atom_rows > max_rows)
THEN
1905 atom_count(subchunk) = iatom - atom_begin(subchunk)
1906 row_count(subchunk) = rows
1907 subchunk = subchunk + 1
1908 atom_begin(subchunk) = iatom
1909 row_begin(subchunk) = row_begin(subchunk - 1) + row_count(subchunk - 1)
1912 rows = rows + atom_rows
1914 atom_count(subchunk) =
cached_layout%chunk_natom - atom_begin(subchunk) + 1
1915 row_count(subchunk) = rows
1917 cpassert(subchunk == nsubchunks)
1919 ALLOCATE (padded_rows(nsubchunks))
1920 DO subchunk = 1, nsubchunks
1921 iatom = atom_begin(subchunk) + atom_count(subchunk) - 1
1922 padded_rows(subchunk) = int(atom_count(subchunk), kind=
int_8)* &
1924 atom_begin(subchunk):iatom))
1928 DO subchunk = 2, nsubchunks
1929 padded_rows_tmp = padded_rows(subchunk)
1930 atom_begin_tmp = atom_begin(subchunk)
1931 atom_count_tmp = atom_count(subchunk)
1932 row_begin_tmp = row_begin(subchunk)
1933 row_count_tmp = row_count(subchunk)
1934 insert_at = subchunk
1935 DO WHILE (insert_at > 1 .AND. padded_rows(insert_at - 1) < padded_rows_tmp)
1936 padded_rows(insert_at) = padded_rows(insert_at - 1)
1937 atom_begin(insert_at) = atom_begin(insert_at - 1)
1938 atom_count(insert_at) = atom_count(insert_at - 1)
1939 row_begin(insert_at) = row_begin(insert_at - 1)
1940 row_count(insert_at) = row_count(insert_at - 1)
1941 insert_at = insert_at - 1
1943 padded_rows(insert_at) = padded_rows_tmp
1944 atom_begin(insert_at) = atom_begin_tmp
1945 atom_count(insert_at) = atom_count_tmp
1946 row_begin(insert_at) = row_begin_tmp
1947 row_count(insert_at) = row_count_tmp
1949 DEALLOCATE (padded_rows)
1962 max_rows, requires_grad)
1965 INTEGER,
INTENT(IN) :: subchunk_index, max_rows
1966 LOGICAL,
INTENT(IN) :: requires_grad
1968 INTEGER :: atom_begin, atom_end, row_begin, row_end
1970 cpassert(parent%uses_atom_chunks)
1971 CALL atom_subchunk_bounds(subchunk_index, max_rows, atom_begin, atom_end, &
1974 atom_end - atom_begin + 1, row_begin, &
1975 row_end - row_begin + 1, requires_grad)
1990 atom_count, row_begin, row_count, &
1994 INTEGER,
INTENT(IN) :: atom_begin, atom_count, row_begin, &
1996 LOGICAL,
INTENT(IN) :: requires_grad
1998 INTEGER :: atom_end, max_grid_size, row_end
2001 cpassert(parent%uses_atom_chunks)
2002 atom_end = atom_begin + atom_count - 1
2003 row_end = row_begin + row_count - 1
2004 cpassert(atom_count > 0)
2005 cpassert(row_count > 0)
2006 mark_used(requires_grad)
2007 max_grid_size = maxval(int(
cached_layout%chunk_atomic_grid_sizes(atom_begin:atom_end)))
2009 ALLOCATE (features%atomic_grid_size_bound_shape(0, max_grid_size))
2010 features%atomic_grid_size_bound_shape = 0_int_8
2012 features%chunk_feature_count = row_count
2013 features%nflat = parent%nflat
2014 features%nflat_local = parent%nflat_local
2015 features%grid_weight_sum = sum(
cached_layout%chunk_grid_weights(row_begin:row_end))
2016 features%uses_atom_chunks = .true.
2017 features%uses_atom_chunk_routing = parent%uses_atom_chunk_routing
2018 CALL add_subchunk_feature_tensors(parent, features, atom_begin, atom_count, row_begin, &
2020 features%active = .true.
2033 SUBROUTINE atom_subchunk_bounds(subchunk_index, max_rows, atom_begin, atom_end, &
2035 INTEGER,
INTENT(IN) :: subchunk_index, max_rows
2036 INTEGER,
INTENT(OUT) :: atom_begin, atom_end, row_begin, row_end
2038 INTEGER :: atom_rows, current_subchunk, iatom, &
2041 cpassert(subchunk_index > 0)
2042 cpassert(max_rows > 0)
2049 current_subchunk = 1
2053 atom_rows = int(
cached_layout%chunk_atomic_grid_sizes(iatom))
2054 IF (rows > 0 .AND. rows + atom_rows > max_rows)
THEN
2055 IF (current_subchunk == subchunk_index)
THEN
2056 atom_end = iatom - 1
2057 row_end = row_cursor - 1
2060 current_subchunk = current_subchunk + 1
2062 row_begin = row_cursor
2065 rows = rows + atom_rows
2066 row_cursor = row_cursor + atom_rows
2069 IF (current_subchunk == subchunk_index)
THEN
2071 row_end = row_cursor - 1
2075 cpabort(
"Requested native SKALA atom subchunk does not exist.")
2077 END SUBROUTINE atom_subchunk_bounds
2088 SUBROUTINE add_subchunk_feature_tensors(parent, features, atom_begin, atom_count, row_begin, &
2092 INTEGER,
INTENT(IN) :: atom_begin, atom_count, row_begin, &
2096 cpassert(parent%active)
2097 cpassert(
ALLOCATED(features%atomic_grid_size_bound_shape))
2099 features%owns_coordinate_tensor = .false.
2100 features%owns_dynamic_tensors = .true.
2101 features%owns_inputs = .true.
2102 features%owns_static_tensors = .true.
2103 features%uses_collapsed_rks_dynamic = parent%uses_collapsed_rks_dynamic
2106 row_count, features%grid_coords_t)
2108 row_count, features%grid_weights_t)
2110 row_count, features%atomic_grid_weights_t)
2112 atom_count, features%atomic_grid_sizes_t)
2114 atom_begin - 1, atom_count, features%coarse_0_atomic_coords_t)
2116 features%atomic_grid_size_bound_shape)
2122 IF (features%uses_collapsed_rks_dynamic)
THEN
2130 CALL torch_dict_insert(features%inputs,
"grid_weights", features%grid_weights_t)
2132 features%atomic_grid_weights_t)
2134 features%atomic_grid_sizes_t)
2136 features%atomic_grid_size_bound_shape_t)
2137 IF (features%uses_collapsed_rks_dynamic)
THEN
2147 features%coarse_0_atomic_coords_t)
2149 END SUBROUTINE add_subchunk_feature_tensors
2156 SUBROUTINE add_owned_feature_tensors(features, requires_grad)
2158 LOGICAL,
INTENT(IN) :: requires_grad
2160 cpassert(
ALLOCATED(features%chunk_density))
2161 cpassert(
ALLOCATED(features%chunk_grad))
2162 cpassert(
ALLOCATED(features%chunk_kin))
2163 cpassert(
ALLOCATED(features%grid_coords))
2164 cpassert(
ALLOCATED(features%grid_weights))
2165 cpassert(
ALLOCATED(features%atomic_grid_weights))
2166 cpassert(
ALLOCATED(features%atomic_grid_sizes))
2167 cpassert(
ALLOCATED(features%atomic_grid_size_bound_shape))
2168 cpassert(
ALLOCATED(features%coarse_0_atomic_coords))
2170 features%owns_coordinate_tensor = .false.
2171 features%owns_dynamic_tensors = .true.
2172 features%owns_inputs = .true.
2173 features%owns_static_tensors = .true.
2184 features%coarse_0_atomic_coords)
2187 features%atomic_grid_size_bound_shape)
2198 CALL torch_dict_insert(features%inputs,
"grid_weights", features%grid_weights_t)
2200 features%atomic_grid_weights_t)
2202 features%atomic_grid_sizes_t)
2204 features%atomic_grid_size_bound_shape_t)
2209 features%coarse_0_atomic_coords_t)
2211 END SUBROUTINE add_owned_feature_tensors
2222 SUBROUTINE add_feature_tensors(features, requires_grad, requires_coordinate_grad, &
2223 requires_stress_grad, use_atom_chunks, requires_weight_grad)
2225 LOGICAL,
INTENT(IN) :: requires_grad, requires_coordinate_grad, &
2226 requires_stress_grad, use_atom_chunks
2227 LOGICAL,
INTENT(IN),
OPTIONAL :: requires_weight_grad
2229 LOGICAL :: my_requires_weight_grad
2231 my_requires_weight_grad = .false.
2232 IF (
PRESENT(requires_weight_grad)) my_requires_weight_grad = requires_weight_grad
2234 features%owns_static_tensors = .false.
2235 features%owns_coordinate_tensor = .false.
2236 features%owns_grid_coordinate_tensor = .false.
2237 features%owns_weight_tensors = .false.
2238 features%owns_dynamic_tensors = .false.
2239 features%owns_inputs = .true.
2240 IF (use_atom_chunks)
THEN
2241 cpassert(.NOT. requires_coordinate_grad)
2242 cpassert(.NOT. requires_stress_grad)
2243 cpassert(.NOT. my_requires_weight_grad)
2246 features%grid_weights_t =
cached_layout%chunk_grid_weights_t
2247 features%atomic_grid_weights_t =
cached_layout%chunk_atomic_grid_weights_t
2248 features%atomic_grid_sizes_t =
cached_layout%chunk_atomic_grid_sizes_t
2249 features%atomic_grid_size_bound_shape_t = &
2251 features%local_feature_indices_t =
cached_layout%chunk_feature_indices_t
2255 features%uses_collapsed_rks_dynamic))
THEN
2259 IF (.NOT. features%uses_collapsed_rks_dynamic .AND. &
2268 features%chunk_density, requires_grad=requires_grad)
2271 requires_grad=requires_grad)
2274 requires_grad=requires_grad)
2278 IF (features%uses_collapsed_rks_dynamic .AND. &
2288 IF (features%uses_collapsed_rks_dynamic)
THEN
2289 features%density_input_t =
cached_layout%chunk_density_input_t
2296 IF (features%uses_collapsed_rks_dynamic)
THEN
2298 features%density_input_t)
2300 features%grad_input_t)
2302 features%kin_input_t)
2313 cached_layout%chunk_inputs_use_collapsed_rks = features%uses_collapsed_rks_dynamic
2317 features%owns_inputs = .false.
2318 features%coarse_0_atomic_coords_t =
cached_layout%chunk_coarse_0_atomic_coords_t
2321 IF (.NOT. requires_stress_grad .AND. .NOT. my_requires_weight_grad)
THEN
2324 features%atomic_grid_weights_t =
cached_layout%atomic_grid_weights_t
2326 features%atomic_grid_sizes_t =
cached_layout%atomic_grid_sizes_t
2327 features%atomic_grid_size_bound_shape_t =
cached_layout%atomic_grid_size_bound_shape_t
2328 features%local_feature_indices_t =
cached_layout%local_feature_indices_t
2331 requires_grad=requires_grad)
2334 requires_grad=requires_grad)
2337 requires_grad=requires_grad)
2341 IF (requires_coordinate_grad .OR. requires_stress_grad .OR. my_requires_weight_grad)
THEN
2342 IF (requires_stress_grad .OR. my_requires_weight_grad)
THEN
2344 IF (requires_stress_grad)
THEN
2348 features%owns_grid_coordinate_tensor = .true.
2356 features%atomic_grid_weights)
2358 CALL torch_dict_insert(features%inputs,
"grid_weights", features%grid_weights_t)
2360 features%atomic_grid_weights_t)
2362 features%atomic_grid_sizes_t)
2364 features%atomic_grid_size_bound_shape_t)
2365 features%owns_weight_tensors = .true.
2383 features%owns_inputs = .false.
2384 features%coarse_0_atomic_coords_t =
cached_layout%coarse_0_atomic_coords_t
2388 IF (requires_coordinate_grad .OR. requires_stress_grad)
THEN
2389 cpassert(.NOT. use_atom_chunks)
2391 features%coarse_0_atomic_coords)
2394 features%coarse_0_atomic_coords_t)
2395 features%owns_coordinate_tensor = .true.
2398 END SUBROUTINE add_feature_tensors
2406 FUNCTION grid_coordinate(pw_grid, index)
RESULT(coord)
2408 INTEGER,
DIMENSION(3),
INTENT(IN) :: index
2409 REAL(kind=
dp),
DIMENSION(3) :: coord
2411 INTEGER,
DIMENSION(3) :: relative_index
2413 relative_index = index - pw_grid%bounds(1, :)
2414 coord = real(relative_index(1), kind=
dp)*pw_grid%dh(:, 1) + &
2415 REAL(relative_index(2), kind=
dp)*pw_grid%dh(:, 2) + &
2416 REAL(relative_index(3), kind=
dp)*pw_grid%dh(:, 3)
2418 END FUNCTION grid_coordinate
2431 distances, pair_distances)
2432 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
2433 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
2435 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: weights
2436 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(OUT) :: partition_atom_coords
2437 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: distances
2438 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN), &
2439 OPTIONAL :: pair_distances
2441 INTEGER :: iatom, jatom, natom
2442 REAL(kind=
dp) :: mu, rab, rsum, switch, total
2443 REAL(kind=
dp),
DIMENSION(3) :: rij
2445 natom =
SIZE(atom_coords, 2)
2446 cpassert(
SIZE(weights) == natom)
2447 cpassert(
SIZE(partition_atom_coords, 1) == 3)
2448 cpassert(
SIZE(partition_atom_coords, 2) == natom)
2449 cpassert(
SIZE(distances) == natom)
2451 IF (
PRESENT(pair_distances))
THEN
2453 rij = grid_point - atom_coords(:, iatom)
2454 distances(iatom) = sqrt(sum(rij**2))
2458 partition_atom_coords(:, iatom) = &
2459 nearest_atom_image_coordinate(atom_coords(:, iatom), grid_point, cell)
2460 rij = grid_point - partition_atom_coords(:, iatom)
2461 distances(iatom) = sqrt(sum(rij**2))
2466 DO iatom = 1, natom - 1
2467 DO jatom = iatom + 1, natom
2468 IF (
PRESENT(pair_distances))
THEN
2469 rab = pair_distances(iatom, jatom)
2471 rij = partition_atom_coords(:, iatom) - partition_atom_coords(:, jatom)
2472 rab = sqrt(sum(rij**2))
2474 IF (rab <= layout_tol) cycle
2475 mu = (distances(iatom) - distances(jatom))/rab
2476 mu = max(-1.0_dp, min(1.0_dp, mu))
2477 switch = 0.5_dp*(1.0_dp - becke_shape(mu))
2478 weights(iatom) = weights(iatom)*switch
2479 weights(jatom) = weights(jatom)*(1.0_dp - switch)
2483 total = sum(weights)
2484 IF (total > 0.0_dp)
THEN
2485 weights = weights/total
2490 IF (distances(iatom) < rsum)
THEN
2491 rsum = distances(iatom)
2496 weights(jatom) = 1.0_dp
2507 SUBROUTINE periodic_image_log_weights(image_coords, distances, log_weights)
2508 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: image_coords
2509 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: distances
2510 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: log_weights
2512 INTEGER :: iimage, image_count, jimage, &
2514 REAL(kind=
dp) :: factor, mu, rab, reference_log_weight, &
2516 REAL(kind=
dp),
DIMENSION(3) :: pair
2518 image_count =
SIZE(distances)
2519 cpassert(
SIZE(image_coords, 1) == 3)
2520 cpassert(
SIZE(image_coords, 2) == image_count)
2521 cpassert(
SIZE(log_weights) == image_count)
2523 nearest_image = minloc(distances, dim=1)
2524 log_weights = -huge(1.0_dp)
2525 reference_log_weight = 0.0_dp
2526 DO jimage = 1, image_count
2527 IF (jimage == nearest_image) cycle
2528 pair = image_coords(:, nearest_image) - image_coords(:, jimage)
2529 rab = sqrt(sum(pair**2))
2530 IF (rab <= layout_tol) cycle
2531 mu = max(-1.0_dp, min(1.0_dp, &
2532 (distances(nearest_image) - distances(jimage))/rab))
2533 factor = 0.5_dp*(1.0_dp - becke_shape(mu))
2534 cpassert(factor > tiny(1.0_dp))
2535 reference_log_weight = reference_log_weight + log(factor)
2537 log_weights(nearest_image) = reference_log_weight
2543 screen_log_weight = reference_log_weight + &
2544 log(epsilon(1.0_dp)/real(image_count, kind=
dp))
2545 DO iimage = 1, image_count
2546 IF (iimage == nearest_image) cycle
2547 pair = image_coords(:, iimage) - image_coords(:, nearest_image)
2548 rab = sqrt(sum(pair**2))
2549 IF (rab <= layout_tol) cycle
2550 mu = max(-1.0_dp, min(1.0_dp, &
2551 (distances(iimage) - distances(nearest_image))/rab))
2552 factor = 0.5_dp*(1.0_dp - becke_shape(mu))
2553 IF (factor <= tiny(1.0_dp)) cycle
2554 log_weights(iimage) = log(factor)
2555 IF (log_weights(iimage) <= screen_log_weight)
THEN
2556 log_weights(iimage) = -huge(1.0_dp)
2560 DO jimage = 1, image_count
2561 IF (jimage == iimage .OR. jimage == nearest_image) cycle
2562 pair = image_coords(:, iimage) - image_coords(:, jimage)
2563 rab = sqrt(sum(pair**2))
2564 IF (rab <= layout_tol) cycle
2565 mu = max(-1.0_dp, min(1.0_dp, &
2566 (distances(iimage) - distances(jimage))/rab))
2567 factor = 0.5_dp*(1.0_dp - becke_shape(mu))
2568 IF (factor <= tiny(1.0_dp))
THEN
2569 log_weights(iimage) = -huge(1.0_dp)
2572 log_weights(iimage) = log_weights(iimage) + log(factor)
2573 IF (log_weights(iimage) <= screen_log_weight)
THEN
2574 log_weights(iimage) = -huge(1.0_dp)
2580 END SUBROUTINE periodic_image_log_weights
2588 SUBROUTINE periodic_image_normalized_weights(image_coords, distances, normalized_weights)
2589 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: image_coords
2590 REAL(kind=
dp),
DIMENSION(:),
INTENT(IN) :: distances
2591 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: normalized_weights
2593 INTEGER :: iimage, image_count, jimage, &
2595 LOGICAL :: use_log_fallback
2596 REAL(kind=
dp) :: factor, max_log_weight, mu, &
2597 normalization, product, rab, &
2598 reference_weight, screen_factor, &
2600 REAL(kind=
dp),
DIMENSION(3) :: pair
2601 REAL(kind=
dp),
DIMENSION(SIZE(distances)) :: log_weights, raw_weights
2603 image_count =
SIZE(distances)
2604 cpassert(
SIZE(image_coords, 1) == 3)
2605 cpassert(
SIZE(image_coords, 2) == image_count)
2606 cpassert(
SIZE(normalized_weights) == image_count)
2607 IF (image_count == 1)
THEN
2608 normalized_weights(1) = 1.0_dp
2612 nearest_image = minloc(distances, dim=1)
2613 reference_weight = 1.0_dp
2614 use_log_fallback = .false.
2615 DO jimage = 1, image_count
2616 IF (jimage == nearest_image) cycle
2617 pair = image_coords(:, nearest_image) - image_coords(:, jimage)
2618 rab = sqrt(sum(pair**2))
2619 IF (rab <= layout_tol) cycle
2620 mu = max(-1.0_dp, min(1.0_dp, &
2621 (distances(nearest_image) - distances(jimage))/rab))
2622 factor = 0.5_dp*(1.0_dp - becke_shape(mu))
2623 cpassert(factor > tiny(1.0_dp))
2624 IF (reference_weight <= tiny(1.0_dp)/factor)
THEN
2625 use_log_fallback = .true.
2628 reference_weight = reference_weight*factor
2631 screen_factor = epsilon(1.0_dp)/real(image_count, kind=
dp)
2632 IF (.NOT. use_log_fallback)
THEN
2633 use_log_fallback = reference_weight <= tiny(1.0_dp)/screen_factor
2635 IF (use_log_fallback)
THEN
2636 CALL periodic_image_log_weights(image_coords, distances, log_weights)
2637 max_log_weight = maxval(log_weights)
2638 cpassert(max_log_weight > -0.5_dp*huge(1.0_dp))
2639 normalized_weights = 0.0_dp
2640 WHERE (log_weights > -0.5_dp*huge(1.0_dp))
2641 normalized_weights = exp(log_weights - max_log_weight)
2643 normalization = sum(normalized_weights)
2644 cpassert(normalization > 0.0_dp)
2645 normalized_weights = normalized_weights/normalization
2649 screen_weight = reference_weight*screen_factor
2650 raw_weights = 0.0_dp
2651 raw_weights(nearest_image) = reference_weight
2652 DO iimage = 1, image_count
2653 IF (iimage == nearest_image) cycle
2654 pair = image_coords(:, iimage) - image_coords(:, nearest_image)
2655 rab = sqrt(sum(pair**2))
2656 IF (rab <= layout_tol) cycle
2657 mu = max(-1.0_dp, min(1.0_dp, &
2658 (distances(iimage) - distances(nearest_image))/rab))
2659 factor = 0.5_dp*(1.0_dp - becke_shape(mu))
2660 IF (factor <= tiny(1.0_dp) .OR. factor <= screen_weight) cycle
2663 DO jimage = 1, image_count
2664 IF (jimage == iimage .OR. jimage == nearest_image) cycle
2665 pair = image_coords(:, iimage) - image_coords(:, jimage)
2666 rab = sqrt(sum(pair**2))
2667 IF (rab <= layout_tol) cycle
2668 mu = max(-1.0_dp, min(1.0_dp, &
2669 (distances(iimage) - distances(jimage))/rab))
2670 factor = 0.5_dp*(1.0_dp - becke_shape(mu))
2671 IF (factor <= tiny(1.0_dp) .OR. product <= screen_weight/factor)
THEN
2675 product = product*factor
2677 raw_weights(iimage) = product
2680 normalization = sum(raw_weights)
2681 cpassert(normalization > 0.0_dp)
2682 normalized_weights = raw_weights/normalization
2684 END SUBROUTINE periodic_image_normalized_weights
2696 image_coords, target_image)
2697 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
2699 INTEGER,
INTENT(IN) :: target_atom
2700 INTEGER,
DIMENSION(3),
INTENT(IN),
OPTIONAL :: image_periodicity
2701 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :), &
2702 INTENT(OUT) :: image_coords
2703 INTEGER,
INTENT(OUT) :: target_image
2705 INTEGER :: base_shift(3), iatom, idir, image_count, &
2706 image_index, ix, ix_max, ix_min, iy, &
2707 iy_max, iy_min, iz, iz_max, iz_min, &
2709 INTEGER,
DIMENSION(3) :: partition_periodicity
2710 REAL(kind=
dp),
DIMENSION(3) :: fractional
2712 natom =
SIZE(atom_coords, 2)
2713 cpassert(
ASSOCIATED(cell))
2714 cpassert(target_atom >= 1 .AND. target_atom <= natom)
2715 cpassert(
SIZE(atom_coords, 1) == 3)
2716 partition_periodicity = cell%perd
2717 IF (
PRESENT(image_periodicity)) partition_periodicity = image_periodicity
2718 cpassert(all(partition_periodicity == 0 .OR. partition_periodicity == 1))
2722 IF (partition_periodicity(idir) == 1)
THEN
2723 image_count = image_count*(2*periodic_partition_image_shell + 1)
2726 ALLOCATE (image_coords(3, image_count))
2731 fractional = matmul(cell%h_inv, &
2732 atom_coords(:, target_atom) - atom_coords(:, iatom))
2734 base_shift(idir) = partition_periodicity(idir)*nint(fractional(idir))
2736 ix_min = base_shift(1) - periodic_partition_image_shell*partition_periodicity(1)
2737 ix_max = base_shift(1) + periodic_partition_image_shell*partition_periodicity(1)
2738 iy_min = base_shift(2) - periodic_partition_image_shell*partition_periodicity(2)
2739 iy_max = base_shift(2) + periodic_partition_image_shell*partition_periodicity(2)
2740 iz_min = base_shift(3) - periodic_partition_image_shell*partition_periodicity(3)
2741 iz_max = base_shift(3) + periodic_partition_image_shell*partition_periodicity(3)
2742 DO iz = iz_min, iz_max
2743 DO iy = iy_min, iy_max
2744 DO ix = ix_min, ix_max
2745 image_index = image_index + 1
2746 image_coords(:, image_index) = atom_coords(:, iatom) + &
2747 matmul(cell%hmat, real([ix, iy, iz], kind=
dp))
2748 IF (iatom == target_atom .AND. all([ix, iy, iz] == 0))
THEN
2749 target_image = image_index
2755 cpassert(image_index == image_count)
2756 cpassert(target_image > 0)
2769 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
2770 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: image_coords
2771 INTEGER,
INTENT(IN) :: target_image
2772 REAL(kind=
dp),
INTENT(OUT) :: weight
2774 INTEGER :: iimage, image_count
2775 REAL(kind=
dp),
DIMENSION(SIZE(image_coords, 2)) :: distances, normalized_weights
2777 image_count =
SIZE(image_coords, 2)
2778 cpassert(
SIZE(image_coords, 1) == 3)
2779 cpassert(target_image >= 1 .AND. target_image <= image_count)
2780 IF (image_count == 1)
THEN
2786 DO iimage = 1, image_count
2787 distances(iimage) = sqrt(sum((grid_point - image_coords(:, iimage))**2))
2789 CALL periodic_image_normalized_weights(image_coords, distances, normalized_weights)
2790 weight = normalized_weights(target_image)
2803 SUBROUTINE periodic_atom_image_partition_value(grid_point, atom_coords, cell, target_atom, &
2804 weight, image_periodicity)
2805 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
2806 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
2808 INTEGER,
INTENT(IN) :: target_atom
2809 REAL(kind=
dp),
INTENT(OUT) :: weight
2810 INTEGER,
DIMENSION(3),
INTENT(IN),
OPTIONAL :: image_periodicity
2812 INTEGER :: target_image
2813 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: image_coords
2816 image_coords, target_image)
2818 DEALLOCATE (image_coords)
2820 END SUBROUTINE periodic_atom_image_partition_value
2837 dweight_datom, dweight_dstrain, image_periodicity)
2838 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
2839 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
2841 INTEGER,
INTENT(IN) :: target_atom
2842 REAL(kind=
dp),
INTENT(OUT) :: weight
2843 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(OUT), &
2844 OPTIONAL :: dweight_datom, dweight_dstrain
2845 INTEGER,
DIMENSION(3),
INTENT(IN),
OPTIONAL :: image_periodicity
2847 INTEGER :: active_count, active_image, active_index, base_shift(3), candidate_image, iatom, &
2848 idir, iimage, image_count, image_index, ix, ix_max, ix_min, iy, iy_max, iy_min, iz, &
2849 iz_max, iz_min, jatom, jdir, jimage, natom, target_image
2850 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: active_images, image_atom
2851 INTEGER,
ALLOCATABLE,
DIMENSION(:, :) :: image_shift
2852 INTEGER,
DIMENSION(3) :: partition_periodicity
2853 LOGICAL :: need_derivatives
2854 REAL(kind=
dp) :: dist_diff, ds_dmu, mu, mu_raw, &
2855 one_minus_switch, rab, switch
2856 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: distances, normalized_weights
2857 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: image_coords, mean_log_derivative, &
2858 rvecs, target_log_derivative, &
2860 REAL(kind=
dp),
DIMENSION(3) :: dmu_image_i, dmu_image_j, ds_image_i, ds_image_j, fractional, &
2861 image_translation, image_weight_derivative, pair, unit_pair
2863 natom =
SIZE(atom_coords, 2)
2864 cpassert(
ASSOCIATED(cell))
2865 cpassert(target_atom >= 1 .AND. target_atom <= natom)
2866 cpassert(
SIZE(atom_coords, 1) == 3)
2867 need_derivatives =
PRESENT(dweight_datom)
2868 cpassert(
PRESENT(dweight_dstrain) .EQV. need_derivatives)
2869 IF (.NOT. need_derivatives)
THEN
2870 CALL periodic_atom_image_partition_value( &
2871 grid_point, atom_coords, cell, target_atom, weight, image_periodicity)
2874 IF (need_derivatives)
THEN
2875 cpassert(
SIZE(dweight_datom, 1) == 3)
2876 cpassert(
SIZE(dweight_datom, 2) == natom)
2877 cpassert(
SIZE(dweight_dstrain, 1) == 3)
2878 cpassert(
SIZE(dweight_dstrain, 2) == 3)
2879 dweight_datom = 0.0_dp
2880 dweight_dstrain = 0.0_dp
2883 partition_periodicity = cell%perd
2884 IF (
PRESENT(image_periodicity)) partition_periodicity = image_periodicity
2885 cpassert(all(partition_periodicity == 0 .OR. partition_periodicity == 1))
2889 IF (partition_periodicity(idir) == 1)
THEN
2890 image_count = image_count*(2*periodic_partition_image_shell + 1)
2893 image_count = natom*image_count
2894 ALLOCATE (active_images(image_count), image_atom(image_count), image_shift(3, image_count), &
2895 image_coords(3, image_count), distances(image_count), &
2896 normalized_weights(image_count), &
2897 rvecs(3, image_count), unit_rvecs(3, image_count), &
2898 mean_log_derivative(3, image_count), &
2899 target_log_derivative(3, image_count))
2904 fractional = matmul(cell%h_inv, &
2905 atom_coords(:, target_atom) - atom_coords(:, iatom))
2907 base_shift(idir) = partition_periodicity(idir)*nint(fractional(idir))
2909 ix_min = base_shift(1) - periodic_partition_image_shell*partition_periodicity(1)
2910 ix_max = base_shift(1) + periodic_partition_image_shell*partition_periodicity(1)
2911 iy_min = base_shift(2) - periodic_partition_image_shell*partition_periodicity(2)
2912 iy_max = base_shift(2) + periodic_partition_image_shell*partition_periodicity(2)
2913 iz_min = base_shift(3) - periodic_partition_image_shell*partition_periodicity(3)
2914 iz_max = base_shift(3) + periodic_partition_image_shell*partition_periodicity(3)
2915 DO iz = iz_min, iz_max
2916 DO iy = iy_min, iy_max
2917 DO ix = ix_min, ix_max
2918 image_index = image_index + 1
2919 image_atom(image_index) = iatom
2920 image_shift(:, image_index) = [ix, iy, iz]
2921 image_coords(:, image_index) = atom_coords(:, iatom) + &
2922 matmul(cell%hmat, real([ix, iy, iz], kind=
dp))
2923 IF (iatom == target_atom .AND. all(image_shift(:, image_index) == 0))
THEN
2924 target_image = image_index
2930 cpassert(image_index == image_count)
2931 DO iimage = 1, image_count
2932 rvecs(:, iimage) = grid_point - image_coords(:, iimage)
2933 distances(iimage) = sqrt(sum(rvecs(:, iimage)**2))
2934 IF (distances(iimage) > layout_tol)
THEN
2935 unit_rvecs(:, iimage) = rvecs(:, iimage)/distances(iimage)
2937 unit_rvecs(:, iimage) = 0.0_dp
2941 CALL periodic_image_normalized_weights(image_coords, distances, normalized_weights)
2942 weight = normalized_weights(target_image)
2944 IF (need_derivatives .AND. weight > 0.0_dp)
THEN
2946 DO iimage = 1, image_count
2947 IF (normalized_weights(iimage) > 0.0_dp)
THEN
2948 active_count = active_count + 1
2949 active_images(active_count) = iimage
2952 cpassert(active_count > 0)
2956 mean_log_derivative = 0.0_dp
2957 target_log_derivative = 0.0_dp
2958 DO active_index = 1, active_count
2959 active_image = active_images(active_index)
2960 DO candidate_image = 1, image_count
2961 IF (candidate_image == active_image) cycle
2963 IF (normalized_weights(candidate_image) > 0.0_dp .AND. &
2964 candidate_image < active_image) cycle
2965 iimage = min(active_image, candidate_image)
2966 jimage = max(active_image, candidate_image)
2967 pair = image_coords(:, iimage) - image_coords(:, jimage)
2968 rab = sqrt(sum(pair**2))
2969 IF (rab <= layout_tol) cycle
2970 unit_pair = pair/rab
2971 dist_diff = distances(iimage) - distances(jimage)
2972 mu_raw = dist_diff/rab
2973 mu = max(-1.0_dp, min(1.0_dp, mu_raw))
2974 switch = 0.5_dp*(1.0_dp - becke_shape(mu))
2975 one_minus_switch = 1.0_dp - switch
2976 IF (abs(mu_raw) < 1.0_dp)
THEN
2977 ds_dmu = -0.5_dp*becke_shape_derivative(mu)
2981 IF (abs(ds_dmu) <= 0.0_dp .OR. switch <= tiny(1.0_dp) .OR. &
2982 one_minus_switch <= tiny(1.0_dp)) cycle
2983 dmu_image_i = (-unit_rvecs(:, iimage)*rab - dist_diff*unit_pair)/rab**2
2984 dmu_image_j = (unit_rvecs(:, jimage)*rab + dist_diff*unit_pair)/rab**2
2985 ds_image_i = ds_dmu*dmu_image_i
2986 ds_image_j = ds_dmu*dmu_image_j
2988 mean_log_derivative(:, iimage) = mean_log_derivative(:, iimage) + &
2989 normalized_weights(iimage)*ds_image_i/switch - &
2990 normalized_weights(jimage)*ds_image_i/one_minus_switch
2991 mean_log_derivative(:, jimage) = mean_log_derivative(:, jimage) + &
2992 normalized_weights(iimage)*ds_image_j/switch - &
2993 normalized_weights(jimage)*ds_image_j/one_minus_switch
2994 IF (target_image == iimage)
THEN
2995 target_log_derivative(:, iimage) = &
2996 target_log_derivative(:, iimage) + ds_image_i/switch
2997 target_log_derivative(:, jimage) = &
2998 target_log_derivative(:, jimage) + ds_image_j/switch
2999 ELSE IF (target_image == jimage)
THEN
3000 target_log_derivative(:, iimage) = &
3001 target_log_derivative(:, iimage) - ds_image_i/one_minus_switch
3002 target_log_derivative(:, jimage) = &
3003 target_log_derivative(:, jimage) - ds_image_j/one_minus_switch
3007 DO jimage = 1, image_count
3008 image_weight_derivative = weight*(target_log_derivative(:, jimage) - &
3009 mean_log_derivative(:, jimage))
3010 jatom = image_atom(jimage)
3011 dweight_datom(:, jatom) = dweight_datom(:, jatom) + &
3012 image_weight_derivative
3013 image_translation = matmul( &
3014 cell%hmat, real(image_shift(:, jimage), kind=
dp))
3017 dweight_dstrain(idir, jdir) = dweight_dstrain(idir, jdir) + &
3018 image_weight_derivative(idir)* &
3019 image_translation(jdir)
3025 DEALLOCATE (active_images, image_atom, image_shift, image_coords, distances, &
3026 normalized_weights, rvecs, unit_rvecs, mean_log_derivative, &
3027 target_log_derivative)
3042 weights, included, dweights_datom, &
3044 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
3045 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
3047 REAL(kind=
dp),
DIMENSION(:),
INTENT(OUT) :: weights
3048 LOGICAL,
DIMENSION(:),
INTENT(OUT) :: included
3049 REAL(kind=
dp),
DIMENSION(:, :, :),
INTENT(OUT) :: dweights_datom, dweights_dstrain
3051 INTEGER :: iatom, idir, jatom, jdir, natom
3052 REAL(kind=
dp) :: dist_diff, ds_dmu, included_sum, mu, &
3053 mu_raw, one_minus_switch, rab, rsum, &
3055 REAL(kind=
dp),
DIMENSION(3) :: dmu_atom_i, dmu_atom_j, ds_atom_i, &
3056 ds_atom_j, pair, unit_pair
3057 REAL(kind=
dp),
DIMENSION(3, 3) :: dmu_strain, ds_strain, mean_strain
3058 REAL(kind=
dp),
DIMENSION(3, SIZE(atom_coords, 2), &
SIZE(atom_coords, 2)) :: log_weight_atom
3059 REAL(kind=
dp),
DIMENSION(3, SIZE(atom_coords, 2)) :: mean_atom, partition_atom_coords, rvecs, &
3062 DIMENSION(3, 3, SIZE(atom_coords, 2)) :: log_weight_strain
3063 REAL(kind=
dp),
DIMENSION(SIZE(atom_coords, 2)) :: distances, normalized_weights, &
3066 natom =
SIZE(atom_coords, 2)
3067 cpassert(
SIZE(weights) == natom)
3068 cpassert(
SIZE(included) == natom)
3069 cpassert(
SIZE(dweights_datom, 1) == 3)
3070 cpassert(
SIZE(dweights_datom, 2) == natom)
3071 cpassert(
SIZE(dweights_datom, 3) == natom)
3072 cpassert(
SIZE(dweights_dstrain, 1) == 3)
3073 cpassert(
SIZE(dweights_dstrain, 2) == 3)
3074 cpassert(
SIZE(dweights_dstrain, 3) == natom)
3078 dweights_datom = 0.0_dp
3079 dweights_dstrain = 0.0_dp
3080 raw_weights = 1.0_dp
3081 log_weight_atom = 0.0_dp
3082 log_weight_strain = 0.0_dp
3085 partition_atom_coords(:, iatom) = &
3086 nearest_atom_image_coordinate(atom_coords(:, iatom), grid_point, cell)
3087 rvecs(:, iatom) = grid_point - partition_atom_coords(:, iatom)
3088 distances(iatom) = sqrt(sum(rvecs(:, iatom)**2))
3089 IF (distances(iatom) > layout_tol)
THEN
3090 unit_rvecs(:, iatom) = rvecs(:, iatom)/distances(iatom)
3092 unit_rvecs(:, iatom) = 0.0_dp
3096 DO iatom = 1, natom - 1
3097 DO jatom = iatom + 1, natom
3098 pair = partition_atom_coords(:, iatom) - partition_atom_coords(:, jatom)
3099 rab = sqrt(sum(pair**2))
3100 IF (rab <= layout_tol) cycle
3101 unit_pair = pair/rab
3102 dist_diff = distances(iatom) - distances(jatom)
3103 mu_raw = dist_diff/rab
3104 mu = max(-1.0_dp, min(1.0_dp, mu_raw))
3105 switch = 0.5_dp*(1.0_dp - becke_shape(mu))
3106 one_minus_switch = 1.0_dp - switch
3108 IF (abs(mu_raw) < 1.0_dp)
THEN
3109 ds_dmu = -0.5_dp*becke_shape_derivative(mu)
3113 IF (abs(ds_dmu) > 0.0_dp .AND. switch > tiny(1.0_dp) .AND. &
3114 one_minus_switch > tiny(1.0_dp))
THEN
3115 dmu_atom_i = (-unit_rvecs(:, iatom)*rab - dist_diff*unit_pair)/rab**2
3116 dmu_atom_j = (unit_rvecs(:, jatom)*rab + dist_diff*unit_pair)/rab**2
3117 ds_atom_i = ds_dmu*dmu_atom_i
3118 ds_atom_j = ds_dmu*dmu_atom_j
3119 log_weight_atom(:, iatom, iatom) = &
3120 log_weight_atom(:, iatom, iatom) + ds_atom_i/switch
3121 log_weight_atom(:, iatom, jatom) = &
3122 log_weight_atom(:, iatom, jatom) - ds_atom_i/one_minus_switch
3123 log_weight_atom(:, jatom, iatom) = &
3124 log_weight_atom(:, jatom, iatom) + ds_atom_j/switch
3125 log_weight_atom(:, jatom, jatom) = &
3126 log_weight_atom(:, jatom, jatom) - ds_atom_j/one_minus_switch
3130 dmu_strain(idir, jdir) = &
3131 ((unit_rvecs(idir, iatom)*rvecs(jdir, iatom) - &
3132 unit_rvecs(idir, jatom)*rvecs(jdir, jatom))*rab - &
3133 dist_diff*unit_pair(idir)*pair(jdir))/rab**2
3136 ds_strain = ds_dmu*dmu_strain
3137 log_weight_strain(:, :, iatom) = &
3138 log_weight_strain(:, :, iatom) + ds_strain/switch
3139 log_weight_strain(:, :, jatom) = &
3140 log_weight_strain(:, :, jatom) - ds_strain/one_minus_switch
3143 raw_weights(iatom) = raw_weights(iatom)*switch
3144 raw_weights(jatom) = raw_weights(jatom)*one_minus_switch
3148 total = sum(raw_weights)
3149 IF (total > 0.0_dp)
THEN
3150 normalized_weights = raw_weights/total
3151 included = normalized_weights > smooth_partition_eps
3156 IF (distances(iatom) < rsum)
THEN
3157 rsum = distances(iatom)
3161 included(jatom) = .true.
3162 weights(jatom) = 1.0_dp
3166 included_sum = sum(raw_weights, mask=included)
3167 IF (included_sum <= 0.0_dp)
THEN
3171 IF (distances(iatom) < rsum)
THEN
3172 rsum = distances(iatom)
3177 included(jatom) = .true.
3179 weights(jatom) = 1.0_dp
3184 IF (included(iatom)) weights(iatom) = raw_weights(iatom)/included_sum
3188 mean_strain = 0.0_dp
3190 IF (.NOT. included(iatom)) cycle
3191 mean_strain = mean_strain + weights(iatom)*log_weight_strain(:, :, iatom)
3193 mean_atom(:, jatom) = mean_atom(:, jatom) + &
3194 weights(iatom)*log_weight_atom(:, jatom, iatom)
3199 IF (.NOT. included(iatom)) cycle
3200 dweights_dstrain(:, :, iatom) = &
3201 weights(iatom)*(log_weight_strain(:, :, iatom) - mean_strain)
3203 dweights_datom(:, jatom, iatom) = &
3204 weights(iatom)*(log_weight_atom(:, jatom, iatom) - mean_atom(:, jatom))
3216 REAL(kind=
dp),
INTENT(IN) :: weight
3217 REAL(kind=
dp) :: scale
3221 IF (weight <= smooth_partition_eps)
THEN
3223 ELSE IF (weight >= smooth_partition_taper_end)
THEN
3226 x = (weight - smooth_partition_eps)/ &
3227 (smooth_partition_taper_end - smooth_partition_eps)
3228 scale = x**3*(10.0_dp + x*(-15.0_dp + 6.0_dp*x))
3239 REAL(kind=
dp),
INTENT(IN) :: weight
3240 REAL(kind=
dp) :: derivative
3244 IF (weight <= smooth_partition_eps .OR. weight >= smooth_partition_taper_end)
THEN
3247 x = (weight - smooth_partition_eps)/ &
3248 (smooth_partition_taper_end - smooth_partition_eps)
3249 derivative = 30.0_dp*x**2*(1.0_dp - x)**2/ &
3250 (smooth_partition_taper_end - smooth_partition_eps)
3260 PURE FUNCTION becke_shape(mu)
RESULT(val)
3261 REAL(kind=
dp),
INTENT(IN) :: mu
3262 REAL(kind=
dp) :: val
3268 val = 0.5_dp*val*(3.0_dp - val*val)
3271 END FUNCTION becke_shape
3278 PURE FUNCTION becke_shape_derivative(mu)
RESULT(val)
3279 REAL(kind=
dp),
INTENT(IN) :: mu
3280 REAL(kind=
dp) :: val
3288 val = val*1.5_dp*(1.0_dp - x*x)
3289 x = 0.5_dp*x*(3.0_dp - x*x)
3292 END FUNCTION becke_shape_derivative
3301 FUNCTION nearest_atom_image_coordinate(atom_coord, grid_point, cell)
RESULT(coord)
3302 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: atom_coord, grid_point
3304 REAL(kind=
dp),
DIMENSION(3) :: coord
3306 REAL(kind=
dp) :: dx, dy, dz
3308 IF (cell%orthorhombic)
THEN
3309 dx = atom_coord(1) - grid_point(1)
3310 dy = atom_coord(2) - grid_point(2)
3311 dz = atom_coord(3) - grid_point(3)
3312 dx = dx - cell%hmat(1, 1)*cell%perd(1)*anint(cell%h_inv(1, 1)*dx)
3313 dy = dy - cell%hmat(2, 2)*cell%perd(2)*anint(cell%h_inv(2, 2)*dy)
3314 dz = dz - cell%hmat(3, 3)*cell%perd(3)*anint(cell%h_inv(3, 3)*dz)
3315 coord = grid_point + [dx, dy, dz]
3317 coord = grid_point +
pbc(grid_point, atom_coord, cell)
3320 END FUNCTION nearest_atom_image_coordinate
3329 FUNCTION nearest_atom(grid_point, atom_coords, cell)
RESULT(owner)
3330 REAL(kind=
dp),
DIMENSION(3),
INTENT(IN) :: grid_point
3331 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(IN) :: atom_coords
3336 REAL(kind=
dp) :: best_r2, dx, dy, dz, r2
3337 REAL(kind=
dp),
DIMENSION(3) :: rij
3340 best_r2 = huge(1.0_dp)
3341 IF (cell%orthorhombic)
THEN
3342 DO iatom = 1,
SIZE(atom_coords, 2)
3343 dx = grid_point(1) - atom_coords(1, iatom)
3344 dy = grid_point(2) - atom_coords(2, iatom)
3345 dz = grid_point(3) - atom_coords(3, iatom)
3346 dx = dx - cell%hmat(1, 1)*cell%perd(1)*anint(cell%h_inv(1, 1)*dx)
3347 dy = dy - cell%hmat(2, 2)*cell%perd(2)*anint(cell%h_inv(2, 2)*dy)
3348 dz = dz - cell%hmat(3, 3)*cell%perd(3)*anint(cell%h_inv(3, 3)*dz)
3349 r2 = dx*dx + dy*dy + dz*dz
3350 IF (r2 < best_r2)
THEN
3356 DO iatom = 1,
SIZE(atom_coords, 2)
3357 rij =
pbc(grid_point, atom_coords(:, iatom), cell)
3359 IF (r2 < best_r2)
THEN
3366 END FUNCTION nearest_atom
Handles all functions related to the CELL.
various utilities that regard array of different kinds: output, allocation,... maybe it is not a good...
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp
Interface to the message passing library MPI.
Define the data structure for the particle information.
Build SKALA TorchScript feature dictionaries from CP2K GPW real-space grids.
subroutine, public periodic_atom_image_partition_from_layout(grid_point, image_coords, target_image, weight)
Return an image-complete periodic atom weight for a prebuilt image layout.
subroutine, public skala_gpw_atom_subchunk_layout(max_rows, atom_begin, atom_count, row_begin, row_count)
Build atom and row bounds for all atom-contiguous subchunks in one pass.
subroutine, public skala_gpw_feature_build_atom_subchunk(parent, features, subchunk_index, max_rows, requires_grad)
Build an atom-contiguous subchunk feature bundle from a rank-local atom chunk.
integer, parameter, public skala_gpw_atom_partition_hard
subroutine, public build_periodic_atom_image_layout(atom_coords, cell, target_atom, image_periodicity, image_coords, target_image)
Build the image coordinates shared by all points of one target-atom block.
type(skala_gpw_layout_cache_type), target, save cached_layout
pure real(kind=dp) function, public smooth_partition_atomic_weight_scale_derivative(weight)
Derivative of the sparse atom-row quadrature taper with respect to partition weight.
subroutine, public skala_gpw_feature_release(features)
Release Torch objects and backing arrays owned by a feature bundle.
integer function, public skala_gpw_atom_subchunk_count(max_rows)
Return how many atom-contiguous subchunks the cached rank chunk needs.
subroutine, public skala_gpw_feature_build(features, rho_set, rho_r, particle_set, cell, requires_grad, weights, requires_coordinate_grad, requires_stress_grad, use_atom_chunks, route_atom_chunks, atom_partition)
Build a flat SKALA molecular feature dictionary from a local GPW grid.
subroutine, public smooth_atom_partition(grid_point, atom_coords, cell, weights, partition_atom_coords, distances, pair_distances)
Build Becke-like smooth atom weights for one native-grid point.
subroutine, public skala_gpw_feature_build_atom_subchunk_bounds(parent, features, atom_begin, atom_count, row_begin, row_count, requires_grad)
Build an atom-contiguous subchunk feature bundle from precomputed bounds.
subroutine, public periodic_atom_image_partition(grid_point, atom_coords, cell, target_atom, weight, dweight_datom, dweight_dstrain, image_periodicity)
Return the smooth weight of one reference-cell atom in an image-complete periodic atom partition....
pure real(kind=dp) function, public smooth_partition_atomic_weight_scale(weight)
Smoothly suppress a sparse atom row's internal quadrature weight at the layout cutoff.
subroutine, public skala_gpw_smooth_partition_derivatives(grid_point, atom_coords, cell, weights, included, dweights_datom, dweights_dstrain)
Build smooth atom weights and their atom/cell deformation derivatives.
integer, parameter, public skala_gpw_atom_partition_smooth
subroutine, public torch_dict_release(dict)
Releases a Torch dictionary and all its ressources.
subroutine, public torch_tensor_narrow(tensor, dim, start_index, length, result)
Creates a view of a contiguous tensor slice.
subroutine, public torch_tensor_to_device_leaf(tensor, requires_grad)
Moves a tensor to the active Torch device and makes it an autograd leaf.
subroutine, public torch_dict_create(dict)
Creates an empty Torch dictionary.
subroutine, public torch_tensor_expand_dim(tensor, dim, extent, result)
Creates an expanded tensor view along one singleton dimension.
subroutine, public torch_dict_insert(dict, key, tensor)
Inserts a Torch tensor into a Torch dictionary.
subroutine, public torch_dict_clone(source, target)
Clones a Torch dictionary.
subroutine, public torch_tensor_release(tensor)
Releases a Torch tensor and all its ressources.
subroutine, public xc_rho_set_get(rho_set, can_return_null, rho, drho, norm_drho, rhoa, rhob, norm_drhoa, norm_drhob, rho_1_3, rhoa_1_3, rhob_1_3, laplace_rho, laplace_rhoa, laplace_rhob, drhoa, drhob, rho_cutoff, drho_cutoff, tau_cutoff, tau, tau_a, tau_b, local_bounds)
returns the various attributes of rho_set
Type defining parameters related to the simulation cell.
represent a pointer to a contiguous 3d array
represent a density, with all the representation and data needed to perform a functional evaluation