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
41
42
43
      !
      ! I/O
      !    
      CHARACTER(*), INTENT(IN) :: fname
44
45
      COMPLEX(DP), INTENT(IN) :: pdepg(npwqx)
      INTEGER, INTENT(IN), OPTIONAL :: iq
Marco Govoni's avatar
Marco Govoni committed
46
      !
Govoni's avatar
Govoni committed
47
      ! Workspace
Marco Govoni's avatar
Marco Govoni committed
48
49
      !
      COMPLEX(DP),ALLOCATABLE :: tmp_vec(:)
50
      INTEGER :: iun,ierr,ig
Govoni's avatar
Govoni committed
51
      CHARACTER(LEN=:),ALLOCATABLE :: charbase64
52
      INTEGER :: nbytes, ndim, iunit, nlen
Govoni's avatar
Govoni committed
53
      CHARACTER(LEN=30) :: endian
54
      INTEGER :: npwqx_g 
55
      INTEGER, ALLOCATABLE :: igq_l2g_kdip(:), igq_l2g(:)
56
      INTEGER, PARAMETER :: default_iq = 1
Marco Govoni's avatar
Marco Govoni committed
57
      INTEGER :: iq_ 
Marco Govoni's avatar
Marco Govoni committed
58
      !
59
      IF( PRESENT(iq) ) THEN 
60
         iq_ = iq
61
62
      ELSE
         iq_ = default_iq
63
      ENDIF 
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
121
122
123
124
      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
125
      !
126
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
      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
168
169
170
171
172
173
174
175
    END SUBROUTINE
    !
    ! ******************************************
    ! READ IN G SPACE 
    !       wfc is read merged in G space
    !       then split in G space
    ! ******************************************
    !
176
    SUBROUTINE pdep_read_G_and_distribute(fname,pdepg,iq)
Marco Govoni's avatar
Marco Govoni committed
177
178
      !
      USE mp_wave,      ONLY : splitwf
179
      USE mp,           ONLY : mp_bcast, mp_max
Marco Govoni's avatar
Marco Govoni committed
180
      USE mp_global,    ONLY : intra_bgrp_comm
Govoni's avatar
Govoni committed
181
      USE base64_module
Marco Govoni's avatar
Marco Govoni committed
182
183
184
185
      !
      ! I/O
      !    
      CHARACTER(*), INTENT(IN) :: fname
186
187
      COMPLEX(DP), INTENT(OUT) :: pdepg(npwqx)
      INTEGER, INTENT(IN), OPTIONAL :: iq
Marco Govoni's avatar
Marco Govoni committed
188
      !
Govoni's avatar
Govoni committed
189
      ! Workspace
Marco Govoni's avatar
Marco Govoni committed
190
      !
Govoni's avatar
Govoni committed
191
      TYPE(json_file) :: json
Marco Govoni's avatar
Marco Govoni committed
192
193
      COMPLEX(DP),ALLOCATABLE :: tmp_vec(:)
      INTEGER :: iun,ierr,ig
Govoni's avatar
Govoni committed
194
      CHARACTER(LEN=1000) :: line
Govoni's avatar
Govoni committed
195
      CHARACTER(LEN=:),ALLOCATABLE :: charbase64
Govoni's avatar
Govoni committed
196
      CHARACTER(LEN=:),ALLOCATABLE :: endian
197
      INTEGER :: nbytes, ndim, iunit, nlen
Govoni's avatar
Govoni committed
198
      LOGICAL :: found, isle
199
200
      INTEGER :: npwqx_g
      INTEGER, ALLOCATABLE :: igq_l2g_kdip(:), igq_l2g(:) 
201
202
      INTEGER, PARAMETER :: default_iq = 1
      INTEGER :: iq_ 
Marco Govoni's avatar
Marco Govoni committed
203
      !
204
      IF( PRESENT(iq) ) THEN 
205
         iq_ = iq
206
      ELSE
207
         iq_ = default_iq
208
      ENDIF 
209
      !
210
      IF ( .NOT. gamma_only ) THEN
211
         !
212
         ! Resume all components 
213
         !
214
215
216
217
218
219
220
221
222
223
         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
224
            !
225
            ! ONLY ROOT W/IN BGRP READS
226
            !
227
228
            nbytes = SIZEOF(tmp_vec(1)) * ndim
            nlen = lenbase64(nbytes)
229
            !
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
            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
250
251
            !
            !
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
            ! <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
271
         !
272
         DEALLOCATE( tmp_vec )
273
         !
274
275
         CALL mp_bcast(pdepg,0,inter_bgrp_comm)
         CALL mp_bcast(pdepg,0,inter_pool_comm)
276
      !
277
278
279
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
      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)
322
      !
323
324
      ENDIF
    !
Marco Govoni's avatar
Marco Govoni committed
325
326
327
    END SUBROUTINE
    !
END MODULE