-
Notifications
You must be signed in to change notification settings - Fork 1
/
mp2_optimize_ri_basis.F
1781 lines (1570 loc) · 74.5 KB
/
mp2_optimize_ri_basis.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 Routines to optimize the RI-MP2 basis. Only exponents of
!> non-contracted auxiliary basis basis are optimized. The derivative
!> of the MP2 energy with respect to the exponents of the basis
!> are calculated numerically.
!> \par History
!> 08.2013 created [Mauro Del Ben]
!> \author Mauro Del Ben
! **************************************************************************************************
MODULE mp2_optimize_ri_basis
USE atomic_kind_types, ONLY: atomic_kind_type
USE basis_set_container_types, ONLY: add_basis_set_to_container,&
remove_basis_from_container
USE basis_set_types, ONLY: allocate_gto_basis_set,&
gto_basis_set_type
USE cp_log_handling, ONLY: cp_add_default_logger,&
cp_get_default_logger,&
cp_logger_create,&
cp_logger_get_default_unit_nr,&
cp_logger_release,&
cp_logger_set,&
cp_logger_type,&
cp_rm_default_logger
USE hfx_types, ONLY: hfx_basis_info_type,&
hfx_basis_type
USE kinds, ONLY: dp
USE libint_wrapper, ONLY: build_eri_size
USE machine, ONLY: default_output_unit,&
m_flush
USE memory_utilities, ONLY: reallocate
USE message_passing, ONLY: mp_para_env_release,&
mp_para_env_type
USE mp2_direct_method, ONLY: mp2_canonical_direct_single_batch
USE mp2_ri_libint, ONLY: libint_ri_mp2,&
read_RI_basis_set,&
release_RI_basis_set
USE mp2_types, ONLY: mp2_biel_type,&
mp2_type
USE orbital_pointers, ONLY: indco,&
init_orbital_pointers,&
nco,&
ncoset,&
nso
USE orbital_symbols, ONLY: cgf_symbol,&
sgf_symbol
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_kind_types, ONLY: get_qs_kind,&
qs_kind_type
USE util, ONLY: sort
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mp2_optimize_ri_basis'
PUBLIC :: optimize_ri_basis_main
CONTAINS
! **************************************************************************************************
!> \brief optimize RI-MP2 basis set
!> \param Emp2 ...
!> \param Emp2_Cou ...
!> \param Emp2_ex ...
!> \param Emp2_S ...
!> \param Emp2_T ...
!> \param dimen ...
!> \param natom ...
!> \param homo ...
!> \param mp2_biel ...
!> \param mp2_env ...
!> \param C ...
!> \param Auto ...
!> \param kind_of ...
!> \param qs_env ...
!> \param para_env ...
!> \param unit_nr ...
!> \param homo_beta ...
!> \param C_beta ...
!> \param Auto_beta ...
!> \author Mauro Del Ben
! **************************************************************************************************
SUBROUTINE optimize_ri_basis_main(Emp2, Emp2_Cou, Emp2_ex, Emp2_S, Emp2_T, dimen, natom, homo, &
mp2_biel, mp2_env, C, Auto, kind_of, &
qs_env, para_env, &
unit_nr, homo_beta, C_beta, Auto_beta)
REAL(KIND=dp), INTENT(OUT) :: Emp2, Emp2_Cou, Emp2_ex, Emp2_S, Emp2_T
INTEGER, INTENT(IN) :: dimen, natom, homo
TYPE(mp2_biel_type), INTENT(IN) :: mp2_biel
TYPE(mp2_type), INTENT(INOUT) :: mp2_env
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: C
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: Auto
INTEGER, DIMENSION(:), INTENT(IN) :: kind_of
TYPE(qs_environment_type), INTENT(IN), POINTER :: qs_env
TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
INTEGER, INTENT(IN) :: unit_nr
INTEGER, INTENT(IN), OPTIONAL :: homo_beta
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
OPTIONAL :: C_beta
REAL(KIND=dp), DIMENSION(:), INTENT(IN), OPTIONAL :: Auto_beta
CHARACTER(len=*), PARAMETER :: routineN = 'optimize_ri_basis_main'
INTEGER :: color_sub, dimen_RI, elements_ij_proc, handle, i, iiter, ikind, ipgf, iset, &
ishell, j, local_unit_nr, max_l_am, max_num_iter, ndof, nkind, number_groups, virtual, &
virtual_beta
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: ij_list_proc, index_table_RI
LOGICAL :: hess_up, open_shell_case, reset_boundary
LOGICAL, ALLOCATABLE, DIMENSION(:) :: basis_was_assoc
REAL(KIND=dp) :: DI, DI_new, DRI, DRI_new, Emp2_AA, Emp2_AA_Cou, Emp2_AA_ex, Emp2_AB, &
Emp2_AB_Cou, Emp2_AB_ex, Emp2_BB, Emp2_BB_Cou, Emp2_BB_ex, Emp2_RI, Emp2_RI_new, &
eps_DI_rel, eps_DRI, eps_step, expon, fac, fad, fae, reset_edge, sumdg, sumxi
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: deriv, dg, g, hdg, lower_B, max_dev, &
max_rel_dev, p, pnew, xi
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: exp_limits, hessin
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: Integ_MP2, Integ_MP2_AA, Integ_MP2_AB, &
Integ_MP2_BB
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cp_logger_type), POINTER :: logger, logger_sub
TYPE(gto_basis_set_type), POINTER :: ri_aux_basis
TYPE(hfx_basis_info_type) :: RI_basis_info
TYPE(hfx_basis_type), DIMENSION(:), POINTER :: basis_S0, RI_basis_parameter
TYPE(mp_para_env_type), POINTER :: para_env_sub
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
open_shell_case = .FALSE.
IF (PRESENT(homo_beta) .AND. PRESENT(C_beta) .AND. PRESENT(Auto_beta)) open_shell_case = .TRUE.
virtual = dimen - homo
eps_DRI = mp2_env%ri_opt_param%DRI
eps_DI_rel = mp2_env%ri_opt_param%DI_rel
eps_step = mp2_env%ri_opt_param%eps_step
max_num_iter = mp2_env%ri_opt_param%max_num_iter
! calculate the ERI's over molecular integrals
Emp2 = 0.0_dp
Emp2_Cou = 0.0_dp
Emp2_ex = 0.0_dp
Emp2_S = 0.0_dp
Emp2_T = 0.0_dp
IF (open_shell_case) THEN
! open shell case
virtual_beta = dimen - homo_beta
! alpha-aplha case
Emp2_AA = 0.0_dp
Emp2_AA_Cou = 0.0_dp
Emp2_AA_ex = 0.0_dp
CALL calc_elem_ij_proc(homo, homo, para_env, elements_ij_proc, ij_list_proc)
CALL mp2_canonical_direct_single_batch(Emp2_AA, Emp2_AA_Cou, Emp2_AA_ex, mp2_env, qs_env, para_env, &
mp2_biel, dimen, C, Auto, 0, homo, homo, &
elements_ij_proc, ij_list_proc, homo, 0, &
Integ_MP2=Integ_MP2_AA)
CALL para_env%sum(Emp2_AA_Cou)
CALL para_env%sum(Emp2_AA_Ex)
CALL para_env%sum(Emp2_AA)
DEALLOCATE (ij_list_proc)
! beta-beta case
Emp2_BB = 0.0_dp
Emp2_BB_Cou = 0.0_dp
Emp2_BB_ex = 0.0_dp
CALL calc_elem_ij_proc(homo_beta, homo_beta, para_env, elements_ij_proc, ij_list_proc)
CALL mp2_canonical_direct_single_batch(Emp2_BB, Emp2_BB_Cou, Emp2_BB_ex, mp2_env, qs_env, para_env, &
mp2_biel, dimen, C_beta, Auto_beta, 0, homo_beta, homo_beta, &
elements_ij_proc, ij_list_proc, homo_beta, 0, &
Integ_MP2=Integ_MP2_BB)
CALL para_env%sum(Emp2_BB_Cou)
CALL para_env%sum(Emp2_BB_Ex)
CALL para_env%sum(Emp2_BB)
DEALLOCATE (ij_list_proc)
! aplha-beta case
Emp2_AB = 0.0_dp
Emp2_AB_Cou = 0.0_dp
Emp2_AB_ex = 0.0_dp
CALL calc_elem_ij_proc(homo, homo_beta, para_env, elements_ij_proc, ij_list_proc)
CALL mp2_canonical_direct_single_batch(Emp2_AB, Emp2_AB_Cou, Emp2_AB_ex, mp2_env, qs_env, para_env, &
mp2_biel, dimen, C, Auto, 0, homo, homo, &
elements_ij_proc, ij_list_proc, homo_beta, 0, &
homo_beta, C_beta, Auto_beta, Integ_MP2=Integ_MP2_AB)
CALL para_env%sum(Emp2_AB_Cou)
CALL para_env%sum(Emp2_AB_Ex)
CALL para_env%sum(Emp2_AB)
DEALLOCATE (ij_list_proc)
Emp2 = Emp2_AA + Emp2_BB + Emp2_AB*2.0_dp !+Emp2_BA
Emp2_Cou = Emp2_AA_Cou + Emp2_BB_Cou + Emp2_AB_Cou*2.0_dp !+Emp2_BA
Emp2_ex = Emp2_AA_ex + Emp2_BB_ex + Emp2_AB_ex*2.0_dp !+Emp2_BA
Emp2_S = Emp2_AB*2.0_dp
Emp2_T = Emp2_AA + Emp2_BB
! Replicate the MO-ERI's over all processes
CALL para_env%sum(Integ_MP2_AA)
CALL para_env%sum(Integ_MP2_BB)
CALL para_env%sum(Integ_MP2_AB)
ELSE
! close shell case
CALL calc_elem_ij_proc(homo, homo, para_env, elements_ij_proc, ij_list_proc)
CALL mp2_canonical_direct_single_batch(Emp2, Emp2_Cou, Emp2_ex, mp2_env, qs_env, para_env, &
mp2_biel, dimen, C, Auto, 0, homo, homo, &
elements_ij_proc, ij_list_proc, homo, 0, &
Integ_MP2=Integ_MP2)
CALL para_env%sum(Emp2_Cou)
CALL para_env%sum(Emp2_Ex)
CALL para_env%sum(Emp2)
DEALLOCATE (ij_list_proc)
! Replicate the MO-ERI's over all processes
CALL para_env%sum(Integ_MP2)
END IF
! create the para_env_sub
number_groups = para_env%num_pe/mp2_env%mp2_num_proc
color_sub = para_env%mepos/mp2_env%mp2_num_proc
ALLOCATE (para_env_sub)
CALL para_env_sub%from_split(para_env, color_sub)
IF (para_env%is_source()) THEN
local_unit_nr = cp_logger_get_default_unit_nr(logger, local=.FALSE.)
ELSE
local_unit_nr = default_output_unit
END IF
NULLIFY (logger_sub)
CALL cp_logger_create(logger_sub, para_env=para_env_sub, &
default_global_unit_nr=local_unit_nr, close_global_unit_on_dealloc=.FALSE.)
CALL cp_logger_set(logger_sub, local_filename="opt_RI_basis_localLog")
CALL cp_add_default_logger(logger_sub)
CALL generate_RI_init_basis(qs_env, mp2_env, nkind, max_rel_dev, basis_was_assoc)
CALL read_RI_basis_set(qs_env, RI_basis_parameter, RI_basis_info, &
natom, nkind, kind_of, index_table_RI, dimen_RI, &
basis_S0)
ndof = 0
max_l_am = 0
DO ikind = 1, nkind
DO iset = 1, RI_basis_parameter(ikind)%nset
ndof = ndof + 1
max_l_am = MAX(max_l_am, MAXVAL(RI_basis_parameter(ikind)%lmax))
END DO
END DO
ALLOCATE (exp_limits(2, nkind))
exp_limits(1, :) = HUGE(0)
exp_limits(2, :) = -HUGE(0)
DO ikind = 1, nkind
DO iset = 1, RI_basis_parameter(ikind)%nset
expon = RI_basis_parameter(ikind)%zet(1, iset)
IF (expon <= exp_limits(1, ikind)) exp_limits(1, ikind) = expon
IF (expon >= exp_limits(2, ikind)) exp_limits(2, ikind) = expon
END DO
IF (basis_was_assoc(ikind)) THEN
exp_limits(1, ikind) = exp_limits(1, ikind)*0.5_dp
exp_limits(2, ikind) = exp_limits(2, ikind)*1.5_dp
ELSE
exp_limits(1, ikind) = exp_limits(1, ikind)*0.8_dp*0.5_dp
exp_limits(2, ikind) = exp_limits(2, ikind)*1.2_dp*1.5_dp
END IF
END DO
DEALLOCATE (basis_was_assoc)
! check if the max angular momentum exceed the libint one
IF (max_l_am > build_eri_size) THEN
CPABORT("The angular momentum needed exceeds the value assumed when configuring libint.")
END IF
! Allocate stuff
ALLOCATE (p(ndof))
p = 0.0_dp
ALLOCATE (xi(ndof))
xi = 0.0_dp
ALLOCATE (g(ndof))
g = 0.0_dp
ALLOCATE (dg(ndof))
dg = 0.0_dp
ALLOCATE (hdg(ndof))
hdg = 0.0_dp
ALLOCATE (pnew(ndof))
pnew = 0.0_dp
ALLOCATE (deriv(ndof))
deriv = 0.0_dp
ALLOCATE (hessin(ndof, ndof))
hessin = 0.0_dp
DO i = 1, ndof
hessin(i, i) = 1.0_dp
END DO
! initialize transformation function
ALLOCATE (lower_B(ndof))
lower_B = 0.0_dp
ALLOCATE (max_dev(ndof))
max_dev = 0.0_dp
! Initialize the transformation function
CALL init_transf(nkind, RI_basis_parameter, lower_B, max_dev, max_rel_dev)
! get the atomic kind set for writing the basis
CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set)
! Calculate RI-MO-ERI's
CALL calc_energy_func(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, Emp2_RI, DRI, DI, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, &
qs_env, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, para_env, unit_nr, &
.TRUE.)
! ! Calculate function (DI) derivatives with respect to the RI basis exponent
CALL calc_energy_func_der(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, DI, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, eps_step, &
qs_env, nkind, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, &
para_env, para_env_sub, number_groups, color_sub, unit_nr, &
p, lower_B, max_dev, &
deriv)
g(:) = deriv
xi(:) = -g
reset_edge = 1.5_dp
DO iiter = 1, max_num_iter
IF (unit_nr > 0) WRITE (unit_nr, '(T3,A,I5)') 'OPTIMIZATION STEP NUMBER', iiter
! perform step
pnew(:) = p + xi
CALL p2basis(nkind, RI_basis_parameter, lower_B, max_dev, pnew)
! check if we have to reset boundaries
reset_boundary = .FALSE.
i = 0
DO ikind = 1, nkind
DO iset = 1, RI_basis_parameter(ikind)%nset
i = i + 1
expon = transf_val(lower_B(i), max_dev(i), pnew(i))
IF (ABS(pnew(i)) > reset_edge .OR. expon < exp_limits(1, ikind) .OR. expon > exp_limits(2, ikind)) THEN
reset_boundary = .TRUE.
EXIT
END IF
END DO
END DO
! IF(nreset>1) reset_boundary=.TRUE.
IF (reset_boundary) THEN
IF (unit_nr > 0) WRITE (unit_nr, '(T3,A)') 'RESET BASIS: one of the exponent hits the boundary'
CALL reset_basis(nkind, ndof, RI_basis_parameter, reset_edge, &
pnew, lower_B, max_dev, max_rel_dev, exp_limits)
p(:) = pnew
xi = 0.0_dp
g = 0.0_dp
dg = 0.0_dp
hdg = 0.0_dp
pnew = 0.0_dp
hessin = 0.0_dp
DO i = 1, ndof
hessin(i, i) = 1.0_dp
END DO
deriv = 0.0_dp
! Calculate RI-MO-ERI's
CALL calc_energy_func(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, Emp2_RI, DRI, DI, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, &
qs_env, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, para_env, unit_nr, &
.FALSE.)
! ! Calculate function (DI) derivatives with respect to the RI basis exponent
CALL calc_energy_func_der(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, DI, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, eps_step, &
qs_env, nkind, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, &
para_env, para_env_sub, number_groups, color_sub, unit_nr, &
p, lower_B, max_dev, &
deriv)
g(:) = deriv
xi(:) = -g
pnew(:) = p + xi
CALL p2basis(nkind, RI_basis_parameter, lower_B, max_dev, pnew)
END IF
! calculate energy at the new point
CALL calc_energy_func(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, Emp2_RI_new, DRI_new, DI_new, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, &
qs_env, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, para_env, unit_nr, &
.FALSE.)
! update energy and direction
DI = DI_new
xi(:) = pnew - p
p(:) = pnew
! check for convergence
IF (unit_nr > 0) THEN
WRITE (unit_nr, *)
DO ikind = 1, nkind
CALL get_qs_kind(qs_kind=qs_kind_set(ikind), basis_set=ri_aux_basis, &
basis_type="RI_AUX")
WRITE (unit_nr, '(T3,A,A)') atomic_kind_set(ikind)%element_symbol, ' RI_opt_basis'
WRITE (unit_nr, '(T3,I3)') RI_basis_parameter(ikind)%nset
DO iset = 1, RI_basis_parameter(ikind)%nset
WRITE (unit_nr, '(T3,10I4)') iset, &
RI_basis_parameter(ikind)%lmin(iset), &
RI_basis_parameter(ikind)%lmax(iset), &
RI_basis_parameter(ikind)%npgf(iset), &
(1, ishell=1, RI_basis_parameter(ikind)%nshell(iset))
DO ipgf = 1, RI_basis_parameter(ikind)%npgf(iset)
WRITE (unit_nr, '(T3,10F16.10)') RI_basis_parameter(ikind)%zet(ipgf, iset), &
(ri_aux_basis%gcc(ipgf, ishell, iset), &
ishell=1, ri_aux_basis%nshell(iset))
END DO
END DO
WRITE (unit_nr, *)
END DO
WRITE (unit_nr, *)
CALL m_flush(unit_nr)
END IF
IF (DI/ABS(Emp2) <= eps_DI_rel .AND. ABS(DRI_new) <= eps_DRI) THEN
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(T3,A,/)') 'OPTIMIZATION CONVERGED'
CALL m_flush(unit_nr)
END IF
EXIT
END IF
! calculate gradients
CALL calc_energy_func_der(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, DI, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, eps_step, &
qs_env, nkind, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, &
para_env, para_env_sub, number_groups, color_sub, unit_nr, &
p, lower_B, max_dev, &
deriv)
! g is the vector containing the old gradient
dg(:) = deriv - g
g(:) = deriv
hdg(:) = MATMUL(hessin, dg)
fac = SUM(dg*xi)
fae = SUM(dg*hdg)
sumdg = SUM(dg*dg)
sumxi = SUM(xi*xi)
hess_up = .TRUE.
IF (fac**2 > sumdg*sumxi*3.0E-8_dp) THEN
fac = 1.0_dp/fac
fad = 1.0_dp/fae
dg(:) = fac*xi - fad*hdg
DO i = 1, ndof
DO j = 1, ndof
hessin(i, j) = hessin(i, j) + fac*xi(i)*xi(j) &
- fad*hdg(i)*hdg(j) &
+ fae*dg(i)*dg(j)
END DO
END DO
ELSE
IF (unit_nr > 0) WRITE (unit_nr, '(T3,A)') 'Skip Hessian Update'
hess_up = .FALSE.
END IF
! new direction
xi(:) = -MATMUL(hessin, g)
END DO
IF (.NOT. (DI/ABS(Emp2) <= eps_DI_rel .AND. ABS(DRI_new) <= eps_DRI)) THEN
IF (unit_nr > 0) WRITE (unit_nr, '(T3,A,I5,A)') 'OPTIMIZATION NOT CONVERGED IN', max_num_iter, ' STEPS.'
IF (unit_nr > 0) WRITE (unit_nr, *)
END IF
DEALLOCATE (max_rel_dev)
DEALLOCATE (p)
DEALLOCATE (xi)
DEALLOCATE (g)
DEALLOCATE (pnew)
DEALLOCATE (dg)
DEALLOCATE (hdg)
DEALLOCATE (deriv)
DEALLOCATE (Hessin)
DEALLOCATE (lower_B)
DEALLOCATE (max_dev)
DEALLOCATE (exp_limits)
IF (open_shell_case) THEN
DEALLOCATE (Integ_MP2_AA)
DEALLOCATE (Integ_MP2_BB)
DEALLOCATE (Integ_MP2_AB)
ELSE
DEALLOCATE (Integ_MP2)
END IF
DEALLOCATE (index_table_RI)
! Release RI basis set
CALL release_RI_basis_set(RI_basis_parameter, basis_S0)
CALL mp_para_env_release(para_env_sub)
CALL cp_rm_default_logger()
CALL cp_logger_release(logger_sub)
CALL timestop(handle)
END SUBROUTINE optimize_ri_basis_main
! **************************************************************************************************
!> \brief ...
!> \param Emp2 ...
!> \param Emp2_AA ...
!> \param Emp2_BB ...
!> \param Emp2_AB ...
!> \param DI_ref ...
!> \param Integ_MP2 ...
!> \param Integ_MP2_AA ...
!> \param Integ_MP2_BB ...
!> \param Integ_MP2_AB ...
!> \param eps ...
!> \param qs_env ...
!> \param nkind ...
!> \param natom ...
!> \param dimen ...
!> \param dimen_RI ...
!> \param homo ...
!> \param virtual ...
!> \param kind_of ...
!> \param index_table_RI ...
!> \param mp2_biel ...
!> \param mp2_env ...
!> \param Auto ...
!> \param C ...
!> \param RI_basis_parameter ...
!> \param RI_basis_info ...
!> \param basis_S0 ...
!> \param open_shell_case ...
!> \param homo_beta ...
!> \param virtual_beta ...
!> \param Auto_beta ...
!> \param C_beta ...
!> \param para_env ...
!> \param para_env_sub ...
!> \param number_groups ...
!> \param color_sub ...
!> \param unit_nr ...
!> \param p ...
!> \param lower_B ...
!> \param max_dev ...
!> \param deriv ...
! **************************************************************************************************
SUBROUTINE calc_energy_func_der(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, DI_ref, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, eps, &
qs_env, nkind, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, &
para_env, para_env_sub, number_groups, color_sub, unit_nr, &
p, lower_B, max_dev, &
deriv)
REAL(KIND=dp), INTENT(IN) :: Emp2
REAL(KIND=dp), INTENT(INOUT) :: Emp2_AA, Emp2_BB, Emp2_AB
REAL(KIND=dp), INTENT(IN) :: DI_ref
REAL(KIND=dp), ALLOCATABLE, &
DIMENSION(:, :, :, :), INTENT(IN) :: Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, &
Integ_MP2_AB
REAL(KIND=dp), INTENT(IN) :: eps
TYPE(qs_environment_type), INTENT(IN), POINTER :: qs_env
INTEGER, INTENT(IN) :: nkind, natom, dimen, dimen_RI, homo, &
virtual
INTEGER, DIMENSION(:), INTENT(IN) :: kind_of
INTEGER, ALLOCATABLE, DIMENSION(:, :), &
INTENT(INOUT) :: index_table_RI
TYPE(mp2_biel_type), INTENT(IN) :: mp2_biel
TYPE(mp2_type), INTENT(INOUT) :: mp2_env
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: Auto
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: C
TYPE(hfx_basis_type), DIMENSION(:), INTENT(IN), &
POINTER :: RI_basis_parameter
TYPE(hfx_basis_info_type), INTENT(IN) :: RI_basis_info
TYPE(hfx_basis_type), DIMENSION(:), INTENT(IN), &
POINTER :: basis_S0
LOGICAL, INTENT(IN) :: open_shell_case
INTEGER, INTENT(IN) :: homo_beta, virtual_beta
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: Auto_beta
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: C_beta
TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env, para_env_sub
INTEGER, INTENT(IN) :: number_groups, color_sub, unit_nr
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: p, lower_B, max_dev
REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: deriv
CHARACTER(LEN=*), PARAMETER :: routineN = 'calc_energy_func_der'
INTEGER :: handle, ideriv, ikind, iset, nseta
REAL(KIND=dp) :: DI, DRI, Emp2_RI, new_basis_val, &
orig_basis_val
REAL(KIND=dp), VOLATILE :: step, temp
CALL timeset(routineN, handle)
step = eps
! cycle over the RI basis set exponent
deriv = 0.0_dp
ideriv = 0
DO ikind = 1, nkind
nseta = RI_basis_parameter(ikind)%nset
DO iset = 1, nseta
! for now only uncontracted aux basis set
ideriv = ideriv + 1
IF (MOD(ideriv, number_groups) /= color_sub) CYCLE
! calculate the numerical derivative
! The eps is the relative change of the exponent for the
! calculation of the numerical derivative
! in the new case eps is just the step length for calculating the numerical derivative
CPASSERT(RI_basis_parameter(ikind)%npgf(iset) == 1)
orig_basis_val = RI_basis_parameter(ikind)%zet(1, iset)
temp = p(ideriv) + step
new_basis_val = transf_val(lower_B(ideriv), max_dev(ideriv), temp)
RI_basis_parameter(ikind)%zet(1, iset) = new_basis_val
CALL calc_energy_func(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, Emp2_RI, DRI, DI, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, &
qs_env, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, &
para_env_sub, unit_nr, .TRUE.)
RI_basis_parameter(ikind)%zet(1, iset) = orig_basis_val
IF (para_env_sub%mepos == 0) THEN
temp = EXP(DI)
temp = temp/EXP(DI_ref)
deriv(ideriv) = LOG(temp)/step
END IF
END DO
END DO
CALL para_env%sum(deriv)
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param Emp2 ...
!> \param Emp2_AA ...
!> \param Emp2_BB ...
!> \param Emp2_AB ...
!> \param Emp2_RI ...
!> \param DRI ...
!> \param DI ...
!> \param Integ_MP2 ...
!> \param Integ_MP2_AA ...
!> \param Integ_MP2_BB ...
!> \param Integ_MP2_AB ...
!> \param qs_env ...
!> \param natom ...
!> \param dimen ...
!> \param dimen_RI ...
!> \param homo ...
!> \param virtual ...
!> \param kind_of ...
!> \param index_table_RI ...
!> \param mp2_biel ...
!> \param mp2_env ...
!> \param Auto ...
!> \param C ...
!> \param RI_basis_parameter ...
!> \param RI_basis_info ...
!> \param basis_S0 ...
!> \param open_shell_case ...
!> \param homo_beta ...
!> \param virtual_beta ...
!> \param Auto_beta ...
!> \param C_beta ...
!> \param para_env ...
!> \param unit_nr ...
!> \param no_write ...
! **************************************************************************************************
SUBROUTINE calc_energy_func(Emp2, Emp2_AA, Emp2_BB, Emp2_AB, Emp2_RI, DRI, DI, &
Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, Integ_MP2_AB, &
qs_env, natom, dimen, dimen_RI, homo, virtual, &
kind_of, index_table_RI, mp2_biel, mp2_env, Auto, C, &
RI_basis_parameter, RI_basis_info, basis_S0, &
open_shell_case, homo_beta, virtual_beta, Auto_beta, C_beta, para_env, unit_nr, &
no_write)
REAL(KIND=dp), INTENT(IN) :: Emp2, Emp2_AA, Emp2_BB, Emp2_AB
REAL(KIND=dp), INTENT(OUT) :: Emp2_RI, DRI, DI
REAL(KIND=dp), ALLOCATABLE, &
DIMENSION(:, :, :, :), INTENT(IN) :: Integ_MP2, Integ_MP2_AA, Integ_MP2_BB, &
Integ_MP2_AB
TYPE(qs_environment_type), INTENT(IN), POINTER :: qs_env
INTEGER, INTENT(IN) :: natom, dimen, dimen_RI, homo, virtual
INTEGER, DIMENSION(:), INTENT(IN) :: kind_of
INTEGER, ALLOCATABLE, DIMENSION(:, :), &
INTENT(INOUT) :: index_table_RI
TYPE(mp2_biel_type), INTENT(IN) :: mp2_biel
TYPE(mp2_type), INTENT(INOUT) :: mp2_env
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: Auto
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: C
TYPE(hfx_basis_type), DIMENSION(:), INTENT(IN), &
POINTER :: RI_basis_parameter
TYPE(hfx_basis_info_type), INTENT(IN) :: RI_basis_info
TYPE(hfx_basis_type), DIMENSION(:), INTENT(IN), &
POINTER :: basis_S0
LOGICAL, INTENT(IN) :: open_shell_case
INTEGER, INTENT(IN) :: homo_beta, virtual_beta
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: Auto_beta
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: C_beta
TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
INTEGER, INTENT(IN) :: unit_nr
LOGICAL, INTENT(IN) :: no_write
CHARACTER(LEN=*), PARAMETER :: routineN = 'calc_energy_func'
INTEGER :: handle
REAL(KIND=dp) :: DI_AA, DI_AB, DI_BB, DRI_AA, DRI_AB, &
DRI_BB, Emp2_RI_AA, Emp2_RI_AB, &
Emp2_RI_BB
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: Lai, Lai_beta
CALL timeset(routineN, handle)
CALL libint_ri_mp2(dimen, dimen_RI, homo, natom, mp2_biel, mp2_env, C, &
kind_of, RI_basis_parameter, RI_basis_info, basis_S0, index_table_RI, &
qs_env, para_env, Lai)
IF (open_shell_case) THEN
CALL libint_ri_mp2(dimen, dimen_RI, homo_beta, natom, mp2_biel, mp2_env, C_beta, &
kind_of, RI_basis_parameter, RI_basis_info, basis_S0, index_table_RI, &
qs_env, para_env, Lai_beta)
END IF
! Contract integrals into energy
IF (open_shell_case) THEN
! alpha-alpha
CALL contract_integrals(DI_AA, Emp2_RI_AA, DRI_AA, Emp2_AA, homo, homo, virtual, virtual, &
1.0_dp, 0.5_dp, .TRUE., &
Auto, Auto, Integ_MP2_AA, &
Lai, Lai, para_env)
! beta-beta
CALL contract_integrals(DI_BB, Emp2_RI_BB, DRI_BB, Emp2_BB, homo_beta, homo_beta, virtual_beta, virtual_beta, &
1.0_dp, 0.5_dp, .TRUE., &
Auto_beta, Auto_beta, Integ_MP2_BB, &
Lai_beta, Lai_beta, para_env)
! alpha-beta
CALL contract_integrals(DI_AB, Emp2_RI_AB, DRI_AB, Emp2_AB*2.0_dp, homo, homo_beta, virtual, virtual_beta, &
1.0_dp, 1.0_dp, .FALSE., &
Auto, Auto_beta, Integ_MP2_AB, &
Lai, Lai_beta, para_env)
Emp2_RI = Emp2_RI_AA + Emp2_RI_BB + Emp2_RI_AB
DRI = DRI_AA + DRI_BB + DRI_AB
DI = DI_AA + DI_BB + DI_AB
ELSE
CALL contract_integrals(DI, Emp2_RI, DRI, Emp2, homo, homo, virtual, virtual, &
2.0_dp, 1.0_dp, .TRUE., &
Auto, Auto, Integ_MP2, &
Lai, Lai, para_env)
END IF
IF (.NOT. no_write) THEN
IF (unit_nr > 0) THEN
WRITE (unit_nr, '(/,(T3,A,T56,F25.14))') &
'Emp2 =', Emp2, &
'Emp2-RI =', Emp2_RI
WRITE (unit_nr, '(T3,A,T56,ES25.10)') &
'DRI =', DRI, &
'DI =', DI, &
'DI/|Emp2| =', DI/ABS(Emp2)
CALL m_flush(unit_nr)
END IF
END IF
DEALLOCATE (Lai)
IF (open_shell_case) DEALLOCATE (Lai_beta)
CALL timestop(handle)
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param nkind ...
!> \param RI_basis_parameter ...
!> \param lower_B ...
!> \param max_dev ...
!> \param max_rel_dev ...
! **************************************************************************************************
PURE SUBROUTINE init_transf(nkind, RI_basis_parameter, lower_B, max_dev, max_rel_dev)
INTEGER, INTENT(IN) :: nkind
TYPE(hfx_basis_type), DIMENSION(:), INTENT(IN), &
POINTER :: RI_basis_parameter
REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: lower_B, max_dev
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: max_rel_dev
INTEGER :: ikind, ipos, iset
ipos = 0
DO ikind = 1, nkind
DO iset = 1, RI_basis_parameter(ikind)%nset
ipos = ipos + 1
lower_B(ipos) = RI_basis_parameter(ikind)%zet(1, iset)*(1.0_dp - max_rel_dev(ipos))
max_dev(ipos) = RI_basis_parameter(ikind)%zet(1, iset)*2.0_dp*max_rel_dev(ipos)
END DO
END DO
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param nkind ...
!> \param RI_basis_parameter ...
!> \param Lower_B ...
!> \param max_dev ...
!> \param p ...
! **************************************************************************************************
SUBROUTINE p2basis(nkind, RI_basis_parameter, Lower_B, max_dev, p)
INTEGER, INTENT(IN) :: nkind
TYPE(hfx_basis_type), DIMENSION(:), INTENT(IN), &
POINTER :: RI_basis_parameter
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
INTENT(IN) :: Lower_B, max_dev, p
INTEGER :: ikind, ipos, iset
REAL(KIND=dp) :: valout
ipos = 0
DO ikind = 1, nkind
DO iset = 1, RI_basis_parameter(ikind)%nset
ipos = ipos + 1
valout = transf_val(lower_B(ipos), max_dev(ipos), p(ipos))
RI_basis_parameter(ikind)%zet(1, iset) = valout
END DO
END DO
END SUBROUTINE
! **************************************************************************************************
!> \brief ...
!> \param nkind ...
!> \param ndof ...
!> \param RI_basis_parameter ...
!> \param reset_edge ...
!> \param pnew ...
!> \param lower_B ...
!> \param max_dev ...
!> \param max_rel_dev ...
!> \param exp_limits ...
! **************************************************************************************************
SUBROUTINE reset_basis(nkind, ndof, RI_basis_parameter, reset_edge, &
pnew, lower_B, max_dev, max_rel_dev, exp_limits)
INTEGER, INTENT(IN) :: nkind, ndof
TYPE(hfx_basis_type), DIMENSION(:), INTENT(IN), &
POINTER :: RI_basis_parameter
REAL(KIND=dp), INTENT(IN) :: reset_edge
REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: pnew, Lower_B, max_dev, max_rel_dev
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: exp_limits
INTEGER :: am_max, iexpo, ikind, ipos, ipos_p, &
iset, la
INTEGER, ALLOCATABLE, DIMENSION(:) :: nf_per_l
INTEGER, DIMENSION(ndof) :: change_expo
LOGICAL, ALLOCATABLE, DIMENSION(:) :: has_to_be_changed
REAL(KIND=dp) :: expo, geom_fact, pmax
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: max_min_exp_per_l
REAL(KIND=dp), DIMENSION(ndof) :: new_expo, old_expo, old_lower_B, &
old_max_dev, old_max_rel_dev, old_pnew
! make a copy of the original parameters
old_pnew = pnew
old_lower_B = lower_B
old_max_dev = max_dev
old_max_rel_dev = max_rel_dev
old_expo = 0.0_dp
ipos = 0
DO ikind = 1, nkind
DO iset = 1, RI_basis_parameter(ikind)%nset
ipos = ipos + 1
old_expo(ipos) = RI_basis_parameter(ikind)%zet(1, iset)
END DO
END DO
pnew = 0.0_dp
lower_B = 0.0_dp
max_dev = 0.0_dp
max_rel_dev = 0.0_dp
change_expo = 0
new_expo = 0.0_dp
ipos = 0
ipos_p = 0
DO ikind = 1, nkind
am_max = MAXVAL(RI_basis_parameter(ikind)%lmax(:))
ALLOCATE (nf_per_l(0:am_max))
nf_per_l = 0
ALLOCATE (max_min_exp_per_l(2, 0:am_max))
max_min_exp_per_l(1, :) = HUGE(0)
max_min_exp_per_l(2, :) = -HUGE(0)
DO iset = 1, RI_basis_parameter(ikind)%nset
la = RI_basis_parameter(ikind)%lmax(iset)
expo = RI_basis_parameter(ikind)%zet(1, iset)
nf_per_l(la) = nf_per_l(la) + 1
IF (expo <= max_min_exp_per_l(1, la)) max_min_exp_per_l(1, la) = expo
IF (expo >= max_min_exp_per_l(2, la)) max_min_exp_per_l(2, la) = expo
END DO
max_min_exp_per_l(1, la) = MAX(max_min_exp_per_l(1, la), exp_limits(1, ikind))
max_min_exp_per_l(2, la) = MIN(max_min_exp_per_l(2, la), exp_limits(2, ikind))
! always s exponents as maximum and minimu
! expo=MAXVAL(max_min_exp_per_l(2,:))
! max_min_exp_per_l(2,0)=expo
! expo=MINVAL(max_min_exp_per_l(1,:))
! max_min_exp_per_l(1,0)=expo
ALLOCATE (has_to_be_changed(0:am_max))
has_to_be_changed = .FALSE.
DO la = 0, am_max
pmax = -HUGE(0)
DO iexpo = 1, nf_per_l(la)
ipos_p = ipos_p + 1
IF (ABS(old_pnew(ipos_p)) >= pmax) pmax = ABS(old_pnew(ipos_p))
! check if any of the exponents go out of range
expo = transf_val(old_lower_B(ipos_p), old_max_dev(ipos_p), old_pnew(ipos_p))
IF (expo < exp_limits(1, ikind) .OR. expo > exp_limits(2, ikind)) has_to_be_changed(la) = .TRUE.
END DO
IF (pmax > reset_edge) has_to_be_changed(la) = .TRUE.
END DO
DO la = 0, am_max
IF (nf_per_l(la) == 1) THEN
ipos = ipos + 1
new_expo(ipos) = max_min_exp_per_l(1, la)
IF (new_expo(ipos) >= exp_limits(1, ikind) .AND. new_expo(ipos) <= exp_limits(2, ikind)) THEN
max_rel_dev(ipos) = (new_expo(ipos) - old_lower_B(ipos))/new_expo(ipos)
IF (max_rel_dev(ipos) <= 0.1_dp) max_rel_dev(ipos) = 0.8_dp
ELSE
new_expo(ipos) = (exp_limits(2, ikind) + exp_limits(1, ikind))/2.0_dp
max_rel_dev(ipos) = (new_expo(ipos) - exp_limits(1, ikind))/new_expo(ipos)
END IF
IF (has_to_be_changed(la)) change_expo(ipos) = 1
ELSE
IF (max_min_exp_per_l(2, la)/max_min_exp_per_l(1, la) < 2.0_dp) THEN
max_min_exp_per_l(1, la) = max_min_exp_per_l(1, la)*0.5
max_min_exp_per_l(2, la) = max_min_exp_per_l(2, la)*1.5
END IF
geom_fact = (max_min_exp_per_l(2, la)/max_min_exp_per_l(1, la))**(1.0_dp/REAL(nf_per_l(la) - 1, dp))
DO iexpo = 1, nf_per_l(la)
ipos = ipos + 1
new_expo(ipos) = max_min_exp_per_l(1, la)*(geom_fact**(iexpo - 1))
max_rel_dev(ipos) = (geom_fact - 1.0_dp)/(geom_fact + 1.0_dp)*0.9_dp
IF (has_to_be_changed(la)) change_expo(ipos) = 1
END DO
END IF
END DO
DEALLOCATE (has_to_be_changed)
DEALLOCATE (nf_per_l)
DEALLOCATE (max_min_exp_per_l)
END DO