pdep_io.f90 10.4 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
!
2
! Copyright (C) 2015-2017 M. Govoni 
Marco Govoni's avatar
Marco Govoni committed
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 .
!
! This file is part of WEST.
!
! Contributors to this file: 
! Marco Govoni
!
!-----------------------------------------------------------------------
MODULE pdep_io
  !----------------------------------------------------------------------------
  !
Govoni's avatar
Govoni committed
17
  USE kinds,        ONLY : DP
18
19
  USE mp_global,    ONLY : me_bgrp,root_bgrp,nproc_bgrp,intra_bgrp_comm,my_pool_id,my_bgrp_id,inter_bgrp_comm,inter_pool_comm,&
                           & intra_pool_comm
20
  USE westcom,      ONLY : npwq, npwq_g, npwqx, ngq, ngq_g, npwqx, igq_q !, igq_l2g_kdip
Govoni's avatar
Govoni committed
21
22
  USE gvect,        ONLY : ig_l2g
  USE json_module,  ONLY : json_file
23
  USE control_flags,   ONLY : gamma_only
Govoni's avatar
Govoni committed
24
  USE base64_module 
Marco Govoni's avatar
Marco Govoni committed
25
26
27
28
29
30
31
32
33
34
35
  !
  IMPLICIT NONE
  !
  CONTAINS
    !
    ! ******************************************
    ! WRITE IN G SPACE 
    !       wfc is passed distributed in G space
    !       then merged and written in R space
    ! ******************************************
    !
36
    SUBROUTINE pdep_merge_and_write_G(fname,pdepg,iq)
Marco Govoni's avatar
Marco Govoni committed
37
38
      !
      USE mp_wave,      ONLY : mergewf
39
      USE mp,           ONLY : mp_bcast, mp_max
Marco Govoni's avatar
Marco Govoni committed
40
      !
Victor Yu's avatar
Victor Yu committed
41
42
      IMPLICIT NONE
      !
Marco Govoni's avatar
Marco Govoni committed
43
44
45
      ! I/O
      !    
      CHARACTER(*), INTENT(IN) :: fname
46
47
      COMPLEX(DP), INTENT(IN) :: pdepg(npwqx)
      INTEGER, INTENT(IN), OPTIONAL :: iq
Marco Govoni's avatar
Marco Govoni committed
48
      !
Govoni's avatar
Govoni committed
49
      ! Workspace
Marco Govoni's avatar
Marco Govoni committed
50
51
      !
      COMPLEX(DP),ALLOCATABLE :: tmp_vec(:)
52
      INTEGER :: iun,ierr,ig
Govoni's avatar
Govoni committed
53
      CHARACTER(LEN=:),ALLOCATABLE :: charbase64
54
      INTEGER :: nbytes, ndim, iunit, nlen
Govoni's avatar
Govoni committed
55
      CHARACTER(LEN=30) :: endian
56
      INTEGER :: npwqx_g 
57
      INTEGER, ALLOCATABLE :: igq_l2g_kdip(:), igq_l2g(:)
58
      INTEGER, PARAMETER :: default_iq = 1
Marco Govoni's avatar
Marco Govoni committed
59
      INTEGER :: iq_ 
Marco Govoni's avatar
Marco Govoni committed
60
      !
61
      IF( PRESENT(iq) ) THEN 
62
         iq_ = iq
63
64
      ELSE
         iq_ = default_iq
65
      ENDIF 
66
      !
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
      IF ( .NOT. gamma_only) THEN
         !
         ! Resume all components 
         !
         ndim = ngq_g(iq_)
         !
         ! <NEW>
         !
         npwqx_g = MAXVAL( ngq_g(:) )
         ALLOCATE( igq_l2g_kdip(npwqx_g) )
         igq_l2g_kdip(:) = 0
         !
         ALLOCATE( igq_l2g(ngq(iq_)) )
         DO ig = 1, ngq(iq_)
            igq_l2g(ig) = ig_l2g( igq_q(ig,iq_) )
         ENDDO
         CALL gq_l2gmap_kdip( npwq_g, ngq_g(iq_), ngq(iq_), igq_l2g, igq_l2g_kdip )
         DEALLOCATE( igq_l2g )
         !
         ! </NEW>
         ! 
