davidson_diago.f90 48.3 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
! 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
!
#define ZERO ( 0.D0, 0.D0 )
#define ONE  ( 1.D0, 0.D0 )
!
!----------------------------------------------------------------------------
SUBROUTINE davidson_diago ( )
  !----------------------------------------------------------------------------
  !
  USE control_flags,   ONLY : gamma_only 
  !
  IMPLICIT NONE
  !
  IF( gamma_only ) THEN 
    CALL davidson_diago_gamma( ) 
  ELSE
    CALL davidson_diago_k( )
  ENDIF 
  !
END SUBROUTINE 
!
!----------------------------------------------------------------------------
SUBROUTINE davidson_diago_gamma ( )
  !----------------------------------------------------------------------------
  !
  ! ... iterative solution of the eigenvalue problem:
  ! ... ( chi - ev ) * dvg = 0
  !
  USE kinds,                ONLY : DP
  USE io_global,            ONLY : stdout
  USE distribution_center,  ONLY : pert
  USE class_idistribute,    ONLY : idistribute
  USE io_push,              ONLY : io_push_title,io_push_bar
44
  USE westcom,              ONLY : dvg,dng,n_pdep_eigen,trev_pdep,n_pdep_maxiter,n_pdep_basis,wstat_calculation,ev,conv,&
45
                                   & n_pdep_restart_from_itr,n_pdep_read_from_file,n_steps_write_restart,n_pdep_times,npwqx,npwq,&
46
                                   & npwqx,npwq,trev_pdep_rel,tr2_dfpt,l_is_wstat_converged,fftdriver
Marco Govoni's avatar
Marco Govoni committed
47
48
49
50
51
52
53
54
  USE pdep_db,              ONLY : pdep_db_write,pdep_db_read
  USE wstat_restart,        ONLY : wstat_restart_write, wstat_restart_clear, wstat_restart_read
  USE mp_world,             ONLY : mpime
  USE mp_global,            ONLY : inter_image_comm
  USE mp,                   ONLY : mp_sum
  USE gvect,                ONLY : gstart
  USE wstat_tools,          ONLY : diagox,serial_diagox,build_hr,symm_hr_distr,redistribute_vr_distr,&
                                   & update_with_vr_distr,refresh_with_vr_distr 
55
  USE types_coulomb,        ONLY : pot3D
Marco Govoni's avatar
Marco Govoni committed
56
  USE dfpt_module,          ONLY : dfpt
Marco Govoni's avatar
Marco Govoni committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  !
  IMPLICIT NONE
  !
  ! ... LOCAL variables
  !
  INTEGER :: nvec, nvecx
    ! dimension of the matrix to be diagonalized
    ! leading dimension of matrix evc, as declared in the calling pgm unit
  INTEGER :: dav_iter, notcnv
    ! integer  number of iterations performed
    ! number of unconverged roots
  INTEGER :: kter, nbase, np, n, m
    ! counter on iterations
    ! dimension of the reduced basis
    ! counter on the reduced basis vectors
    ! do-loop counters
    ! counter on the bands
  INTEGER :: ierr,mloc,mstart,mend
  INTEGER,ALLOCATABLE :: ishift(:)
  REAL(DP), ALLOCATABLE :: ew(:)
  REAL(DP), ALLOCATABLE :: hr_distr(:,:), vr_distr(:,:)
  !
  INTEGER :: il1,il2,ig1,ig2,i
  REAL(DP) :: time_spent(2)
Marco Govoni's avatar
Marco Govoni committed
81
  INTEGER :: sternop_ncalls(2)
Marco Govoni's avatar
Marco Govoni committed
82
  CHARACTER(LEN=8) :: iter_label
Marco Govoni's avatar
Marco Govoni committed
83
84
  REAL(DP) :: pccg_res_tr2
  INTEGER :: dfpt_dim, diago_dim
Marco Govoni's avatar
Marco Govoni committed
85
86
87
88
89
90
91
92
93
94
95
  !
  REAL(DP), EXTERNAL :: GET_CLOCK
  !
  ! ... INITIALIZATION
  !
  l_is_wstat_converged = .FALSE.
  nvec = n_pdep_eigen
  nvecx = n_pdep_basis
  !
  CALL start_clock( 'chidiago' )
  time_spent(1)=get_clock( 'chidiago' )
Marco Govoni's avatar
Marco Govoni committed
96
  CALL get_clock_called( 'stern' , sternop_ncalls(1) )
Marco Govoni's avatar
Marco Govoni committed
97
98
99
100
101
102
103
104
105
106
107
108
  !
  ! ... DISTRIBUTE nvecx
  !
  pert = idistribute()
  CALL pert%init(nvecx,'i','nvecx',.TRUE.)
  !CALL index_distr_init(pert,nvecx,'i','nvecx',.TRUE.) 
  CALL wstat_memory_report() ! Before allocating I report the memory required. 
  !
  ! ... MEMORY ALLOCATION
  !
  IF ( nvec > nvecx / 2 ) CALL errore( 'chidiago', 'nvecx is too small', 1 )
  !
109
  ALLOCATE( dvg( npwqx, pert%nlocx ), STAT=ierr )
Marco Govoni's avatar
Marco Govoni committed
110
111
112
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate dvg ', ABS(ierr) )
  !
113
  ALLOCATE( dng( npwqx, pert%nlocx ), STAT=ierr )
