wfreq_restart.f90 23.7 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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
57
58
59
60
61
62
! 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 wfreq_restart
  !----------------------------------------------------------------------------
  !
  USE iotk_module
  USE kinds,                   ONLY : DP
  USE io_files,                ONLY : tmp_dir
  !
  IMPLICIT NONE
  !
  SAVE
  !
  TYPE :: bks_type
     INTEGER :: lastdone_ks
     INTEGER :: lastdone_band
     INTEGER :: old_ks
     INTEGER :: old_band
     INTEGER :: max_ks
     INTEGER :: max_band
     INTEGER :: min_ks
     INTEGER :: min_band
  END TYPE bks_type
  !
  INTERFACE solvewfreq_restart_write
     MODULE PROCEDURE solvewfreq_restart_write_real, solvewfreq_restart_write_complex
  END INTERFACE 
  !
  INTERFACE solvewfreq_restart_read
     MODULE PROCEDURE solvewfreq_restart_read_real, solvewfreq_restart_read_complex
  END INTERFACE 
  !
  CONTAINS
    !
    ! BKS
    !
    !------------------------------------------------------------------------
    SUBROUTINE write_bks(bks,dirname,fname)
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage,intra_bgrp_comm
      USE mp_world,             ONLY : mpime,root,world_comm,nproc
      USE io_global,            ONLY : stdout 
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get,mp_sum
      USE io_files,             ONLY : delete_if_present
      !
      IMPLICIT NONE
      !
      ! I/O
      !
      TYPE(bks_type),INTENT(IN) :: bks
63
      CHARACTER(LEN=512),INTENT(IN) :: dirname, fname
