~jose-soler/siesta/unfolding

« back to all changes in this revision

Viewing changes to Pseudo/atom/Contrib/pseudv.for

  • Committer: Alberto Garcia
  • Date: 2016-01-25 16:00:16 UTC
  • mto: (483.3.1 rel-4.0)
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: albertog@icmab.es-20160125160016-c1qivg1zw06kgyfd
Prepare GPL release

* Include proper headers

* Add Docs/Contributors.txt and NOTICE.txt files.

* Update READMEs and LICENSE files in several directories.

* Remove Pseudo/atom, Util/test-xml

* Remove DOM files from Src/xmlparser

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
C
2
 
      subroutine pseudv(pstype,icorr,ispp,nr,a,b,r,rab,nameat,norb,
3
 
     &                  ncore,no,lo,so,zo,znuc,zel,cdd,cdu,cdc,viod,
4
 
     &                  viou,vid,viu,vod,vou,etot,ev,ek,ep)
5
 
c
6
 
c *************************************************************
7
 
c *                                                           *
8
 
c *     This routine was written by Norman J. Troullier Jr.   *
9
 
c *   Nov. 1989, while at the U. of Minnesota, all            *
10
 
c *   comments concerning this routine should be directed     *
11
 
c *   to him.                                                 *
12
 
c *                                                           *
13
 
c *     troullie@128.101.224.101                              *
14
 
c *     troullie@csfsa.cs.umn.edu                             *
15
 
c *     612 625-0392                                          *
16
 
c *                                                           *
17
 
c *     pseudv generates a pseudopotential using the          *
18
 
c *   scheme of D. Vanderbilt, ref. Physical Review B,        *
19
 
c *   vol. 32, num 12, page 8412.                             *
20
 
c *   The general format of this routine is the same as the   *
21
 
c *   pseudo, pseudk and pseudt routines.  Output/input is    *
22
 
c *   compatible.                                             *
23
 
c *                                                           *
24
 
c *************************************************************
25
 
c
26
 
C     .. Parameters ..
27
 
      double precision zero, deltas, tpfive, one, two
28
 
      parameter (zero=0.D0,deltas=5.D-3,tpfive=2.5D0,one=1.D0,two=2.D0)
29
 
      double precision small, small2, small3, pzfive
30
 
      parameter (small=1.D-32,small2=1.D-8,small3=1.D-16,pzfive=0.05D0)
31
 
      double precision pfive, small4, ai
32
 
      parameter (pfive=0.5D0,small4=1.D-6,ai=2*137.0360411D0)
33
 
      double precision onepf, oneh
34
 
      parameter (onepf=1.5D0,oneh=100.D0)
35
 
      integer lmax, norbmx, nrmax
36
 
      parameter (lmax=4,norbmx=40,nrmax=1000)
37
 
C     ..
38
 
C     .. Scalar Arguments ..
39
 
      double precision a, b, zel, znuc
40
 
      integer ncore, norb, nr, pstype
41
 
      character ispp*1, icorr*2, nameat*2
42
 
C     ..
43
 
C     .. Array Arguments ..
44
 
      double precision cdc(*), cdd(*), cdu(*), ek(*), ep(*), etot(10),
45
 
     &                 ev(*), r(*), rab(*), so(*), vid(*), viod(lmax,*),
46
 
     &                 viou(lmax,*), viu(*), vod(*), vou(*), zo(*)
47
 
      integer lo(*), no(*)
48
 
C     ..
49
 
C     .. Local Scalars ..
50
 
      double precision ac, ag, arp, arpm, b0, b2, b4, bc, c1, c2, c3,
51
 
     &                 cdcp, cfac, cl, coshxr, dcl, delta, dev, devold,
52
 
     &                 ecut, eviae, fcut, gamma, gg, pi, rbnew, rbold,
53
 
     &                 rcfac, rcopf, rextr, rmind, rminu, rrc, rzero,
54
 
     &                 sinhb2, sinhxr, tanb, vap, vapp, viodj, viouj,
55
 
     &                 vp2z, vps, vpsdm, vpsum, vrc, xr, zeld, zelt,
56
 
     &                 zelu, zion, zot, zratio, zval
57
 
      integer i, icore, ifcore, iflag, ifull, ist, j, j3rc, jcut, jrc,
58
 
     &        k, ka, ll, llp, lp, ncp, nextr, noi, npotd, npotu
59
 
      character blank*1, irel*3, nicore*4
60
 
C     ..
61
 
C     .. Local Arrays ..
62
 
      double precision ab(5), aj(3,3), bj(3), coe(5), rc(5), rcut(10),
63
 
     &                 rr(5)
64
 
      integer indd(5), indu(5)
65
 
      character il(5)*1, iray(6)*10, ititle(7)*10
66
 
C     ..
67
 
C     .. External Subroutines ..
68
 
      external difnrl, difrel, dsolv2, etotal, ext, gaussj, polcoe,
69
 
     &         potran, potrv, potrw, velect, wtrans, zedate
70
 
C     ..
71
 
C     .. Intrinsic Functions ..
72
 
      intrinsic abs, atan, cosh, exp, log, sin, sinh, sqrt
73
 
C     ..
74
 
      double precision ar(nrmax), arps(nrmax), br(nrmax), f(nrmax),
75
 
     &                 g(nrmax), v(nrmax), wk1(nrmax), wk2(nrmax),
76
 
     &                 wk3(nrmax), wk4(nrmax), wk5(nrmax), wk6(nrmax),
77
 
     &                 wkb(6*nrmax)