!         npwq_g = MAXVAL(igq_l2g_kdip(1:ndim,iq))
!         CALL mp_max(npwq_g,intra_pool_comm)
!         CALL mp_max(npwq_g,intra_bgrp_comm)
         !
         ALLOCATE( tmp_vec(npwq_g) )
         tmp_vec=0._DP
         !
         CALL mergewf( pdepg(:), tmp_vec, npwq, igq_l2g_kdip, me_bgrp, nproc_bgrp, root_bgrp, intra_bgrp_comm)
         DEALLOCATE( igq_l2g_kdip ) 
         !
         ! ONLY ROOT W/IN BGRP WRITES
         !
         IF(me_bgrp==root_bgrp) THEN 
            !
            nbytes = SIZEOF(tmp_vec(1)) * ndim
            nlen = lenbase64(nbytes)
            ALLOCATE(CHARACTER(LEN=nlen) :: charbase64)
            CALL base64_encode_complex(tmp_vec(1:ndim), ndim, charbase64) 
            !
            IF( islittleendian() ) THEN 
               endian = '"islittleendian" : true'
            ELSE
               endian = '"islittleendian" : false'
            ENDIF  
            !
            OPEN( NEWUNIT=iunit, FILE = TRIM(fname) )
            WRITE( iunit, '(a)' ) '{'
            WRITE( iunit, '(a,i0,a)' ) '"meta" : { "name" : "eigenpotential", "type" : "complex double", "space" : "G",&
                         "ndim" : ', ndim, ', "encoding" : "base64", '//TRIM(endian)//' }'
            WRITE( iunit, '(a)') ', "data" : ' 
            WRITE( iunit, '(a)' ) '"'//charbase64//'"'
            WRITE( iunit, '(a)' ) '}'
            CLOSE( iunit ) 
            !
            DEALLOCATE( charbase64 )
            !
         END IF
         !
         DEALLOCATE( tmp_vec )
Marco Govoni's avatar
Marco Govoni committed
127
      !
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
      ELSE
         !
         ! Resume all components 
         !
         ALLOCATE( tmp_vec(npwq_g) )
         tmp_vec=0._DP
         !
         CALL mergewf( pdepg(:), tmp_vec, npwq, ig_l2g(1:npwq), me_bgrp, nproc_bgrp, root_bgrp, intra_bgrp_comm) 
         !
         ! ONLY ROOT W/IN BGRP WRITES
         !
         IF(me_bgrp==root_bgrp) THEN 
            !
            ndim = npwq_g
            nbytes = SIZEOF(tmp_vec(1)) * ndim
            nlen = lenbase64(nbytes)
            ALLOCATE(CHARACTER(LEN=nlen) :: charbase64)
            CALL base64_encode_complex(tmp_vec(1:ndim), ndim, charbase64) 
            !
            IF( islittleendian() ) THEN 
               endian = '"islittleendian" : true'
            ELSE
               endian = '"islittleendian" : false'
            ENDIF  
            !
            OPEN( NEWUNIT=iunit, FILE = TRIM(fname) )
            WRITE( iunit, '(a)' ) '{'
            WRITE( iunit, '(a,i0,a)' ) '"meta" : { "name" : "eigenpotential", "type" : "complex double", "space" : "G",&
                         "ndim" : ', ndim, ', "encoding" : "base64", '//TRIM(endian)//' }'
            WRITE( iunit, '(a)') ', "data" : ' 
            WRITE( iunit, '(a)' ) '"'//charbase64//'"'
            WRITE( iunit, '(a)' ) '}'
            CLOSE( iunit ) 
            !
            DEALLOCATE( charbase64 )
            !
         ENDIF
         !
         DEALLOCATE( tmp_vec )
         !
      ENDIF
    !
