function3d.f90 9.8 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
17
18
19
20
21
22
23
24
25
26
27
28
29
!
! Copyright (C) 2015-2017 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 function3d 
 ! -----------------------------------------------------------------
 !
 IMPLICIT NONE
 !
 CONTAINS
 ! 
 !-----------------------------------------------------------------
   SUBROUTINE write_function3d ( fname, nx, ny, nz, ng, ngx, funct3d_g )
   ! -----------------------------------------------------------------
   !
   USE kinds,           ONLY : DP
   USE cell_base,       ONLY : celldm, at
   USE control_flags,   ONLY : gamma_only
   USE mp_bands,        ONLY : me_bgrp
   USE base64_module
30
   USE fourier_interpolation
Marco Govoni's avatar
Marco Govoni committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
   !
   IMPLICIT NONE
   !
   ! I/O 
   !
   CHARACTER(LEN=*),INTENT(IN) :: fname
   INTEGER, INTENT(IN) :: nx, ny, nz, ng, ngx
   COMPLEX(DP),INTENT(IN) :: funct3d_g(ngx)
   ! 
   ! Workspace
   !
   INTEGER              :: iu, ndim, nbytes, nlen, nmaps, i
   COMPLEX(DP),ALLOCATABLE :: funct3d_r_complex(:)
   REAL(DP),ALLOCATABLE :: funct3d_r_double(:)
   INTEGER, ALLOCATABLE :: nl(:,:)
   CHARACTER(LEN=14)    :: lab(3)
   CHARACTER(LEN=:),ALLOCATABLE :: charbase64
   CHARACTER(LEN=:),ALLOCATABLE :: ctype
   !
   ! 1) Fourier interpolate funct3_g --> funct3d_r
   !
   IF( gamma_only ) THEN 
      nmaps = 2
   ELSE 
      nmaps = 1 
   ENDIF
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
57
   ALLOCATE( nl(ngx,nmaps) )
58
   CALL get_G2R_mapping (nx, ny, nz, ng, ngx, nmaps, nl)
Marco Govoni's avatar
Marco Govoni committed
59
   ALLOCATE( funct3d_r_complex(nx*ny*nz) )
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
60
   CALL single_invfft_toArbitraryRGrid (funct3d_r_complex, nx, ny, nz, ng, ngx, nmaps, nl, funct3d_g)
Marco Govoni's avatar
Marco Govoni committed
61
62
63
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
   DEALLOCATE( nl )
   !
   IF( me_bgrp == 0 ) THEN
      !
      ! 2) Encode 
      !
      IF( gamma_only ) THEN 
         ALLOCATE( funct3d_r_double( nx*ny*nz ) ) 
         funct3d_r_double = REAL(funct3d_r_complex(:),KIND=DP)
         DEALLOCATE(funct3d_r_complex)
         ndim = nx*ny*nz
         nbytes = SIZEOF(funct3d_r_double(1)) * ndim
         nlen = lenbase64(nbytes)
         ALLOCATE(CHARACTER(LEN=nlen) :: charbase64)
         IF (.NOT. islittleendian()) CALL base64_byteswap_double(nbytes,funct3d_r_double(1:ndim))
         CALL base64_encode_double(funct3d_r_double(1:ndim), ndim, charbase64)
         DEALLOCATE(funct3d_r_double)
         ctype = "double"
      ELSE
         ndim = nx*ny*nz
         nbytes = SIZEOF(funct3d_r_complex(1)) * ndim
         nlen = lenbase64(nbytes)
         ALLOCATE(CHARACTER(LEN=nlen) :: charbase64)
         IF (.NOT. islittleendian()) CALL base64_byteswap_complex(nbytes,funct3d_r_complex(1:ndim))
         CALL base64_encode_complex(funct3d_r_complex(1:ndim), ndim, charbase64)
         DEALLOCATE(funct3d_r_complex)
         ctype = "complex"
      ENDIF
      !
      ! 3) Write 
      !
      OPEN(NEWUNIT=iu,FILE=TRIM(ADJUSTL(fname)))
      !
      WRITE(iu,'(a)') '<?xml version="1.0" encoding="UTF-8"?>'
      WRITE(iu,'(a)') '<fpmd:function3d xmlns:fpmd="http://www.quantum-simulation.org/ns/fpmd/fpmd-1.0"'
      WRITE(iu,'(a)') 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
      WRITE(iu,'(a)') 'xsi:schemaLocation="http://www.quantum-simulation.org/ns/fpmd/fpmd-1.0 function3d.xsd"'
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
98
      WRITE(iu,'(a)') 'name="delta_v">'
Marco Govoni's avatar
Marco Govoni committed
99
100
101
      DO i = 1, 3
         WRITE(lab(i),'(f14.6)') celldm(1) * at(i,1) 
      ENDDO     
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
102
      WRITE(iu,'(a)') '<domain a="'//TRIM(ADJUSTL(lab(1)))//" "//TRIM(ADJUSTL(lab(2)))//" "//TRIM(ADJUSTL(lab(3)))//'"'