78
 
      integer nops(norbmx)
79
 
C     ..
80
 
C     ..
81
 
C     .. Data statements ..
82
 
c
83
 
      data il/'s', 'p', 'd', 'f', 'g'/
84
 
C     ..
85
 
      do 10 i = 1, 5
86
 
         indd(i) = 0
87
 
         indu(i) = 0
88
 
   10 continue
89
 
      if (ncore .eq. norb) return
90
 
      ifcore = pstype - 1
91
 
      pi = 4*atan(one)
92
 
c
93
 
c  Spin-polarized potentails should be unscreened with
94
 
c  a spin-polarized valence charge.  This was not
95
 
c  done in pseudo and pseudk in earlier versions
96
 
c  of this program.
97
 
c
98
 
      if (ispp .eq. 's') then
99
 
         blank = 's'
100
 
      else
101
 
         blank = ' '
102
 
      end if
103
 
c
104
 
c  read rc(s),rc(p),rc(d),rc(f),cfac,rcfac
105
 
c
106
 
c    cfac is used for the pseudocore - the pseudocore stops where
107
 
c  the core charge density equals cfac times the renormalized
108
 
c  valence charge density (renormalized to make the atom neutral).
109
 
c  If cfac is input as negative, the full core charge is used,
110
 
c  if cfac is input as zero, it is set equal to one.
111
 
c    rcfac is used for the pseudocore cut off radius.  If set
112
 
c  to less then or equal to zero cfac is used.  cfac must be
113
 
c  set to greater then zero.
114
 
c
115
 
      read(5,9000) (rc(i),i=1,4), cfac, rcfac
116
 
 9000 format(6f10.5)
117
 
      if (cfac .eq. zero) cfac = one
118
 
c
119
 
c   Reset vod and vou to zero.  They are here used to store
120
 
c   the pseudo valence charge density.
121
 
c
122
 
      do 20 i = 1, nr
123
 
         vod(i) = zero
124
 
         vou(i) = zero
125
 
   20 continue
126
 
c
127
 
c  Print the heading.
128
 
c
129
 
      write(6,9010) nameat
130
 
 9010 format(//a2,' Pseudopotential Vanderbilt generation',/1x,50('-'),
131
 
     &      //' nl    s    eigenvalue',6x,'rc',4x,6x,'cl',9x,'gamma',7x,
132
 
     &      'delta',/)
133
 
c
134
 
c      start loop over valence orbitals
135
 
c
136
 
      ncp = ncore + 1
137
 
      do 330 i = ncp, norb
138
 
         lp = lo(i) + 1
139
 
         llp = lo(i)*lp
140
 
         if (so(i) .lt. 0.1D0) then
141
 
            if (indd(lp) .ne. 0) then
142
 
               write(6,9020) lp - 1
143
 
               call ext(800+lp)
144
 
            else
145
 
               indd(lp) = i
146
 
            end if
147
 
         else
148
 
            if (indu(lp) .ne. 0) then
149
 
               write(6,9030) lp - 1
150
 
               call ext(810+lp)
151
 
            else
152
 
               indu(lp) = i
153
 
            end if
154
 
         end if
155
 
 9020    format(//
156
 
     &         'error in pseudv - two down spin orbitals of the same ',
157
 
     &         /'angular momentum (',i1,') exist')
158
 
 9030    format(//'error in pseudv - two up spin orbitals of the same ',
159
 
     &         /'angular momentum (',i1,') exist')
160
 
c
161
 
c      find all electron wave function
162
 
c
163
 
         do 30 j = 1, nr
164
 
            ar(j) = zero
165
 
   30    continue
166
 
         if (so(i) .lt. 0.1D0) then
167
 
            do 40 j = 2, nr
168
 
               v(j) = viod(lp,j)/r(j) + vid(j)
169
 
   40       continue
170
 
         else
171
 
            do 50 j = 2, nr
172
 
               v(j) = viou(lp,j)/r(j) + viu(j)
173
 
   50       continue
174
 
         end if
175
 
         if (ispp .ne. 'r') then
176
 
            do 60 j = 2, nr
177
 
               v(j) = v(j) + llp/r(j)**2
178
 
   60       continue
179
 
         end if
180
 
         if (ispp .ne. 'r') then
181
 
            call difnrl(0,i,v,ar,br,nr,a,b,r,rab,norb,no,lo,so,znuc,
182
 
     &                  viod,viou,vid,viu,ev,iflag)
183
 
         else
184
 
            call difrel(0,i,v,ar,br,nr,a,b,r,rab,norb,no,lo,so,znuc,
185
 
     &                  viod,viou,vid,viu,ev)
186
 
         end if
187
 
c
188
 
c  njtj  ***  plotting routines ***
189
 
c  potrw is called to save an usefull number of points
190
 
c  of the wave function to make a plot.  The info is
191
 
c  written to the current plot.dat file.
192
 
c
193
 
         ist = 1
194
 
         if (ar(nr-85) .lt. zero) ist = -1
195
 
         call potrw(ar,r,nr-85,lo(i),1,ist,rc(lp))
196
 
c
197
 
c  njtj  ***  user should adjust for their needs  ***
198
 
c
199
 
c  Find the last zero and extremum.
200
 
c
201
 
         ka = lo(i) + 1
202
 
         if (so(i) .lt. 0.1D0 .and. lo(i) .ne. 0) ka = -lo(i)
203
 
         nextr = no(i) - lo(i)
204
 
         rzero = zero
205
 
         arp = br(2)
206
 
c
207
 
         if (ispp .eq. 'r') then
