types_enpara.F90 28.4 KB
Newer Older
1 2 3 4 5 6 7
!--------------------------------------------------------------------------------
! Copyright (c) 2016 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------

MODULE m_types_enpara
8 9 10 11 12 13 14 15 16 17 18
  USE m_judft
  IMPLICIT NONE
  PRIVATE
  TYPE t_enpara
     REAL, ALLOCATABLE    :: el0(:,:,:)
     REAL                 :: evac0(2,2)
     REAL, ALLOCATABLE    :: ello0(:,:,:)
     REAL, ALLOCATABLE    :: el1(:,:,:)
     REAL                 :: evac1(2,2)
     REAL, ALLOCATABLE    :: ello1(:,:,:)
     REAL, ALLOCATABLE    :: enmix(:)
19 20 21 22 23
     INTEGER, ALLOCATABLE :: skiplo(:,:)
     LOGICAL, ALLOCATABLE :: lchange(:,:,:)
     LOGICAL, ALLOCATABLE :: lchg_v(:,:)
     LOGICAL, ALLOCATABLE :: llochg(:,:,:)
     REAL                 :: epara_min
24 25 26 27 28 29 30 31 32 33
     LOGICAL              :: ready ! are the enpara's ok for calculation?
     LOGICAL              :: floating !floating energy parameters are relative to potential
     INTEGER,ALLOCATABLE  :: qn_el(:,:,:)    !>if these are .ne.0 they are understood as
     INTEGER,ALLOCATABLE  :: qn_ello(:,:,:)  !>quantum numbers
   CONTAINS
     PROCEDURE :: init
     PROCEDURE :: update
     PROCEDURE :: read
     PROCEDURE :: write
     PROCEDURE :: mix
34
  END TYPE t_enpara
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

  CHARACTER(len=1),PARAMETER,dimension(0:9):: ch=(/'s','p','d','f','g','h','i','j','k','l'/)

  PUBLIC:: t_enpara

