r_inpXML.F90 109 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! 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.
!--------------------------------------------------------------------------------

7
MODULE m_rinpXML
8 9 10 11 12 13 14
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!
!!! The routine r_inpXML reads in the inp.xml file
!!!
!!!                               GM'16
!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15
CONTAINS
16
   SUBROUTINE r_inpXML(&
17
      atoms,obsolete,vacuum,input,stars,sliceplot,banddos,DIMENSION,forcetheo,field,&
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
      cell,sym,xcpot,noco,oneD,hybrid,kpts,enpara,coreSpecInput,wann,&
      noel,namex,relcor,a1,a2,a3,dtild,xmlElectronStates,&
      xmlPrintCoreStates,xmlCoreOccs,atomTypeSpecies,speciesRepAtomType,&
      l_kpts)

      USE iso_c_binding
      USE m_juDFT
      USE m_types
      USE m_types_forcetheo_extended
      USE m_symdata , ONLY : nammap, ord2, l_c2
      USE m_rwsymfile
      USE m_xmlIntWrapFort
      USE m_inv3
      USE m_spg2set
      USE m_closure, ONLY : check_close
      USE m_symproperties
      USE m_calculator
      USE m_constants
      USE m_inpeig
37
      USE m_inpnoco
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
      USE m_sort
      USE m_types_xcpot_inbuild
#ifdef CPP_LIBXC
      USE xc_f03_lib_m
#endif
      IMPLICIT NONE

      TYPE(t_input),INTENT(INOUT)   :: input
      TYPE(t_sym),INTENT(INOUT)     :: sym
      TYPE(t_stars),INTENT(INOUT)   :: stars
      TYPE(t_atoms),INTENT(INOUT)   :: atoms
      TYPE(t_vacuum),INTENT(INOUT)   :: vacuum
      TYPE(t_obsolete),INTENT(INOUT) :: obsolete
      TYPE(t_kpts),INTENT(INOUT)     :: kpts
      TYPE(t_oneD),INTENT(INOUT)     :: oneD
      TYPE(t_hybrid),INTENT(INOUT)   :: hybrid
      TYPE(t_cell),INTENT(INOUT)     :: cell
      TYPE(t_banddos),INTENT(INOUT)  :: banddos
      TYPE(t_sliceplot),INTENT(INOUT):: sliceplot
      CLASS(t_xcpot),INTENT(INOUT),ALLOCATABLE :: xcpot
      TYPE(t_noco),INTENT(INOUT)     :: noco
      TYPE(t_dimension),INTENT(OUT)  :: dimension
      TYPE(t_enpara)   ,INTENT(OUT)  :: enpara
61
      TYPE(t_field), INTENT(INOUT)   :: field
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
      CLASS(t_forcetheo),ALLOCATABLE,INTENT(OUT):: forcetheo
      TYPE(t_coreSpecInput),INTENT(OUT) :: coreSpecInput
      TYPE(t_wann)   ,INTENT(INOUT)  :: wann
      LOGICAL, INTENT(OUT)           :: l_kpts
      INTEGER,          ALLOCATABLE, INTENT(INOUT) :: xmlElectronStates(:,:)
      INTEGER,          ALLOCATABLE, INTENT(INOUT) :: atomTypeSpecies(:)
      INTEGER,          ALLOCATABLE, INTENT(INOUT) :: speciesRepAtomType(:)
      REAL,             ALLOCATABLE, INTENT(INOUT) :: xmlCoreOccs(:,:,:)
      LOGICAL,          ALLOCATABLE, INTENT(INOUT) :: xmlPrintCoreStates(:,:)
      CHARACTER(len=3), ALLOCATABLE, INTENT(INOUT) :: noel(:)
      CHARACTER(len=4), INTENT(OUT)  :: namex
      CHARACTER(len=12), INTENT(OUT) :: relcor
      REAL, INTENT(OUT)              :: a1(3),a2(3),a3(3)
      REAL, INTENT(OUT)              :: dtild

      CHARACTER(len=8) :: name(10)

      !+odim
      INTEGER MM,vM,m_cyl
      LOGICAL invs1,zrfs1
      INTEGER chi,rot
      LOGICAL d1,band
      NAMELIST /odim/ d1,MM,vM,m_cyl,chi,rot,invs1,zrfs1
      !-odim
      ! ..
      ! ..  Local Variables
      REAL     :: scpos  ,zc
      INTEGER ieq,i,k,na,n,ii,id_c,id_x
      REAL s3,ah,a,hs2,rest,thetaj
      LOGICAL l_hyb,l_sym,ldum
      INTEGER :: ierr
      ! ..
      !...  Local Arrays
      !   CHARACTER :: helpchar(atoms%ntype)
      CHARACTER(len=  4) :: chntype
      CHARACTER(len= 41) :: chform
      CHARACTER(len=100) :: line

      CHARACTER(len=20) :: tempNumberString
      CHARACTER(len=150) :: format
      CHARACTER(len=20) :: mixingScheme
      CHARACTER(len=10) :: loType
      LOGICAL :: kptGamma, l_relcor,ldummy
      INTEGER :: iAtomType, startCoreStates, endCoreStates
      CHARACTER(len=100) :: xPosString, yPosString, zPosString
      CHARACTER(len=200) :: coreStatesString
      !   REAL :: tempTaual(3,atoms%nat)
      REAL             :: coreStateOccs(29,2)
      INTEGER          :: coreStateNprnc(29), coreStateKappa(29)
      INTEGER          :: speciesXMLElectronStates(29)
      REAL             :: speciesXMLCoreOccs(2,29)
      LOGICAL          :: speciesXMLPrintCoreStates(29)

      INTEGER            :: iType, iLO, iSpecies, lNumCount, nNumCount, iLLO, jsp, j, l, absSum, numTokens
      INTEGER            :: numberNodes, nodeSum, numSpecies, n2spg, n1, n2, ikpt, iqpt
      INTEGER            :: atomicNumber, coreStates, gridPoints, lmax, lnonsphr, lmaxAPW
      INTEGER            :: latticeDef, symmetryDef, nop48, firstAtomOfType, errorStatus
      INTEGER            :: loEDeriv, ntp1, ios, ntst, jrc, minNeigd, providedCoreStates, providedStates
      INTEGER            :: nv, nv2, kq1, kq2, kq3, nprncTemp, kappaTemp, tempInt
121
      INTEGER            :: ldau_l(4), numVac, numU, ldau_use(4)
122 123 124 125 126
      INTEGER            :: speciesEParams(0:3)
      INTEGER            :: mrotTemp(3,3,48)
      REAL               :: tauTemp(3,48)
      REAL               :: bk(3)
      LOGICAL            :: flipSpin, l_eV, invSym, l_qfix, relaxX, relaxY, relaxZ
127
      LOGICAL            :: coreConfigPresent, l_enpara, l_orbcomp, tempBool, l_nocoinp
128 129 130
      REAL               :: magMom, radius, logIncrement, qsc(3), latticeScale, dr
      REAL               :: aTemp, zp, rmtmax, sumWeight, ldau_u(4), ldau_j(4), tempReal
      REAL               :: weightScale, eParamUp, eParamDown
131
      LOGICAL            :: l_amf(4), l_hia,l_j0,l_U
132 133 134 135 136 137 138 139 140
      REAL, PARAMETER    :: boltzmannConst = 3.1668114e-6 ! value is given in Hartree/Kelvin
      INTEGER            :: lcutm,lcutwf,hybSelect(4)
      REAL               :: evac0Temp(2,2)

      CHARACTER(LEN=200,KIND=c_char) :: schemaFilename, docFilename
      CHARACTER(LEN=255) :: valueString, lString, nString, token
      CHARACTER(LEN=255) :: xPathA, xPathB, xPathC, xPathD, xPathE
      CHARACTER(LEN=11)  :: latticeType
      CHARACTER(LEN=50)  :: versionString
141
      CHARACTER(LEN=150) :: kPointsPrefix
142

143 144
      INTEGER            :: altKPointSetIndex,  altKPointSetIndices(2)
      LOGICAL            :: ldaSpecies
145
      REAL               :: socscaleSpecies,b_field_mtspecies,vcaspecies
146 147 148 149 150 151 152 153 154

      INTEGER, ALLOCATABLE :: lNumbers(:), nNumbers(:), speciesLLO(:)
      INTEGER, ALLOCATABLE :: loOrderList(:)
      INTEGER, ALLOCATABLE :: speciesNLO(:)
      INTEGER, ALLOCATABLE :: multtab(:,:), invOps(:), optype(:)
      INTEGER, ALLOCATABLE :: lmx1(:), nq1(:), nlhtp1(:)
      INTEGER, ALLOCATABLE :: speciesLOEDeriv(:)
      REAL,    ALLOCATABLE :: speciesLOeParams(:), speciesLLOReal(:)
      LOGICAL, ALLOCATABLE :: wannAtomList(:)
155

Daniel Wortmann's avatar
Daniel Wortmann committed
156 157
! Variables for MT radius testing:

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
      REAL                 :: dtild1,kmax1,dvac1
      LOGICAL              :: l_test
      INTEGER, ALLOCATABLE :: jri1(:), lmax1(:)
      REAL, ALLOCATABLE    :: rmt1(:), dx1(:)

      EXTERNAL prp_xcfft_box

      interface
         function dropInputSchema() bind(C, name="dropInputSchema")
            use iso_c_binding
            INTEGER(c_int) dropInputSchema
         end function dropInputSchema
      end interface

      errorStatus = 0
      errorStatus = dropInputSchema()
      IF(errorStatus.NE.0) THEN
         STOP 'Error: Cannot print out FleurInputSchema.xsd'
      END IF

      schemaFilename = "FleurInputSchema.xsd"//C_NULL_CHAR
      docFilename = "inp.xml"//C_NULL_CHAR

      !TODO! these switches should be in the inp-file
      input%l_core_confpot=.true. !former CPP_CORE
      input%l_useapw=.false.   !former CPP_APW
      !WRITE(*,*) 'Start reading of inp.xml file'
      CALL xmlInitInterface()
      CALL xmlParseSchema(schemaFilename)
      CALL xmlParseDoc(docFilename)
      CALL xmlValidateDoc()
      CALL xmlInitXPath()

      ! Check version of inp.xml
      versionString = xmlGetAttributeValue('/fleurInput/@fleurInputVersion')
      IF((TRIM(ADJUSTL(versionString)).NE.'0.27').AND.(TRIM(ADJUSTL(versionString)).NE.'0.28').AND.&
         (TRIM(ADJUSTL(versionString)).NE.'0.29')) THEN
         STOP 'version number of inp.xml file is not compatible with this fleur version'
      END IF

      ! Get number of atoms, atom types, and atom species

      numberNodes = xmlGetNumberOfNodes('/fleurInput/atomGroups/atomGroup/relPos')
      numberNodes = numberNodes + xmlGetNumberOfNodes('/fleurInput/atomGroups/atomGroup/absPos')
      numberNodes = numberNodes + xmlGetNumberOfNodes('/fleurInput/atomGroups/atomGroup/filmPos')

      atoms%nat = numberNodes

      numberNodes = xmlGetNumberOfNodes('/fleurInput/atomGroups/atomGroup')

      atoms%ntype = numberNodes

      numSpecies = xmlGetNumberOfNodes('/fleurInput/atomSpecies/species')

      ALLOCATE(atoms%nz(atoms%ntype))     !nz and zatom have the same content!
      ALLOCATE(atoms%zatom(atoms%ntype))  !nz and zatom have the same content!
      ALLOCATE(atoms%jri(atoms%ntype))
      ALLOCATE(atoms%dx(atoms%ntype))
      ALLOCATE(atoms%lmax(atoms%ntype))
      ALLOCATE(atoms%nlo(atoms%ntype))
      ALLOCATE(atoms%ncst(atoms%ntype))
      ALLOCATE(atoms%lnonsph(atoms%ntype))
      ALLOCATE(atoms%nflip(atoms%ntype))
      ALLOCATE(atoms%l_geo(atoms%ntype))
      ALLOCATE(atoms%lda_u(4*atoms%ntype))
