-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathadd_pw2qmcpack_to_qe-6.4.1.diff
2410 lines (2401 loc) · 80 KB
/
add_pw2qmcpack_to_qe-6.4.1.diff
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
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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
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
220
221
222
223
224
225
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
330
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
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
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
535
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
632
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
676
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
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
804
805
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
845
846
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
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
diff --git a/PP/src/Makefile b/PP/src/Makefile
index 7a28254..d023918 100644
--- a/PP/src/Makefile
+++ b/PP/src/Makefile
@@ -62,7 +62,7 @@ all : tldeps open_grid.x average.x bands.x dos.x epsilon.x initial_state.x fs.x
plan_avg.x plotband.x plotproj.x plotrho.x pmw.x pp.x projwfc.x \
pawplot.x sumpdos.x pw2wannier90.x pw2critic.x pw2gw.x \
wannier_ham.x wannier_plot.x molecularpdos.x \
- pw2bgw.x wfck2r.x fermi_velocity.x fermi_proj.x ppacf.x
+ pw2bgw.x wfck2r.x fermi_velocity.x fermi_proj.x ppacf.x pw2qmcpack.x
libpp.a : $(PPOBJS)
@@ -204,6 +204,11 @@ fs.x : fermisurface.o libpp.a $(MODULES) $(LIBOBJS)
fermisurface.o libpp.a $(MODULES) $(LIBOBJS) $(QELIBS)
- ( cd ../../bin ; ln -fs ../PP/src/$@ . )
+pw2qmcpack.x : pw2qmcpack.o libpp.a $(MODULES) $(LIBOBJS)
+ $(LD) $(LDFLAGS) -o $@ \
+ pw2qmcpack.o libpp.a $(MODULES) $(LIBOBJS) $(QELIBS)
+ - ( cd ../../bin ; ln -fs ../PP/src/$@ . )
+
tldeps :
if test -n "$(TLDEPS)" ; then \
( cd ../.. ; $(MAKE) $(TLDEPS) || exit 1 ) ; fi
diff --git a/PP/src/pw2qmcpack.f90 b/PP/src/pw2qmcpack.f90
new file mode 100644
index 0000000..5633d2a
--- /dev/null
+++ b/PP/src/pw2qmcpack.f90
@@ -0,0 +1,1340 @@
+!
+! Copyright (C) 2004 PWSCF group
+! 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 .
+!
+!-----------------------------------------------------------------------
+PROGRAM pw2qmcpack
+ !-----------------------------------------------------------------------
+
+ ! This subroutine writes the file "prefix".pwscf.xml and "prefix".pwscf.h5
+ ! containing the plane wave coefficients and other stuff needed by QMCPACK.
+
+ USE io_files, ONLY : nd_nmbr, prefix, tmp_dir
+ USE io_global, ONLY : stdout, ionode, ionode_id
+ USE mp, ONLY : mp_bcast
+ USE mp_global, ONLY : mp_startup, npool, nimage, nproc_pool, nproc_file, nproc_pool_file
+ USE control_flags, ONLY : gamma_only
+ USE mp_world, ONLY : world_comm, nproc
+ USE environment,ONLY : environment_start, environment_end
+ USE KINDS, ONLY : DP
+ !
+ IMPLICIT NONE
+ INTEGER :: ios
+ LOGICAL :: write_psir, expand_kp, debug
+ REAL(DP) :: t1, t2, dt
+ ! directory for temporary files
+ CHARACTER(len=256) :: outdir
+ !
+ CHARACTER(LEN=256), EXTERNAL :: trimcheck
+
+ NAMELIST / inputpp / prefix, outdir, write_psir, expand_kp, debug
+#ifdef __MPI
+ CALL mp_startup ( )
+#endif
+
+ CALL environment_start ( 'pw2qmcpack' )
+#if defined(__HDF5) || defined(__HDF5_C)
+ IF ( nimage > 1) &
+ CALL errore('pw2qmcpack', ' image parallelization not (yet) implemented', 1)
+
+ ! CALL start_postproc(nd_nmbr)
+ !
+ ! set default values for variables in namelist
+ !
+ prefix = 'pwscf'
+ write_psir = .false.
+ expand_kp = .false.
+ debug = .false.
+ CALL get_environment_variable( 'ESPRESSO_TMPDIR', outdir )
+ IF ( TRIM( outdir ) == ' ' ) outdir = './'
+ ios = 0
+ IF ( ionode ) THEN
+ !
+ CALL input_from_file ( )
+ !READ (5, inputpp, err=200, iostat=ios)
+ READ (5, inputpp, iostat=ios)
+ tmp_dir = trimcheck (outdir)
+ !
+ END IF
+ CALL mp_bcast( ios, ionode_id, world_comm )
+ IF ( ios/=0 ) CALL errore('pw2qmcpack', 'reading inputpp namelist', ABS(ios))
+ !
+ ! ... Broadcast variables
+ !
+ CALL mp_bcast(prefix, ionode_id, world_comm )
+ CALL mp_bcast(tmp_dir, ionode_id, world_comm )
+ CALL mp_bcast(write_psir, ionode_id, world_comm )
+ CALL mp_bcast(expand_kp, ionode_id, world_comm )
+ CALL mp_bcast(debug, ionode_id, world_comm )
+ !
+ CALL start_clock ( 'read_file' )
+ CALL read_file
+ CALL stop_clock ( 'read_file' )
+ IF ( gamma_only ) &
+ CALL errore('pw2qmcpack', 'Using gamma trick results a reduced G space that is not supported by QMCPACK &
+ & though pw2qmcpack itself still can convert the WF to an h5 file in this case (experts only). &
+ & Please run pw.x with k point 0.0 0.0 0.0 instead of gamma.',1)
+ !
+ CALL openfil_pp
+ !
+ CALL start_clock ( 'compute_qmcpack' )
+ CALL compute_qmcpack(write_psir, expand_kp, debug)
+ CALL stop_clock ( 'compute_qmcpack' )
+ !
+ IF ( ionode ) THEN
+ WRITE( 6, * )
+ !
+ CALL print_clock( 'read_file_lite' )
+ CALL print_clock( 'compute_qmcpack' )
+ !
+ WRITE( 6, '(/5x,"Called by read_file_lite:")' )
+ CALL print_clock ( 'read_pseudo' )
+ CALL print_clock ( 'read_rho' )
+ CALL print_clock ( 'fft_rho' )
+ CALL print_clock ( 'read_wave' )
+ !
+ WRITE( 6, '(/5x,"Called by compute_qmcpack:")' )
+ CALL print_clock ( 'big_loop' )
+ CALL print_clock ( 'write_h5' )
+ CALL print_clock ( 'glue_h5' )
+ ENDIF
+#else
+#error HDF5 flag neither enabled during configure nor added manually in make.inc
+ CALL errore('pw2qmcpack', ' HDF5 flag not enabled during configure',1)
+#endif
+ CALL environment_end ( 'pw2qmcpack' )
+ CALL stop_pp
+ STOP
+
+
+
+END PROGRAM pw2qmcpack
+
+SUBROUTINE check_norm(ng, eigvec, collect_intra_pool, jks, ibnd, tag)
+ USE mp_pools, ONLY: me_pool, intra_pool_comm
+ USE mp, ONLY: mp_sum
+ USE KINDS, ONLY: DP
+ !
+ IMPLICIT NONE
+ !
+ COMPLEX(DP), INTENT(IN) :: eigvec(ng)
+ INTEGER, INTENT(IN) :: ng, jks, ibnd
+ LOGICAL, INTENT(IN) :: collect_intra_pool
+ CHARACTER(len=*), INTENT(IN) :: tag
+ !
+ INTEGER :: ig
+ REAL(DP) :: total_norm
+ !
+ ! check normalization before writing h5
+ total_norm = 0.d0
+ !$omp parallel do reduction(+:total_norm)
+ DO ig=1, ng
+ total_norm = total_norm + eigvec(ig)*CONJG(eigvec(ig))
+ ENDDO
+ ! collect within a pool
+ IF(collect_intra_pool) CALL mp_sum ( total_norm , intra_pool_comm )
+ ! compute the norm
+ total_norm = SQRT(total_norm)
+ ! check abort if necessary
+ IF (me_pool==0 .AND. ABS(total_norm-1.d0)>1.d-6) THEN
+ WRITE(*,"(A,I3,A,I3,3A,F20.15)") "The wrong norm of k-point ", jks, " band ", ibnd, " , ", &
+ tag, ", is ", total_norm
+ IF (.NOT. collect_intra_pool) THEN
+ WRITE(*,"(3A)") "The orbitals went wrong before being written to h5 file. ", &
+ "Please first add debug=.true. in the pw2qmcpack input file to check ", &
+ "if the orbitals can be read from QE files correctly."
+ ELSE
+ WRITE(*,"(2A)") "The orbitals read from QE files are incorrect. ", &
+ "Perhaps your QE orbital files are corrupted."
+ ENDIF
+ STOP
+ ENDIF
+ !
+END SUBROUTINE
+
+SUBROUTINE compute_qmcpack(write_psir, expand_kp, debug)
+
+ USE kinds, ONLY: DP
+ USE ions_base, ONLY : nat, ntyp => nsp, ityp, tau, zv, atm
+ USE cell_base, ONLY: omega, alat, tpiba2, at, bg
+ USE constants, ONLY: tpi
+ USE run_info, ONLY: title
+ USE gvect, ONLY: ngm, ngm_g, g, ig_l2g
+ USE klist , ONLY: nks, nelec, nelup, neldw, wk, xk, nkstot
+ USE lsda_mod, ONLY: lsda, nspin, isk
+ USE scf, ONLY: rho, rho_core, rhog_core, vnew
+ USE wvfct, ONLY: npw, npwx, nbnd, g2kin, wg, et
+ USE klist, ONLY: igk_k
+ USE gvecw, ONLY : ecutwfc
+ USE control_flags, ONLY: gamma_only
+ USE becmod, ONLY: becp, calbec, allocate_bec_type, deallocate_bec_type
+ USE io_global, ONLY: stdout, ionode, ionode_id
+ USE mp_world, ONLY: world_comm, mpime
+ USE io_files, ONLY: nd_nmbr, nwordwfc, iunwfc, iun => iunsat, tmp_dir, prefix
+ USE wavefunctions, ONLY : evc, psic
+ use iotk_module
+ use iotk_xtox_interf
+ USE mp_global, ONLY: inter_pool_comm, intra_pool_comm, nproc_pool, kunit
+ USE mp_global, ONLY: npool, my_pool_id, intra_image_comm
+ USE mp_pools, ONLY: me_pool
+ USE mp, ONLY: mp_sum, mp_max, mp_bcast, mp_barrier
+ use scatter_mod, ONLY : gather_grid, scatter_grid
+ use fft_base, ONLY : dffts
+ use fft_interfaces, ONLY : invfft, fwfft
+ USE dfunct, ONLY : newd
+ USE symm_base, ONLY : nsym, s, ftau
+
+ IMPLICIT NONE
+ LOGICAL :: write_psir, expand_kp, debug
+ LOGICAL :: pool_ionode
+ INTEGER :: ig, ibnd, ik, io, na, j, ispin, nbndup, nbnddown, &
+ nk, ngtot, ig7, ikk, iks, kpcnt, jks, nt, ijkb0, ikb, ih, jh, jkb, at_num, &
+ nelec_tot, nelec_up, nelec_down, ii, igx, igy, igz, n_rgrid(3), &
+ nkqs, nr1s,nr2s,nr3s
+ INTEGER, ALLOCATABLE :: indx(:), igtog(:), igtomin(:), g_global_to_local(:)
+ LOGICAL :: exst, found
+ REAL(DP) :: ek, eloc, enl, charge, etotefield
+ REAL(DP) :: bg_qmc(3,3), g_red(3), lattice_real(3,3)
+ COMPLEX(DP), ALLOCATABLE :: phase(:),eigpacked(:)
+ COMPLEX(DP), ALLOCATABLE :: psitr(:)
+ REAL(DP), ALLOCATABLE :: tau_r(:,:),psireal(:),eigval(:) !,g_cart(:,:)
+ INTEGER :: ios, ierr, h5len,oldh5,ig_c,save_complex, nup,ndown
+ INTEGER, EXTERNAL :: atomic_number, is_complex
+ !REAL(DP), ALLOCATABLE :: g_qmc(:,:)
+ INTEGER, ALLOCATABLE :: gint_den(:,:), gint_qmc(:,:)
+ COMPLEX(DP), ALLOCATABLE :: den_g_global(:,:)
+ REAL (DP), EXTERNAL :: ewald
+ COMPLEX(DP), ALLOCATABLE, TARGET :: tmp_psic(:)
+ COMPLEX(DP), DIMENSION(:), POINTER :: psiCptr
+ REAL(DP), DIMENSION(:), POINTER :: psiRptr
+! **********************************************************************
+ INTEGER :: npw_sym
+ INTEGER, ALLOCATABLE, TARGET :: igk_sym(:)
+ REAL(DP), ALLOCATABLE :: g2kin_sym(:)
+! **********************************************************************
+ INTEGER :: nkfull,max_nk,max_sym,isym,nxxs
+ INTEGER , ALLOCATABLE :: num_irrep(:)
+ INTEGER, ALLOCATABLE :: xkfull_index(:,:) ! maps to sym_list and xk_full_list
+ INTEGER, ALLOCATABLE :: sym_list(:)
+ REAL(DP), ALLOCATABLE :: xk_full_list(:,:)
+ REAL(DP) :: t1, t2, dt
+ integer, allocatable :: rir(:)
+ COMPLEX(DP), ALLOCATABLE :: tmp_evc(:)
+
+ CHARACTER(256) :: tmp,h5name,eigname,tmp_combo
+ CHARACTER(iotk_attlenx) :: attr
+
+ INTEGER :: rest, nbase, basekindex, nktot
+ REAL(DP) :: xk_cryst(3)
+
+ INTEGER :: npwx_tot, igk_g;
+
+ NULLIFY(psiRptr)
+ NULLIFY(psiCptr)
+
+ ! Ye Luo
+ ! define the pool level ionode
+ ! an image ionode must be pool ionode
+ if(me_pool==0) then
+ pool_ionode=.true.
+ else
+ pool_ionode=.false.
+ endif
+
+ ! MAMorales:
+ ! removed USPP functions
+
+ ! Ye Luo:
+ ! sum up npwx to npwx_tot inside a pool and maximize it among pools.
+ npwx_tot = npwx
+ CALL mp_sum ( npwx_tot, intra_pool_comm )
+ CALL mp_max ( npwx_tot, inter_pool_comm )
+ !write(*,*) mpime, ionode_id, npwx_tot, npw
+
+ ! this limits independent definition of ecutrho to < 4*ecutwf
+ ! four times npwx should be enough
+ ALLOCATE (indx (4*npwx_tot) )
+ ALLOCATE (igtog (4*npwx_tot) )
+ ALLOCATE (g_global_to_local(ngm_g) )
+ ALLOCATE (igtomin(4*npwx_tot) )
+ ALLOCATE (tmp_evc(npwx_tot) )
+
+ indx(:) = 0
+ igtog(:) = 0
+ igtomin(:) = 0
+
+ rest = ( nkstot - kunit * ( nkstot / kunit / npool ) * npool ) / kunit
+ nbase = nks * my_pool_id
+ IF ( ( my_pool_id + 1 ) > rest ) nbase = nbase + rest * kunit
+ !write(*,*) "debug",mpime, nks, nbase
+
+ IF( lsda ) THEN
+! IF( expand_kp ) &
+! CALL errore ('pw2qmcpack','expand_kp not implemented with nspin>1`', 1)
+ nbndup = nbnd
+ nbnddown = nbnd
+ nk = nks/2
+ nktot = nkstot/2
+ ! nspin = 2
+ ELSE
+ nbndup = nbnd
+ nbnddown = 0
+ nk = nks
+ nktot = nkstot
+ ! nspin = 1
+ ENDIF
+
+! ! sanity check for lsda logic to follow
+! if (ionode) then
+! DO ik = 1, nktot
+! iks=ik+nktot
+! xk_cryst(:) = at(1,:)*xk(1,ik) + at(2,:)*xk(2,ik) + at(3,:)*xk(3,ik) - ( at(1,:)*xk(1,iks) + at(2,:)*xk(2,iks) + at(3,:)*xk(3,iks))
+! if (abs(xk_cryst(1))+abs(xk_cryst(2))+abs(xk_cryst(3)) .gt. 1e-12) then
+! print *,"not paired %i %i",ik,iks
+! endif
+! ENDDO
+! endif
+
+
+ !
+
+ ! for now, I'm assuming that symmetry rotations do not affect npw,
+ ! meaning that rotations don't displace elements outside the cutoff
+ nr1s = dffts%nr1
+ nr2s = dffts%nr2
+ nr3s = dffts%nr3
+ nxxs = dffts%nr1x * dffts%nr2x * dffts%nr3x
+ allocate (igk_sym( npwx ), g2kin_sym ( npwx ) )
+
+ if (ionode) then
+ if(expand_kp) then
+ max_sym = min(48, 2 * nsym)
+ max_nk = nktot * max_sym
+ ALLOCATE(num_irrep(nktot),xkfull_index(nktot,max_sym),sym_list(max_nk))
+ ALLOCATE(xk_full_list(3,max_nk))
+ ALLOCATE(rir(nxxs))
+ call generate_symmetry_equivalent_list()
+ if(ionode) print *,'Total number of k-points after expansion:',nkfull
+ else
+ ALLOCATE(num_irrep(nktot),xkfull_index(nktot,1),sym_list(nktot))
+ ALLOCATE(xk_full_list(3,nktot))
+ nkfull = nktot
+ do ik = 1, nktot
+ xk_full_list(:,ik) = xk(:,ik)
+ num_irrep(ik) = 1
+ sym_list(ik) = 1
+ xkfull_index(ik,1) = ik
+ enddo
+ endif
+ else
+ if(expand_kp) then
+ max_sym = min(48, 2 * nsym)
+ max_nk = nktot * max_sym
+ ALLOCATE(num_irrep(nktot),xkfull_index(nktot,max_sym),sym_list(max_nk))
+ ALLOCATE(xk_full_list(3,max_nk))
+ ALLOCATE(rir(nxxs))
+ else
+ ALLOCATE(num_irrep(nktot),xkfull_index(nktot,1),sym_list(nktot))
+ ALLOCATE(xk_full_list(3,nktot))
+ nkfull = nktot
+ endif
+ endif
+
+ CALL mp_bcast(xkfull_index, ionode_id, world_comm )
+ CALL mp_bcast(xk_full_list, ionode_id, world_comm )
+ CALL mp_bcast(sym_list, ionode_id, world_comm )
+ CALL mp_bcast(num_irrep, ionode_id, world_comm )
+ CALL mp_bcast(nkfull, ionode_id, world_comm )
+
+ ! IF ( nbase > 0 ) THEN
+ ! num_irrep(1:nks) = num_irrep(nbase+1:nbase+nks)
+ ! xk_full_list(:,1:nks) = xk_full_list(:,nbase+1:nbase+nks)
+ ! END IF
+
+ DO ik = 1, nks
+ basekindex = ik + nbase
+ CALL gk_sort (xk (1, basekindex), ngm, g, ecutwfc / tpiba2, npw, igk_k(:,ik), g2kin)
+
+ DO ig =1, npw
+ ! mapping to the global g vectors.
+ igk_g = ig_l2g(igk_k(ig,ik))
+ IF( igk_g > 4*npwx_tot ) &
+ CALL errore ('pw2qmcpack','increase allocation of index', ig)
+ indx( igk_g ) = 1
+ ENDDO
+ ENDDO
+ call mp_max(indx, world_comm)
+
+ ngtot = 0
+ ! igtomin maps indices from the full set of G-vectors to the
+ ! minimal set which includes the G-spheres of all k-points
+ DO ig = 1, 4*npwx_tot
+ IF( indx(ig) > 0 ) THEN
+ ngtot = ngtot + 1
+ igtog(ngtot) = ig
+ igtomin(ig) = ngtot
+ ENDIF
+ ENDDO
+
+ if (debug) then
+ write(6,"(A)") " pw2qmcpack found"
+ write(6,"(A,A8,2A8,4A10,1A4)") " MPI rank", "pool id", "npwx", "npw", "npwx_tot", "ngtot", "ngm", "ngm_g", "nks"
+ write(*,"(A,I9,I8,2I8,4I10,1I4)") " ", mpime, me_pool, npwx, npw, npwx_tot, ngtot, ngm, ngm_g, nks
+ endif
+
+ ALLOCATE (gint_qmc(3,ngtot))
+ ALLOCATE (gint_den(3,ngm_g))
+ ALLOCATE (den_g_global(ngm_g,nspin))
+ !ALLOCATE (g_qmc(3,ngtot))
+ !ALLOCATE (g_cart(3,ngtot))
+ ALLOCATE (tau_r(3,nat))
+
+ ! get the number of electrons
+ nelec_tot= NINT(nelec)
+ nup=NINT(nelup)
+ ndown=NINT(neldw)
+
+ if(nup .eq. 0) then
+ ndown=nelec_tot/2
+ nup=nelec_tot-ndown
+ endif
+
+ bg_qmc(:,:)=bg(:,:)/alat
+
+ if((npool>1) .and. (my_pool_id>0)) then
+ h5name = TRIM( prefix ) // '.pwscf.h5' // "_part"//trim(iotk_itoa(my_pool_id))
+ else
+ h5name = TRIM( prefix ) // '.pwscf.h5'
+ endif
+ eigname = "eigenstates_"//trim(iotk_itoa(nr1s))//'_'//trim(iotk_itoa(nr2s))//'_'//trim(iotk_itoa(nr3s))
+
+ tmp = TRIM( tmp_dir )//TRIM( h5name )
+ h5len = LEN_TRIM(tmp)
+
+#if defined(__HDF5) || defined(__HDF5_C)
+ ! writing to xml and hdf5
+ ! open hdf5 file
+ oldh5=0
+ if(pool_ionode) CALL esh5_open_file(tmp,h5len,oldh5)
+
+
+ if(ionode) then
+ !! create a file for particle set
+ tmp = TRIM( tmp_dir ) // TRIM( prefix )// '.ptcl.xml'
+ CALL iotk_open_write(iun, FILE=TRIM(tmp), ROOT="qmcsystem", IERR=ierr )
+
+ CALL iotk_write_attr (attr,"name","global",first=.true.)
+ CALL iotk_write_begin(iun, "simulationcell",ATTR=attr)
+ CALL iotk_write_attr (attr,"name","lattice",first=.true.)
+ CALL iotk_write_attr (attr,"units","bohr")
+ CALL iotk_write_begin(iun, "parameter",ATTR=attr)
+
+ lattice_real=alat*at
+ WRITE(iun,100) lattice_real(1,1), lattice_real(2,1), lattice_real(3,1)
+ WRITE(iun,100) lattice_real(1,2), lattice_real(2,2), lattice_real(3,2)
+ WRITE(iun,100) lattice_real(1,3), lattice_real(2,3), lattice_real(3,3)
+
+ CALL esh5_write_supercell(lattice_real)
+
+ CALL iotk_write_end(iun, "parameter")
+ CALL iotk_write_attr (attr,"name","reciprocal",first=.true.)
+ CALL iotk_write_attr (attr,"units","2pi/bohr")
+ CALL iotk_write_begin(iun, "parameter",ATTR=attr)
+ WRITE(iun,100) bg_qmc(1,1), bg_qmc(2,1), bg_qmc(3,1)
+ WRITE(iun,100) bg_qmc(1,2), bg_qmc(2,2), bg_qmc(3,2)
+ WRITE(iun,100) bg_qmc(1,3), bg_qmc(2,3), bg_qmc(3,3)
+ CALL iotk_write_end(iun, "parameter")
+
+ CALL iotk_write_attr (attr,"name","bconds",first=.true.)
+ CALL iotk_write_begin(iun, "parameter",ATTR=attr)
+ WRITE(iun,'(a)') 'p p p'
+ CALL iotk_write_end(iun, "parameter")
+
+ CALL iotk_write_attr (attr,"name","LR_dim_cutoff",first=.true.)
+ CALL iotk_write_begin(iun, "parameter",ATTR=attr)
+ WRITE(iun,'(a)') '15'
+ CALL iotk_write_end(iun, "parameter")
+ CALL iotk_write_end(iun, "simulationcell")
+
+ ! <particleset name="ions">
+ CALL iotk_write_attr (attr,"name","ion0",first=.true.)
+ CALL iotk_write_attr (attr,"size",nat)
+ CALL iotk_write_begin(iun, "particleset",ATTR=attr)
+
+ CALL esh5_open_atoms(nat,ntyp)
+
+ ! ionic species --> group
+ DO na=1,ntyp
+
+ tmp=TRIM(atm(na))
+ h5len=LEN_TRIM(tmp)
+ CALL esh5_write_species(na,tmp,h5len,atomic_number(tmp),zv(na))
+
+ CALL iotk_write_attr (attr,"name",TRIM(atm(na)),first=.true.)
+ CALL iotk_write_begin(iun, "group",ATTR=attr)
+ ! charge
+ CALL iotk_write_attr (attr,"name","charge",first=.true.)
+ CALL iotk_write_begin(iun, "parameter",ATTR=attr)
+ write(iun,"(8X, F3.1)") zv(na)
+ CALL iotk_write_end(iun, "parameter")
+ ! atomic number
+ CALL iotk_write_attr (attr,"name","atomicnumber",first=.true.)
+ CALL iotk_write_begin(iun, "parameter",ATTR=attr)
+ write(iun,"(8X, I3)") atomic_number(TRIM(atm(na)))
+ CALL iotk_write_end(iun, "parameter")
+ !
+ CALL iotk_write_end(iun, "group")
+ ENDDO
+
+
+ ! <attrib name="ionid"/>
+ CALL iotk_write_attr (attr,"name","ionid",first=.true.)
+ CALL iotk_write_attr (attr,"datatype","stringArray")
+ CALL iotk_write_begin(iun, "attrib",ATTR=attr)
+ write(iun,'(a)') (TRIM(atm(ityp(na))),na=1,nat)
+ CALL iotk_write_end(iun, "attrib")
+
+ ! <attrib name="position"/>
+ CALL iotk_write_attr (attr,"name","position",first=.true.)
+ CALL iotk_write_attr (attr,"datatype","posArray")
+ CALL iotk_write_attr (attr,"condition","0")
+ CALL iotk_write_begin(iun, "attrib",ATTR=attr)
+ ! write in cartesian coordinates in bohr
+ ! problem with xyz ordering inrelation to real-space grid
+ DO na = 1, nat
+ tau_r(1,na)=alat*tau(1,na)
+ tau_r(2,na)=alat*tau(2,na)
+ tau_r(3,na)=alat*tau(3,na)
+ WRITE(iun,100) (tau_r(j,na),j=1,3)
+ ENDDO
+ !write(iun,100) tau
+ CALL iotk_write_end(iun, "attrib")
+ CALL iotk_write_end(iun, "particleset")
+
+ !cartesian positions
+ CALL esh5_write_positions(tau_r)
+ CALL esh5_write_species_ids(ityp)
+
+ CALL esh5_close_atoms()
+ ! </particleset>
+
+ ! <particleset name="e">
+ CALL iotk_write_attr (attr,"name","e",first=.true.)
+ CALL iotk_write_attr (attr,"random","yes")
+ CALL iotk_write_attr (attr,"random_source","ion0")
+ CALL iotk_write_begin(iun, "particleset",ATTR=attr)
+
+ ! <group name="u" size="" >
+ CALL iotk_write_attr (attr,"name","u",first=.true.)
+ CALL iotk_write_attr (attr,"size",nup)
+ CALL iotk_write_begin(iun, "group",ATTR=attr)
+ CALL iotk_write_attr (attr,"name","charge",first=.true.)
+ CALL iotk_write_begin(iun, "parameter",ATTR=attr)
+ write(iun,*) -1
+ CALL iotk_write_end(iun, "parameter")
+ CALL iotk_write_end(iun, "group")
+
+ ! <group name="d" size="" >
+ CALL iotk_write_attr (attr,"name","d",first=.true.)
+ CALL iotk_write_attr (attr,"size",ndown)
+ CALL iotk_write_begin(iun, "group",ATTR=attr)
+ CALL iotk_write_attr (attr,"name","charge",first=.true.)
+ CALL iotk_write_begin(iun, "parameter",ATTR=attr)
+ write(iun,*) -1
+ CALL iotk_write_end(iun, "parameter")
+ CALL iotk_write_end(iun, "group")
+ CALL iotk_write_end(iun, "particleset")
+ CALL iotk_close_write(iun)
+
+ !! close the file
+ !!DO ik = 0, nk-1
+ ik=0
+ ! NOT create a xml input file for each k-point
+ ! IF(nk .gt. 1) THEN
+ ! tmp = TRIM( tmp_dir ) // TRIM( prefix ) //TRIM(iotk_index(ik))// '.wfs.xml'
+ ! ELSE
+ ! tmp = TRIM( tmp_dir ) // TRIM( prefix )// '.wfs.xml'
+ ! ENDIF
+ tmp = TRIM( tmp_dir ) // TRIM( prefix )// '.wfs.xml'
+ CALL iotk_open_write(iun, FILE=TRIM(tmp), ROOT="qmcsystem", IERR=ierr )
+ ! <wavefunction name="psi0">
+ CALL iotk_write_attr (attr,"name","psi0",first=.true.)
+ CALL iotk_write_attr (attr,"target","e")
+ CALL iotk_write_begin(iun, "wavefunction",ATTR=attr)
+ write(iun,'(a)') '<!-- Uncomment this out to use plane-wave basis functions'
+ CALL iotk_write_attr (attr,"type","PW",first=.true.)
+ CALL iotk_write_attr (attr,"href",TRIM(h5name))
+ CALL iotk_write_attr (attr,"version","1.10")
+ CALL iotk_write_begin(iun, "determinantset",ATTR=attr)
+ write(iun,'(a)') '--> '
+ CALL iotk_write_attr (attr,"type","bspline",first=.true.)
+ CALL iotk_write_attr (attr,"href",TRIM(h5name))
+ CALL iotk_write_attr (attr,"sort","1")
+ CALL iotk_write_attr (attr,"tilematrix","1 0 0 0 1 0 0 0 1")
+ CALL iotk_write_attr (attr,"twistnum","0")
+ CALL iotk_write_attr (attr,"source","ion0")
+ CALL iotk_write_attr (attr,"version","0.10")
+ CALL iotk_write_begin(iun, "determinantset",ATTR=attr)
+ CALL iotk_write_attr (attr,"ecut",ecutwfc/2,first=.true.)
+ ! basisset to overwrite cutoff to a smaller value
+ !CALL iotk_write_begin(iun, "basisset",ATTR=attr)
+ ! ! add grid to use spline on FFT grid
+ ! CALL iotk_write_attr (attr,"dir","0",first=.true.)
+ ! CALL iotk_write_attr (attr,"npts",nr1s)
+ ! CALL iotk_write_attr (attr,"closed","no")
+ ! CALL iotk_write_empty(iun, "grid",ATTR=attr)
+ ! CALL iotk_write_attr (attr,"dir","1",first=.true.)
+ ! CALL iotk_write_attr (attr,"npts",nr2s)
+ ! CALL iotk_write_attr (attr,"closed","no")
+ ! CALL iotk_write_empty(iun, "grid",ATTR=attr)
+ ! CALL iotk_write_attr (attr,"dir","2",first=.true.)
+ ! CALL iotk_write_attr (attr,"npts",nr3s)
+ ! CALL iotk_write_attr (attr,"closed","no")
+ ! CALL iotk_write_empty(iun, "grid",ATTR=attr)
+ !CALL iotk_write_end(iun, "basisset")
+
+ !CALL iotk_write_attr (attr,"href",TRIM(h5name),first=.true.)
+ !CALL iotk_write_empty(iun, "coefficients",ATTR=attr)
+
+ ! write the index of the twist angle
+ !!!! remove twistIndex and twistAngle
+ !using determinantset@twistnum
+ !CALL iotk_write_attr (attr,"name","twistIndex",first=.true.)
+ !CALL iotk_write_begin(iun, "h5tag",ATTR=attr)
+ !write(iun,*) ik
+ !CALL iotk_write_end(iun, "h5tag")
+
+ !CALL iotk_write_attr (attr,"name","twistAngle",first=.true.)
+ !CALL iotk_write_begin(iun, "h5tag",ATTR=attr)
+ !g_red(1)=at(1,1)*xk(1,ik+1)+at(2,1)*xk(2,ik+1)+at(3,1)*xk(3,ik+1)
+ !g_red(2)=at(1,2)*xk(1,ik+1)+at(2,2)*xk(2,ik+1)+at(3,2)*xk(3,ik+1)
+ !g_red(3)=at(1,3)*xk(1,ik+1)+at(2,3)*xk(2,ik+1)+at(3,3)*xk(3,ik+1)
+ !!write(iun,100) xk(1,ik+1),xk(2,ik+1),xk(3,ik+1)
+ !write(iun,100) g_red(1),g_red(2),g_red(3)
+ !CALL iotk_write_end(iun, "h5tag")
+ !write(iun,'(a)') '<!-- Uncomment this out for bspline wavefunctions '
+ !!CALL iotk_write_attr (attr,"name","eigenstates",first=.true.)
+ !!CALL iotk_write_begin(iun, "h5tag",ATTR=attr)
+ !!write(iun,'(a)') TRIM(eigname)
+ !!CALL iotk_write_end(iun, "h5tag")
+ !write(iun,'(a)') '--> '
+
+
+ CALL iotk_write_begin(iun, "slaterdeterminant")
+ ! build determinant for up electrons
+ CALL iotk_write_attr (attr,"id","updet",first=.true.)
+ CALL iotk_write_attr (attr,"size",nup)
+ CALL iotk_write_begin(iun, "determinant",ATTR=attr)
+ CALL iotk_write_attr (attr,"mode","ground",first=.true.)
+ CALL iotk_write_attr (attr,"spindataset",0)
+ CALL iotk_write_begin(iun, "occupation",ATTR=attr)
+ CALL iotk_write_end(iun, "occupation")
+ CALL iotk_write_end(iun, "determinant")
+
+ ! build determinant for down electrons
+ CALL iotk_write_attr (attr,"id","downdet",first=.true.)
+ CALL iotk_write_attr (attr,"size",ndown)
+ IF( lsda ) CALL iotk_write_attr (attr,"ref","updet")
+ CALL iotk_write_begin(iun, "determinant",ATTR=attr)
+ CALL iotk_write_attr (attr,"mode","ground",first=.true.)
+ IF( lsda ) THEN
+ CALL iotk_write_attr (attr,"spindataset",1)
+ ELSE
+ CALL iotk_write_attr (attr,"spindataset",0)
+ ENDIF
+ CALL iotk_write_begin(iun, "occupation",ATTR=attr)
+ CALL iotk_write_end(iun, "occupation")
+ CALL iotk_write_end(iun, "determinant")
+ CALL iotk_write_end(iun, "slaterdeterminant")
+
+ CALL iotk_write_end(iun, "determinantset")
+
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ ! two-body jastro
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ CALL iotk_write_attr (attr,"name","J2",first=.true.)
+ CALL iotk_write_attr (attr,"type","Two-Body");
+ CALL iotk_write_attr (attr,"function","Bspline");
+ CALL iotk_write_attr (attr,"print","yes");
+ CALL iotk_write_begin(iun, "jastrow",ATTR=attr)
+
+ ! for uu
+ CALL iotk_write_attr (attr,"speciesA","u",first=.true.)
+ CALL iotk_write_attr (attr,"speciesB","u")
+ !CALL iotk_write_attr (attr,"rcut","10")
+ CALL iotk_write_attr (attr,"size","8")
+ CALL iotk_write_begin(iun, "correlation",ATTR=attr)
+ CALL iotk_write_attr (attr,"id","uu",first=.true.)
+ CALL iotk_write_attr (attr,"type","Array")
+ CALL iotk_write_begin(iun, "coefficients",ATTR=attr)
+ write(iun,*) "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
+ CALL iotk_write_end(iun, "coefficients")
+ CALL iotk_write_end(iun, "correlation")
+
+ ! for ud
+ CALL iotk_write_attr (attr,"speciesA","u",first=.true.)
+ CALL iotk_write_attr (attr,"speciesB","d")
+ !CALL iotk_write_attr (attr,"rcut","10")
+ CALL iotk_write_attr (attr,"size","8")
+ CALL iotk_write_begin(iun, "correlation",ATTR=attr)
+ CALL iotk_write_attr (attr,"id","ud",first=.true.)
+ CALL iotk_write_attr (attr,"type","Array")
+ CALL iotk_write_begin(iun, "coefficients",ATTR=attr)
+ write(iun,*) "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
+ CALL iotk_write_end(iun, "coefficients")
+ CALL iotk_write_end(iun, "correlation")
+
+ CALL iotk_write_end(iun, "jastrow")
+
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ ! one-body jastro
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ CALL iotk_write_attr (attr,"name","J1",first=.true.)
+ CALL iotk_write_attr (attr,"type","One-Body");
+ CALL iotk_write_attr (attr,"function","Bspline");
+ CALL iotk_write_attr (attr,"source","ion0");
+ CALL iotk_write_attr (attr,"print","yes");
+ CALL iotk_write_begin(iun, "jastrow",ATTR=attr)
+
+ DO na=1,ntyp
+ tmp=TRIM(atm(na))
+ tmp_combo='e'//TRIM(atm(na))
+
+ !h5len=LEN_TRIM(tmp)
+ CALL iotk_write_attr (attr,"elementType",TRIM(tmp),first=.true.)
+ !CALL iotk_write_attr (attr,"rcut","10")
+ CALL iotk_write_attr (attr,"size","8")
+ CALL iotk_write_begin(iun, "correlation",ATTR=attr)
+
+ CALL iotk_write_attr (attr,"id",TRIM(tmp_combo),first=.true.)
+ CALL iotk_write_attr (attr,"type","Array")
+ CALL iotk_write_begin(iun, "coefficients",ATTR=attr)
+ write(iun,*) "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
+ CALL iotk_write_end(iun, "coefficients")
+ CALL iotk_write_end(iun, "correlation")
+ ENDDO
+ CALL iotk_write_end(iun, "jastrow")
+
+ CALL iotk_write_end(iun, "wavefunction")
+
+ CALL iotk_close_write(iun)
+ !ENDDO
+
+ endif ! ionode
+
+ g_global_to_local(:)=0
+ DO ig=1,ngm
+ g_global_to_local(ig_l2g(ig))=ig
+ ENDDO
+
+ gint_qmc(:,:) = 0
+ DO ig=1, ngtot
+ ig_c = g_global_to_local(igtog(ig))
+ if (ig_c>0) then
+ !g_cart(1,ig)=tpi/alat*g(1,ig_c)
+ !g_cart(2,ig)=tpi/alat*g(2,ig_c)
+ !g_cart(3,ig)=tpi/alat*g(3,ig_c)
+ !g_qmc(1,ig)=at(1,1)*g(1,ig_c)+at(2,1)*g(2,ig_c)+at(3,1)*g(3,ig_c)
+ !g_qmc(2,ig)=at(1,2)*g(1,ig_c)+at(2,2)*g(2,ig_c)+at(3,2)*g(3,ig_c)
+ !g_qmc(3,ig)=at(1,3)*g(1,ig_c)+at(2,3)*g(2,ig_c)+at(3,3)*g(3,ig_c)
+ gint_qmc(1,ig)=NINT(at(1,1)*g(1,ig_c)+at(2,1)*g(2,ig_c)+at(3,1)*g(3,ig_c))
+ gint_qmc(2,ig)=NINT(at(1,2)*g(1,ig_c)+at(2,2)*g(2,ig_c)+at(3,2)*g(3,ig_c))
+ gint_qmc(3,ig)=NINT(at(1,3)*g(1,ig_c)+at(2,3)*g(2,ig_c)+at(3,3)*g(3,ig_c))
+ !WRITE(io,'(3(1x,f20.15))') g_cart(1,ig),g_cart(2,ig),g_cart(3,ig)
+ endif
+ ENDDO
+ call mp_sum(gint_qmc, intra_pool_comm)
+
+ gint_den(:,:) = 0
+ DO ig=1,ngm
+ gint_den(1,ig_l2g(ig))=NINT(at(1,1)*g(1,ig)+at(2,1)*g(2,ig)+at(3,1)*g(3,ig))
+ gint_den(2,ig_l2g(ig))=NINT(at(1,2)*g(1,ig)+at(2,2)*g(2,ig)+at(3,2)*g(3,ig))
+ gint_den(3,ig_l2g(ig))=NINT(at(1,3)*g(1,ig)+at(2,3)*g(2,ig)+at(3,3)*g(3,ig))
+ ENDDO
+ DO ispin=1,nspin
+ den_g_global(:,ispin) = 0.d0
+ DO ig=1,ngm
+ den_g_global(ig_l2g(ig),ispin) = rho%of_g(ig,ispin)
+ ENDDO
+ ENDDO
+
+ call mp_sum(gint_den, intra_pool_comm)
+ call mp_sum(den_g_global, intra_pool_comm)
+
+ n_rgrid(1)=nr1s
+ n_rgrid(2)=nr2s
+ n_rgrid(3)=nr3s
+
+ save_complex=0
+ if(ionode) then
+ DO ik = 1, nktot
+ !! evaluate the phase
+ !phase(:) = (0.d0,0.d0)
+ !if ( ig_(ik,ib)>0) phase( dffts%nl(ig_(ik,ib)) ) = (1.d0,0.d0)
+ g_red(1)=at(1,1)*xk_full_list(1,ik)+at(2,1)*xk_full_list(2,ik)+at(3,1)*xk_full_list(3,ik)
+ g_red(2)=at(1,2)*xk_full_list(1,ik)+at(2,2)*xk_full_list(2,ik)+at(3,2)*xk_full_list(3,ik)
+ g_red(3)=at(1,3)*xk_full_list(1,ik)+at(2,3)*xk_full_list(2,ik)+at(3,3)*xk_full_list(3,ik)
+
+ IF(g_red(1)*g_red(1)+g_red(2)*g_red(2)+g_red(3)*g_red(3)>1e-12) THEN
+ save_complex=1
+ END IF
+ END DO
+ endif
+
+ CALL mp_bcast(save_complex, ionode_id, world_comm )
+
+
+
+! WRITE(io,'(A10,3(1x,i6))') 'ngrid: ',n_rgrid(1:3)
+
+ !CALL esh5_open_electrons(nup, ndown,nspin,nk,nbnd,n_rgrid)!, save_complex)
+ !CALL esh5_open_electrons(nup, ndown, nspin, nkfull, nbnd, n_rgrid)!, save_complex)
+
+ if(pool_ionode) then
+ if(ionode) then
+ CALL esh5_open_electrons_base(nup, ndown, nspin, nkfull, nbnd, n_rgrid)!, save_complex)
+ else
+ CALL esh5_open_electrons(nup, ndown, nspin, nkfull, nbnd, n_rgrid)!, save_complex)
+ endif
+ endif
+
+! IF (write_psir) THEN
+! CALL esh5_write_psi_r_mesh(n_rgrid)
+! ENDIF
+
+ !!NOT YET DECIDED
+ !!CALL esh5_write_basis(g_qmc,g_cart,ngtot)
+ !!CALL esh5_write_parameters(nelec_tot,nspin,nbnd,nkfull,ecutwfc/2,alat,at)
+ !
+
+ ALLOCATE (eigpacked(ngtot))
+ ALLOCATE (eigval(nbnd))
+
+!ionode writes all k-point and ev data
+ if(ionode)then
+ DO ik = 1, nkstot
+ basekindex = ik + nbase
+ ispin = 1
+ if (basekindex > nktot) then
+ ispin = 2
+ basekindex = basekindex - nktot
+ endif
+ DO iks = 1,num_irrep(basekindex)
+ jks = xkfull_index(basekindex,iks)
+ g_red(1)=at(1,1)*xk_full_list(1,jks)+at(2,1)*xk_full_list(2,jks)+at(3,1)*xk_full_list(3,jks)
+ g_red(2)=at(1,2)*xk_full_list(1,jks)+at(2,2)*xk_full_list(2,jks)+at(3,2)*xk_full_list(3,jks)
+ g_red(3)=at(1,3)*xk_full_list(1,jks)+at(2,3)*xk_full_list(2,jks)+at(3,3)*xk_full_list(3,jks)
+
+ CALL esh5_open_kpoint(jks)
+ CALL esh5_write_kpoint_data(g_red,wk(basekindex)/num_irrep(basekindex),ngtot,iks,num_irrep(basekindex))
+
+ ! only the 1 index kpoint will write this g vectors
+ if(ik == 1) then
+ CALL esh5_write_gvectors_k(gint_qmc,ngtot)
+ endif
+
+! if (lsda) then
+! ispin = isk(ik)
+! else
+! ispin=1
+! endif
+
+ CALL esh5_open_spin(ispin)
+ DO ibnd = 1, nbnd
+ eigval(ibnd)=0.5*et(ibnd,ik)
+ ENDDO
+ CALL esh5_write_eigvalues(eigval)
+ CALL esh5_close_spin()
+
+
+ CALL esh5_close_kpoint()
+ ENDDO
+ ENDDO
+ else
+ DO ik = 1, nks
+ basekindex = ik + nbase
+ if (basekindex > nktot) then
+ basekindex = basekindex - nktot
+ ispin=2
+ else
+ ispin=1
+ endif
+ DO iks = 1,num_irrep(basekindex)
+ jks = xkfull_index(basekindex,iks)
+ g_red(1)=at(1,1)*xk_full_list(1,jks)+at(2,1)*xk_full_list(2,jks)+at(3,1)*xk_full_list(3,jks)
+ g_red(2)=at(1,2)*xk_full_list(1,jks)+at(2,2)*xk_full_list(2,jks)+at(3,2)*xk_full_list(3,jks)
+ g_red(3)=at(1,3)*xk_full_list(1,jks)+at(2,3)*xk_full_list(2,jks)+at(3,3)*xk_full_list(3,jks)
+
+ !! open kpoint
+ if(pool_ionode) CALL esh5_open_kpoint(jks)
+! CALL esh5_write_kpoint_data(g_red,wk(ik)/num_irrep(basekindex),ngtot)
+! if (lsda) then
+! ispin = isk(ik)
+! else
+! ispin=1
+! endif
+ if(pool_ionode) CALL esh5_open_spin(ispin)
+ if(pool_ionode) CALL esh5_close_spin()
+
+ if(pool_ionode) CALL esh5_close_kpoint()
+
+ ENDDO
+ ENDDO
+ endif
+
+100 FORMAT (3(1x,f20.15))
+
+ if(save_complex /=1 .and. write_psir) ALLOCATE(psireal(nxxs))
+ if(write_psir .or. expand_kp) then
+ ALLOCATE(psitr(nxxs))
+ IF(nproc_pool > 1) ALLOCATE(tmp_psic(nxxs))
+ endif
+
+! if(ionode) print *,'PW2QMCPACK npw=',npw,'ngtot=',ngtot
+ ! open real-space wavefunction on FFT grid
+ !!CALL esh5_open_eigr(nr1s,nr2s,nr3s)
+ !DO ik = 1, nk
+
+ CALL start_clock ( 'big_loop' )
+ if(nks .eq. 1) then ! treat 1 kpoint specially
+ if(pool_ionode) write(*,"(A,I8,A)") ' k pool ', my_pool_id, ' has only 1 Kpoint. Bypass everything '
+ ik=1
+ basekindex = ik + nbase
+ if (basekindex > nktot) then
+ basekindex = basekindex - nktot
+ ispin=2
+ else
+ ispin=1
+ endif
+ if(debug) write(6,*) " starting davcio!"
+ CALL davcio (evc, 2*nwordwfc, iunwfc, ik, - 1)
+ if(debug) write(6,*) " davcio ends!"
+ if(pool_ionode) CALL esh5_open_kpoint(basekindex)
+ if(pool_ionode) CALL esh5_open_spin(ispin)
+ DO ibnd = 1, nbnd
+ eigpacked(:)=(0.d0,0.d0)
+ eigpacked(igtomin(ig_l2g(igk_k(1:npw,ik))))=evc(1:npw,ibnd)
+ IF (debug) CALL check_norm(npw, evc(1,ibnd), .true., jks, ibnd, "before collection")
+ if (debug) write(6,"(A,1I5)") " collecting band ", ibnd
+ CALL mp_sum ( eigpacked , intra_pool_comm )
+ if (debug) write(6,"(A,1I5)") " writing band ", ibnd
+ if (pool_ionode) THEN
+ CALL check_norm(ngtot, eigpacked, .false., jks, ibnd, "after collection before writing")
+ CALL esh5_write_psi_g(ibnd,eigpacked,ngtot)
+ ENDIF
+ enddo
+ if(pool_ionode) CALL esh5_close_spin()
+ if(pool_ionode) CALL esh5_close_kpoint()
+ else ! nk .neq. 1
+ DO ik = 1, nks
+ basekindex = ik + nbase
+ if (basekindex > nktot) then
+ basekindex = basekindex - nktot
+ ispin=2
+ else
+ ispin=1
+ endif
+ DO iks = 1,num_irrep(basekindex)
+ jks = xkfull_index(basekindex,iks)
+ isym = sym_list(jks)
+
+ if(expand_kp) then
+ call generate_symmetry_rotation(isym)
+ endif
+
+ if(pool_ionode) CALL esh5_open_kpoint(jks)
+
+! if(ionode) print *,'PW2QMCPACK ik,iks=',ik,iks
+
+! DO ispin = 1, nspin
+! ikk = ik + nk*(ispin-1)
+! if (lsda) then
+! ispin = isk(ik)
+! else
+! ispin=1
+! endif
+
+ !!! MAM: This could be outside the num_irrep group is ispin = 1,
+ !!! can I switch the order of esh5_open_spin and
+ !!! esh5_open_kpoint???
+ CALL gk_sort (xk (1:3, ik), ngm, g, ecutwfc / tpiba2, npw, igk_k(:,ik), g2kin)
+ CALL davcio (evc, 2*nwordwfc, iunwfc, ik, - 1)
+ CALL gk_sort (xk_full_list (1:3, jks), ngm, g, ecutwfc / tpiba2, npw_sym, igk_sym, g2kin_sym)
+ if(npw .ne. npw_sym ) then
+ write(*,*) 'Warning!!!: npw != npw_sym: ',npw,npw_sym
+ endif
+