CONTAINS
  SUBROUTINE init(this,atoms,jspins)
    USE m_types_setup
    USE m_constants
    CLASS(t_enpara),INTENT(inout):: this
    TYPE(t_atoms),INTENT(IN)     :: atoms
    INTEGER,INTENT(IN)           :: jspins

    INTEGER :: n,i,jsp

    ALLOCATE(this%el0(0:atoms%lmaxd,atoms%ntype,jspins),this%el1(0:atoms%lmaxd,atoms%ntype,jspins))
    ALLOCATE(this%ello0(atoms%nlod,atoms%ntype,jspins),this%ello1(atoms%nlod,atoms%ntype,jspins))
    this%el0=-1E99
    this%ello0=-1E99
    this%evac0=-1E99


    ALLOCATE(this%llochg(atoms%nlod,atoms%ntype,jspins))
    ALLOCATE(this%lchg_v(2,jspins))
    ALLOCATE(this%skiplo(atoms%ntype,jspins))
    ALLOCATE(this%lchange(0:atoms%lmaxd,atoms%ntype,jspins))
    this%llochg=.FALSE.;this%lchg_v=.FALSE.;this%lchange=.FALSE.
    this%skiplo=0
    ALLOCATE(this%enmix(jspins))
    this%enmix=0.0

    this%ready=.FALSE.
    this%floating=.FALSE.

    ALLOCATE(this%qn_el(0:3,atoms%ntype,jspins))
    ALLOCATE(this%qn_ello(atoms%nlod,atoms%ntype,jspins))

    !Set most simple defaults
    DO jsp=1,jspins
       DO n = 1,atoms%ntype
          IF ( atoms%nz(n) < 3 ) THEN
             this%qn_el(0:3,n,jsp) =  (/1,2,3,4/) 
          ELSEIF ( atoms%nz(n) < 11 ) THEN
             this%qn_el(0:3,n,jsp) =  (/2,2,3,4/) 
          ELSEIF ( atoms%nz(n) < 19 ) THEN
             this%qn_el(0:3,n,jsp) =  (/3,3,3,4/) 
          ELSEIF ( atoms%nz(n) < 31 ) THEN
             this%qn_el(0:3,n,jsp) =  (/4,4,3,4/) 
          ELSEIF ( atoms%nz(n) < 37 ) THEN
             this%qn_el(0:3,n,jsp) =  (/4,4,4,4/) 
          ELSEIF ( atoms%nz(n) < 49 ) THEN
             this%qn_el(0:3,n,jsp) =  (/5,5,4,4/) 
          ELSEIF ( atoms%nz(n) < 55 ) THEN
             this%qn_el(0:3,n,jsp) =  (/5,5,5,4/) 
          ELSEIF ( atoms%nz(n) < 72 ) THEN
             this%qn_el(0:3,n,jsp) =  (/6,6,5,4/) 
          ELSEIF ( atoms%nz(n) < 81 ) THEN
             this%qn_el(0:3,n,jsp) =  (/6,6,5,5/) 
          ELSEIF ( atoms%nz(n) < 87 ) THEN
             this%qn_el(0:3,n,jsp) =  (/6,6,6,5/) 
          ELSE
             this%qn_el(0:3,n,jsp) =  (/7,7,6,5/) 
          ENDIF
          
          DO i = 1, atoms%nlo(n)
             this%qn_ello(i,n,jsp) = this%qn_el(atoms%llo(i,n),n,jsp) - 1
             this%skiplo(n,jsp) = this%skiplo(n,jsp) + (2*atoms%llo(i,n)+1)
          ENDDO
          this%evac0=eVac0Default_const
       ENDDO
    ENDDO

  END SUBROUTINE init

  !> This subroutine adjusts the energy parameters to the potential. In particular, it
  !! calculated them in case of qn_el>-1,qn_ello>-1
  !! Before this was done in lodpot.F
  SUBROUTINE update(enpara,mpi,atoms,vacuum,input,v)
    USE m_types_setup
    USE m_types_mpi
    USE m_xmlOutput
    USE m_types_potden
    CLASS(t_enpara),INTENT(inout):: enpara
    TYPE(t_mpi),INTENT(IN)      :: mpi
    TYPE(t_atoms),INTENT(IN)    :: atoms
    TYPE(t_input),INTENT(IN)    :: input
    TYPE(t_vacuum),INTENT(IN)   :: vacuum
    TYPE(t_potden),INTENT(IN)   :: v


    LOGICAL ::  l_done(0:atoms%lmaxd,atoms%ntype,input%jspins)
    LOGICAL ::  lo_done(atoms%nlod,atoms%ntype,input%jspins)
    REAL    ::  vbar,vz0,rj
    INTEGER ::  n,jsp,l,ilo,j,ivac
    CHARACTER(LEN=20)    :: attributes(5)

    IF (mpi%irank  == 0) CALL openXMLElement('energyParameters',(/'units'/),(/'Htr'/))

    l_done = .FALSE.;lo_done=.FALSE.
    DO jsp = 1,input%jspins
       !$OMP PARALLEL DO DEFAULT(none) &
       !$OMP SHARED(atoms,enpara,jsp,l_done,mpi,v,lo_done) &
       !$OMP PRIVATE(n,l,ilo)
       !! First calculate energy paramter from quantum numbers if these are given...
       !! l_done stores the index of those energy parameter updated
       DO n = 1, atoms%ntype
          DO l = 0,3
             IF( enpara%qn_el(l,n,jsp) >0)THEN 
                l_done(l,n,jsp) = .TRUE.
                enpara%el0(l,n,jsp)=priv_method1(.FALSE.,l,n,enpara%qn_el(l,n,jsp),atoms,mpi,v%mt(:,0,n,jsp))
                IF( l .EQ. 3 ) THEN
                   enpara%el0(4:,n,jsp) = enpara%el0(3,n,jsp)
                   l_done(4:,n,jsp) = .TRUE.
                END IF
             ELSE IF( enpara%qn_el(l,n,jsp)<0 ) THEN
                l_done(l,n,jsp) = .TRUE.
                enpara%el0(l,n,jsp)=priv_method2(.FALSE.,l,n,enpara%qn_el(l,n,jsp),atoms,mpi,v%mt(:,0,n,jsp))
                IF( l .EQ. 3 ) THEN
                   enpara%el0(4:,n,jsp) = enpara%el0(3,n,jsp)
                   l_done(4:,n,jsp) = .TRUE.
                END IF
             ELSE 
                l_done(l,n,jsp) = .FALSE.
             END IF
          ENDDO ! l
          ! Now for the lo's
          DO ilo = 1, atoms%nlo(n)
             l = atoms%llo(ilo,n)
             IF( enpara%qn_ello(ilo,n,jsp) >0) THEN
                lo_done(ilo,n,jsp) = .TRUE.
                enpara%ello0(ilo,n,jsp)=priv_method1(.TRUE.,l,n,enpara%qn_ello(l,n,jsp),atoms,mpi,v%mt(:,0,n,jsp))
             ELSE IF(enpara%qn_ello(ilo,n,jsp)<0) THEN
                lo_done(ilo,n,jsp) = .TRUE.
                enpara%ello0(ilo,n,jsp) = priv_method2(.TRUE.,l,n,enpara%qn_ello(l,n,jsp),atoms,mpi,v%mt(:,0,n,jsp))                
             ELSE
                lo_done(ilo,n,jsp) = .FALSE.
             ENDIF
          ENDDO
       ENDDO ! n
       !$OMP END PARALLEL DO

       !!   Now check for floating energy parameters (not for those with l_done=T)
       IF (enpara%floating) THEN
          types_loop: DO n = 1,atoms%ntype 
             !
             !--->    determine energy parameters if lepr=1. the reference energy
             !--->    is the value of the l=0 potential at approximately rmt/4.
             !
             j = atoms%jri(n) - (LOG(4.0)/atoms%dx(n)+1.51)
             rj = atoms%rmt(n)*EXP(atoms%dx(n)* (j-atoms%jri(n)))
             vbar = v%mt(j,0,n,jsp)/rj
             IF (mpi%irank.EQ.0) THEN
                WRITE ( 6,'('' spin'',i2,'', atom type'',i3,'' ='',f12.6,''   r='',f8.5)') jsp,n,vbar,rj
                WRITE (16,'('' spin'',i2,'', atom type'',i3,'' ='',f12.6,''   r='',f8.5)') jsp,n,vbar,rj
             ENDIF
             DO l = 0,atoms%lmax(n)
                IF ( .NOT.l_done(l,n,jsp) ) THEN
                   enpara%el0(l,n,jsp) = vbar + enpara%el0(l,n,jsp)
                END IF
             ENDDO
             IF (atoms%nlo(n).GE.1) THEN
                DO ilo = 1,atoms%nlo(n)
                   IF ( .NOT. lo_done(ilo,n,jsp) ) THEN
                      enpara%ello0(ilo,n,jsp) = vbar + enpara%ello0(ilo,n,jsp)
                      !+apw+lo
                      IF (atoms%l_dulo(ilo,n)) THEN
                         enpara%ello0(ilo,n,jsp) = enpara%el0(atoms%llo(ilo,n),n,jsp)
                      ENDIF
                      !-apw+lo
                   END IF
                END DO
             ENDIF
          END DO types_loop
       ENDIF
       IF (input%film) THEN
          !
          !--->    vacuum energy parameters: for floating: relative to potential
          !--->    at vacuum-interstitial interface (better for electric field)
          !
          DO ivac = 1,vacuum%nvac
             vz0 = 0.0
             IF (enpara%floating) THEN
                vz0 = v%vacz(1,ivac,jsp)
                IF (mpi%irank.EQ.0) THEN
                   WRITE ( 6,'('' spin'',i2,'', vacuum   '',i3,'' ='',f12.6)') jsp,ivac,vz0 
                   WRITE (16,'('' spin'',i2,'', vacuum   '',i3,'' ='',f12.6)') jsp,ivac,vz0
                ENDIF
             ENDIF
             enpara%evac0(ivac,jsp) = enpara%evac0(ivac,jsp) + vz0
             IF (input%l_inpXML) THEN
                enpara%evac0(ivac,jsp) = v%vacz(vacuum%nmz,ivac,jsp) + enpara%evac0(ivac,jsp)
             END IF
             IF (mpi%irank.EQ.0) THEN
                attributes = ''
                WRITE(attributes(1),'(i0)') ivac
                WRITE(attributes(2),'(i0)') jsp
                WRITE(attributes(3),'(f16.10)') v%vacz(1,ivac,jsp)
                WRITE(attributes(4),'(f16.10)') v%vacz(vacuum%nmz,ivac,jsp)
                WRITE(attributes(5),'(f16.10)') enpara%evac0(ivac,jsp)
                CALL writeXMLElementForm('vacuumEP',(/'vacuum','spin  ','vzIR  ','vzInf ','value '/),&
                     attributes(1:5),RESHAPE((/6+4,4,4,5,5+13,8,1,16,16,16/),(/5,2/)))
             END IF
          ENDDO
          IF (vacuum%nvac.EQ.1) THEN
             enpara%evac0(2,jsp) = enpara%evac0(1,jsp)
          END IF
       END IF
    END DO

    enpara%ready=(ALL(enpara%el0>-1E99).AND.ALL(enpara%ello0>-1E99))
    enpara%epara_min=MIN(MINVAL(enpara%el0),MINVAL(enpara%ello0))
    
    IF (mpi%irank  == 0) CALL closeXMLElement('energyParameters')
  END SUBROUTINE update

  REAL FUNCTION priv_method1(lo,l,n,nqn,atoms,mpi,vr)RESULT(e)
    USE m_types_setup
    USE m_types_mpi
    USE m_radsra
    USE m_differ
    USE m_xmlOutput
    USE m_constants
    IMPLICIT NONE
    LOGICAL,INTENT(IN):: lo
    INTEGER,INTENT(IN):: l,n,nqn
    TYPE(t_atoms),INTENT(IN)::atoms
    TYPE(t_mpi),INTENT(IN)  ::mpi
    REAL,INTENT(IN):: vr(:)


    INTEGER jsp,j,ilo,i
    INTEGER nodeu,node,ierr,msh
    REAL   e_up,e_lo,lnd
    REAL   d,rn,fl,fn,fj,t2,rr,t1,ldmt,us,dus,c
    LOGICAL start
    !     ..
    !     .. Local Arrays .. 
    REAL, ALLOCATABLE :: f(:,:),vrd(:)
    CHARACTER(LEN=20)    :: attributes(6)
    c=c_light(1.0)

    !Core potential setup done for each n,l now 
    d = EXP(atoms%dx(n))
    ! set up core-mesh
    rn = atoms%rmt(n)
    msh = atoms%jri(n)
    DO WHILE (rn < atoms%rmt(n) + 20.0)
       msh = msh + 1
       rn = rn*d
    ENDDO
    rn = atoms%rmsh(1,n)*( d**(msh-1) )
    ALLOCATE ( f(msh,2),vrd(msh) )
    ! extend core potential (linear with slope t1 / a.u.)
    vrd(:atoms%jri(n))=vr(:atoms%jri(n))
    t1=0.125
    t2 = vrd(atoms%jri(n))/atoms%rmt(n) - atoms%rmt(n)*t1
    rr = atoms%rmt(n)
    DO j = atoms%jri(n) + 1, msh
       rr = d*rr
       vrd(j) = rr*( t2 + rr*t1 )
    ENDDO

    node = nqn - (l+1)
    e = 0.0 
    ! determine upper edge
    nodeu = -1 ; start = .TRUE.
    DO WHILE ( nodeu <= node ) 
       CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
            atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
       IF  ( ( nodeu > node ) .AND. start ) THEN
          e = e - 1.0
          nodeu = -1
       ELSE
          e = e + 0.01
          start = .FALSE.
       ENDIF
    ENDDO

    e_up = e
    IF (node /= 0) THEN
       ! determine lower edge
       nodeu = node + 1
       DO WHILE ( nodeu >= node ) 
          CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
               atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
          e = e - 0.01
       ENDDO
       e_lo = e
    ELSE
       e_lo = -9.99 
    ENDIF
    ! calculate core
    e  = (e_up+e_lo)/2
    fn = REAL(nqn) ; fl = REAL(l) ; fj = fl + 0.5
    CALL differ(fn,fl,fj,c,atoms%zatom(n),atoms%dx(n),atoms%rmsh(1,n),&
         rn,d,msh,vrd, e, f(:,1),f(:,2),ierr)
    IF (mpi%irank  == 0) THEN
       attributes = ''
       WRITE(attributes(1),'(i0)') n
       WRITE(attributes(2),'(i0)') jsp
       WRITE(attributes(3),'(i0,a1)') nqn, ch(l)
       WRITE(attributes(4),'(f8.2)') e_lo
       WRITE(attributes(5),'(f8.2)') e_up
       WRITE(attributes(6),'(f16.10)') e
       IF (lo) THEN
          CALL writeXMLElementForm('loAtomicEP',(/'atomType     ','spin         ','branch       ',&
               'branchLowest ','branchHighest','value        '/),&
               attributes,RESHAPE((/10,4,6,12,13,5,6,1,3,8,8,16/),(/6,2/)))
       ELSE
          CALL writeXMLElementForm('atomicEP',(/'atomType     ','spin         ','branch       ',&
               'branchLowest ','branchHighest','value        '/),&
               attributes,RESHAPE((/12,4,6,12,13,5,6,1,3,8,8,16/),(/6,2/)))
       ENDIF
       WRITE(6,'(a6,i3,i2,a1,a12,f6.2,a3,f6.2,a13,f8.4)') '  Atom',n,nqn,ch(l),' branch from',&
            e_lo, ' to',e_up,' htr. ; e_l =',e
    ENDIF
  END FUNCTION priv_method1

  REAL FUNCTION priv_method2(lo,l,n,nqn,atoms,mpi,vr)RESULT(e)
    USE m_types_setup
    USE m_types_mpi
    USE m_radsra
    USE m_differ
    USE m_xmlOutput
    USE m_constants
    IMPLICIT NONE
    LOGICAL,INTENT(IN):: lo
    INTEGER,INTENT(IN):: l,n,nqn
    TYPE(t_atoms),INTENT(IN)::atoms
    TYPE(t_mpi),INTENT(IN)  ::mpi
    REAL,INTENT(IN):: vr(:)

    INTEGER jsp,j,ilo,i
    INTEGER nodeu,node,ierr,msh
    REAL   e_up,e_lo,lnd,e_up_temp,e_lo_temp,large_e_step
    REAL   d,rn,fl,fn,fj,t2,rr,t1,ldmt,us,dus,c
    LOGICAL start
    !     ..
    !     .. Local Arrays .. 

    REAL, ALLOCATABLE :: f(:,:),vrd(:)
    CHARACTER(LEN=20)    :: attributes(6)
    
    c=c_light(1.0)

    !Core potential setup done for each n,l now 
    d = EXP(atoms%dx(n))
    ! set up core-mesh
    rn = atoms%rmt(n)
    msh = atoms%jri(n)
    DO WHILE (rn < atoms%rmt(n) + 20.0)
       msh = msh + 1
       rn = rn*d
    ENDDO
    rn = atoms%rmsh(1,n)*( d**(msh-1) )
    ALLOCATE ( f(msh,2),vrd(msh) )
    ! extend core potential (linear with slope t1 / a.u.)
    vrd(:atoms%jri(n))=vr(:atoms%jri(n))
    t1=0.125
    t2 = vrd(atoms%jri(n))/atoms%rmt(n) - atoms%rmt(n)*t1
    rr = atoms%rmt(n)
    DO j = atoms%jri(n) + 1, msh
       rr = d*rr
       vrd(j) = rr*( t2 + rr*t1 )
    ENDDO
    ! search for branches
    node = ABS(nqn) - (l+1)
    e = 0.0 ! The initial value of e is arbitrary.
    large_e_step = 5.0 ! 5.0 Htr steps for coarse energy searches

    ! determine upper band edge
    ! Step 1: Coarse search for the band edge
    CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
         atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
    DO WHILE ( nodeu > node )
       e = e - large_e_step
       CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
            atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
    END DO
    DO WHILE ( nodeu <= node )
       e = e + large_e_step
       CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
            atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
    END DO
    e_up_temp = e
    e_lo_temp = e - large_e_step
    ! Step 2: Fine band edge determination by bisection search
    DO WHILE ((e_up_temp - e_lo_temp) > 1e-2)
       e = (e_up_temp + e_lo_temp) / 2.0
       CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
            atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
       IF (nodeu > node) THEN
          e_up_temp = e
       ELSE
          e_lo_temp = e
       END IF
    END DO
    e_up = (e_up_temp + e_lo_temp) / 2.0
    e    = e_up

    ! determine lower band edge
    IF (node == 0) THEN
       e_lo = -49.99
    ELSE
       ! Step 1: Coarse search for the band edge
       nodeu = node
       DO WHILE ( nodeu >= node )
          e = e - large_e_step
          CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
               atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
       ENDDO
       e_up_temp = e + large_e_step
       e_lo_temp = e
       ! Step 2: Fine band edge determination by bisection search
       DO WHILE ((e_up_temp - e_lo_temp) > 1e-2)
          e = (e_up_temp + e_lo_temp) / 2.0
          CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
               atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
          IF (nodeu < node) THEN
             e_lo_temp = e
          ELSE
             e_up_temp = e
          END IF
       END DO
       e_lo = (e_up_temp + e_lo_temp) / 2.0
    END IF


    ! determince notches by intersection
    ldmt= -99.0 !ldmt = logarithmic derivative @ MT boundary
    lnd = -l-1
    DO WHILE ( ABS(ldmt-lnd) .GE. 1E-07) 
       e = (e_up+e_lo)/2
       CALL radsra(e,l,vr(:),atoms%rmsh(1,n),&
            atoms%dx(n),atoms%jri(n),c, us,dus,nodeu,f(:,1),f(:,2))
       ldmt = dus/us
       IF( ldmt .GT. lnd) THEN
          e_lo = e
       ELSE IF( ldmt .LT. lnd ) THEN
          e_up = e
          e_lo = e_lo
       END IF
    END DO

    IF (mpi%irank == 0) THEN
       attributes = ''
       WRITE(attributes(1),'(i0)') n
       WRITE(attributes(2),'(i0)') jsp
       WRITE(attributes(3),'(i0,a1)') ABS(nqn), ch(l)
       WRITE(attributes(4),'(f16.10)') ldmt
       WRITE(attributes(5),'(f16.10)') e
       IF (lo) THEN
          CALL writeXMLElementForm('heloAtomicEP',(/'atomType      ','spin          ','branch        ',&
               'logDerivMT    ','value         '/),&
               attributes(1:5),reshape((/8,4,6,12,5+17,6,1,3,16,16/),(/5,2/)))
       ELSE
          CALL writeXMLElementForm('heAtomicEP',(/'atomType      ','spin          ','branch        ',&
               'logDerivMT    ','value         '/),&
               attributes(1:5),reshape((/10,4,6,12,5+17,6,1,3,16,16/),(/5,2/)))
       ENDIF
       WRITE (6,'(a7,i3,i2,a1,a12,f7.2,a4,f7.2,a5)') "  Atom ",n,nqn,ch(l)," branch, D = ",&
            ldmt, " at ",e," htr."
    ENDIF
  END FUNCTION priv_method2

  SUBROUTINE READ(enpara,atoms,jspins,film,l_required)
    USE m_types_setup
    IMPLICIT NONE
    CLASS(t_enpara),INTENT(INOUT):: enpara
    INTEGER, INTENT (IN)        :: jspins
    TYPE(t_atoms),INTENT(IN)    :: atoms
    LOGICAL,INTENT(IN)          :: film,l_required

    INTEGER :: n,l,lo,skip_t,io_err,jsp
    logical :: l_exist

    INQUIRE(file="enpara",exist=l_exist)
    IF (.NOT.l_exist.AND.l_required) CALL juDFT_error("No enpara file found")
    IF (.NOT.l_exist) RETURN

    OPEN(40,file="enpara",form="formatted",status="old")

    DO jsp=1,jspins

       !-->  first line contains mixing parameter!

       READ (40,FMT ='(48x,f10.6)',END=200) enpara%enmix(jsp)
       READ (40,*)                       ! skip next line
       IF (enpara%enmix(jsp).EQ.0.0) enpara%enmix(jsp) = 1.0
       WRITE (6,FMT=8001) jsp
       WRITE (6,FMT=8000)
       skip_t = 0
       DO n = 1,atoms%ntype
          READ (40,FMT=8040,END=200) (enpara%el0(l,n,jsp),l=0,3),&
               (enpara%lchange(l,n,jsp),l=0,3),enpara%skiplo(n,jsp)    
          WRITE (6,FMT=8140) n,(enpara%el0(l,n,jsp),l=0,3),&
               (enpara%lchange(l,n,jsp),l=0,3),enpara%skiplo(n,jsp)    
          !
          !--->    energy parameters for the local orbitals
          !
          IF (atoms%nlo(n).GE.1) THEN
             skip_t = skip_t + enpara%skiplo(n,jsp) * atoms%neq(n)
             READ (40,FMT=8039,END=200)  (enpara%ello0(lo,n,jsp),lo=1,atoms%nlo(n))
             READ (40,FMT=8038,END=200) (enpara%llochg(lo,n,jsp),lo=1,atoms%nlo(n))
             WRITE (6,FMT=8139)          (enpara%ello0(lo,n,jsp),lo=1,atoms%nlo(n))
             WRITE (6,FMT=8138)         (enpara%llochg(lo,n,jsp),lo=1,atoms%nlo(n))
          ELSEIF (enpara%skiplo(n,jsp).GT.0) THEN
             WRITE (6,*) "for atom",n," no LO's were specified"
             WRITE (6,*) 'but skiplo was set to',enpara%skiplo 
             CALL juDFT_error("No LO's but skiplo",calledby ="enpara",&
                  hint="If no LO's are set skiplo must be 0 in enpara")
          END IF
          !Integer values mean we have to set the qn-arrays
          enpara%qn_el(:,n,jsp)=0
          DO l=0,3
             IF (enpara%el0(l,n,jsp)==NINT(enpara%el0(l,n,jsp))) enpara%qn_el(l,n,jsp)=NINT(enpara%el0(l,n,jsp))
          ENDDO
          enpara%qn_ello(:,n,jsp)=0
          DO l=1,atoms%nlo(n)
             IF (enpara%ello0(l,n,jsp)==NINT(enpara%ello0(l,n,jsp))) enpara%qn_ello(l,n,jsp)=NINT(enpara%ello0(l,n,jsp))
          ENDDO
          !
          !--->    set the energy parameters with l>3 to the value of l=3
          !
          enpara%el0(4:,n,jsp) = enpara%el0(3,n,jsp)
       ENDDO   ! atoms%ntype

       IF (film) THEN
          enpara%lchg_v = .TRUE.
          READ (40,FMT=8050,END=200) enpara%evac0(1,jsp),enpara%lchg_v(1,jsp),enpara%evac0(2,jsp)
          WRITE (6,FMT=8150)         enpara%evac0(1,jsp),enpara%lchg_v(1,jsp),enpara%evac0(2,jsp)
       ENDIF
       IF (atoms%nlod.GE.1) THEN               
          WRITE (6,FMT=8090) jsp,skip_t
          WRITE (6,FMT=8091) 
       END IF
    END DO

    CLOSE(40)
    ! input formats

