upf.f90 3.7 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 31 32 33
      USE iotk_module
      !
      USE read_upf_v1_module
      USE read_upf_v2_module
      USE write_upf_v2_module
      !
      IMPLICIT NONE
      PUBLIC
      !
      CONTAINS

!------------------------------------------------+
SUBROUTINE read_upf(upf, grid, ierr, unit, filename)             !
   !---------------------------------------------+
   ! Read pseudopotential in UPF format version 2, uses iotk
   !
34
   USE radial_grids, ONLY: radial_grid_type, deallocate_radial_grid
35 36 37 38 39 40 41 42 43 44 45 46 47 48
   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
49
         CALL errore('read_upf',&
50 51 52 53 54 55 56 57 58 59 60
         '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)
   !
61
   CALL read_upf_v2( u, upf, grid, ierr )
62 63 64
   !
   IF(ierr>0) THEN
      REWIND(u)
65 66 67
      CALL deallocate_pseudo_upf( upf )
      CALL deallocate_radial_grid( grid )
      CALL read_upf_v1( u, upf, grid, ierr )
68 69 70 71 72 73 74
   ENDIF

   RETURN

END SUBROUTINE read_upf

!------------------------------------------------+
75
SUBROUTINE write_upf(upf, conf, unit, filename)             !
76
   !---------------------------------------------+
giannozz's avatar
giannozz committed
77
   ! Write pseudopotential in UPF format version 2, uses iotk
78 79
   !
   IMPLICIT NONE
80 81 82 83
   TYPE(pseudo_upf),INTENT(IN)            :: upf       ! the pseudo data
   TYPE(pseudo_config),OPTIONAL,INTENT(IN):: conf      ! the pseudo GENERATION data
   INTEGER,INTENT(IN),OPTIONAL            :: unit      ! i/o unit
   CHARACTER(len=*),INTENT(IN),OPTIONAL   :: filename  ! i/o filename
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
   !
   INTEGER :: u, ierr ! i/o unit and error handler

   ierr = 0

   IF(.not. present(unit)) THEN
      IF (.not. present(filename)) &
         CALL errore('read_upf_v2',&
         '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 = 'unknown', form = &
      'formatted', iostat = ierr)
   IF(ierr>0) CALL errore('write_upf', 'Cannot open file: '//TRIM(filename),1)
   !
103
   CALL write_upf_v2( u, upf, conf )
104 105 106 107 108 109 110 111 112 113 114 115
   !
   IF(ierr>0) &
      CALL errore('write_upf','Errore while writing pseudopotential file',1)

END SUBROUTINE write_upf


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