223 224
      ALLOCATE(atoms%lda_hia(4*atoms%ntype))
      ALLOCATE(atoms%j0(4*atoms%ntype))
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
      ALLOCATE(atoms%bmu(atoms%ntype))
      ALLOCATE(atoms%relax(3,atoms%ntype))
      ALLOCATE(atoms%neq(atoms%ntype))
      ALLOCATE(atoms%taual(3,atoms%nat))
      ALLOCATE(atoms%label(atoms%nat))
      ALLOCATE(atoms%pos(3,atoms%nat))
      ALLOCATE(atoms%rmt(atoms%ntype))
      ALLOCATE(atoms%numStatesProvided(atoms%ntype))
      ALLOCATE(atoms%namex(atoms%ntype))
      ALLOCATE(atoms%icorr(atoms%ntype))
      ALLOCATE(atoms%igrd(atoms%ntype))
      ALLOCATE(atoms%krla(atoms%ntype))
      ALLOCATE(atoms%relcor(atoms%ntype))

      atoms%namex = ''
      atoms%icorr = -99

      ALLOCATE(atoms%ncv(atoms%ntype)) ! For what is this?
      ALLOCATE(atoms%ngopr(atoms%nat)) ! For what is this?
      ALLOCATE(atoms%lapw_l(atoms%ntype)) ! Where do I put this?
      ALLOCATE(atoms%invsat(atoms%nat)) ! Where do I put this?

      ALLOCATE(noco%l_relax(atoms%ntype),noco%b_con(2,atoms%ntype))
      ALLOCATE(noco%alphInit(atoms%ntype),noco%alph(atoms%ntype),noco%beta(atoms%ntype))
      ALLOCATE(noco%socscale(atoms%ntype))

      DEALLOCATE(atomTypeSpecies,speciesRepAtomType)
      ALLOCATE(atomTypeSpecies(atoms%ntype))
      ALLOCATE(speciesRepAtomType(numSpecies))
      atomTypeSpecies = -1
      speciesRepAtomType = -1

      DEALLOCATE(xmlElectronStates,xmlPrintCoreStates,xmlCoreOccs)
      ALLOCATE(xmlElectronStates(29,atoms%ntype))
      ALLOCATE(xmlPrintCoreStates(29,atoms%ntype))
      ALLOCATE(xmlCoreOccs(2,29,atoms%ntype))
      xmlElectronStates = noState_const
      xmlPrintCoreStates = .FALSE.
      xmlCoreOccs = 0.0

      ALLOCATE (kpts%ntetra(4,kpts%ntet),kpts%voltet(kpts%ntet))

      ALLOCATE (wannAtomList(atoms%nat))

      ! Read in constants

      xPathA = '/fleurInput/constants/constant'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      DO i = 1, numberNodes
         WRITE(xPathB,*) TRIM(ADJUSTL(xPathA)), '[',i,']'
         tempReal = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB))//'/@value'))
         valueString = xmlGetAttributeValue(TRIM(ADJUSTL(xPathB))//'/@name')
         CALL ASSIGN_var(valueString,tempReal)
      END DO
279

280 281 282 283
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Comment section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

284 285 286 287 288 289 290 291 292 293 294
      input%comment = '        '
      xPathA = '/fleurInput/comment'
      valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA)))))
      DO i = 1, LEN(TRIM(ADJUSTL(valueString)))
         IF (valueString(i:i).EQ.achar(10)) valueString(i:i) = ' ' !remove line breaks
      END DO
      valueString = TRIM(ADJUSTL(valueString))
      DO i = 1, 10
         j = (i-1) * 8 + 1
         input%comment(i) = valueString(j:j+7)
      END DO
295

296 297 298 299
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Start of calculationSetup section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

300 301 302 303 304 305 306 307 308 309 310 311 312
      ! Read general cutoff parameters

      input%rkmax = evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/cutoffs/@Kmax'))
      stars%gmax = evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/cutoffs/@Gmax'))

      stars%gmaxInit = stars%gmax

      xPathA = '/fleurInput/calculationSetup/cutoffs/@numbands'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      dimension%neigd = 0
      IF(numberNodes.EQ.1) THEN
         valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA)))))
         IF(TRIM(ADJUSTL(valueString)).EQ.'all') THEN
313
            dimension%neigd = -1
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
         ELSE
            READ(valueString,*) dimension%neigd
         END IF
      END IF

      ! Read SCF loop parametrization

      input%itmax = evaluateFirstIntOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/scfLoop/@itmax'))
      input%minDistance = evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/scfLoop/@minDistance'))
      input%maxiter = evaluateFirstIntOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/scfLoop/@maxIterBroyd'))

      valueString = TRIM(ADJUSTL(xmlGetAttributeValue('/fleurInput/calculationSetup/scfLoop/@imix')))
      SELECT CASE (valueString)
      CASE ('straight')
         input%imix = 0
      CASE ('Broyden1')
         input%imix = 3
      CASE ('Broyden2')
         input%imix = 5
      CASE ('Anderson')
         input%imix = 7
Daniel Wortmann's avatar
Daniel Wortmann committed
335 336
      CASE ("Pulay")
         input%imix = 9
337 338 339 340 341 342
      CASE ("pPulay")
         input%imix = 11
      CASE ("rPulay")
         input%imix = 13
      CASE ("aPulay")
         input%imix = 15
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
      CASE DEFAULT
         STOP 'Error: unknown mixing scheme selected!'
      END SELECT

      input%alpha = evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/scfLoop/@alpha'))
input%preconditioning_param = evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/scfLoop/@preconditioning_param'))
      input%spinf = evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/scfLoop/@spinf'))

      ! Get parameters for core electrons

      input%ctail = evaluateFirstBoolOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/coreElectrons/@ctail'))
      IF((TRIM(ADJUSTL(versionString)).EQ.'0.27')) THEN
         input%coretail_lmax = 99
      ELSE
       input%coretail_lmax = evaluateFirstIntOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/coreElectrons/@coretail_lmax'))
      END IF
      input%frcor = evaluateFirstBoolOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/coreElectrons/@frcor'))
      input%kcrel = evaluateFirstIntOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/coreElectrons/@kcrel'))

      ! Read in magnetism parameters

      input%jspins = evaluateFirstIntOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/magnetism/@jspins'))
      noco%l_noco = evaluateFirstBoolOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/magnetism/@l_noco'))
      input%swsp = evaluateFirstBoolOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/magnetism/@swsp'))
      input%lflip = evaluateFirstBoolOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/magnetism/@lflip'))
      input%fixed_moment=evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/magnetism/@fixed_moment'))
369

370
  IF (ABS(input%fixed_moment)>1E-8.AND.(input%jspins==1.OR.noco%l_noco)) CALL judft_error("Fixed moment only in collinear calculations with two spins")
371

372 373 374 375 376 377 378 379 380 381 382 383 384
      ! Read in optional expert modes switches

      xPathA = '/fleurInput/calculationSetup/expertModes'
      numberNodes = xmlGetNumberOfNodes(xPathA)

      input%gw = 0
      input%secvar = .FALSE.

      IF (numberNodes.EQ.1) THEN
         input%gw = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@gw'))
         input%secvar = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@secvar'))
      END IF

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
      ! Check for alternative k point sets for the chosen FLEUR mode

      xPathA = '/fleurInput/output'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      banddos%band = .FALSE.
      IF (numberNodes.EQ.1) THEN
         banddos%band = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@band'))
      END IF

      altKPointSetIndices(:) = -1
      xPathA = '/fleurInput/calculationSetup/bzIntegration/altKPointSet'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      IF(numberNodes.NE.0) THEN
         DO i = 1, numberNodes
            WRITE(xPathA,*) '/fleurInput/calculationSetup/bzIntegration/altKPointSet[',i,']/@purpose'
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA)))))
            IF((altKPointSetIndices(2).EQ.-1).AND.(TRIM(ADJUSTL(valueString)).EQ.'GW')) THEN
               altKPointSetIndices(2) = i
            ELSE IF((altKPointSetIndices(1).EQ.-1).AND.(TRIM(ADJUSTL(valueString)).EQ.'bands')) THEN
               altKPointSetIndices(1) = i
            END IF
         END DO
      END IF

      altKPointSetIndex = -1
      IF(banddos%band) THEN
         altKPointSetIndex = altKPointSetIndices(1)
      ELSE IF (input%gw.EQ.2) THEN
         altKPointSetIndex = altKPointSetIndices(2)
      END IF

      IF (altKPointSetIndex.NE.-1) THEN
         WRITE(kPointsPrefix,*) '/fleurInput/calculationSetup/bzIntegration/altKPointSet[',altKPointSetIndex,']'
