cdnsp.f90 6.89 KB
Newer Older
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
      MODULE m_cdnsp
      USE m_juDFT
!     *******************************************************
!     sets up the starting density for the spin-polarized
!     calculation from a paramagnetic density
!     changed to suit both ferromagnetic and antiferro-
!     magnetic case. changes only in mt-part - r.pentcheva Jan'96
!     *******************************************************
      CONTAINS
        SUBROUTINE cdnsp(&
             &                 atoms,input,vacuum,sphhar,&
             &                 stars,sym,cell,DIMENSION)

          USE m_intgr, ONLY : intgr3
          USE m_constants, ONLY : pi_const
          USE m_loddop
          USE m_wrtdop
          USE m_types
          IMPLICIT NONE
          !     ..
          TYPE(t_stars),INTENT(IN)  :: stars
          TYPE(t_vacuum),INTENT(IN) :: vacuum
          TYPE(t_atoms),INTENT(IN)  :: atoms
          TYPE(t_sphhar),INTENT(IN) :: sphhar
          TYPE(t_input),INTENT(INOUT)  :: input
          TYPE(t_sym),INTENT(IN)    :: sym
          TYPE(t_cell),INTENT(IN)   :: cell
          TYPE(t_dimension),INTENT(IN)::DIMENSION
          !     ..
          !     .. Local Scalars ..
          REAL dummy,p,pp,qtot1,qtot2,spmtot,qval,sfp
          INTEGER i,iter,ivac,j,k,lh,n,na,nt,jsp_new
          INTEGER ios 
          LOGICAL n_exist
          !     ..
          !     .. Local Arrays ..
          REAL rhoc(DIMENSION%msh,atoms%ntype)
          COMPLEX, ALLOCATABLE :: qpw(:,:),rhtxy(:,:,:,:)
          REAL   , ALLOCATABLE :: rho(:,:,:,:),rht(:,:,:)
          CHARACTER(len=140), ALLOCATABLE :: clines(:)
          CHARACTER(len=140)              :: lineread
          !      ..
          sfp = 2 * SQRT(pi_const)
          !sphhar%nlhd = MAXVAL(sphhar%nlh(:))

          IF (input%jspins/=2)  CALL juDFT_error&
               &     ("cdnsp: set jspins = 2 and remove fl7para!",calledby&
               &     ="cdnsp")
          ALLOCATE ( rho(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,input%jspins),qpw(stars%ng3,input%jspins) )
          ALLOCATE ( rhtxy(vacuum%nmzxy,stars%ng2-1,2,input%jspins),rht(vacuum%nmz,2,input%jspins) )

          !
          OPEN (17,file='cdnc',form='unformatted',status='old')
          DO n = 1,atoms%ntype
             READ (17) (rhoc(j,n),j=1,atoms%jri(n))
             READ (17) dummy
          ENDDO
          CLOSE (17)

          nt = 71                   !gs, see sub mix
          OPEN (nt,file='cdn1',form='unformatted',status='old')
          !
          !     ---> set jspins=1 to read the paramagnetic density
          ! 
          input%jspins=1
          CALL loddop(&
               &            stars,vacuum,atoms,sphhar,&
               &            input,sym,&
               &            nt,&
               &            iter,rho,qpw,rht,rhtxy)
          input%jspins=2
          !
          qval = 0.
          na = 1
          !
          !     ---> set jspins=2
          jsp_new = 2
          !
          DO n = 1,atoms%ntype
             DO j = 1,atoms%jri(n)
                rho(j,0,n,1) = rho(j,0,n,1) - rhoc(j,n)/sfp
             ENDDO
             !         WRITE (16,FMT='(8f10.4)') (rho(i,0,n,1),i=1,16)
             CALL intgr3(rho(1,0,n,1),atoms%rmsh(1,n),atoms%dx(n),atoms%jri(n),qval)
             p = (atoms%bmu(n)+sfp*qval)/ (2.*sfp*qval)
             pp = 1. - p
             DO j = 1,atoms%jri(n)
                rho(j,0,n,jsp_new) = pp*rho(j,0,n,1) + rhoc(j,n)/ (2.*sfp)
                rho(j,0,n,1)       =  p*rho(j,0,n,1) + rhoc(j,n)/ (2.*sfp)
             ENDDO
             DO lh = 1,sphhar%nlh(atoms%ntypsy(na))
                DO j = 1,atoms%jri(n)
                   rho(j,lh,n,jsp_new) = pp*rho(j,lh,n,1)
                   rho(j,lh,n,1)       =  p*rho(j,lh,n,1)
                ENDDO
             ENDDO
             na = na + atoms%neq(n)
          ENDDO
          DO k = 1,stars%ng3
             qpw(k,jsp_new) = 0.5 * qpw(k,1)
             qpw(k,1)       = qpw(k,jsp_new)
          ENDDO
          IF (input%film) THEN
             DO ivac = 1,vacuum%nvac
                DO j = 1, vacuum%nmz
                   rht(j,ivac,jsp_new) = 0.5 * rht(j,ivac,1)
                   rht(j,ivac,1)       = rht(j,ivac,jsp_new)
                ENDDO
                DO k = 2, stars%ng2
                   DO j = 1,vacuum%nmzxy
                      rhtxy(j,k-1,ivac,jsp_new) = 0.5 * rhtxy(j,k-1,ivac,1)
                      rhtxy(j,k-1,ivac,1)       = rhtxy(j,k-1,ivac,jsp_new)
                   ENDDO
                ENDDO
             ENDDO
          ENDIF
          !     ----> write the spin-polarized density on unit nt
          REWIND nt
          CALL wrtdop(&
               &            stars,vacuum,atoms,sphhar,&
               &            input,sym,&
               &            nt,&
               &            iter,rho,qpw,rht,rhtxy)
          CLOSE (nt)
          !
          !     -----> This part is only used for testing th e magnetic moment in 
          !     ----->   each sphere
          !
          DO n = 1,atoms%ntype
             qtot1=0.00
             qtot2=0.00
             CALL intgr3(rho(1,0,n,1),atoms%rmsh(1,n),atoms%dx(n),atoms%jri(n),qtot1)
             CALL intgr3(rho(1,0,n,jsp_new),atoms%rmsh(1,n),atoms%dx(n),atoms%jri(n),qtot2)
             spmtot=sfp*(qtot1-qtot2)
             WRITE (6,'('' moment in sphere '',2x,'':'',f8.4)') spmtot
          ENDDO

          !--->   read enpara and then double it
          INQUIRE(file='enpara',exist=n_exist)
          IF (n_exist) THEN
             OPEN(40,file ='enpara',status='old',form='formatted')
             REWIND 40
             n = 0
             DO
                READ (40,'(a)',iostat = ios) lineread
                IF (ios/=0) EXIT
                n          = n+1
             ENDDO

             ALLOCATE (clines(n))

             REWIND 40
             DO i = 1,n
                READ (40,'(a)') clines(i)
             ENDDO

             REWIND 40
             DO i = 1,n
                WRITE (40,'(a)') TRIM(clines(i))
             ENDDO
             DO i = 1,n
                WRITE (40,'(a)') TRIM(clines(i))
             ENDDO

             DEALLOCATE (clines,rho,qpw,rhtxy,rht)
             CLOSE(40)
          ENDIF
          !
          ! for lda+U: flip n-matrix
          !
          IF (atoms%n_u.GT.0) THEN
             INQUIRE (file='n_mmp_mat',exist=n_exist)
             IF (n_exist) THEN
                OPEN (69,file='n_mmp_mat',status='old',form='formatted')
                REWIND 69

                n=0
                DO
                   READ (69,'(a)',iostat=ios) lineread
                   IF (ios.NE.0) EXIT
                   n = n+1
                ENDDO
                ALLOCATE (clines(n))
                REWIND 69
                DO i=1,n
                   WRITE (69,'(a)') TRIM(clines(i))
                ENDDO
                DO i=1,n
                   WRITE (69,'(a)') TRIM(clines(i))
                ENDDO
                DEALLOCATE (clines)

                CLOSE(69)
             ENDIF
          ENDIF


        END SUBROUTINE cdnsp
      END MODULE m_cdnsp