208
 
            if (so(i) .lt. 0.1D0) then
209
 
               arp = ka*ar(2)/r(2) + (ev(i)-viod(lp,2)/r(2)-vid(2)+
210
 
     &               ai*ai)*br(2)/ai
211
 
            else
212
 
               arp = ka*ar(2)/r(2) + (ev(i)-viou(lp,2)/r(2)-viu(2)+
213
 
     &               ai*ai)*br(2)/ai
214
 
            end if
215
 
         end if
216
 
c
217
 
         do 70 j = 3, nr
218
 
            if (nextr .eq. 0) go to 80
219
 
            if (ar(j-1)*ar(j) .le. zero) rzero = (ar(j)*r(j-1)-
220
 
     &          ar(j-1)*r(j))/(ar(j)-ar(j-1))
221
 
            arpm = arp
222
 
            arp = br(j)
223
 
c
224
 
            if (ispp .eq. 'r') then
225
 
               if (so(i) .lt. 0.1D0) then
226
 
                  arp = ka*ar(j)/r(j) + (ev(i)-viod(lp,j)/r(j)-vid(j)+
227
 
     &                  ai*ai)*br(j)/ai
228
 
               else
229
 
                  arp = ka*ar(j)/r(j) + (ev(i)-viou(lp,j)/r(j)-viu(j)+
230
 
     &                  ai*ai)*br(j)/ai
231
 
               end if
232
 
            end if
233
 
c
234
 
            if (arp*arpm .gt. zero) go to 70
235
 
            rextr = (arp*r(j-1)-arpm*r(j))/(arp-arpm)
236
 
            nextr = nextr - 1
237
 
   70    continue
238
 
c
239
 
c  Check rc, if outside bounds reset.
240
 
c
241
 
   80    continue
242
 
         if (rzero .lt. r(2)) rzero = r(2)
243
 
         if (rc(lp) .gt. rzero .and. rc(lp) .lt. rextr) go to 90
244
 
         if (rc(lp) .ge. zero) write(6,9040) rc(lp), rextr
245
 
 9040    format(/'Warning, the Core radius =',f5.2,
246
 
     &         /' is larger then wave function',' extrema position =',
247
 
     &         f5.2,/)
248
 
         if (rc(lp) .lt. zero) rc(lp) = rzero - rc(lp)*(rextr-rzero)
249
 
c
250
 
c  Find the index for grid point closest to 1.5*rc.
251
 
c  Find the index for 3*rc which is used for matching norms.
252
 
c
253
 
   90    continue
254
 
         rcopf = onepf*rc(lp)
255
 
         do 100 j = 1, nr
256
 
            if (r(j) .le. rcopf) then
257
 
               jrc = j
258
 
            end if
259
 
            if (r(j) .lt. 3*rc(lp)) then
260
 
               j3rc = j
261
 
            end if
262
 
  100    continue
263
 
c
264
 
c  Reset the n quantum numbers.
265
 
c
266
 
         do 110 j = 1, norb
267
 
            nops(j) = 0
268
 
  110    continue
269
 
         nops(i) = lp
270
 
c
271
 
c   Set up potential vl1, first find true potential,
272
 
c   its first and second derivative at rc.  Store new
273
 
c   potential(unscreen it first, screening added back
274
 
c   in dsolv2).
275
 
c
276
 
         if (so(i) .lt. 0.1D0) then
277
 
            vrc = viod(lp,jrc)/r(jrc) + vid(jrc)
278
 
            ab(1) = viod(lp,jrc-2)/r(jrc-2) + vid(jrc-2)
279
 
            ab(2) = viod(lp,jrc-1)/r(jrc-1) + vid(jrc-1)
280
 
            ab(3) = vrc
281
 
            ab(4) = viod(lp,jrc+1)/r(jrc+1) + vid(jrc+1)
282
 
            ab(5) = viod(lp,jrc+2)/r(jrc+2) + vid(jrc+2)
283
 
         else
284
 
            vrc = viou(lp,jrc)/r(jrc) + viu(jrc)
285
 
            ab(1) = viou(lp,jrc-2)/r(jrc-2) + viu(jrc-2)
286
 
            ab(2) = viou(lp,jrc-1)/r(jrc-1) + viu(jrc-1)
287
 
            ab(3) = vrc
288
 
            ab(4) = viou(lp,jrc+1)/r(jrc+1) + viu(jrc+1)
289
 
            ab(5) = viou(lp,jrc+2)/r(jrc+2) + viu(jrc+2)
290
 
         end if
291
 
         rr(1) = r(jrc-2) - r(jrc)
292
 
         rr(2) = r(jrc-1) - r(jrc)
293
 
         rr(3) = zero
294
 
         rr(4) = r(jrc+1) - r(jrc)
295
 
         rr(5) = r(jrc+2) - r(jrc)
296
 
         call polcoe(rr,ab,5,coe)
297
 
         vap = coe(2)
298
 
         vapp = 2*coe(3)
299
 
         bj(1) = vrc
300
 
         bj(2) = vap
301
 
         bj(3) = vapp
302
 
         aj(1,1) = one
303
 
         aj(2,1) = zero
304
 
         aj(3,1) = zero
305
 
         aj(1,2) = r(jrc)**2
306
 
         aj(2,2) = 2*r(jrc)
307
 
         aj(3,2) = 2*one
308
 
         aj(1,3) = r(jrc)**4
309
 
         aj(2,3) = 4*r(jrc)**3
310
 
         aj(3,3) = 12*r(jrc)**2