Marco Govoni's avatar
Marco Govoni committed
103
104
105
      DO i = 1, 3
         WRITE(lab(i),'(f14.6)') celldm(1) * at(i,2) 
      ENDDO     
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
106
      WRITE(iu,'(a)') 'b="'//TRIM(ADJUSTL(lab(1)))//" "//TRIM(ADJUSTL(lab(2)))//" "//TRIM(ADJUSTL(lab(3)))//'"'
Marco Govoni's avatar
Marco Govoni committed
107
108
109
      DO i = 1, 3
         WRITE(lab(i),'(f14.6)') celldm(1) * at(i,3) 
      ENDDO     
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
110
      WRITE(iu,'(a)') 'c="'//TRIM(ADJUSTL(lab(1)))//" "//TRIM(ADJUSTL(lab(2)))//" "//TRIM(ADJUSTL(lab(3)))//'"/>'
Marco Govoni's avatar
Marco Govoni committed
111
112
113
      WRITE(lab(1),'(i14)') nx
      WRITE(lab(2),'(i14)') ny
      WRITE(lab(3),'(i14)') nz
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
114
115
116
      WRITE(iu,'(a)') '<grid nx="'//TRIM(ADJUSTL(lab(1)))//'" ny="'//TRIM(ADJUSTL(lab(2)))//'" nz="'//TRIM(ADJUSTL(lab(3)))//'"/>'
      WRITE(iu,'(a)') '<grid_function type="'//ctype//'" nx="'//TRIM(ADJUSTL(lab(1)))//'" ny="'//TRIM(ADJUSTL(lab(2)))// &
            &'" nz="'//TRIM(ADJUSTL(lab(3)))//'" encoding="base64">'
mahe's avatar
mahe committed
117
      CALL write_long_string(iu,charbase64) 
Marco Govoni's avatar
Marco Govoni committed
118
      WRITE(iu,'(a)') '</grid_function>'
119
      WRITE(iu,'(a)') '</fpmd:function3d>'
Marco Govoni's avatar
Marco Govoni committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
      !
      CLOSE(iu)
      !
      DEALLOCATE(charbase64)
      DEALLOCATE(ctype)
      !
   ELSE 
      DEALLOCATE( funct3d_r_complex )
   ENDIF 
   !
   END SUBROUTINE 
 ! 
 !-----------------------------------------------------------------
   SUBROUTINE read_function3d ( fname, nx, ny, nz, ng, ngx, funct3d_g )
   ! -----------------------------------------------------------------
   !
   USE kinds,           ONLY : DP
   USE control_flags,   ONLY : gamma_only
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
138
139
   USE mp,              ONLY : mp_bcast
   USE mp_bands,        ONLY : me_bgrp, intra_bgrp_comm
Marco Govoni's avatar
Marco Govoni committed
140
   USE base64_module
141
   USE fourier_interpolation
Marco Govoni's avatar
Marco Govoni committed
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
   !
   IMPLICIT NONE
   !
   ! I/O 
   !
   CHARACTER(LEN=*),INTENT(IN) :: fname
   INTEGER,INTENT(OUT) :: nx, ny, nz
   INTEGER,INTENT(IN) :: ng, ngx
   COMPLEX(DP),INTENT(OUT) :: funct3d_g(ngx)
   ! 
   ! Workspace
   !
   INTEGER :: iu, i, nlen, ndim, nbytes, nmaps 
   COMPLEX(DP),ALLOCATABLE :: funct3d_r_complex(:)
   REAL(DP),ALLOCATABLE :: funct3d_r_double(:)
   INTEGER :: nlines, j, is, ie, ios, nline_start, nline_end
   CHARACTER(LEN=256) :: buffline
   CHARACTER(LEN=:),ALLOCATABLE :: bufftag
   CHARACTER(LEN=:),ALLOCATABLE :: buff2
   CHARACTER(LEN=:),ALLOCATABLE :: charbase64
   LOGICAL :: lread 
   LOGICAL :: lstop
   INTEGER, ALLOCATABLE :: nl(:,:)
   CHARACTER(LEN=:),ALLOCATABLE :: ctype 
   !
   IF( me_bgrp == 0 ) THEN
      !
      OPEN(NEWUNIT=iu,FILE=TRIM(ADJUSTL(fname)))
      !
      DO
         READ(iu,'(a)',IOSTAT=ios) buffline
         IF( ios /=0 ) EXIT
         IF( INDEX(buffline,"<grid_function ") /= 0 ) THEN
            bufftag = TRIM(buffline)
            EXIT
         ENDIF
      ENDDO
      !
      DO WHILE ( INDEX(bufftag,">") == 0 )
         READ(iu,'(a)',IOSTAT=ios) buffline
         IF( ios /=0 ) EXIT
         bufftag = bufftag // TRIM(buffline)
      ENDDO
      !
      IF( INDEX(bufftag,">") == 0 ) THEN
        lread = .false.
      ELSE
        is = INDEX(bufftag,'nx="') + 4
        buff2 = bufftag(is:)
        ie = INDEX(buff2,'"') + is - 2
        READ(bufftag(is:ie),*) nx
        is = INDEX(bufftag,'ny="') + 4
        buff2 = bufftag(is:)
        ie = INDEX(buff2,'"') + is - 2
        READ(bufftag(is:ie),*) ny
        is = INDEX(bufftag,'nz="') + 4
        buff2 = bufftag(is:)
        ie = INDEX(buff2,'"') + is - 2
        READ(bufftag(is:ie),*) nz
        is = INDEX(bufftag,'type="') + 6
        buff2 = bufftag(is:)
        ie = INDEX(buff2,'"') + is - 2
        ctype = bufftag(is:ie)
        lread = .true.
      ENDIF
      !      
      lstop = .FALSE.
      !
      IF( lread ) THEN
         ndim = nx*ny*nz
         SELECT CASE (ctype)
         CASE("double") 
            nbytes = SIZEOF(1._DP) * ndim
         CASE("complex")
            nbytes = SIZEOF(CMPLX(1._DP,0._DP,KIND=DP)) * ndim
         CASE DEFAULT
         END SELECT 
         nlen = lenbase64(nbytes)
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
220
         charbase64=""
