-
Notifications
You must be signed in to change notification settings - Fork 1
/
kg_environment.F
750 lines (628 loc) · 30.9 KB
/
kg_environment.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Routines for a Kim-Gordon-like partitioning into molecular subunits
!> \par History
!> 2012.07 created [Martin Haeufel]
!> \author Martin Haeufel
! **************************************************************************************************
MODULE kg_environment
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind
USE basis_set_types, ONLY: get_gto_basis_set,&
gto_basis_set_type
USE bibliography, ONLY: Andermatt2016,&
cite_reference
USE cell_types, ONLY: cell_type
USE cp_files, ONLY: close_file,&
open_file
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_io_unit,&
cp_logger_type
USE distribution_1d_types, ONLY: distribution_1d_type
USE distribution_2d_types, ONLY: distribution_2d_type
USE external_potential_types, ONLY: get_potential,&
local_potential_type
USE input_constants, ONLY: kg_tnadd_atomic,&
kg_tnadd_embed,&
kg_tnadd_embed_ri,&
kg_tnadd_none
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE integration_grid_types, ONLY: allocate_intgrid,&
integration_grid_type
USE kg_environment_types, ONLY: kg_environment_type
USE kg_vertex_coloring_methods, ONLY: kg_vertex_coloring
USE kinds, ONLY: dp,&
int_4,&
int_4_size,&
int_8
USE lri_environment_init, ONLY: lri_env_basis,&
lri_env_init
USE message_passing, ONLY: mp_para_env_type
USE molecule_types, ONLY: molecule_type
USE particle_types, ONLY: particle_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_grid_atom, ONLY: initialize_atomic_grid
USE qs_kind_types, ONLY: get_qs_kind,&
qs_kind_type
USE qs_neighbor_list_types, ONLY: get_iterator_info,&
neighbor_list_iterate,&
neighbor_list_iterator_create,&
neighbor_list_iterator_p_type,&
neighbor_list_iterator_release,&
neighbor_list_set_p_type,&
release_neighbor_list_sets
USE qs_neighbor_lists, ONLY: atom2d_build,&
atom2d_cleanup,&
build_neighbor_lists,&
local_atoms_type,&
pair_radius_setup,&
write_neighbor_lists
USE string_utilities, ONLY: uppercase
USE task_list_types, ONLY: deallocate_task_list
USE util, ONLY: sort
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'kg_environment'
PUBLIC :: kg_env_create, kg_build_neighborlist, kg_build_subsets
CONTAINS
! **************************************************************************************************
!> \brief Allocates and intitializes kg_env
!> \param qs_env ...
!> \param kg_env the object to create
!> \param qs_kind_set ...
!> \param input ...
!> \par History
!> 2012.07 created [Martin Haeufel]
!> \author Martin Haeufel
! **************************************************************************************************
SUBROUTINE kg_env_create(qs_env, kg_env, qs_kind_set, input)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(kg_environment_type), POINTER :: kg_env
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(section_vals_type), OPTIONAL, POINTER :: input
ALLOCATE (kg_env)
CALL init_kg_env(qs_env, kg_env, qs_kind_set, input)
END SUBROUTINE kg_env_create
! **************************************************************************************************
!> \brief Initializes kg_env
!> \param qs_env ...
!> \param kg_env ...
!> \param qs_kind_set ...
!> \param input ...
!> \par History
!> 2012.07 created [Martin Haeufel]
!> 2018.01 TNADD correction {JGH]
!> \author Martin Haeufel
! **************************************************************************************************
SUBROUTINE init_kg_env(qs_env, kg_env, qs_kind_set, input)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(kg_environment_type), POINTER :: kg_env
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(section_vals_type), OPTIONAL, POINTER :: input
CHARACTER(LEN=*), PARAMETER :: routineN = 'init_kg_env'
CHARACTER(LEN=10) :: intgrid
INTEGER :: handle, i, iatom, ib, ikind, iunit, n, &
na, natom, nbatch, nkind, np, nr
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: bid
REAL(KIND=dp) :: load, radb, rmax
TYPE(cp_logger_type), POINTER :: logger
TYPE(gto_basis_set_type), POINTER :: lri_aux_basis
TYPE(integration_grid_type), POINTER :: ig_full, ig_mol
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(qs_kind_type), POINTER :: qs_kind
TYPE(section_vals_type), POINTER :: lri_section
CALL timeset(routineN, handle)
CALL cite_reference(Andermatt2016)
NULLIFY (para_env)
NULLIFY (kg_env%sab_orb_full)
NULLIFY (kg_env%sac_kin)
NULLIFY (kg_env%subset_of_mol)
NULLIFY (kg_env%subset)
NULLIFY (kg_env%tnadd_mat)
NULLIFY (kg_env%lri_env)
NULLIFY (kg_env%lri_env1)
NULLIFY (kg_env%int_grid_atom)
NULLIFY (kg_env%int_grid_molecules)
NULLIFY (kg_env%int_grid_full)
NULLIFY (kg_env%lri_density)
NULLIFY (kg_env%lri_rho1)
kg_env%nsubsets = 0
! get coloring method settings
CALL section_vals_val_get(input, "DFT%KG_METHOD%COLORING_METHOD", i_val=kg_env%coloring_method)
! get method for nonadditive kinetic energy embedding potential
CALL section_vals_val_get(input, "DFT%KG_METHOD%TNADD_METHOD", i_val=kg_env%tnadd_method)
!
SELECT CASE (kg_env%tnadd_method)
CASE (kg_tnadd_embed, kg_tnadd_embed_ri)
! kinetic energy functional
kg_env%xc_section_kg => section_vals_get_subs_vals(input, "DFT%KG_METHOD%XC")
IF (.NOT. ASSOCIATED(kg_env%xc_section_kg)) THEN
CALL cp_abort(__LOCATION__, &
"KG runs require a kinetic energy functional set in &KG_METHOD")
END IF
CASE (kg_tnadd_atomic, kg_tnadd_none)
NULLIFY (kg_env%xc_section_kg)
CASE DEFAULT
CPABORT("KG:TNADD METHOD")
END SELECT
IF (kg_env%tnadd_method == kg_tnadd_embed_ri) THEN
! initialize the LRI environment
! Check if LRI_AUX basis is available
rmax = 0.0_dp
nkind = SIZE(qs_kind_set)
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
NULLIFY (lri_aux_basis)
CALL get_qs_kind(qs_kind, basis_set=lri_aux_basis, basis_type="LRI_AUX")
CPASSERT(ASSOCIATED(lri_aux_basis))
CALL get_gto_basis_set(gto_basis_set=lri_aux_basis, kind_radius=radb)
rmax = MAX(rmax, radb)
END DO
rmax = 1.25_dp*rmax
lri_section => section_vals_get_subs_vals(input, "DFT%KG_METHOD%LRIGPW")
CALL lri_env_init(kg_env%lri_env, lri_section)
CALL lri_env_basis("LRI", qs_env, kg_env%lri_env, qs_kind_set)
!
! if energy correction is performed with force calculation,
! then response calculation requires
! perturbation density for kernel calculation
CALL lri_env_init(kg_env%lri_env1, lri_section)
CALL lri_env_basis("LRI", qs_env, kg_env%lri_env1, qs_kind_set)
!
! integration grid
!
CALL section_vals_val_get(input, "DFT%KG_METHOD%INTEGRATION_GRID", c_val=intgrid)
CALL uppercase(intgrid)
SELECT CASE (intgrid)
CASE ("SMALL")
nr = 50
na = 38
CASE ("MEDIUM")
nr = 100
na = 110
CASE ("LARGE")
nr = 200
na = 302
CASE ("HUGE")
nr = 400
na = 590
CASE DEFAULT
CPABORT("KG:INTEGRATION_GRID")
END SELECT
NULLIFY (logger)
logger => cp_get_default_logger()
iunit = cp_logger_get_default_io_unit(logger)
CALL initialize_atomic_grid(kg_env%int_grid_atom, nr, na, rmax, iunit=iunit)
! load balancing
CALL get_qs_env(qs_env=qs_env, natom=natom, para_env=para_env)
np = para_env%num_pe
load = REAL(natom, KIND=dp)*kg_env%int_grid_atom%ntot/REAL(np, KIND=dp)
!
CALL allocate_intgrid(kg_env%int_grid_full)
ig_full => kg_env%int_grid_full
CALL allocate_intgrid(kg_env%int_grid_molecules)
ig_mol => kg_env%int_grid_molecules
nbatch = (natom*kg_env%int_grid_atom%nbatch)/np
nbatch = NINT((nbatch + 1)*1.2_dp)
ALLOCATE (bid(2, nbatch))
nbatch = 0
DO iatom = 1, natom
DO ib = 1, kg_env%int_grid_atom%nbatch
IF (para_env%mepos == MOD(iatom + ib, np)) THEN
nbatch = nbatch + 1
CPASSERT(nbatch <= SIZE(bid, 2))
bid(1, nbatch) = iatom
bid(2, nbatch) = ib
END IF
END DO
END DO
!
ig_full%nbatch = nbatch
ALLOCATE (ig_full%grid_batch(nbatch))
!
ig_mol%nbatch = nbatch
ALLOCATE (ig_mol%grid_batch(nbatch))
!
DO i = 1, nbatch
iatom = bid(1, i)
ib = bid(2, i)
!
ig_full%grid_batch(i)%ref_atom = iatom
ig_full%grid_batch(i)%ibatch = ib
ig_full%grid_batch(i)%np = kg_env%int_grid_atom%batch(ib)%np
ig_full%grid_batch(i)%radius = kg_env%int_grid_atom%batch(ib)%rad
ig_full%grid_batch(i)%rcenter(1:3) = kg_env%int_grid_atom%batch(ib)%rcenter(1:3)
n = ig_full%grid_batch(i)%np
ALLOCATE (ig_full%grid_batch(i)%rco(3, n))
ALLOCATE (ig_full%grid_batch(i)%weight(n))
ALLOCATE (ig_full%grid_batch(i)%wref(n))
ALLOCATE (ig_full%grid_batch(i)%wsum(n))
ig_full%grid_batch(i)%weight(:) = kg_env%int_grid_atom%batch(ib)%weight(:)
!
ig_mol%grid_batch(i)%ref_atom = iatom
ig_mol%grid_batch(i)%ibatch = ib
ig_mol%grid_batch(i)%np = kg_env%int_grid_atom%batch(ib)%np
ig_mol%grid_batch(i)%radius = kg_env%int_grid_atom%batch(ib)%rad
ig_mol%grid_batch(i)%rcenter(1:3) = kg_env%int_grid_atom%batch(ib)%rcenter(1:3)
n = ig_mol%grid_batch(i)%np
ALLOCATE (ig_mol%grid_batch(i)%rco(3, n))
ALLOCATE (ig_mol%grid_batch(i)%weight(n))
ALLOCATE (ig_mol%grid_batch(i)%wref(n))
ALLOCATE (ig_mol%grid_batch(i)%wsum(n))
ig_mol%grid_batch(i)%weight(:) = kg_env%int_grid_atom%batch(ib)%weight(:)
END DO
!
DEALLOCATE (bid)
END IF
CALL timestop(handle)
END SUBROUTINE init_kg_env
! **************************************************************************************************
!> \brief builds either the full neighborlist or neighborlists of molecular
!> \brief subsets, depending on parameter values
!> \param qs_env ...
!> \param sab_orb the return type, a neighborlist
!> \param sac_kin ...
!> \param molecular if false, the full neighborlist is build
!> \param subset_of_mol the molecular subsets
!> \param current_subset the subset of which the neighborlist is to be build
!> \par History
!> 2012.07 created [Martin Haeufel]
!> \author Martin Haeufel
! **************************************************************************************************
SUBROUTINE kg_build_neighborlist(qs_env, sab_orb, sac_kin, &
molecular, subset_of_mol, current_subset)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
OPTIONAL, POINTER :: sab_orb, sac_kin
LOGICAL, OPTIONAL :: molecular
INTEGER, DIMENSION(:), OPTIONAL, POINTER :: subset_of_mol
INTEGER, OPTIONAL :: current_subset
CHARACTER(LEN=*), PARAMETER :: routineN = 'kg_build_neighborlist'
INTEGER :: handle, ikind, nkind
LOGICAL :: mic, molecule_only
LOGICAL, ALLOCATABLE, DIMENSION(:) :: orb_present, tpot_present
REAL(dp) :: subcells
REAL(dp), ALLOCATABLE, DIMENSION(:) :: orb_radius, tpot_radius
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: pair_radius
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(distribution_1d_type), POINTER :: distribution_1d
TYPE(distribution_2d_type), POINTER :: distribution_2d
TYPE(gto_basis_set_type), POINTER :: orb_basis_set
TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:) :: atom2d
TYPE(local_potential_type), POINTER :: tnadd_potential
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(section_vals_type), POINTER :: neighbor_list_section
CALL timeset(routineN, handle)
NULLIFY (para_env)
! restrict lists to molecular subgroups
molecule_only = .FALSE.
IF (PRESENT(molecular)) molecule_only = molecular
! enforce minimum image convention if we use molecules
mic = molecule_only
CALL get_qs_env(qs_env=qs_env, &
atomic_kind_set=atomic_kind_set, &
qs_kind_set=qs_kind_set, &
cell=cell, &
distribution_2d=distribution_2d, &
molecule_set=molecule_set, &
local_particles=distribution_1d, &
particle_set=particle_set, &
para_env=para_env)
CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
! Allocate work storage
nkind = SIZE(atomic_kind_set)
ALLOCATE (orb_radius(nkind), tpot_radius(nkind))
orb_radius(:) = 0.0_dp
tpot_radius(:) = 0.0_dp
ALLOCATE (orb_present(nkind), tpot_present(nkind))
ALLOCATE (pair_radius(nkind, nkind))
ALLOCATE (atom2d(nkind))
CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
molecule_set, molecule_only, particle_set=particle_set)
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom2d(ikind)%list)
CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
IF (ASSOCIATED(orb_basis_set)) THEN
orb_present(ikind) = .TRUE.
IF (PRESENT(subset_of_mol)) THEN
CALL get_gto_basis_set(gto_basis_set=orb_basis_set, kind_radius=orb_radius(ikind))
ELSE
CALL get_gto_basis_set(gto_basis_set=orb_basis_set, short_kind_radius=orb_radius(ikind))
END IF
ELSE
orb_present(ikind) = .FALSE.
orb_radius(ikind) = 0.0_dp
END IF
END DO
IF (PRESENT(sab_orb)) THEN
! Build the orbital-orbital overlap neighbor list
CALL pair_radius_setup(orb_present, orb_present, orb_radius, orb_radius, pair_radius)
IF (PRESENT(subset_of_mol)) THEN
CALL build_neighbor_lists(sab_orb, particle_set, atom2d, cell, pair_radius, &
mic=mic, subcells=subcells, molecular=molecule_only, subset_of_mol=subset_of_mol, &
current_subset=current_subset, nlname="sab_orb")
ELSE
CALL build_neighbor_lists(sab_orb, particle_set, atom2d, cell, pair_radius, &
mic=mic, subcells=subcells, molecular=molecule_only, nlname="sab_orb")
END IF
! Print out the neighborlist
neighbor_list_section => section_vals_get_subs_vals(qs_env%input, "DFT%KG_METHOD%PRINT%NEIGHBOR_LISTS")
IF (molecule_only) THEN
CALL write_neighbor_lists(sab_orb, particle_set, cell, para_env, neighbor_list_section, &
"/SAB_ORB_MOLECULAR", "sab_orb", "MOLECULAR SUBSET NEIGHBORLIST")
ELSE
CALL write_neighbor_lists(sab_orb, particle_set, cell, para_env, neighbor_list_section, &
"/SAB_ORB_FULL", "sab_orb", "FULL NEIGHBORLIST")
END IF
END IF
IF (PRESENT(sac_kin)) THEN
DO ikind = 1, nkind
tpot_present(ikind) = .FALSE.
CALL get_qs_kind(qs_kind_set(ikind), tnadd_potential=tnadd_potential)
IF (ASSOCIATED(tnadd_potential)) THEN
CALL get_potential(potential=tnadd_potential, radius=tpot_radius(ikind))
tpot_present(ikind) = .TRUE.
END IF
END DO
CALL pair_radius_setup(orb_present, tpot_present, orb_radius, tpot_radius, pair_radius)
CALL build_neighbor_lists(sac_kin, particle_set, atom2d, cell, pair_radius, &
subcells=subcells, operator_type="ABC", nlname="sac_kin")
neighbor_list_section => section_vals_get_subs_vals(qs_env%input, &
"DFT%KG_METHOD%PRINT%NEIGHBOR_LISTS")
CALL write_neighbor_lists(sac_kin, particle_set, cell, para_env, neighbor_list_section, &
"/SAC_KIN", "sac_kin", "ORBITAL kin energy potential")
END IF
! Release work storage
CALL atom2d_cleanup(atom2d)
DEALLOCATE (atom2d)
DEALLOCATE (orb_present, tpot_present)
DEALLOCATE (orb_radius, tpot_radius)
DEALLOCATE (pair_radius)
CALL timestop(handle)
END SUBROUTINE kg_build_neighborlist
! **************************************************************************************************
!> \brief Removes all replicated pairs from a 2d integer buffer array
!> \param pairs_buffer the array, assumed to have the shape (2,:)
!> \param n number of pairs (in), number of disjunct pairs (out)
!> \par History
!> 2012.07 created [Martin Haeufel]
!> 2014.11 simplified [Ole Schuett]
!> \author Martin Haeufel
! **************************************************************************************************
SUBROUTINE kg_remove_duplicates(pairs_buffer, n)
INTEGER(KIND=int_4), DIMENSION(:, :), &
INTENT(INOUT) :: pairs_buffer
INTEGER, INTENT(INOUT) :: n
CHARACTER(LEN=*), PARAMETER :: routineN = 'kg_remove_duplicates'
INTEGER :: handle, i, npairs
INTEGER, DIMENSION(n) :: ind
INTEGER(KIND=int_8), DIMENSION(n) :: sort_keys
INTEGER(KIND=int_4), DIMENSION(2, n) :: work
CALL timeset(routineN, handle)
IF (n > 0) THEN
! represent a pair of int_4 as a single int_8 number, simplifies sorting.
sort_keys(1:n) = ISHFT(INT(pairs_buffer(1, 1:n), KIND=int_8), 8*int_4_size)
sort_keys(1:n) = sort_keys(1:n) + pairs_buffer(2, 1:n) !upper + lower bytes
CALL sort(sort_keys, n, ind)
! add first pair, the case npairs==0 was excluded above
npairs = 1
work(:, 1) = pairs_buffer(:, ind(1))
! remove duplicates from the sorted list
DO i = 2, n
IF (sort_keys(i) /= sort_keys(i - 1)) THEN
npairs = npairs + 1
work(:, npairs) = pairs_buffer(:, ind(i))
END IF
END DO
n = npairs
pairs_buffer(:, :n) = work(:, :n)
END IF
CALL timestop(handle)
END SUBROUTINE kg_remove_duplicates
! **************************************************************************************************
!> \brief writes the graph to file using the DIMACS standard format
!> for a definition of the file format see
!> mat.gsia.cmu.edu?COLOR/general/ccformat.ps
!> c comment line
!> p edge NODES EDGES
!> with NODES - number of nodes
!> EDGES - numer of edges
!> e W V
!> ...
!> there is one edge descriptor line for each edge in the graph
!> for an edge (w,v) the fields W and V specify its endpoints
!> \param pairs ...
!> \param nnodes the total number of nodes
! **************************************************************************************************
SUBROUTINE write_to_file(pairs, nnodes)
INTEGER(KIND=int_4), ALLOCATABLE, &
DIMENSION(:, :), INTENT(IN) :: pairs
INTEGER, INTENT(IN) :: nnodes
CHARACTER(LEN=*), PARAMETER :: routineN = 'write_to_file'
INTEGER :: handle, i, imol, jmol, npairs, unit_nr
INTEGER(KIND=int_4), ALLOCATABLE, DIMENSION(:, :) :: sorted_pairs
CALL timeset(routineN, handle)
! get the number of disjunct pairs
npairs = SIZE(pairs, 2)
ALLOCATE (sorted_pairs(2, npairs))
! reorder pairs such that pairs(1,*) < pairs(2,*)
DO i = 1, npairs
! get molecular ids
imol = pairs(1, i)
jmol = pairs(2, i)
IF (imol > jmol) THEN
! switch pair and store
sorted_pairs(1, i) = jmol
sorted_pairs(2, i) = imol
ELSE
! keep ordering just copy
sorted_pairs(1, i) = imol
sorted_pairs(2, i) = jmol
END IF
END DO
! remove duplicates and get the number of disjunct pairs (number of edges)
CALL kg_remove_duplicates(sorted_pairs, npairs)
! should now be half as much pairs as before
CPASSERT(npairs == SIZE(pairs, 2)/2)
CALL open_file(unit_number=unit_nr, file_name="graph.col")
WRITE (unit_nr, '(A6,1X,I8,1X,I8)') "p edge", nnodes, npairs
! only write out the first npairs entries
DO i = 1, npairs
WRITE (unit_nr, '(A1,1X,I8,1X,I8)') "e", sorted_pairs(1, i), sorted_pairs(2, i)
END DO
CALL close_file(unit_nr)
DEALLOCATE (sorted_pairs)
CALL timestop(handle)
END SUBROUTINE write_to_file
! **************************************************************************************************
!> \brief ...
!> \param kg_env ...
!> \param para_env ...
! **************************************************************************************************
SUBROUTINE kg_build_subsets(kg_env, para_env)
TYPE(kg_environment_type), POINTER :: kg_env
TYPE(mp_para_env_type), POINTER :: para_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'kg_build_subsets'
INTEGER :: color, handle, i, iatom, imol, isub, &
jatom, jmol, nmol, npairs, npairs_local
INTEGER(KIND=int_4) :: ncolors
INTEGER(KIND=int_4), ALLOCATABLE, DIMENSION(:) :: color_of_node
INTEGER(KIND=int_4), ALLOCATABLE, DIMENSION(:, :) :: msg_gather, pairs, pairs_buffer
INTEGER, ALLOCATABLE, DIMENSION(:) :: nnodes_of_color
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator
CALL timeset(routineN, handle)
! first: get a (local) list of pairs from the (local) neighbor list data
nmol = SIZE(kg_env%molecule_set)
npairs = 0
CALL neighbor_list_iterator_create(nl_iterator, kg_env%sab_orb_full)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom)
imol = kg_env%atom_to_molecule(iatom)
jmol = kg_env%atom_to_molecule(jatom)
!IF (imol<jmol) THEN
IF (imol .NE. jmol) THEN
npairs = npairs + 2
END IF
END DO
CALL neighbor_list_iterator_release(nl_iterator)
ALLOCATE (pairs_buffer(2, npairs))
npairs = 0
CALL neighbor_list_iterator_create(nl_iterator, kg_env%sab_orb_full)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom)
imol = kg_env%atom_to_molecule(iatom)
jmol = kg_env%atom_to_molecule(jatom)
IF (imol .NE. jmol) THEN
! add pair to the local list
! add both orderings - makes it easier to build the neighborlist
npairs = npairs + 1
pairs_buffer(1, npairs) = imol
pairs_buffer(2, npairs) = jmol
npairs = npairs + 1
pairs_buffer(2, npairs) = imol
pairs_buffer(1, npairs) = jmol
END IF
END DO
CALL neighbor_list_iterator_release(nl_iterator)
! remove duplicates
CALL kg_remove_duplicates(pairs_buffer, npairs)
! get the maximum number of local pairs on all nodes (size of the mssg)
! remember how many pairs we have local
npairs_local = npairs
CALL para_env%max(npairs)
! allocate message
ALLOCATE (pairs(2, npairs))
pairs(:, 1:npairs_local) = pairs_buffer(:, 1:npairs_local)
pairs(:, npairs_local + 1:) = 0
DEALLOCATE (pairs_buffer)
! second: gather all data on the master node
! better would be a scheme where duplicates are removed in a tree-like reduction scheme.
! this step can be needlessly memory intensive in the current implementation.
IF (para_env%is_source()) THEN
ALLOCATE (msg_gather(2, npairs*para_env%num_pe))
ELSE
ALLOCATE (msg_gather(2, 1))
END IF
msg_gather = 0
CALL para_env%gather(pairs, msg_gather)
DEALLOCATE (pairs)
IF (para_env%is_source()) THEN
! shift all non-zero entries to the beginning of the array and count the number of actual pairs
npairs = 0
DO i = 1, SIZE(msg_gather, 2)
IF (msg_gather(1, i) .NE. 0) THEN
npairs = npairs + 1
msg_gather(:, npairs) = msg_gather(:, i)
END IF
END DO
! remove duplicates
CALL kg_remove_duplicates(msg_gather, npairs)
ALLOCATE (pairs(2, npairs))
pairs(:, 1:npairs) = msg_gather(:, 1:npairs)
DEALLOCATE (msg_gather)
!WRITE(*,'(A48,5X,I10,4X,A2,1X,I10)') " KG| Total number of overlapping molecular pairs",npairs/2,"of",nmol*(nmol-1)/2
! write to file, nnodes = number of molecules
IF (.FALSE.) THEN
CALL write_to_file(pairs, SIZE(kg_env%molecule_set))
END IF
! vertex coloring algorithm
CALL kg_vertex_coloring(kg_env, pairs, ncolors, color_of_node)
DEALLOCATE (pairs)
ELSE
DEALLOCATE (msg_gather)
END IF
!WRITE(*,'(A27,40X,I6,1X,A6)') ' KG| Vertex coloring result', ncolors, 'colors'
! broadcast the number of colors to all nodes
CALL para_env%bcast(ncolors)
IF (.NOT. ALLOCATED(color_of_node)) ALLOCATE (color_of_node(nmol))
! broadcast the resulting coloring to all nodes.....
CALL para_env%bcast(color_of_node)
IF ((kg_env%nsubsets .NE. 0) .AND. (ncolors .NE. kg_env%nsubsets)) THEN
! number of subsets has changed
! deallocate stuff if necessary
IF (ASSOCIATED(kg_env%subset)) THEN
DO isub = 1, kg_env%nsubsets
CALL release_neighbor_list_sets(kg_env%subset(isub)%sab_orb)
CALL deallocate_task_list(kg_env%subset(isub)%task_list)
END DO
DEALLOCATE (kg_env%subset)
NULLIFY (kg_env%subset)
END IF
END IF
! allocate and nullify some stuff
IF (.NOT. ASSOCIATED(kg_env%subset)) THEN
ALLOCATE (kg_env%subset(ncolors))
DO i = 1, ncolors
NULLIFY (kg_env%subset(i)%sab_orb)
NULLIFY (kg_env%subset(i)%task_list)
END DO
END IF
! set the number of subsets
kg_env%nsubsets = ncolors
! counting loop
ALLOCATE (nnodes_of_color(ncolors))
nnodes_of_color = 0
DO i = 1, nmol ! nmol=nnodes
color = color_of_node(i)
kg_env%subset_of_mol(i) = color
nnodes_of_color(color) = nnodes_of_color(color) + 1
END DO
DEALLOCATE (nnodes_of_color)
DEALLOCATE (color_of_node)
CALL timestop(handle)
END SUBROUTINE kg_build_subsets
END MODULE kg_environment