pdep_db.f90 7.76 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
!
! Copyright (C) 2015-2016 M. Govoni 
! 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 .
!
! This file is part of WEST.
!
! Contributors to this file: 
! Marco Govoni
!
!-----------------------------------------------------------------------
MODULE pdep_db
  !----------------------------------------------------------------------------
  !
Marco Govoni's avatar
Marco Govoni committed
17
18
19
  USE iotk_module
  USE kinds,     ONLY : DP
  USE io_files,  ONLY : tmp_dir
Marco Govoni's avatar
Marco Govoni committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  !
  IMPLICIT NONE
  !
  !
  CONTAINS
    !
    !
    ! *****************************
    ! PDEP WRITE
    ! *****************************
    !
    !------------------------------------------------------------------------
    SUBROUTINE pdep_db_write( )
      !------------------------------------------------------------------------
      !
Marco Govoni's avatar
Marco Govoni committed
35
      USE xml_io_base,          ONLY : create_directory
Marco Govoni's avatar
Marco Govoni committed
36
37
38
39
      USE mp,                   ONLY : mp_bcast,mp_barrier
      USE mp_world,             ONLY : mpime,root,world_comm
      USE mp_global,            ONLY : my_image_id
      USE io_global,            ONLY : stdout 
Marco Govoni's avatar
Marco Govoni committed
40
      USE westcom,              ONLY : wstat_calculation,n_pdep_times,n_pdep_eigen,n_pdep_maxiter,n_dfpt_maxiter, &
Marco Govoni's avatar
Marco Govoni committed
41
                                     & n_steps_write_restart,n_pdep_restart_from_itr,n_pdep_read_from_file,trev_pdep, &
Marco Govoni's avatar
Marco Govoni committed
42
43
                                     & tr2_dfpt,l_deflate,l_kinetic_only,ev,dvg,west_prefix,trev_pdep_rel, &
                                     & l_minimize_exx_if_active,l_use_ecutrho,wstat_save_dir 
Marco Govoni's avatar
Marco Govoni committed
44
45
46
      USE pdep_io,              ONLY : pdep_merge_and_write_G 
      USE io_push,              ONLY : io_push_bar
      USE distribution_center,  ONLY : pert 
Marco Govoni's avatar
Marco Govoni committed
47
      USE json_module,          ONLY : json_file 
Marco Govoni's avatar
Marco Govoni committed
48
49
50
      !
      IMPLICIT NONE
      !
51
      CHARACTER(LEN=512)    :: fname
Marco Govoni's avatar
Marco Govoni committed
52
53
54
55
56
57
      CHARACTER(LEN=6)      :: my_label
      REAL(DP), EXTERNAL    :: GET_CLOCK
      REAL(DP) :: time_spent(2)
      CHARACTER(20),EXTERNAL :: human_readable_time
      INTEGER :: iunout,global_j,local_j
      INTEGER :: ierr
Govoni's avatar
Govoni committed
58
59
      CHARACTER(12) :: eigenpot_filename(n_pdep_eigen)
      !
Marco Govoni's avatar
Marco Govoni committed
60
61
      TYPE(json_file) :: json 
      INTEGER :: iunit      
Marco Govoni's avatar
Marco Govoni committed
62
63
64
65
66
      !
      ! MPI BARRIER
      !
      CALL mp_barrier(world_comm)
      !
Govoni's avatar
Govoni committed
67
68
69
70
71
72
73
      ! SET FILENAMES
      !
      DO global_j = 1, n_pdep_eigen
         WRITE(my_label,'(i6.6)') global_j
         eigenpot_filename(global_j) = "E"//TRIM(ADJUSTL(my_label))//".json" 
      ENDDO
      !
Marco Govoni's avatar
Marco Govoni committed
74
75
76
77
      ! TIMING
      !
      CALL start_clock('pdep_db')
      time_spent(1)=get_clock('pdep_db')
Marco Govoni's avatar
Marco Govoni committed
78
79
80
      !
      IF ( mpime == root ) THEN
         !
Marco Govoni's avatar
Marco Govoni committed
81
         CALL json%initialize()
Marco Govoni's avatar
Marco Govoni committed
82
         !
Marco Govoni's avatar
Marco Govoni committed
83
84
         CALL add_intput_parameters_to_json_file( 2,(/1,2/) , json )
         ! 
Govoni's avatar
Govoni committed
85
86
         CALL json%add('output.eigenval',ev(1:n_pdep_eigen))
         CALL json%add('output.eigenpot',eigenpot_filename(1:n_pdep_eigen))