Marco Govoni's avatar
Marco Govoni committed
221
222
223
224
225
226
227
228
         !
         DO
            READ(iu,'(a)',IOSTAT=ios) buffline
            IF( ios /=0 ) EXIT
            IF(INDEX(buffline,"</grid_function>") /= 0) THEN
               lstop = .TRUE.
               EXIT
            ENDIF
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
229
            charbase64 = charbase64 // TRIM( buffline )
Marco Govoni's avatar
Marco Govoni committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
         ENDDO
      ELSE
         CALL errore("","Could not start tag",1)
      ENDIF
      !
      IF( .NOT. lstop ) THEN
         CALL errore("","Could not close tag",1)
      ENDIF
      !
      CLOSE(iu)
      !
      ALLOCATE( funct3d_r_complex(1:ndim) )
      !
      SELECT CASE(ctype) 
      CASE("double")
         ALLOCATE( funct3d_r_double(1:ndim) )
         CALL base64_decode_double(charbase64(1:nlen), ndim, funct3d_r_double(1:ndim))
         DEALLOCATE( charbase64 )
         IF (.NOT. islittleendian()) CALL base64_byteswap_double(nbytes,funct3d_r_double(1:ndim)) 
         funct3d_r_complex(:) = CMPLX( funct3d_r_double(:), 0._DP, KIND=DP)
         DEALLOCATE( funct3d_r_double )
      CASE("complex")
         CALL base64_decode_complex(charbase64(1:nlen), ndim, funct3d_r_complex(1:ndim))
         DEALLOCATE( charbase64 )
         IF (.NOT. islittleendian()) CALL base64_byteswap_complex(nbytes,funct3d_r_complex(1:ndim)) 
      CASE DEFAULT
      END SELECT
      !
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
258
259
260
261
262
263
264
   ENDIF 
   !
   CALL mp_bcast(ndim,0,intra_bgrp_comm)  
   CALL mp_bcast(nx,0,intra_bgrp_comm)  
   CALL mp_bcast(ny,0,intra_bgrp_comm)  
   CALL mp_bcast(nz,0,intra_bgrp_comm)  
   IF( .NOT. ALLOCATED(funct3d_r_complex)) ALLOCATE( funct3d_r_complex(1:ndim) )
Marco Govoni's avatar
Marco Govoni committed
265
266
267
268
269
270
271
272
   !
   ! 1) F interpolate funct3_r --> funct3d_g
   !
   IF( gamma_only ) THEN 
      nmaps = 2
   ELSE 
      nmaps = 1 
   ENDIF
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
273
   ALLOCATE( nl(ngx,nmaps) )
274
   CALL get_G2R_mapping (nx, ny, nz, ng, ngx, nmaps, nl)
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
275
   CALL single_fwfft_fromArbitraryRGrid (funct3d_r_complex, nx, ny, nz, ng, ngx, nmaps, nl, funct3d_g)
Marco Govoni's avatar
Marco Govoni committed
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
   DEALLOCATE( nl )
   DEALLOCATE( funct3d_r_complex )
   !
 END SUBROUTINE
 !
 !
 SUBROUTINE write_long_string(iu,longstring) 
   !
   ! Write a long string on multiple lines (each line has a max of 72 charachter)
   ! The unit "iu" is NOT opened and closed here 
   !
   IMPLICIT NONE
   !
   ! I/O
   !
   INTEGER,INTENT(IN) :: iu
   CHARACTER(LEN=*) :: longstring
   !
   ! Workspace
   !
   INTEGER :: j, nlines, thislen
   INTEGER, PARAMETER :: maxlen = 72
   !
   thislen = LEN(longstring)
   nlines = thislen / maxlen
   IF( MOD( thislen, maxlen ) > 0 ) nlines = nlines + 1
   DO j = 1, nlines
Marco Govoni's avatar
Bug fix    
Marco Govoni committed
303
      WRITE(iu,'(a)') longstring((j-1)*maxlen+1:MIN(j*maxlen,thislen))
Marco Govoni's avatar
Marco Govoni committed
304
305
306
307
308
   ENDDO
   !
 END SUBROUTINE
 !
END MODULE