1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <time.h>
5 #include "openmx_common.h"
6 
7 static void Find_ln(int i0, int n0, int res[2]);
8 
Find_CGrids(int Real_Position,int n1,int n2,int n3,double Cxyz[4],int NOC[4])9 void Find_CGrids(int Real_Position, int n1, int n2, int n3,
10                  double Cxyz[4], int NOC[4])
11 {
12   /*********************************************************
13    Real_Position==0
14        gives the coordinates in the original cell (Rn==0)
15 
16    Real_Position==1
17        gives the coordinates in the translated cell
18   **********************************************************/
19 
20   int Rn,l1,l2,l3,N;
21   int nn1,nn2,nn3,res[2];
22   double x0,y0,z0;
23 
24   Find_ln(1,n1,res);
25   l1  = res[0];
26   nn1 = res[1];
27 
28   Find_ln(2,n2,res);
29   l2  = res[0];
30   nn2 = res[1];
31 
32   Find_ln(3,n3,res);
33   l3  = res[0];
34   nn3 = res[1];
35 
36   if (CpyCell<abs(l1) || CpyCell<abs(l2) || CpyCell<abs(l3)){
37 
38     /* outside of Tcell */
39 
40     Rn = 0;
41     x0 = atv[Rn][1];
42     y0 = atv[Rn][2];
43     z0 = atv[Rn][3];
44 
45     Cxyz[1] = 10e+5;
46     Cxyz[2] = 10e+5;
47     Cxyz[3] = 10e+5;
48 
49   }
50   else{
51 
52     Rn = R_atv(CpyCell,l1,l2,l3);
53     x0 = atv[Rn][1];
54     y0 = atv[Rn][2];
55     z0 = atv[Rn][3];
56 
57     /*  N = nn1*Ngrid2*Ngrid3 + nn2*Ngrid3 + nn3;  */
58 
59     if (Real_Position==0){
60       Cxyz[1] = (double)nn1*gtv[1][1] + (double)nn2*gtv[2][1]
61               + (double)nn3*gtv[3][1] + Grid_Origin[1];
62       Cxyz[2] = (double)nn1*gtv[1][2] + (double)nn2*gtv[2][2]
63               + (double)nn3*gtv[3][2] + Grid_Origin[2];
64       Cxyz[3] = (double)nn1*gtv[1][3] + (double)nn2*gtv[2][3]
65               + (double)nn3*gtv[3][3] + Grid_Origin[3];
66     }
67     else{
68       Cxyz[1] = x0
69               + (double)nn1*gtv[1][1] + (double)nn2*gtv[2][1]
70               + (double)nn3*gtv[3][1] + Grid_Origin[1];
71       Cxyz[2] = y0
72               + (double)nn1*gtv[1][2] + (double)nn2*gtv[2][2]
73               + (double)nn3*gtv[3][2] + Grid_Origin[2];
74       Cxyz[3] = z0
75               + (double)nn1*gtv[1][3] + (double)nn2*gtv[2][3]
76               + (double)nn3*gtv[3][3] + Grid_Origin[3];
77     }
78   }
79 
80   NOC[0] = Rn;
81   NOC[1] = nn1;
82   NOC[2] = nn2;
83   NOC[3] = nn3;
84 }
85 
86 
Find_ln(int i0,int n0,int res[2])87 void Find_ln(int i0, int n0, int res[2])
88 {
89   int l0,n1,N;
90 
91   if      (i0==1) N = Ngrid1;
92   else if (i0==2) N = Ngrid2;
93   else if (i0==3) N = Ngrid3;
94 
95   if (n0<0){
96     l0 = -((abs(n0)-1)/N + 1);
97     n1 = N - (abs(n0) - N*(abs(l0)-1));
98   }
99   else if (N<=n0){
100     l0 = n0/N;
101     n1 = n0 - N*l0;
102   }
103   else{
104     l0 = 0;
105     n1 = n0;
106   }
107 
108   res[0] = l0;
109   res[1] = n1;
110 }
111 
112