311
 
         call gaussj(aj,3,3,bj,1,1)
312
 
         b0 = bj(1)
313
 
         b2 = bj(2)
314
 
         b4 = bj(3)
315
 
         if (so(i) .lt. 0.1D0) then
316
 
            do 120 j = 1, jrc
317
 
               viod(lp,j) = ((b0+b2*r(j)**2+b4*r(j)**4)-vid(j))*r(j)
318
 
  120       continue
319
 
         else
320
 
            do 130 j = 1, jrc
321
 
               viou(lp,j) = ((b0+b2*r(j)**2+b4*r(j)**4)-viu(j))*r(j)
322
 
  130       continue
323
 
         end if
324
 
c
325
 
c  Set up the functions f(r/rc) and g(r/rc) and  modify the ionic potential.
326
 
c
327
 
         if (lp .eq. 1) then
328
 
            dcl = sqrt(znuc)
329
 
         else
330
 
            dcl = -2*one*lp*llp
331
 
         end if
332
 
         cl = dcl
333
 
         sinhb2 = (sinh(one))**2
334
 
c
335
 
         do 140 j = 1, nr
336
 
            rrc = r(j)/rc(lp)/onepf
337
 
            f(j) = oneh**(-((sinh(rrc))**2)/sinhb2)
338
 
            if (f(j) .lt. small2) f(j) = zero
339
 
            g(j) = f(j)
340
 
  140    continue
341
 
         if (so(i) .lt. 0.1D0) then
342
 
            do 150 j = 2, nr
343
 
               viod(lp,j) = viod(lp,j) + dcl*f(j)*r(j)
344
 
  150       continue
345
 
         else
346
 
            do 160 j = 2, nr
347
 
               viou(lp,j) = viou(lp,j) + dcl*f(j)*r(j)
348
 
  160       continue
349
 
         end if
350
 
         dcl = dcl/2
351
 
c
352
 
c   Start the iteration loop to find cl.
353
 
c
354
 
         eviae = ev(i)
355
 
         devold = zero
356
 
         do 190 j = 1, 100
357
 
            call dsolv2(j,2,blank,ifcore,nr,a,b,r,rab,norb,ncore,nops,
358
 
     &                  lo,so,zo,znuc,cdd,cdu,cdc,viod,viou,vid,viu,ev,
359
 
     &                  ek,ep)
360
 
            dev = eviae - ev(i)
361
 
c
362
 
c    The abs(dev-devold) condition was added to eliminate
363
 
c    division by zero errors in the calculation of
364
 
c    dcl = -dev*dcl / (dev-devold).
365
 
c
366
 
            if ((abs(dev).lt.small2.or.abs(dev-devold).lt.small3) .and.
367
 
     &          j .ne. 1) then
368
 
c
369
 
               go to 200
370
 
c
371
 
            else
372
 
               if (j .gt. 15 .or. abs(dev) .lt. 0.001D0) then
373
 
c
374
 
c  Use newton raphson iteration to change cl.
375
 
c
376
 
                  dcl = -dev*dcl/(dev-devold)
377
 
               else
378
 
                  if (dev*dcl .le. zero) then
379
 
                     dcl = -dcl/4
380
 
                  end if
381
 
               end if
382
 
            end if
383
 
c
384
 
c  Find the new potential.
385
 
c
386
 
            if (so(i) .lt. 0.1D0) then
387
 
               do 170 k = 2, nr
388
 
                  viod(lp,k) = viod(lp,k) + dcl*r(k)*f(k)
389
 
  170          continue
390
 
            else
391
 
               do 180 k = 2, nr
392
 
                  viou(lp,k) = viou(lp,k) + dcl*r(k)*f(k)
393
 
  180          continue
394
 
            end if
395
 
            cl = cl + dcl
396
 
            devold = dev
397
 
  190    continue
398
 
c
399
 
c  End the iteration loop for cl.
400
 
c
401
 
         call ext(820+lp)
402
 
c
403
 
c  Find the new pseudo-wavefunction.
404
 
c
405
 
  200    continue
406
 
         if (so(i) .lt. 0.1D0) then
407
 
            do 210 j = 2, nr
408
 
               v(j) = (viod(lp,j)+llp/r(j))/r(j) + vid(j)
409
 
  210       continue
410
 
         else
411
 
            do 220 j = 2, nr
412
 
               v(j) = (viou(lp,j)+llp/r(j))/r(j) + viu(j)
413
 
  220       continue
414
 
         end if
415
 
         do 230 j = 1, nr
416
 
            arps(j) = zero
417
 
  230    continue
418
 
         call difnrl(0,i,v,arps,br,nr,a,b,r,rab,norb,nops,lo,so,znuc,
419
 
     &               viod,viou,vid,viu,ev,iflag)
420
 
c
421
 
c  Compute yl store in g, store ln(arps) in br.
422
 
c
423
 
         do 240 j = 2, nr
424
 
            g(j) = arps(j)*f(j)
425
 
  240    continue
426
 
         do 250 j = 2, nr
427
 
            br(j) = log(arps(j)+small)
428
 
  250    continue
429
 
c
430
 
c  Compute delta and gamma.
431
 
c
432
 
         gamma = abs(ar(j3rc)/arps(j3rc)+ar(j3rc+1)/arps(j3rc+1))/2
433
 
         ag = zero
434
 
         gg = zero
435
 
         ll = 4
436
 
         do 260 j = 2, nr
437
 
            ag = ag + ll*arps(j)*g(j)*rab(j)
438
 
            gg = gg + ll*g(j)*g(j)*rab(j)