418 419
      END IF

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
      ! Read in Brillouin zone integration parameters

      kpts%nkpt3 = 0
      l_kpts = .FALSE.

      valueString = TRIM(ADJUSTL(xmlGetAttributeValue('/fleurInput/calculationSetup/bzIntegration/@mode')))
      SELECT CASE (valueString)
      CASE ('hist')
         input%gauss = .FALSE.
         input%tria = .FALSE.
      CASE ('gauss')
         input%gauss = .TRUE.
         input%tria = .FALSE.
      CASE ('tria')
         input%gauss = .FALSE.
         input%tria = .TRUE.
      CASE DEFAULT
         STOP 'Invalid bzIntegration mode selected!'
      END SELECT

      nodeSum = 0
      xPathA = '/fleurInput/calculationSetup/bzIntegration/@fermiSmearingEnergy'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      nodeSum = nodeSum + numberNodes
      IF (numberNodes.EQ.1) THEN
         input%tkb = evaluateFirstOnly(xmlGetAttributeValue(xPathA))
      END IF
      xPathA = '/fleurInput/calculationSetup/bzIntegration/@fermiSmearingTemp'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      nodeSum = nodeSum + numberNodes
      IF (numberNodes.EQ.1) THEN
         input%tkb = evaluateFirstOnly(xmlGetAttributeValue(xPathA))
         input%tkb = boltzmannConst * input%tkb
      END IF
      IF(nodeSum.GE.2) THEN
         STOP 'Error: Multiple fermi Smearing parameters provided in input file!'
      END IF

      xPathA = '/fleurInput/calculationSetup/bzIntegration/@valenceElectrons'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      IF (numberNodes.EQ.1) THEN
         input%zelec = evaluateFirstOnly(xmlGetAttributeValue(xPathA))
      ELSE
         STOP 'Error: Optionality of valence electrons in input file not yet implemented!'
      END IF

466
      IF (altKPointSetIndex.EQ.-1) THEN
467 468 469
         WRITE(kPointsPrefix,*) '/fleurInput/calculationSetup/bzIntegration'
      END IF

470 471
      ! Option kPointDensity
      kpts%kPointDensity(:) = 0.0
472
      xPathA = TRIM(ADJUSTL(kPointsPrefix))//'/kPointDensity'
473 474 475 476 477 478 479 480 481 482 483
      numberNodes = xmlGetNumberOfNodes(xPathA)
      IF (numberNodes.EQ.1) THEN
         l_kpts = .FALSE.
         kpts%kPointDensity(1) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@denX'))
         kpts%kPointDensity(2) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@denY'))
         kpts%kPointDensity(3) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@denZ'))
         kpts%l_gamma = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@gamma'))
         kpts%specificationType = 4
      END IF

      ! Option kPointMesh
484
      xPathA = TRIM(ADJUSTL(kPointsPrefix))//'/kPointMesh'
485 486 487 488 489 490 491 492 493 494 495 496
      numberNodes = xmlGetNumberOfNodes(xPathA)
      IF (numberNodes.EQ.1) THEN
         l_kpts = .FALSE.
         kpts%nkpt3(1) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@nx'))
         kpts%nkpt3(2) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@ny'))
         kpts%nkpt3(3) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@nz'))
         kpts%l_gamma = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@gamma'))
         kpts%nkpt = kpts%nkpt3(1) * kpts%nkpt3(2) * kpts%nkpt3(3)
         kpts%specificationType = 2
      END IF

      ! Option kPointCount
497
      xPathA = TRIM(ADJUSTL(kPointsPrefix))//'/kPointCount'
498 499 500 501 502 503 504 505 506 507 508 509 510 511
      numberNodes = xmlGetNumberOfNodes(xPathA)
      IF (numberNodes.EQ.1) THEN
         l_kpts = .FALSE.
         kpts%nkpt = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@count'))
         kpts%l_gamma = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@gamma'))
         kpts%nkpt = kpts%nkpt
         kpts%specificationType = 1

         ALLOCATE(kpts%bk(3,kpts%nkpt))
         ALLOCATE(kpts%wtkpt(kpts%nkpt))
         kpts%bk = 0.0
         kpts%wtkpt = 0.0
         kpts%posScale = 1.0

512
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(kPointsPrefix))//'/kPointCount/specialPoint')
513 514 515 516 517 518 519 520 521
         IF(numberNodes.EQ.1) THEN
            STOP 'Error: Single special k point provided. This does not make sense!'
         END IF
         kpts%numSpecialPoints = numberNodes
         IF(kpts%numSpecialPoints.GE.2) THEN
            DEALLOCATE(kpts%specialPoints)
            ALLOCATE(kpts%specialPoints(3,kpts%numSpecialPoints))
            ALLOCATE(kpts%specialPointNames(kpts%numSpecialPoints))
            DO i = 1, kpts%numSpecialPoints
522
               WRITE(xPathA,*) TRIM(ADJUSTL(kPointsPrefix))//'/kPointCount/specialPoint[',i,']'
523 524 525 526 527 528 529 530 531 532 533 534 535 536
               valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA)))))
               kpts%specialPoints(1,i) = evaluatefirst(valueString)
               kpts%specialPoints(2,i) = evaluatefirst(valueString)
               kpts%specialPoints(3,i) = evaluatefirst(valueString)
               kpts%specialPointNames(i) = xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@name')
            END DO
         END IF
      ELSE
         DEALLOCATE(kpts%specialPoints)
         ALLOCATE(kpts%specialPoints(3,kpts%numSpecialPoints))
         ALLOCATE(kpts%specialPointNames(kpts%numSpecialPoints))
      END IF

      ! Option kPointList
537
      numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(kPointsPrefix))//'/kPointList')
538 539
      IF (numberNodes.EQ.1) THEN
         l_kpts = .TRUE.
540
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(kPointsPrefix))//'/kPointList/kPoint')
541 542 543 544 545 546 547 548
         kpts%nkpt = numberNodes
         kpts%l_gamma = .FALSE.
         ALLOCATE(kpts%bk(3,kpts%nkpt))
         ALLOCATE(kpts%wtkpt(kpts%nkpt))
         kpts%bk = 0.0
         kpts%wtkpt = 0.0
         kpts%specificationType = 3

549 550
         kpts%posScale = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(kPointsPrefix))//'/kPointList/@posScale'))
         weightScale = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(kPointsPrefix))//'/kPointList/@weightScale'))
551 552

         DO i = 1, kpts%nkpt
553
            WRITE(xPathA,*) TRIM(ADJUSTL(kPointsPrefix))//'/kPointList/kPoint[',i,']'
554 555 556 557 558 559 560 561 562
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA)))))
            READ(valueString,*) kpts%bk(1,i), kpts%bk(2,i), kpts%bk(3,i)
            kpts%bk(:,i)=kpts%bk(:,i)/kpts%posScale
            kpts%wtkpt(i) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@weight'))
            kpts%wtkpt(i) = kpts%wtkpt(i) / weightScale
         END DO
         kpts%posScale=1.0
      END IF

563 564 565 566 567 568 569 570 571 572 573 574 575
      ! Option kPointListFile
      xPathA = TRIM(ADJUSTL(kPointsPrefix))//'/kPointListFile'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      IF (numberNodes.EQ.1) THEN
         valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@filename')))
         OPEN (41,file=TRIM(ADJUSTL(valueString)),form='formatted',status='old')
            READ (41,*) kpts%nkpt
         CLOSE (41)
         ALLOCATE(kpts%bk(3,kpts%nkpt))
         ALLOCATE(kpts%wtkpt(kpts%nkpt))
         kpts%bk = 0.0
         kpts%wtkpt = 0.0
         kpts%l_gamma = .FALSE.
576
         l_kpts = .TRUE.
577 578
         kpts%specificationType = 3
         kpts%posScale=1.0
579 580 581 582 583 584 585

         ! We need to set input%film for inpeig. Unfortunately this is actually initialized at a later stage.
         ! So we do it here additionally.
         input%film = .FALSE.
         numberNodes = xmlGetNumberOfNodes('/fleurInput/cell/filmLattice')
         IF (numberNodes.EQ.1) input%film = .TRUE.

586 587 588
         CALL inpeig(atoms,cell,input,.FALSE.,kpts,kptsFilename=TRIM(ADJUSTL(valueString)))
      END IF

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
      ! Read in optional SOC parameters if present

      xPathA = '/fleurInput/calculationSetup/soc'
      numberNodes = xmlGetNumberOfNodes(xPathA)

      noco%l_soc = .FALSE.
      noco%theta = 0.0
      noco%phi = 0.0

      IF (numberNodes.EQ.1) THEN
         noco%theta=evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@theta'))
         noco%phi=evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@phi'))
         noco%l_soc = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@l_soc'))
         noco%l_spav = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@spav'))
      END IF

      ! Read in optional noco parameters if present

      xPathA = '/fleurInput/calculationSetup/nocoParams'
      numberNodes = xmlGetNumberOfNodes(xPathA)

      noco%l_ss = .FALSE.
      noco%l_mperp = .FALSE.
      noco%l_constr = .FALSE.
      noco%mix_b = 0.0
      noco%qss = 0.0

      noco%l_relax(:) = .FALSE.
      noco%alphInit(:) = 0.0
      noco%alph(:) = 0.0
      noco%beta(:) = 0.0
      noco%b_con(:,:) = 0.0

      IF ((noco%l_noco).AND.(numberNodes.EQ.0)) THEN
         STOP 'Error: l_noco is true but no noco parameters set in xml input file!'
      END IF

      IF (numberNodes.EQ.1) THEN
         noco%l_ss = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@l_ss'))
         noco%l_mperp = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@l_mperp'))
         noco%l_constr = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@l_constr'))
630
         noco%l_mtNocoPot = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@l_mtNocoPot'))
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

         noco%mix_b = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@mix_b'))

         valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/qss')))
         READ(valueString,*) noco%qss(1), noco%qss(2), noco%qss(3)

         !WRITE(*,*) 'Note: TODO: Calculation of q points!'

         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/qsc')
         IF (numberNodes.EQ.1) THEN
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/qsc')))
            READ(valueString,*) qsc(1), qsc(2), qsc(3)
            DO i = 1, 3
               noco%qss(i) = noco%qss(i) / qsc(i)
            END DO
            !WRITE(*,*) 'Note: TODO: Integrate qsc directly into qss in input file!'
            !WRITE(*,*) '(no problem for users)'
         END IF
      END IF

      ! Read in optional 1D parameters if present

      xPathA = '/fleurInput/calculationSetup/oneDParams'
      numberNodes = xmlGetNumberOfNodes(xPathA)

      oneD%odd%d1 = .FALSE.

      IF (numberNodes.EQ.1) THEN
         oneD%odd%d1 = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@d1'))
         oneD%odd%M = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@MM'))
         oneD%odd%mb = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@vM'))
         oneD%odd%m_cyl = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@m_cyl'))
         oneD%odd%chi = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@chi'))
         oneD%odd%rot = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@rot'))
         oneD%odd%invs = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@invs1'))
         oneD%odd%zrfs = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@zrfs1'))
      END IF

      ! Read in optional geometry optimization parameters

      xPathA = '/fleurInput/calculationSetup/geometryOptimization'
      numberNodes = xmlGetNumberOfNodes(xPathA)

      input%l_f = .FALSE.
      input%qfix = 0

      IF (numberNodes.EQ.1) THEN
         input%l_f = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@l_f'))
         input%xa = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@xa'))
         input%thetad = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@thetad'))
         input%epsdisp = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@epsdisp'))
         input%epsforce = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@epsforce'))

         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/@qfix')
         IF (numberNodes.EQ.1) THEN
            input%qfix = 1
            l_qfix = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@qfix'))
            IF (l_qfix) THEN
               input%qfix = 2
            END IF
         END IF
      END IF

      ! Read in optional general LDA+U parameters

      xPathA = '/fleurInput/calculationSetup/ldaU'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      IF (numberNodes.EQ.1) THEN
         input%ldauLinMix = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@l_linMix'))
         input%ldauMixParam = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@mixParam'))
         input%ldauSpinf = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@spinf'))
      END IF