Marco Govoni's avatar
Marco Govoni committed
170
171
172
173
174
175
176
177
    END SUBROUTINE
    !
    ! ******************************************
    ! READ IN G SPACE 
    !       wfc is read merged in G space
    !       then split in G space
    ! ******************************************
    !
178
    SUBROUTINE pdep_read_G_and_distribute(fname,pdepg,iq)
Marco Govoni's avatar
Marco Govoni committed
179
180
      !
      USE mp_wave,      ONLY : splitwf
181
      USE mp,           ONLY : mp_bcast, mp_max
Marco Govoni's avatar
Marco Govoni committed
182
      USE mp_global,    ONLY : intra_bgrp_comm
Govoni's avatar
Govoni committed
183
      USE base64_module
Marco Govoni's avatar
Marco Govoni committed
184
      !
Victor Yu's avatar
Victor Yu committed
185
186
      IMPLICIT NONE
      !
Marco Govoni's avatar
Marco Govoni committed
187
188
189
      ! I/O
      !    
      CHARACTER(*), INTENT(IN) :: fname
190
191
      COMPLEX(DP), INTENT(OUT) :: pdepg(npwqx)
      INTEGER, INTENT(IN), OPTIONAL :: iq
Marco Govoni's avatar
Marco Govoni committed
192
      !
Govoni's avatar
Govoni committed
193
      ! Workspace
Marco Govoni's avatar
Marco Govoni committed
194
      !
Govoni's avatar
Govoni committed
195
      TYPE(json_file) :: json
Marco Govoni's avatar
Marco Govoni committed
196
197
      COMPLEX(DP),ALLOCATABLE :: tmp_vec(:)
      INTEGER :: iun,ierr,ig
Govoni's avatar
Govoni committed
198
      CHARACTER(LEN=1000) :: line
Govoni's avatar
Govoni committed
199
      CHARACTER(LEN=:),ALLOCATABLE :: charbase64
Govoni's avatar
Govoni committed
200
      CHARACTER(LEN=:),ALLOCATABLE :: endian
201
      INTEGER :: nbytes, ndim, iunit, nlen
Govoni's avatar
Govoni committed
202
      LOGICAL :: found, isle
203
204
      INTEGER :: npwqx_g
      INTEGER, ALLOCATABLE :: igq_l2g_kdip(:), igq_l2g(:) 
205
206
      INTEGER, PARAMETER :: default_iq = 1
      INTEGER :: iq_ 
Marco Govoni's avatar
Marco Govoni committed
207
      !
208
      IF( PRESENT(iq) ) THEN 
209
         iq_ = iq
210
      ELSE
211
         iq_ = default_iq
212
      ENDIF 
213
      !
214
      IF ( .NOT. gamma_only ) THEN
215
         !
216
         ! Resume all components 
217
         !
218
219
220
221
222
223
224
225
226
227
         ndim = ngq_g(iq_)
!         npwq_g = MAXVAL(igq_l2g_kdip(1:ndim,iq))
!         CALL mp_max(npwq_g,intra_pool_comm)
!         CALL mp_max(npwq_g,intra_bgrp_comm)
         !
         ALLOCATE( tmp_vec(npwq_g) )
         tmp_vec=0._DP
         pdepg=0._DP
         !
         IF(my_pool_id==0.AND.my_bgrp_id==0) THEN
228
            !
229
            ! ONLY ROOT W/IN BGRP READS
230
            !
231
232
            nbytes = SIZEOF(tmp_vec(1)) * ndim
            nlen = lenbase64(nbytes)