8038 FORMAT (14x,60(l1,8x))
8039 FORMAT (8x,60f9.5)
8040 FORMAT (8x,4f9.5,9x,4l1,9x,i3)
8050 FORMAT (19x,f9.5,9x,l1,15x,f9.5)

    ! output formats

8138 FORMAT (' --> change   ',60(l1,8x))
8139 FORMAT (' --> lo ',60f9.5)
8140 FORMAT (' -->',i3,1x,4f9.5,' change: ',4l1,' skiplo: ',i3)
8150 FORMAT ('  vacuum parameter=',f9.5,' change: ',l1, ' second vacuum=',f9.5)
8001 FORMAT ('READING enpara for spin: ',i1)
8000 FORMAT (/,' energy parameters:',/,t10,'s',t20, 'p',t30,'d',t37,'higher l - - -')
8090 FORMAT ('Spin: ',i1,' -- ',i3,'eigenvalues')
8091 FORMAT ('will be skipped for energyparameter computation')

    RETURN

200 WRITE (6,*) 'the end of the file enpara has been reached while'
    WRITE (6,*) 'reading the energy-parameters.'
    WRITE (6,*) 'possible reason: energy parameters have not been'
    WRITE (6,*) 'specified for all atom types.'
    WRITE (6,FMT='(a,i4)') 'the actual number of atom-types is: ntype=',atoms%ntype
    CALL juDFT_error ("unexpected end of file enpara reached while reading")
  END SUBROUTINE read


  SUBROUTINE WRITE(enpara, atoms,jspins,film)

    ! write enpara-file
    !
    USE m_types_setup
    IMPLICIT NONE
    CLASS(t_enpara),INTENT(IN) :: enpara
    INTEGER, INTENT (IN) :: jspins
    LOGICAL,INTENT(IN)   :: film
    TYPE(t_atoms),INTENT(IN) :: atoms

    INTEGER n,l,lo,jspin

    OPEN(unit=40,file="enpara",form="formatted",status="replace")

    DO jspin=1,jspins
       WRITE (40,FMT=8035) jspin,enpara%enmix(jspin)
       WRITE (40,FMT=8036)
