1 /**********************************************************************
2   TRAN_Add_Density_Lead.c:
3 
4   TRAN_Add_Density_Lead.c is a subroutine to correct charge density
5   near the boundary region of the extended system.
6   The charge density from that of electrodes is added to the regions
7   [0:TRAN_grid_bound[0]] and [TRAN_grid_bound[1]:Ngrid1-1].
8 
9   Log of TRAN_Add_Density_Lead.c:
10 
11      24/July/2008  Released by T.Ozaki
12      24/Apr/2012   Modified by T.Ozaki
13 
14 ***********************************************************************/
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <mpi.h>
20 #include "tran_variables.h"
21 #include "tran_prototypes.h"
22 
23 void Density_Grid_Copy_B2D();
24 
25 
26 
TRAN_Add_Density_Lead(MPI_Comm comm1,int SpinP_switch,int Ngrid1,int Ngrid2,int Ngrid3,int My_NumGridB_AB,double ** Density_Grid_B)27 void TRAN_Add_Density_Lead(
28             MPI_Comm comm1,
29             int SpinP_switch,
30             int Ngrid1,
31             int Ngrid2,
32             int Ngrid3,
33             int My_NumGridB_AB,
34             double **Density_Grid_B)
35 
36 #define grid_e_ref(i,j,k)  ( ((i)-l1[0])*Ngrid2*Ngrid3+(j)*Ngrid3+(k) )
37 
38 {
39   int side,l1[2];
40   int i,j,k,spin,N2D,GNs,GN,BN_AB;
41   int myid,numprocs;
42 
43   MPI_Comm_size(comm1,&numprocs);
44   MPI_Comm_rank(comm1,&myid);
45 
46   if (myid==Host_ID){
47     printf("<TRAN_Add_Density_Lead>\n");
48   }
49 
50   /* set N2D and GNs */
51 
52   N2D = Ngrid1*Ngrid2;
53   GNs = ((myid*N2D+numprocs-1)/numprocs)*Ngrid3;
54 
55   /******************************************************
56     add contribution to charge density from electrodes
57     side=0 -> left lead
58     side=1 -> right lead
59   ******************************************************/
60 
61   for (side=0; side<=1; side++){
62 
63     if (side==0){
64       l1[0] = 0;
65       l1[1] = TRAN_grid_bound[0];
66     }
67     else{
68       l1[0] = TRAN_grid_bound[1];
69       l1[1] = Ngrid1-1;
70     }
71 
72     for (spin=0; spin<=SpinP_switch; spin++){
73       for (BN_AB=0; BN_AB<My_NumGridB_AB; BN_AB++){
74 
75 	GN = BN_AB + GNs;
76 	i = GN/(Ngrid2*Ngrid3);
77 	j = (GN - i*Ngrid2*Ngrid3)/Ngrid3;
78 	k = GN - i*Ngrid2*Ngrid3 - j*Ngrid3;
79 
80 	if ( l1[0]<=i && i<=l1[1] ) {
81 
82 	  Density_Grid_B[spin][BN_AB] += ElectrodeDensity_Grid[side][spin][ grid_e_ref(i,j,k) ];
83 
84 	  if (SpinP_switch==0){
85 	    Density_Grid_B[1][BN_AB] += ElectrodeDensity_Grid[side][0][ grid_e_ref(i,j,k) ];
86 	  }
87 	}
88 
89       }
90     }
91   }
92 
93   /******************************************************
94              MPI: from the partitions B to D
95   ******************************************************/
96 
97   Density_Grid_Copy_B2D();
98 }
99