upf.f90 2.6 KB
Newer Older
1
! Copyright (C) 2008 Quantum ESPRESSO group
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
! This file is distributed under the terms of the
! GNU General Public License. See the file `License'
! in the root directory of the present distribution,
! or http://www.gnu.org/copyleft/gpl.txt .
!
!=----------------------------------------------------------------------------=!
      MODULE upf_module
!=----------------------------------------------------------------------------=!
!  this module handles reading and writing of unified pseudopotential format (UPF)
!  it can manage v2 read/write and v1 read only.
!
! A macro to trim both from left and right
#define TRIM(a) trim(adjustl(a))
      !
      USE kinds,        ONLY: DP
17
      USE pseudo_types, ONLY: pseudo_upf, deallocate_pseudo_upf
18 19 20 21 22 23 24 25 26 27 28 29 30
      USE iotk_module
      !
      USE read_upf_v1_module
      USE read_upf_v2_module
      !
      IMPLICIT NONE
      PUBLIC
      !
      CONTAINS

!------------------------------------------------+
SUBROUTINE read_upf(upf, grid, ierr, unit, filename)             !
   !---------------------------------------------+
31 32 33 34
   ! Read pseudopotential in UPF format (either v.1 or v.2)
   ! ierr = -1 : read UPF v.1 
   ! ierr =  0 : read UPF v.2 
   ! ierr =  1 : not an UPF file, or error while reading
35
   !
36
   USE radial_grids, ONLY: radial_grid_type, deallocate_radial_grid
37 38 39 40 41 42 43 44 45 46 47 48 49 50
   USE read_upf_v1_module,ONLY: read_upf_v1
   IMPLICIT NONE
   INTEGER,INTENT(IN),OPTIONAL             :: unit      ! i/o unit
   CHARACTER(len=*),INTENT(IN),OPTIONAL    :: filename  ! i/o filename
   TYPE(pseudo_upf),INTENT(INOUT) :: upf       ! the pseudo data
   TYPE(radial_grid_type),OPTIONAL,INTENT(INOUT),TARGET :: grid
   INTEGER,INTENT(OUT) :: ierr
   !
   INTEGER :: u         ! i/o unit

   ierr = 0

   IF(.not. present(unit)) THEN
      IF (.not. present(filename)) &
giannozz's avatar
giannozz committed
51
         CALL errore('read_upf',&
52 53 54 55 56 57 58 59 60 61 62
         'You have to specify at least one between filename and unit',1)
      CALL iotk_free_unit(u)
   ELSE
      u = unit
   ENDIF
   !
   IF(present(filename)) &
      open (unit = u, file = filename, status = 'old', form = &
      'formatted', iostat = ierr)
   IF(ierr>0) CALL errore('read_upf', 'Cannot open file: '//TRIM(filename),1)
   !
63
   CALL read_upf_v2( u, upf, grid, ierr )
64 65 66
   !
   IF(ierr>0) THEN
      REWIND(u)
67 68 69
      CALL deallocate_pseudo_upf( upf )
      CALL deallocate_radial_grid( grid )
      CALL read_upf_v1( u, upf, grid, ierr )
70
      IF(ierr==0) ierr=-1
71 72 73 74 75 76 77 78 79 80 81
   ENDIF

   RETURN

END SUBROUTINE read_upf

!=----------------------------------------------------------------------------=!
      END MODULE upf_module
!=----------------------------------------------------------------------------=!
#undef TRIM