8035   FORMAT (5x,'energy parameters          for spin ',i1,' mix=',f10.6)
8036   FORMAT (t6,'atom',t15,'s',t24,'p',t33,'d',t42,'f')
       DO n = 1,atoms%ntype
          WRITE (6,FMT=8040)  n, (enpara%el0(l,n,jspin),l=0,3),&
               &                          (enpara%lchange(l,n,jspin),l=0,3),enpara%skiplo(n,jspin)
          WRITE (40,FMT=8040) n, (enpara%el0(l,n,jspin),l=0,3),&
               &                          (enpara%lchange(l,n,jspin),l=0,3),enpara%skiplo(n,jspin)
          !--->    energy parameters for the local orbitals
          IF (atoms%nlo(n).GE.1) THEN
             WRITE (6,FMT=8039) (enpara%ello0(lo,n,jspin),lo=1,atoms%nlo(n))
             WRITE (6,FMT=8038) (enpara%llochg(lo,n,jspin),lo=1,atoms%nlo(n))
             WRITE (40,FMT=8039) (enpara%ello0(lo,n,jspin),lo=1,atoms%nlo(n))
             WRITE (40,FMT=8038) (enpara%llochg(lo,n,jspin),lo=1,atoms%nlo(n))
          END IF

       ENDDO