439
 
            ll = 6 - ll
440
 
  260    continue
441
 
         ag = ag/3
442
 
         gg = gg/3
443
 
         delta = sqrt((ag/gg)**2+(one/gamma**2-one)/gg) - ag/gg
444
 
c
445
 
c  Modify the pseudo-wavefunction.
446
 
c
447
 
         do 270 j = 2, nr
448
 
            arps(j) = gamma*arps(j)*(one+delta*f(j))
449
 
  270    continue
450
 
c
451
 
c     Find d(ln(wl)/dr and store in g().  Note the use of additional
452
 
c   given information of the Vanderbilt method, i.e. the use of
453
 
c   d(ln(wl)/dr to improve stability.
454
 
c
455
 
         do 280 j = 4, nr - 2
456
 
            ab(1) = br(j-2)
457
 
            ab(2) = br(j-1)
458
 
            ab(3) = br(j)
459
 
            ab(4) = br(j+1)
460
 
            ab(5) = br(j+2)
461
 
            rr(1) = r(j-2) - r(j)
462
 
            rr(2) = r(j-1) - r(j)
463
 
            rr(3) = zero
464
 
            rr(4) = r(j+1) - r(j)
465
 
            rr(5) = r(j+2) - r(j)
466
 
            call polcoe(rr,ab,5,coe)
467
 
            g(j) = coe(2)
468
 
  280    continue
469
 
         g(nr-1) = g(nr-2)
470
 
         g(nr) = g(nr-2)
471
 
         ab(1) = g(4)
472
 
         ab(2) = g(5)
473
 
         ab(3) = g(6)
474
 
         ab(4) = g(7)
475
 
         ab(5) = g(8)
476
 
         rr(1) = r(4) - r(3)
477
 
         rr(2) = r(5) - r(3)
478
 
         rr(3) = r(6) - r(3)
479
 
         rr(4) = r(7) - r(3)
480
 
         rr(5) = r(8) - r(3)
481
 
         call polcoe(rr,ab,5,coe)
482
 
         g(3) = coe(1)
483
 
         ab(1) = g(3)
484
 
         ab(2) = g(4)
485
 
         ab(3) = g(5)
486
 
         ab(4) = g(6)
487
 
         ab(5) = g(7)
488
 
         rr(1) = r(3) - r(2)
489
 
         rr(2) = r(4) - r(2)
490
 
         rr(3) = r(5) - r(2)
491
 
         rr(4) = r(6) - r(2)
492
 
         rr(5) = r(7) - r(2)
493
 
         call polcoe(rr,ab,5,coe)
494
 
         g(2) = coe(1)
495
 
c
496
 
c   Find constants for inversion.
497
 
c
498
 
         c3 = log(oneh)/onepf/rc(lp)/sinhb2
499
 
         c2 = 2/onepf/rc(lp)*c3
500
 
         c1 = c3**2
501
 
c
502
 
c    Modify potential and find total charge density.
503
 
c
504
 
         if (so(i) .lt. 0.1D0) then
505
 
            do 290 j = 2, nr
506
 
               vod(j) = vod(j) + zo(i)*arps(j)*arps(j)
507
 
  290       continue
508
 
         else
509
 
            do 300 j = 2, nr
510
 
               vou(j) = vou(j) + zo(i)*arps(j)*arps(j)
511
 
  300       continue
512
 
         end if
513
 
         if (so(i) .lt. 0.1D0) then
514
 
            do 310 j = 2, nr
515
 
               xr = two*r(j)/rc(lp)/onepf
516
 
               sinhxr = sinh(xr)
517
 
               coshxr = cosh(xr)
518
 
               viod(lp,j) = viod(lp,j) + delta*f(j)/(one+delta*f(j))*
519
 
     &                      (c1*(sinhxr)**2-c2*coshxr-2*c3*sinhxr*g(j))*
520
 
     &                      r(j)
521
 
  310       continue
522
 
         else
523
 
            do 320 j = 2, nr
524
 
               xr = two*r(j)/rc(lp)/onepf
525
 
               sinhxr = sinh(xr)
526
 
               coshxr = cosh(xr)
527
 
               viou(lp,j) = viou(lp,j) + delta*f(j)/(one+delta*f(j))*
528
 
     &                      (c1*(sinhxr)**2-c2*coshxr-2*c3*sinhxr*g(j))*
529
 
     &                      r(j)
530
 
  320       continue
531
 
         end if
532
 
c
533
 
c  njtj  ***  plotting routines ***
534
 
c  potrw is called to save a usefull number of points
535
 
c  of the pseudowave function to make a plot.  The
536
 
c  info is written to the current plot.dat file.
537
 
c  wtrans is called to fourier transform the the pseudo
538
 
c  wave function and save it to the current plot.dat file.
539
 
c
540
 
         ist = 1
541
 
         if (arps(nr-85) .lt. zero) ist = -1
542
 
         call potrw(arps,r,nr-85,lo(i),0,ist,rc(lp))
543
 
         call wtrans(arps,r,nr,lo(i),ist,wk1,wk2,wk3)
544
 
c
545
 
c  njtj  ***  user should adjust for their needs  ***
546
 
c
547
 
         write(6,9050) nops(i), il(lp), so(i), ev(i), rc(lp), cl,
548
 
     &     gamma, delta
549
 
 9050    format(1x,i1,a1,f6.1,2f12.6,f12.3,2f12.4)
550
 
  330 continue
551
 
c
552
 
c   End the loop over the valence orbitals.
553
 
c
554
 
c    Reset the n quantum numbers to include all valence orbitals.
555
 
c  Compute the ratio between the valence charge present and the
556
 
c  valence charge of a neutral atom.
557
 
c  Transfer pseudo valence charge to charge array
558
 
c
559
 
      zval = zero
560
 
      zratio = zero
561
 
      do 340 i = ncp, norb
562
 
         nops(i) = lo(i) + 1
563
 
         zval = zval + zo(i)
564
 
  340 continue
565
 
      zion = zval + znuc - zel
566
 
      if (zval .ne. zero) zratio = zion/zval
567
 
      vod(1) = zero
568
 
      vou(1) = zero
569
 
      do 350 i = 1, nr
570
 
         cdd(i) = vod(i)
571
 
  350 continue
572
 
      do 360 i = 1, nr
573
 
         cdu(i) = vou(i)
574
 
  360 continue
575
 
c
576
 
c  If a core correction is indicated construct pseudo core charge
577
 
c  cdc(r) = ac*r * sin(bc*r) inside r(icore)
578
 
c  if cfac < 0 or the valence charge is zero the full core is used
579
 
c
580
 
      if (ifcore .ne. 0) then
581
 
         ac = zero
582
 
         bc = zero
583
 
         icore = 1
584
 
         if (cfac .le. zero .or. zratio .eq. zero) then
585
 
            write(6,9060) r(icore), ac, bc
586
 
         else
587
 
            if (rcfac .le. zero) then
588
 
               do 370 i = nr, 2, -1
589
 
                  if (cdc(i) .gt. cfac*zratio*
590
 
     &                (cdd(i)+cdu(i))) go to 390
591
 
  370          continue
592
 
            else
593
 
               do 380 i = nr, 2, -1
594
 
                  if (r(i) .le. rcfac) go to 390
595
 
  380          continue
596
 
            end if
597
 
  390       continue
598
 
            icore = i
599
 
            cdcp = (cdc(icore+1)-cdc(icore))/(r(icore+1)-r(icore))
600
 
            tanb = cdc(icore)/(r(icore)*cdcp-cdc(icore))
601
 
            rbold = tpfive
602
 
            do 410 i = 1, 50
603
 
               rbnew = pi + atan(tanb*rbold)
604
 
               if (abs(rbnew-rbold) .lt. .00001D0) then
605
 
                  bc = rbnew/r(icore)
606
 
                  ac = cdc(icore)/(r(icore)*sin(rbnew))
607
 
                  do 400 j = 1, icore
608
 
                     cdc(j) = ac*r(j)*sin(bc*r(j))
609
 
  400             continue
610
 
                  write(6,9060) r(icore), ac, bc
611
 
c
612
 
                  go to 420
613
 
c
614
 
               else
615
 
                  rbold = rbnew
616
 
               end if
617
 
  410       continue
618
 
            write(6,9070)
619
 
            call ext(830)
620
 
         end if
621
 
      end if
622
 
 9060 format(//' core correction used',/' pseudo core inside r =',f6.3,
623
 
     &      /' ac =',f6.3,' bc =',f6.3,/)
624
 
 9070 format(//' error in pseudv - noncovergence in finding ',
625
 
     &      /'pseudo-core values')
626
 
c
627
 
c  End the pseudo core charge.
628
 
c  Compute the potential due to pseudo valence charge.
629
 
c
630
 
c  njtj  ***  NOTE  ***
631
 
c  Spin-polarized potentails should be unscreend with
632
 
c  spin-polarized valence charge.  This was not
633
 
c  done in pseudo and pseudok in earlier versions
634
 
c  of this program.
635
 
c  njtj  ***  NOTE  ***
636
 
c
637
 
  420 continue
638
 
      if (ispp .eq. 's') then
639
 
         blank = 's'
640
 
      else
641
 
         blank = ' '
642
 
      end if
643
 
      call velect(0,1,icorr,blank,ifcore,nr,r,rab,zval,cdd,cdu,cdc,vod,
644
 
     &            vou,etot)
645
 
c
646
 
c  Construct the ionic pseudopotential and find the cutoff,
647
 
c  ecut should be adjusted to give a reassonable ionic cutoff
648
 
c  radius, but should not alter the pseudopotential, ie.,
649
 
c  the ionic cutoff radius should not be inside the pseudopotential
650
 
c  cutoff radius
651
 
c
652
 
      write(6,9080)
653
 
 9080 format(/)
654
 
      ecut = deltas
655
 
      do 490 i = ncp, norb
656
 
         lp = lo(i) + 1
657
 
         if (so(i) .lt. 0.1D0) then
658
 
            do 430 j = 2, nr
659
 
               viod(lp,j) = viod(lp,j) + (vid(j)-vod(j))*r(j)
660
 
               vp2z = viod(lp,j) + 2*zion
661
 
               if (abs(vp2z) .gt. ecut) jcut = j
662
 
  430       continue
663
 
            rcut(i-ncore) = r(jcut)
664
 
            do 440 j = jcut, nr
665
 
               fcut = exp(-5*(r(j)-r(jcut)))
666
 
               viod(lp,j) = -2*zion + fcut*(viod(lp,j)+2*zion)
667
 
  440       continue
668
 
            do 450 j = 2, nr
669
 
               v(j) = viod(lp,j)/r(j)
670
 
  450       continue
671
 
         else
672
 
            do 460 j = 2, nr
673
 
               viou(lp,j) = viou(lp,j) + (viu(j)-vou(j))*r(j)
674
 
               vp2z = viou(lp,j) + 2*zion
675
 
               if (abs(vp2z) .gt. ecut) jcut = j
676
 
  460       continue
677
 
            rcut(i-ncore) = r(jcut)
678
 
            do 470 j = jcut, nr
679
 
               fcut = exp(-5*(r(j)-r(jcut)))
680
 
               viou(lp,j) = -2*zion + fcut*(viou(lp,j)+2*zion)
681
 
  470       continue
682
 
            do 480 j = 2, nr
683
 
               v(j) = viou(lp,j)/r(j)
684
 
  480       continue
685
 
         end if
686
 
c
687
 
c  njtj  ***  plotting routines ***
688
 
c
689
 
         call potran(lo(i)+1,v,r,nr,zion,wk1,wk2,wk3)
690
 
         call potrv(v,r,nr-120,lo(i),zion)
691
 
c
692
 
c  njtj  ***  user should adjust for their needs  ***
693
 
c
694
 
  490 continue
695
 
c
696
 
c  njtj  ***  plotting routines ***
697
 
c   The calls to 1)potran take the fourier transform of
698
 
c   the potential and saves it in the current plot.dat file,
699
 
c   2)potrv saves the potential in the current plot.dat file
700
 