Marco Govoni's avatar
Marco Govoni committed
87
         !
Marco Govoni's avatar
Marco Govoni committed
88
89
90
91
         OPEN( NEWUNIT=iunit, FILE=TRIM( wstat_save_dir ) // '/' // TRIM('wstat-save.json') )
         CALL json%print_file( iunit )
         CLOSE( iunit )
         CALL json%destroy()
Marco Govoni's avatar
Marco Govoni committed
92
         !
Marco Govoni's avatar
Marco Govoni committed
93
      ENDIF
Marco Govoni's avatar
Marco Govoni committed
94
95
96
97
98
99
100
101
102
103
      !
      ! 3) CREATE THE EIGENVECTOR FILES
      !
      DO local_j=1,pert%nloc
         !
         ! local -> global
         !
         global_j = pert%l2g(local_j)
         IF(global_j>n_pdep_eigen) CYCLE
         ! 
Govoni's avatar
Govoni committed
104
         fname = TRIM( wstat_save_dir ) // "/"//TRIM(eigenpot_filename(global_j))
Marco Govoni's avatar
Marco Govoni committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
         CALL pdep_merge_and_write_G(fname,dvg(:,local_j))
         !
      ENDDO
      !
      ! MPI BARRIER
      !
      CALL mp_barrier( world_comm )
      !
      ! TIMING
      !
      time_spent(2)=get_clock('pdep_db')
      CALL stop_clock('pdep_db')
      !
      WRITE(stdout,'(  5x," ")')
      CALL io_push_bar()
      WRITE(stdout, "(5x, 'Database written in ',a20)") human_readable_time(time_spent(2)-time_spent(1)) 
Marco Govoni's avatar
Marco Govoni committed
121
      WRITE(stdout, "(5x, 'In location : ',a)") TRIM( wstat_save_dir )  
Marco Govoni's avatar
Marco Govoni committed
122
123
124
125
126
127
128
129
130
131
132
133
134
      CALL io_push_bar()
      !
    END SUBROUTINE
    !
    !
    ! *****************************
    ! PDEP READ
    ! *****************************
    !
    !------------------------------------------------------------------------
    SUBROUTINE pdep_db_read( nglob_to_be_read )
      !------------------------------------------------------------------------
      !
135
      USE westcom,             ONLY : n_pdep_eigen,ev,dvg,west_prefix,npwq0x,wstat_save_dir
Marco Govoni's avatar
Marco Govoni committed
136
137
138
139
140
141
142
      USE io_global,           ONLY : stdout 
      USE mp,                  ONLY : mp_bcast,mp_barrier
      USE mp_world,            ONLY : world_comm,mpime,root
      USE mp_global,           ONLY : my_image_id
      USE pdep_io,             ONLY : pdep_read_G_and_distribute
      USE io_push,             ONLY : io_push_bar
      USE distribution_center, ONLY : pert
Govoni's avatar
Govoni committed
143
      USE json_module,         ONLY : json_file 
Marco Govoni's avatar
Marco Govoni committed
144
145
146
147
148
      !
      IMPLICIT NONE
      !
      INTEGER, INTENT(IN) :: nglob_to_be_read  
      !
149
      CHARACTER(LEN=512) :: dirname,fname
Marco Govoni's avatar
Marco Govoni committed
150
151
152
153
154
155
156
157
      CHARACTER(LEN=6)      :: my_label
      REAL(DP), EXTERNAL    :: GET_CLOCK
      REAL(DP) :: time_spent(2)
      CHARACTER(20),EXTERNAL :: human_readable_time
      INTEGER :: ierr, n_eigen_to_get
      INTEGER :: tmp_n_pdep_eigen
      INTEGER :: dime, iun, global_j, local_j
      REAL(DP),ALLOCATABLE :: tmp_ev(:)
Marco Govoni's avatar
Marco Govoni committed
158
      LOGICAL :: found
Govoni's avatar
Govoni committed
159
160
      TYPE(json_file) :: json
      CHARACTER(12),ALLOCATABLE :: eigenpot_filename(:) 
Marco Govoni's avatar
Marco Govoni committed
161
162
163
164
165
166
167
168
169
170
171
      !
      ! MPI BARRIER
      !
      CALL mp_barrier(world_comm)
      !
      CALL start_clock('pdep_db')
      !
      ! TIMING
      !
      time_spent(1)=get_clock('pdep_db')
      !
Marco Govoni's avatar
Marco Govoni committed
172
173
      ! 1)  READ THE INPUT FILE
      !
