-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_ot_minimizer.F
1572 lines (1422 loc) · 66.1 KB
/
qs_ot_minimizer.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 orbital transformations
!> \par History
!> None
!> \author Joost VandeVondele (09.2002)
! **************************************************************************************************
MODULE qs_ot_minimizer
USE cp_dbcsr_api, ONLY: dbcsr_add,&
dbcsr_copy,&
dbcsr_dot,&
dbcsr_get_info,&
dbcsr_init_random,&
dbcsr_p_type,&
dbcsr_scale,&
dbcsr_set
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE kinds, ONLY: dp,&
int_8
USE mathlib, ONLY: diamat_all
USE preconditioner, ONLY: apply_preconditioner
USE qs_ot, ONLY: qs_ot_get_derivative,&
qs_ot_get_derivative_ref
USE qs_ot_types, ONLY: qs_ot_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
PUBLIC :: ot_mini
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_ot_minimizer'
CONTAINS
!
! the minimizer interface
! should present all possible modes of minimization
! these include CG SD DIIS
!
!
! IN the case of nspin != 1 we have a gradient that is distributed over different qs_ot_env.
! still things remain basically the same, since there are no constraints between the different qs_ot_env
! we only should take care that the various scalar products are taken over the full vectors.
! all the information needed and collected can be stored in the fist qs_ot_env only
! (indicating that the data type for the gradient/position and minization should be separated)
!
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
!> \param matrix_hc ...
! **************************************************************************************************
SUBROUTINE ot_mini(qs_ot_env, matrix_hc)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_hc
CHARACTER(len=*), PARAMETER :: routineN = 'ot_mini'
INTEGER :: handle, ispin, nspin
LOGICAL :: do_ener, do_ks
REAL(KIND=dp) :: tmp
CALL timeset(routineN, handle)
nspin = SIZE(qs_ot_env)
do_ks = qs_ot_env(1)%settings%ks
do_ener = qs_ot_env(1)%settings%do_ener
qs_ot_env(1)%OT_METHOD_FULL = ""
! compute the gradient for the variables x
IF (.NOT. qs_ot_env(1)%energy_only) THEN
qs_ot_env(1)%gradient = 0.0_dp
DO ispin = 1, nspin
IF (do_ks) THEN
SELECT CASE (qs_ot_env(1)%settings%ot_algorithm)
CASE ("TOD")
CALL qs_ot_get_derivative(matrix_hc(ispin)%matrix, qs_ot_env(ispin)%matrix_x, &
qs_ot_env(ispin)%matrix_sx, &
qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin))
CASE ("REF")
CALL qs_ot_get_derivative_ref(matrix_hc(ispin)%matrix, &
qs_ot_env(ispin)%matrix_x, qs_ot_env(ispin)%matrix_sx, &
qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin))
CASE DEFAULT
CPABORT("ALGORITHM NYI")
END SELECT
END IF
! and also the gradient along the direction
IF (qs_ot_env(1)%use_dx) THEN
IF (do_ks) THEN
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_dx, tmp)
qs_ot_env(1)%gradient = qs_ot_env(1)%gradient + tmp
IF (qs_ot_env(1)%settings%do_rotation) THEN
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_dx, tmp)
qs_ot_env(1)%gradient = qs_ot_env(1)%gradient + 0.5_dp*tmp
END IF
END IF
IF (do_ener) THEN
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_gx, qs_ot_env(ispin)%ener_dx)
qs_ot_env(1)%gradient = qs_ot_env(1)%gradient + tmp
END IF
ELSE
IF (do_ks) THEN
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_gx, tmp)
qs_ot_env(1)%gradient = qs_ot_env(1)%gradient - tmp
IF (qs_ot_env(1)%settings%do_rotation) THEN
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_gx, tmp)
qs_ot_env(1)%gradient = qs_ot_env(1)%gradient - 0.5_dp*tmp
END IF
END IF
IF (do_ener) THEN
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_gx, qs_ot_env(ispin)%ener_gx)
qs_ot_env(1)%gradient = qs_ot_env(1)%gradient - tmp
END IF
END IF
END DO
END IF
SELECT CASE (qs_ot_env(1)%settings%OT_METHOD)
CASE ("CG")
IF (current_point_is_fine(qs_ot_env)) THEN
qs_ot_env(1)%OT_METHOD_FULL = "OT CG"
CALL ot_new_cg_direction(qs_ot_env)
qs_ot_env(1)%line_search_count = 0
ELSE
qs_ot_env(1)%OT_METHOD_FULL = "OT LS"
END IF
CALL do_line_search(qs_ot_env)
CASE ("SD")
IF (current_point_is_fine(qs_ot_env)) THEN
qs_ot_env(1)%OT_METHOD_FULL = "OT SD"
CALL ot_new_sd_direction(qs_ot_env)
qs_ot_env(1)%line_search_count = 0
ELSE
qs_ot_env(1)%OT_METHOD_FULL = "OT LS"
END IF
CALL do_line_search(qs_ot_env)
CASE ("DIIS")
qs_ot_env(1)%OT_METHOD_FULL = "OT DIIS"
CALL ot_diis_step(qs_ot_env)
CASE ("BROY")
qs_ot_env(1)%OT_METHOD_FULL = "OT BROY"
CALL ot_broyden_step(qs_ot_env)
CASE DEFAULT
CPABORT("OT_METHOD NYI")
END SELECT
CALL timestop(handle)
END SUBROUTINE ot_mini
!
! checks if the current point is a good point for finding a new direction
! or if we should improve the line_search, if it is used
!
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
!> \return ...
! **************************************************************************************************
FUNCTION current_point_is_fine(qs_ot_env) RESULT(res)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
LOGICAL :: res
res = .FALSE.
! only if we have a gradient it can be fine
IF (.NOT. qs_ot_env(1)%energy_only) THEN
! we have not yet started with the line search
IF (qs_ot_env(1)%line_search_count .EQ. 0) THEN
res = .TRUE.
RETURN
END IF
IF (qs_ot_env(1)%line_search_might_be_done) THEN
! here we put the more complicated logic later
res = .TRUE.
RETURN
END IF
END IF
END FUNCTION current_point_is_fine
!
! performs various kinds of line searches
!
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
! **************************************************************************************************
SUBROUTINE do_line_search(qs_ot_env)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
SELECT CASE (qs_ot_env(1)%settings%line_search_method)
CASE ("GOLD")
CALL do_line_search_gold(qs_ot_env)
CASE ("3PNT")
CALL do_line_search_3pnt(qs_ot_env)
CASE ("2PNT")
CALL do_line_search_2pnt(qs_ot_env)
CASE ("NONE")
CALL do_line_search_none(qs_ot_env)
CASE DEFAULT
CPABORT("NYI")
END SELECT
END SUBROUTINE do_line_search
! **************************************************************************************************
!> \brief moves x adding the right amount (ds) of the gradient or search direction
!> \param ds ...
!> \param qs_ot_env ...
!> \par History
!> 08.2004 created [ Joost VandeVondele ] copied here from a larger number of subroutines
! **************************************************************************************************
SUBROUTINE take_step(ds, qs_ot_env)
REAL(KIND=dp) :: ds
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
CHARACTER(len=*), PARAMETER :: routineN = 'take_step'
INTEGER :: handle, ispin, nspin
LOGICAL :: do_ener, do_ks
CALL timeset(routineN, handle)
nspin = SIZE(qs_ot_env)
do_ks = qs_ot_env(1)%settings%ks
do_ener = qs_ot_env(1)%settings%do_ener
! now update x to take into account this new step
! either dx or -gx is the direction to use
IF (qs_ot_env(1)%use_dx) THEN
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_add(qs_ot_env(ispin)%matrix_x, qs_ot_env(ispin)%matrix_dx, &
alpha_scalar=1.0_dp, beta_scalar=ds)
IF (qs_ot_env(ispin)%settings%do_rotation) THEN
CALL dbcsr_add(qs_ot_env(ispin)%rot_mat_x, qs_ot_env(ispin)%rot_mat_dx, &
alpha_scalar=1.0_dp, beta_scalar=ds)
END IF
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
qs_ot_env(ispin)%ener_x = qs_ot_env(ispin)%ener_x + ds*qs_ot_env(ispin)%ener_dx
END DO
END IF
ELSE
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_add(qs_ot_env(ispin)%matrix_x, qs_ot_env(ispin)%matrix_gx, &
alpha_scalar=1.0_dp, beta_scalar=-ds)
IF (qs_ot_env(ispin)%settings%do_rotation) THEN
CALL dbcsr_add(qs_ot_env(ispin)%rot_mat_x, qs_ot_env(ispin)%rot_mat_gx, &
alpha_scalar=1.0_dp, beta_scalar=-ds)
END IF
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
qs_ot_env(ispin)%ener_x = qs_ot_env(ispin)%ener_x - ds*qs_ot_env(ispin)%ener_gx
END DO
END IF
END IF
CALL timestop(handle)
END SUBROUTINE take_step
! implements a golden ratio search as a robust way of minimizing
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
! **************************************************************************************************
SUBROUTINE do_line_search_gold(qs_ot_env)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
CHARACTER(len=*), PARAMETER :: routineN = 'do_line_search_gold'
REAL(KIND=dp), PARAMETER :: gold_sec = 0.3819_dp
INTEGER :: count, handle
REAL(KIND=dp) :: ds
CALL timeset(routineN, handle)
qs_ot_env(1)%line_search_count = qs_ot_env(1)%line_search_count + 1
count = qs_ot_env(1)%line_search_count
qs_ot_env(1)%line_search_might_be_done = .FALSE.
qs_ot_env(1)%energy_only = .TRUE.
IF (count + 1 .GT. SIZE(qs_ot_env(1)%OT_pos)) THEN
! should not happen, we pass with a warning first
! you can increase the size of OT_pos and the like in qs_ot_env
CPABORT("MAX ITER EXCEEDED : FATAL")
END IF
IF (qs_ot_env(1)%line_search_count .EQ. 1) THEN
qs_ot_env(1)%line_search_left = 1
qs_ot_env(1)%line_search_right = 0
qs_ot_env(1)%line_search_mid = 1
qs_ot_env(1)%ot_pos(1) = 0.0_dp
qs_ot_env(1)%ot_energy(1) = qs_ot_env(1)%etotal
qs_ot_env(1)%ot_pos(2) = qs_ot_env(1)%ds_min/gold_sec
ELSE
qs_ot_env(1)%ot_energy(count) = qs_ot_env(1)%etotal
! it's essentially a book keeping game.
! keep left on the left, keep (bring) right on the right
! and mid in between these two
IF (qs_ot_env(1)%line_search_right .EQ. 0) THEN ! we do not yet have the right bracket
IF (qs_ot_env(1)%ot_energy(count - 1) .LT. qs_ot_env(1)%ot_energy(count)) THEN
qs_ot_env(1)%line_search_right = count
qs_ot_env(1)%ot_pos(count + 1) = qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid) + &
(qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_right) - &
qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid))*gold_sec
ELSE
qs_ot_env(1)%line_search_left = qs_ot_env(1)%line_search_mid
qs_ot_env(1)%line_search_mid = count
qs_ot_env(1)%ot_pos(count + 1) = qs_ot_env(1)%ot_pos(count)/gold_sec ! expand
END IF
ELSE
! first determine where we are and construct the new triplet
IF (qs_ot_env(1)%ot_pos(count) .LT. qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid)) THEN
IF (qs_ot_env(1)%ot_energy(count) .LT. qs_ot_env(1)%ot_energy(qs_ot_env(1)%line_search_mid)) THEN
qs_ot_env(1)%line_search_right = qs_ot_env(1)%line_search_mid
qs_ot_env(1)%line_search_mid = count
ELSE
qs_ot_env(1)%line_search_left = count
END IF
ELSE
IF (qs_ot_env(1)%ot_energy(count) .LT. qs_ot_env(1)%ot_energy(qs_ot_env(1)%line_search_mid)) THEN
qs_ot_env(1)%line_search_left = qs_ot_env(1)%line_search_mid
qs_ot_env(1)%line_search_mid = count
ELSE
qs_ot_env(1)%line_search_right = count
END IF
END IF
! now find the new point in the largest section
IF ((qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_right) &
- qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid)) .GT. &
(qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid) &
- qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_left))) THEN
qs_ot_env(1)%ot_pos(count + 1) = &
qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid) + &
gold_sec*(qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_right) &
- qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid))
ELSE
qs_ot_env(1)%ot_pos(count + 1) = &
qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_left) + &
gold_sec*(qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid) &
- qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_left))
END IF
! check for termination
IF (((qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_right) &
- qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid)) .LT. &
qs_ot_env(1)%ds_min*qs_ot_env(1)%settings%gold_target) .AND. &
((qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_mid) &
- qs_ot_env(1)%ot_pos(qs_ot_env(1)%line_search_left)) .LT. &
qs_ot_env(1)%ds_min*qs_ot_env(1)%settings%gold_target)) THEN
qs_ot_env(1)%energy_only = .FALSE.
qs_ot_env(1)%line_search_might_be_done = .TRUE.
END IF
END IF
END IF
ds = qs_ot_env(1)%OT_pos(count + 1) - qs_ot_env(1)%OT_pos(count)
qs_ot_env(1)%ds_min = qs_ot_env(1)%OT_pos(count + 1)
CALL take_step(ds, qs_ot_env)
CALL timestop(handle)
END SUBROUTINE do_line_search_gold
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
! **************************************************************************************************
SUBROUTINE do_line_search_3pnt(qs_ot_env)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
CHARACTER(len=*), PARAMETER :: routineN = 'do_line_search_3pnt'
INTEGER :: count, handle
REAL(KIND=dp) :: denom, ds, fa, fb, fc, nom, pos, val, &
xa, xb, xc
CALL timeset(routineN, handle)
qs_ot_env(1)%line_search_might_be_done = .FALSE.
qs_ot_env(1)%energy_only = .TRUE.
! a three point interpolation based on the energy
qs_ot_env(1)%line_search_count = qs_ot_env(1)%line_search_count + 1
count = qs_ot_env(1)%line_search_count
qs_ot_env(1)%ot_energy(count) = qs_ot_env(1)%etotal
SELECT CASE (count)
CASE (1)
qs_ot_env(1)%ot_pos(count) = 0.0_dp
qs_ot_env(1)%ot_pos(count + 1) = qs_ot_env(1)%ds_min*0.8_dp
CASE (2)
IF (qs_ot_env(1)%OT_energy(count) .GT. qs_ot_env(1)%OT_energy(count - 1)) THEN
qs_ot_env(1)%OT_pos(count + 1) = qs_ot_env(1)%ds_min*0.5_dp
ELSE
qs_ot_env(1)%OT_pos(count + 1) = qs_ot_env(1)%ds_min*1.4_dp
END IF
CASE (3)
xa = qs_ot_env(1)%OT_pos(1)
xb = qs_ot_env(1)%OT_pos(2)
xc = qs_ot_env(1)%OT_pos(3)
fa = qs_ot_env(1)%OT_energy(1)
fb = qs_ot_env(1)%OT_energy(2)
fc = qs_ot_env(1)%OT_energy(3)
nom = (xb - xa)**2*(fb - fc) - (xb - xc)**2*(fb - fa)
denom = (xb - xa)*(fb - fc) - (xb - xc)*(fb - fa)
IF (ABS(denom) .LE. 1.0E-18_dp*MAX(ABS(fb - fc), ABS(fb - fa))) THEN
pos = xb
ELSE
pos = xb - 0.5_dp*nom/denom ! position of the stationary point
END IF
val = (pos - xa)*(pos - xb)*fc/((xc - xa)*(xc - xb)) + &
(pos - xb)*(pos - xc)*fa/((xa - xb)*(xa - xc)) + &
(pos - xc)*(pos - xa)*fb/((xb - xc)*(xb - xa))
IF (val .LT. fa .AND. val .LE. fb .AND. val .LE. fc) THEN ! OK, we go to a minimum
! we take a guard against too large steps
qs_ot_env(1)%OT_pos(count + 1) = MAX(MAXVAL(qs_ot_env(1)%OT_pos(1:3))*0.01_dp, &
MIN(pos, MAXVAL(qs_ot_env(1)%OT_pos(1:3))*4.0_dp))
ELSE ! just take an extended step
qs_ot_env(1)%OT_pos(count + 1) = MAXVAL(qs_ot_env(1)%OT_pos(1:3))*2.0_dp
END IF
qs_ot_env(1)%energy_only = .FALSE.
qs_ot_env(1)%line_search_might_be_done = .TRUE.
CASE DEFAULT
CPABORT("NYI")
END SELECT
ds = qs_ot_env(1)%OT_pos(count + 1) - qs_ot_env(1)%OT_pos(count)
qs_ot_env(1)%ds_min = qs_ot_env(1)%OT_pos(count + 1)
CALL take_step(ds, qs_ot_env)
CALL timestop(handle)
END SUBROUTINE do_line_search_3pnt
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
! **************************************************************************************************
SUBROUTINE do_line_search_2pnt(qs_ot_env)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
CHARACTER(len=*), PARAMETER :: routineN = 'do_line_search_2pnt'
INTEGER :: count, handle
REAL(KIND=dp) :: a, b, c, ds, pos, val, x0, x1
CALL timeset(routineN, handle)
qs_ot_env(1)%line_search_might_be_done = .FALSE.
qs_ot_env(1)%energy_only = .TRUE.
! a three point interpolation based on the energy
qs_ot_env(1)%line_search_count = qs_ot_env(1)%line_search_count + 1
count = qs_ot_env(1)%line_search_count
qs_ot_env(1)%ot_energy(count) = qs_ot_env(1)%etotal
SELECT CASE (count)
CASE (1)
qs_ot_env(1)%ot_pos(count) = 0.0_dp
qs_ot_env(1)%ot_grad(count) = qs_ot_env(1)%gradient
qs_ot_env(1)%ot_pos(count + 1) = qs_ot_env(1)%ds_min*1.0_dp
CASE (2)
x0 = 0.0_dp
c = qs_ot_env(1)%ot_energy(1)
b = qs_ot_env(1)%ot_grad(1)
x1 = qs_ot_env(1)%ot_pos(2)
a = (qs_ot_env(1)%ot_energy(2) - b*x1 - c)/(x1**2)
IF (a .LE. 0.0_dp) a = 1.0E-15_dp
pos = -b/(2.0_dp*a)
val = a*pos**2 + b*pos + c
qs_ot_env(1)%energy_only = .FALSE.
qs_ot_env(1)%line_search_might_be_done = .TRUE.
IF (val .LT. qs_ot_env(1)%ot_energy(1) .AND. val .LE. qs_ot_env(1)%ot_energy(2)) THEN
! we go to a minimum, but ...
! we take a guard against too large steps
qs_ot_env(1)%OT_pos(count + 1) = MAX(MAXVAL(qs_ot_env(1)%OT_pos(1:2))*0.01_dp, &
MIN(pos, MAXVAL(qs_ot_env(1)%OT_pos(1:2))*4.0_dp))
ELSE ! just take an extended step
qs_ot_env(1)%OT_pos(count + 1) = MAXVAL(qs_ot_env(1)%OT_pos(1:2))*2.0_dp
END IF
CASE DEFAULT
CPABORT("NYI")
END SELECT
ds = qs_ot_env(1)%OT_pos(count + 1) - qs_ot_env(1)%OT_pos(count)
qs_ot_env(1)%ds_min = qs_ot_env(1)%OT_pos(count + 1)
CALL take_step(ds, qs_ot_env)
CALL timestop(handle)
END SUBROUTINE do_line_search_2pnt
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
! **************************************************************************************************
SUBROUTINE do_line_search_none(qs_ot_env)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
CALL take_step(qs_ot_env(1)%ds_min, qs_ot_env)
END SUBROUTINE do_line_search_none
!
! creates a new SD direction, using the preconditioner if associated
! also updates the gradient for line search
!
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
! **************************************************************************************************
SUBROUTINE ot_new_sd_direction(qs_ot_env)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
CHARACTER(len=*), PARAMETER :: routineN = 'ot_new_sd_direction'
INTEGER :: handle, ispin, itmp, k, n, nener, nspin
LOGICAL :: do_ener, do_ks
REAL(KIND=dp) :: tmp
TYPE(cp_logger_type), POINTER :: logger
CALL timeset(routineN, handle)
!***SCP
nspin = SIZE(qs_ot_env)
logger => cp_get_default_logger()
do_ks = qs_ot_env(1)%settings%ks
do_ener = qs_ot_env(1)%settings%do_ener
IF (ASSOCIATED(qs_ot_env(1)%preconditioner)) THEN
IF (.NOT. qs_ot_env(1)%use_dx) CPABORT("use dx")
qs_ot_env(1)%gnorm = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL apply_preconditioner(qs_ot_env(ispin)%preconditioner, &
qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_dx)
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_dx, tmp)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
END DO
IF (qs_ot_env(1)%gnorm .LT. 0.0_dp) THEN
logger => cp_get_default_logger()
WRITE (cp_logger_get_default_unit_nr(logger), *) "WARNING Preconditioner not positive definite !"
END IF
DO ispin = 1, nspin
CALL dbcsr_scale(qs_ot_env(ispin)%matrix_dx, -1.0_dp)
END DO
IF (qs_ot_env(1)%settings%do_rotation) THEN
DO ispin = 1, nspin
! right now no preconditioner yet
CALL dbcsr_copy(qs_ot_env(ispin)%rot_mat_dx, qs_ot_env(ispin)%rot_mat_gx)
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_dx, tmp)
! added 0.5, because we have (antisymmetry) only half the number of variables
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + 0.5_dp*tmp
END DO
DO ispin = 1, nspin
CALL dbcsr_scale(qs_ot_env(ispin)%rot_mat_dx, -1.0_dp)
END DO
END IF
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
qs_ot_env(ispin)%ener_dx = qs_ot_env(ispin)%ener_gx
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_dx, qs_ot_env(ispin)%ener_gx)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
qs_ot_env(ispin)%ener_dx = -qs_ot_env(ispin)%ener_dx
END DO
END IF
ELSE
qs_ot_env(1)%gnorm = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_gx, tmp)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
END DO
IF (qs_ot_env(1)%settings%do_rotation) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_gx, tmp)
! added 0.5, because we have (antisymmetry) only half the number of variables
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + 0.5_dp*tmp
END DO
END IF
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_gx, qs_ot_env(ispin)%ener_gx)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
END DO
END IF
END IF
k = 0
n = 0
nener = 0
IF (do_ks) THEN
CALL dbcsr_get_info(qs_ot_env(1)%matrix_x, nfullrows_total=n)
DO ispin = 1, nspin
CALL dbcsr_get_info(qs_ot_env(ispin)%matrix_x, nfullcols_total=itmp)
k = k + itmp
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
nener = nener + SIZE(qs_ot_env(ispin)%ener_x)
END DO
END IF
! Handling the case of no free variables to optimize
IF (INT(n, KIND=int_8)*INT(k, KIND=int_8) + nener /= 0) THEN
qs_ot_env(1)%delta = SQRT(ABS(qs_ot_env(1)%gnorm)/(INT(n, KIND=int_8)*INT(k, KIND=int_8) + nener))
qs_ot_env(1)%gradient = -qs_ot_env(1)%gnorm
ELSE
qs_ot_env(1)%delta = 0.0_dp
qs_ot_env(1)%gradient = 0.0_dp
END IF
CALL timestop(handle)
END SUBROUTINE ot_new_sd_direction
!
! creates a new CG direction. Implements Polak-Ribierre variant
! using the preconditioner if associated
! also updates the gradient for line search
!
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
! **************************************************************************************************
SUBROUTINE ot_new_cg_direction(qs_ot_env)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
CHARACTER(len=*), PARAMETER :: routineN = 'ot_new_cg_direction'
INTEGER :: handle, ispin, itmp, k, n, nener, nspin
LOGICAL :: do_ener, do_ks
REAL(KIND=dp) :: beta_pr, gnorm_cross, test_down, tmp
TYPE(cp_logger_type), POINTER :: logger
CALL timeset(routineN, handle)
nspin = SIZE(qs_ot_env)
logger => cp_get_default_logger()
do_ks = qs_ot_env(1)%settings%ks
do_ener = qs_ot_env(1)%settings%do_ener
gnorm_cross = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_gx_old, tmp)
gnorm_cross = gnorm_cross + tmp
END DO
IF (qs_ot_env(1)%settings%do_rotation) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_gx_old, tmp)
! added 0.5, because we have (antisymmetry) only half the number of variables
gnorm_cross = gnorm_cross + 0.5_dp*tmp
END DO
END IF
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_gx, qs_ot_env(ispin)%ener_gx_old)
gnorm_cross = gnorm_cross + tmp
END DO
END IF
IF (ASSOCIATED(qs_ot_env(1)%preconditioner)) THEN
DO ispin = 1, nspin
CALL apply_preconditioner(qs_ot_env(ispin)%preconditioner, &
qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_gx_old)
END DO
qs_ot_env(1)%gnorm = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_gx_old, tmp)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
END DO
IF (qs_ot_env(1)%gnorm .LT. 0.0_dp) THEN
WRITE (cp_logger_get_default_unit_nr(logger), *) "WARNING Preconditioner not positive definite !"
END IF
DO ispin = 1, nspin
CALL dbcsr_copy(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_gx_old)
END DO
IF (qs_ot_env(1)%settings%do_rotation) THEN
DO ispin = 1, nspin
! right now no preconditioner yet
CALL dbcsr_copy(qs_ot_env(ispin)%rot_mat_gx_old, qs_ot_env(ispin)%rot_mat_gx)
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_gx_old, tmp)
! added 0.5, because we have (antisymmetry) only half the number of variables
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + 0.5_dp*tmp
END DO
DO ispin = 1, nspin
CALL dbcsr_copy(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_gx_old)
END DO
END IF
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
qs_ot_env(ispin)%ener_gx_old = qs_ot_env(ispin)%ener_gx
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_gx, qs_ot_env(ispin)%ener_gx_old)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
qs_ot_env(ispin)%ener_gx = qs_ot_env(ispin)%ener_gx_old
END DO
END IF
ELSE
IF (do_ks) THEN
qs_ot_env(1)%gnorm = 0.0_dp
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_gx, tmp)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
CALL dbcsr_copy(qs_ot_env(ispin)%matrix_gx_old, qs_ot_env(ispin)%matrix_gx)
END DO
IF (qs_ot_env(1)%settings%do_rotation) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_gx, tmp)
! added 0.5, because we have (antisymmetry) only half the number of variables
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + 0.5_dp*tmp
CALL dbcsr_copy(qs_ot_env(ispin)%rot_mat_gx_old, qs_ot_env(ispin)%rot_mat_gx)
END DO
END IF
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_gx, qs_ot_env(ispin)%ener_gx)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
qs_ot_env(ispin)%ener_gx_old = qs_ot_env(ispin)%ener_gx
END DO
END IF
END IF
k = 0
n = 0
nener = 0
IF (do_ks) THEN
CALL dbcsr_get_info(qs_ot_env(1)%matrix_x, nfullrows_total=n)
DO ispin = 1, nspin
CALL dbcsr_get_info(qs_ot_env(ispin)%matrix_x, nfullcols_total=itmp)
k = k + itmp
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
nener = nener + SIZE(qs_ot_env(ispin)%ener_x)
END DO
END IF
! Handling the case of no free variables to optimize
IF (INT(n, KIND=int_8)*INT(k, KIND=int_8) + nener /= 0) THEN
qs_ot_env(1)%delta = SQRT(ABS(qs_ot_env(1)%gnorm)/(INT(n, KIND=int_8)*INT(k, KIND=int_8) + nener))
beta_pr = (qs_ot_env(1)%gnorm - gnorm_cross)/qs_ot_env(1)%gnorm_old
ELSE
qs_ot_env(1)%delta = 0.0_dp
beta_pr = 0.0_dp
END IF
beta_pr = MAX(beta_pr, 0.0_dp) ! reset to SD
test_down = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_add(qs_ot_env(ispin)%matrix_dx, qs_ot_env(ispin)%matrix_gx, &
alpha_scalar=beta_pr, beta_scalar=-1.0_dp)
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_dx, tmp)
test_down = test_down + tmp
IF (qs_ot_env(1)%settings%do_rotation) THEN
CALL dbcsr_add(qs_ot_env(ispin)%rot_mat_dx, qs_ot_env(ispin)%rot_mat_gx, &
alpha_scalar=beta_pr, beta_scalar=-1.0_dp)
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_dx, tmp)
test_down = test_down + 0.5_dp*tmp
END IF
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
qs_ot_env(ispin)%ener_dx = beta_pr*qs_ot_env(ispin)%ener_dx - qs_ot_env(ispin)%ener_gx
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_gx, qs_ot_env(ispin)%ener_dx)
test_down = test_down + tmp
END DO
END IF
IF (test_down .GE. 0.0_dp) THEN ! reset to SD
beta_pr = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_add(qs_ot_env(ispin)%matrix_dx, qs_ot_env(ispin)%matrix_gx, &
alpha_scalar=beta_pr, beta_scalar=-1.0_dp)
IF (qs_ot_env(1)%settings%do_rotation) THEN
CALL dbcsr_add(qs_ot_env(ispin)%rot_mat_dx, &
qs_ot_env(ispin)%rot_mat_gx, alpha_scalar=beta_pr, beta_scalar=-1.0_dp)
END IF
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
qs_ot_env(ispin)%ener_dx = beta_pr*qs_ot_env(ispin)%ener_dx - qs_ot_env(ispin)%ener_gx
END DO
END IF
END IF
! since we change the direction we have to adjust the gradient
qs_ot_env(1)%gradient = beta_pr*qs_ot_env(1)%gradient - qs_ot_env(1)%gnorm
qs_ot_env(1)%gnorm_old = qs_ot_env(1)%gnorm
CALL timestop(handle)
END SUBROUTINE ot_new_cg_direction
! **************************************************************************************************
!> \brief ...
!> \param qs_ot_env ...
! **************************************************************************************************
SUBROUTINE ot_diis_step(qs_ot_env)
TYPE(qs_ot_type), DIMENSION(:), POINTER :: qs_ot_env
CHARACTER(len=*), PARAMETER :: routineN = 'ot_diis_step'
INTEGER :: diis_bound, diis_m, handle, i, info, &
ispin, itmp, j, k, n, nener, nspin
LOGICAL :: do_ener, do_ks, do_ot_sd
REAL(KIND=dp) :: overlap, tmp, tr_xnew_gx, tr_xold_gx
TYPE(cp_logger_type), POINTER :: logger
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
do_ks = qs_ot_env(1)%settings%ks
do_ener = qs_ot_env(1)%settings%do_ener
nspin = SIZE(qs_ot_env)
diis_m = qs_ot_env(1)%settings%diis_m
IF (qs_ot_env(1)%diis_iter .LT. diis_m) THEN
diis_bound = qs_ot_env(1)%diis_iter + 1
ELSE
diis_bound = diis_m
END IF
j = MOD(qs_ot_env(1)%diis_iter, diis_m) + 1 ! index in the circular array
! copy the position and the error vector in the diis buffers
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_copy(qs_ot_env(ispin)%matrix_h_x(j)%matrix, qs_ot_env(ispin)%matrix_x)
IF (qs_ot_env(ispin)%settings%do_rotation) THEN
CALL dbcsr_copy(qs_ot_env(ispin)%rot_mat_h_x(j)%matrix, qs_ot_env(ispin)%rot_mat_x)
END IF
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
qs_ot_env(ispin)%ener_h_x(j, :) = qs_ot_env(ispin)%ener_x(:)
END DO
END IF
IF (ASSOCIATED(qs_ot_env(1)%preconditioner)) THEN
qs_ot_env(1)%gnorm = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL apply_preconditioner(qs_ot_env(ispin)%preconditioner, &
qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_h_e(j)%matrix)
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_h_e(j)%matrix, &
tmp)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
END DO
IF (qs_ot_env(1)%gnorm .LT. 0.0_dp) THEN
WRITE (cp_logger_get_default_unit_nr(logger), *) "WARNING Preconditioner not positive definite !"
END IF
DO ispin = 1, nspin
CALL dbcsr_scale(qs_ot_env(ispin)%matrix_h_e(j)%matrix, -qs_ot_env(1)%ds_min)
END DO
IF (qs_ot_env(1)%settings%do_rotation) THEN
DO ispin = 1, nspin
CALL dbcsr_copy(qs_ot_env(ispin)%rot_mat_h_e(j)%matrix, qs_ot_env(ispin)%rot_mat_gx)
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_h_e(j)%matrix, &
tmp)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + 0.5_dp*tmp
END DO
DO ispin = 1, nspin
CALL dbcsr_scale(qs_ot_env(ispin)%rot_mat_h_e(j)%matrix, -qs_ot_env(1)%ds_min)
END DO
END IF
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
qs_ot_env(ispin)%ener_h_e(j, :) = qs_ot_env(ispin)%ener_gx(:)
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_h_e(j, :), qs_ot_env(ispin)%ener_gx(:))
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
qs_ot_env(ispin)%ener_h_e(j, :) = -qs_ot_env(1)%ds_min*qs_ot_env(ispin)%ener_h_e(j, :)
END DO
END IF
ELSE
qs_ot_env(1)%gnorm = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, qs_ot_env(ispin)%matrix_gx, tmp)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
CALL dbcsr_add(qs_ot_env(ispin)%matrix_h_e(j)%matrix, &
qs_ot_env(ispin)%matrix_gx, alpha_scalar=0.0_dp, beta_scalar=-qs_ot_env(1)%ds_min)
END DO
IF (qs_ot_env(1)%settings%do_rotation) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_gx, qs_ot_env(ispin)%rot_mat_gx, tmp)
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + 0.5_dp*tmp
CALL dbcsr_add(qs_ot_env(ispin)%rot_mat_h_e(j)%matrix, &
qs_ot_env(ispin)%rot_mat_gx, alpha_scalar=0.0_dp, beta_scalar=-qs_ot_env(1)%ds_min)
END DO
END IF
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_gx(:), qs_ot_env(ispin)%ener_gx(:))
qs_ot_env(1)%gnorm = qs_ot_env(1)%gnorm + tmp
qs_ot_env(ispin)%ener_h_e(j, :) = -qs_ot_env(1)%ds_min*qs_ot_env(ispin)%ener_gx(:)
END DO
END IF
END IF
k = 0
n = 0
nener = 0
IF (do_ks) THEN
CALL dbcsr_get_info(qs_ot_env(1)%matrix_x, nfullrows_total=n)
DO ispin = 1, nspin
CALL dbcsr_get_info(qs_ot_env(ispin)%matrix_x, nfullcols_total=itmp)
k = k + itmp
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
nener = nener + SIZE(qs_ot_env(ispin)%ener_x)
END DO
END IF
! Handling the case of no free variables to optimize
IF (INT(n, KIND=int_8)*INT(k, KIND=int_8) + nener /= 0) THEN
qs_ot_env(1)%delta = SQRT(ABS(qs_ot_env(1)%gnorm)/(INT(n, KIND=int_8)*INT(k, KIND=int_8) + nener))
qs_ot_env(1)%gradient = -qs_ot_env(1)%gnorm
ELSE
qs_ot_env(1)%delta = 0.0_dp
qs_ot_env(1)%gradient = 0.0_dp
END IF
! make the diis matrix and solve it
DO i = 1, diis_bound
! I think there are two possible options, with and without preconditioner
! as a metric
! the second option seems most logical to me, and it seems marginally faster
! in some of the tests
IF (.FALSE.) THEN
qs_ot_env(1)%ls_diis(i, j) = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_h_e(j)%matrix, &
qs_ot_env(ispin)%matrix_h_e(i)%matrix, &
tmp)
qs_ot_env(1)%ls_diis(i, j) = qs_ot_env(1)%ls_diis(i, j) + tmp
IF (qs_ot_env(ispin)%settings%do_rotation) THEN
CALL dbcsr_dot(qs_ot_env(ispin)%rot_mat_h_e(j)%matrix, &
qs_ot_env(ispin)%rot_mat_h_e(i)%matrix, &
tmp)
qs_ot_env(1)%ls_diis(i, j) = qs_ot_env(1)%ls_diis(i, j) + 0.5_dp*tmp
END IF
END DO
END IF
IF (do_ener) THEN
DO ispin = 1, nspin
tmp = DOT_PRODUCT(qs_ot_env(ispin)%ener_h_e(j, :), qs_ot_env(ispin)%ener_h_e(i, :))
qs_ot_env(1)%ls_diis(i, j) = qs_ot_env(1)%ls_diis(i, j) + tmp
END DO
END IF
ELSE
qs_ot_env(1)%ls_diis(i, j) = 0.0_dp
IF (do_ks) THEN
DO ispin = 1, nspin
CALL dbcsr_dot(qs_ot_env(ispin)%matrix_gx, &
qs_ot_env(ispin)%matrix_h_e(i)%matrix, &
tmp)
qs_ot_env(1)%ls_diis(i, j) = qs_ot_env(1)%ls_diis(i, j) - qs_ot_env(1)%ds_min*tmp
IF (qs_ot_env(ispin)%settings%do_rotation) THEN