233
            !
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
            IF(me_bgrp==root_bgrp) THEN 
               !
               ALLOCATE(CHARACTER(LEN=(nlen+2)) :: charbase64)
               !
               OPEN( NEWUNIT=iunit, FILE = TRIM(fname) )
               READ( iunit, * ) 
               READ( iunit, '(a)' ) line 
               READ( iunit, * ) 
               READ( iunit, '(a)' ) charbase64
               CLOSE( iunit )
               CALL base64_decode_complex(charbase64(2:(nlen+1)), ndim, tmp_vec(1:ndim)) 
               DEALLOCATE( charbase64 )
               !
               CALL json%load_from_string("{"//TRIM(line)//"}")
               CALL json%get('meta.islittleendian', isle, found)
               CALL json%destroy()
               !
               IF (islittleendian() .NEQV. isle) CALL base64_byteswap_complex(nbytes,tmp_vec(1:ndim))
               !
            END IF
254
255
            !
            !
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
            ! <NEW>
            !
            npwqx_g = MAXVAL( ngq_g(:) )
            ALLOCATE( igq_l2g_kdip(npwqx_g) )
            igq_l2g_kdip(:) = 0
            !
            ALLOCATE( igq_l2g(ngq(iq_)) )
            DO ig = 1, ngq(iq_)
               igq_l2g(ig) = ig_l2g( igq_q(ig,iq_) )
            ENDDO
            CALL gq_l2gmap_kdip( npwq_g, ngq_g(iq_), ngq(iq_), igq_l2g, igq_l2g_kdip )
            DEALLOCATE( igq_l2g )
            !
            ! </NEW>
            ! 
            CALL splitwf( pdepg, tmp_vec, npwq, igq_l2g_kdip, me_bgrp, nproc_bgrp, root_bgrp, intra_bgrp_comm) 
            DEALLOCATE( igq_l2g_kdip ) 
            !
         ENDIF
275
         !
276
         DEALLOCATE( tmp_vec )
277
         !
278
279
         CALL mp_bcast(pdepg,0,inter_bgrp_comm)
         CALL mp_bcast(pdepg,0,inter_pool_comm)
280
      !
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
      ELSE
         !
         ! Resume all components 
         !
         ALLOCATE( tmp_vec(npwq_g) )
         tmp_vec=0._DP
         pdepg=0._DP
         !
         IF(my_pool_id==0.AND.my_bgrp_id==0) THEN
            !
            ! ONLY ROOT W/IN BGRP READS
            !
            ndim = npwq_g
            nbytes = SIZEOF(tmp_vec(1)) * ndim
            nlen = lenbase64(nbytes)
            !
            IF(me_bgrp==root_bgrp) THEN 
               !
               ALLOCATE(CHARACTER(LEN=(nlen+2)) :: charbase64)
               !
               OPEN( NEWUNIT=iunit, FILE = TRIM(fname) )
               READ( iunit, * ) 
               READ( iunit, '(a)' ) line 
               READ( iunit, * ) 
               READ( iunit, '(a)' ) charbase64
               CLOSE( iunit )
               CALL base64_decode_complex(charbase64(2:(nlen+1)), ndim, tmp_vec(1:ndim)) 
               DEALLOCATE( charbase64 )
               !
               CALL json%load_from_string("{"//TRIM(line)//"}")
               CALL json%get('meta.islittleendian', isle, found)
               CALL json%destroy()
               !
               IF (islittleendian() .NEQV. isle) CALL base64_byteswap_complex(nbytes,tmp_vec(1:ndim))
               !
            END IF
            !
            CALL splitwf( pdepg, tmp_vec, npwq, ig_l2g(1:npwq), me_bgrp, nproc_bgrp, root_bgrp, intra_bgrp_comm) 
            !
         ENDIF
         !
         DEALLOCATE( tmp_vec )
         !
         CALL mp_bcast(pdepg,0,inter_bgrp_comm)
         CALL mp_bcast(pdepg,0,inter_pool_comm)
326
      !
327
328
      ENDIF
    !
Marco Govoni's avatar
Marco Govoni committed
329
330
331
    END SUBROUTINE
    !
END MODULE