Marco Govoni's avatar
Marco Govoni committed
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
      INTEGER :: iunout
      !
      ! Workspace
      !
      INTEGER :: ierr
      !
      IF ( mpime == root ) THEN
         !
         ! ... open XML descriptor
         !
         CALL iotk_free_unit( iunout, ierr )
         CALL iotk_open_write( iunout, FILE = TRIM( dirname ) // '/' // TRIM(fname) , BINARY = .FALSE., IERR = ierr )
         !
      END IF
      !
      CALL mp_bcast( ierr, root, world_comm )
      !
      CALL errore( 'wfreq_restart', 'cannot open restart file for writing', ierr )
      !
      IF ( mpime == root ) THEN  
         !
         CALL iotk_write_begin( iunout, "BKS-SUMMARY" )
         CALL iotk_write_dat( iunout, "lastdone_ks"  , bks%lastdone_ks   )
         CALL iotk_write_dat( iunout, "lastdone_band", bks%lastdone_band )
         CALL iotk_write_dat( iunout, "old_ks"       , bks%old_ks       )
         CALL iotk_write_dat( iunout, "old_band"     , bks%old_band     )
         CALL iotk_write_dat( iunout, "max_ks"       , bks%max_ks       )
         CALL iotk_write_dat( iunout, "max_band"     , bks%max_band     )
         CALL iotk_write_dat( iunout, "min_ks"       , bks%min_ks       )
         CALL iotk_write_dat( iunout, "min_band"     , bks%min_band     )
         CALL iotk_write_end( iunout, "BKS-SUMMARY"  )
         !
         CALL iotk_close_write( iunout )
         !
      END IF
      !
      CALL mp_barrier(world_comm)
      !
    END SUBROUTINE
    !
    !------------------------------------------------------------------------
    SUBROUTINE read_bks(bks,dirname,fname)
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage,intra_bgrp_comm
      USE mp_world,             ONLY : mpime,root,world_comm,nproc
      USE io_global,            ONLY : stdout 
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get,mp_sum
      USE io_files,             ONLY : delete_if_present
      !
      IMPLICIT NONE
      !
      ! I/O
      !
      TYPE(bks_type),INTENT(OUT) :: bks
119
      CHARACTER(LEN=512),INTENT(IN) :: dirname, fname
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
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
170
171
172
173
174
175
176
177
178
179
180
      INTEGER :: iunout
      !
      ! Workspace
      !
      INTEGER :: ierr
      !
      IF ( mpime == root ) THEN
         !
         ! ... open XML descriptor
         !
         CALL iotk_free_unit( iunout, ierr )
         CALL iotk_open_read( iunout, FILE = TRIM( dirname ) // '/' // TRIM(fname) , BINARY = .FALSE., IERR = ierr )
         !
      END IF
      !
      CALL mp_bcast( ierr, root, world_comm )
      !
      CALL errore( 'wfreq_restart', 'cannot open restart file for reading', ierr )
      !
      IF ( mpime == root ) THEN  
         !
         CALL iotk_scan_begin( iunout, "BKS-SUMMARY" )
         CALL iotk_scan_dat( iunout, "lastdone_ks"  , bks%lastdone_ks   )
         CALL iotk_scan_dat( iunout, "lastdone_band", bks%lastdone_band )
         CALL iotk_scan_dat( iunout, "old_ks"       , bks%old_ks        )
         CALL iotk_scan_dat( iunout, "old_band"     , bks%old_band      )
         CALL iotk_scan_dat( iunout, "max_ks"       , bks%max_ks        )
         CALL iotk_scan_dat( iunout, "max_band"     , bks%max_band      )
         CALL iotk_scan_dat( iunout, "min_ks"       , bks%min_ks        )
         CALL iotk_scan_dat( iunout, "min_band"     , bks%min_band      )
         CALL iotk_scan_end( iunout, "BKS-SUMMARY"  )
         !
         CALL iotk_close_read( iunout )
         !
      END IF
      !
      CALL mp_bcast( bks%lastdone_ks   , root , world_comm )
      CALL mp_bcast( bks%lastdone_band , root , world_comm )
      CALL mp_bcast( bks%old_ks        , root , world_comm )
      CALL mp_bcast( bks%old_band      , root , world_comm )
      CALL mp_bcast( bks%max_ks        , root , world_comm )
      CALL mp_bcast( bks%max_band      , root , world_comm )
      CALL mp_bcast( bks%min_ks        , root , world_comm )
      CALL mp_bcast( bks%min_band      , root , world_comm )
      !
    END SUBROUTINE
    !
    !------------------------------------------------------------------------
    SUBROUTINE clear_bks(dirname,fname)
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage,intra_bgrp_comm
      USE mp_world,             ONLY : mpime,root,world_comm,nproc
      USE io_global,            ONLY : stdout 
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get,mp_sum
      USE io_files,             ONLY : delete_if_present
      !
      IMPLICIT NONE
      !
      ! I/O
      !
181
      CHARACTER(LEN=512),INTENT(IN) :: dirname, fname
Marco Govoni's avatar
Marco Govoni committed
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
220
221
222
223
224
      !
      ! Workspace
      !
      INTEGER :: ierr
      !
      IF ( mpime == root ) THEN
         !
         CALL delete_if_present( TRIM( dirname ) // '/' // TRIM(fname) ) 
         !
      END IF
      !
      CALL mp_barrier( world_comm )
      !
    END SUBROUTINE
    !
    !
    ! SOLVEWFREQ
    !
    !------------------------------------------------------------------------
    SUBROUTINE solvewfreq_restart_write_real(bks,dmat,zmat,npg,npl)
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage,intra_bgrp_comm
      USE mp_world,             ONLY : mpime,root,world_comm,nproc
      USE io_global,            ONLY : stdout 
      USE westcom,              ONLY : west_prefix
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get,mp_sum
      USE io_files,             ONLY : delete_if_present
      USE distribution_center,  ONLY : ifr,rfr
      USE west_io,              ONLY : serial_data_write 
      !
      IMPLICIT NONE
      !
      ! I/O
      !
      TYPE(bks_type),INTENT(IN) :: bks
      INTEGER, INTENT(IN) :: npg, npl
      REAL(DP), INTENT(IN) :: dmat(npg,npl,ifr%nloc)
      COMPLEX(DP), INTENT(IN) :: zmat(npg,npl,rfr%nloc)
      !
      ! Workspace
      !
      INTEGER :: ierr
225
      CHARACTER(LEN=512) :: dirname,fname
Marco Govoni's avatar
Marco Govoni committed
226
227
228
229
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
322
323
324
325
326
327
328
329
      CHARACTER(29) :: my_label
      CHARACTER(33) :: my_label2
      INTEGER :: local_j,global_j
      INTEGER :: ip, ip_glob
      REAL(DP),ALLOCATABLE :: tmp_dmat(:,:,:) 
      COMPLEX(DP),ALLOCATABLE :: tmp_zmat(:,:,:) 
      LOGICAL :: lproc
      INTEGER :: iunit
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      CALL start_clock('sw_restart')
      !
      ! MKDIR 
      !
      dirname = TRIM( tmp_dir ) // TRIM( west_prefix ) // '.wfreq.restart'
      CALL my_mkdir( dirname )
      !  
      ! DMAT
      !
      ALLOCATE( tmp_dmat( npg, npl, ifr%nglob ))
      tmp_dmat = 0._DP
      DO ip = 1, ifr%nloc
         ip_glob = ifr%l2g(ip)
         tmp_dmat(:,:,ip_glob) = dmat(:,:,ip)
      ENDDO
      CALL mp_sum( tmp_dmat, intra_bgrp_comm ) 
      !
      WRITE(my_label,'("dmat_iks",i5.5,"_iv",i5.5,"_I",i6.6)') bks%lastdone_ks, bks%lastdone_band, my_image_id
      fname=TRIM( dirname ) // '/' // TRIM(my_label)
      iunit = 2000 + my_image_id
      lproc = (me_bgrp == 0)
      CALL serial_data_write(lproc,iunit,fname,tmp_dmat,npg,npl,ifr%nglob)
      !
      DEALLOCATE(tmp_dmat)
      !
      ! ZMAT
      !
      ALLOCATE( tmp_zmat( npg, npl, rfr%nglob ))
      tmp_zmat = 0._DP
      DO ip = 1, rfr%nloc
         ip_glob = rfr%l2g(ip)
         tmp_zmat(:,:,ip_glob) = zmat(:,:,ip)
      ENDDO
      CALL mp_sum( tmp_zmat, intra_bgrp_comm ) 
      !   
      WRITE(my_label,'("zmat_iks",i5.5,"_iv",i5.5,"_I",i6.6)') bks%lastdone_ks, bks%lastdone_band, my_image_id
      fname=TRIM( dirname ) // '/' // TRIM(my_label)
      iunit = 2000 + my_image_id
      lproc = (me_bgrp == 0)
      CALL serial_data_write(lproc,iunit,fname,tmp_zmat,npg,npl,rfr%nglob)
      !
      DEALLOCATE(tmp_zmat)
      !
      ! CLEAR
      !
      IF(bks%old_ks/=0.AND.bks%old_band/=0) THEN
         IF( me_bgrp == 0 ) THEN
            WRITE(my_label2,'("dmat_iks",i5.5,"_iv",i5.5,"_I",i6.6,".raw")') &
            & bks%old_ks, bks%old_band, my_image_id
            fname=TRIM(my_label2)
            CALL delete_if_present( TRIM( dirname ) // '/' // TRIM( fname ) )
            WRITE(my_label2,'("zmat_iks",i5.5,"_iv",i5.5,"_I",i6.6,".raw")') & 
            & bks%old_ks, bks%old_band, my_image_id
            fname=TRIM(my_label2)
            CALL delete_if_present( TRIM( dirname ) // '/' // TRIM( fname ) )
         ENDIF
      ENDIF
      !
      ! CREATE THE SUMMARY FILE
      !
      fname = "summary_w.xml"
      CALL write_bks( bks, dirname, fname ) 
      !
      CALL stop_clock('sw_restart')
      !
    END SUBROUTINE
    !
    !------------------------------------------------------------------------
    SUBROUTINE solvewfreq_restart_write_complex(bks,dmat,zmat,npg,npl)
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage,intra_bgrp_comm
      USE mp_world,             ONLY : mpime,root,world_comm,nproc
      USE io_global,            ONLY : stdout 
      USE westcom,              ONLY : west_prefix
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get,mp_sum
      USE io_files,             ONLY : delete_if_present
      USE distribution_center,  ONLY : ifr,rfr
      USE west_io,              ONLY : serial_data_write 
      !
      IMPLICIT NONE
      !
      ! I/O
      !
      TYPE(bks_type),INTENT(IN) :: bks
      INTEGER, INTENT(IN) :: npg, npl
      COMPLEX(DP), INTENT(IN) :: dmat(npg,npl,ifr%nloc)
      COMPLEX(DP), INTENT(IN) :: zmat(npg,npl,rfr%nloc)
      !
      ! Workspace
      !
      INTEGER :: ierr
330
      CHARACTER(LEN=512) :: dirname,fname
Marco Govoni's avatar
Marco Govoni committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
      CHARACTER(29) :: my_label
      CHARACTER(33) :: my_label2
      INTEGER :: local_j,global_j
      INTEGER :: ip, ip_glob
      COMPLEX(DP),ALLOCATABLE :: tmp_dmat(:,:,:) 
      COMPLEX(DP),ALLOCATABLE :: tmp_zmat(:,:,:) 
      LOGICAL :: lproc
      INTEGER :: iunit
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      CALL start_clock('sw_restart')
      !
      ! MKDIR 
      !
      dirname = TRIM( tmp_dir ) // TRIM( west_prefix ) // '.wfreq.restart'
      CALL my_mkdir( dirname )
      !  
      ! DMAT
      !
      ALLOCATE( tmp_dmat( npg, npl, ifr%nglob ))
      tmp_dmat = 0._DP
      DO ip = 1, ifr%nloc
         ip_glob = ifr%l2g(ip)
         tmp_dmat(:,:,ip_glob) = dmat(:,:,ip)
      ENDDO
      CALL mp_sum( tmp_dmat, intra_bgrp_comm ) 
      !
      WRITE(my_label,'("dmat_iks",i5.5,"_iv",i5.5,"_I",i6.6)') bks%lastdone_ks, bks%lastdone_band, my_image_id
      fname=TRIM( dirname ) // '/' // TRIM(my_label)
      iunit = 2000 + my_image_id
      lproc = (me_bgrp == 0)
      CALL serial_data_write(lproc,iunit,fname,tmp_dmat,npg,npl,ifr%nglob)
      !
      DEALLOCATE(tmp_dmat)
      !
      ! ZMAT
      !
      ALLOCATE( tmp_zmat( npg, npl, rfr%nglob ))
      tmp_zmat = 0._DP
      DO ip = 1, rfr%nloc
         ip_glob = rfr%l2g(ip)
         tmp_zmat(:,:,ip_glob) = zmat(:,:,ip)
      ENDDO
      CALL mp_sum( tmp_zmat, intra_bgrp_comm ) 
      !   
      WRITE(my_label,'("zmat_iks",i5.5,"_iv",i5.5,"_I",i6.6)') bks%lastdone_ks, bks%lastdone_band, my_image_id
      fname=TRIM( dirname ) // '/' // TRIM(my_label)
      iunit = 2000 + my_image_id
      lproc = (me_bgrp == 0)
      CALL serial_data_write(lproc,iunit,fname,tmp_zmat,npg,npl,rfr%nglob)
      !
      DEALLOCATE(tmp_zmat)
      !
      ! CLEAR
      !
      IF(bks%old_ks/=0.AND.bks%old_band/=0) THEN
         IF( me_bgrp == 0 ) THEN
            WRITE(my_label2,'("dmat_iks",i5.5,"_iv",i5.5,"_I",i6.6,".raw")') &
            & bks%old_ks, bks%old_band, my_image_id
            fname=TRIM(my_label2)
            CALL delete_if_present( TRIM( dirname ) // '/' // TRIM( fname ) )
            WRITE(my_label2,'("zmat_iks",i5.5,"_iv",i5.5,"_I",i6.6,".raw")') & 
            & bks%old_ks, bks%old_band, my_image_id
            fname=TRIM(my_label2)
            CALL delete_if_present( TRIM( dirname ) // '/' // TRIM( fname ) )
         ENDIF
      ENDIF
      !
      ! CREATE THE SUMMARY FILE
      !
      fname = "summary_w.xml"
      CALL write_bks( bks, dirname, fname ) 
      !
      CALL stop_clock('sw_restart')
      !
    END SUBROUTINE
    !
    !
    !
    !
    !------------------------------------------------------------------------
    SUBROUTINE solvewfreq_restart_read_real( bks, dmat, zmat, npg, npl )
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage,intra_bgrp_comm
      USE mp_world,             ONLY : mpime,root,world_comm
      USE io_global,            ONLY : stdout 
      USE westcom,              ONLY : west_prefix
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get
      USE distribution_center,  ONLY : ifr,rfr
      USE west_io,              ONLY : serial_data_read 
      !
      IMPLICIT NONE
      !
      ! I/O
      !
      TYPE(bks_type), INTENT(OUT) :: bks
      INTEGER, INTENT(IN) :: npg, npl
      REAL(DP), INTENT(OUT) :: dmat(npg,npl,ifr%nloc)
      COMPLEX(DP), INTENT(OUT) :: zmat(npg,npl,rfr%nloc)
      !
      ! Workspace
      !
      INTEGER :: ierr
437
      CHARACTER(LEN=512) :: dirname,fname
Marco Govoni's avatar
Marco Govoni committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
      REAL(DP), EXTERNAL :: GET_CLOCK
      REAL(DP) :: time_spent(2)
      CHARACTER(20),EXTERNAL :: human_readable_time
      CHARACTER(29) :: my_label
      INTEGER :: local_j,global_j
      INTEGER :: ip,ip_glob
      REAL(DP),ALLOCATABLE :: tmp_dmat(:,:,:)
      COMPLEX(DP),ALLOCATABLE :: tmp_zmat(:,:,:)
      LOGICAL :: lproc
      INTEGER :: iunit
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      !
      ! MKDIR 
      !
      dirname = TRIM( tmp_dir ) // TRIM( west_prefix ) // '.wfreq.restart'
      CALL my_mkdir( dirname )
      !
      CALL start_clock('sw_restart')
      time_spent(1)=get_clock('sw_restart')
      !
      ! CREATE THE SUMMARY FILE
      !
      fname = "summary_w.xml"
      CALL read_bks( bks, dirname, fname )
      !
      ! READ
      !
      ALLOCATE( tmp_dmat( npg, npl, ifr%nglob ))
      !
      WRITE(my_label,'("dmat_iks",i5.5,"_iv",i5.5,"_I",i6.6)') bks%lastdone_ks, bks%lastdone_band, my_image_id
      fname=TRIM( dirname ) // '/' // TRIM(my_label) 
      lproc = (me_bgrp==0) 
      iunit = 2000 + my_image_id
      CALL serial_data_read(lproc,iunit,fname,tmp_dmat,npg,npl,ifr%nglob)
      !
      CALL mp_bcast( tmp_dmat, 0, intra_bgrp_comm)
      DO ip = 1, ifr%nloc
         ip_glob = ifr%l2g(ip)
         dmat(:,:,ip) = tmp_dmat(:,:,ip_glob)
      ENDDO
      DEALLOCATE(tmp_dmat)
      !
      ALLOCATE( tmp_zmat( npg, npl, rfr%nglob ))
      !
      WRITE(my_label,'("zmat_iks",i5.5,"_iv",i5.5,"_I",i6.6)') bks%lastdone_ks, bks%lastdone_band, my_image_id
      fname=TRIM( dirname ) // '/' // TRIM(my_label) 
      lproc = (me_bgrp==0) 
      iunit = 2000 + my_image_id
      CALL serial_data_read(lproc,iunit,fname,tmp_zmat,npg,npl,rfr%nglob)
      !
      CALL mp_bcast( tmp_zmat, 0, intra_bgrp_comm)
      DO ip = 1, rfr%nloc
         ip_glob = rfr%l2g(ip)
         zmat(:,:,ip) = tmp_zmat(:,:,ip_glob)
      ENDDO
      DEALLOCATE(tmp_zmat)
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      time_spent(2)=get_clock('sw_restart')
      CALL stop_clock('sw_restart')
      !
      WRITE(stdout,'(1/, 5x,"[I/O] -------------------------------------------------------")')
      WRITE(stdout, "(5x, '[I/O] RESTART read in ',a20)") human_readable_time(time_spent(2)-time_spent(1)) 
      WRITE(stdout, "(5x, '[I/O] In location : ',a)") TRIM( dirname )  
      WRITE(stdout,'(5x,"[I/O] -------------------------------------------------------")')
      !
    END SUBROUTINE
    !
    !------------------------------------------------------------------------
    SUBROUTINE solvewfreq_restart_read_complex( bks, dmat, zmat, npg, npl )
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage,intra_bgrp_comm
      USE mp_world,             ONLY : mpime,root,world_comm
      USE io_global,            ONLY : stdout 
      USE westcom,              ONLY : west_prefix
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get
      USE distribution_center,  ONLY : ifr,rfr
      USE west_io,              ONLY : serial_data_read 
      !
      IMPLICIT NONE
      !
      ! I/O
      !
      TYPE(bks_type), INTENT(OUT) :: bks
      INTEGER, INTENT(IN) :: npg, npl
      COMPLEX(DP), INTENT(OUT) :: dmat(npg,npl,ifr%nloc)
      COMPLEX(DP), INTENT(OUT) :: zmat(npg,npl,rfr%nloc)
      !
      ! Workspace
      !
      INTEGER :: ierr
535
      CHARACTER(LEN=512) :: dirname,fname
Marco Govoni's avatar
Marco Govoni committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
      REAL(DP), EXTERNAL :: GET_CLOCK
      REAL(DP) :: time_spent(2)
      CHARACTER(20),EXTERNAL :: human_readable_time
      CHARACTER(29) :: my_label
      INTEGER :: local_j,global_j
      INTEGER :: ip,ip_glob
      COMPLEX(DP),ALLOCATABLE :: tmp_dmat(:,:,:)
      COMPLEX(DP),ALLOCATABLE :: tmp_zmat(:,:,:)
      LOGICAL :: lproc
      INTEGER :: iunit
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      !
      ! MKDIR 
      !
      dirname = TRIM( tmp_dir ) // TRIM( west_prefix ) // '.wfreq.restart'
      CALL my_mkdir( dirname )
      !
      CALL start_clock('sw_restart')
      time_spent(1)=get_clock('sw_restart')
      !
      ! CREATE THE SUMMARY FILE
      !
      fname = "summary_w.xml"
      CALL read_bks( bks, dirname, fname )
      !
      ! READ
      !
      ALLOCATE( tmp_dmat( npg, npl, ifr%nglob ))
      !
      WRITE(my_label,'("dmat_iks",i5.5,"_iv",i5.5,"_I",i6.6)') bks%lastdone_ks, bks%lastdone_band, my_image_id
      fname=TRIM( dirname ) // '/' // TRIM(my_label) 
      lproc = (me_bgrp==0) 
      iunit = 2000 + my_image_id
      CALL serial_data_read(lproc,iunit,fname,tmp_dmat,npg,npl,ifr%nglob)
      !
      CALL mp_bcast( tmp_dmat, 0, intra_bgrp_comm)
      DO ip = 1, ifr%nloc
         ip_glob = ifr%l2g(ip)
         dmat(:,:,ip) = tmp_dmat(:,:,ip_glob)
      ENDDO
      DEALLOCATE(tmp_dmat)
      !
      ALLOCATE( tmp_zmat( npg, npl, rfr%nglob ))
      !
      WRITE(my_label,'("zmat_iks",i5.5,"_iv",i5.5,"_I",i6.6)') bks%lastdone_ks, bks%lastdone_band, my_image_id
      fname=TRIM( dirname ) // '/' // TRIM(my_label) 
      lproc = (me_bgrp==0) 
      iunit = 2000 + my_image_id
      CALL serial_data_read(lproc,iunit,fname,tmp_zmat,npg,npl,rfr%nglob)
      !
      CALL mp_bcast( tmp_zmat, 0, intra_bgrp_comm)
      DO ip = 1, rfr%nloc
         ip_glob = rfr%l2g(ip)
         zmat(:,:,ip) = tmp_zmat(:,:,ip_glob)
      ENDDO
      DEALLOCATE(tmp_zmat)
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      time_spent(2)=get_clock('sw_restart')
      CALL stop_clock('sw_restart')
      !
      WRITE(stdout,'(1/, 5x,"[I/O] -------------------------------------------------------")')
      WRITE(stdout, "(5x, '[I/O] RESTART read in ',a20)") human_readable_time(time_spent(2)-time_spent(1)) 
      WRITE(stdout, "(5x, '[I/O] In location : ',a)") TRIM( dirname )  
      WRITE(stdout,'(5x,"[I/O] -------------------------------------------------------")')
      !
    END SUBROUTINE
    !
    !
    ! GFREQ
    !
    !------------------------------------------------------------------------
    SUBROUTINE solvegfreq_restart_write( bks )
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage
      USE mp_world,             ONLY : mpime,root,world_comm,nproc
      USE io_global,            ONLY : stdout 
      USE westcom,              ONLY : west_prefix
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get
      USE io_files,             ONLY : delete_if_present
      !
      IMPLICIT NONE
      !
      ! I/O
      !
      TYPE(bks_type), INTENT(IN) :: bks
      !
      ! Workspace
      !
      INTEGER :: ierr
632
      CHARACTER(LEN=512) :: dirname,fname
Marco Govoni's avatar
Marco Govoni committed
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
      REAL(DP), EXTERNAL :: GET_CLOCK
      REAL(DP) :: time_spent(2)
      CHARACTER(20),EXTERNAL :: human_readable_time
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      !
      ! MKDIR 
      !
      dirname = TRIM( tmp_dir ) // TRIM( west_prefix ) // '.wfreq.restart'
      CALL my_mkdir( dirname )
      !
      CALL start_clock('sg_restart')
      !
      fname = "summary_g.xml"
      CALL write_bks( bks, dirname, fname )
      !
      CALL stop_clock('sg_restart')
      !
    END SUBROUTINE
    !
    !
    !
    !------------------------------------------------------------------------
    SUBROUTINE solvegfreq_restart_read( bks )
      !------------------------------------------------------------------------
      !
      USE mp_global,            ONLY : my_image_id,me_bgrp,inter_image_comm,nimage
      USE mp_world,             ONLY : mpime,root,world_comm
      USE io_global,            ONLY : stdout 
      USE westcom,              ONLY : west_prefix
      USE mp,                   ONLY : mp_barrier,mp_bcast,mp_get
      !
      IMPLICIT NONE
      !
      ! I/O
      !
      TYPE(bks_type),INTENT(OUT) :: bks 
      !
      ! Workspace
      !
      INTEGER :: ierr
676
      CHARACTER(LEN=512) :: dirname,fname
Marco Govoni's avatar
Marco Govoni committed
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
      REAL(DP), EXTERNAL :: GET_CLOCK
      REAL(DP) :: time_spent(2)
      CHARACTER(20),EXTERNAL :: human_readable_time
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      !
      ! MKDIR 
      !
      dirname = TRIM( tmp_dir ) // TRIM( west_prefix ) // '.wfreq.restart'
      CALL my_mkdir( dirname )
      !
      CALL start_clock('sg_restart')
      time_spent(1)=get_clock('sg_restart')
      !
      fname = "summary_g.xml"
      CALL read_bks( bks, dirname, fname ) 
      !
      ! BARRIER
      !
      CALL mp_barrier(world_comm)
      time_spent(2)=get_clock('sg_restart')
      CALL stop_clock('sg_restart')
      !
      WRITE(stdout,'(1/, 5x,"[I/O] -------------------------------------------------------")')
      WRITE(stdout, "(5x, '[I/O] RESTART read in ',a20)") human_readable_time(time_spent(2)-time_spent(1)) 
      WRITE(stdout, "(5x, '[I/O] In location : ',a)") TRIM( dirname )  
      WRITE(stdout,'(5x,"[I/O] -------------------------------------------------------")')
      !
    END SUBROUTINE
    !
END MODULE