c   3)zion is saved to the current plot.dat file wtih a
701
 
c   marker 'zio' for latter plotting
702
 
cag removed
703
 
c      write(3,9090)
704
 
c      write(3,9100) zion
705
 
c 9090 format(1x,'marker zio')
706
 
c 9100 format(2x,f5.2)
707
 
c
708
 
c  njtj  ***  user should adjust for their needs  ***
709
 
c
710
 
c   Convert spin-polarized potentials back to nonspin-polarized
711
 
c   by occupation weight(zo).  Assumes core polarization is
712
 
c   zero, ie. polarization is only a valence effect.
713
 
c
714
 
      if (ispp .eq. 's') then
715
 
         do 520 i = ncp, norb, 2
716
 
            lp = lo(i) + 1
717
 
            zot = zo(i) + zo(i+1)
718
 
            if (zot .ne. zero) then
719
 
               do 500 j = 2, nr
720
 
                  viod(lp,j) = (viod(lp,j)*zo(i)+viou(lp,j)*zo(i+1))/zot
721
 
                  viou(lp,j) = viod(lp,j)
722
 
  500          continue
723
 
            else
724
 
               do 510 j = 2, nr
725
 
                  viod(lp,j) = viod(lp,j)/2 + viou(lp,j)/2
726
 
                  viou(lp,j) = viod(lp,j)
