-
Notifications
You must be signed in to change notification settings - Fork 1
/
ef.f
2080 lines (2065 loc) · 94.5 KB
/
ef.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
SUBROUTINE EF(XPARAM, NVAR, FUNCT)
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
REAL*8 LAMDA,LAMDA0
INCLUDE 'SIZES'
DIMENSION XPARAM(MAXPAR)
**********************************************************************
*
* EF IS A QUASI NEWTON RAPHSON OPTIMIZATION ROUTINE BASED ON
* Jacs Simons P-RFO algorithm as implemented by Jon Baker
* (J.COMP.CHEM. 7, 385). Step scaling to keep length within
* trust radius is taken from Culot et al. (Theo. Chim. Acta 82, 189)
* The trust radius can be updated dynamically according to Fletcher.
* Safeguards on valid step for TS searches based on actual/predicted
* function change and change in TS mode are own modifications
*
* ON ENTRY XPARAM = VALUES OF PARAMETERS TO BE OPTIMISED.
* NVAR = NUMBER OF PARAMETERS TO BE OPTIMISED.
*
* ON EXIT XPARAM = OPTIMISED PARAMETERS.
* FUNCT = HEAT OF FORMATION IN KCAL/MOL.
*
* Current version implementing combined NR, P-RFO and QA algorithm
* together with thrust radius update and step rejection was
* made october 1992 by F.Jensen, Odense, DK
*
**********************************************************************
C
COMMON /MESAGE/ IFLEPO,ISCF
COMMON /GEOVAR/ NDUM,LOC(2,MAXPAR), IDUMY, XARAM(MAXPAR)
COMMON /GEOM / GEO(3,NUMATM)
COMMON /GEOSYM/ NDEP,LOCPAR(MAXPAR),IDEPFN(MAXPAR),LOCDEP(MAXPAR)
COMMON /ISTOPE/ AMS(107)
COMMON /LAST / LAST
COMMON /KEYWRD/ KEYWRD
COMMON /TIME / TIME0
COMMON /GRADNT/ GRAD(MAXPAR),GNFINA
COMMON /MOLKST/ NUMAT,NAT(NUMATM),NFIRST(NUMATM),NMIDLE(NUMATM),
1 NLAST(NUMATM), NORBS, NELECS,NALPHA,NBETA,
2 NCLOSE,NOPEN,NDUMY,FRACT
COMMON /NUMCAL/ NUMCAL
COMMON /TIMDMP/ TLEFT, TDUMP
COMMON /SIGMA2/ GNEXT1(MAXPAR), GMIN1(MAXPAR)
CONVEX COMMON /NLLCOM/ HESS(MAXPAR,MAXPAR),BMAT(MAXPAR,MAXPAR),
CONVEX 1PMAT(MAXPAR*MAXPAR)
COMMON /NLLCOM/ HESS(MAXPAR,MAXPAR),BMAT(MAXPAR,MAXPAR),
1PMAT(MAXHES)
CONVEX COMMON /SCRACH/ PVEC
COMMON /SCFTYP/ EMIN, LIMSCF
COMMON/OPTEF/OLDF(MAXPAR),D(MAXPAR),VMODE(MAXPAR),
$U(MAXPAR,MAXPAR),DD,rmin,rmax,omin,xlamd,xlamd0,skal,
$MODE,NSTEP,NEGREQ,IPRNT
COMMON/THREADS/NUM_THREADS
COMMON/FLUSHX/NFLUSH
DIMENSION IPOW(9), EIGVAL(MAXPAR),TVEC(MAXPAR),SVEC(MAXPAR),
1FX(MAXPAR),HESSC(MAXHES),UC(MAXPAR**2),oldfx(maxpar),
1oldeig(maxpar),
$oldhss(maxpar,maxpar),oldu(maxpar,maxpar),ooldf(maxpar)
DIMENSION BB(MAXPAR,MAXPAR)
LOGICAL RESTRT,SCF1,LIMSCF,LOG
LOGICAL LUPD,lts,lrjk,lorjk,rrscal,donr,gnmin
CHARACTER KEYWRD*241
EQUIVALENCE(IPOW(1),IHESS)
DATA ICALCN,ZERO,ONE,TWO /0,0.D0,1.D0,2.D0/
DATA tmone /1.0d-1/, TMTWO/1.0D-2/, TMSIX/1.0D-06/
data three/3.0d0/, four/4.0d0/,
1pt25/0.25d0/, pt5/0.50d0/, pt75/0.75d0/
data demin/2.0d-2/, gmin/5.0d0/
C GET ALL INITIALIZATION DATA
IF(ICALCN.NE.NUMCAL)
1CALL EFSTR(XPARAM,FUNCT,IHESS,NTIME,ILOOP,IGTHES,
$MXSTEP,IRECLC,IUPD,DMAX,DDMAX,dmin,TOL2,TOTIME,TIME1,TIME2,nvar,
$SCF1,LUPD,ldump,log,rrscal,donr,gnmin)
lts=.false.
if (negreq.eq.1) lts=.true.
lorjk=.false.
c osmin is smallest step for which a ts-mode overlap less than omin
c will be rejected. for updated hessians there is little hope of
c better overlap by reducing the step below 0.005. for exact hessian
c the overlap should go toward one as the step become smaller, but
c don't allow very small steps
osmin=0.005d0
if(ireclc.eq.1)osmin=0.001d0
IF (SCF1) THEN
GNFINA=SQRT(DOT(GRAD,GRAD,NVAR))
IFLEPO=1
RETURN
ENDIF
C CHECK THAT GEOMETRY IS NOT ALREADY OPTIMIZED
RMX=SQRT(DOT(GRAD,GRAD,NVAR))
IF (RMX.LT.TOL2) THEN
IFLEPO=2
LAST=1
RETURN
ENDIF
C GET INITIAL HESSIAN. IF ILOOP IS .LE.0 THIS IS AN OPTIMIZATION RESTART
C AND HESSIAN SHOULD ALREADY BE AVAILABLE
IF (ILOOP .GT. 0) CALL GETHES(XPARAM,IGTHES,NVAR,iloop,TOTIME)
C START OF MAIN LOOP
C WE NOW HAVE GRADIENTS AND A HESSIAN. IF THIS IS THE FIRST
C TIME THROUGH DON'T UPDATE THE HESSIAN. FOR LATER LOOPS ALSO
C CHECK IF WE NEED TO RECALCULATE THE HESSIAN
IFLEPO=0
itime=0
10 CONTINUE
c store various things for possibly omin rejection
do 30 i=1,nvar
oldfx(i)=fx(i)
ooldf(i)=oldf(i)
oldeig(i)=eigval(i)
do 20 j=1,nvar
oldhss(i,j)=hess(i,j)
oldu(i,j)=u(i,j)
20 continue
30 continue
IF (IHESS.GE.IRECLC.AND.IFLEPO.NE.15) THEN
ILOOP=1
IHESS=0
if (igthes.ne.3)IGTHES=1
CALL GETHES(XPARAM,IGTHES,NVAR,iloop,TOTIME)
ENDIF
IF (IHESS.GT.0) CALL UPDHES(SVEC,TVEC,NVAR,IUPD)
IF(IPRNT.GE.2) call geout(6)
IF(IPRNT.GE.2) THEN
WRITE(6,'('' XPARAM '')')
WRITE(6,'(5(2I3,F10.4))')(LOC(1,I),LOC(2,I),XPARAM(I),I=1,NV
1AR)
WRITE(6,'('' GRADIENTS'')')
WRITE(6,'(3X,8F9.3)')(GRAD(I),I=1,NVAR)
ENDIF
C
C PRINT RESULTS IN CYCLE
GNFINA=SQRT(DOT(GRAD,GRAD,NVAR))
TIME2=SECOND()
if (itime.eq.0) time1=time0
TSTEP=TIME2-TIME1
IF (TSTEP.LT.ZERO)TSTEP=ZERO
TLEFT=TLEFT-TSTEP
TIME1=TIME2
itime=itime+1
IF (TLEFT .LT. TSTEP*TWO) GOTO 280
IF(LDUMP.EQ.0)THEN
WRITE(6,40)NSTEP+1,MIN(TSTEP,9999.99D0),
1MIN(TLEFT,9999999.9D0),MIN(GNFINA,999999.999D0),FUNCT
IF(LOG)WRITE(11,40)NSTEP+1,MIN(TSTEP,9999.99D0),
1MIN(TLEFT,9999999.9D0),MIN(GNFINA,999999.999D0),FUNCT
40 FORMAT(' CYCLE:',I4,' TIME:',F7.2,' TIME LEFT:',F9.1,
1' GRAD.:',F10.3,' HEAT:',G13.7)
IF ( NFLUSH.NE.0 ) THEN
IF ( MOD(NSTEP+1,NFLUSH).EQ.0) THEN
call flushm(6)
call flushm(11)
ENDIF
ENDIF
ELSE
WRITE(6,50)MIN(TLEFT,9999999.9D0),
1MIN(GNFINA,999999.999D0),FUNCT
IF(LOG)WRITE(11,50)MIN(TLEFT,9999999.9D0),
1MIN(GNFINA,999999.999D0),FUNCT
50 FORMAT(' RESTART FILE WRITTEN, TIME LEFT:',F9.1,
1' GRAD.:',F10.3,' HEAT:',G13.7)
IF ( NFLUSH.NE.0 ) THEN
IF ( MOD(NSTEP+1,NFLUSH).EQ.0) THEN
call flushm(6)
call flushm(11)
ENDIF
ENDIF
ENDIF
IHESS=IHESS+1
NSTEP=NSTEP+1
C
C TEST FOR CONVERGENCE
C
RMX=SQRT(DOT(GRAD,GRAD,NVAR))
IF (RMX.LT.TOL2)GOTO 250
OLDE = FUNCT
oldgn = rmx
DO 60 I=1,NVAR
OLDF(I)=GRAD(I)
60 CONTINUE
C
C if the optimization is in cartesian coordinates, we should remove
C translation and rotation modes. Possible problem if run is in
C internal but with exactly 3*natoms variable (i.e. dummy atoms
C are also optimized).
if (nvar.eq.3*numat) then
if (nstep.eq.1) write(6,70)
70 format(1x,'WARNING! EXACTLY 3N VARIABLES. EF ASSUMES THIS IS A',
$ ' CARTESIAN OPTIMIZATION.',/,1x,'IF THE OPTIMIZATION IS',
$ ' IN INTERNAL COORDINATES, EF WILL NOT WORK')
call prjfc(hess,xparam,nvar)
endif
IJ=0
DO 80 I=1,NVAR
DO 80 J=1,I
IJ=IJ+1
HESSC(IJ)=HESS(J,I)
80 CONTINUE
CONVEX CALL HQRII(HESSC,NVAR,NVAR,EIGVAL,UC)
CALL RSP(HESSC,NVAR,NVAR,EIGVAL,UC)
IJ=0
DO 90 I=1,NVAR
IF (ABS(EIGVAL(I)).LT.TMSIX) EIGVAL(I)=ZERO
DO 90 J=1,NVAR
IJ=IJ+1
U(J,I)=UC(IJ)
90 CONTINUE
IF (IPRNT.GE.3) CALL PRTHES(EIGVAL,NVAR)
IF (MXSTEP.EQ.0) nstep=0
IF (MXSTEP.EQ.0) GOTO 280
NEG=0
DO 100 I=1,NVAR
IF (EIGVAL(I) .LT. ZERO)NEG=NEG+1
100 CONTINUE
IF (IPRNT.GE.1)WRITE(6,110)NEG,(eigval(i),i=1,neg)
110 FORMAT(/,10X,'HESSIAN HAS',I3,' NEGATIVE EIGENVALUE(S)',6f7.1,/)
c if an eigenvalue has been zero out it is probably one of the T,R modes
c in a cartesian optimization. zero corresponding fx to allow formation
c of step without these contributions. a more safe criteria for deciding
c whether this actually is a cartesian optimization should be put in
c some day...
DO 120 I=1,NVAR
FX(I)=DOT(U(1,I),GRAD,NVAR)
if (abs(eigval(i)).eq.zero) fx(i)=zero
120 CONTINUE
c form geometry step d
130 CALL FORMD(EIGVAL,FX,NVAR,DMAX,osmin,LTS,lrjk,lorjk,rrscal,donr)
c if lorjk is true, then ts mode overlap is less than omin, reject prev step
if (lorjk) then
if (iprnt.ge.1)write(6,*)' Now undoing previous step'
dmax=odmax
dd=odd
olde=oolde
do i=1,nvar
fx(i)=oldfx(i)
oldf(i)=ooldf(i)
eigval(i)=oldeig(i)
do j=1,nvar
hess(i,j)=oldhss(i,j)
u(i,j)=oldu(i,j)
enddo
enddo
DO 140 I=1,NVAR
XPARAM(I)=XPARAM(I)-D(I)
K=LOC(1,I)
L=LOC(2,I)
GEO(L,K)=XPARAM(I)
140 CONTINUE
IF(NDEP.NE.0) CALL SYMTRY
dmax=min(dmax,dd)/two
odmax=dmax
odd=dd
nstep=nstep-1
if (dmax.lt.dmin) goto 230
if (iprnt.ge.1)write(6,*)
1' Finish undoing, now going for new step'
goto 130
endif
C
C FORM NEW TRIAL XPARAM AND GEO
C
DO 150 I=1,NVAR
XPARAM(I)=XPARAM(I)+D(I)
K=LOC(1,I)
L=LOC(2,I)
GEO(L,K)=XPARAM(I)
150 CONTINUE
IF(NDEP.NE.0) CALL SYMTRY
C
C COMPARE PREDICTED E-CHANGE WITH ACTUAL
C
depre=zero
imode=1
if (mode.ne.0)imode=mode
do 160 i=1,nvar
xtmp=xlamd
if (lts .and. i.eq.imode) xtmp=xlamd0
if (abs(xtmp-eigval(i)).lt.tmtwo) then
ss=zero
else
ss=skal*fx(i)/(xtmp-eigval(i))
endif
frodo=ss*fx(i) + pt5*ss*ss*eigval(i)
c write(6,88)i,fx(i),ss,xtmp,eigval(i),frodo
depre=depre+frodo
160 continue
c88 format(i3,f10.3,f10.6,f10.3,4f10.6)
C
C GET GRADIENT FOR NEW GEOMETRY
C
CALL COMPFG(XPARAM, .TRUE., FUNCT, .TRUE., GRAD, .TRUE.)
if(gnmin)gntest=sqrt(dot(grad,grad,nvar))
DEACT = FUNCT-OLDE
RATIO = DEACT/DEPRE
if(iprnt.ge.1)WRITE(6,170)DEACT,DEPRE,RATIO
170 FORMAT(5X,'ACTUAL, PREDICTED ENERGY CHANGE, RATIO',2F10.3,F10.5)
lrjk=.false.
C if this is a minimum search, don't allow the energy to raise
if (.not.lts .and. funct.gt.olde) then
if (iprnt.ge.1)write(6,180)funct,min(dmax,dd)/two
180 format(1x,'energy raises ',f10.4,' rejecting step, ',
$ 'reducing dmax to',f7.4)
lrjk=.true.
endif
if (gnmin .and. gntest.gt.oldgn) then
if (iprnt.ge.1)write(6,181)gntest,min(dmax,dd)/two
181 format(1x,'gradient norm raises ',f10.4,' rejecting step, ',
$ 'reducing dmax to',f7.4)
lrjk=.true.
endif
if (lts .and. (ratio.lt.rmin .or. ratio.gt.rmax) .and.
$(abs(depre).gt.demin .or. abs(deact).gt.demin)) then
if (iprnt.ge.1)write(6,190)min(dmax,dd)/two
190 format(1x,'unacceptable ratio,',
$ ' rejecting step, reducing dmax to',f7.4)
lrjk=.true.
endif
if (lrjk) then
DO 200 I=1,NVAR
XPARAM(I)=XPARAM(I)-D(I)
K=LOC(1,I)
L=LOC(2,I)
GEO(L,K)=XPARAM(I)
200 CONTINUE
IF(NDEP.NE.0) CALL SYMTRY
dmax=min(dmax,dd)/two
if (dmax.lt.dmin) goto 230
goto 130
endif
IF(IPRNT.GE.1)WRITE(6,210)DD
210 FORMAT(5X,'STEPSIZE USED IS',F9.5)
IF(IPRNT.GE.2) THEN
WRITE(6,'('' CALCULATED STEP'')')
WRITE(6,'(3X,8F9.5)')(D(I),I=1,NVAR)
ENDIF
C
C POSSIBLE USE DYNAMICAL TRUST RADIUS
odmax=dmax
odd=dd
oolde=olde
IF (LUPD .and. ( (RMX.gt.gmin) .or.
$ (abs(depre).gt.demin .or. abs(deact).gt.demin) ) ) THEN
c Fletcher recommend dmax=dmax/4 and dmax=dmax*2
c these are are a little more conservative since hessian is being updated
c don't reduce trust radius due to ratio for min searches
if (lts .and. ratio.le.tmone .or. ratio.ge.three)
$ dmax=min(dmax,dd)/two
if (lts .and. ratio.ge.pt75 .and. ratio.le.(four/three)
$ .and. dd.gt.(dmax-tmsix))
$ dmax=dmax*sqrt(two)
c allow wider limits for increasing trust radius for min searches
if (.not.lts .and. ratio.ge.pt5
$ .and. dd.gt.(dmax-tmsix))
$ dmax=dmax*sqrt(two)
c be brave if 0.90 < ratio < 1.10 ...
if (abs(ratio-one).lt.tmone) dmax=dmax*sqrt(two)
dmax=max(dmax,dmin-tmsix)
dmax=min(dmax,ddmax)
ENDIF
c allow stepsize up to 0.1 in the end-game where changes are less
c than demin and gradient is less than gmin
IF (LUPD .and. RMX.lt.gmin .and.
$ (abs(depre).lt.demin .and. abs(deact).lt.demin) )
$ dmax=max(dmax,tmone)
if(iprnt.ge.1)WRITE(6,220)DMAX
220 FORMAT(5X,'CURRENT TRUST RADIUS = ',F7.5)
230 if (dmax.lt.dmin) then
write(6,240)dmin
240 format(/,5x,'TRUST RADIUS NOW LESS THAN ',F7.5,' OPTIMIZATION',
$ ' TERMINATING',/,5X,
1' GEOMETRY MAY NOT BE COMPLETELY OPTIMIZED')
goto 270
endif
C CHECK STEPS AND ENOUGH TIME FOR ANOTHER PASS
if (nstep.ge.mxstep) goto 280
C IN USER UNFRIENDLY ENVIROMENT, SAVE RESULTS EVERY 1 CPU HRS
ITTEST=AINT((TIME2-TIME0)/TDUMP)
IF (ITTEST.GT.NTIME) THEN
LDUMP=1
NTIME=MAX(ITTEST,(NTIME+1))
IPOW(9)=2
TT0=SECOND()-TIME0
CALL EFSAV(TT0,HESS,FUNCT,GRAD,XPARAM,PMAT,-NSTEP,NSTEP,BMAT,I
1POW)
ELSE
LDUMP=0
ENDIF
C RETURN FOR ANOTHER CYCLE
GOTO 10
C
C ****** OPTIMIZATION TERMINATION ******
C
250 CONTINUE
WRITE(6,260)RMX,TOL2
260 FORMAT(/,5X,'RMS GRADIENT =',F9.5,' IS LESS THAN CUTOFF =',
1F9.5,//)
270 IFLEPO=15
LAST=1
C SAVE HESSIAN ON FILE 9
IPOW(9)=2
TT0=SECOND()-TIME0
CALL EFSAV(TT0,HESS,FUNCT,GRAD,XPARAM,PMAT,-NSTEP,NSTEP,BMAT,I
1POW)
C CALL COMPFG TO CALCULATE ENERGY FOR FIXING MO-VECTOR BUG
CALL COMPFG(XPARAM, .TRUE., FUNCT, .TRUE., GRAD, .FALSE.)
RETURN
280 CONTINUE
C WE RAN OUT OF TIME or too many iterations. DUMP RESULTS
IF (TLEFT .LT. TSTEP*TWO) THEN
WRITE(6,290)
290 FORMAT(/,5X,'NOT ENOUGH TIME FOR ANOTHER CYCLE')
ENDIF
IF (nstep.ge.mxstep) THEN
WRITE(6,300)
300 FORMAT(/,5X,'EXCESS NUMBER OF OPTIMIZATION CYCLES')
ENDIF
IPOW(9)=1
TT0=SECOND()-TIME0
CALL EFSAV(TT0,HESS,FUNCT,GRAD,XPARAM,PMAT,-NSTEP,NSTEP,BMAT,I
1POW)
STOP
END
SUBROUTINE EFSAV(TT0,HESS,FUNCT,GRAD,XPARAM,PMAT,IL,JL,BMAT,IPOW)
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
INCLUDE 'SIZES'
CHARACTER ELEMNT*2, KEYWRD*241, KOMENT*81, TITLE*81
DIMENSION HESS(MAXPAR,*),GRAD(*),BMAT(MAXPAR,*),IPOW(9),
1 XPARAM(*), PMAT(*)
**********************************************************************
*
* EFSAV STORES AND RETRIEVE DATA USED IN THE EF GEOMETRY
* OPTIMISATION. VERY SIMILAR TO POWSAV.
*
* ON INPUT HESS = HESSIAN MATRIX, PARTIAL OR WHOLE.
* GRAD = GRADIENTS.
* XPARAM = CURRENT STATE OF PARAMETERS.
* IL = INDEX OF HESSIAN,
* JL = CYCLE NUMBER REACHED SO-FAR.
* BMAT = "B" MATRIX!
* IPOW = INDICES AND FLAGS.
* IPOW(9)= 0 FOR RESTORE, 1 FOR DUMP, 2 FOR SILENT DUMP
*
**********************************************************************
COMMON /GEOVAR/ NVAR,LOC(2,MAXPAR), IDUMY, DUMY(MAXPAR)
COMMON /ELEMTS/ ELEMNT(107)
COMMON /GEOSYM/ NDEP,LOCPAR(MAXPAR),IDEPFN(MAXPAR),
1 LOCDEP(MAXPAR)
COMMON/OPTEF/OLDF(MAXPAR),D(MAXPAR),VMODE(MAXPAR),
$U(MAXPAR,MAXPAR),DD,rmin,rmax,omin,xlamd,xlamd0,skal,
$MODE,NSTEP,NEGREQ,IPRNT
COMMON /TITLES/ KOMENT,TITLE
COMMON /GEOKST/ NATOMS,LABELS(NUMATM),
1 NA(NUMATM),NB(NUMATM),NC(NUMATM)
COMMON /GEOM / GEO(3,NUMATM)
COMMON /LOCVAR/ LOCVAR(2,MAXPAR)
COMMON /NUMSCF/ NSCF
COMMON /KEYWRD/ KEYWRD
COMMON /VALVAR/ VALVAR(MAXPAR),NUMVAR
COMMON /DENSTY/ P(MPACK), PA(MPACK), PB(MPACK)
COMMON /ALPARM/ ALPARM(3,MAXPAR),X0, X1, X2, JLOOP
COMMON /MOLKST/ NUMAT,NAT(NUMATM),NFIRST(NUMATM),NMIDLE(NUMATM),
1 NLAST(NUMATM), NORBS, NELECS,NALPHA,NBETA,
2 NCLOSE,NOPEN,NDUMY,FRACT
COMMON /PATH / LATOM,LPARAM,REACT(200)
OPEN(UNIT=9,FILE='FOR009',STATUS='UNKNOWN',FORM='UNFORMATTED')
REWIND 9
OPEN(UNIT=10,FILE='FOR010',STATUS='UNKNOWN',FORM='UNFORMATTED')
REWIND 10
IR=9
IF(IPOW(9) .EQ. 1 .OR. IPOW(9) .EQ. 2) THEN
FUNCT1=SQRT(DOT(GRAD,GRAD,NVAR))
IF(IPOW(9).EQ.1)THEN
WRITE(6,'(//10X,''CURRENT VALUE OF GRADIENT NORM =''
1 ,F12.6)')FUNCT1
WRITE(6,'(/10X,''CURRENT VALUE OF GEOMETRY'',/)')
CALL GEOUT(6)
ENDIF
C
C IPOW(1) AND IPOW(9) ARE USED ALREADY, THE REST ARE FREE FOR USE
C
IPOW(8)=NSCF
WRITE(IR)IPOW,IL,JL,FUNCT,TT0
WRITE(IR)(XPARAM(I),I=1,NVAR)
WRITE(IR)( GRAD(I),I=1,NVAR)
WRITE(IR)((HESS(J,I),J=1,NVAR),I=1,NVAR)
WRITE(IR)((BMAT(J,I),J=1,NVAR),I=1,NVAR)
WRITE(IR)(OLDF(I),I=1,NVAR),(D(I),I=1,NVAR),(VMODE(I),I=1,NVAR)
WRITE(IR)DD,MODE,NSTEP,NEGREQ
LINEAR=(NVAR*(NVAR+1))/2
WRITE(IR)(PMAT(I),I=1,LINEAR)
LINEAR=(NORBS*(NORBS+1))/2
WRITE(10)(PA(I),I=1,LINEAR)
IF(NALPHA.NE.0)WRITE(10)(PB(I),I=1,LINEAR)
IF(LATOM .NE. 0) THEN
WRITE(IR)((ALPARM(J,I),J=1,3),I=1,NVAR)
WRITE(IR)JLOOP,X0, X1, X2
ENDIF
CLOSE(9)
CLOSE(10)
RETURN
ELSE
C# WRITE(6,'(//10X,'' READING DATA FROM DISK''/)')
READ(IR,END=10,ERR=10)IPOW,IL,JL,FUNCT,TT0
NSCF=IPOW(8)
I=TT0/1000000
TT0=TT0-I*1000000
WRITE(6,'(//10X,''TOTAL TIME USED SO FAR:'',
1 F13.2,'' SECONDS'')')TT0
WRITE(6,'( 10X,'' FUNCTION:'',F17.6)')FUNCT
READ(IR)(XPARAM(I),I=1,NVAR)
READ(IR)( GRAD(I),I=1,NVAR)
READ(IR)((HESS(J,I),J=1,NVAR),I=1,NVAR)
READ(IR)((BMAT(J,I),J=1,NVAR),I=1,NVAR)
READ(IR)(OLDF(I),I=1,NVAR),(D(I),I=1,NVAR),(VMODE(I),I=1,NVAR)
READ(IR)DD,MODE,NSTEP,NEGREQ
LINEAR=(NVAR*(NVAR+1))/2
READ(IR)(PMAT(I),I=1,LINEAR)
LINEAR=(NORBS*(NORBS+1))/2
C READ DENSITY MATRIX
READ(10)(PA(I),I=1,LINEAR)
IF(NALPHA.NE.0)READ(10)(PB(I),I=1,LINEAR)
IF(LATOM.NE.0) THEN
READ(IR)((ALPARM(J,I),J=1,3),I=1,NVAR)
READ(IR)JLOOP,X0, X1, X2
IL=IL+1
ENDIF
CLOSE(9)
CLOSE(10)
RETURN
10 WRITE(6,'(//10X,''NO RESTART FILE EXISTS!'')')
STOP
ENDIF
END
SUBROUTINE EFSTR(XPARAM,FUNCT,IHESS,NTIME,ILOOP,IGTHES,MXSTEP,
$IRECLC,IUPD,DMAX,DDMAX,dmin,TOL2,TOTIME,TIME1,TIME2,nvar,
$SCF1,LUPD,ldump,log,rrscal,donr,gnmin)
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
INCLUDE 'SIZES'
DIMENSION XPARAM(*)
C
COMMON /ISTOPE/ AMS(107)
COMMON /LAST / LAST
COMMON /KEYWRD/ KEYWRD
COMMON /TIMEX / TIME0
COMMON /GRADNT/ GRAD(MAXPAR),GNFINA
COMMON /MOLKST/ NUMAT,NAT(NUMATM),NFIRST(NUMATM),NMIDLE(NUMATM),
1 NLAST(NUMATM), NORBS, NELECS,NALPHA,NBETA,
2 NCLOSE,NOPEN,NDUMY,FRACT
COMMON /NUMCAL/ NUMCAL
COMMON /SCFTYP/ EMIN, LIMSCF
COMMON /NLLCOM/ HESS(MAXPAR,MAXPAR),BMAT(MAXPAR,MAXPAR),
*PMAT(MAXPAR)
COMMON/OPTEF/OLDF(MAXPAR),D(MAXPAR),VMODE(MAXPAR),
$U(MAXPAR,MAXPAR),DD,rmin,rmax,omin,xlamd,xlamd0,skal,
$MODE,NSTEP,NEGREQ,IPRNT
DIMENSION IPOW(9)
LOGICAL RESTRT,SCF1,LDUM,LUPD,log,rrscal,donr,gnmin
C ***** Added by Jiro Toyoda at 1994-05-25 *****
LOGICAL LIMSCF
C ***************************** at 1994-05-25 *****
CHARACTER*241 KEYWRD,LINE
CHARACTER CHDOT*1,ZERO*1,NINE*1,CH*1
DATA CHDOT,ZERO,NINE /'.','0','9'/
DATA ICALCN,ZZERO /0,0.D0/
C GET ALL INITIALIZATION DATA
NVAR=ABS(NVAR)
LDUMP=0
ICALCN=NUMCAL
LUPD=(INDEX(KEYWRD,' NOUPD') .EQ. 0)
RESTRT=(INDEX(KEYWRD,'RESTART') .NE. 0)
LOG = INDEX(KEYWRD,'NOLOG').EQ.0
SCF1=(INDEX(KEYWRD,'1SCF') .NE. 0)
NSTEP=0
IHESS=0
LAST=0
NTIME=0
ILOOP=1
IMIN=INDEX(KEYWRD,' EF')
IF(IMIN.NE.0) THEN
MODE=0
IGTHES=0
IUPD =2
NEGREQ=0
ddmax=0.5d0
ENDIF
LIMSCF=.FALSE.
ITS=INDEX(KEYWRD,' TS')
IF(ITS.NE.0) THEN
MODE=1
IGTHES=1
IUPD =1
NEGREQ=1
rmin=0.0d0
rmax=4.0d0
omin=0.8d0
ddmax=0.3d0
ENDIF
rrscal=.false.
I=INDEX(KEYWRD,' RSCAL')
IF(I.NE.0) rrscal=.true.
donr=.true.
I=INDEX(KEYWRD,' NONR')
IF(I.NE.0) donr=.false.
gnmin=.false.
I=INDEX(KEYWRD,' GNMIN')
IF(I.NE.0) gnmin=.true.
IPRNT=0
IP=INDEX(KEYWRD,' PRNT=')
IF(IP.NE.0) IPRNT=READA(KEYWRD,IP)
IF(IPRNT.GT.5)IPRNT=5
IF(IPRNT.LT.0)IPRNT=0
MXSTEP=2000
I=INDEX(KEYWRD,' CYCLES=')
IF(I.NE.0) MXSTEP=READA(KEYWRD,I)
IF (I.NE.0 .AND. MXSTEP.EQ.0 .AND. IP.EQ.0) IPRNT=3
IRECLC=999999
I=INDEX(KEYWRD,' RECALC=')
IF(I.NE.0) IRECLC=READA(KEYWRD,I)
I=INDEX(KEYWRD,' IUPD=')
IF(I.NE.0) IUPD=READA(KEYWRD,I)
I=INDEX(KEYWRD,' MODE=')
IF(I.NE.0) MODE=READA(KEYWRD,I)
DMIN=1.0D-3
I=INDEX(KEYWRD,' DDMIN=')
IF(I.NE.0) DMIN=READA(KEYWRD,I)
DMAX=0.2D0
I=INDEX(KEYWRD,' DMAX=')
IF(I.NE.0) DMAX=READA(KEYWRD,I)
I=INDEX(KEYWRD,' DDMAX=')
IF(I.NE.0) DDMAX=READA(KEYWRD,I)
TOL2=1.D+0
IF(INDEX(KEYWRD,' PREC') .NE. 0) TOL2=5.D-2
I=INDEX(KEYWRD,' GNORM=')
IF(I.NE.0) TOL2=READA(KEYWRD,I)
IF(INDEX(KEYWRD,' LET').EQ.0.AND.TOL2.LT.0.01D0)THEN
WRITE(6,'(/,A)')' GNORM HAS BEEN SET TOO LOW, RESET TO 0
1.01. SPECIFY LET AS KEYWORD TO ALLOW GNORM LESS THAN 0.01'
TOL2=0.01D0
ENDIF
I=INDEX(KEYWRD,' HESS=')
IF(I.NE.0) IGTHES=READA(KEYWRD,I)
I=INDEX(KEYWRD,' RMIN=')
IF(I.NE.0) RMIN=READA(KEYWRD,I)
I=INDEX(KEYWRD,' RMAX=')
IF(I.NE.0) RMAX=READA(KEYWRD,I)
I=INDEX(KEYWRD,' OMIN=')
IF(I.NE.0) OMIN=READA(KEYWRD,I)
TIME1=TIME0
TIME2=TIME1
C DONE WITH ALL INITIALIZING STUFF.
C CHECK THAT OPTIONS REQUESTED ARE RESONABLE
IF(NVAR.GT.(3*NUMAT-6) .and. numat.ge.3)WRITE(6,25)
25 FORMAT(/,'*** WARNING! MORE VARIABLES THAN DEGREES OF FREEDOM',
1/)
IF((ITS.NE.0).AND.(IUPD.EQ.2))THEN
WRITE(6,*)' TS SEARCH AND BFGS UPDATE WILL NOT WORK'
STOP
ENDIF
IF((ITS.NE.0).AND.(IGTHES.EQ.0))THEN
WRITE(6,*)' TS SEARCH REQUIRE BETTER THAN DIAGONAL HESSIAN'
STOP
ENDIF
IF((IGTHES.LT.0).OR.(IGTHES.GT.3))THEN
WRITE(6,*)' UNRECOGNIZED HESS OPTION',IGTHES
STOP
ENDIF
IF((OMIN.LT.0.d0).OR.(OMIN.GT.1.d0))THEN
WRITE(6,*)' OMIN MUST BE BETWEEN 0 AND 1',OMIN
STOP
ENDIF
IF (RESTRT) THEN
C
C RESTORE DATA. I INDICATES (HESSIAN RESTART OR OPTIMIZATION
C RESTART). IF I .GT. 0 THEN HESSIAN RESTART AND I IS LAST
C STEP CALCULATED IN THE HESSIAN. IF I .LE. 0 THEN J (NSTEP)
C IN AN OPTIMIZATION HAS BEEN DONE.
C
IPOW(9)=0
mtmp=mode
CALL EFSAV(TT0,HESS,FUNCT,GRAD,XPARAM,PMAT,I,J,BMAT,IPOW)
mode=mtmp
K=TT0/1000000.D0
TIME0=TIME0-TT0+K*1000000.D0
ILOOP=I
IF (I .GT. 0) THEN
IGTHES=4
NSTEP=J
WRITE(6,'(10X,''RESTARTING HESSIAN AT POINT'',I4)')ILOOP
IF(NSTEP.NE.0)WRITE(6,'(10X,''IN OPTIMIZATION STEP'',I4)'
1)NSTEP
ELSE
NSTEP=J
WRITE(6,'(//10X,''RESTARTING OPTIMIZATION AT STEP'',I4)')
1NSTEP
DO 26 I=1,NVAR
26 GRAD(I)=ZZERO
CALL COMPFG(XPARAM, .TRUE., FUNCT, .TRUE., GRAD, .TRUE.)
ENDIF
ELSE
C NOT A RESTART, WE NEED TO GET THE GRADIENTS
DO 30 I=1,NVAR
30 GRAD(I)=ZZERO
CALL COMPFG(XPARAM, .TRUE., FUNCT, .TRUE., GRAD, .TRUE.)
ENDIF
return
end
SUBROUTINE FORMD(EIGVAL,FX,NVAR,DMAX,
1osmin,ts,lrjk,lorjk,rrscal,donr)
C This version forms geometry step by either pure NR, P-RFO or QA
C algorithm, under the condition that the steplength is less than dmax
IMPLICIT REAL*8(A-H,O-Z)
REAL*8 LAMDA,lamda0
INCLUDE 'SIZES'
logical ts,rscal,frodo1,frodo2,lrjk,lorjk,rrscal,donr
DIMENSION EIGVAL(MAXPAR),FX(MAXPAR)
COMMON/OPTEF/OLDF(MAXPAR),D(MAXPAR),VMODE(MAXPAR),
$U(MAXPAR,MAXPAR),DD,rmin,rmax,omin,xlamd,xlamd0,skal,
$MODE,NSTEP,NEGREQ,IPRNT
DATA ZERO/0.0D0/, HALF/0.5D0/, TWO/2.0D+00/, TOLL/1.0D-8/
DATA STEP/5.0D-02/, TEN/1.0D+1/, ONE/1.0D+0/, BIG/1.0D+3/
DATA FOUR/4.0D+00/
DATA TMTWO/1.0D-2/, TMSIX/1.0D-06/, SFIX/1.0D+01/, EPS/1.0D-12/
C
MAXIT=999
NUMIT=0
SKAL=ONE
rscal=rrscal
it=0
jt=1
if (ts) then
IF(MODE.NE.0) THEN
CALL OVERLP(dmax,osmin,NEWMOD,NVAR,lorjk)
if (lorjk) return
C
C ON RETURN FROM OVERLP, NEWMOD IS THE TS MODE
C
IF(NEWMOD.NE.MODE .and. iprnt.ge.1) WRITE(6,1000) MODE,NEWMOD
1000 FORMAT(5X,'WARNING! MODE SWITCHING. WAS FOLLOWING MODE ',I3,
$ ' NOW FOLLOWING MODE ',I3)
MODE=NEWMOD
IT=MODE
ELSE
IT=1
ENDIF
eigit=eigval(it)
IF (IPRNT.GE.1) THEN
WRITE(6,900)IT,EIGIT
WRITE(6,910)(U(I,IT),I=1,NVAR)
900 FORMAT(/,5X,'TS MODE IS NUMBER',I3,' WITH EIGENVALUE',F9.1,/,
*5X,'AND COMPONENTS',/)
910 FORMAT(5X,8F9.4)
ENDIF
endif
if (it.eq.1) jt=2
eone=eigval(jt)
ssmin=max(abs(eone)*eps,(ten*eps))
ssmax=max(big,abs(eone))
ssmax=ssmax*big
sstoll=toll
d2max=dmax*dmax
c write(6,*)'from formd, eone, ssmin, ssmax, sstoll',
c $eone,ssmin,ssmax,sstoll
C SOLVE ITERATIVELY FOR LAMDA
C INITIAL GUESS FOR LAMDA IS ZERO EXCEPT NOTE THAT
C LAMDA SHOULD BE LESS THAN EIGVAL(1)
C START BY BRACKETING ROOT, THEN HUNT IT DOWN WITH BRUTE FORCE BISECT.
C
frodo1=.false.
frodo2=.false.
LAMDA=ZERO
lamda0=zero
if (ts .and. eigit.lt.zero .and. eone.ge.zero .and. donr) then
if (iprnt.ge.1) then
write(6,*)' ts search, correct hessian, trying pure NR step'
endif
goto 776
endif
if (.not.ts .and. eone.ge.zero .and. donr) then
if (iprnt.ge.1) then
write(6,*)' min search, correct hessian, trying pure NR step'
endif
goto 776
endif
5 if (ts) then
lamda0=eigval(it)+sqrt(eigval(it)**2+four*fx(it)**2)
lamda0=lamda0*half
if (iprnt.ge.1)WRITE(6,1030) LAMDA0
endif
SSTEP = STEP
IF(EONE.LE.ZERO) LAMDA=EONE-SSTEP
IF(EONE.GT.ZERO) SSTEP=EONE
BL = LAMDA - SSTEP
BU = LAMDA + SSTEP*HALF
20 FL = ZERO
FU = ZERO
DO 30 I = 1,NVAR
if (i.eq.it) goto 30
FL = FL + (FX(I)*FX(I))/(BL-EIGVAL(I))
FU = FU + (FX(I)*FX(I))/(BU-EIGVAL(I))
30 CONTINUE
FL = FL - BL
FU = FU - BU
c write(6,*)'bl,bu,fl,fu from brack'
c write(6,668)bl,bu,fl,fu
c668 format(6f20.15)
IF (FL*FU .LT. ZERO) GOTO 40
BL = BL - (EONE-BL)
BU = BU + HALF*(EONE-BU)
IF (BL.LE.-SSMAX) then
BL = -SSMAX
frodo1=.true.
endif
IF (abs(eone-bu).le.ssmin) then
BU = EONE-SSMIN
frodo2=.true.
endif
IF (frodo1.and.frodo2) THEN
WRITE(6,*)'NUMERICAL PROBLEMS IN BRACKETING LAMDA',
$ EONE,BL,BU,FL,FU
write(6,*)' going for fixed step size....'
goto 450
ENDIF
GOTO 20
40 CONTINUE
NCNT = 0
XLAMDA = ZERO
50 CONTINUE
FL = ZERO
FU = ZERO
FM = ZERO
LAMDA = HALF*(BL+BU)
DO 60 I = 1,NVAR
if (i.eq.it) goto 60
FL = FL + (FX(I)*FX(I))/(BL-EIGVAL(I))
FU = FU + (FX(I)*FX(I))/(BU-EIGVAL(I))
FM = FM + (FX(I)*FX(I))/(LAMDA-EIGVAL(I))
60 CONTINUE
FL = FL - BL
FU = FU - BU
FM = FM - LAMDA
c write(6,*)'bl,bu,lamda,fl,fu,fm from search'
c write(6,668)bl,bu,lamda,fl,fu,fm
IF (ABS(XLAMDA-LAMDA).LT.sstoll) GOTO 776
NCNT = NCNT + 1
IF (NCNT.GT.1000) THEN
WRITE(6,*)'TOO MANY ITERATIONS IN LAMDA BISECT',
$ BL,BU,LAMDA,FL,FU
STOP
ENDIF
XLAMDA = LAMDA
IF (FM*FU.LT.ZERO) BL = LAMDA
IF (FM*FL.LT.ZERO) BU = LAMDA
GOTO 50
C
776 if (iprnt.ge.1) WRITE(6,1031) LAMDA
C
C CALCULATE THE STEP
C
DO 310 I=1,NVAR
D(I)=ZERO
310 CONTINUE
DO 330 I=1,NVAR
if (lamda.eq.zero .and. abs(eigval(i)).lt.tmtwo) then
temp=zero
else
TEMP=FX(I)/(LAMDA-EIGVAL(I))
endif
if (i.eq.it) then
TEMP=FX(IT)/(LAMDA0-EIGVAL(IT))
endif
if (iprnt.ge.5) write(6,*)'formd, delta step',i,temp
DO 320 J=1,NVAR
D(J)=D(J)+TEMP*U(J,I)
320 CONTINUE
330 CONTINUE
dd=sqrt(dot(d,d,nvar))
if(lamda.eq.zero .and. lamda0.eq.zero .and.iprnt.ge.1)
1 write(6,777)dd
777 format(1x,'pure NR-step has length',f10.5)
if(lamda.ne.zero .and. lamda0.ne.-lamda .and.iprnt.ge.1)
1write(6,778)dd
778 format(1x,'P-RFO-step has length',f10.5)
if (dd.lt.(dmax+tmsix)) then
xlamd=lamda
xlamd0=lamda0
return
endif
if (lamda.eq.zero .and. lamda0.eq.zero) goto 5
if (rscal) then
SKAL=DMAX/DD
DO 160 I=1,NVAR
D(I)=D(I)*SKAL
160 CONTINUE
DD=SQRT(DOT(D,D,NVAR))
IF(IPRNT.GE.1)WRITE(6,170)SKAL
170 FORMAT(5X,'CALCULATED STEP SIZE TOO LARGE, SCALED WITH',F9.5)
xlamd=lamda
xlamd0=lamda0
return
endif
450 LAMDA=ZERO
frodo1=.false.
frodo2=.false.
SSTEP = STEP
IF(EONE.LE.ZERO) LAMDA=EONE-SSTEP
if (ts .and. -eigit.lt.eone) lamda=-eigit-sstep
IF(EONE.GT.ZERO) SSTEP=EONE
BL = LAMDA - SSTEP
BU = LAMDA + SSTEP*HALF
520 FL = ZERO
FU = ZERO
DO 530 I = 1,NVAR
if (i.eq.it) goto 530
FL = FL + (FX(I)/(BL-EIGVAL(I)))**2
FU = FU + (FX(I)/(BU-EIGVAL(I)))**2
530 CONTINUE
if (ts) then
FL = FL + (FX(IT)/(BL+EIGVAL(IT)))**2
FU = FU + (FX(IT)/(BU+EIGVAL(IT)))**2
endif
FL = FL - d2max
FU = FU - d2max
c write(6,*)'bl,bu,fl,fu from brack2'
c write(6,668)bl,bu,fl,fu
IF (FL*FU .LT. ZERO) GOTO 540
BL = BL - (EONE-BL)
BU = BU + HALF*(EONE-BU)
IF (BL.LE.-SSMAX) then
BL = -SSMAX
frodo1=.true.
endif
IF (abs(eone-bu).le.ssmin) then
BU = EONE-SSMIN
frodo2=.true.
endif
IF (frodo1.and.frodo2) THEN
WRITE(6,*)'NUMERICAL PROBLEMS IN BRACKETING LAMDA',
$ EONE,BL,BU,FL,FU
write(6,*)' going for fixed level shifted NR step...'
c both lamda searches failed, go for fixed level shifted nr
c this is unlikely to produce anything useful, but maybe we're lucky
lamda=eone-sfix
lamda0=eigit+sfix
rscal=.true.
goto 776
ENDIF
GOTO 520
540 CONTINUE
NCNT = 0
XLAMDA = ZERO
550 CONTINUE
FL = ZERO
FU = ZERO
FM = ZERO
LAMDA = HALF*(BL+BU)
DO 560 I = 1,NVAR
if (i.eq.it) goto 560
FL = FL + (FX(I)/(BL-EIGVAL(I)))**2
FU = FU + (FX(I)/(BU-EIGVAL(I)))**2
FM = FM + (FX(I)/(LAMDA-EIGVAL(I)))**2
560 CONTINUE
if (ts) then
FL = FL + (FX(IT)/(BL+EIGVAL(IT)))**2
FU = FU + (FX(IT)/(BU+EIGVAL(IT)))**2
FM = FM + (FX(IT)/(LAMDA+EIGVAL(IT)))**2
endif
FL = FL - d2max
FU = FU - d2max
FM = FM - d2max
c write(6,*)'bl,bu,lamda,fl,fu,fm from search2'
c write(6,668)bl,bu,lamda,fl,fu,fm
IF (ABS(XLAMDA-LAMDA).LT.sstoll) GOTO 570
NCNT = NCNT + 1
IF (NCNT.GT.1000) THEN
WRITE(6,*)'TOO MANY ITERATIONS IN LAMDA BISECT',
$ BL,BU,LAMDA,FL,FU
STOP
ENDIF
XLAMDA = LAMDA
IF (FM*FU.LT.ZERO) BL = LAMDA
IF (FM*FL.LT.ZERO) BU = LAMDA
GOTO 550
C
570 CONTINUE
lamda0=-lamda
rscal=.true.
goto 776
C
1030 FORMAT(1X,'lamda that maximizes along ts modes = ',F15.5)