704
      xPathA = '/fleurInput/calculationSetup/onsiteGF'
705 706
      numberNodes = xmlGetNumberOfNodes(xPathA)
      IF (numberNodes.EQ.1) THEN
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
         xPathA = '/fleurInput/calculationSetup/onsiteGF/@sigma'
         numberNodes = xmlGetNumberOfNodes(xPathA)
         IF(numberNodes.EQ.1) input%onsite_sigma = evaluateFirstIntOnly(xmlGetAttributeValue(xPathA))

         xPathA = '/fleurInput/calculationSetup/onsiteGF/@ne'
         numberNodes = xmlGetNumberOfNodes(xPathA)
         IF(numberNodes.EQ.1) input%onsite_ne = evaluateFirstIntOnly(xmlGetAttributeValue(xPathA))

         xPathA = '/fleurInput/calculationSetup/onsiteGF/@mode'
         numberNodes = xmlGetNumberOfNodes(xPathA)
         IF(numberNodes.EQ.1) input%onsite_mode = evaluateFirstIntOnly(xmlGetAttributeValue(xPathA))

         xPathA = '/fleurInput/calculationSetup/onsiteGF/@nz'
         numberNodes = xmlGetNumberOfNodes(xPathA)
         IF(numberNodes.EQ.1) input%onsite_nz = evaluateFirstIntOnly(xmlGetAttributeValue(xPathA))

723 724 725 726 727 728 729
         xPathA = '/fleurInput/calculationSetup/onsiteGF/@l_tetra'
         numberNodes = xmlGetNumberOfNodes(xPathA)
         IF(numberNodes.EQ.1) input%onsite_tetra = evaluateFirstBoolOnly(xmlGetAttributeValue(xPathA))
      
         xPathA = '/fleurInput/calculationSetup/onsiteGF/@l_sphavg'
         numberNodes = xmlGetNumberOfNodes(xPathA)
         IF(numberNodes.EQ.1) input%onsite_sphavg = evaluateFirstBoolOnly(xmlGetAttributeValue(xPathA))
730
      END IF
731
      
732
      IF((input%onsite_tetra).AND.(.NOT.input%tria)) THEN
733 734 735
         CALL juDFT_error("Tetrahedron method not set up with this k-point-set but l_tetra is true", calledby="r_inpXML")
      ENDIF

736
      IF(input%onsite_mode.GT.2) CALL juDFT_error("No valid mode for the energy contour of Green's function", calledby="r_inpXML")
737

738

739 740 741 742 743 744 745 746 747 748
      ! Read in optional q point mesh for spin spirals

      xPathA = '/fleurInput/calculationSetup/spinSpiralQPointMesh'
      numberNodes = xmlGetNumberOfNodes(xPathA)

      !   IF ((noco%l_ss).AND.(numberNodes.EQ.0)) THEN
      !      STOP 'Error: l_ss is true but no q point mesh set in xml input file!'
      !   END IF

      ! Read in optional E-Field parameters
749 750
     
      xPathA = '/fleurInput/calculationSetup/fields'
751 752 753
      numberNodes = xmlGetNumberOfNodes(xPathA)

      IF (numberNodes.EQ.1) THEN
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
         IF (xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/@b_field')>0) THEN
            field%b_field=evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'//@b_field'))
            field%l_b_field=.true.
         ENDIF
         field%efield%zsigma = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@zsigma'))
         field%efield%sig_b(1) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@sig_b_1'))
         field%efield%sig_b(2) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@sig_b_2'))
         field%efield%plot_charge = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@plot_charge'))
         field%efield%plot_rho = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@plot_rho'))
         field%efield%autocomp = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@autocomp'))
         field%efield%dirichlet = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@dirichlet'))
         field%efield%l_eV = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@eV'))

         numberNodes=xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/shape')
         ALLOCATE(field%efield%shapes(numberNodes))
         DO i=1,numberNodes
            WRITE(xPathB,"(a,a,i0,a)") TRIM(ADJUSTL(xpathA)),'/shape[',i,']'
            field%efield%shapes(i)=TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB)))))
         ENDDO
         
774 775 776 777 778 779 780 781 782 783 784
      END IF

      ! Read in optional energy parameter limits

      xPathA = '/fleurInput/calculationSetup/energyParameterLimits'
      numberNodes = xmlGetNumberOfNodes(xPathA)

      IF (numberNodes.EQ.1) THEN
         input%ellow = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@ellow'))
         input%elup = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@elup'))
      END IF