727
 
  510          continue
728
 
            end if
729
 
  520    continue
730
 
      end if
731
 
c
732
 
      do 530 i = 1, nr
733
 
         vid(i) = vod(i)
734
 
         viu(i) = vou(i)
735
 
  530 continue
736
 
c
737
 
c   Test the pseudopotential self consistency.  Spin-polarized
738
 
c   is tested as spin-polarized(since up/down potentials are
739
 
c   now the same)
740
 
c
741
 
      call dsolv2(0,1,blank,ifcore,nr,a,b,r,rab,norb,ncore,nops,lo,so,
742
 
     &            zo,znuc,cdd,cdu,cdc,viod,viou,vid,viu,ev,ek,ep)
743
 
c
744
 
c  Printout the pseudo eigenvalues after cutoff.
745
 
c
746
 
      write(6,9110) (il(lo(i)+1),rcut(i-ncore),i=ncp,norb)
747
 
      write(6,9120) (ev(i),i=ncp,norb)
748
 
 9110 format(//' test of eigenvalues',//' rcut =',8(2x,a1,f7.2))
749
 
 9120 format(' eval =',8(2x,f8.5))
750
 
c
751
 
c  Printout the data for potentials.
752
 
c
753
 
      write(6,9130)
754
 
 9130 format(///' l    vps(0)    vpsmin      at r',/)
755
 
      do 560 i = 1, lmax
756
 
         if (indd(i)+indu(i) .eq. 0) go to 560
757
 
         if (indd(i) .ne. 0) then
758
 
            vpsdm = zero
759
 
            do 540 j = 2, nr
760
 
               if (r(j) .lt. .00001D0) go to 540
761
 
               vps = viod(i,j)/r(j)
762
 
               if (vps .lt. vpsdm) then
763
 
                  vpsdm = vps
764
 
                  rmind = r(j)
765
 
               end if
766
 
  540       continue
767
 
            write(6,9140) il(i), viod(i,2)/r(2), vpsdm, rmind
768
 
         end if
769
 
         if (indu(i) .ne. 0) then
770
 
            vpsum = zero
771
 
            do 550 j = 2, nr
772
 
               if (r(j) .lt. .00001D0) go to 550
773
 
               vps = viou(i,j)/r(j)
774
 
               if (vps .lt. vpsum) then
775
 
                  vpsum = vps
776
 
                  rminu = r(j)
777
 
               end if
778
 
  550       continue
779
 
            write(6,9140) il(i), viou(i,2)/r(2), vpsum, rminu
780
 
         end if
781
 
 9140    format(1x,a1,3f10.3)
782
 
  560 continue
783
 
c
784
 
c   Print out the energies from etotal.
785
 
c
786
 
      call etotal(pstype,one,nameat,norb-ncore,nops(ncp),lo(ncp),
787
 
     &            so(ncp),zo(ncp),etot,ev(ncp),ek(ncp),ep(ncp))
788
 
c
789
 
c  Find the jobname and date, date is a machine
790
 
c  dependent routine and must be chosen/written/
791
 
c  comment in/out in the zedate section.
792
 
c
793
 
      iray(1) = 'atom-lda  '
794
 
      call zedate(iray(2))
795
 
      iray(3) = 'Vanderbilt'
796
 
      iray(4) = ' Pseudo - '
797
 
      iray(5) = 'potential '
798
 
      iray(6) = 'generation'
799
 
c
800
 
c  Encode the title array.
801
 
c
802
 
      do 570 i = 1, 7
803
 
         ititle(i) = '          '
804
 
  570 continue
805
 
      do 580 i = 1, lmax
806
 
         if (indd(i) .eq. 0 .and. indu(i) .eq. 0) go to 580
807
 
         zelu = zero
808
 
         zeld = zero
809
 
         if (indd(i) .ne. 0) then
810
 
            noi = no(indd(i))
811
 
            zeld = zo(indd(i))
812
 
         end if
813
 
         if (indu(i) .ne. 0) then
814
 
            noi = no(indu(i))
815
 
            zelu = zo(indu(i))
816
 
         end if
817
 
         zelt = zeld + zelu
818
 
         if (ispp .ne. 's') then
819
 
            write(ititle(2*i-1),9150) noi, il(i), zelt
820
 
            write(ititle(2*i),9160) ispp, rc(i)
821
 
 9150       format(' ',i1,a1,'(',f5.2,')')
822
 
 9160       format(a1,' rc=',f5.2)
823
 
         else
824
 
            write(ititle(2*i-1),9170) noi, il(i), zeld
825
 
            write(ititle(2*i),9180) zelu, ispp, rc(i)
826
 
 9170       format(i1,a1,'  (',f4.2,',')
827
 
 9180       format(f4.2,')',a1,f4.2)
828
 
         end if
829
 
  580 continue
830
 
c
831
 
c  Construct relativistic sum and difference potentials.
832
 
c
833
 
      if (ispp .eq. 'r') then
834
 
         if (indu(1) .eq. 0) go to 600
835
 
         indd(1) = indu(1)
836
 
         indu(1) = 0
837
 
         do 590 j = 2, nr
838
 
            viod(1,j) = viou(1,j)
839
 
            viou(1,j) = zero
840
 
  590    continue
841
 
  600    continue
842
 
         do 620 i = 2, lmax
843
 
            if (indd(i) .eq. 0 .or. indu(i) .eq. 0) go to 620
844
 
            do 610 j = 2, nr
845
 
               viodj = viod(i,j)
846
 
               viouj = viou(i,j)
847
 
               viod(i,j) = ((i-1)*viodj+i*viouj)/(2*i-1)
848
 
               viou(i,j) = 2*(viouj-viodj)/(2*i-1)
849
 
  610       continue
850
 
  620    continue
851
 
      end if
852
 
c
853
 
c  Determine the number of  potentials.  Coded them as
854
 
c  two digits, where the first digit is the number
855
 
c  of down or sum potentials and the second the number of
856
 
c  up or difference potentials.
857
 
c
858
 
      npotd = 0
859
 
      npotu = 0
860
 
      do 630 i = 1, lmax
861
 
         if (indd(i) .ne. 0) npotd = npotd + 1
862
 
         if (indu(i) .ne. 0) npotu = npotu + 1
863
 
  630 continue
864
 
c
865
 
c  Write the heading to the current pseudo.dat
866
 
c  file (unit=1).
867
 
c
868
 
      ifull = 0
869
 
      if (cfac .le. zero .or. zratio .eq. zero) ifull = 1
870
 
      if (ifcore .eq. 1) then
871
 
         if (ifull .eq. 0) then
872
 
            nicore = 'pcec'
873
 
         else
874
 
            nicore = 'fcec'
875
 
         end if
876
 
      else if (ifcore .eq. 2) then
877
 
         if (ifull .eq. 0) then
878
 
            nicore = 'pche'
879
 
         else
880
 
            nicore = 'fche'
881
 
         end if
882
 
      else
883
 
         nicore = 'nc  '
884
 
      end if
885
 
      if (ispp .eq. 's') then
886
 
         irel = 'isp'
887
 
      else if (ispp .eq. 'r') then
888
 
         irel = 'rel'
889
 
      else
890
 
         irel = 'nrl'
891
 
      end if
892
 
      rewind 1
893
 
      write(1) nameat, icorr, irel, nicore, (iray(i),i=1,6),
894
 
     &  (ititle(i),i=1,7), npotd, npotu, nr - 1, a, b, zion
895
 
      write(1) (r(i),i=2,nr)
896
 
c
897
 
c  Write the potentials to the current pseudo.dat
898
 
c  file (unit=1).
899
 
c
900
 
      do 640 i = 1, lmax
901
 
         if (indd(i) .eq. 0) go to 640
902
 
         write(1) i - 1, (viod(i,j),j=2,nr)
903
 
  640 continue
904
 
      do 650 i = 1, lmax
905
 
         if (indu(i) .eq. 0) go to 650
906
 
         write(1) i - 1, (viou(i,j),j=2,nr)
907
 
  650 continue
908
 
c
909
 
c  Write the charge densities to the current pseudo.dat
910
 
c  file (unit=1).
911
 
c
912
 
      if (ifcore .ne. 1) then
913
 
         write(1) (zero,i=2,nr)
914
 
      else
915
 
         write(1) (cdc(i),i=2,nr)
916
 
      end if
917
 
      write(1) (zratio*(cdd(i)+cdu(i)),i=2,nr)
918
 
c
919
 
      return
920
 
c
921
 
      end