Marco Govoni's avatar
Marco Govoni committed
174
      !
Marco Govoni's avatar
Marco Govoni committed
175
      IF ( mpime == root ) THEN
Marco Govoni's avatar
Marco Govoni committed
176
         !
Marco Govoni's avatar
Marco Govoni committed
177
         CALL json%initialize()
178
         CALL json%load_file( filename = TRIM( wstat_save_dir ) // '/' // TRIM('wstat-save.json') )
Marco Govoni's avatar
Marco Govoni committed
179
180
         ! 
         CALL json%get('input.wstat_control.n_pdep_eigen', tmp_n_pdep_eigen, found) 
Govoni's avatar
Govoni committed
181
182
         CALL json%get('output.eigenval', tmp_ev, found)
         CALL json%get('output.eigenpot', eigenpot_filename, found) 
Marco Govoni's avatar
Marco Govoni committed
183
         !
Marco Govoni's avatar
Marco Govoni committed
184
         CALL json%destroy()
Marco Govoni's avatar
Marco Govoni committed
185
186
187
188
         !
      ENDIF
      !
      CALL mp_bcast( tmp_n_pdep_eigen, root, world_comm )
Marco Govoni's avatar
Marco Govoni committed
189
      !
Marco Govoni's avatar
Marco Govoni committed
190
      ! In case nglob_to_be_read is 0, overwrite it with the read value 
Marco Govoni's avatar
Marco Govoni committed
191
192
      !
      IF (nglob_to_be_read==0) THEN 
Marco Govoni's avatar
Marco Govoni committed
193
194
         n_eigen_to_get = tmp_n_pdep_eigen
         n_pdep_eigen=tmp_n_pdep_eigen
Marco Govoni's avatar
Marco Govoni committed
195
      ELSE
Marco Govoni's avatar
Marco Govoni committed
196
         n_eigen_to_get = MIN(tmp_n_pdep_eigen,nglob_to_be_read)
Marco Govoni's avatar
Marco Govoni committed
197
      ENDIF
Marco Govoni's avatar
Marco Govoni committed
198
      !
Marco Govoni's avatar
Marco Govoni committed
199
      IF(.NOT.ALLOCATED(ev)) ALLOCATE(ev(n_eigen_to_get))
Marco Govoni's avatar
Marco Govoni committed
200
      IF ( mpime==root ) ev(1:nglob_to_be_read) = tmp_ev(1:nglob_to_be_read)
Marco Govoni's avatar
Marco Govoni committed
201
202
      CALL mp_bcast( ev, root, world_comm )
      !
Govoni's avatar
Govoni committed
203
204
205
206
207
      IF( mpime /= root ) THEN 
         ALLOCATE( eigenpot_filename(1:tmp_n_pdep_eigen) )
      ENDIF
      CALL mp_bcast(eigenpot_filename,root,world_comm)
      !
Marco Govoni's avatar
Marco Govoni committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
      ! 3)  READ THE EIGENVECTOR FILES
      !
      IF(.NOT.ALLOCATED(dvg)) THEN
         ALLOCATE(dvg(npwq0x,pert%nlocx))
         dvg = 0._DP
      ENDIF
      !
      DO local_j=1,pert%nloc
         !
         ! local -> global
         !
         global_j = pert%l2g(local_j)
         IF(global_j>n_eigen_to_get) CYCLE
         ! 
Govoni's avatar
Govoni committed
222
         fname = TRIM( dirname ) // "/"//TRIM(eigenpot_filename(global_j))
Marco Govoni's avatar
Marco Govoni committed
223
224
225
226
227
228
229
         CALL pdep_read_G_and_distribute(fname,dvg(:,local_j))
         !
      ENDDO
      !
      ! MPI BARRIER
      !
      CALL mp_barrier( world_comm )
230
      DEALLOCATE( eigenpot_filename ) 
Marco Govoni's avatar
Marco Govoni committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      !
      ! TIMING
      !
      time_spent(2)=get_clock('pdep_db')
      CALL stop_clock('pdep_db')
      !
      WRITE(stdout,'(  5x," ")')
      CALL io_push_bar()
      WRITE(stdout, "(5x, 'Database read in ',a20)") human_readable_time(time_spent(2)-time_spent(1)) 
      WRITE(stdout, "(5x, 'In location : ',a)") TRIM( dirname )  
      WRITE(stdout, "(5x, 'Eigen. found : ',i12)") n_eigen_to_get
      CALL io_push_bar()
      !
    END SUBROUTINE
    !
END MODULE