-
Notifications
You must be signed in to change notification settings - Fork 1
/
mixed_cdft_utils.F
1938 lines (1870 loc) · 106 KB
/
mixed_cdft_utils.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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------------------------------!
! 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 Utility subroutines for mixed CDFT calculations
!> \par History
!> separated from mixed_cdft_methods [01.2017]
!> \author Nico Holmberg [01.2017]
! **************************************************************************************************
MODULE mixed_cdft_utils
USE atomic_kind_types, ONLY: atomic_kind_type
USE cell_types, ONLY: cell_type
USE cp_array_utils, ONLY: cp_1d_i_p_type,&
cp_1d_r_p_type,&
cp_2d_r_p_type
USE cp_blacs_env, ONLY: cp_blacs_env_create,&
cp_blacs_env_type
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: dbcsr_desymmetrize,&
dbcsr_get_info,&
dbcsr_init_p,&
dbcsr_p_type,&
dbcsr_release,&
dbcsr_release_p,&
dbcsr_type
USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
copy_fm_to_dbcsr_bc
USE cp_files, ONLY: open_file
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_copy_general,&
cp_fm_create,&
cp_fm_get_info,&
cp_fm_release,&
cp_fm_to_fm,&
cp_fm_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_create,&
cp_logger_set,&
cp_logger_type,&
cp_to_string
USE cp_output_handling, ONLY: cp_print_key_finished_output,&
cp_print_key_unit_nr
USE cp_realspace_grid_init, ONLY: init_input_type
USE cp_subsys_types, ONLY: cp_subsys_get,&
cp_subsys_type
USE cube_utils, ONLY: init_cube_info,&
return_cube_max_iradius
USE d3_poly, ONLY: init_d3_poly_module
USE force_env_types, ONLY: force_env_get,&
force_env_type,&
multiple_fe_list
USE gaussian_gridlevels, ONLY: init_gaussian_gridlevel
USE global_types, ONLY: global_environment_type
USE hirshfeld_types, ONLY: create_hirshfeld_type,&
release_hirshfeld_type,&
set_hirshfeld_info
USE input_constants, ONLY: becke_cutoff_element,&
mixed_cdft_parallel,&
mixed_cdft_parallel_nobuild,&
mixed_cdft_serial,&
outer_scf_becke_constraint,&
outer_scf_hirshfeld_constraint,&
shape_function_gaussian
USE input_section_types, ONLY: section_vals_duplicate,&
section_vals_get,&
section_vals_get_subs_vals,&
section_vals_release,&
section_vals_type,&
section_vals_val_get
USE kinds, ONLY: default_path_length,&
dp
USE message_passing, ONLY: mp_request_type,&
mp_waitall
USE mixed_cdft_types, ONLY: mixed_cdft_result_type_release,&
mixed_cdft_result_type_set,&
mixed_cdft_settings_type,&
mixed_cdft_type,&
mixed_cdft_work_type_init
USE mixed_environment_types, ONLY: get_mixed_env,&
mixed_environment_type
USE pw_env_methods, ONLY: pw_env_create
USE pw_env_types, ONLY: pw_env_get,&
pw_env_type
USE pw_grid_types, ONLY: HALFSPACE,&
pw_grid_type
USE pw_grids, ONLY: do_pw_grid_blocked_false,&
pw_grid_create,&
pw_grid_release
USE pw_pool_types, ONLY: pw_pool_create,&
pw_pool_p_type,&
pw_pool_type
USE qs_cdft_types, ONLY: cdft_control_create,&
cdft_control_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_kind_types, ONLY: create_qs_kind_set,&
qs_kind_type
USE realspace_grid_types, ONLY: realspace_grid_desc_p_type,&
realspace_grid_input_type,&
realspace_grid_type,&
rs_grid_create,&
rs_grid_create_descriptor,&
rs_grid_print
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
! *** Public subroutines ***
PUBLIC :: mixed_cdft_parse_settings, mixed_cdft_transfer_settings, &
mixed_cdft_init_structures, mixed_cdft_redistribute_arrays, &
mixed_cdft_print_couplings, map_permutation_to_states, hfun_zero, &
mixed_cdft_release_work, mixed_cdft_read_block_diag, &
mixed_cdft_get_blocks, mixed_cdft_diagonalize_blocks, &
mixed_cdft_assemble_block_diag
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mixed_cdft_utils'
CONTAINS
! **************************************************************************************************
!> \brief Parse settings for mixed cdft calculation and check their consistency
!> \param force_env the force_env that holds the CDFT mixed_env
!> \param mixed_env the mixed_env that holds the CDFT states
!> \param mixed_cdft control section for mixed CDFT
!> \param settings container for settings related to the mixed CDFT calculation
!> \param natom the total number of atoms
!> \par History
!> 01.2017 created [Nico Holmberg]
! **************************************************************************************************
SUBROUTINE mixed_cdft_parse_settings(force_env, mixed_env, mixed_cdft, &
settings, natom)
TYPE(force_env_type), POINTER :: force_env
TYPE(mixed_environment_type), POINTER :: mixed_env
TYPE(mixed_cdft_type), POINTER :: mixed_cdft
TYPE(mixed_cdft_settings_type) :: settings
INTEGER :: natom
INTEGER :: i, iatom, iforce_eval, igroup, &
nforce_eval, nkinds
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: constraint_type
INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: array_sizes
LOGICAL :: is_match
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cdft_control_type), POINTER :: cdft_control
TYPE(cp_1d_i_p_type), ALLOCATABLE, DIMENSION(:, :) :: atoms
TYPE(cp_1d_r_p_type), ALLOCATABLE, DIMENSION(:, :) :: coeff
TYPE(dft_control_type), POINTER :: dft_control
TYPE(force_env_type), POINTER :: force_env_qs
TYPE(pw_env_type), POINTER :: pw_env
TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
TYPE(qs_environment_type), POINTER :: qs_env
NULLIFY (dft_control, qs_env, pw_env, auxbas_pw_pool, force_env_qs, &
cdft_control)
! Allocate storage for temporaries used for checking settings consistency
settings%max_nkinds = 30
nforce_eval = SIZE(force_env%sub_force_env)
ALLOCATE (settings%grid_span(nforce_eval))
ALLOCATE (settings%npts(3, nforce_eval))
ALLOCATE (settings%cutoff(nforce_eval))
ALLOCATE (settings%rel_cutoff(nforce_eval))
ALLOCATE (settings%spherical(nforce_eval))
ALLOCATE (settings%rs_dims(2, nforce_eval))
ALLOCATE (settings%odd(nforce_eval))
ALLOCATE (settings%atoms(natom, nforce_eval))
IF (mixed_cdft%run_type == mixed_cdft_parallel) THEN
ALLOCATE (settings%coeffs(natom, nforce_eval))
settings%coeffs = 0.0_dp
END IF
! Some of the checked settings are only defined for certain types of constraints
! We nonetheless use arrays that are large enough to contain settings for all constraints
! This is not completely optimal...
ALLOCATE (settings%si(6, nforce_eval))
ALLOCATE (settings%sb(8, nforce_eval))
ALLOCATE (settings%sr(5, nforce_eval))
ALLOCATE (settings%cutoffs(settings%max_nkinds, nforce_eval))
ALLOCATE (settings%radii(settings%max_nkinds, nforce_eval))
settings%grid_span = 0
settings%npts = 0
settings%cutoff = 0.0_dp
settings%rel_cutoff = 0.0_dp
settings%spherical = 0
settings%is_spherical = .FALSE.
settings%rs_dims = 0
settings%odd = 0
settings%is_odd = .FALSE.
settings%atoms = 0
settings%si = 0
settings%sr = 0.0_dp
settings%sb = .FALSE.
settings%cutoffs = 0.0_dp
settings%radii = 0.0_dp
! Get information from the sub_force_envs
DO iforce_eval = 1, nforce_eval
IF (.NOT. ASSOCIATED(force_env%sub_force_env(iforce_eval)%force_env)) CYCLE
force_env_qs => force_env%sub_force_env(iforce_eval)%force_env
IF (mixed_env%do_mixed_qmmm_cdft) THEN
qs_env => force_env_qs%qmmm_env%qs_env
ELSE
CALL force_env_get(force_env_qs, qs_env=qs_env)
END IF
CALL get_qs_env(qs_env, pw_env=pw_env, dft_control=dft_control)
IF (.NOT. dft_control%qs_control%cdft) &
CALL cp_abort(__LOCATION__, &
"A mixed CDFT simulation with multiple force_evals was requested, "// &
"but CDFT constraints were not active in the QS section of all force_evals!")
cdft_control => dft_control%qs_control%cdft_control
CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
settings%bo = auxbas_pw_pool%pw_grid%bounds_local
! Only the rank 0 process collects info about pw_grid and CDFT
IF (force_env_qs%para_env%is_source()) THEN
! Grid settings
settings%grid_span(iforce_eval) = auxbas_pw_pool%pw_grid%grid_span
settings%npts(:, iforce_eval) = auxbas_pw_pool%pw_grid%npts
settings%cutoff(iforce_eval) = auxbas_pw_pool%pw_grid%cutoff
settings%rel_cutoff(iforce_eval) = dft_control%qs_control%relative_cutoff
IF (auxbas_pw_pool%pw_grid%spherical) settings%spherical(iforce_eval) = 1
settings%rs_dims(:, iforce_eval) = auxbas_pw_pool%pw_grid%para%group%num_pe_cart
IF (auxbas_pw_pool%pw_grid%grid_span == HALFSPACE) settings%odd(iforce_eval) = 1
! Becke constraint atoms/coeffs
IF (cdft_control%natoms .GT. SIZE(settings%atoms, 1)) &
CALL cp_abort(__LOCATION__, &
"More CDFT constraint atoms than defined in mixed section. "// &
"Use default values for MIXED\MAPPING.")
settings%atoms(1:cdft_control%natoms, iforce_eval) = cdft_control%atoms
IF (mixed_cdft%run_type == mixed_cdft_parallel) &
settings%coeffs(1:cdft_control%natoms, iforce_eval) = cdft_control%group(1)%coeff
! Integer type settings
IF (cdft_control%type == outer_scf_becke_constraint) THEN
settings%si(1, iforce_eval) = cdft_control%becke_control%cutoff_type
settings%si(2, iforce_eval) = cdft_control%becke_control%cavity_shape
END IF
settings%si(3, iforce_eval) = dft_control%multiplicity
settings%si(4, iforce_eval) = SIZE(cdft_control%group)
settings%si(5, iforce_eval) = cdft_control%type
IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
settings%si(6, iforce_eval) = cdft_control%hirshfeld_control%shape_function
settings%si(6, iforce_eval) = cdft_control%hirshfeld_control%gaussian_shape
END IF
! Logicals
IF (cdft_control%type == outer_scf_becke_constraint) THEN
settings%sb(1, iforce_eval) = cdft_control%becke_control%cavity_confine
settings%sb(2, iforce_eval) = cdft_control%becke_control%should_skip
settings%sb(3, iforce_eval) = cdft_control%becke_control%print_cavity
settings%sb(4, iforce_eval) = cdft_control%becke_control%in_memory
settings%sb(5, iforce_eval) = cdft_control%becke_control%adjust
settings%sb(8, iforce_eval) = cdft_control%becke_control%use_bohr
END IF
IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
settings%sb(8, iforce_eval) = cdft_control%hirshfeld_control%use_bohr
END IF
settings%sb(6, iforce_eval) = cdft_control%atomic_charges
settings%sb(7, iforce_eval) = qs_env%has_unit_metric
! Reals
IF (cdft_control%type == outer_scf_becke_constraint) THEN
settings%sr(1, iforce_eval) = cdft_control%becke_control%rcavity
settings%sr(2, iforce_eval) = cdft_control%becke_control%rglobal
settings%sr(3, iforce_eval) = cdft_control%becke_control%eps_cavity
END IF
IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
settings%sr(2, iforce_eval) = cdft_control%hirshfeld_control%radius
END IF
settings%sr(4, iforce_eval) = dft_control%qs_control%eps_rho_rspace
settings%sr(5, iforce_eval) = pw_env%cube_info(pw_env%auxbas_grid)%max_rad_ga
IF (cdft_control%type == outer_scf_becke_constraint) THEN
IF (cdft_control%becke_control%cutoff_type == becke_cutoff_element) THEN
nkinds = SIZE(cdft_control%becke_control%cutoffs_tmp)
IF (nkinds .GT. settings%max_nkinds) &
CALL cp_abort(__LOCATION__, &
"More than "//TRIM(cp_to_string(settings%max_nkinds))// &
" unique elements were defined in BECKE_CONSTRAINT\ELEMENT_CUTOFF. Are you sure"// &
" your input is correct? If yes, please increase max_nkinds and recompile.")
settings%cutoffs(1:nkinds, iforce_eval) = cdft_control%becke_control%cutoffs_tmp(:)
END IF
IF (cdft_control%becke_control%adjust) THEN
CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set)
IF (.NOT. SIZE(atomic_kind_set) == SIZE(cdft_control%becke_control%radii_tmp)) &
CALL cp_abort(__LOCATION__, &
"Length of keyword BECKE_CONSTRAINT\ATOMIC_RADII does not "// &
"match number of atomic kinds in the input coordinate file.")
nkinds = SIZE(cdft_control%becke_control%radii_tmp)
IF (nkinds .GT. settings%max_nkinds) &
CALL cp_abort(__LOCATION__, &
"More than "//TRIM(cp_to_string(settings%max_nkinds))// &
" unique elements were defined in BECKE_CONSTRAINT\ATOMIC_RADII. Are you sure"// &
" your input is correct? If yes, please increase max_nkinds and recompile.")
settings%radii(1:nkinds, iforce_eval) = cdft_control%becke_control%radii_tmp(:)
END IF
END IF
IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
IF (ASSOCIATED(cdft_control%hirshfeld_control%radii)) THEN
CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set)
IF (.NOT. SIZE(atomic_kind_set) == SIZE(cdft_control%hirshfeld_control%radii)) &
CALL cp_abort(__LOCATION__, &
"Length of keyword HIRSHFELD_CONSTRAINT&RADII does not "// &
"match number of atomic kinds in the input coordinate file.")
nkinds = SIZE(cdft_control%hirshfeld_control%radii)
IF (nkinds .GT. settings%max_nkinds) &
CALL cp_abort(__LOCATION__, &
"More than "//TRIM(cp_to_string(settings%max_nkinds))// &
" unique elements were defined in HIRSHFELD_CONSTRAINT&RADII. Are you sure"// &
" your input is correct? If yes, please increase max_nkinds and recompile.")
settings%radii(1:nkinds, iforce_eval) = cdft_control%hirshfeld_control%radii(:)
END IF
END IF
END IF
END DO
! Make sure the grids are consistent
CALL force_env%para_env%sum(settings%grid_span)
CALL force_env%para_env%sum(settings%npts)
CALL force_env%para_env%sum(settings%cutoff)
CALL force_env%para_env%sum(settings%rel_cutoff)
CALL force_env%para_env%sum(settings%spherical)
CALL force_env%para_env%sum(settings%rs_dims)
CALL force_env%para_env%sum(settings%odd)
is_match = .TRUE.
DO iforce_eval = 2, nforce_eval
is_match = is_match .AND. (settings%grid_span(1) == settings%grid_span(iforce_eval))
is_match = is_match .AND. (settings%npts(1, 1) == settings%npts(1, iforce_eval))
is_match = is_match .AND. (settings%cutoff(1) == settings%cutoff(iforce_eval))
is_match = is_match .AND. (settings%rel_cutoff(1) == settings%rel_cutoff(iforce_eval))
is_match = is_match .AND. (settings%spherical(1) == settings%spherical(iforce_eval))
is_match = is_match .AND. (settings%rs_dims(1, 1) == settings%rs_dims(1, iforce_eval))
is_match = is_match .AND. (settings%rs_dims(2, 1) == settings%rs_dims(2, iforce_eval))
is_match = is_match .AND. (settings%odd(1) == settings%odd(iforce_eval))
END DO
IF (.NOT. is_match) &
CALL cp_abort(__LOCATION__, &
"Mismatch detected in the &MGRID settings of the CDFT force_evals.")
IF (settings%spherical(1) == 1) settings%is_spherical = .TRUE.
IF (settings%odd(1) == 1) settings%is_odd = .TRUE.
! Make sure CDFT settings are consistent
CALL force_env%para_env%sum(settings%atoms)
IF (mixed_cdft%run_type == mixed_cdft_parallel) &
CALL force_env%para_env%sum(settings%coeffs)
settings%ncdft = 0
DO i = 1, SIZE(settings%atoms, 1)
DO iforce_eval = 2, nforce_eval
IF (mixed_cdft%run_type == mixed_cdft_parallel) THEN
IF (settings%atoms(i, 1) /= settings%atoms(i, iforce_eval)) is_match = .FALSE.
IF (settings%coeffs(i, 1) /= settings%coeffs(i, iforce_eval)) is_match = .FALSE.
END IF
END DO
IF (settings%atoms(i, 1) /= 0) settings%ncdft = settings%ncdft + 1
END DO
IF (.NOT. is_match .AND. mixed_cdft%run_type == mixed_cdft_parallel) &
CALL cp_abort(__LOCATION__, &
"Mismatch detected in the &CDFT section of the CDFT force_evals. "// &
"Parallel mode mixed CDFT requires identical constraint definitions in both CDFT states. "// &
"Switch to serial mode or disable keyword PARALLEL_BUILD if you "// &
"want to use nonidentical constraint definitions.")
CALL force_env%para_env%sum(settings%si)
CALL force_env%para_env%sum(settings%sr)
DO i = 1, SIZE(settings%sb, 1)
CALL force_env%para_env%sum(settings%sb(i, 1))
DO iforce_eval = 2, nforce_eval
CALL force_env%para_env%sum(settings%sb(i, iforce_eval))
IF (settings%sb(i, 1) .NEQV. settings%sb(i, iforce_eval)) is_match = .FALSE.
END DO
END DO
DO i = 1, SIZE(settings%si, 1)
DO iforce_eval = 2, nforce_eval
IF (settings%si(i, 1) /= settings%si(i, iforce_eval)) is_match = .FALSE.
END DO
END DO
DO i = 1, SIZE(settings%sr, 1)
DO iforce_eval = 2, nforce_eval
IF (settings%sr(i, 1) /= settings%sr(i, iforce_eval)) is_match = .FALSE.
END DO
END DO
IF (.NOT. is_match) &
CALL cp_abort(__LOCATION__, &
"Mismatch detected in the &CDFT settings of the CDFT force_evals.")
! Some CDFT features are currently disabled for mixed calculations: check that these features were not requested
IF (mixed_cdft%dlb .AND. .NOT. settings%sb(1, 1)) &
CALL cp_abort(__LOCATION__, &
"Parallel mode mixed CDFT load balancing requires Gaussian cavity confinement.")
! Check for identical constraints in case of run type serial/parallel_nobuild
IF (mixed_cdft%run_type /= mixed_cdft_parallel) THEN
! Get array sizes
ALLOCATE (array_sizes(nforce_eval, settings%si(4, 1), 2))
array_sizes(:, :, :) = 0
DO iforce_eval = 1, nforce_eval
IF (.NOT. ASSOCIATED(force_env%sub_force_env(iforce_eval)%force_env)) CYCLE
force_env_qs => force_env%sub_force_env(iforce_eval)%force_env
IF (mixed_env%do_mixed_qmmm_cdft) THEN
qs_env => force_env_qs%qmmm_env%qs_env
ELSE
CALL force_env_get(force_env_qs, qs_env=qs_env)
END IF
CALL get_qs_env(qs_env, dft_control=dft_control)
cdft_control => dft_control%qs_control%cdft_control
IF (force_env_qs%para_env%is_source()) THEN
DO igroup = 1, SIZE(cdft_control%group)
array_sizes(iforce_eval, igroup, 1) = SIZE(cdft_control%group(igroup)%atoms)
array_sizes(iforce_eval, igroup, 2) = SIZE(cdft_control%group(igroup)%coeff)
END DO
END IF
END DO
! Sum up array sizes and check consistency
CALL force_env%para_env%sum(array_sizes)
IF (ANY(array_sizes(:, :, 1) /= array_sizes(1, 1, 1)) .OR. &
ANY(array_sizes(:, :, 2) /= array_sizes(1, 1, 2))) &
mixed_cdft%identical_constraints = .FALSE.
! Check constraint definitions
IF (mixed_cdft%identical_constraints) THEN
! Prepare temporary storage
ALLOCATE (atoms(nforce_eval, settings%si(4, 1)))
ALLOCATE (coeff(nforce_eval, settings%si(4, 1)))
ALLOCATE (constraint_type(nforce_eval, settings%si(4, 1)))
constraint_type(:, :) = 0
DO iforce_eval = 1, nforce_eval
DO i = 1, settings%si(4, 1)
NULLIFY (atoms(iforce_eval, i)%array)
NULLIFY (coeff(iforce_eval, i)%array)
ALLOCATE (atoms(iforce_eval, i)%array(array_sizes(iforce_eval, i, 1)))
ALLOCATE (coeff(iforce_eval, i)%array(array_sizes(iforce_eval, i, 1)))
atoms(iforce_eval, i)%array(:) = 0
coeff(iforce_eval, i)%array(:) = 0
END DO
! Get constraint definitions
IF (.NOT. ASSOCIATED(force_env%sub_force_env(iforce_eval)%force_env)) CYCLE
force_env_qs => force_env%sub_force_env(iforce_eval)%force_env
IF (mixed_env%do_mixed_qmmm_cdft) THEN
qs_env => force_env_qs%qmmm_env%qs_env
ELSE
CALL force_env_get(force_env_qs, qs_env=qs_env)
END IF
CALL get_qs_env(qs_env, dft_control=dft_control)
cdft_control => dft_control%qs_control%cdft_control
IF (force_env_qs%para_env%is_source()) THEN
DO i = 1, settings%si(4, 1)
atoms(iforce_eval, i)%array(:) = cdft_control%group(i)%atoms
coeff(iforce_eval, i)%array(:) = cdft_control%group(i)%coeff
constraint_type(iforce_eval, i) = cdft_control%group(i)%constraint_type
END DO
END IF
END DO
! Sum up constraint definitions and check consistency
DO i = 1, settings%si(4, 1)
DO iforce_eval = 1, nforce_eval
CALL force_env%para_env%sum(atoms(iforce_eval, i)%array)
CALL force_env%para_env%sum(coeff(iforce_eval, i)%array)
CALL force_env%para_env%sum(constraint_type(iforce_eval, i))
END DO
DO iforce_eval = 2, nforce_eval
DO iatom = 1, SIZE(atoms(1, i)%array)
IF (atoms(1, i)%array(iatom) /= atoms(iforce_eval, i)%array(iatom)) &
mixed_cdft%identical_constraints = .FALSE.
IF (coeff(1, i)%array(iatom) /= coeff(iforce_eval, i)%array(iatom)) &
mixed_cdft%identical_constraints = .FALSE.
IF (.NOT. mixed_cdft%identical_constraints) EXIT
END DO
IF (constraint_type(1, i) /= constraint_type(iforce_eval, i)) &
mixed_cdft%identical_constraints = .FALSE.
IF (.NOT. mixed_cdft%identical_constraints) EXIT
END DO
IF (.NOT. mixed_cdft%identical_constraints) EXIT
END DO
! Deallocate temporary storage
DO iforce_eval = 1, nforce_eval
DO i = 1, settings%si(4, 1)
DEALLOCATE (atoms(iforce_eval, i)%array)
DEALLOCATE (coeff(iforce_eval, i)%array)
END DO
END DO
DEALLOCATE (atoms)
DEALLOCATE (coeff)
DEALLOCATE (constraint_type)
END IF
DEALLOCATE (array_sizes)
END IF
! Deallocate some arrays that are no longer needed
IF (mixed_cdft%identical_constraints .AND. mixed_cdft%run_type /= mixed_cdft_parallel_nobuild) THEN
DO iforce_eval = 1, nforce_eval
IF (.NOT. ASSOCIATED(force_env%sub_force_env(iforce_eval)%force_env)) CYCLE
force_env_qs => force_env%sub_force_env(iforce_eval)%force_env
IF (mixed_env%do_mixed_qmmm_cdft) THEN
qs_env => force_env_qs%qmmm_env%qs_env
ELSE
CALL force_env_get(force_env_qs, qs_env=qs_env)
END IF
CALL get_qs_env(qs_env, dft_control=dft_control)
cdft_control => dft_control%qs_control%cdft_control
IF (mixed_cdft%run_type == mixed_cdft_parallel) THEN
IF (.NOT. dft_control%qs_control%gapw) THEN
DO i = 1, SIZE(cdft_control%group)
DEALLOCATE (cdft_control%group(i)%coeff)
DEALLOCATE (cdft_control%group(i)%atoms)
END DO
IF (.NOT. cdft_control%atomic_charges) DEALLOCATE (cdft_control%atoms)
END IF
ELSE IF (mixed_cdft%run_type == mixed_cdft_serial) THEN
IF (iforce_eval == 1) CYCLE
DO igroup = 1, SIZE(cdft_control%group)
IF (.NOT. dft_control%qs_control%gapw) THEN
DEALLOCATE (cdft_control%group(igroup)%coeff)
DEALLOCATE (cdft_control%group(igroup)%atoms)
END IF
END DO
IF (cdft_control%type == outer_scf_becke_constraint) THEN
IF (.NOT. cdft_control%atomic_charges) DEALLOCATE (cdft_control%atoms)
IF (cdft_control%becke_control%cavity_confine) &
CALL release_hirshfeld_type(cdft_control%becke_control%cavity_env)
IF (cdft_control%becke_control%cutoff_type == becke_cutoff_element) &
DEALLOCATE (cdft_control%becke_control%cutoffs_tmp)
IF (cdft_control%becke_control%adjust) &
DEALLOCATE (cdft_control%becke_control%radii_tmp)
END IF
END IF
END DO
END IF
END SUBROUTINE mixed_cdft_parse_settings
! **************************************************************************************************
!> \brief Transfer settings to mixed_cdft
!> \param force_env the force_env that holds the CDFT states
!> \param mixed_cdft the control section for mixed CDFT calculations
!> \param settings container for settings related to the mixed CDFT calculation
!> \par History
!> 01.2017 created [Nico Holmberg]
! **************************************************************************************************
SUBROUTINE mixed_cdft_transfer_settings(force_env, mixed_cdft, settings)
TYPE(force_env_type), POINTER :: force_env
TYPE(mixed_cdft_type), POINTER :: mixed_cdft
TYPE(mixed_cdft_settings_type) :: settings
INTEGER :: i, nkinds
LOGICAL :: is_match
TYPE(cdft_control_type), POINTER :: cdft_control
NULLIFY (cdft_control)
is_match = .TRUE.
! Transfer global settings
mixed_cdft%multiplicity = settings%si(3, 1)
mixed_cdft%has_unit_metric = settings%sb(7, 1)
mixed_cdft%eps_rho_rspace = settings%sr(4, 1)
mixed_cdft%nconstraint = settings%si(4, 1)
settings%radius = settings%sr(5, 1)
! Transfer settings only needed if the constraint should be built in parallel
IF (mixed_cdft%run_type == mixed_cdft_parallel) THEN
IF (settings%sb(6, 1)) &
CALL cp_abort(__LOCATION__, &
"Calculation of atomic Becke charges not supported with parallel mode mixed CDFT")
IF (mixed_cdft%nconstraint /= 1) &
CALL cp_abort(__LOCATION__, &
"Parallel mode mixed CDFT does not yet support multiple constraints.")
IF (settings%si(5, 1) /= outer_scf_becke_constraint) &
CALL cp_abort(__LOCATION__, &
"Parallel mode mixed CDFT does not support Hirshfeld constraints.")
ALLOCATE (mixed_cdft%cdft_control)
CALL cdft_control_create(mixed_cdft%cdft_control)
cdft_control => mixed_cdft%cdft_control
ALLOCATE (cdft_control%atoms(settings%ncdft))
cdft_control%atoms = settings%atoms(1:settings%ncdft, 1)
ALLOCATE (cdft_control%group(1))
ALLOCATE (cdft_control%group(1)%atoms(settings%ncdft))
ALLOCATE (cdft_control%group(1)%coeff(settings%ncdft))
NULLIFY (cdft_control%group(1)%weight)
NULLIFY (cdft_control%group(1)%gradients)
NULLIFY (cdft_control%group(1)%integrated)
cdft_control%group(1)%atoms = cdft_control%atoms
cdft_control%group(1)%coeff = settings%coeffs(1:settings%ncdft, 1)
cdft_control%natoms = settings%ncdft
cdft_control%atomic_charges = settings%sb(6, 1)
cdft_control%becke_control%cutoff_type = settings%si(1, 1)
cdft_control%becke_control%cavity_confine = settings%sb(1, 1)
cdft_control%becke_control%should_skip = settings%sb(2, 1)
cdft_control%becke_control%print_cavity = settings%sb(3, 1)
cdft_control%becke_control%in_memory = settings%sb(4, 1)
cdft_control%becke_control%adjust = settings%sb(5, 1)
cdft_control%becke_control%cavity_shape = settings%si(2, 1)
cdft_control%becke_control%use_bohr = settings%sb(8, 1)
cdft_control%becke_control%rcavity = settings%sr(1, 1)
cdft_control%becke_control%rglobal = settings%sr(2, 1)
cdft_control%becke_control%eps_cavity = settings%sr(3, 1)
nkinds = 0
IF (cdft_control%becke_control%cutoff_type == becke_cutoff_element) THEN
CALL force_env%para_env%sum(settings%cutoffs)
DO i = 1, SIZE(settings%cutoffs, 1)
IF (settings%cutoffs(i, 1) /= settings%cutoffs(i, 2)) is_match = .FALSE.
IF (settings%cutoffs(i, 1) /= 0.0_dp) nkinds = nkinds + 1
END DO
IF (.NOT. is_match) &
CALL cp_abort(__LOCATION__, &
"Mismatch detected in the &BECKE_CONSTRAINT "// &
"&ELEMENT_CUTOFF settings of the two force_evals.")
ALLOCATE (cdft_control%becke_control%cutoffs_tmp(nkinds))
cdft_control%becke_control%cutoffs_tmp = settings%cutoffs(1:nkinds, 1)
END IF
nkinds = 0
IF (cdft_control%becke_control%adjust) THEN
CALL force_env%para_env%sum(settings%radii)
DO i = 1, SIZE(settings%radii, 1)
IF (settings%radii(i, 1) /= settings%radii(i, 2)) is_match = .FALSE.
IF (settings%radii(i, 1) /= 0.0_dp) nkinds = nkinds + 1
END DO
IF (.NOT. is_match) &
CALL cp_abort(__LOCATION__, &
"Mismatch detected in the &BECKE_CONSTRAINT "// &
"&ATOMIC_RADII settings of the two force_evals.")
ALLOCATE (cdft_control%becke_control%radii(nkinds))
cdft_control%becke_control%radii = settings%radii(1:nkinds, 1)
END IF
END IF
END SUBROUTINE mixed_cdft_transfer_settings
! **************************************************************************************************
!> \brief Initialize all the structures needed for a mixed CDFT calculation
!> \param force_env the force_env that holds the CDFT mixed_env
!> \param force_env_qs the force_env that holds the qs_env, which is CDFT state specific
!> \param mixed_env the mixed_env that holds the CDFT states
!> \param mixed_cdft the control section for mixed CDFT calculations
!> \param settings container for settings related to the mixed CDFT calculation
!> \par History
!> 01.2017 created [Nico Holmberg]
! **************************************************************************************************
SUBROUTINE mixed_cdft_init_structures(force_env, force_env_qs, mixed_env, mixed_cdft, settings)
TYPE(force_env_type), POINTER :: force_env, force_env_qs
TYPE(mixed_environment_type), POINTER :: mixed_env
TYPE(mixed_cdft_type), POINTER :: mixed_cdft
TYPE(mixed_cdft_settings_type) :: settings
CHARACTER(len=default_path_length) :: c_val, input_file_path, output_file_path
INTEGER :: i, imap, iounit, j, lp, n_force_eval, &
ncpu, nforce_eval, ntargets, offset, &
unit_nr
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: bounds
INTEGER, DIMENSION(2, 3) :: bo, bo_mixed
INTEGER, DIMENSION(3) :: higher_grid_layout
INTEGER, DIMENSION(:), POINTER :: i_force_eval, mixed_rs_dims, recvbuffer, &
recvbuffer2, sendbuffer
LOGICAL :: is_match
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell_mix
TYPE(cp_logger_type), POINTER :: logger
TYPE(cp_subsys_type), POINTER :: subsys_mix
TYPE(global_environment_type), POINTER :: globenv
TYPE(mp_request_type), DIMENSION(3) :: req
TYPE(pw_env_type), POINTER :: pw_env
TYPE(pw_grid_type), POINTER :: pw_grid
TYPE(pw_pool_p_type), DIMENSION(:), POINTER :: pw_pools
TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
POINTER :: rs_descs
TYPE(realspace_grid_input_type) :: input_settings
TYPE(realspace_grid_type), DIMENSION(:), POINTER :: rs_grids
TYPE(section_vals_type), POINTER :: force_env_section, force_env_sections, kind_section, &
print_section, root_section, rs_grid_section, subsys_section
NULLIFY (cell_mix, subsys_mix, force_env_section, subsys_section, &
print_section, root_section, kind_section, force_env_sections, &
rs_grid_section, auxbas_pw_pool, pw_env, pw_pools, pw_grid, &
sendbuffer, qs_env, mixed_rs_dims, i_force_eval, recvbuffer, &
recvbuffer2, globenv, atomic_kind_set, qs_kind_set, rs_descs, &
rs_grids)
logger => cp_get_default_logger()
CALL force_env_get(force_env=force_env, force_env_section=force_env_section)
print_section => section_vals_get_subs_vals(force_env_section, "MIXED%MIXED_CDFT%PRINT%PROGRAM_RUN_INFO")
iounit = cp_print_key_unit_nr(logger, print_section, '', extension='.mixedLog')
is_match = .TRUE.
nforce_eval = SIZE(force_env%sub_force_env)
ncpu = force_env%para_env%num_pe
! Get infos about the mixed subsys
IF (.NOT. mixed_env%do_mixed_qmmm_cdft) THEN
CALL force_env_get(force_env=force_env, &
subsys=subsys_mix)
ELSE
CALL get_qs_env(force_env_qs%qmmm_env%qs_env, &
cp_subsys=subsys_mix)
END IF
! Init structures only needed when the CDFT states are treated in parallel
IF (mixed_cdft%run_type == mixed_cdft_parallel) THEN
! Start building the mixed auxbas_pw_pool
CALL pw_env_create(mixed_cdft%pw_env)
! Decide what kind of layout to use and setup the grid
! Processor mappings currently supported:
! (2np,1) --> (np,1)
! (nx,2ny) --> (nx,ny)
! (nx,ny) --> (nx*ny/2,1) (required when xc_smooth is in use and with intermediate proc counts)
!
! For cases 2 and 3, dlb redistributes YZ slices from overloaded processors to underloaded processors
! For case 1, XZ slices are redistributed
! TODO: Unify mappings. Now we essentially have separate code for cases 1-2 and 3.
! This leads to very messy code especially with dlb turned on...
! In terms of memory usage, it would be beneficial to replace case 1 with 3
! and implement a similar arbitrary mapping to replace case 2
mixed_cdft%is_pencil = .FALSE. ! Flag to control the first two mappings
mixed_cdft%is_special = .FALSE. ! Flag to control the last mapping
! With xc smoothing, the grid is always (ncpu/2,1) distributed
! and correct behavior cannot be guaranteed for ncpu/2 > nx, so we abort...
IF (ncpu/2 .GT. settings%npts(1, 1)) &
CPABORT("ncpu/2 => nx: decrease ncpu or disable xc_smoothing")
!
ALLOCATE (mixed_rs_dims(2))
IF (settings%rs_dims(2, 1) /= 1) mixed_cdft%is_pencil = .TRUE.
IF (.NOT. mixed_cdft%is_pencil .AND. ncpu .GT. settings%npts(1, 1)) mixed_cdft%is_special = .TRUE.
IF (mixed_cdft%is_special) THEN
mixed_rs_dims = (/-1, -1/)
ELSE IF (mixed_cdft%is_pencil) THEN
mixed_rs_dims = (/settings%rs_dims(1, 1), 2*settings%rs_dims(2, 1)/)
ELSE
mixed_rs_dims = (/2*settings%rs_dims(1, 1), 1/)
END IF
IF (.NOT. mixed_env%do_mixed_qmmm_cdft) THEN
CALL force_env_get(force_env=force_env, &
cell=cell_mix)
ELSE
CALL get_qs_env(force_env_qs%qmmm_env%qs_env, &
cell=cell_mix)
END IF
CALL pw_grid_create(pw_grid, force_env%para_env, cell_mix%hmat, grid_span=settings%grid_span(1), &
npts=settings%npts(:, 1), cutoff=settings%cutoff(1), &
spherical=settings%is_spherical, odd=settings%is_odd, &
fft_usage=.TRUE., ncommensurate=0, icommensurate=1, &
blocked=do_pw_grid_blocked_false, rs_dims=mixed_rs_dims, &
iounit=iounit)
! Check if the layout was successfully created
IF (mixed_cdft%is_special) THEN
IF (.NOT. pw_grid%para%group%num_pe_cart(2) /= 1) is_match = .FALSE.
ELSE IF (mixed_cdft%is_pencil) THEN
IF (.NOT. pw_grid%para%group%num_pe_cart(1) == mixed_rs_dims(1)) is_match = .FALSE.
ELSE
IF (.NOT. pw_grid%para%group%num_pe_cart(2) == 1) is_match = .FALSE.
END IF
IF (.NOT. is_match) &
CALL cp_abort(__LOCATION__, &
"Unable to create a suitable grid distribution "// &
"for mixed CDFT calculations. Try decreasing the total number "// &
"of processors or disabling xc_smoothing.")
DEALLOCATE (mixed_rs_dims)
! Create the pool
bo_mixed = pw_grid%bounds_local
ALLOCATE (pw_pools(1))
NULLIFY (pw_pools(1)%pool)
CALL pw_pool_create(pw_pools(1)%pool, pw_grid=pw_grid)
! Initialize Gaussian cavity confinement
IF (mixed_cdft%cdft_control%becke_control%cavity_confine) THEN
CALL create_hirshfeld_type(mixed_cdft%cdft_control%becke_control%cavity_env)
CALL set_hirshfeld_info(mixed_cdft%cdft_control%becke_control%cavity_env, &
shape_function_type=shape_function_gaussian, iterative=.FALSE., &
radius_type=mixed_cdft%cdft_control%becke_control%cavity_shape, &
use_bohr=mixed_cdft%cdft_control%becke_control%use_bohr)
END IF
! Gaussian confinement/wavefunction overlap method needs qs_kind_set
! Gaussian cavity confinement also needs the auxbas_rs_grid
IF (mixed_cdft%cdft_control%becke_control%cavity_confine .OR. &
mixed_cdft%wfn_overlap_method) THEN
print_section => section_vals_get_subs_vals(force_env_section, &
"PRINT%GRID_INFORMATION")
ALLOCATE (mixed_cdft%pw_env%gridlevel_info)
CALL init_gaussian_gridlevel(mixed_cdft%pw_env%gridlevel_info, &
ngrid_levels=1, cutoff=settings%cutoff, &
rel_cutoff=settings%rel_cutoff(1), &
print_section=print_section)
ALLOCATE (rs_descs(1))
ALLOCATE (rs_grids(1))
ALLOCATE (mixed_cdft%pw_env%cube_info(1))
higher_grid_layout = (/-1, -1, -1/)
CALL init_d3_poly_module()
CALL init_cube_info(mixed_cdft%pw_env%cube_info(1), &
pw_grid%dr(:), pw_grid%dh(:, :), &
pw_grid%dh_inv(:, :), &
pw_grid%orthorhombic, settings%radius)
NULLIFY (root_section, force_env_section, force_env_sections, rs_grid_section)
CALL force_env_get(force_env, root_section=root_section)
force_env_sections => section_vals_get_subs_vals(root_section, "FORCE_EVAL")
CALL multiple_fe_list(force_env_sections, root_section, i_force_eval, n_force_eval)
CALL section_vals_duplicate(force_env_sections, force_env_section, &
i_force_eval(2), i_force_eval(2))
rs_grid_section => section_vals_get_subs_vals(force_env_section, "DFT%MGRID%RS_GRID")
CALL init_input_type(input_settings, &
nsmax=2*MAX(1, return_cube_max_iradius(mixed_cdft%pw_env%cube_info(1))) + 1, &
rs_grid_section=rs_grid_section, ilevel=1, &
higher_grid_layout=higher_grid_layout)
NULLIFY (rs_descs(1)%rs_desc)
CALL rs_grid_create_descriptor(rs_descs(1)%rs_desc, pw_grid, input_settings)
IF (rs_descs(1)%rs_desc%distributed) higher_grid_layout = rs_descs(1)%rs_desc%group_dim
CALL rs_grid_create(rs_grids(1), rs_descs(1)%rs_desc)
CALL rs_grid_print(rs_grids(1), iounit)
mixed_cdft%pw_env%rs_descs => rs_descs
mixed_cdft%pw_env%rs_grids => rs_grids
! qs_kind_set
subsys_section => section_vals_get_subs_vals(force_env_sections, "SUBSYS", &
i_rep_section=i_force_eval(1))
kind_section => section_vals_get_subs_vals(subsys_section, "KIND")
NULLIFY (qs_kind_set)
CALL cp_subsys_get(subsys_mix, atomic_kind_set=atomic_kind_set)
CALL create_qs_kind_set(qs_kind_set, atomic_kind_set, kind_section, &
force_env%para_env, force_env_section, silent=.FALSE.)
mixed_cdft%qs_kind_set => qs_kind_set
DEALLOCATE (i_force_eval)
CALL section_vals_release(force_env_section)
END IF
CALL force_env_get(force_env=force_env, &
force_env_section=force_env_section)
CALL pw_grid_release(pw_grid)
mixed_cdft%pw_env%auxbas_grid = 1
NULLIFY (mixed_cdft%pw_env%pw_pools)
mixed_cdft%pw_env%pw_pools => pw_pools
bo = settings%bo
! Determine which processors need to exchange data when redistributing the weight/gradient
IF (.NOT. mixed_cdft%is_special) THEN
ALLOCATE (mixed_cdft%dest_list(2))
ALLOCATE (mixed_cdft%source_list(2))
imap = force_env%para_env%mepos/2
mixed_cdft%dest_list = (/imap, imap + force_env%para_env%num_pe/2/)
imap = MOD(force_env%para_env%mepos, force_env%para_env%num_pe/2) + &
MODULO(force_env%para_env%mepos, force_env%para_env%num_pe/2)
mixed_cdft%source_list = (/imap, imap + 1/)
! Determine bounds of the data that is replicated
ALLOCATE (mixed_cdft%recv_bo(4))
ALLOCATE (sendbuffer(2), recvbuffer(2), recvbuffer2(2))
IF (mixed_cdft%is_pencil) THEN
sendbuffer = (/bo_mixed(1, 2), bo_mixed(2, 2)/)
ELSE
sendbuffer = (/bo_mixed(1, 1), bo_mixed(2, 1)/)
END IF
! Communicate bounds in steps
CALL force_env%para_env%isend(msgin=sendbuffer, dest=mixed_cdft%dest_list(1), &
request=req(1))
CALL force_env%para_env%irecv(msgout=recvbuffer, source=mixed_cdft%source_list(1), &
request=req(2))
CALL force_env%para_env%irecv(msgout=recvbuffer2, source=mixed_cdft%source_list(2), &
request=req(3))
CALL req(1)%wait()
CALL force_env%para_env%isend(msgin=sendbuffer, dest=mixed_cdft%dest_list(2), &
request=req(1))
CALL mp_waitall(req)
mixed_cdft%recv_bo(1:2) = recvbuffer
mixed_cdft%recv_bo(3:4) = recvbuffer2
DEALLOCATE (sendbuffer, recvbuffer, recvbuffer2)
ELSE
IF (mixed_env%do_mixed_qmmm_cdft) THEN
qs_env => force_env_qs%qmmm_env%qs_env
ELSE
CALL force_env_get(force_env_qs, qs_env=qs_env)
END IF
CALL get_qs_env(qs_env, pw_env=pw_env)
CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
! work out the pw grid points each proc holds in the two (identical) parallel proc groups
! note we only care about the x dir since we assume the y dir is not subdivided
ALLOCATE (bounds(0:auxbas_pw_pool%pw_grid%para%group%num_pe - 1, 1:2))
DO i = 0, auxbas_pw_pool%pw_grid%para%group%num_pe - 1
bounds(i, 1:2) = auxbas_pw_pool%pw_grid%para%bo(1:2, 1, i, 1)
bounds(i, 1:2) = bounds(i, 1:2) - auxbas_pw_pool%pw_grid%npts(1)/2 - 1
END DO
! work out which procs to send my grid points
! first get the number of target procs per group
ntargets = 0
offset = -1
DO i = 0, auxbas_pw_pool%pw_grid%para%group%num_pe - 1
IF ((bounds(i, 1) .GE. bo_mixed(1, 1) .AND. bounds(i, 1) .LE. bo_mixed(2, 1)) .OR. &
(bounds(i, 2) .GE. bo_mixed(1, 1) .AND. bounds(i, 2) .LE. bo_mixed(2, 1))) THEN
ntargets = ntargets + 1
IF (offset == -1) offset = i
ELSE IF (bounds(i, 2) .GT. bo_mixed(2, 1)) THEN
EXIT
ELSE
CYCLE
END IF
END DO
ALLOCATE (mixed_cdft%dest_list(ntargets))
ALLOCATE (mixed_cdft%dest_list_bo(2, ntargets))
! now determine the actual grid points to send
j = 1
DO i = offset, offset + ntargets - 1
mixed_cdft%dest_list(j) = i
mixed_cdft%dest_list_bo(:, j) = (/bo_mixed(1, 1) + (bounds(i, 1) - bo_mixed(1, 1)), &
bo_mixed(2, 1) + (bounds(i, 2) - bo_mixed(2, 1))/)
j = j + 1
END DO
ALLOCATE (mixed_cdft%dest_list_save(ntargets), mixed_cdft%dest_bo_save(2, ntargets))
! We need to store backups of these arrays since they might get reallocated during dlb
mixed_cdft%dest_list_save = mixed_cdft%dest_list
mixed_cdft%dest_bo_save = mixed_cdft%dest_list_bo
! finally determine which procs will send me grid points
! now we need info about y dir also
DEALLOCATE (bounds)
ALLOCATE (bounds(0:pw_pools(1)%pool%pw_grid%para%group%num_pe - 1, 1:4))
DO i = 0, pw_pools(1)%pool%pw_grid%para%group%num_pe - 1
bounds(i, 1:2) = pw_pools(1)%pool%pw_grid%para%bo(1:2, 1, i, 1)
bounds(i, 3:4) = pw_pools(1)%pool%pw_grid%para%bo(1:2, 2, i, 1)
bounds(i, 1:2) = bounds(i, 1:2) - pw_pools(1)%pool%pw_grid%npts(1)/2 - 1
bounds(i, 3:4) = bounds(i, 3:4) - pw_pools(1)%pool%pw_grid%npts(2)/2 - 1
END DO
ntargets = 0
offset = -1
DO i = 0, pw_pools(1)%pool%pw_grid%para%group%num_pe - 1
IF ((bo(1, 1) .GE. bounds(i, 1) .AND. bo(1, 1) .LE. bounds(i, 2)) .OR. &
(bo(2, 1) .GE. bounds(i, 1) .AND. bo(2, 1) .LE. bounds(i, 2))) THEN
ntargets = ntargets + 1
IF (offset == -1) offset = i
ELSE IF (bo(2, 1) .LT. bounds(i, 1)) THEN
EXIT
ELSE
CYCLE
END IF
END DO
ALLOCATE (mixed_cdft%source_list(ntargets))
ALLOCATE (mixed_cdft%source_list_bo(4, ntargets))
j = 1
DO i = offset, offset + ntargets - 1
mixed_cdft%source_list(j) = i
IF (bo(1, 1) .GE. bounds(i, 1) .AND. bo(2, 1) .LE. bounds(i, 2)) THEN
mixed_cdft%source_list_bo(:, j) = (/bo(1, 1), bo(2, 1), &
bounds(i, 3), bounds(i, 4)/)
ELSE IF (bo(1, 1) .GE. bounds(i, 1) .AND. bo(1, 1) .LE. bounds(i, 2)) THEN
mixed_cdft%source_list_bo(:, j) = (/bo(1, 1), bounds(i, 2), &
bounds(i, 3), bounds(i, 4)/)
ELSE
mixed_cdft%source_list_bo(:, j) = (/bounds(i, 1), bo(2, 1), &
bounds(i, 3), bounds(i, 4)/)
END IF
j = j + 1
END DO
ALLOCATE (mixed_cdft%source_list_save(ntargets), mixed_cdft%source_bo_save(4, ntargets))
! We need to store backups of these arrays since they might get reallocated during dlb
mixed_cdft%source_list_save = mixed_cdft%source_list
mixed_cdft%source_bo_save = mixed_cdft%source_list_bo
DEALLOCATE (bounds)
END IF
ELSE
! Create loggers to redirect the output of all CDFT states to different files
! even when the states are treated in serial (the initial print of QS data [basis set etc] for
! all states unfortunately goes to the first log file)
CALL force_env_get(force_env, root_section=root_section)
ALLOCATE (mixed_cdft%sub_logger(nforce_eval - 1))
DO i = 1, nforce_eval - 1
IF (force_env%para_env%is_source()) THEN
CALL section_vals_val_get(root_section, "GLOBAL%PROJECT_NAME", &
c_val=input_file_path)
lp = LEN_TRIM(input_file_path)
input_file_path(lp + 1:LEN(input_file_path)) = "-r-"//ADJUSTL(cp_to_string(i + 1))
lp = LEN_TRIM(input_file_path)
output_file_path = input_file_path(1:lp)//".out"
CALL open_file(file_name=output_file_path, file_status="UNKNOWN", &
file_action="WRITE", file_position="APPEND", &
unit_number=unit_nr)
ELSE
unit_nr = -1
END IF
CALL cp_logger_create(mixed_cdft%sub_logger(i)%p, &
para_env=force_env%para_env, &
default_global_unit_nr=unit_nr, &
close_global_unit_on_dealloc=.FALSE.)
! Try to use better names for the local log if it is not too late
CALL section_vals_val_get(root_section, "GLOBAL%OUTPUT_FILE_NAME", &
c_val=c_val)
IF (c_val /= "") THEN
CALL cp_logger_set(mixed_cdft%sub_logger(i)%p, &
local_filename=TRIM(c_val)//"_localLog")
END IF
CALL section_vals_val_get(root_section, "GLOBAL%PROJECT", c_val=c_val)
IF (c_val /= "") THEN
CALL cp_logger_set(mixed_cdft%sub_logger(i)%p, &
local_filename=TRIM(c_val)//"_localLog")
END IF
mixed_cdft%sub_logger(i)%p%iter_info%project_name = c_val
CALL section_vals_val_get(root_section, "GLOBAL%PRINT_LEVEL", &
i_val=mixed_cdft%sub_logger(i)%p%iter_info%print_level)
END DO
IF (mixed_cdft%wfn_overlap_method) THEN
! qs_kind_set
NULLIFY (root_section, force_env_section, force_env_sections, rs_grid_section)
CALL force_env_get(force_env, root_section=root_section)
force_env_sections => section_vals_get_subs_vals(root_section, "FORCE_EVAL")
CALL multiple_fe_list(force_env_sections, root_section, i_force_eval, n_force_eval)
CALL section_vals_duplicate(force_env_sections, force_env_section, &
i_force_eval(2), i_force_eval(2))
subsys_section => section_vals_get_subs_vals(force_env_sections, "SUBSYS", &
i_rep_section=i_force_eval(1))
kind_section => section_vals_get_subs_vals(subsys_section, "KIND")
NULLIFY (qs_kind_set)
CALL cp_subsys_get(subsys_mix, atomic_kind_set=atomic_kind_set)
CALL create_qs_kind_set(qs_kind_set, atomic_kind_set, kind_section, &
force_env%para_env, force_env_section, silent=.FALSE.)
mixed_cdft%qs_kind_set => qs_kind_set
DEALLOCATE (i_force_eval)
CALL section_vals_release(force_env_section)
mixed_cdft%qs_kind_set => qs_kind_set
END IF
CALL force_env_get(force_env=force_env, &
force_env_section=force_env_section)