-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_pawrhoij.F90
4605 lines (4198 loc) · 174 KB
/
m_pawrhoij.F90
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
!!****m* ABINIT/m_pawrhoij
!! NAME
!! m_pawrhoij
!!
!! FUNCTION
!! This module contains the definition of the pawrhoij_type structured datatype,
!! as well as related functions and methods.
!! pawrhoij_type variables define rhoij occupancies matrixes used within PAW formalism.
!!
!! COPYRIGHT
!! Copyright (C) 2012-2022 ABINIT group (MT, FJ)
!! This file is distributed under the terms of the
!! GNU General Public License, see ~abinit/COPYING
!! or http://www.gnu.org/copyleft/gpl.txt .
!!
!! NOTES
!! FOR DEVELOPPERS: in order to preserve the portability of libPAW library,
!! please consult ~abinit/src/??_libpaw/libpaw-coding-rules.txt
!!
!! SOURCE
#include "libpaw.h"
MODULE m_pawrhoij
USE_DEFS
USE_MSG_HANDLING
USE_MPI_WRAPPERS
USE_MEMORY_PROFILING
#ifdef LIBPAW_HAVE_NETCDF
use netcdf
#endif
use m_libpaw_tools, only : libpaw_flush, libpaw_to_upper
use m_paw_io, only : pawio_print_ij
use m_pawang, only : pawang_type
use m_pawtab, only : pawtab_type
use m_paral_atom, only : get_my_atmtab, free_my_atmtab, get_my_natom
implicit none
private
!public procedures.
public :: pawrhoij_alloc
public :: pawrhoij_free
public :: pawrhoij_nullify
public :: pawrhoij_copy
public :: pawrhoij_gather
public :: pawrhoij_bcast
public :: pawrhoij_redistribute
public :: pawrhoij_io
public :: pawrhoij_unpack
public :: pawrhoij_init_unpacked
public :: pawrhoij_free_unpacked
public :: pawrhoij_filter
public :: pawrhoij_inquire_dim
public :: pawrhoij_print_rhoij
public :: pawrhoij_symrhoij
public :: pawrhoij_mpisum_unpacked
interface pawrhoij_mpisum_unpacked
module procedure pawrhoij_mpisum_unpacked_1D
module procedure pawrhoij_mpisum_unpacked_2D
end interface pawrhoij_mpisum_unpacked
!private procedures.
private :: pawrhoij_isendreceive_getbuffer
private :: pawrhoij_isendreceive_fillbuffer
!!***
!!****t* m_pawrhoij/pawrhoij_type
!! NAME
!! pawrhoij_type
!!
!! FUNCTION
!! This structured datatype contains rhoij quantities (occucpancies)
!! and related data, used in PAW calculations.
!!
!! SOURCE
type,public :: pawrhoij_type
!Integer scalars
integer :: cplex_rhoij
! cplex_rhoij=1 if rhoij are real
! cplex_rhoij=2 if rhoij are complex (spin-orbit, pawcpxocc=2, ...)
integer :: itypat
! itypat=type of the atom
integer :: lmn_size
! Number of (l,m,n) elements for the paw basis
integer :: lmn2_size
! lmn2_size=lmn_size*(lmn_size+1)/2
! where lmn_size is the number of (l,m,n) elements for the paw basis
integer :: lmnmix_sz=0
! lmnmix_sz=number of (lmn,lmn_prime) verifying l<=lmix and l_prime<=lmix
! i.e. number of rhoij elements being mixed during SCF cycle
! lmnmix_sz=0 if mixing data are not used
integer :: ngrhoij=0
! First dimension of array grhoij
integer :: nrhoijsel=0
! nrhoijsel
! Number of non-zero values of rhoij
! This is the size of rhoijp(:,:) (see below in this datastructure)
integer :: nspden
! Number of spin-density components for rhoij (may be different from nspden for density)
integer :: nspinor
! Number of spinorial components
integer :: nsppol
! Number of independent spin-components
integer :: qphase
! qphase=2 if rhoij contain a exp(-i.q.r) phase (as in the q<>0 RF case), 1 if not
! (this may change the ij symmetry)
integer :: use_rhoij_=0
! 1 if pawrhoij%rhoij_ is allocated
integer :: use_rhoijp=0
! 1 if pawrhoij%rhoijp and pawrhoij%rhoijselect are allocated
integer :: use_rhoijres=0
! 1 if pawrhoij%rhoijres is allocated
!Integer arrays
integer, allocatable :: kpawmix(:)
! kpawmix(lmnmix_sz)
! Indirect array selecting the elements of rhoij
! being mixed during SCF cycle
integer, allocatable :: rhoijselect(:)
! rhoijselect(lmn2_size)
! Indirect array selecting the non-zero elements of rhoij:
! rhoijselect(isel,ispden)=klmn if rhoij(klmn,ispden) is non-zero
!Real (real(dp)) arrays
real(dp), allocatable :: grhoij (:,:,:)
! grhoij(ngrhoij,cplex_rhoij*qphase*lmn2_size,nspden)
! Gradients of Rho_ij wrt xred, strains, ... (non-packed storage)
real(dp), allocatable :: rhoij_ (:,:)
! rhoij_(cplex_rhoij*qphase*lmn2_size,nspden)
! Array used to (temporary) store Rho_ij in a non-packed storage mode
real(dp), allocatable :: rhoijp (:,:)
! rhoijp(cplex_rhoij*qphase*lmn2_size,nspden)
! Augmentation waves occupancies Rho_ij in PACKED STORAGE (only non-zero elements are stored)
real(dp), allocatable :: rhoijres (:,:)
! rhoijres(cplex_rhoij*qphase*lmn2_size,nspden)
! Rho_ij residuals during SCF cycle (non-packed storage)
! ==== Storage for the 1st dimension ====
! For each klmn=ij:
! When RHOij is complex (cplex=2):
! rhoij(2*ij-1,:) contains the real part
! rhoij(2*ij ,:) contains the imaginary part
! When a exp(-i.q.r) phase is included (qphase=2):
! rhoij(1:cplex_dij*lmn2_size,:)
! contains the real part of the phase, i.e. RHO_ij*cos(q.r)
! rhoij(cplex_dij*lmn2_size+1:2*cplex_dij*lmn2_size,:)
! contains the imaginary part of the phase, i.e. RHO_ij*sin(q.r)
! ==== Storage for the 2nd dimension ====
! No magnetism
! rhoij(:,1) contains rhoij
! Collinear magnetism
! rhoij(:,1) contains rhoij^up
! rhoij(:,2) contains rhoij^dowm
! Non-collinear magnetism
! rhoij(:,1) contains rhoij
! rhoij(:,2) contains rhoij magnetization along x
! rhoij(:,3) contains rhoij magnetization along y
! rhoij(:,4) contains rhoij magnetization along z
end type pawrhoij_type
!!***
CONTAINS
!===========================================================
!!***
!----------------------------------------------------------------------
!!****f* m_pawrhoij/pawrhoij_alloc
!! NAME
!! pawrhoij_alloc
!!
!! FUNCTION
!! Initialize and allocate a pawrhoij datastructure
!!
!! INPUTS
!! [comm_atom] = communicator over atoms (OPTIONAL)
!! cplex_rhoij=1 if rhoij are real, 2 if rhoij are complex (spin-orbit, pawcpxocc=2, ...)
!! [my_atmtab(:)] = Index of atoms treated by current proc (OPTIONAL)
!! nspden=number of spin-components for rhoij
!! nsppol=number of spinorial components for rhoij
!! nsppol=number of independant spin-components for rhoij
!! typat(:)=types of atoms
!! [lmnsize(:)]=array of (l,m,n) sizes for rhoij for each type of atom (OPTIONAL)
!! must be present if [pawtab] argument is not passed
!! [ngrhoij]=number of gradients to be allocated (OPTIONAL, default=0)
!! [nlmnmix]=number of rhoij elements to be mixed during SCF cycle (OPTIONAL, default=0)
!! [pawtab(:)] <type(pawtab_type)>=paw tabulated starting data (OPTIONAL)
!! must be present if [lmnsize(:)] argument is not passed
!! [qphase]=2 if the rhoij contain a exp(iqR) phase, 1 otherwise (OPTIONAL, default=1)
!! Typical use: 1st-order rhoij at q<>0
!! [use_rhoij_]=1 if pawrhoij(:)%rhoij_ has to be allocated (OPTIONAL, default=0)
!! [use_rhoijp]=1 if pawrhoij(:)%rhoijp has to be allocated (OPTIONAL, default=1)
!! (in that case, pawrhoij%rhoijselect is also allocated)
!! [use_rhoijres]=1 if pawrhoij(:)%rhoijres has to be allocated (OPTIONAL, default=0)
!!
!! SIDE EFFECTS
!! pawrhoij(:)<type(pawrhoij_type)>= rhoij datastructure
!!
!! NOTES
!! One of the two optional arguments lmnsize(:) or pawtab(:) must be present !
!! If both are present, only pawtab(:) is used.
!!
!! SOURCE
subroutine pawrhoij_alloc(pawrhoij,cplex_rhoij,nspden,nspinor,nsppol,typat,&
& lmnsize,ngrhoij,nlmnmix,pawtab,qphase,use_rhoij_,use_rhoijp,& ! Optional
& use_rhoijres,comm_atom,mpi_atmtab) ! Optional
!Arguments ------------------------------------
!scalars
integer,intent(in) :: cplex_rhoij,nspden,nspinor,nsppol
integer,optional,intent(in):: comm_atom,ngrhoij,nlmnmix,qphase
integer,optional,intent(in):: use_rhoij_,use_rhoijp,use_rhoijres
integer,optional,target,intent(in) :: mpi_atmtab(:)
!arrays
integer,intent(in) :: typat(:)
integer,optional,target,intent(in) :: lmnsize(:)
type(pawrhoij_type),intent(inout) :: pawrhoij(:)
type(pawtab_type),optional,intent(in) :: pawtab(:)
!Local variables-------------------------------
!scalars
integer :: irhoij,irhoij_,itypat,lmn2_size,my_comm_atom,my_qphase, nn1,natom,nrhoij
logical :: has_rhoijp,my_atmtab_allocated,paral_atom
character(len=500) :: msg
!array
integer,pointer :: lmn_size(:),my_atmtab(:)
! *************************************************************************
nrhoij=size(pawrhoij);natom=size(typat)
if (nrhoij>natom) then
msg=' wrong sizes (1) !'
LIBPAW_BUG(msg)
end if
!Select lmn_size for each atom type
if (present(pawtab)) then
nn1=size(pawtab)
if (maxval(typat)>nn1) then
msg=' wrong sizes (2) !'
LIBPAW_BUG(msg)
end if
LIBPAW_POINTER_ALLOCATE(lmn_size,(nn1))
do itypat=1,nn1
lmn_size(itypat)=pawtab(itypat)%lmn_size
end do
else if (present(lmnsize)) then
nn1=size(lmnsize)
if (maxval(typat)>nn1) then
msg=' wrong sizes (3) !'
LIBPAW_BUG(msg)
end if
lmn_size => lmnsize
else
msg=' one of the 2 arguments pawtab or lmnsize must be present !'
LIBPAW_BUG(msg)
end if
!Set up parallelism over atoms
paral_atom=(present(comm_atom).and.(nrhoij/=natom))
nullify(my_atmtab);if (present(mpi_atmtab)) my_atmtab => mpi_atmtab
my_comm_atom=xpaw_mpi_comm_self;if (present(comm_atom)) my_comm_atom=comm_atom
call get_my_atmtab(my_comm_atom,my_atmtab,my_atmtab_allocated,paral_atom,natom)
my_qphase=1;if (present(qphase)) my_qphase=qphase
if (nrhoij>0) then
do irhoij=1,nrhoij
irhoij_=irhoij;if (paral_atom) irhoij_=my_atmtab(irhoij)
itypat=typat(irhoij_)
lmn2_size=lmn_size(itypat)*(lmn_size(itypat)+1)/2
! Scalars initializations
pawrhoij(irhoij)%cplex_rhoij=cplex_rhoij
pawrhoij(irhoij)%qphase=my_qphase
pawrhoij(irhoij)%itypat=itypat
pawrhoij(irhoij)%lmn_size=lmn_size(itypat)
pawrhoij(irhoij)%lmn2_size=lmn2_size
pawrhoij(irhoij)%nspden=nspden
pawrhoij(irhoij)%nspinor=nspinor
pawrhoij(irhoij)%nsppol=nsppol
pawrhoij(irhoij)%nrhoijsel=0
pawrhoij(irhoij)%lmnmix_sz=0
pawrhoij(irhoij)%ngrhoij=0
pawrhoij(irhoij)%use_rhoij_=0
pawrhoij(irhoij)%use_rhoijres=0
! Arrays allocations
has_rhoijp=.true.; if (present(use_rhoijp)) has_rhoijp=(use_rhoijp>0)
if (has_rhoijp) then
pawrhoij(irhoij)%use_rhoijp=1
LIBPAW_ALLOCATE(pawrhoij(irhoij)%rhoijselect,(lmn2_size))
LIBPAW_ALLOCATE(pawrhoij(irhoij)%rhoijp,(cplex_rhoij*my_qphase*lmn2_size,nspden))
pawrhoij(irhoij)%rhoijselect(:)=0
pawrhoij(irhoij)%rhoijp(:,:)=zero
end if
if (present(ngrhoij)) then
if (ngrhoij>0) then
pawrhoij(irhoij)%ngrhoij=ngrhoij
LIBPAW_ALLOCATE(pawrhoij(irhoij)%grhoij,(ngrhoij,cplex_rhoij*my_qphase*lmn2_size,nspden))
pawrhoij(irhoij)%grhoij=zero
end if
end if
if (present(nlmnmix)) then
if (nlmnmix>0) then
pawrhoij(irhoij)%lmnmix_sz=nlmnmix
LIBPAW_ALLOCATE(pawrhoij(irhoij)%kpawmix,(nlmnmix))
pawrhoij(irhoij)%kpawmix=0
end if
end if
if (present(use_rhoij_)) then
if (use_rhoij_>0) then
pawrhoij(irhoij)%use_rhoij_=use_rhoij_
LIBPAW_ALLOCATE(pawrhoij(irhoij)%rhoij_,(cplex_rhoij*my_qphase*lmn2_size,nspden))
pawrhoij(irhoij)%rhoij_=zero
end if
end if
if (present(use_rhoijres)) then
if (use_rhoijres>0) then
pawrhoij(irhoij)%use_rhoijres=use_rhoijres
LIBPAW_ALLOCATE(pawrhoij(irhoij)%rhoijres,(cplex_rhoij*my_qphase*lmn2_size,nspden))
pawrhoij(irhoij)%rhoijres=zero
end if
end if
end do
end if
if (present(pawtab)) then
LIBPAW_POINTER_DEALLOCATE(lmn_size)
end if
!Destroy atom table used for parallelism
call free_my_atmtab(my_atmtab,my_atmtab_allocated)
end subroutine pawrhoij_alloc
!!***
!----------------------------------------------------------------------
!!****f* m_pawrhoij/pawrhoij_free
!! NAME
!! pawrhoij_free
!!
!! FUNCTION
!! Destroy a pawrhoij datastructure
!!
!! SIDE EFFECTS
!! pawrhoij(:)<type(pawrhoij_type)>= rhoij datastructure
!!
!! SOURCE
subroutine pawrhoij_free(pawrhoij)
!Arguments ------------------------------------
!arrays
type(pawrhoij_type),intent(inout) :: pawrhoij(:)
!Local variables-------------------------------
!scalars
integer :: irhoij,nrhoij
! *************************************************************************
nrhoij=size(pawrhoij)
if (nrhoij>0) then
do irhoij=1,nrhoij
pawrhoij(irhoij)%cplex_rhoij=1
pawrhoij(irhoij)%qphase=1
pawrhoij(irhoij)%nrhoijsel=0
pawrhoij(irhoij)%ngrhoij=0
pawrhoij(irhoij)%lmnmix_sz=0
pawrhoij(irhoij)%use_rhoij_=0
pawrhoij(irhoij)%use_rhoijp=0
pawrhoij(irhoij)%use_rhoijres=0
if (allocated(pawrhoij(irhoij)%rhoijp)) then
LIBPAW_DEALLOCATE(pawrhoij(irhoij)%rhoijp)
end if
if (allocated(pawrhoij(irhoij)%rhoijselect)) then
LIBPAW_DEALLOCATE(pawrhoij(irhoij)%rhoijselect)
end if
if (allocated(pawrhoij(irhoij)%grhoij)) then
LIBPAW_DEALLOCATE(pawrhoij(irhoij)%grhoij)
end if
if (allocated(pawrhoij(irhoij)%kpawmix)) then
LIBPAW_DEALLOCATE(pawrhoij(irhoij)%kpawmix)
end if
if (allocated(pawrhoij(irhoij)%rhoij_)) then
LIBPAW_DEALLOCATE(pawrhoij(irhoij)%rhoij_)
end if
if (allocated(pawrhoij(irhoij)%rhoijres)) then
LIBPAW_DEALLOCATE(pawrhoij(irhoij)%rhoijres)
end if
end do
end if
end subroutine pawrhoij_free
!!***
!----------------------------------------------------------------------
!!****f* m_pawrhoij/pawrhoij_nullify
!! NAME
!! pawrhoij_nullify
!!
!! FUNCTION
!! Nullify (initialize to null) a pawrhoij datastructure
!!
!! SIDE EFFECTS
!! pawrhoij(:)<type(pawrhoij_type)>= rhoij datastructure
!!
!! SOURCE
subroutine pawrhoij_nullify(pawrhoij)
!Arguments ------------------------------------
!arrays
type(pawrhoij_type),intent(inout) :: pawrhoij(:)
!Local variables-------------------------------
!scalars
integer :: irhoij,nrhoij
! *************************************************************************
! MGPAW: This one could be removed/renamed,
! variables can be initialized in the datatype declaration
! Do we need to expose this in the public API?
nrhoij=size(pawrhoij)
if (nrhoij>0) then
do irhoij=1,nrhoij
pawrhoij(irhoij)%cplex_rhoij=1
pawrhoij(irhoij)%qphase=1
pawrhoij(irhoij)%nrhoijsel=0
pawrhoij(irhoij)%ngrhoij=0
pawrhoij(irhoij)%lmnmix_sz=0
pawrhoij(irhoij)%use_rhoij_=0
pawrhoij(irhoij)%use_rhoijp=0
pawrhoij(irhoij)%use_rhoijres=0
end do
end if
end subroutine pawrhoij_nullify
!!***
!----------------------------------------------------------------------
!!****f* m_pawrhoij/pawrhoij_copy
!! NAME
!! pawrhoij_copy
!!
!! FUNCTION
!! Copy one pawrhoij datastructure into another
!! Can take into accound changes of dimensions
!! Can copy a shared pawrhoij into distributed ones (when parallelism is activated)
!!
!! INPUTS
!! keep_cplex= optional argument (logical, default=.TRUE.)
!! if .TRUE. pawrhoij_out(:)%cplex_rhoij is NOT MODIFIED,
!! even if different from pawrhoij_in(:)%cplex_rhoij
!! keep_qphase= optional argument (logical, default=.TRUE.)
!! if .TRUE. pawrhoij_out(:)%cplex_rhoij is NOT MODIFIED,
!! even if different from pawrhoij_in(:)%qphase
!! keep_itypat= optional argument (logical, default=.FALSE.)
!! if .TRUE. pawrhoij_out(:)%ityp is NOT MODIFIED,
!! even if different from pawrhoij_in(:)%ityp
!! keep_nspden= optional argument (logical, default=.TRUE.)
!! if .TRUE. pawrhoij_out(:)%nspden is NOT MODIFIED,
!! even if different from pawrhoij_in(:)%nspden
!! mpi_atmtab(:)=--optional-- indexes of the atoms treated by current proc
!! comm_atom=--optional-- MPI communicator over atoms
!! pawrhoij_in(:)<type(pawrhoij_type)>= input rhoij datastructure
!!
!! SIDE EFFECTS
!! pawrhoij_out(:)<type(pawrhoij_type)>= output rhoij datastructure
!!
!! NOTES
!! In case of a single copy operation pawrhoij_out must have been allocated.
!!
!! SOURCE
subroutine pawrhoij_copy(pawrhoij_in,pawrhoij_cpy, &
& keep_cplex,keep_qphase,keep_itypat,keep_nspden,& ! optional arguments
& mpi_atmtab,comm_atom) ! optional arguments (parallelism)
!Arguments ------------------------------------
!scalars
integer,optional,intent(in) :: comm_atom
logical,intent(in),optional :: keep_cplex,keep_qphase,keep_itypat,keep_nspden
!arrays
integer,optional,target,intent(in) :: mpi_atmtab(:)
type(pawrhoij_type),intent(in) :: pawrhoij_in(:)
type(pawrhoij_type),intent(inout),target :: pawrhoij_cpy(:)
!Local variables-------------------------------
!scalars
integer :: cplex,cplex_in,cplex_out,i_in,i_out,ilmn,iphase
integer :: irhoij,ispden,jrhoij,lmn2_size_in,lmn2_size_out,lmnmix,my_comm_atom,my_nrhoij
integer :: ngrhoij,nrhoij_in,nrhoij_max,nrhoij_out,nselect,nselect_out
integer :: nspden_in,nspden_out,paral_case,qphase,qphase_in,qphase_out
integer :: use_rhoij_,use_rhoijp,use_rhoijres
logical :: change_dim,keep_cplex_,keep_qphase_,keep_itypat_,keep_nspden_,my_atmtab_allocated,paral_atom
character(len=500) :: msg
!arrays
integer,pointer :: my_atmtab(:)
integer,allocatable :: nlmn(:),typat(:)
type(pawrhoij_type),pointer :: pawrhoij_out(:)
! *************************************************************************
!Retrieve sizes
nrhoij_in=size(pawrhoij_in);nrhoij_out=size(pawrhoij_cpy)
!Init flags
keep_cplex_=.true.
if (present(keep_cplex)) keep_cplex_=keep_cplex
keep_qphase_=.true.
if (present(keep_qphase)) keep_qphase_=keep_qphase
keep_itypat_=.false.
if (present(keep_itypat)) keep_itypat_=keep_itypat
keep_nspden_=.true.
if (present(keep_nspden)) keep_nspden_=keep_nspden
!Set up parallelism over atoms
paral_atom=(present(comm_atom));if (paral_atom) paral_atom=(xpaw_mpi_comm_size(comm_atom)>1)
nullify(my_atmtab);if (present(mpi_atmtab)) my_atmtab => mpi_atmtab
my_comm_atom=xpaw_mpi_comm_self;if (present(comm_atom)) my_comm_atom=comm_atom
my_atmtab_allocated=.false.
!Determine in which case we are (parallelism, ...)
!No parallelism: a single copy operation
paral_case=0;nrhoij_max=nrhoij_in
pawrhoij_out => pawrhoij_cpy
if (paral_atom) then
if (nrhoij_out<nrhoij_in) then ! Parallelism: the copy operation is a scatter
call get_my_natom(my_comm_atom,my_nrhoij,nrhoij_in)
if (my_nrhoij==nrhoij_out) then
call get_my_atmtab(my_comm_atom,my_atmtab,my_atmtab_allocated,paral_atom,nrhoij_in)
paral_case=1;nrhoij_max=nrhoij_out
pawrhoij_out => pawrhoij_cpy
else
msg=' nrhoij_out should be equal to my_natom !'
LIBPAW_BUG(msg)
end if
else ! Parallelism: the copy operation is a gather
call get_my_natom(my_comm_atom,my_nrhoij,nrhoij_out)
if (my_nrhoij==nrhoij_in) then
paral_case=2;nrhoij_max=nrhoij_in
LIBPAW_DATATYPE_ALLOCATE(pawrhoij_out,(nrhoij_in))
call pawrhoij_nullify(pawrhoij_out)
if (nrhoij_in>0) then
LIBPAW_ALLOCATE(typat,(nrhoij_in))
LIBPAW_ALLOCATE(nlmn,(nrhoij_in))
do irhoij=1,nrhoij_in
typat(irhoij)=irhoij;nlmn(irhoij)=pawrhoij_in(irhoij)%lmn_size
end do
call pawrhoij_alloc(pawrhoij_out,pawrhoij_cpy(1)%cplex_rhoij,&
& pawrhoij_cpy(1)%nspden,pawrhoij_cpy(1)%nspinor,pawrhoij_cpy(1)%nsppol,typat,&
& lmnsize=nlmn,ngrhoij=pawrhoij_cpy(1)%ngrhoij,nlmnmix=pawrhoij_cpy(1)%lmnmix_sz,&
& qphase=pawrhoij_cpy(1)%qphase,&
& use_rhoij_=pawrhoij_cpy(1)%use_rhoij_,&
& use_rhoijp=pawrhoij_cpy(1)%use_rhoijp,&
& use_rhoijres=pawrhoij_cpy(1)%use_rhoijres)
LIBPAW_DEALLOCATE(typat)
LIBPAW_DEALLOCATE(nlmn)
end if
else
msg=' nrhoij_in should be equal to my_natom!'
LIBPAW_BUG(msg)
end if
end if
end if
!Loop on rhoij components
if (nrhoij_max>0) then
do irhoij=1,nrhoij_max
jrhoij=irhoij;if (paral_case==1) jrhoij=my_atmtab(irhoij)
lmn2_size_in=pawrhoij_in(jrhoij)%lmn2_size
lmn2_size_out=lmn2_size_in
cplex_in=pawrhoij_in(jrhoij)%cplex_rhoij
cplex_out=cplex_in;if(keep_cplex_)cplex_out=pawrhoij_out(irhoij)%cplex_rhoij
qphase_in=pawrhoij_in(jrhoij)%qphase
qphase_out=qphase_in;if(keep_qphase_)qphase_out=pawrhoij_out(irhoij)%qphase
nspden_in=pawrhoij_in(jrhoij)%nspden
nselect=pawrhoij_in(jrhoij)%nrhoijsel
nselect_out=pawrhoij_out(irhoij)%nrhoijsel
nspden_out=nspden_in;if(keep_nspden_)nspden_out=pawrhoij_out(irhoij)%nspden
change_dim=(pawrhoij_out(irhoij)%cplex_rhoij/=cplex_out.or. &
& pawrhoij_out(irhoij)%qphase/=qphase_out.or. &
& pawrhoij_out(irhoij)%lmn2_size/=lmn2_size_out.or. &
& pawrhoij_out(irhoij)%nspden/=nspden_out.or. &
& nselect/=nselect_out)
cplex=min(cplex_in,cplex_out)
qphase=min(qphase_in,qphase_out)
! Scalars
pawrhoij_out(irhoij)%cplex_rhoij=cplex_out+0
pawrhoij_out(irhoij)%qphase=qphase_out+0
pawrhoij_out(irhoij)%nspden=nspden_out+0
pawrhoij_out(irhoij)%lmn2_size=lmn2_size_out+0
pawrhoij_out(irhoij)%lmn_size=pawrhoij_in(jrhoij)%lmn_size+0
if(.not.keep_itypat_) pawrhoij_out(irhoij)%itypat =pawrhoij_in(jrhoij)%itypat+0
if(.not.keep_nspden_) pawrhoij_out(irhoij)%nsppol =pawrhoij_in(jrhoij)%nsppol+0
if(.not.keep_nspden_) pawrhoij_out(irhoij)%nspinor=pawrhoij_in(jrhoij)%nspinor+0
pawrhoij_out(irhoij)%nrhoijsel=nselect+0
! if (pawrhoij_out(irhoij)%itypat/=pawrhoij_in(jrhoij)%itypat) then
! write(unit=msg,fmt='(a,i3,a)') 'Type of atom ',jrhoij,' is different (dont copy it) !'
! LIBPAW_COMMENT(msg)
! end if
! Optional pointer: non-zero elements of rhoij
use_rhoijp=pawrhoij_in(jrhoij)%use_rhoijp
if (pawrhoij_out(irhoij)%use_rhoijp/=use_rhoijp) then
if (pawrhoij_out(irhoij)%use_rhoijp>0) then
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%rhoijp)
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%rhoijselect)
end if
if (use_rhoijp>0) then
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%rhoijp,(cplex_out*qphase_out*lmn2_size_out,nspden_out))
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%rhoijselect,(lmn2_size_out))
pawrhoij_out(irhoij)%rhoijselect=0
end if
end if
pawrhoij_out(irhoij)%use_rhoijp=use_rhoijp
if (use_rhoijp>0) then
if (change_dim) then
if(allocated(pawrhoij_out(irhoij)%rhoijp)) then
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%rhoijp)
end if
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%rhoijp,(cplex_out*qphase_out*lmn2_size_out,nspden_out))
end if
pawrhoij_out(irhoij)%rhoijp(:,:)=zero
if (nspden_out==1) then
if (nspden_in==2) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,nselect
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1) &
& +pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,2)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else ! nspden_in==1 or 4
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,nselect
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end if
else if (nspden_out==2) then
if (nspden_in==1) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,nselect
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,1)= &
& half*pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1)+zero
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,2)= &
& half*pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else if (nspden_in==2) then
do ispden=1,nspden_out
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,nselect
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,ispden)= &
& pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,ispden)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end do
else ! nspden_in==4
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,nselect
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,1)= &
& half*(pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1) &
& +pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,4))+zero
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,2)= &
& half*(pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1) &
& -pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,4))+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end if
else if (nspden_out==4) then
if (nspden_in==1) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,nselect
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1)+zero
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,2:4)=zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else if (nspden_in==2) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,nselect
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1) &
& +pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,2)+zero
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,4)= &
& pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,1) &
& -pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,2)+zero
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,2:3)=zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else ! nspden_in==4
do ispden=1,nspden_out
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,nselect
pawrhoij_out(irhoij)%rhoijp(i_out+1:i_out+cplex,ispden)= &
& pawrhoij_in(jrhoij)%rhoijp(i_in+1:i_in+cplex,ispden)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end do
end if
end if
end if
! Optional pointer: indexes for non-zero elements selection
if (use_rhoijp>0) then
if (change_dim) then
if(allocated(pawrhoij_out(irhoij)%rhoijselect)) then
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%rhoijselect)
end if
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%rhoijselect,(lmn2_size_out))
end if
pawrhoij_out(irhoij)%rhoijselect=0
pawrhoij_out(irhoij)%rhoijselect(1:nselect)=pawrhoij_in(jrhoij)%rhoijselect(1:nselect)+0
end if
! Optional pointer: indexes of rhoij to be mixed
lmnmix=pawrhoij_in(jrhoij)%lmnmix_sz
if (pawrhoij_out(irhoij)%lmnmix_sz/=lmnmix) then
if (pawrhoij_out(irhoij)%lmnmix_sz>0) then
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%kpawmix)
end if
if (lmnmix>0) then
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%kpawmix,(lmnmix))
pawrhoij_out(irhoij)%kpawmix=0
end if
pawrhoij_out(irhoij)%lmnmix_sz=lmnmix
end if
if (lmnmix>0) pawrhoij_out(irhoij)%kpawmix(1:lmnmix)=pawrhoij_in(jrhoij)%kpawmix(1:lmnmix)
! Optional pointer: gradients of rhoij
ngrhoij=pawrhoij_in(jrhoij)%ngrhoij
if (pawrhoij_out(irhoij)%ngrhoij/=ngrhoij) then
if (pawrhoij_out(irhoij)%ngrhoij>0) then
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%grhoij)
end if
if (ngrhoij>0) then
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%grhoij,(ngrhoij,cplex_out*qphase_out*lmn2_size_out,nspden_out))
end if
pawrhoij_out(irhoij)%ngrhoij=ngrhoij
end if
if (ngrhoij>0) then
if (change_dim) then
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%grhoij)
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%grhoij,(ngrhoij,cplex_out*qphase_out*lmn2_size_out,nspden_out))
end if
pawrhoij_out(irhoij)%grhoij(:,:,:)=zero
if (nspden_out==1) then
if (nspden_in==2) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1) &
& +pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,2)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else ! nspden_in==1 or 4
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end if
else if (nspden_out==2) then
if (nspden_in==1) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,1)= &
& half*pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1)+zero
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,2)= &
& half*pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else if (nspden_in==2) then
do ispden=1,nspden_out
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,ispden)= &
& pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,ispden)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end do
else ! nspden_in==4
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,1)= &
& half*(pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1) &
& +pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,4))+zero
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,2)= &
& half*(pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1) &
& -pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,4))+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end if
else if (nspden_out==4) then
if (nspden_in==1) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1)+zero
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,2:4)=zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else if (nspden_in==2) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1) &
& +pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,2)+zero
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,4)= &
& pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,1) &
& -pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,2)+zero
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,2:3)=zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else ! nspden_in==4
do ispden=1,nspden_out
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%grhoij(1:ngrhoij,i_out+1:i_out+cplex,ispden)= &
& pawrhoij_in(jrhoij)%grhoij(1:ngrhoij,i_in+1:i_in+cplex,ispden)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end do
end if
end if
end if
! Optional pointer: residuals of rhoij
use_rhoijres=pawrhoij_in(jrhoij)%use_rhoijres
if (pawrhoij_out(irhoij)%use_rhoijres/=use_rhoijres) then
if (pawrhoij_out(irhoij)%use_rhoijres>0) then
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%rhoijres)
end if
if (use_rhoijres>0) then
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%rhoijres,(cplex_out*qphase_out*lmn2_size_out,nspden_out))
end if
pawrhoij_out(irhoij)%use_rhoijres=use_rhoijres
end if
if (use_rhoijres>0) then
if (change_dim) then
LIBPAW_DEALLOCATE(pawrhoij_out(irhoij)%rhoijres)
LIBPAW_ALLOCATE(pawrhoij_out(irhoij)%rhoijres,(cplex_out*qphase_out*lmn2_size_out,nspden_out))
end if
pawrhoij_out(irhoij)%rhoijres(:,:)=zero
if (nspden_out==1) then
if (nspden_in==2) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,1) &
& +pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,2)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else ! nspden_in==1 or 4
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,1)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end if
else if (nspden_out==2) then
if (nspden_in==1) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,1)= &
& half*pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,1)+zero
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,2)= &
& half*pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,1)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else if (nspden_in==2) then
do ispden=1,nspden_out
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,ispden)= &
& pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,ispden)+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end do
else ! nspden_in==4
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,1)= &
& half*(pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,1) &
& +pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,4))+zero
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,2)= &
& half*(pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,1) &
& -pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,4))+zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
end if
else if (nspden_out==4) then
if (nspden_in==1) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,1)= &
& pawrhoij_in(jrhoij)%rhoijres(i_in+1:i_in+cplex,1)+zero
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,2:4)=zero
i_in=i_in+cplex_in;i_out=i_out+cplex_out
end do
end do
else if (nspden_in==2) then
do iphase=1,qphase
i_in=(iphase-1)*lmn2_size_in;i_out=(iphase-1)*lmn2_size_out
do ilmn=1,lmn2_size_out
pawrhoij_out(irhoij)%rhoijres(i_out+1:i_out+cplex,1)= &