785 786 787 788 789 790 791 792 793

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! End of calculationSetup section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Start of cell section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
      ! Read in lattice parameters

      a1 = 0.0
      a2 = 0.0
      a3 = 0.0
      cell%z1 = 0.0
      dtild = 0.0
      input%film = .FALSE.
      latticeType = 'bulkLattice'
      latticeDef = 0
      symmetryDef = 0
      cell%latnam = 'any'

      numberNodes = xmlGetNumberOfNodes('/fleurInput/cell/filmLattice')

      IF (numberNodes.EQ.1) THEN
         input%film = .TRUE.
         latticeType = 'filmLattice'
      END IF

      xPathA = '/fleurInput/cell/'//latticeType
      numberNodes = xmlGetNumberOfNodes(xPathA)

      IF (numberNodes.EQ.1) THEN
         latticeScale = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@scale'))
         input%scaleCell = latticeScale
         valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@latnam')))
         READ(valueString,*) cell%latnam

         IF(input%film) THEN
            cell%z1 = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@dVac'))
            dtild = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@dTilda'))
            vacuum%dvac = cell%z1
            a3(3) = dtild
            evac0Temp = eVac0Default_const
            xPathB = TRIM(ADJUSTL(xPathA))//'/vacuumEnergyParameters'
            numberNodes = xmlGetNumberOfNodes(xPathB)
            IF(numberNodes.GE.1) THEN
               DO i = 1, numberNodes
                  xPathC = ''
                  WRITE(xPathC,'(a,i0,a)') TRIM(ADJUSTL(xPathB))//'[',i,']'
                  numVac = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathC))//'/@vacuum'))
                  eParamUp = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathC))//'/@spinUp'))
                  eParamDown = eParamUp
                  IF (xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathC))//'/@spinDown').GE.1) THEN
                     eParamDown = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathC))//'/@spinDown'))
                  END IF
                  evac0Temp(numVac,1) = eParamUp
                  IF(input%jspins.GT.1) evac0Temp(numVac,2) = eParamDown
                  IF(i.EQ.1) THEN
                     evac0Temp(3-numVac,1) = eParamUp
                     IF(input%jspins.GT.1) evac0Temp(3-numVac,2) = eParamDown
                  END IF
               END DO
            END IF
         END IF

         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/a1')
         IF (numberNodes.EQ.1) THEN
            latticeDef = 1
            input%scaleA1 = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/a1/@scale'))
            a1(1) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/a1'))
            a1(1) = a1(1) * input%scaleA1
            numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/a2')
            IF (numberNodes.EQ.1) THEN
               latticeDef = 2
               input%scaleA2 = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/a2/@scale'))
               a2(2) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/a2'))
               a2(2) = a2(2) * input%scaleA2
            END IF
            IF(.NOT.input%film) THEN
               input%scaleC = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/c/@scale'))
               a3(3) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/c'))
               a3(3) = a3(3) * input%scaleC
            END IF
         END IF

         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/row-1')
         IF (numberNodes.EQ.1) THEN
            latticeDef = 3
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/row-1')))
            a1(1) = evaluateFirst(valueString)
            a1(2) = evaluateFirst(valueString)
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/row-2')))
            a2(1) = evaluateFirst(valueString)
            a2(2) = evaluateFirst(valueString)
            IF(.NOT.input%film) THEN
               input%scaleC = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/c/@scale'))
               a3(3) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/c'))
               a3(3) = a3(3) * input%scaleC
            END IF
         END IF

         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/bravaisMatrix')
         IF (numberNodes.EQ.1) THEN
            latticeDef = 4
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/bravaisMatrix/row-1')))
            a1(1) = evaluateFirst(valueString)
            a1(2) = evaluateFirst(valueString)
            IF(.NOT.input%film) THEN
               a1(3) = evaluateFirst(valueString)
            END IF
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/bravaisMatrix/row-2')))
            a2(1) = evaluateFirst(valueString)
            a2(2) = evaluateFirst(valueString)
            IF(.NOT.input%film) THEN
               a2(3) = evaluateFirst(valueString)
            END IF
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/bravaisMatrix/row-3')))
            IF(.NOT.input%film) THEN
               a3(1) = evaluateFirst(valueString)
               a3(2) = evaluateFirst(valueString)
               a3(3) = evaluateFirst(valueString)
            ELSE
               !WRITE(*,*) 'Note: For film calculations only the upper left 2x2 part of the Bravais matrix is considered.'
            END IF
         END IF
      END IF ! Note: Further ways to define lattices might be added later. (1D lattice,...)

      ! Construction of amat requires additional information about the lattice
      ! and is done later (scroll down)!

      ! Read in symmetry parameters

      sym%namgrp = 'any'

      xPathA = '/fleurInput/cell/symmetry'
      numberNodes = xmlGetNumberOfNodes('/fleurInput/cell/symmetry')

      IF (numberNodes.EQ.1) THEN
         sym%symSpecType = 2
         symmetryDef = 1
         valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@spgrp')))
         READ(valueString,*) sym%namgrp
         sym%invs = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@invs'))
         sym%zrfs = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@zrfs'))
         sym%invs2 = sym%invs.AND.sym%zrfs

         IF (sym%namgrp.EQ.'any ') THEN
            sym%nop = 48
            ! Read in sym.out file if sym%namgrp='any' set.
            CALL rw_symfile('r',94,'sym.out',48,cell%bmat,&
                 &                        mrotTemp,tauTemp,sym%nop,sym%nop2,sym%symor)
            IF (ALLOCATED(sym%mrot)) THEN
               DEALLOCATE(sym%mrot)
            END IF
            ALLOCATE(sym%mrot(3,3,sym%nop))
            IF (ALLOCATED(sym%tau)) THEN
               DEALLOCATE(sym%tau)
            END IF
            ALLOCATE(sym%tau(3,sym%nop))

            DO k = 1, sym%nop
               DO i = 1, 3
                  DO j = 1, 3
                     sym%mrot(j,i,k) = mrotTemp(j,i,k)
                  END DO
                  sym%tau(i,k) = tauTemp(i,k)
               END DO
            END DO
         ELSE
            n2spg = 0
            DO i = 1, 20
               IF (sym%namgrp.EQ.nammap(i)) n2spg = i
            END DO
            IF (n2spg == 0 ) THEN
               WRITE (*,*) 'Spacegroup ',sym%namgrp,' not known! Choose one of:'
               WRITE (*,'(20(a4,1x))') (nammap(i),i=1,20)
               CALL juDFT_error("Could not determine spacegroup!", calledby = "r_inpXML")
            END IF
            IF ((n2spg.GE.13).AND.(n2spg.LE.17)) THEN
               IF (.not.((cell%latnam.EQ.'hx3').OR.(cell%latnam.EQ.'hex'))) THEN
                  CALL juDFT_error("Use only hex or hx3 with p3, p3m1, p31m, p6 or p6m!", calledby ="r_inpXML")
               END IF
            END IF
            sym%nop = ord2(n2spg)
            IF (sym%invs) THEN
               sym%nop = 2*sym%nop
               IF (sym%zrfs.and.(.not.l_c2(n2spg))) sym%nop = 2*sym%nop
            ELSE
               IF (sym%zrfs) sym%nop = 2*sym%nop
            END IF
            IF (ALLOCATED(sym%mrot)) THEN
               DEALLOCATE(sym%mrot)
            END IF
            ALLOCATE(sym%mrot(3,3,sym%nop))
            IF (ALLOCATED(sym%tau)) THEN
               DEALLOCATE(sym%tau)
            END IF
            ALLOCATE(sym%tau(3,sym%nop))
            CALL spg2set(sym%nop,sym%zrfs,sym%invs,sym%namgrp,cell%latnam,&
                 &                     sym%mrot,sym%tau,sym%nop2,sym%symor)
         END IF
      END IF

      xPathA = '/fleurInput/cell/symmetryFile'
      numberNodes = xmlGetNumberOfNodes(xPathA)

      IF (numberNodes.EQ.1) THEN
         symmetryDef = 2
         sym%symSpecType = 1
         sym%nop = 48
         valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@filename')))

         CALL rw_symfile('r',94,TRIM(ADJUSTL(valueString)),48,cell%bmat,&
              &                     mrotTemp,tauTemp,sym%nop,sym%nop2,sym%symor)

         IF (ALLOCATED(sym%mrot)) THEN
            DEALLOCATE(sym%mrot)
         END IF
         ALLOCATE(sym%mrot(3,3,sym%nop))
         IF (ALLOCATED(sym%tau)) THEN
            DEALLOCATE(sym%tau)
         END IF
         ALLOCATE(sym%tau(3,sym%nop))

         sym%invs = .FALSE.
         sym%zrfs = .FALSE.

         DO k = 1, sym%nop
            absSum = 0
            DO i = 1, 3
               DO j = 1, 3
                  sym%mrot(j,i,k) = mrotTemp(j,i,k)
                  absSum = absSum + ABS(sym%mrot(j,i,k))
               END DO
               sym%tau(i,k) = tauTemp(i,k)
            END DO
            IF (absSum.EQ.3) THEN
               IF (ALL(sym%tau(:,k).EQ.0.0)) THEN
                  IF ((sym%mrot(1,1,k).EQ.-1).AND.(sym%mrot(2,2,k).EQ.-1).AND.(sym%mrot(3,3,k).EQ.-1)) sym%invs = .TRUE.
                  IF ((sym%mrot(1,1,k).EQ.1).AND.(sym%mrot(2,2,k).EQ.1).AND.(sym%mrot(3,3,k).EQ.-1)) sym%zrfs = .TRUE.
               END IF
            END IF
         END DO

         sym%invs2 = sym%invs.AND.sym%zrfs
      END IF

      xPathA = '/fleurInput/cell/symmetryOperations'
      numberNodes = xmlGetNumberOfNodes(xPathA)
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
      IF (numberNodes.EQ.1) THEN
         sym%symSpecType = 3
         symmetryDef = 3

         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/symOp')
         sym%nop = numberNodes

         IF (ALLOCATED(sym%mrot)) DEALLOCATE(sym%mrot)
         ALLOCATE(sym%mrot(3,3,sym%nop))
         IF (ALLOCATED(sym%tau)) DEALLOCATE(sym%tau)
         ALLOCATE(sym%tau(3,sym%nop))
         sym%symor = .TRUE.
         DO i = 1, sym%nop
            WRITE(xPathB,*) TRIM(ADJUSTL(xPathA))//'/symOp[',i,']/row-1'
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB)))))
            READ(valueString,*) sym%mrot(1,1,i), sym%mrot(1,2,i), sym%mrot(1,3,i), sym%tau(1,i)

            WRITE(xPathB,*) TRIM(ADJUSTL(xPathA))//'/symOp[',i,']/row-2'
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB)))))
            READ(valueString,*) sym%mrot(2,1,i), sym%mrot(2,2,i), sym%mrot(2,3,i), sym%tau(2,i)

            WRITE(xPathB,*) TRIM(ADJUSTL(xPathA))//'/symOp[',i,']/row-3'
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB)))))
            READ(valueString,*) sym%mrot(3,1,i), sym%mrot(3,2,i), sym%mrot(3,3,i), sym%tau(3,i)

            IF ((sym%tau(1,i)**2 + sym%tau(2,i)**2 + sym%tau(3,i)**2).GT.1.e-8) THEN
               sym%symor = .FALSE.
            END IF
            DO j = 1,3
               IF (ABS(sym%tau(j,i)-0.33333) < 0.00001) THEN
                  sym%tau(j,i) = 1./3.
               ENDIF
               IF (ABS(sym%tau(j,i)+0.33333) < 0.00001) THEN
                  sym%tau(j,i) = -1./3.
               ENDIF
               IF (ABS(sym%tau(j,i)-0.66667) < 0.00001) THEN
                  sym%tau(j,i) = 2./3.
               ENDIF
               IF (ABS(sym%tau(j,i)+0.66667) < 0.00001) THEN
                  sym%tau(j,i) = -2./3.
               ENDIF
            ENDDO
         END DO
      END IF

      ! Calculate missing symmetry and cell properties and check consistency of parameters.

      ! Construction of amat
      SELECT CASE (latticeDef)
      CASE (1)
         IF (cell%latnam.EQ.'squ') THEN
            a2(2) = a1(1)
         ELSE IF (cell%latnam.EQ.'c-b') THEN
            aTemp = a1(1)
            a1(1) = aTemp*0.5*sqrt(2.0)
            a1(2) = -aTemp*0.5
            a2(1) = aTemp*0.5*sqrt(2.0)
            a2(2) = aTemp*0.5
         ELSE IF (cell%latnam.EQ.'hex') THEN
            aTemp = 0.5*a1(1)
            a1(1) = aTemp*sqrt(3.0)
            a1(2) = -aTemp
            a2(1) = a1(1)
            a2(2) = aTemp
         ELSE IF (cell%latnam.EQ.'hx3') THEN
            aTemp = 0.5*a1(1)
            a1(1) = aTemp
            a1(2) = -aTemp*sqrt(3.0)
            a2(1) = a1(1)
            a2(2) = -a1(2)
         ELSE IF (cell%latnam.EQ.'fcc') THEN
            aTemp = a1(1)
            a1(1) =       0.0 ; a1(2) = 0.5*aTemp ; a1(3) = 0.5*aTemp
            a2(1) = 0.5*aTemp ; a2(2) =       0.0 ; a2(3) = 0.5*aTemp
            a3(1) = 0.5*aTemp ; a3(2) = 0.5*aTemp ; a3(3) =       0.0
         ELSE IF (cell%latnam.EQ.'bcc') THEN
            aTemp = a1(1)
            a1(1) =-0.5*aTemp ; a1(2) = 0.5*aTemp ; a1(3) = 0.5*aTemp
            a2(1) = 0.5*aTemp ; a2(2) =-0.5*aTemp ; a2(3) = 0.5*aTemp
            a3(1) = 0.5*aTemp ; a3(2) = 0.5*aTemp ; a3(3) =-0.5*aTemp
         ELSE
            CALL juDFT_error("latnam is incompatible to parametrization of lattice (1)", calledby ="r_inpXML")
         END IF
      CASE (2)
         IF ((cell%latnam.EQ.'c-r').OR.(cell%latnam.EQ.'p-r')) THEN
            IF (cell%latnam.EQ.'c-r') THEN
               a1(2) = -a2(2)
               a2(1) =  a1(1)
            END IF
         ELSE
            CALL juDFT_error("latnam is incompatible to parametrization of lattice (2)", calledby ="r_inpXML")
         END IF
      CASE (3)
         IF (.NOT.(cell%latnam.EQ.'obl')) THEN
            CALL juDFT_error("latnam is incompatible to parametrization of lattice (3)", calledby ="r_inpXML")
         END IF
      CASE (4)
         IF (.NOT.(cell%latnam.EQ.'any')) THEN
            CALL juDFT_error("latnam is incompatible to parametrization of lattice (4)", calledby ="r_inpXML")
         END IF
      CASE DEFAULT
         CALL juDFT_error("Illegal lattice definition", calledby ="r_inpXML")
      END SELECT

      IF (latticeScale.EQ.0.0) latticeScale = 1.0
      IF (.NOT.input%film) vacuum%dvac = a3(3)
      vacuum%dvac = latticeScale*vacuum%dvac
      dtild = latticeScale*dtild

      cell%amat(:,1) = a1(:) * latticeScale
      cell%amat(:,2) = a2(:) * latticeScale
      cell%amat(:,3) = a3(:) * latticeScale

      CALL inv3(cell%amat,cell%bmat,cell%omtil)
      cell%bmat(:,:) = tpi_const*cell%bmat(:,:)
      cell%omtil = abs(cell%omtil)

      IF (input%film.AND..NOT.oneD%odd%d1) THEN
         cell%vol = (cell%omtil/cell%amat(3,3))*vacuum%dvac
         cell%area = cell%omtil/cell%amat(3,3)
         !-odim
      ELSE IF (oneD%odd%d1) THEN
         cell%area = tpi_const*cell%amat(3,3)
         cell%vol = pi_const*(vacuum%dvac**2)*cell%amat(3,3)/4.0
         !+odim
      ELSE
         cell%vol = cell%omtil
         cell%area = cell%amat(1,1)*cell%amat(2,2)-cell%amat(1,2)*cell%amat(2,1)
         IF (cell%area.lt.1.0e-7) THEN
            IF (cell%latnam.EQ.'any') THEN
               cell%area = 1.
            ELSE
               CALL juDFT_error("area = 0",calledby ="r_inpXML")
            END IF
         END IF
      END IF

      ! Construction of missing symmetry information
      IF ((symmetryDef.EQ.2).OR.(symmetryDef.EQ.3)) THEN
         nop48 = 48
         ALLOCATE (invOps(sym%nop),multtab(sym%nop,sym%nop),optype(nop48))
         CALL check_close(sym%nop,sym%mrot,sym%tau,&
              &                      multtab,invOps,optype)

         CALL symproperties(nop48,optype,input%film,sym%nop,multtab,cell%amat,&
              &                        sym%symor,sym%mrot,sym%tau,&
              &                        invSym,sym%invs,sym%zrfs,sym%invs2,sym%nop,sym%nop2)
         DEALLOCATE(invOps,multtab,optype)
         IF (.not.input%film) sym%nop2=sym%nop
         IF (input%film) THEN
            DO n = 1, sym%nop
               DO i = 1, 3
                  IF (ABS(sym%tau(i,n)) > 0.00001) THEN
                     CALL juDFT_error("nonsymmorphic symmetries not yet implemented for films!",calledby ="r_inpXML")
                  ENDIF
               END DO
            END DO
         END IF
      END IF
      sym%invs2 = sym%invs.AND.sym%zrfs

      ALLOCATE (sym%invarop(atoms%nat,sym%nop),sym%invarind(atoms%nat))
      ALLOCATE (sym%multab(sym%nop,sym%nop),sym%invtab(sym%nop))
      ALLOCATE (sym%invsatnr(atoms%nat),sym%d_wgn(-3:3,-3:3,3,sym%nop))

      !some settings for film calculations
      vacuum%nmzd = 250
      vacuum%nmzxyd = 100
      vacuum%nvac = 2
      IF (sym%zrfs.OR.sym%invs) vacuum%nvac = 1
      IF (oneD%odd%d1) vacuum%nvac = 1
      cell%z1 = vacuum%dvac/2
      vacuum%nmz = vacuum%nmzd
      vacuum%delz = 25.0/vacuum%nmz
      IF (oneD%odd%d1) vacuum%delz = 20.0 / vacuum%nmz
      IF (vacuum%nmz.GT.vacuum%nmzd) CALL juDFT_error("nmzd",calledby ="inped")
      vacuum%nmzxy = vacuum%nmzxyd
      IF (vacuum%nmzxy.GT.vacuum%nmzxyd) CALL juDFT_error("nmzxyd",calledby ="inped")