Marco Govoni's avatar
Marco Govoni committed
114
115
116
117
118
119
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
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate dng ', ABS(ierr) )
  !
  ALLOCATE( hr_distr( nvecx, pert%nlocx ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate hr_distr ', ABS(ierr) )
  !
  ALLOCATE( vr_distr( nvecx, pert%nlocx ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate vr_distr ', ABS(ierr) )
  !
  ALLOCATE( ew( nvecx ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate ew ', ABS(ierr) )
  !
  ALLOCATE( ev( nvec  ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate ev ', ABS(ierr) )
  !
  ALLOCATE( conv( nvec ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate conv ', ABS(ierr) )
  !
  nbase  = nvec
  conv   = .FALSE.
  ev     = 0._DP
  ew     = 0._DP
  dng  = 0._DP
  dvg  = 0._DP
  hr_distr(:,:) = 0._DP
  vr_distr(:,:) = 0._DP
  notcnv  = nvec 
  dav_iter = -2
  !
148
  CALL pot3D%init(fftdriver,.FALSE.,'default')
149
  CALL pot3d%print_divergence()
150
  !
Marco Govoni's avatar
Marco Govoni committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  ! KIND OF CALCULATION
  !
  SELECT CASE(wstat_calculation)
  CASE('r','R')
     !
     ! RESTART
     !
     CALL wstat_restart_read( dav_iter, notcnv, nbase, ew, hr_distr, vr_distr )
     !
  CASE('s','S')
     !
     ! FROM SCRATCH
     !
     ! ... Eventually read from file
     !
     IF(n_pdep_read_from_file>0) CALL pdep_db_read( n_pdep_read_from_file )
     !
     ! ... Eventually randomize
     !
170
     IF(n_pdep_read_from_file<nvec) CALL do_randomize ( dvg, n_pdep_read_from_file+1, nvec )
Marco Govoni's avatar
Marco Govoni committed
171
172
173
174
175
     !
     ! ... MGS
     !
     CALL do_mgs( dvg, 1, nvec)
     !
Marco Govoni's avatar
Marco Govoni committed
176
177
     dfpt_dim = nbase
     diago_dim = nbase
Marco Govoni's avatar
Marco Govoni committed
178
179
180
     WRITE(stdout, "( /,5x,'                  *----------*              *----------*               *----------*') ")
     WRITE(stdout, &
         & "(   5x,'#     Iteration = | ', a8,' |','   ','DFPT_dim = | ', i8,' |', '   ','Diago_dim = | ', i8,' |  x 1/2')")&
Marco Govoni's avatar
Marco Govoni committed
181
         & 'starting', dfpt_dim, diago_dim
Marco Govoni's avatar
Marco Govoni committed
182
183
184
185
186
187
188
189
190
191
192
193
194
     WRITE(stdout, "(   5x,'                  *----------*              *----------*               *----------*') ")
     !
     ! Apply operator with DFPT
     !
     mloc = 0
     mstart = 1 
     DO il1 = 1, pert%nloc
        ig1 = pert%l2g(il1)
        IF( ig1 < 1 .OR. ig1 > nvec ) CYCLE
        IF( mloc==0 ) mstart = il1
        mloc = mloc + 1
     ENDDO
     !
Marco Govoni's avatar
Marco Govoni committed
195
196
     pccg_res_tr2 = -1._DP
     CALL dfpt ( mloc, dvg(1,mstart), dng(1,mstart), pccg_res_tr2 ) 
Marco Govoni's avatar
Marco Govoni committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
     dav_iter = -1
     CALL wstat_restart_write( dav_iter, notcnv, nbase, ew, hr_distr, vr_distr)
     !
  CASE DEFAULT
     CALL errore('chidiago', 'Wrong wstat_calculation',1)
  END SELECT
  !
  IF( dav_iter == -2 ) CALL errore( 'chidiago','Cannot find the 1st starting loop',1) 
  !
  IF( dav_iter == -1 ) THEN
     !
     ! < EXTRA STEP > 
     !
     dvg = dng
     CALL do_mgs( dvg, 1, nvec)
     !
Marco Govoni's avatar
Marco Govoni committed
213
214
     dfpt_dim = nbase
     diago_dim = nbase
Marco Govoni's avatar
Marco Govoni committed
215
216
217
     WRITE(stdout, "( /,5x,'                  *----------*              *----------*               *----------*') ")
     WRITE(stdout, &
         & "(   5x,'#     Iteration = | ', a8,' |','   ','DFPT_dim = | ', i8,' |', '   ','Diago_dim = | ', i8,' |  x 2/2')")&
Marco Govoni's avatar
Marco Govoni committed
218
         & 'starting', dfpt_dim, diago_dim
Marco Govoni's avatar
Marco Govoni committed
219
220
221
222
223
224
225
226
227
228
229
230
231
     WRITE(stdout, "(   5x,'                  *----------*              *----------*               *----------*') ")
     !
     ! Apply operator with DFPT
     !
     mloc = 0
     mstart = 1 
     DO il1 = 1, pert%nloc
        ig1 = pert%l2g(il1)
        IF( ig1 < 1 .OR. ig1 > nvec ) CYCLE
        IF( mloc==0 ) mstart = il1
        mloc = mloc + 1
     ENDDO
     !
Marco Govoni's avatar
Marco Govoni committed
232
233
     pccg_res_tr2 = MIN(0.01_DP,1000000._DP*tr2_dfpt)
     CALL dfpt ( mloc, dvg(1,mstart), dng(1,mstart), pccg_res_tr2 ) 
Marco Govoni's avatar
Marco Govoni committed
234
235
236
237
238
239
240
241
242
243
244
     ! 
     ! </ EXTRA STEP >
     !
     ! hr = <dvg|dng>
     !
     CALL build_hr( dvg, dng, mstart, mstart+mloc-1, hr_distr, 1, nvec ) 
     !
     ! ... diagonalize the reduced hamiltonian
     !
     CALL diagox( nbase, nvec, hr_distr, nvecx, ew, vr_distr )
     time_spent(2)=get_clock( 'chidiago' )
Marco Govoni's avatar
Marco Govoni committed
245
     CALL get_clock_called( 'stern' , sternop_ncalls(2) )
Marco Govoni's avatar
Marco Govoni committed
246
247
248
249
     ev(1:nvec) = ew(1:nvec)
     !
     ! Write the eigenvalues & time spent
     !
Marco Govoni's avatar
Marco Govoni committed
250
     CALL output_ev_and_time(nvec,ev,conv,time_spent,sternop_ncalls,pccg_res_tr2,dfpt_dim,diago_dim,dav_iter,notcnv)
Marco Govoni's avatar
Marco Govoni committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
     dav_iter = 0
     CALL wstat_restart_write( dav_iter, notcnv, nbase, ew, hr_distr, vr_distr)
     !
  ENDIF
  !
  ! --------------------
  !
  ! ... iterate
  !
  ! --------------------
  !
  iterate: DO kter = 1, n_pdep_maxiter
     !
     time_spent(1) = get_clock( 'chidiago' )
Marco Govoni's avatar
Marco Govoni committed
265
     CALL get_clock_called( 'stern' , sternop_ncalls(1) )
Marco Govoni's avatar
Marco Govoni committed
266
267
268
     !
     dav_iter = dav_iter + 1
     !
Marco Govoni's avatar
Marco Govoni committed
269
270
     dfpt_dim = notcnv
     diago_dim = nbase+notcnv
Marco Govoni's avatar
Marco Govoni committed
271
272
     WRITE(stdout, "( /,5x,'                  *----------*              *----------*               *----------*') ")
     WRITE(stdout, "(   5x,'#     Iteration = | ', i8,' |','   ','DFPT_dim = | ', i8,' |', '   ','Diago_dim = | ', i8,' |')") &
Marco Govoni's avatar
Marco Govoni committed
273
         &dav_iter, dfpt_dim, diago_dim
Marco Govoni's avatar
Marco Govoni committed
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
     WRITE(stdout, "(   5x,'                  *----------*              *----------*               *----------*') ")
     !
     ALLOCATE( ishift( nvecx ), STAT=ierr )
     IF( ierr /= 0 ) CALL errore( 'chidiago',' cannot allocate ishift ', ABS(ierr) )
     ishift=0
     np = 0
     !
     DO n = 1, nvec
        !
        IF ( .NOT. conv(n) ) THEN
           !
           ! ... this root not yet converged ... 
           !
           np = np + 1
           !
           ! ... reorder eigenvectors so that coefficients for unconverged
           ! ... roots come first. This allows to use quick matrix-matrix 
           ! ... multiplications to set a new basis vector (see below)
           !
           !IF ( np /= n ) vr(:,np) = vr(:,n)
           ishift(nbase+np) = n
           !
           ew(nbase+np) = ev(n)
           !   
        END IF
        !
     END DO
     !
     ! ... expand the basis set with new basis vectors ( H - e*S )|psi> ...
     !
     CALL redistribute_vr_distr( notcnv, nbase, nvecx, vr_distr, ishift )
     DEALLOCATE(ishift)
     CALL update_with_vr_distr( dvg, dng, notcnv, nbase, nvecx, vr_distr, ew )
     !
     ! ... MGS
     !
     CALL do_mgs(dvg,nbase+1,nbase+notcnv)
     !
     ! apply the response function to new vectors
     !
     ! determine image that actually compute dng first
     !
     mloc = 0 
     mstart = 1
     DO il1 = 1, pert%nloc
        ig1 = pert%l2g(il1)
        IF( ig1 < nbase+1 .OR. ig1 > nbase+notcnv ) CYCLE
        IF( mloc==0 ) mstart = il1
        mloc = mloc + 1
     ENDDO
     !
     ! Apply operator with DFPT
     !
Marco Govoni's avatar
Marco Govoni committed
327
328
     pccg_res_tr2 = tr2_dfpt
     CALL dfpt ( mloc, dvg(1,mstart), dng(1,mstart), pccg_res_tr2 ) 
Marco Govoni's avatar
Marco Govoni committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
     !
     ! ... update the reduced hamiltonian
     !
     !
     ! hr = <dvg|dng>
     !
     CALL build_hr( dvg, dng, mstart, mstart+mloc-1, hr_distr, nbase+1, nbase+notcnv ) 
     !
     nbase = nbase + notcnv
     !
     CALL symm_hr_distr(hr_distr,nbase,nvecx)
     !
     ! ... diagonalize the reduced hamiltonian
     !
     CALL diagox( nbase, nvec, hr_distr, nvecx, ew, vr_distr )
     time_spent(2)=get_clock( 'chidiago' )
Marco Govoni's avatar
Marco Govoni committed
345
     CALL get_clock_called( 'stern' , sternop_ncalls(2) )
Marco Govoni's avatar
Marco Govoni committed
346
347
348
349
350
351
352
353
354
355
356
357
358
     !
     ! ... test for convergence
     !
     conv(1:nvec) = ( ( ABS( (ew(1:nvec) - ev(1:nvec))/ev(1:nvec) ) < trev_pdep_rel ) &
                 .AND. ( ABS( ew(1:nvec) - ev(1:nvec) ) < trev_pdep ) )
     !conv(1:nvec) = ( ( ABS( ew(1:nvec) - ev(1:nvec) ) < trev_pdep ) )
     !
     notcnv = COUNT( .NOT. conv(:) )
     !
     ev(1:nvec) = ew(1:nvec)
     !
     ! Write the eigenvalues & time spent
     !
Marco Govoni's avatar
Marco Govoni committed
359
     CALL output_ev_and_time(nvec,ev,conv,time_spent,sternop_ncalls,pccg_res_tr2,dfpt_dim,diago_dim,dav_iter,notcnv)
Marco Govoni's avatar
Marco Govoni committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
     !
     ! ... if overall convergence has been achieved, or the dimension of
     ! ... the reduced basis set is becoming too large, or in any case if
     ! ... we are at the last iteration refresh the basis set. i.e. replace
     ! ... the first nvec elements with the current estimate of the
     ! ... eigenvectors;  set the basis dimension to nvec.
     !
     IF ( notcnv == 0 .OR. nbase+notcnv > nvecx .OR. kter == n_pdep_maxiter ) THEN
        !
        CALL start_clock( 'chidiago:last' )
        !
        IF ( notcnv == 0 ) THEN
           !
           ! ... all roots converged: return
           !
           CALL stop_clock( 'chidiago:last' )
           !
           CALL refresh_with_vr_distr( dvg, nvec, nbase, nvecx, vr_distr )
           !
           CALL pdep_db_write( )
           CALL wstat_restart_clear()
Marco Govoni's avatar
Marco Govoni committed
381
           !CALL output_a_report(-1)
Marco Govoni's avatar
Marco Govoni committed
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
           !
           WRITE(iter_label,'(i8)') kter
           CALL io_push_title("Convergence achieved !!! in "//TRIM(iter_label)//" steps") 
           l_is_wstat_converged = .TRUE.
           !
           EXIT iterate
           !
        ELSE IF ( kter == n_pdep_maxiter ) THEN
           !
           ! ... last iteration, some roots not converged: return
           !
           WRITE( stdout, '(5X,"WARNING: ",I5, &
                &   " eigenvalues not converged in chidiago")' ) notcnv
           !
           CALL stop_clock( 'chidiago:last' )
           !
           EXIT iterate
           !
        END IF
        !
        ! ... refresh psi, H*psi and S*psi
        !
        WRITE(stdout,'(/,7x,"Refresh the basis set")')
        !
        CALL refresh_with_vr_distr( dvg, nvec, nbase, nvecx, vr_distr )
        CALL refresh_with_vr_distr( dng, nvec, nbase, nvecx, vr_distr )
        !
        ! ... refresh the reduced hamiltonian
        !
        nbase = nvec
        !
        hr_distr = 0._DP
        vr_distr = 0._DP
        !
        DO il1 = 1, pert%nloc
           ig1 = pert%l2g(il1)
           IF( ig1 > nbase ) CYCLE 
           hr_distr(ig1,il1) = ev(ig1)
           vr_distr(ig1,il1) = 1._DP
        ENDDO
        !
        CALL stop_clock( 'chidiago:last' )
        !
     END IF
     !
     CALL wstat_restart_write( dav_iter, notcnv, nbase, ew, hr_distr, vr_distr)
Marco Govoni's avatar
Marco Govoni committed
428
     !CALL output_a_report(dav_iter)
Marco Govoni's avatar
Marco Govoni committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
     !
  END DO iterate
  !
  !
  DEALLOCATE( conv )
  DEALLOCATE( ew )
  DEALLOCATE( ev )
  DEALLOCATE( hr_distr )
  DEALLOCATE( vr_distr )
  !
  !
  DEALLOCATE( dng )
  DEALLOCATE( dvg )  
  !
  CALL stop_clock( 'chidiago' )
  !
  RETURN
  !
END SUBROUTINE
!
!----------------------------------------------------------------------------
SUBROUTINE davidson_diago_k ( )
  !----------------------------------------------------------------------------
  !
  ! ... iterative solution of the eigenvalue problem:
  ! ... ( chi - ev ) * dvg = 0
  !
  USE kinds,                ONLY : DP
  USE io_global,            ONLY : stdout
  USE distribution_center,  ONLY : pert
  USE class_idistribute,    ONLY : idistribute
  USE io_push,              ONLY : io_push_title,io_push_bar
  USE westcom,              ONLY : dvg,dng,n_pdep_eigen,trev_pdep,n_pdep_maxiter,n_pdep_basis,wstat_calculation,ev,conv,&
462
463
                                   & n_pdep_restart_from_itr,n_pdep_read_from_file,n_steps_write_restart,n_pdep_times,&
                                   & trev_pdep_rel,tr2_dfpt,l_is_wstat_converged, &
464
                                   & ngq,npwq,igq_q,npwqx
Marco Govoni's avatar
Marco Govoni committed
465
466
467
468
469
  USE pdep_db,              ONLY : pdep_db_write,pdep_db_read
  USE wstat_restart,        ONLY : wstat_restart_write, wstat_restart_clear, wstat_restart_read
  USE mp_world,             ONLY : mpime
  USE mp_global,            ONLY : inter_image_comm
  USE mp,                   ONLY : mp_sum
470
471
  USE gvect,                ONLY : gstart, g, ngm
  USE gvecw,                ONLY : gcutw
Marco Govoni's avatar
Marco Govoni committed
472
473
  USE wstat_tools,          ONLY : diagox,serial_diagox,build_hr,symm_hr_distr,redistribute_vr_distr,&
                                   & update_with_vr_distr,refresh_with_vr_distr 
474
  USE types_bz_grid,        ONLY : q_grid
475
  USE types_coulomb,        ONLY : pot3D
Marco Govoni's avatar
Marco Govoni committed
476
  USE dfpt_module,          ONLY : dfpt
Marco Govoni's avatar
Marco Govoni committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
  !
  IMPLICIT NONE
  !
  ! ... LOCAL variables
  !
  INTEGER :: nvec, nvecx
    ! dimension of the matrix to be diagonalized
    ! leading dimension of matrix evc, as declared in the calling pgm unit
  INTEGER :: dav_iter, notcnv
    ! integer  number of iterations performed
    ! number of unconverged roots
  INTEGER :: kter, nbase, np, n, m
    ! counter on iterations
    ! dimension of the reduced basis
    ! counter on the reduced basis vectors
    ! do-loop counters
    ! counter on the bands
  INTEGER :: ierr,mloc,mstart,mend
  INTEGER,ALLOCATABLE :: ishift(:)
  REAL(DP), ALLOCATABLE :: ew(:)
  COMPLEX(DP), ALLOCATABLE :: hr_distr(:,:), vr_distr(:,:)
  !
  INTEGER :: il1,il2,ig1,ig2,i
  REAL(DP) :: time_spent(2)
Marco Govoni's avatar
Marco Govoni committed
501
  INTEGER :: sternop_ncalls(2)
Marco Govoni's avatar
Marco Govoni committed
502
  CHARACTER(LEN=8) :: iter_label
Marco Govoni's avatar
Marco Govoni committed
503
504
  REAL(DP) :: pccg_res_tr2
  INTEGER :: dfpt_dim, diago_dim
Marco Govoni's avatar
Marco Govoni committed
505
  !
506
507
  INTEGER :: iq, lastdone_iq
  LOGICAL :: l_restart_q_done
508
  LOGICAL :: l_print_pdep_read
509
510
  REAL(DP) :: q(3)
  !
Marco Govoni's avatar
Marco Govoni committed
511
512
513
514
515
516
517
  REAL(DP), EXTERNAL :: GET_CLOCK
  !
  ! ... INITIALIZATION
  !
  l_is_wstat_converged = .FALSE.
  nvec = n_pdep_eigen
  nvecx = n_pdep_basis
518
519
  lastdone_iq = 0
  l_restart_q_done = .FALSE.
Marco Govoni's avatar
Marco Govoni committed
520
521
522
523
524
525
526
527
528
529
530
531
  !
  ! ... DISTRIBUTE nvecx
  !
  pert=idistribute()
  CALL pert%init(nvecx,'i','nvecx',.TRUE.)
  !CALL index_distr_init(pert,nvecx,'i','nvecx',.TRUE.) 
  CALL wstat_memory_report() ! Before allocating I report the memory required. 
  !
  ! ... MEMORY ALLOCATION
  !
  IF ( nvec > nvecx / 2 ) CALL errore( 'chidiago', 'nvecx is too small', 1 )
  !
532
  ALLOCATE( dvg( npwqx, pert%nlocx ), STAT=ierr )
Marco Govoni's avatar
Marco Govoni committed
533
534
535
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate dvg ', ABS(ierr) )
  !
536
  ALLOCATE( dng( npwqx, pert%nlocx ), STAT=ierr )
Marco Govoni's avatar
Marco Govoni committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate dng ', ABS(ierr) )
  !
  ALLOCATE( hr_distr( nvecx, pert%nlocx ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate hr_distr ', ABS(ierr) )
  !
  ALLOCATE( vr_distr( nvecx, pert%nlocx ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate vr_distr ', ABS(ierr) )
  !
  ALLOCATE( ew( nvecx ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate ew ', ABS(ierr) )
  !
  ALLOCATE( ev( nvec  ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate ev ', ABS(ierr) )
  !
  ALLOCATE( conv( nvec ), STAT=ierr )
  IF( ierr /= 0 ) &
     CALL errore( 'chidiago',' cannot allocate conv ', ABS(ierr) )
  !
560
  QPOINTS_LOOP: &
561
  DO iq = 1, q_grid%np
562
563
564
565
     !
     CALL start_clock( 'chidiago' )
     time_spent(1)=get_clock( 'chidiago' )
     CALL get_clock_called( 'stern' , sternop_ncalls(1) )
Marco Govoni's avatar
Marco Govoni committed
566
     !
567
568
569
570
571
572
573
574
575
576
     nbase  = nvec
     conv   = .FALSE.
     ev     = 0._DP
     ew     = 0._DP
     dng  = 0._DP
     dvg  = 0._DP
     hr_distr(:,:) = 0._DP
     vr_distr(:,:) = 0._DP
     notcnv  = nvec 
     dav_iter = -2
Marco Govoni's avatar
Marco Govoni committed
577
     !
578
     ! set local number of G vectors for perturbation at q
Marco Govoni's avatar
Marco Govoni committed
579
     !
580
     npwq = ngq(iq)
Marco Govoni's avatar
Marco Govoni committed
581
     !
582
     ! compute Coulomb potential
Marco Govoni's avatar
Marco Govoni committed
583
     !
584
     CALL pot3D%init('Wave',.TRUE.,'default',iq)
585
     CALL pot3d%print_divergence()
Marco Govoni's avatar
Marco Govoni committed
586
     !
587
     IF ( q_grid%np > 1 ) THEN
Marco Govoni's avatar
Marco Govoni committed
588
        !
589
        IF ( wstat_calculation == 'S' ) THEN
Marco Govoni's avatar
Marco Govoni committed
590
           !
591
592
           WRITE( stdout, '(/,5x,64a)' ) ('=',i=1,64)
           WRITE( stdout, '(5x,a,i5.5,a,3f10.4,a)')&
593
           'PDEP calculation at q(',iq,') = (',q_grid%p_cart(:,iq),' )'
594
           WRITE( stdout, '(5x,64a)' ) ('-',i=1,64)
Marco Govoni's avatar
Marco Govoni committed
595
           !
596
        ELSEIF ( wstat_calculation == 'R' .AND. l_restart_q_done ) THEN
Marco Govoni's avatar
Marco Govoni committed
597
           !
598
599
           WRITE( stdout, '(/,5x,64a)' ) ('=',i=1,64)
           WRITE( stdout, '(5x,a,i5.5,a,3f10.4,a)')&
600
           'PDEP calculation at q(',iq,') = (',q_grid%p_cart(:,iq),' )'
601
           WRITE( stdout, '(5x,64a)' ) ('-',i=1,64)
Marco Govoni's avatar
Marco Govoni committed
602
           !
603
        ENDIF
Marco Govoni's avatar
Marco Govoni committed
604
        !
605
     ENDIF
Marco Govoni's avatar
Marco Govoni committed
606
     !
607
     ! KIND OF CALCULATION
Marco Govoni's avatar
Marco Govoni committed
608
     !
609
610
611
     SELECT CASE(wstat_calculation)
     CASE('r','R')
        !
612
613
614
615
616
617
618
619
620
621
622
623
624
        IF ( .NOT. l_restart_q_done ) THEN
           !
           CALL wstat_restart_read( dav_iter, notcnv, nbase, ew, hr_distr, vr_distr, lastdone_iq, iq )
           !
           IF ( iq < lastdone_iq ) THEN
              CYCLE QPOINTS_LOOP
           ELSEIF ( iq == lastdone_iq .AND. notcnv == 0 ) THEN
              CYCLE QPOINTS_LOOP
           ENDIF
           !
           l_restart_q_done = .TRUE.
           wstat_calculation = 'S' ! Start from scratch for the next q (after restart has been done)
           !
625
626
627
628
629
630
631
632
        ENDIF
        !
     CASE('s','S')
        !
        ! FROM SCRATCH
        !
        ! ... Eventually read from file
        !
633
634
635
636
637
638
        IF (iq==1) THEN 
           l_print_pdep_read = .TRUE.
        ELSE 
           l_print_pdep_read = .FALSE.
        ENDIF
        IF(n_pdep_read_from_file>0) CALL pdep_db_read( n_pdep_read_from_file, iq, l_print_pdep_read)
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
        !
        ! ... Eventually randomize
        !
        IF(n_pdep_read_from_file<nvec) CALL do_randomize_q ( dvg, n_pdep_read_from_file+1, nvec, iq)
        !
        ! ... MGS
        !
        CALL do_mgs( dvg, 1, nvec)
        !
        dfpt_dim = nbase
        diago_dim = nbase
        WRITE(stdout, "( /,5x,'                  *----------*              *----------*               *----------*') ")
        WRITE(stdout, &
            & "(   5x,'#     Iteration = | ', a8,' |','   ','DFPT_dim = | ', i8,' |', '   ','Diago_dim = | ', i8,' |  x 1/2')")&
            & 'starting', dfpt_dim, diago_dim
        WRITE(stdout, "(   5x,'                  *----------*              *----------*               *----------*') ")
        !
        ! Apply operator with DFPT
        !
        mloc = 0
        mstart = 1 
        DO il1 = 1, pert%nloc
           ig1 = pert%l2g(il1)
           IF( ig1 < 1 .OR. ig1 > nvec ) CYCLE
           IF( mloc==0 ) mstart = il1
           mloc = mloc + 1
        ENDDO
        !
        pccg_res_tr2 = -1._DP
668
        !
669
        CALL dfpt ( mloc, dvg(1,mstart), dng(1,mstart), pccg_res_tr2, iq ) 
670
671
672
673
674
675
676
677
        dav_iter = -1
        CALL wstat_restart_write( dav_iter, notcnv, nbase, ew, hr_distr, vr_distr, iq)
        !
     CASE DEFAULT
        !
        CALL errore('chidiago', 'Wrong wstat_calculation',1)
        !
     END SELECT
Marco Govoni's avatar
Marco Govoni committed
678
     !
679
     IF( dav_iter == -2 ) CALL errore( 'chidiago','Cannot find the 1st starting loop',1) 
Marco Govoni's avatar
Marco Govoni committed
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
     IF( dav_iter == -1 ) THEN
        !
        ! < EXTRA STEP > 
        !
        dvg = dng
        CALL do_mgs( dvg, 1, nvec)
        !
        dfpt_dim = nbase
        diago_dim = nbase
        WRITE(stdout, "( /,5x,'                  *----------*              *----------*               *----------*') ")
        WRITE(stdout, &
            & "(   5x,'#     Iteration = | ', a8,' |','   ','DFPT_dim = | ', i8,' |', '   ','Diago_dim = | ', i8,' |  x 2/2')")&
            & 'starting', dfpt_dim, diago_dim
        WRITE(stdout, "(   5x,'                  *----------*              *----------*               *----------*') ")
        !
        ! Apply operator with DFPT
        !
        mloc = 0
        mstart = 1 
        DO il1 = 1, pert%nloc
           ig1 = pert%l2g(il1)
           IF( ig1 < 1 .OR. ig1 > nvec ) CYCLE
           IF( mloc==0 ) mstart = il1
           mloc = mloc + 1
        ENDDO
        !
        pccg_res_tr2 = MIN(0.01_DP,1000000._DP*tr2_dfpt)
708
        !
709
        CALL dfpt ( mloc, dvg(1,mstart), dng(1,mstart), pccg_res_tr2, iq ) 
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
        ! 
        ! </ EXTRA STEP >
        !
        ! hr = <dvg|dng>
        !
        CALL build_hr( dvg, dng, mstart, mstart+mloc-1, hr_distr, 1, nvec ) 
        !
        ! ... diagonalize the reduced hamiltonian
        !
        CALL diagox( nbase, nvec, hr_distr, nvecx, ew, vr_distr )
        time_spent(2)=get_clock( 'chidiago' )
        CALL get_clock_called( 'stern' , sternop_ncalls(2) )
        ev(1:nvec) = ew(1:nvec)
        !
        ! Write the eigenvalues & time spent
        !
        CALL output_ev_and_time_q( nvec,ev,conv,time_spent,sternop_ncalls,pccg_res_tr2,dfpt_dim,diago_dim,dav_iter,notcnv,iq )
        dav_iter = 0
        CALL wstat_restart_write( dav_iter, notcnv, nbase, ew, hr_distr, vr_distr, iq )
        !
     ENDIF
Marco Govoni's avatar
Marco Govoni committed
731
     !
732
     ! --------------------
Marco Govoni's avatar
Marco Govoni committed
733
     !
734
     ! ... iterate
Marco Govoni's avatar
Marco Govoni committed
735
     !
736
     ! --------------------
Marco Govoni's avatar
Marco Govoni committed
737
     !
738
     iterate: DO kter = 1, n_pdep_maxiter
Marco Govoni's avatar
Marco Govoni committed
739
        !
740
741
        time_spent(1) = get_clock( 'chidiago' )
        CALL get_clock_called( 'stern' , sternop_ncalls(1) )
Marco Govoni's avatar
Marco Govoni committed
742
        !
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
        dav_iter = dav_iter + 1
        !
        dfpt_dim = notcnv
        diago_dim = nbase+notcnv
        WRITE(stdout, "( /,5x,'                  *----------*              *----------*               *----------*') ")
        WRITE(stdout, "(   5x,'#     Iteration = | ', i8,' |','   ','DFPT_dim = | ', i8,' |', '   ','Diago_dim = | ', i8,' |')") &
         &dav_iter, dfpt_dim, diago_dim
        WRITE(stdout, "(   5x,'                  *----------*              *----------*               *----------*') ")
        !
        ALLOCATE( ishift( nvecx ), STAT=ierr )
        IF( ierr /= 0 ) CALL errore( 'chidiago',' cannot allocate ishift ', ABS(ierr) )
        ishift=0
        np = 0
        !
        DO n = 1, nvec
Marco Govoni's avatar
Marco Govoni committed
758
           !
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
           IF ( .NOT. conv(n) ) THEN
              !
              ! ... this root not yet converged ... 
              !
              np = np + 1
              !
              ! ... reorder eigenvectors so that coefficients for unconverged
              ! ... roots come first. This allows to use quick matrix-matrix 
              ! ... multiplications to set a new basis vector (see below)
              !
              !IF ( np /= n ) vr(:,np) = vr(:,n)
              ishift(nbase+np) = n
              !
              ew(nbase+np) = ev(n)
              !   
           END IF
Marco Govoni's avatar
Marco Govoni committed
775
           !
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
        END DO
        !
        ! ... expand the basis set with new basis vectors ( H - e*S )|psi> ...
        !
        CALL redistribute_vr_distr( notcnv, nbase, nvecx, vr_distr, ishift )
        DEALLOCATE(ishift)
        CALL update_with_vr_distr( dvg, dng, notcnv, nbase, nvecx, vr_distr, ew )
        !
        ! ... MGS
        !
        CALL do_mgs(dvg,nbase+1,nbase+notcnv)
        !
        ! apply the response function to new vectors
        !
        ! determine image that actually compute dng first
        !
        mloc = 0 
        mstart = 1
        DO il1 = 1, pert%nloc
           ig1 = pert%l2g(il1)
           IF( ig1 < nbase+1 .OR. ig1 > nbase+notcnv ) CYCLE
           IF( mloc==0 ) mstart = il1
           mloc = mloc + 1
        ENDDO
        !
        ! Apply operator with DFPT
        !
        pccg_res_tr2 = tr2_dfpt
804
        !
805
        CALL dfpt ( mloc, dvg(1,mstart), dng(1,mstart), pccg_res_tr2, iq ) 
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
        !
        ! ... update the reduced hamiltonian
        !
        !
        ! hr = <dvg|dng>
        !
        CALL build_hr( dvg, dng, mstart, mstart+mloc-1, hr_distr, nbase+1, nbase+notcnv ) 
        !
        nbase = nbase + notcnv
        !
        CALL symm_hr_distr(hr_distr,nbase,nvecx)
        !
        ! ... diagonalize the reduced hamiltonian
        !
        CALL diagox( nbase, nvec, hr_distr, nvecx, ew, vr_distr )
        time_spent(2)=get_clock( 'chidiago' )
        CALL get_clock_called( 'stern' , sternop_ncalls(2) )
        !
        ! ... test for convergence
        !
        conv(1:nvec) = ( ( ABS( (ew(1:nvec) - ev(1:nvec))/ev(1:nvec) ) < trev_pdep_rel ) &
                    .AND. ( ABS( ew(1:nvec) - ev(1:nvec) ) < trev_pdep ) )
        !conv(1:nvec) = ( ( ABS( ew(1:nvec) - ev(1:nvec) ) < trev_pdep ) )
        !
        notcnv = COUNT( .NOT. conv(:) )
        !
        ev(1:nvec) = ew(1:nvec)
        !
        ! Write the eigenvalues & time spent
        !
        CALL output_ev_and_time_q(nvec,ev,conv,time_spent,sternop_ncalls,pccg_res_tr2,dfpt_dim,diago_dim,dav_iter,notcnv,iq)
        !
        ! ... if overall convergence has been achieved, or the dimension of
        ! ... the reduced basis set is becoming too large, or in any case if
        ! ... we are at the last iteration refresh the basis set. i.e. replace
        ! ... the first nvec elements with the current estimate of the
        ! ... eigenvectors;  set the basis dimension to nvec.
        !
        IF ( notcnv == 0 .OR. nbase+notcnv > nvecx .OR. kter == n_pdep_maxiter ) THEN
Marco Govoni's avatar
Marco Govoni committed
845
           !
846
           CALL start_clock( 'chidiago:last' )
Marco Govoni's avatar
Marco Govoni committed
847
           !
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
           IF ( notcnv == 0 ) THEN
              !
              ! ... all roots converged: return
              !
              CALL stop_clock( 'chidiago:last' )
              !
              CALL refresh_with_vr_distr( dvg, nvec, nbase, nvecx, vr_distr )
              !
              CALL pdep_db_write( iq )
              CALL wstat_restart_clear()
              !CALL output_a_report(-1)
              !
              WRITE(iter_label,'(i8)') kter
              CALL io_push_title("Convergence achieved !!! in "//TRIM(iter_label)//" steps") 
              l_is_wstat_converged = .TRUE.
              !
              EXIT iterate
              !
           ELSE IF ( kter == n_pdep_maxiter ) THEN
              !
              ! ... last iteration, some roots not converged: return
              !
              WRITE( stdout, '(5X,"WARNING: ",I5, &
                   &   " eigenvalues not converged in chidiago")' ) notcnv
              !
              CALL stop_clock( 'chidiago:last' )
              !
              EXIT iterate
              !
           END IF
Marco Govoni's avatar
Marco Govoni committed
878
           !
879
           ! ... refresh psi, H*psi and S*psi
Marco Govoni's avatar
Marco Govoni committed
880
           !
881
           WRITE(stdout,'(/,7x,"Refresh the basis set")')
Marco Govoni's avatar
Marco Govoni committed
882
           !
883
884
           CALL refresh_with_vr_distr( dvg, nvec, nbase, nvecx, vr_distr )
           CALL refresh_with_vr_distr( dng, nvec, nbase, nvecx, vr_distr )
Marco Govoni's avatar
Marco Govoni committed
885
           !
886
           ! ... refresh the reduced hamiltonian
Marco Govoni's avatar
Marco Govoni committed
887
           !
888
           nbase = nvec
Marco Govoni's avatar
Marco Govoni committed
889
           !
890
891
           hr_distr = 0._DP
           vr_distr = 0._DP
Marco Govoni's avatar
Marco Govoni committed
892
           !
893
894
895
896
897
898
899
900
           DO il1 = 1, pert%nloc
              ig1 = pert%l2g(il1)
              IF ( ig1 < 1 .OR. ig1 > nbase ) CYCLE
              hr_distr(ig1,il1) = ev(ig1)
              vr_distr(ig1,il1) = 1._DP
           ENDDO
           !
           CALL stop_clock( 'chidiago:last' )
Marco Govoni's avatar
Marco Govoni committed
901
902
903
           !
        END IF
        !
904
905
        CALL wstat_restart_write( dav_iter, notcnv, nbase, ew, hr_distr, vr_distr, iq)
        !!CALL output_a_report(dav_iter)
Marco Govoni's avatar
Marco Govoni committed
906
        !
907
     END DO iterate
Marco Govoni's avatar
Marco Govoni committed
908
  !
909
910
  CALL stop_clock( 'chidiago' )
  !
911
  ENDDO QPOINTS_LOOP ! iq
Marco Govoni's avatar
Marco Govoni committed
912
913
914
915
916
917
918
919
920
  !
  DEALLOCATE( conv )
  DEALLOCATE( ew )
  DEALLOCATE( ev )
  DEALLOCATE( hr_distr )
  DEALLOCATE( vr_distr )
  !
  !
  DEALLOCATE( dng )
921
  DEALLOCATE( dvg )
Marco Govoni's avatar
Marco Govoni committed
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
  !
  RETURN
  !
END SUBROUTINE
!
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!
SUBROUTINE do_mgs(amat,m_global_start,m_global_end) 
  !
  ! MGS of the vectors beloging to the interval [ m_global_start, m_global_end ]
  !    also with respect to the vectors belonging to the interval [ 1, m_global_start -1 ]
  !
  USE kinds,                  ONLY : DP
  USE io_global,              ONLY : stdout
  USE mp_global,              ONLY : intra_bgrp_comm,inter_image_comm,my_image_id,nimage,world_comm 
  USE gvect,                  ONLY : gstart
  USE mp,                     ONLY : mp_sum,mp_barrier,mp_bcast
941
  USE westcom,                ONLY : npwq,npwqx
Marco Govoni's avatar
Marco Govoni committed
942
943
944
945
946
947
948
949
950
  USE control_flags,          ONLY : gamma_only
  USE io_push,                ONLY : io_push_title 
  USE distribution_center,    ONLY : pert
  !
  IMPLICIT NONE
  !
  ! I/O
  !
  INTEGER, INTENT(IN) :: m_global_start,m_global_end
951
  COMPLEX(DP) :: amat(npwqx,pert%nlocx)
Marco Govoni's avatar
Marco Govoni committed
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
  !
  ! Workspace 
  !
  INTEGER :: ig, ip, j, ncol
  INTEGER :: k_global, k_local, j_local, k_id
  REAL(DP) :: anorm
  REAL(DP),ALLOCATABLE :: braket(:)
  REAL(DP) :: time_spent(2)
  COMPLEX(DP),ALLOCATABLE :: vec(:),zbraket(:)
  LOGICAL :: unfinished
  COMPLEX(DP) :: za
  INTEGER :: m_local_start,m_local_end
  !
  REAL(DP),EXTERNAL :: DDOT
  COMPLEX(DP),EXTERNAL :: ZDOTC
  !
  CALL mp_barrier(world_comm)
  !
  CALL start_clock ('paramgs')
  !
972
  ALLOCATE( vec(npwqx), zbraket(pert%nloc), braket(pert%nloc) )
Marco Govoni's avatar
Marco Govoni committed
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
  !
  ! 1) Run some checks
  !
  IF( m_global_start < 1 .OR. m_global_start > m_global_end .OR. m_global_end > pert%nglob ) &
     & CALL errore( 'mgs', 'do_mgs problem', 1 )
  !
  ! 2) Localize m_global_start
  !
  m_local_start = 1 
  DO ip = 1, pert%nloc
     ig = pert%l2g(ip)
     IF( ig < m_global_start ) CYCLE
     m_local_start = ip 
     EXIT
  ENDDO
  !
  ! 3) Localize m_global_end
  !
  m_local_end = pert%nloc
  DO ip = pert%nloc, 1, -1
     ig = pert%l2g(ip)
     IF( ig > m_global_end ) CYCLE
     m_local_end = ip
     EXIT
  ENDDO
  !
  j_local=1
  unfinished=.true.
  !
  !DO k_global=m_global_start,m_global_end
  DO k_global=1,m_global_end
     !
     CALL pert%g2l(k_global,k_local,k_id)
     !CALL distr_g2l(k_global,k_local,k_id,nimage)
     !
     IF(my_image_id==k_id) THEN
        !
        ! 4) Eventually, normalize the current vector
        !
        IF( k_global >= m_global_start ) THEN
           !
           ! anorm = < k_l | k_l >
           !
           IF(gamma_only) THEN
1017
              anorm = 2.0_DP * DDOT(2*npwq,amat(1,k_local),1,amat(1,k_local),1)
Marco Govoni's avatar
Marco Govoni committed
1018
1019
              IF(gstart==2) anorm = anorm - REAL(amat(1,k_local),KIND=DP) * REAL(amat(1,k_local),KIND=DP)
           ELSE
1020
              anorm = DDOT(2*npwq,amat(1,k_local),1,amat(1,k_local),1)
Marco Govoni's avatar
Marco Govoni committed
1021
1022
1023
1024
1025
1026
1027
           ENDIF
           !
           CALL mp_sum(anorm,intra_bgrp_comm)
           !
           ! normalize | k_l >
           !
           za = CMPLX( 1._DP/SQRT(anorm), 0._DP,KIND=DP)
1028
           CALL ZSCAL(npwq,za,amat(1,k_local),1)
Marco Govoni's avatar
Marco Govoni committed
1029
1030
1031
1032
1033
           !
        ENDIF
        !
        ! 5) Copy the current vector into V
        !
1034
        CALL ZCOPY(npwqx,amat(1,k_local),1,vec(1),1)
Marco Govoni's avatar
Marco Govoni committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
        !
        j_local=MAX(k_local+1,m_local_start)
        !
        !IF(j_local>pert%nloc) unfinished=.false.
        IF(j_local>m_local_end) unfinished=.false.
        !
     ENDIF
     !
     ! BCAST | vec >
     !
     CALL mp_bcast(vec,k_id,inter_image_comm)
     !
     ! Update when needed
     !
     IF(unfinished) THEN
        !
        ! IN the range ip=j_local:pert%nloc    = >    | ip > = | ip > - | vec > * < vec | ip >
        !
        IF(gamma_only) THEN
           DO ip = j_local,m_local_end !pert%nloc
1055
              braket(ip) = 2._DP * DDOT(2*npwq,vec(1),1,amat(1,ip),1)
Marco Govoni's avatar
Marco Govoni committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
           ENDDO
           !IF (gstart==2) FORALL( ip=j_local:pert%nloc ) braket(ip) = braket(ip) - REAL(vec(1),KIND=DP)*REAL(amat(1,ip),KIND=DP) 
           IF (gstart==2) FORALL( ip=j_local:m_local_end ) braket(ip) = braket(ip) - REAL(vec(1),KIND=DP)*REAL(amat(1,ip),KIND=DP) 
           !CALL mp_sum(braket(j_local:pert%nloc),intra_bgrp_comm)
           CALL mp_sum(braket(j_local:m_local_end),intra_bgrp_comm)
           !FORALL(ip=j_local:pert%nloc) zbraket(ip) = CMPLX( braket(ip), 0._DP, KIND=DP)
           FORALL(ip=j_local:m_local_end) zbraket(ip) = CMPLX( braket(ip), 0._DP, KIND=DP)
        ELSE
           DO ip = j_local,m_local_end !pert%nloc
1065
              zbraket(ip) = ZDOTC(npwq,vec(1),1,amat(1,ip),1)
Marco Govoni's avatar
Marco Govoni committed
1066
1067
1068
1069
1070
1071
1072
           ENDDO
           !CALL mp_sum(zbraket(j_local:pert%nloc),intra_bgrp_comm)
           CALL mp_sum(zbraket(j_local:m_local_end),intra_bgrp_comm)
        ENDIF
        !
        ncol=m_local_end-j_local+1
        !ncol=m_global_end-j_local+1
1073
        CALL ZGERU(npwqx,ncol,(-1._DP,0._DP),vec(1),1,zbraket(j_local),1,amat(1,j_local),npwqx)
Marco Govoni's avatar
Marco Govoni committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
        !
     ENDIF
     !
  ENDDO
  !
  DEALLOCATE( vec,zbraket,braket )
  !
  CALL mp_barrier(world_comm)
  !
  CALL stop_clock ('paramgs')
  !
END SUBROUTINE
!
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!
SUBROUTINE do_randomize ( amat, mglobalstart, mglobalend  ) 
  !
  ! Randomize in dvg the vectors belonging to [ mglobalstart, mglobalend ]
  !
  USE kinds,                ONLY : DP
  USE random_numbers,       ONLY : randy
  USE gvect,                ONLY : g,gstart,ngm_g,ig_l2g
1098
  USE westcom,              ONLY : npwq,dvg,npwqx
Marco Govoni's avatar
Marco Govoni committed
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
  USE constants,            ONLY : tpi
  USE mp,                   ONLY : mp_barrier
  USE mp_global,            ONLY : world_comm
  USE distribution_center,  ONLY : pert
  !
  IMPLICIT NONE
  !
  ! I/O
  !
  INTEGER,INTENT(IN) :: mglobalstart, mglobalend
1109
  COMPLEX(DP) :: amat(npwqx,pert%nlocx)
Marco Govoni's avatar
Marco Govoni committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  !
  ! Workspace
  !
  REAL(DP),ALLOCATABLE :: random_num_debug(:,:)
  INTEGER :: il1,ig1,ig
  REAL(DP) :: aux_real
  REAL(DP) :: rr, arg
  !
  CALL mp_barrier(world_comm)
  !
  CALL start_clock ('randomize')
  !
  ! Random numbers are generated according to G-global
  !
  ALLOCATE(random_num_debug(2,ngm_g))
  !
  DO il1=1,pert%nloc
     !
     ig1=pert%l2g(il1)
     IF( ig1 < mglobalstart .OR. ig1 > mglobalend ) CYCLE
     !
     ! Initialize the sequence
     !
     aux_real=randy(ig1)
     !
     DO ig=1,ngm_g
        random_num_debug(1:2,ig) = (/ randy(), randy() /)
     ENDDO
     !
     amat(:,il1) = 0._DP
!$OMP PARALLEL private(ig,rr,arg)
!$OMP DO
1142
     DO ig=gstart,npwq
Marco Govoni's avatar
Marco Govoni committed
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
        rr = random_num_debug(1,ig_l2g(ig))
        arg = tpi * random_num_debug(2,ig_l2g(ig))
        amat(ig,il1) = CMPLX( rr*COS( arg ), rr*SIN( arg ), KIND=DP) / &
                      ( g(1,ig)*g(1,ig) + &
                        g(2,ig)*g(2,ig) + &
                        g(3,ig)*g(3,ig) + 1.0_DP )
     ENDDO
!$OMP ENDDO
!$OMP END PARALLEL
     !
  ENDDO
  !
  DEALLOCATE(random_num_debug)
  !
  CALL stop_clock ('randomize')
  !
END SUBROUTINE
!
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!
1165
1166
1167
1168
1169
1170
1171
SUBROUTINE do_randomize_q (amat, mglobalstart, mglobalend, iq) 
  !
  ! Randomize in dvg the vectors belonging to [ mglobalstart, mglobalend ]
  !
  USE kinds,                ONLY : DP
  USE random_numbers,       ONLY : randy
  USE gvect,                ONLY : g,gstart,ngm_g,ig_l2g
Matteo Gerosa's avatar
Matteo Gerosa committed
1172
1173
  USE westcom,              ONLY : dvg,npwqx,ngq,igq_q
  USE constants,            ONLY : tpi,eps8
1174
1175
1176
1177
  USE cell_base,            ONLY : tpiba2
  USE mp,                   ONLY : mp_barrier
  USE mp_global,            ONLY : world_comm
  USE distribution_center,  ONLY : pert
Matteo Gerosa's avatar
Matteo Gerosa committed
1178
  USE types_bz_grid,        ONLY : q_grid
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
  !
  IMPLICIT NONE
  !
  ! I/O
  !
  INTEGER,INTENT(IN) :: mglobalstart, mglobalend
  COMPLEX(DP) :: amat(npwqx,pert%nlocx)
  INTEGER,INTENT(IN) :: iq
  !
  ! Workspace
  !
  REAL(DP),ALLOCATABLE :: random_num_debug(:,:)
  INTEGER :: il1,ig1,ig
  REAL(DP) :: aux_real
Matteo Gerosa's avatar
Matteo Gerosa committed
1193
  REAL(DP) :: rr, arg, qg(3), qgnorm2
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
  !
  CALL mp_barrier(world_comm)
  !
  CALL start_clock ('randomize')
  !
  ! Random numbers are generated according to G-global
  !
  ALLOCATE(random_num_debug(2,ngm_g))
  !
  DO il1=1,pert%nloc
     !
     ig1=pert%l2g(il1)
     IF( ig1 < mglobalstart .OR. ig1 > mglobalend ) CYCLE
     !
     ! Initialize the sequence
     !
     aux_real=randy(ig1)
     !
     DO ig=1,ngm_g
        random_num_debug(1:2,ig) = (/ randy(), randy() /)
     ENDDO
     !
     amat(:,il1) = 0._DP
!$OMP PARALLEL private(ig,rr,arg)
!$OMP DO
Matteo Gerosa's avatar
Matteo Gerosa committed
1219
     DO ig=1,ngq(iq)
1220
        qg(:) = q_grid%p_cart(:,iq) + g(:,igq_q(ig,iq))
Matteo Gerosa's avatar
Matteo Gerosa committed
1221
1222
        qgnorm2 = SUM( qg(:)**2 ) * tpiba2
        IF ( qgnorm2 < eps8 ) CYCLE
1223
1224
        rr = random_num_debug(1,ig_l2g(igq_q(ig,iq)))
        arg = tpi * random_num_debug(2,ig_l2g(igq_q(ig,iq)))
Matteo Gerosa's avatar
Matteo Gerosa committed
1225
        amat(ig,il1) = CMPLX( rr*COS( arg ), rr*SIN( arg ), KIND=DP) / ( qgnorm2 + 1.0_DP ) 
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
     ENDDO
!$OMP ENDDO
!$OMP END PARALLEL
     !
  ENDDO
  !
  DEALLOCATE(random_num_debug)
  !
  CALL stop_clock ('randomize')
  !
END SUBROUTINE
!
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!
Marco Govoni's avatar
Marco Govoni committed
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
!SUBROUTINE output_a_report(iteration)
!   !
!   USE json_module,  ONLY : json_file
!   USE kinds,        ONLY : DP
!   USE westcom,      ONLY : n_pdep_eigen,ev,conv,wstat_output_dir,logfile
!   USE west_io ,     ONLY : serial_table_output
!   USE mp_world,     ONLY : mpime,root
!   !
!   IMPLICIT NONE
!   !
!   TYPE(json_file) :: json
!   INTEGER,INTENT(IN) :: iteration
!   CHARACTER(LEN=9) :: pref
!   INTEGER :: ip, ierr, iunit
!   !REAL(DP) :: out_tab(n_pdep_eigen,3)
!   !
!   INTEGER,ALLOCATABLE :: converged(:)
!   !
!   ALLOCATE( converged(n_pdep_eigen) )
!   !
!   converged = 0
!   DO ip=1,n_pdep_eigen
!      !out_tab(ip,1) = REAL(ip,DP)
!      !ipert(ip) = ip 
!      !out_tab(ip,2) = ev(ip)
!      !out_tab(ip,3) = 0._DP
!      !IF(conv(ip)) out_tab(ip,3) = 1._DP
!      IF(conv(ip)) converged(ip) = 1
!   ENDDO
!   IF(iteration>=0) THEN 
!      WRITE(pref,"('itr_',i5.5)") iteration
!   ELSE
!      pref='converged'
!   ENDIF
!   !CALL serial_table_output(mpime==root,4000,'wstat.'//TRIM(ADJUSTL(pref)),out_tab,n_pdep_eigen,3,(/'   iprt','eigenv.','  conv.'/))
!   !
!   IF( mpime == root ) THEN 
!      !
!      CALL json%initialize()
!      !
!      CALL json%load_file(filename=TRIM(logfile))
!      !
!      CALL json%add('output.davitr'//TRIM(pref)//.'egvl',  ev        ( 1:n_pdep_eigen ) )
!      CALL json%add('output.davitr'//TRIM(pref)//.'conv',  converged ( 1:n_pdep_eigen ) )
!      !
!      !OPEN(UNIT=4000,FILE=TRIM(ADJUSTL(wstat_output_dir))//"/o-wstat."//TRIM(ADJUSTL(pref))//".json",IOSTAT=ierr) 
!      !CALL json%print_file( 4000 )
!      !CLOSE( 4000, IOSTAT=ierr )
!      !
!      OPEN( NEWUNIT=iunit,FILE=TRIM(logfile) )
!      CALL json%print_file( iunit )
!      CLOSE( iunit )
!      !
!      CALL json%destroy()
!      !
!   ENDIF 
!   !
!   !DEALLOCATE( iprt ) 
!   DEALLOCATE( converged ) 
!   !
!END SUBROUTINE
Marco Govoni's avatar
Marco Govoni committed
1303
1304
1305
1306
1307
!
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!
Marco Govoni's avatar
Marco Govoni committed
1308
SUBROUTINE output_ev_and_time(nvec,ev_,conv_,time,sternop,tr2,dfpt_dim,diago_dim,dav_iter,notcnv)
Marco Govoni's avatar
Marco Govoni committed
1309
   !
Marco Govoni's avatar
Marco Govoni committed
1310
   USE json_module,          ONLY : json_file
Marco Govoni's avatar
Marco Govoni committed
1311
1312
1313
   USE kinds,                ONLY : DP
   USE io_global,            ONLY : stdout
   USE io_push,              ONLY : io_push_title,io_push_bar
Marco Govoni's avatar
Marco Govoni committed
1314
1315
   USE mp_world,             ONLY : mpime, root
   USE westcom,              ONLY : logfile
Marco Govoni's avatar
Marco Govoni committed
1316
1317
1318
   !
   IMPLICIT NONE
   !
Marco Govoni's avatar
Marco Govoni committed
1319
   TYPE(json_file) :: json
Marco Govoni's avatar
Marco Govoni committed
1320
   INTEGER,INTENT(IN) :: nvec
Marco Govoni's avatar
Marco Govoni committed
1321
1322
   REAL(DP),INTENT(IN) :: ev_(nvec)
   LOGICAL,INTENT(IN) :: conv_(nvec)
Marco Govoni's avatar
Marco Govoni committed
1323
   REAL(DP),INTENT(IN) :: time(2)
Marco Govoni's avatar
Marco Govoni committed
1324
1325
1326
   INTEGER,INTENT(IN) :: sternop(2)
   REAL(DP),INTENT(IN) :: tr2
   INTEGER,INTENT(IN) :: dav_iter, notcnv, dfpt_dim, diago_dim
Marco Govoni's avatar
Marco Govoni committed
1327
   !
Marco Govoni's avatar
Marco Govoni committed
1328
   INTEGER :: i,j, ip
Marco Govoni's avatar
Marco Govoni committed
1329
   CHARACTER(20),EXTERNAL :: human_readable_time
Marco Govoni's avatar
Marco Govoni committed
1330
1331
1332
   CHARACTER(LEN=6) :: cdav
   INTEGER :: ndav,iunit
   LOGICAL :: found
Marco Govoni's avatar
Marco Govoni committed
1333
1334
1335
   !
   WRITE(stdout,'(7X,a)') ' '
   DO i = 1, INT( nvec / 9 )
Marco Govoni's avatar
Marco Govoni committed
1336
      WRITE(stdout,'(6X, 9(f9.5,1x))') (ev_(j), j=9*(i-1)+1,9*i)
Marco Govoni's avatar
Marco Govoni committed
1337
   ENDDO
Marco Govoni's avatar
Marco Govoni committed
1338
   IF( MOD(nvec,9) > 0 ) WRITE(stdout,'(6X, 9(f9.5,1x))') (ev_(j), j=9*INT(nvec/9)+1,nvec)
Marco Govoni's avatar
Marco Govoni committed
1339
1340
1341
1342
1343
1344
   WRITE(stdout,'(7X,a)') ' '
   CALL io_push_bar()
   WRITE(stdout, "(5x,'Tot. elapsed time ',a,',  time spent in last iteration ',a) ") &
   TRIM(human_readable_time(time(2))), TRIM(human_readable_time(time(2)-time(1)))
   CALL io_push_bar()
   !
Marco Govoni's avatar
Marco Govoni committed
1345
1346
1347
1348
1349
1350
   IF( mpime == root ) THEN 
      !
      CALL json%initialize()
      !
      CALL json%load_file(filename=TRIM(logfile))
      !
Govoni's avatar
Govoni committed
1351
      CALL json%get('exec.ndav', ndav, found )
Marco Govoni's avatar
Marco Govoni committed
1352
1353
1354
1355
1356
1357
1358
1359
1360
      !
      IF( found ) THEN 
         ndav = ndav+1
      ELSE 
         ndav = 1
      ENDIF
      !
      WRITE(cdav,'(i6)') ndav
      !
Govoni's avatar
Govoni committed
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
      CALL json%update('exec.ndav', ndav, found )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').dav_iter', dav_iter )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').ev',  ev_        ( 1:nvec ) )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').conv', conv_ ( 1:nvec ) )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').notcnv', notcnv )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').time_elap:sec', time(2) )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').time_elap:hum', TRIM(human_readable_time(time(2))) )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').time_iter:sec', (time(2)-time(1)))
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').time_iter:hum', TRIM(human_readable_time(time(2)-time(1))) )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').sternop_ncalls', sternop(2)-sternop(1) )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').dfpt_tr2', tr2 )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').dfpt_dim', dfpt_dim )
      CALL json%add('exec.davitr('//TRIM(ADJUSTL(cdav))//').diago_dim', diago_dim )
Marco Govoni's avatar
Marco Govoni committed
1374
1375
1376
1377
1378
1379
1380
1381
1382
      !
      OPEN( NEWUNIT=iunit,FILE=TRIM(logfile) )
      CALL json%print_file( iunit )
      CLOSE( iunit )
      !
      CALL json%destroy()
      !
   ENDIF 
   !
Marco Govoni's avatar
Marco Govoni committed
1383
END SUBROUTINE
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396

! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!
SUBROUTINE output_ev_and_time_q(nvec,ev_,conv_,time,sternop,tr2,dfpt_dim,diago_dim,dav_iter,notcnv,iq)
   !
   USE json_module,          ONLY : json_file
   USE kinds,                ONLY : DP
   USE io_global,            ONLY : stdout
   USE io_push,              ONLY : io_push_title,io_push_bar
   USE mp_world,             ONLY : mpime, root
   USE westcom,              ONLY : logfile
1397
   USE types_bz_grid,        ONLY : q_grid
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
   !
   IMPLICIT NONE
   !
   TYPE(json_file) :: json
   INTEGER,INTENT(IN) :: nvec
   REAL(DP),INTENT(IN) :: ev_(nvec)
   LOGICAL,INTENT(IN) :: conv_(nvec)
   REAL(DP),INTENT(IN) :: time(2)
   INTEGER,INTENT(IN) :: sternop(2)
   REAL(DP),INTENT(IN) :: tr2
   INTEGER,INTENT(IN) :: dav_iter, notcnv, dfpt_dim, diago_dim
   INTEGER,INTENT(IN) :: iq
   !
   INTEGER :: i,j, ip
   CHARACTER(20),EXTERNAL :: human_readable_time
1413
1414
   CHARACTER(LEN=6) :: cdav
   CHARACTER(LEN=5) :: cq
1415
   INTEGER :: ndav(q_grid%np),iunit
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
   LOGICAL :: found
   !
   WRITE(stdout,'(7X,a)') ' '
   DO i = 1, INT( nvec / 9 )
      WRITE(stdout,'(6X, 9(f9.5,1x))') (ev_(j), j=9*(i-1)+1,9*i)
   ENDDO
   IF( MOD(nvec,9) > 0 ) WRITE(stdout,'(6X, 9(f9.5,1x))') (ev_(j), j=9*INT(nvec/9)+1,nvec)
   WRITE(stdout,'(7X,a)') ' '
   CALL io_push_bar()
   WRITE(stdout, "(5x,'Tot. elapsed time ',a,',  time spent in last iteration ',a) ") &
   TRIM(human_readable_time(time(2))), TRIM(human_readable_time(time(2)-time(1)))
   CALL io_push_bar()
   !
   IF( mpime == root ) THEN 
      !
      CALL json%initialize()
      !
      CALL json%load_file(filename=TRIM(logfile))
      !
      WRITE(cq,'(i5)') iq
      !
      CALL json%get('exec.ndav.Q'//TRIM((ADJUSTL(cq))), ndav(iq), found )
      !
      IF( found ) THEN 
         ndav(iq) = ndav(iq)+1
      ELSE 
         ndav(iq) = 1
      ENDIF
      !
      WRITE(cdav,'(i6)') ndav(iq)
      !
      CALL json%update('exec.ndav.Q'//TRIM(ADJUSTL(cq)), ndav(iq), found )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').dav_iter', dav_iter )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').ev',  ev_        ( 1:nvec ) )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').conv', conv_ ( 1:nvec ) )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').notcnv', notcnv )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').time_elap:sec', time(2) )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').time_elap:hum',&
        & TRIM(human_readable_time(time(2))) )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').time_iter:sec',&
        & (time(2)-time(1)))
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').time_iter:hum',&
        & TRIM(human_readable_time(time(2)-time(1))) )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').sternop_ncalls',&
        & sternop(2)-sternop(1) )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').dfpt_tr2', tr2 )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').dfpt_dim', dfpt_dim )
      CALL json%add('exec.davitr.Q'//TRIM(ADJUSTL(cq))//'('//TRIM(ADJUSTL(cdav))//').diago_dim', diago_dim )
      !
      OPEN( NEWUNIT=iunit,FILE=TRIM(logfile) )
      CALL json%print_file( iunit )
      CLOSE( iunit )
      !
      CALL json%destroy()
      !
   ENDIF 
   !
END SUBROUTINE