-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_mo_io.F
1473 lines (1269 loc) · 63.9 KB
/
qs_mo_io.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 Definition and initialisation of the mo data type.
!> \par History
!> - adapted to the new QS environment data structure (02.04.2002,MK)
!> - set_mo_occupation added (17.04.02,MK)
!> - correct_mo_eigenvalues added (18.04.02,MK)
!> - calculate_density_matrix moved from qs_scf to here (22.04.02,MK)
!> - mo_set_p_type added (23.04.02,MK)
!> - PRIVATE attribute set for TYPE mo_set_type (23.04.02,MK)
!> - started conversion to LSD (1.2003, Joost VandeVondele)
!> - Split of from qs_mo_types (07.2014, JGH)
!> \author Matthias Krack (09.05.2001,MK)
! **************************************************************************************************
MODULE qs_mo_io
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind,&
get_atomic_kind_set
USE basis_set_types, ONLY: get_gto_basis_set,&
gto_basis_set_type
USE cp_dbcsr_api, ONLY: dbcsr_binary_write,&
dbcsr_checksum,&
dbcsr_create,&
dbcsr_p_type,&
dbcsr_release,&
dbcsr_type
USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
copy_fm_to_dbcsr
USE cp_files, ONLY: close_file,&
open_file
USE cp_fm_types, ONLY: cp_fm_get_info,&
cp_fm_get_submatrix,&
cp_fm_set_all,&
cp_fm_set_submatrix,&
cp_fm_to_fm,&
cp_fm_type,&
cp_fm_write_unformatted
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type,&
cp_to_string
USE cp_output_handling, ONLY: cp_p_file,&
cp_print_key_finished_output,&
cp_print_key_generate_filename,&
cp_print_key_should_output,&
cp_print_key_unit_nr
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE kahan_sum, ONLY: accurate_sum
USE kinds, ONLY: default_path_length,&
default_string_length,&
dp
USE message_passing, ONLY: mp_para_env_type
USE orbital_pointers, ONLY: indco,&
nco,&
nso
USE orbital_symbols, ONLY: cgf_symbol,&
sgf_symbol
USE orbital_transformation_matrices, ONLY: orbtramat
USE particle_types, ONLY: particle_type
USE physcon, ONLY: evolt
USE qs_density_matrices, ONLY: calculate_density_matrix
USE qs_dftb_types, ONLY: qs_dftb_atom_type
USE qs_dftb_utils, ONLY: get_dftb_atom_param
USE qs_kind_types, ONLY: get_qs_kind,&
get_qs_kind_set,&
qs_kind_type
USE qs_mo_occupation, ONLY: set_mo_occupation
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_mo_io'
PUBLIC :: wfn_restart_file_name, &
write_rt_mos_to_restart, &
read_rt_mos_from_restart, &
write_dm_binary_restart, &
write_mo_set_to_output_unit, &
write_mo_set_to_restart, &
read_mo_set_from_restart, &
read_mos_restart_low, &
write_mo_set_low
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param mo_array ...
!> \param particle_set ...
!> \param dft_section ...
!> \param qs_kind_set ...
! **************************************************************************************************
SUBROUTINE write_mo_set_to_restart(mo_array, particle_set, dft_section, qs_kind_set)
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mo_array
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(section_vals_type), POINTER :: dft_section
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
CHARACTER(LEN=*), PARAMETER :: routineN = 'write_mo_set_to_restart'
CHARACTER(LEN=30), DIMENSION(2), PARAMETER :: &
keys = (/"SCF%PRINT%RESTART_HISTORY", "SCF%PRINT%RESTART "/)
INTEGER :: handle, ikey, ires, ispin
TYPE(cp_logger_type), POINTER :: logger
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
IF (BTEST(cp_print_key_should_output(logger%iter_info, &
dft_section, keys(1)), cp_p_file) .OR. &
BTEST(cp_print_key_should_output(logger%iter_info, &
dft_section, keys(2)), cp_p_file)) THEN
IF (mo_array(1)%use_mo_coeff_b) THEN
! we are using the dbcsr mo_coeff
! we copy it to the fm for anycase
DO ispin = 1, SIZE(mo_array)
IF (.NOT. ASSOCIATED(mo_array(ispin)%mo_coeff_b)) THEN
CPASSERT(.FALSE.)
END IF
CALL copy_dbcsr_to_fm(mo_array(ispin)%mo_coeff_b, &
mo_array(ispin)%mo_coeff) !fm->dbcsr
END DO
END IF
DO ikey = 1, SIZE(keys)
IF (BTEST(cp_print_key_should_output(logger%iter_info, &
dft_section, keys(ikey)), cp_p_file)) THEN
ires = cp_print_key_unit_nr(logger, dft_section, keys(ikey), &
extension=".wfn", file_status="REPLACE", file_action="WRITE", &
do_backup=.TRUE., file_form="UNFORMATTED")
CALL write_mo_set_low(mo_array, particle_set=particle_set, &
qs_kind_set=qs_kind_set, ires=ires)
CALL cp_print_key_finished_output(ires, logger, dft_section, TRIM(keys(ikey)))
END IF
END DO
END IF
CALL timestop(handle)
END SUBROUTINE write_mo_set_to_restart
! **************************************************************************************************
!> \brief calculates density matrix from mo set and writes the density matrix
!> into a binary restart file
!> \param mo_array mos
!> \param dft_section dft input section
!> \param tmpl_matrix template dbcsr matrix
!> \author Mohammad Hossein Bani-Hashemian
! **************************************************************************************************
SUBROUTINE write_dm_binary_restart(mo_array, dft_section, tmpl_matrix)
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mo_array
TYPE(section_vals_type), POINTER :: dft_section
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: tmpl_matrix
CHARACTER(LEN=*), PARAMETER :: routineN = 'write_dm_binary_restart'
CHARACTER(LEN=default_path_length) :: file_name, project_name
INTEGER :: handle, ispin, unit_nr
LOGICAL :: do_dm_restart
REAL(KIND=dp) :: cs_pos
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_type), POINTER :: matrix_p_tmp
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
IF (logger%para_env%is_source()) THEN
unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
ELSE
unit_nr = -1
END IF
project_name = logger%iter_info%project_name
CALL section_vals_val_get(dft_section, "SCF%PRINT%DM_RESTART_WRITE", l_val=do_dm_restart)
NULLIFY (matrix_p_tmp)
IF (do_dm_restart) THEN
ALLOCATE (matrix_p_tmp)
DO ispin = 1, SIZE(mo_array)
CALL dbcsr_create(matrix_p_tmp, template=tmpl_matrix(ispin)%matrix, name="DM RESTART")
IF (.NOT. ASSOCIATED(mo_array(ispin)%mo_coeff_b)) CPABORT("mo_coeff_b NOT ASSOCIATED")
CALL copy_fm_to_dbcsr(mo_array(ispin)%mo_coeff, mo_array(ispin)%mo_coeff_b)
CALL calculate_density_matrix(mo_array(ispin), matrix_p_tmp, &
use_dbcsr=.TRUE., retain_sparsity=.FALSE.)
WRITE (file_name, '(A,I0,A)') TRIM(project_name)//"_SCF_DM_SPIN_", ispin, "_RESTART.dm"
cs_pos = dbcsr_checksum(matrix_p_tmp, pos=.TRUE.)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A,E20.8)') "Writing restart DM "//TRIM(file_name)//" with checksum: ", cs_pos
END IF
CALL dbcsr_binary_write(matrix_p_tmp, file_name)
CALL dbcsr_release(matrix_p_tmp)
END DO
DEALLOCATE (matrix_p_tmp)
END IF
CALL timestop(handle)
END SUBROUTINE write_dm_binary_restart
! **************************************************************************************************
!> \brief ...
!> \param mo_array ...
!> \param rt_mos ...
!> \param particle_set ...
!> \param dft_section ...
!> \param qs_kind_set ...
! **************************************************************************************************
SUBROUTINE write_rt_mos_to_restart(mo_array, rt_mos, particle_set, dft_section, &
qs_kind_set)
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mo_array
TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: rt_mos
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(section_vals_type), POINTER :: dft_section
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
CHARACTER(LEN=*), PARAMETER :: routineN = 'write_rt_mos_to_restart'
CHARACTER(LEN=43), DIMENSION(2), PARAMETER :: keys = (/ &
"REAL_TIME_PROPAGATION%PRINT%RESTART_HISTORY", &
"REAL_TIME_PROPAGATION%PRINT%RESTART "/)
INTEGER :: handle, ikey, ires
TYPE(cp_logger_type), POINTER :: logger
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
IF (BTEST(cp_print_key_should_output(logger%iter_info, &
dft_section, keys(1)), cp_p_file) .OR. &
BTEST(cp_print_key_should_output(logger%iter_info, &
dft_section, keys(2)), cp_p_file)) THEN
DO ikey = 1, SIZE(keys)
IF (BTEST(cp_print_key_should_output(logger%iter_info, &
dft_section, keys(ikey)), cp_p_file)) THEN
ires = cp_print_key_unit_nr(logger, dft_section, keys(ikey), &
extension=".rtpwfn", file_status="REPLACE", file_action="WRITE", &
do_backup=.TRUE., file_form="UNFORMATTED")
CALL write_mo_set_low(mo_array, rt_mos=rt_mos, qs_kind_set=qs_kind_set, &
particle_set=particle_set, ires=ires)
CALL cp_print_key_finished_output(ires, logger, dft_section, TRIM(keys(ikey)))
END IF
END DO
END IF
CALL timestop(handle)
END SUBROUTINE write_rt_mos_to_restart
! **************************************************************************************************
!> \brief ...
!> \param mo_array ...
!> \param qs_kind_set ...
!> \param particle_set ...
!> \param ires ...
!> \param rt_mos ...
! **************************************************************************************************
SUBROUTINE write_mo_set_low(mo_array, qs_kind_set, particle_set, ires, rt_mos)
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mo_array
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
INTEGER :: ires
TYPE(cp_fm_type), DIMENSION(:), INTENT(IN), &
OPTIONAL :: rt_mos
CHARACTER(LEN=*), PARAMETER :: routineN = 'write_mo_set_low'
INTEGER :: handle, iatom, ikind, imat, iset, &
ishell, ispin, lmax, lshell, &
max_block, nao, natom, nmo, nset, &
nset_max, nshell_max, nspin
INTEGER, DIMENSION(:), POINTER :: nset_info, nshell
INTEGER, DIMENSION(:, :), POINTER :: l, nshell_info
INTEGER, DIMENSION(:, :, :), POINTER :: nso_info
TYPE(gto_basis_set_type), POINTER :: orb_basis_set
TYPE(qs_dftb_atom_type), POINTER :: dftb_parameter
CALL timeset(routineN, handle)
nspin = SIZE(mo_array)
nao = mo_array(1)%nao
IF (ires > 0) THEN
! *** create some info about the basis set first ***
natom = SIZE(particle_set, 1)
nset_max = 0
nshell_max = 0
DO iatom = 1, natom
NULLIFY (orb_basis_set, dftb_parameter)
CALL get_atomic_kind(particle_set(iatom)%atomic_kind, kind_number=ikind)
CALL get_qs_kind(qs_kind_set(ikind), &
basis_set=orb_basis_set, dftb_parameter=dftb_parameter)
IF (ASSOCIATED(orb_basis_set)) THEN
CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
nset=nset, &
nshell=nshell, &
l=l)
nset_max = MAX(nset_max, nset)
DO iset = 1, nset
nshell_max = MAX(nshell_max, nshell(iset))
END DO
ELSEIF (ASSOCIATED(dftb_parameter)) THEN
CALL get_dftb_atom_param(dftb_parameter, lmax=lmax)
nset_max = MAX(nset_max, 1)
nshell_max = MAX(nshell_max, lmax + 1)
ELSE
! We assume here an atom without a basis set
! CPABORT("Unknown basis type. ")
END IF
END DO
ALLOCATE (nso_info(nshell_max, nset_max, natom))
nso_info(:, :, :) = 0
ALLOCATE (nshell_info(nset_max, natom))
nshell_info(:, :) = 0
ALLOCATE (nset_info(natom))
nset_info(:) = 0
DO iatom = 1, natom
NULLIFY (orb_basis_set, dftb_parameter)
CALL get_atomic_kind(particle_set(iatom)%atomic_kind, kind_number=ikind)
CALL get_qs_kind(qs_kind_set(ikind), &
basis_set=orb_basis_set, dftb_parameter=dftb_parameter)
IF (ASSOCIATED(orb_basis_set)) THEN
CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
nset=nset, &
nshell=nshell, &
l=l)
nset_info(iatom) = nset
DO iset = 1, nset
nshell_info(iset, iatom) = nshell(iset)
DO ishell = 1, nshell(iset)
lshell = l(ishell, iset)
nso_info(ishell, iset, iatom) = nso(lshell)
END DO
END DO
ELSEIF (ASSOCIATED(dftb_parameter)) THEN
CALL get_dftb_atom_param(dftb_parameter, lmax=lmax)
nset_info(iatom) = 1
nshell_info(1, iatom) = lmax + 1
DO ishell = 1, lmax + 1
lshell = ishell - 1
nso_info(ishell, 1, iatom) = nso(lshell)
END DO
ELSE
! We assume here an atom without a basis set
! CPABORT("Unknown basis type. ")
END IF
END DO
WRITE (ires) natom, nspin, nao, nset_max, nshell_max
WRITE (ires) nset_info
WRITE (ires) nshell_info
WRITE (ires) nso_info
DEALLOCATE (nset_info)
DEALLOCATE (nshell_info)
DEALLOCATE (nso_info)
END IF
! use the scalapack block size as a default for buffering columns
CALL cp_fm_get_info(mo_array(1)%mo_coeff, ncol_block=max_block)
DO ispin = 1, nspin
nmo = mo_array(ispin)%nmo
IF ((ires > 0) .AND. (nmo > 0)) THEN
WRITE (ires) nmo, &
mo_array(ispin)%homo, &
mo_array(ispin)%lfomo, &
mo_array(ispin)%nelectron
WRITE (ires) mo_array(ispin)%eigenvalues(1:nmo), &
mo_array(ispin)%occupation_numbers(1:nmo)
END IF
IF (PRESENT(rt_mos)) THEN
DO imat = 2*ispin - 1, 2*ispin
CALL cp_fm_write_unformatted(rt_mos(imat), ires)
END DO
ELSE
CALL cp_fm_write_unformatted(mo_array(ispin)%mo_coeff, ires)
END IF
END DO
CALL timestop(handle)
END SUBROUTINE write_mo_set_low
! **************************************************************************************************
!> \brief ...
!> \param filename ...
!> \param exist ...
!> \param section ...
!> \param logger ...
!> \param kp ...
!> \param xas ...
!> \param rtp ...
! **************************************************************************************************
SUBROUTINE wfn_restart_file_name(filename, exist, section, logger, kp, xas, rtp)
CHARACTER(LEN=default_path_length), INTENT(OUT) :: filename
LOGICAL, INTENT(OUT) :: exist
TYPE(section_vals_type), POINTER :: section
TYPE(cp_logger_type), POINTER :: logger
LOGICAL, INTENT(IN), OPTIONAL :: kp, xas, rtp
INTEGER :: n_rep_val
LOGICAL :: my_kp, my_rtp, my_xas
TYPE(section_vals_type), POINTER :: print_key
my_kp = .FALSE.
my_xas = .FALSE.
my_rtp = .FALSE.
IF (PRESENT(kp)) my_kp = kp
IF (PRESENT(xas)) my_xas = xas
IF (PRESENT(rtp)) my_rtp = rtp
exist = .FALSE.
CALL section_vals_val_get(section, "WFN_RESTART_FILE_NAME", n_rep_val=n_rep_val)
IF (n_rep_val > 0) THEN
CALL section_vals_val_get(section, "WFN_RESTART_FILE_NAME", c_val=filename)
ELSE
IF (my_xas) THEN
! try to read from the filename that is generated automatically from the printkey
print_key => section_vals_get_subs_vals(section, "PRINT%RESTART")
filename = cp_print_key_generate_filename(logger, print_key, &
extension="", my_local=.FALSE.)
ELSE IF (my_rtp) THEN
! try to read from the filename that is generated automatically from the printkey
print_key => section_vals_get_subs_vals(section, "REAL_TIME_PROPAGATION%PRINT%RESTART")
filename = cp_print_key_generate_filename(logger, print_key, &
extension=".rtpwfn", my_local=.FALSE.)
ELSE IF (my_kp) THEN
! try to read from the filename that is generated automatically from the printkey
print_key => section_vals_get_subs_vals(section, "SCF%PRINT%RESTART")
filename = cp_print_key_generate_filename(logger, print_key, &
extension=".kp", my_local=.FALSE.)
ELSE
! try to read from the filename that is generated automatically from the printkey
print_key => section_vals_get_subs_vals(section, "SCF%PRINT%RESTART")
filename = cp_print_key_generate_filename(logger, print_key, &
extension=".wfn", my_local=.FALSE.)
END IF
END IF
IF (.NOT. my_xas) THEN
INQUIRE (FILE=filename, exist=exist)
END IF
END SUBROUTINE wfn_restart_file_name
! **************************************************************************************************
!> \brief ...
!> \param mo_array ...
!> \param atomic_kind_set ...
!> \param qs_kind_set ...
!> \param particle_set ...
!> \param para_env ...
!> \param id_nr ...
!> \param multiplicity ...
!> \param dft_section ...
!> \param natom_mismatch ...
!> \param cdft ...
!> \param out_unit ...
! **************************************************************************************************
SUBROUTINE read_mo_set_from_restart(mo_array, atomic_kind_set, qs_kind_set, particle_set, &
para_env, id_nr, multiplicity, dft_section, natom_mismatch, &
cdft, out_unit)
TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mo_array
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(mp_para_env_type), POINTER :: para_env
INTEGER, INTENT(IN) :: id_nr, multiplicity
TYPE(section_vals_type), POINTER :: dft_section
LOGICAL, INTENT(OUT), OPTIONAL :: natom_mismatch
LOGICAL, INTENT(IN), OPTIONAL :: cdft
INTEGER, INTENT(IN), OPTIONAL :: out_unit
CHARACTER(LEN=*), PARAMETER :: routineN = 'read_mo_set_from_restart'
CHARACTER(LEN=default_path_length) :: file_name
INTEGER :: handle, ispin, my_out_unit, natom, &
nspin, restart_unit
LOGICAL :: exist, my_cdft
TYPE(cp_logger_type), POINTER :: logger
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
my_cdft = .FALSE.
IF (PRESENT(cdft)) my_cdft = cdft
my_out_unit = -1
IF (PRESENT(out_unit)) my_out_unit = out_unit
nspin = SIZE(mo_array)
restart_unit = -1
IF (para_env%is_source()) THEN
natom = SIZE(particle_set, 1)
CALL wfn_restart_file_name(file_name, exist, dft_section, logger)
IF (id_nr /= 0) THEN
! Is it one of the backup files?
file_name = TRIM(file_name)//".bak-"//ADJUSTL(cp_to_string(id_nr))
END IF
CALL open_file(file_name=file_name, &
file_action="READ", &
file_form="UNFORMATTED", &
file_status="OLD", &
unit_number=restart_unit)
END IF
CALL read_mos_restart_low(mo_array, para_env=para_env, qs_kind_set=qs_kind_set, &
particle_set=particle_set, natom=natom, &
rst_unit=restart_unit, multiplicity=multiplicity, natom_mismatch=natom_mismatch)
IF (PRESENT(natom_mismatch)) THEN
! read_mos_restart_low only the io_node returns natom_mismatch, must broadcast it
CALL para_env%bcast(natom_mismatch)
IF (natom_mismatch) THEN
IF (para_env%is_source()) CALL close_file(unit_number=restart_unit)
CALL timestop(handle)
RETURN
END IF
END IF
! Close restart file
IF (para_env%is_source()) THEN
IF (my_out_unit > 0) THEN
WRITE (UNIT=my_out_unit, FMT="(T2,A)") &
"WFN_RESTART| Restart file "//TRIM(file_name)//" read"
END IF
CALL close_file(unit_number=restart_unit)
END IF
! CDFT has no real dft_section and does not need to print
IF (.NOT. my_cdft) THEN
DO ispin = 1, nspin
CALL write_mo_set_to_output_unit(mo_array(ispin), atomic_kind_set, qs_kind_set, &
particle_set, dft_section, 4, 0, final_mos=.FALSE.)
END DO
END IF
CALL timestop(handle)
END SUBROUTINE read_mo_set_from_restart
! **************************************************************************************************
!> \brief ...
!> \param mo_array ...
!> \param rt_mos ...
!> \param atomic_kind_set ...
!> \param qs_kind_set ...
!> \param particle_set ...
!> \param para_env ...
!> \param id_nr ...
!> \param multiplicity ...
!> \param dft_section ...
! **************************************************************************************************
SUBROUTINE read_rt_mos_from_restart(mo_array, rt_mos, atomic_kind_set, qs_kind_set, &
particle_set, para_env, id_nr, multiplicity, dft_section)
TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mo_array
TYPE(cp_fm_type), DIMENSION(:), POINTER :: rt_mos
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(mp_para_env_type), POINTER :: para_env
INTEGER, INTENT(IN) :: id_nr, multiplicity
TYPE(section_vals_type), POINTER :: dft_section
CHARACTER(LEN=*), PARAMETER :: routineN = 'read_rt_mos_from_restart'
CHARACTER(LEN=default_path_length) :: file_name
INTEGER :: handle, ispin, natom, nspin, &
restart_unit, unit_nr
LOGICAL :: exist
TYPE(cp_logger_type), POINTER :: logger
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
nspin = SIZE(mo_array)
restart_unit = -1
IF (para_env%is_source()) THEN
natom = SIZE(particle_set, 1)
CALL wfn_restart_file_name(file_name, exist, dft_section, logger, rtp=.TRUE.)
IF (id_nr /= 0) THEN
! Is it one of the backup files?
file_name = TRIM(file_name)//".bak-"//ADJUSTL(cp_to_string(id_nr))
END IF
unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T2,A)') "Read RTP restart from the file: "//TRIM(file_name)
END IF
CALL open_file(file_name=file_name, &
file_action="READ", &
file_form="UNFORMATTED", &
file_status="OLD", &
unit_number=restart_unit)
END IF
CALL read_mos_restart_low(mo_array, rt_mos=rt_mos, para_env=para_env, &
particle_set=particle_set, qs_kind_set=qs_kind_set, natom=natom, &
rst_unit=restart_unit, multiplicity=multiplicity)
! Close restart file
IF (para_env%is_source()) CALL close_file(unit_number=restart_unit)
DO ispin = 1, nspin
CALL write_mo_set_to_output_unit(mo_array(ispin), atomic_kind_set, qs_kind_set, &
particle_set, dft_section, 4, 0, final_mos=.FALSE.)
END DO
CALL timestop(handle)
END SUBROUTINE read_rt_mos_from_restart
! **************************************************************************************************
!> \brief Reading the mos from apreviously defined restart file
!> \param mos ...
!> \param para_env ...
!> \param qs_kind_set ...
!> \param particle_set ...
!> \param natom ...
!> \param rst_unit ...
!> \param multiplicity ...
!> \param rt_mos ...
!> \param natom_mismatch ...
!> \par History
!> 12.2007 created [MI]
!> \author MI
! **************************************************************************************************
SUBROUTINE read_mos_restart_low(mos, para_env, qs_kind_set, particle_set, natom, rst_unit, &
multiplicity, rt_mos, natom_mismatch)
TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
INTEGER, INTENT(IN) :: natom, rst_unit
INTEGER, INTENT(in), OPTIONAL :: multiplicity
TYPE(cp_fm_type), DIMENSION(:), OPTIONAL, POINTER :: rt_mos
LOGICAL, INTENT(OUT), OPTIONAL :: natom_mismatch
INTEGER :: homo, homo_read, i, iatom, ikind, imat, irow, iset, iset_read, ishell, &
ishell_read, iso, ispin, lfomo_read, lmax, lshell, my_mult, nao, nao_read, natom_read, &
nelectron, nelectron_read, nmo, nmo_read, nnshell, nset, nset_max, nshell_max, nspin, &
nspin_read, offset_read
INTEGER, DIMENSION(:), POINTER :: nset_info, nshell
INTEGER, DIMENSION(:, :), POINTER :: l, nshell_info
INTEGER, DIMENSION(:, :, :), POINTER :: nso_info, offset_info
LOGICAL :: minbas, natom_match, use_this
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eig_read, occ_read
REAL(KIND=dp), DIMENSION(:, :), POINTER :: vecbuffer, vecbuffer_read
TYPE(cp_logger_type), POINTER :: logger
TYPE(gto_basis_set_type), POINTER :: orb_basis_set
TYPE(qs_dftb_atom_type), POINTER :: dftb_parameter
logger => cp_get_default_logger()
nspin = SIZE(mos)
nao = mos(1)%nao
my_mult = 0
IF (PRESENT(multiplicity)) my_mult = multiplicity
IF (para_env%is_source()) THEN
READ (rst_unit) natom_read, nspin_read, nao_read, nset_max, nshell_max
IF (PRESENT(rt_mos)) THEN
IF (nspin_read /= nspin) THEN
CPABORT("To change nspin is not possible. ")
END IF
ELSE
! we should allow for restarting with different spin settings
IF (nspin_read /= nspin) THEN
WRITE (cp_logger_get_default_unit_nr(logger), *) &
"READ RESTART : WARNING : nspin is not equal "
END IF
! this case needs fixing of homo/lfomo/nelec/occupations ...
IF (nspin_read > nspin) THEN
CPABORT("Reducing nspin is not possible. ")
END IF
END IF
natom_match = (natom_read == natom)
IF (natom_match) THEN ! actually do the read read
! Let's make it possible to change the basis set
ALLOCATE (nso_info(nshell_max, nset_max, natom_read))
ALLOCATE (nshell_info(nset_max, natom_read))
ALLOCATE (nset_info(natom_read))
ALLOCATE (offset_info(nshell_max, nset_max, natom_read))
IF (nao_read /= nao) THEN
WRITE (cp_logger_get_default_unit_nr(logger), *) &
" READ RESTART : WARNING : DIFFERENT # AOs ", nao, nao_read
IF (PRESENT(rt_mos)) &
CPABORT("To change basis is not possible. ")
END IF
READ (rst_unit) nset_info
READ (rst_unit) nshell_info
READ (rst_unit) nso_info
i = 1
DO iatom = 1, natom
DO iset = 1, nset_info(iatom)
DO ishell = 1, nshell_info(iset, iatom)
offset_info(ishell, iset, iatom) = i
i = i + nso_info(ishell, iset, iatom)
END DO
END DO
END DO
ALLOCATE (vecbuffer_read(1, nao_read))
END IF ! natom_match
END IF ! ionode
! make natom_match and natom_mismatch uniform across all nodes
CALL para_env%bcast(natom_match)
IF (PRESENT(natom_mismatch)) natom_mismatch = .NOT. natom_match
! handle natom_match false
IF (.NOT. natom_match) THEN
IF (PRESENT(natom_mismatch)) THEN
WRITE (cp_logger_get_default_unit_nr(logger), *) &
" READ RESTART : WARNING : DIFFERENT natom, returning ", natom, natom_read
RETURN
ELSE
CPABORT("Incorrect number of atoms in restart file. ")
END IF
END IF
CALL para_env%bcast(nspin_read)
ALLOCATE (vecbuffer(1, nao))
DO ispin = 1, nspin
nmo = mos(ispin)%nmo
homo = mos(ispin)%homo
mos(ispin)%eigenvalues(:) = 0.0_dp
mos(ispin)%occupation_numbers(:) = 0.0_dp
CALL cp_fm_set_all(mos(ispin)%mo_coeff, 0.0_dp)
IF (para_env%is_source() .AND. (nmo > 0)) THEN
READ (rst_unit) nmo_read, homo_read, lfomo_read, nelectron_read
ALLOCATE (eig_read(nmo_read), occ_read(nmo_read))
eig_read = 0.0_dp
occ_read = 0.0_dp
nmo = MIN(nmo, nmo_read)
IF (nmo_read < nmo) &
CALL cp_warn(__LOCATION__, &
"The number of MOs on the restart unit is smaller than the number of "// &
"the allocated MOs. The MO set will be padded with zeros!")
IF (nmo_read > nmo) &
CALL cp_warn(__LOCATION__, &
"The number of MOs on the restart unit is greater than the number of "// &
"the allocated MOs. The read MO set will be truncated!")
READ (rst_unit) eig_read(1:nmo_read), occ_read(1:nmo_read)
mos(ispin)%eigenvalues(1:nmo) = eig_read(1:nmo)
mos(ispin)%occupation_numbers(1:nmo) = occ_read(1:nmo)
DEALLOCATE (eig_read, occ_read)
mos(ispin)%homo = homo_read
mos(ispin)%lfomo = lfomo_read
IF (homo_read > nmo) THEN
IF (nelectron_read == mos(ispin)%nelectron) THEN
CALL cp_warn(__LOCATION__, &
"The number of occupied MOs on the restart unit is larger than "// &
"the allocated MOs. The read MO set will be truncated and the occupation numbers recalculated!")
CALL set_mo_occupation(mo_set=mos(ispin))
ELSE
! can not make this a warning i.e. homo must be smaller than nmo
! otherwise e.g. set_mo_occupation will go out of bounds
CPABORT("Number of occupied MOs on restart unit larger than allocated MOs. ")
END IF
END IF
END IF
CALL para_env%bcast(nmo)
CALL para_env%bcast(mos(ispin)%homo)
CALL para_env%bcast(mos(ispin)%lfomo)
CALL para_env%bcast(mos(ispin)%nelectron)
CALL para_env%bcast(mos(ispin)%eigenvalues)
CALL para_env%bcast(mos(ispin)%occupation_numbers)
IF (PRESENT(rt_mos)) THEN
DO imat = 2*ispin - 1, 2*ispin
DO i = 1, nmo
IF (para_env%is_source()) THEN
READ (rst_unit) vecbuffer
ELSE
vecbuffer(1, :) = 0.0_dp
END IF
CALL para_env%bcast(vecbuffer)
CALL cp_fm_set_submatrix(rt_mos(imat), &
vecbuffer, 1, i, nao, 1, transpose=.TRUE.)
END DO
END DO
ELSE
DO i = 1, nmo
IF (para_env%is_source()) THEN
READ (rst_unit) vecbuffer_read
! now, try to assign the read to the real vector
! in case the basis set changed this involves some guessing
irow = 1
DO iatom = 1, natom
NULLIFY (orb_basis_set, dftb_parameter, l, nshell)
CALL get_atomic_kind(particle_set(iatom)%atomic_kind, kind_number=ikind)
CALL get_qs_kind(qs_kind_set(ikind), &
basis_set=orb_basis_set, dftb_parameter=dftb_parameter)
IF (ASSOCIATED(orb_basis_set)) THEN
CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
nset=nset, &
nshell=nshell, &
l=l)
minbas = .FALSE.
ELSEIF (ASSOCIATED(dftb_parameter)) THEN
CALL get_dftb_atom_param(dftb_parameter, lmax=lmax)
nset = 1
minbas = .TRUE.
ELSE
! assume an atom without basis set
! CPABORT("Unknown basis set type. ")
nset = 0
END IF
use_this = .TRUE.
iset_read = 1
DO iset = 1, nset
ishell_read = 1
IF (minbas) THEN
nnshell = lmax + 1
ELSE
nnshell = nshell(iset)
END IF
DO ishell = 1, nnshell
IF (minbas) THEN
lshell = ishell - 1
ELSE
lshell = l(ishell, iset)
END IF
IF (iset_read > nset_info(iatom)) use_this = .FALSE.
IF (use_this) THEN ! avoids out of bound access of the lower line if false
IF (nso(lshell) == nso_info(ishell_read, iset_read, iatom)) THEN
offset_read = offset_info(ishell_read, iset_read, iatom)
ishell_read = ishell_read + 1
IF (ishell_read > nshell_info(iset, iatom)) THEN
ishell_read = 1
iset_read = iset_read + 1
END IF
ELSE
use_this = .FALSE.
END IF
END IF
DO iso = 1, nso(lshell)
IF (use_this) THEN
IF (offset_read - 1 + iso .LT. 1 .OR. offset_read - 1 + iso .GT. nao_read) THEN
vecbuffer(1, irow) = 0.0_dp
ELSE
vecbuffer(1, irow) = vecbuffer_read(1, offset_read - 1 + iso)
END IF
ELSE
vecbuffer(1, irow) = 0.0_dp
END IF
irow = irow + 1
END DO
use_this = .TRUE.
END DO
END DO
END DO
ELSE
vecbuffer(1, :) = 0.0_dp
END IF
CALL para_env%bcast(vecbuffer)
CALL cp_fm_set_submatrix(mos(ispin)%mo_coeff, &
vecbuffer, 1, i, nao, 1, transpose=.TRUE.)
END DO
END IF
! Skip extra MOs if there any
IF (para_env%is_source()) THEN
!ignore nmo = 0
IF (nmo > 0) THEN
DO i = nmo + 1, nmo_read
READ (rst_unit) vecbuffer_read
END DO
END IF
END IF
IF (.NOT. PRESENT(rt_mos)) THEN
IF (ispin == 1 .AND. nspin_read < nspin) THEN
mos(ispin + 1)%homo = mos(ispin)%homo
mos(ispin + 1)%lfomo = mos(ispin)%lfomo
nelectron = mos(ispin)%nelectron
IF (my_mult .NE. 1) THEN
CALL cp_abort(__LOCATION__, &
"Restarting an LSD calculation from an LDA wfn only works for multiplicity=1 (singlets).")
END IF
IF (mos(ispin + 1)%nelectron < 0) THEN
CPABORT("LSD: too few electrons for this multiplisity. ")
END IF
mos(ispin + 1)%eigenvalues = mos(ispin)%eigenvalues
mos(ispin)%occupation_numbers = mos(ispin)%occupation_numbers/2.0_dp
mos(ispin + 1)%occupation_numbers = mos(ispin)%occupation_numbers
CALL cp_fm_to_fm(mos(ispin)%mo_coeff, mos(ispin + 1)%mo_coeff)
EXIT
END IF
END IF
END DO ! ispin
DEALLOCATE (vecbuffer)
IF (para_env%is_source()) THEN
DEALLOCATE (vecbuffer_read)
DEALLOCATE (offset_info)
DEALLOCATE (nso_info)
DEALLOCATE (nshell_info)
DEALLOCATE (nset_info)
END IF
END SUBROUTINE read_mos_restart_low
! **************************************************************************************************
!> \brief Write MO information to output file (eigenvalues, occupation numbers, coefficients)
!> \param mo_set ...
!> \param atomic_kind_set ...
!> \param qs_kind_set ...
!> \param particle_set ...
!> \param dft_section ...
!> \param before Digits before the dot
!> \param kpoint An integer that labels the current k point, e.g. its index
!> \param final_mos ...
!> \param spin ...
!> \param solver_method ...
!> \param rtp ...
!> \param cpart ...
!> \param sim_step ...
!> \param umo_set ...
!> \date 15.05.2001
!> \par History:
!> - Optionally print Cartesian MOs (20.04.2005, MK)
!> - Revise printout of MO information (05.05.2021, MK)
!> \par Variables
!> - after : Number of digits after point.
!> - before: Number of digits before point.
!> \author Matthias Krack (MK)
!> \version 1.1
! **************************************************************************************************
SUBROUTINE write_mo_set_to_output_unit(mo_set, atomic_kind_set, qs_kind_set, particle_set, &
dft_section, before, kpoint, final_mos, spin, &
solver_method, rtp, cpart, sim_step, umo_set)
TYPE(mo_set_type), INTENT(IN) :: mo_set
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(section_vals_type), POINTER :: dft_section