8038   FORMAT (' --> change   ',60(l1,8x))
8039   FORMAT (' --> lo ',60f9.5)
8040   FORMAT (' -->',i3,1x,4f9.5,' change: ',4l1,' skiplo: ',i3)

       IF (film) THEN
          WRITE (40,FMT=8050) enpara%evac0(1,jspin),enpara%lchg_v(1,jspin),enpara%evac0(2,jspin)
          WRITE (6,FMT=8050)  enpara%evac0(1,jspin),enpara%lchg_v(1,jspin),enpara%evac0(2,jspin)
8050      FORMAT ('  vacuum parameter=',f9.5,' change: ',l1,&
               &           ' second vacuum=',f9.5)
       ENDIF
    ENDDO
    CLOSE(40)
    RETURN
  END SUBROUTINE WRITE





  SUBROUTINE mix(enpara,atoms,vacuum,input,vr,vz)
    !------------------------------------------------------------------
    USE m_types_setup
    IMPLICIT NONE
    CLASS(t_enpara),INTENT(INOUT)  :: enpara
    TYPE(t_atoms),INTENT(IN)       :: atoms
    TYPE(t_vacuum),INTENT(IN)      :: vacuum
    TYPE(t_input),INTENT(IN)       :: input

    REAL,    INTENT(IN) :: vr(:,:,:)
    REAL,    INTENT(IN) :: vz(vacuum%nmzd,2)

    INTEGER ityp,j,l,lo,jsp,n
    REAL    vbar,maxdist,maxdist2
    INTEGER same(atoms%nlod)

    maxdist2=0.0
    DO jsp=1,SIZE(enpara%el0,3)
       maxdist=0.0
       DO ityp = 1,atoms%ntype
          !        look for LO's energy parameters equal to the LAPW (and previous LO) ones
          same = 0
          DO lo = 1,atoms%nlo(ityp)
             IF(enpara%el0(atoms%llo(lo,ityp),ityp,jsp).eq.enpara%ello0(lo,ityp,jsp)) same(lo)=-1
             DO l = 1,lo-1
                IF(atoms%llo(l,ityp).ne.atoms%llo(lo,ityp)) cycle
                IF(enpara%ello0(l,ityp,jsp).eq.enpara%ello0(lo,ityp,jsp).and.same(lo).eq.0) same(lo)=l
             ENDDO
          ENDDO
          !
          !--->   change energy parameters
          !
          DO l = 0,3
             WRITE(6,*) 'Type:',ityp,' l:',l
             WRITE(6,FMT=777) enpara%el0(l,ityp,jsp),enpara%el1(l,ityp,jsp),&
                  ABS(enpara%el0(l,ityp,jsp)-enpara%el1(l,ityp,jsp))
             maxdist=MAX(maxdist,ABS(enpara%el0(l,ityp,jsp)-enpara%el1(l,ityp,jsp)))
             IF ( enpara%lchange(l,ityp,jsp) ) THEN
                maxdist2=MAX(maxdist2,ABS(enpara%el0(l,ityp,jsp)-enpara%el1(l,ityp,jsp)))
                enpara%el0(l,ityp,jsp) =(1.0-enpara%enmix(jsp))*enpara%el0(l,ityp,jsp) + &
                     enpara%enmix(jsp)*enpara%el1(0,ityp,jsp)
             ENDIF
          ENDDO
          IF ( enpara%lchange(3,ityp,jsp) ) enpara%el0(4:,ityp,jsp) = enpara%el0(3,ityp,jsp)
          !
          !--->    determine and change local orbital energy parameters
          !
          DO lo = 1,atoms%nlo(ityp)
             IF (atoms%l_dulo(lo,ityp)) THEN
                enpara%ello0(lo,ityp,jsp) =enpara%el0(atoms%llo(lo,ityp),ityp,jsp)
             ELSE
                IF (enpara%llochg(lo,ityp,jsp) ) THEN
                   IF(same(lo).EQ.-1) THEN
                      enpara%ello0(lo,ityp,jsp) = enpara%el0(atoms%llo(lo,ityp),ityp,jsp)
                      CYCLE
                   ELSE IF(same(lo).GT.0) THEN
                      enpara%ello0(lo,ityp,jsp) = enpara%ello0(same(lo),ityp,jsp)
                      CYCLE
                   ENDIF
                ENDIF
                WRITE(6,*) 'Type:',ityp,' lo:',lo
                WRITE(6,FMT=777) enpara%ello0(lo,ityp,jsp),enpara%ello1(lo,ityp,jsp),&
                     ABS(enpara%ello0(lo,ityp,jsp)-enpara%ello1(lo,ityp,jsp))
                maxdist=MAX(maxdist,ABS(enpara%ello0(lo,ityp,jsp)-enpara%ello1(lo,ityp,jsp)))
                IF (enpara%llochg(lo,ityp,jsp) ) THEN
                   maxdist2=MAX(maxdist2,ABS(enpara%ello0(lo,ityp,jsp)-enpara%ello1(lo,ityp,jsp)))
                   enpara%ello0(lo,ityp,jsp) =(1.0-enpara%enmix(jsp))*enpara%ello0(lo,ityp,jsp)+&
                        enpara%enmix(jsp)*enpara%ello1(lo,ityp,jsp)
                ENDIF
             END IF
          END DO
          !Shift if floating energy parameters are used
          IF (enpara%floating) THEN
             j = atoms%jri(ityp) - (LOG(4.0)/atoms%dx(ityp)+1.51)
             vbar = vr(j,ityp,jsp)/( atoms%rmt(ityp)*EXP(atoms%dx(ityp)*(j-atoms%jri(ityp))) )
             enpara%el0(:,n,jsp)=enpara%el0(:,n,jsp)-vbar
          ENDIF
       END DO


       IF (input%film) THEN
          WRITE(6,*) 'Vacuum:'
          DO n=1,vacuum%nvac
             WRITE(6,FMT=777) enpara%evac0(n,jsp),enpara%evac1(n,jsp),ABS(enpara%evac0(n,jsp)-enpara%evac1(n,jsp))
             maxdist=MAX(maxdist,ABS(enpara%evac0(n,jsp)-enpara%evac1(n,jsp)))
             IF (enpara%lchg_v(n,jsp) ) THEN
                maxdist2=MAX(maxdist2,ABS(enpara%evac0(n,jsp)-enpara%evac1(n,jsp)))
                enpara%evac0(n,jsp) =(1.0-enpara%enmix(jsp))*enpara%evac0(n,jsp)+&
                     enpara%enmix(jsp)*enpara%evac1(n,jsp)
             END IF
          END DO
          IF (vacuum%nvac==1) enpara%evac0(2,jsp) = enpara%evac0(1,jsp)
          IF (enpara%floating) enpara%evac0(:,jsp)=enpara%evac0(:,jsp)-vz(:,jsp)
       ENDIF
       WRITE(6,'(a36,f12.6)') 'Max. mismatch of energy parameters:', maxdist
    END DO
    IF (maxdist2>1.0) CALL juDFT_warn&
         ("Energy parameter mismatch too large",hint&
         ="If any energy parameters calculated from the output "//&
         "differ from the input by more than 1Htr, chances are "//&
         "high that your initial setup was broken.")
    RETURN
777 FORMAT('Old:',f8.5,' new:',f8.5,' diff:',f8.5)

  END SUBROUTINE mix


763
END MODULE m_types_enpara