1214 1215 1216 1217 1218 1219 1220 1221 1222

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! End of cell section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Start of XC functional section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
      !Read in libxc parameters if present
      if(      xmlGetNumberOfNodes('/fleurInput/xcFunctional/LibXCID')   == 1 &
         .and. xmlGetNumberOfNodes('/fleurInput/xcFunctional/LibXCName') == 1) then
         call judft_error("LibXC is given both by Name and ID and is therefore overdetermined", calledby="r_inpXML")
      endif

      IF (xmlGetNumberOfNodes('/fleurInput/xcFunctional/LibXCID') == 1) THEN
         id_x=evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/xcFunctional/LibXCID/@exchange'))
         id_c=evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/xcFunctional/LibXCID/@correlation'))
      ELSEIF (xmlGetNumberOfNodes('/fleurInput/xcFunctional/LibXCName') == 1) THEN
         valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL('/fleurInput/xcFunctional/LibXCName/@exchange')))))
1234
#ifdef CPP_LIBXC
1235 1236 1237
         id_x =  xc_f03_functional_get_number(TRIM(valueString))
         valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL('/fleurInput/xcFunctional/LibXCName/@correlation')))))
         id_c =  xc_f03_functional_get_number(TRIM(valueString))
1238
#else
1239 1240 1241 1242 1243 1244
         CALL judft_error("To use libxc functionals you have to compile with libXC support")
#endif
      ELSE
         id_x=0; id_c=0
      ENDIF

Daniel Wortmann's avatar
Daniel Wortmann committed
1245
      !write (*,*) "id_x = ", id_x, "id_c = ", id_c
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295

      ! Read in xc functional parameters
      valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL('/fleurInput/xcFunctional/@name')))))
      namex(1:4) = valueString(1:4)
      l_relcor = evaluateFirstBoolOnly(xmlGetAttributeValue('/fleurInput/xcFunctional/@relativisticCorrections'))

      relcor = 'non-relativi'
      IF (l_relcor) THEN
         relcor = 'relativistic'
      END IF

      !now initialize the xcpot variable
      CALL setXCParameters(atoms,valueString,l_relcor,input%jspins,id_x,id_c,xcpot)

      xPathA = '/fleurInput/calculationSetup/cutoffs/@GmaxXC'
      numberNodes = xmlGetNumberOfNodes(xPathA)
      xcpot%gmaxxc = stars%gmax
      IF(numberNodes.EQ.1) THEN
         xcpot%gmaxxc = evaluateFirstOnly(xmlGetAttributeValue(xPathA))
      END IF
      hybrid%l_hybrid=xcpot%is_hybrid()

      ALLOCATE(hybrid%lcutm1(atoms%ntype),hybrid%lcutwf(atoms%ntype),hybrid%select1(4,atoms%ntype))

      obsolete%lwb=.FALSE.
      IF (xcpot%is_gga()) THEN
         obsolete%ndvgrd=6
         obsolete%chng=-0.1e-11
      END IF

      IF (xcpot%is_gga()) THEN
         obsolete%ndvgrd = max(obsolete%ndvgrd,3)
      END IF

      hybrid%gcutm1 = input%rkmax - 0.5
      hybrid%tolerance1 = 1.0e-4
      hybrid%ewaldlambda = 3
      hybrid%lexp = 16
      hybrid%bands1 = dimension%neigd

      numberNodes = xmlGetNumberOfNodes('/fleurInput/calculationSetup/prodBasis')
      IF (numberNodes==0) THEN
         IF (hybrid%l_hybrid) CALL judft_error("Mixed product basis input missing in inp.xml")
      ELSE
         hybrid%gcutm1=evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/prodBasis/@gcutm'))
         hybrid%tolerance1=evaluateFirstOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/prodBasis/@tolerance'))
         hybrid%ewaldlambda=evaluateFirstIntOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/prodBasis/@ewaldlambda'))
         hybrid%lexp=evaluateFirstIntOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/prodBasis/@lexp'))
         hybrid%bands1=evaluateFirstIntOnly(xmlGetAttributeValue('/fleurInput/calculationSetup/prodBasis/@bands'))
      ENDIF
1296

1297 1298 1299
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! End of XC functional section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1300

1301 1302 1303 1304
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Start of species section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1305 1306 1307 1308 1309 1310
      ALLOCATE (speciesNLO(numSpecies))
      ALLOCATE(atoms%speciesName(numSpecies))

      atoms%numStatesProvided = 0
      atoms%lapw_l(:) = -1
      atoms%n_u = 0
1311
      atoms%n_hia = 0
1312
      atoms%n_j0 = 0
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339

      DEALLOCATE(noel)
      ALLOCATE(noel(atoms%ntype))

      DO iSpecies = 1, numSpecies
         ! Attributes of species
         WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']'
         atoms%speciesName(iSpecies) = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@name')))
         atomicNumber = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@atomicNumber'))
         coreStates = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@coreStates'))
         magMom = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@magMom'))
         flipSpin = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@flipSpin'))

         ! Attributes of mtSphere element of species
         radius = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/mtSphere/@radius'))
         gridPoints = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/mtSphere/@gridPoints'))
         logIncrement = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/mtSphere/@logIncrement'))

         ! Attributes of atomicCutoffs element of species
         lmax = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/atomicCutoffs/@lmax'))
         lnonsphr = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/atomicCutoffs/@lnonsphr'))
         lmaxAPW = -1
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/atomicCutoffs/@lmaxAPW')
         IF (numberNodes.EQ.1) THEN
            lmaxAPW = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/atomicCutoffs/@lmaxAPW'))
         END IF

1340 1341
         !ldau_use determines what these u parameters are going to be used for
         !It is determined from l_hia and l_j0 and l_u
1342
         !  1 -> LDA+U
1343 1344 1345 1346
         !  2 -> LDA+U and calculation of J0
         !  3 -> LDA+HIA 
         !  4 -> LDA+HIA and calculation of J0
         !  5 -> only calculate j0 (necessary)
1347

1348
         numU = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/ldaU')
1349
         IF (numU.GT.4) CALL juDFT_error("Too many U parameters provided for a certain species (maximum is 4).",calledby ="r_inpXML")
1350 1351 1352 1353
         ldau_l = -1
         ldau_u = 0.0
         ldau_j = 0.0
         l_amf = .FALSE.
1354
         ldau_use = -1
1355 1356
         DO i = 1, numU
            WRITE(xPathB,*) i
1357 1358 1359 1360 1361 1362 1363 1364
            ldau_l(i) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@l'))
            ldau_u(i) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@U'))
            ldau_j(i) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@J'))
            l_amf(i) = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@l_amf'))

            numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@l_hia')
            IF(numberNodes.EQ.1) THEN
               l_hia = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@l_hia'))
1365
            ELSE
1366
               l_hia = .false.
1367
            ENDIF
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
            numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@l_U')
            IF(numberNodes.EQ.1) THEN
               l_U = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@l_U'))
            ELSE
               l_U = .NOT.l_hia
            ENDIF

            IF(l_hia.AND.l_U) CALL juDFT_error("LDA+U and LDA+HIA on the same orbital is not allowed", calledby="r_inpXML")

            numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@l_j0')
            IF(numberNodes.EQ.1) THEN
               l_j0 = evaluateFirstBoolOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/ldaU['//TRIM(ADJUSTL(xPathB))//']/@l_j0'))
            ELSE
               l_j0 = l_hia
            ENDIF
            IF(.NOT.l_hia) THEN
               IF(.NOT.l_j0) THEN
                  ldau_use(i) = 1
               ELSE
                  ldau_use(i) = 2
               ENDIF
            ELSE 
               IF(.NOT.l_j0) THEN
                  ldau_use(i) = 3
               ELSE
                  ldau_use(i) = 4
               ENDIF
            ENDIF 
            IF(.NOT.l_U.AND.l_j0) ldau_use(i) = 5
         ENDDO
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

         speciesNLO(iSpecies) = 0
         WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']/lo'
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA)))
         DO iLO = 1, numberNodes
            WRITE(xPathB,*) TRIM(ADJUSTL(xPathA)),'[',iLO,']/@l'
            WRITE(xPathC,*) TRIM(ADJUSTL(xPathA)),'[',iLO,']/@n'
            lString = xmlGetAttributeValue(TRIM(ADJUSTL(xPathB)))
            nString = xmlGetAttributeValue(TRIM(ADJUSTL(xPathC)))
            CALL getIntegerSequenceFromString(TRIM(ADJUSTL(lString)), lNumbers, lNumCount)
            CALL getIntegerSequenceFromString(TRIM(ADJUSTL(nString)), nNumbers, nNumCount)
            IF(lNumCount.NE.nNumCount) THEN
               STOP 'Error in LO input: l quantum number count does not equal n quantum number count'
            END IF
            speciesNLO(iSpecies) = speciesNLO(iSpecies) + lNumCount
            DEALLOCATE (lNumbers, nNumbers)
         END DO
1415 1416 1417 1418 1419 1420 1421 1422
         ! Special switches for species
         vcaspecies=0.0
         WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']/special'
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA)))
         IF (numberNodes==1) THEN
            vcaSpecies   = evaluateFirstOnly(TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@vca_charge'))))
         ENDIF
       
1423 1424 1425 1426 1427
         DO iType = 1, atoms%ntype
            WRITE(xPathA,*) '/fleurInput/atomGroups/atomGroup[',iType,']/@species'
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA)))))
            IF(TRIM(ADJUSTL(atoms%speciesName(iSpecies))).EQ.TRIM(ADJUSTL(valueString))) THEN
               atoms%nz(iType) = atomicNumber
1428
               atoms%zatom(iType) = atoms%nz(iType)
1429 1430 1431 1432
               IF (atoms%nz(iType).EQ.0) THEN
                  WRITE(*,*) 'Note: Replacing atomic number 0 by 1.0e-10 on atom type ', iType
                  atoms%zatom(iType) = 1.0e-10
               END IF
1433
               atoms%zatom(iType)=atoms%zatom(iType)+vcaspecies
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
               noel(iType) = namat_const(atoms%nz(iType))
               atoms%rmt(iType) = radius
               atoms%jri(iType) = gridPoints
               atoms%dx(iType) = logIncrement
               atoms%lmax(iType) = lmax
               atoms%nlo(iType) = speciesNLO(iSpecies)
               atoms%ncst(iType) = coreStates
               atoms%lnonsph(iType) = lnonsphr
               atoms%lapw_l(iType) = lmaxAPW
               IF (flipSpin) THEN
                  atoms%nflip(iType) = 1
               ELSE
                  atoms%nflip(iType) = 0
               ENDIF
               atoms%bmu(iType) = magMom
               DO i = 1, numU
1450
                  IF(ldau_use(i).LE.2.AND.ldau_use(i).GT.0) THEN
1451 1452 1453 1454 1455 1456
                     atoms%n_u = atoms%n_u + 1
                     atoms%lda_u(atoms%n_u)%l = ldau_l(i)
                     atoms%lda_u(atoms%n_u)%u = ldau_u(i)
                     atoms%lda_u(atoms%n_u)%j = ldau_j(i)
                     atoms%lda_u(atoms%n_u)%l_amf = l_amf(i)
                     atoms%lda_u(atoms%n_u)%atomType = iType
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
                     IF(ldau_use(i).EQ.2) THEN
                        IF(input%jspins.EQ.2) THEN
                           input%l_gf = .true. 
                           atoms%n_j0 = atoms%n_j0 + 1
                           atoms%j0(atoms%n_j0)%l        = ldau_l(i)
                           atoms%j0(atoms%n_j0)%u        = ldau_u(i)
                           atoms%j0(atoms%n_j0)%j        = ldau_j(i)
                           atoms%j0(atoms%n_j0)%atomType = iType
                        ELSE
                           CALL juDFT_warn("No J0 calculation for non-magnetic systems", calledby="r_inpXML")
                        ENDIF 
                     ENDIF
                  ELSE IF(ldau_use(i).LE.4.AND.ldau_use(i).GT.0) THEN
                     input%l_gf = .true. 
1471 1472 1473 1474
                     atoms%n_hia = atoms%n_hia + 1
                     atoms%lda_hia(atoms%n_hia)%l        = ldau_l(i)
                     atoms%lda_hia(atoms%n_hia)%u        = ldau_u(i)
                     atoms%lda_hia(atoms%n_hia)%j        = ldau_j(i)
1475
                     atoms%lda_u(atoms%n_u)%l_amf        = l_amf(i)
1476
                     atoms%lda_hia(atoms%n_hia)%atomType = iType
1477 1478 1479 1480 1481 1482 1483 1484
                     IF(ldau_use(i).EQ.4) THEN
                        IF(input%jspins.EQ.2) THEN
                           atoms%n_j0 = atoms%n_j0 + 1
                           atoms%j0(atoms%n_j0)%l        = ldau_l(i)
                           atoms%j0(atoms%n_j0)%u        = ldau_u(i)
                           atoms%j0(atoms%n_j0)%j        = ldau_j(i)
                           atoms%j0(atoms%n_j0)%atomType = iType
                        END IF
1485
                     END IF 
1486
                  ELSE IF(ldau_use(i).EQ.5) THEN
1487
                     IF(input%jspins.EQ.2) THEN
1488
                        input%l_gf = .true. 
1489
                        atoms%n_j0 = atoms%n_j0 + 1
1490 1491 1492
                        atoms%j0(atoms%n_j0)%l        = ldau_l(i)
                        atoms%j0(atoms%n_j0)%u        = ldau_u(i)
                        atoms%j0(atoms%n_j0)%j        = ldau_j(i)
1493 1494 1495
                        atoms%j0(atoms%n_j0)%atomType = iType
                     ELSE
                        CALL juDFT_warn("No J0 calculation for non-magnetic systems", calledby="r_inpXML")
1496 1497 1498
                     ENDIF 
                  ELSE
                     CALL juDFT_error("Invalid use for U-Parameters", hint="This is a bug in FLEUR, please report")
1499 1500
                  END IF 
               ENDDO
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
               atomTypeSpecies(iType) = iSpecies
               IF(speciesRepAtomType(iSpecies).EQ.-1) speciesRepAtomType(iSpecies) = iType
            END IF
         END DO
      END DO

      atoms%lmaxd = maxval(atoms%lmax(:))
      atoms%llod  = 0
      atoms%nlod = 0
      DO iType = 1, atoms%ntype
         atoms%nlod = max(atoms%nlod,atoms%nlo(iType))
      END DO
      atoms%nlod = max(atoms%nlod,2) ! for chkmt
      ALLOCATE(atoms%llo(atoms%nlod,atoms%ntype)); atoms%llo=-1
      ALLOCATE(atoms%ulo_der(atoms%nlod,atoms%ntype))
      ALLOCATE(atoms%l_dulo(atoms%nlod,atoms%ntype)) ! For what is this?

      dimension%nstd = 29

      ALLOCATE(atoms%coreStateOccs(dimension%nstd,2,atoms%ntype)); atoms%coreStateOccs=0.0
      ALLOCATE(atoms%coreStateNprnc(dimension%nstd,atoms%ntype))
      ALLOCATE(atoms%coreStateKappa(dimension%nstd,atoms%ntype))

      CALL enpara%init(atoms,input%jspins)
      enpara%evac0(:,:) = evac0Temp(:,:)

      DO iSpecies = 1, numSpecies
         ALLOCATE(speciesLLO(speciesNLO(iSpecies)))
         ALLOCATE(speciesLOeParams(speciesNLO(iSpecies)))
         ALLOCATE(speciesLOEDeriv(speciesNLO(iSpecies)))

         ! Attributes of energyParameters element of species
         WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']'
         speciesEParams(0) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/energyParameters/@s'))
         speciesEParams(1) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/energyParameters/@p'))
         speciesEParams(2) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/energyParameters/@d'))
         speciesEParams(3) = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/energyParameters/@f'))

         ! Parameters for hybrid functionals
         IF (hybrid%l_hybrid) THEN
            WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']/prodBasis'
            numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA)))
            IF (numberNodes.NE.1) CALL judft_error("Parameters for mixed basis are missing for some specified")
            lcutm =evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@lcutm'))
            lcutwf=evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@lcutwf'))
            xPathA=xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@select')
            hybSelect(1) = NINT(evaluateFirst(xPathA))
            hybSelect(2) = NINT(evaluateFirst(xPathA))
            hybSelect(3) = NINT(evaluateFirst(xPathA))
            hybSelect(4) = NINT(evaluateFirst(xPathA))
         ENDIF

         ! Special switches for species
         ldaspecies=.FALSE.
         socscalespecies=1.0
         WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']/special'
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA)))
         IF (numberNodes==1) THEN
            ldaSpecies = evaluateFirstBoolOnly(TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@lda'))))
            socscaleSpecies   = evaluateFirstOnly(TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@socscale'))))
1561 1562 1563 1564
            IF (xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/@b_field_mt')>0) THEN
               b_field_mtSpecies=evaluateFirstOnly(TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/@b_field_mt'))))
               field%l_b_field=.TRUE.
            ENDIF
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
         ENDIF
         ! Explicitely provided core configurations

         coreConfigPresent = .FALSE.
         providedCoreStates = 0
         providedStates = 0
         coreStateOccs = 0.0
         speciesXMLElectronStates = noState_const
         speciesXMLCoreOccs = -1.0
         speciesXMLPrintCoreStates = .FALSE.
         WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']/electronConfig'
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA)))
         IF (numberNodes.EQ.1) THEN
            coreConfigPresent = .TRUE.
            valueString = xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/coreConfig')
            token = popFirstStringToken(valueString)
            DO WHILE (token.NE.' ')
               IF (token(1:1).EQ.'[') THEN
                  DO i = 1, 6
                     IF (TRIM(ADJUSTL(token)).EQ.nobleGasConfigList_const(i)) THEN
                        IF (providedCoreStates+nobleGasNumStatesList_const(i).GT.29) THEN
                           STOP 'Error: Too many core states provided in xml input file!'
                        END IF
                        DO j = providedCoreStates+1, providedCoreStates+nobleGasNumStatesList_const(i)
                           coreStateOccs(j-providedCoreStates,:) = coreStateNumElecsList_const(j)
                           coreStateNprnc(j-providedCoreStates) = coreStateNprncList_const(j)
                           coreStateKappa(j-providedCoreStates) = coreStateKappaList_const(j)
                           speciesXMLElectronStates(j) = coreState_const
                        END DO
                        providedCoreStates = providedCoreStates + nobleGasNumStatesList_const(i)
                     END IF
                  END DO
               ELSE
                  DO i = 1, 29
                     IF (TRIM(ADJUSTL(token)).EQ.coreStateList_const(i)) THEN
                        providedCoreStates = providedCoreStates + 1
                        IF (providedCoreStates.GT.29) THEN
                           STOP 'Error: Too many core states provided in xml input file!'
                        END IF
                        coreStateOccs(providedCoreStates,:) = coreStateNumElecsList_const(i)
                        coreStateNprnc(providedCoreStates) = coreStateNprncList_const(i)
                        coreStateKappa(providedCoreStates) = coreStateKappaList_const(i)
                        speciesXMLElectronStates(i) = coreState_const
                     END IF
                  END DO
               END IF
               token = popFirstStringToken(valueString)
            END DO
            numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA))//'/valenceConfig')
            providedStates = providedCoreStates
            IF(numberNodes.EQ.1) THEN
               valueString = xmlGetAttributeValue(TRIM(ADJUSTL(xPathA))//'/valenceConfig')
               token = popFirstStringToken(valueString)
               DO WHILE (token.NE.' ')
                  DO i = 1, 29
                     IF (TRIM(ADJUSTL(token)).EQ.coreStateList_const(i)) THEN
                        providedStates = providedStates + 1
                        IF (providedStates.GT.29) THEN
                           STOP 'Error: Too many valence states provided in xml input file!'
                        END IF
                        coreStateOccs(providedStates,:) = coreStateNumElecsList_const(i)
                        coreStateNprnc(providedStates) = coreStateNprncList_const(i)
                        coreStateKappa(providedStates) = coreStateKappaList_const(i)
                        speciesXMLElectronStates(i) = valenceState_const
                     END IF
                  END DO
                  token = popFirstStringToken(valueString)
               END DO
            END IF
         END IF

         ! Explicitely provided core occupations

         WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']/electronConfig/stateOccupation'
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA)))
         IF (numberNodes.GE.1) THEN
            IF (.NOT.coreConfigPresent) THEN
               WRITE(*,*) 'Note: This just has to be implemented:'
               STOP 'Error: Core occupation given while core config not set!'
            END IF
            DO i = 1, numberNodes
               WRITE(xPathB,*) TRIM(ADJUSTL(xPathA)),'[',i,']'
               valueString = xmlGetAttributeValue(TRIM(ADJUSTL(xPathB))//'/@state')
               nprncTemp = 0
               kappaTemp = 0
               DO j = 1, 29
                  IF (TRIM(ADJUSTL(valueString)).EQ.coreStateList_const(j)) THEN
                     nprncTemp = coreStateNprncList_const(j)
                     kappaTemp = coreStateKappaList_const(j)
                     speciesXMLPrintCoreStates(j) = .TRUE.
                     speciesXMLCoreOccs(1,j) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB))//'/@spinUp'))
                     speciesXMLCoreOccs(2,j) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB))//'/@spinDown'))
                  END IF
               END DO
               DO j = 1, providedStates
                  IF ((nprncTemp.EQ.coreStateNprnc(j)).AND.(kappaTemp.EQ.coreStateKappa(j))) THEN
                     coreStateOccs(j,1) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB))//'/@spinUp'))
                     coreStateOccs(j,2) = evaluateFirstOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathB))//'/@spinDown'))
                  END IF
               END DO
            END DO
         END IF

         ! local orbitals

         WRITE(xPathA,*) '/fleurInput/atomSpecies/species[',iSpecies,']/lo'
         numberNodes = xmlGetNumberOfNodes(TRIM(ADJUSTL(xPathA)))
         iLLO = 1
         DO iLO = 1, numberNodes
            WRITE(xPathB,*) TRIM(ADJUSTL(xPathA)),'[',iLO,']/@l'
            WRITE(xPathC,*) TRIM(ADJUSTL(xPathA)),'[',iLO,']/@n'
            WRITE(xPathD,*) TRIM(ADJUSTL(xPathA)),'[',iLO,']/@type'
            WRITE(xPathE,*) TRIM(ADJUSTL(xPathA)),'[',iLO,']/@eDeriv'
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathD)))))
            lString = xmlGetAttributeValue(TRIM(ADJUSTL(xPathB)))
            nString = xmlGetAttributeValue(TRIM(ADJUSTL(xPathC)))
            CALL getIntegerSequenceFromString(TRIM(ADJUSTL(lString)), lNumbers, lNumCount)
            CALL getIntegerSequenceFromString(TRIM(ADJUSTL(nString)), nNumbers, nNumCount)
            IF(lNumCount.NE.nNumCount) THEN
               STOP 'Error in LO input: l quantum number count does not equal n quantum number count'
            END IF
            loEDeriv = evaluateFirstIntOnly(xmlGetAttributeValue(TRIM(ADJUSTL(xPathE))))
            DO i = 1, lNumCount
               speciesLLO(iLLO) = lNumbers(i)
               speciesLOeParams(iLLO) = nNumbers(i)
               IF(TRIM(ADJUSTL(valueString)).EQ.'HELO') THEN
                  speciesLOeParams(iLLO) = -speciesLOeParams(iLLO)
               END IF
               speciesLOEDeriv(iLLO) = loEDeriv
               iLLO = iLLO + 1
            END DO
            DEALLOCATE (lNumbers, nNumbers)
         END DO

         ! sort LOs according to l quantum number

         ALLOCATE (loOrderList(speciesNLO(iSpecies)),speciesLLOReal(speciesNLO(iSpecies)))
         DO iLLO = 1, speciesNLO(iSpecies)
            speciesLLOReal(iLLO) = speciesLLO(iLLO)
         END DO
1705
         CALL sort(loOrderList(:speciesNLO(iSpecies)),speciesLLOReal(:speciesNLO(iSpecies)))
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
         DEALLOCATE(speciesLLOReal)

         ! apply species parameters to atom groups

         DO iType = 1, atoms%ntype
            WRITE(xPathA,*) '/fleurInput/atomGroups/atomGroup[',iType,']/@species'
            valueString = TRIM(ADJUSTL(xmlGetAttributeValue(TRIM(ADJUSTL(xPathA)))))
            IF(TRIM(ADJUSTL(atoms%speciesName(iSpecies))).EQ.TRIM(ADJUSTL(valueString))) THEN
               atoms%numStatesProvided(iType) = providedStates
               IF (coreConfigPresent) THEN
                  IF (providedCoreStates.NE.atoms%ncst(iType)) THEN
                     WRITE(6,*) " providedCoreStates:",providedCoreStates
                     WRITE(6,*) "atoms%ncst(iType)  :",atoms%ncst(iType)
                     STOP 'Wrong number of core states provided!'
                  END IF
                  DO k = 1, providedStates !atoms%ncst(iType)
                     atoms%coreStateOccs(k,1,iType) = coreStateOccs(k,1)
                     atoms%coreStateOccs(k,2,iType) = coreStateOccs(k,2)
                     atoms%coreStateNprnc(k,iType) = coreStateNprnc(k)
                     atoms%coreStateKappa(k,iType) = coreStateKappa(k)
                     xmlElectronStates(k,iType) = speciesXMLElectronStates(k)
                     xmlPrintCoreStates(k,iType) = speciesXMLPrintCoreStates(k)
                     xmlCoreOccs (1,k,iType) = speciesXMLCoreOccs(1,k)
                     xmlCoreOccs (2,k,iType) = speciesXMLCoreOccs(2,k)
                  END DO
               END IF
               DO iLLO = 1, speciesNLO(iSpecies)
                  atoms%llo(iLLO,iType) = speciesLLO(loOrderList(iLLO))
                  atoms%ulo_der(iLLO,iType) = speciesLOEDeriv(loOrderList(iLLO))
                  atoms%llod = max(abs(atoms%llo(iLLO,iType)),atoms%llod)
                  DO jsp = 1, input%jspins
                     enpara%ello0(iLLO,iType,jsp) = speciesLOeParams(loOrderList(iLLO))
                     IF (enpara%ello0(iLLO,iType,jsp)==NINT(enpara%ello0(iLLO,iType,jsp))) THEN
                        enpara%qn_ello(iLLO,iType,jsp)=NINT(enpara%ello0(iLLO,iType,jsp))
                        enpara%ello0(iLLO,iType,jsp)=0
                     ELSE
                        enpara%qn_ello(iLLO,iType,jsp)=0
                     ENDIF
                     enpara%skiplo(iType,jsp)=enpara%skiplo(iType,jsp)+(2*atoms%llo(iLLO,itype)+1)
                  END DO
               END DO
               ! Energy parameters
               DO jsp = 1, input%jspins
                  DO l = 0, 3
                     enpara%el0(l,iType,jsp) = speciesEParams(l)
                     IF (enpara%el0(l,iType,jsp)==NINT(enpara%el0(l,iType,jsp))) THEN
                        enpara%qn_el(l,iType,jsp)=nint(enpara%el0(l,iType,jsp))
                        enpara%el0(l,iType,jsp)=0
                     ELSE
                        enpara%qn_el(l,iType,jsp)=0
                     ENDIF
                  END DO
                  DO l = 4,atoms%lmax(iType)
                     enpara%el0(l,iType,jsp) = enpara%el0(3,iType,jsp)
                  END DO
               END DO
               !Hybrid functional stuff
               hybrid%lcutm1(iType) = 4
               hybrid%lcutwf(iType) = atoms%lmax(iType) - atoms%lmax(iType) / 10
               hybrid%select1(:,iType) = (/4, 0, 4, 2 /)
               IF (hybrid%l_hybrid) THEN
                  hybrid%lcutm1(iType)=lcutm
                  hybrid%lcutwf(iType)=lcutwf
                  hybrid%select1(:,iType)=hybSelect
               ENDIF
               ! Explicit xc functional
               SELECT TYPE(xcpot)
               TYPE IS(t_xcpot_inbuild)
                  xcpot%lda_atom(iType)=ldaSpecies
               END SELECT
               noco%socscale(iType)=socscaleSpecies
1777 1778 1779 1780 1781 1782 1783
               IF (field%l_b_field) THEN
                  IF (.NOT.ALLOCATED(field%b_field_mt)) THEN
                     ALLOCATE(field%b_field_mt(atoms%ntype))
                     field%b_field_mt=0.0
                  ENDIF
                  field%b_field_mt(itype)=b_field_mtSpecies
               ENDIF
1784 1785 1786 1787 1788
            END IF
         END DO
         DEALLOCATE(loOrderList)
         DEALLOCATE(speciesLLO,speciesLOeParams,speciesLOEDeriv)
      END DO
1789 1790 1791 1792 1793 1794 1795 1796 1797

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! End of species section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! Start of atomGroup section
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846