1 /* Copyright (C) 2015 Atsushi Togo */
2 /* All rights reserved. */
3 
4 /* This file was originally part of spglib and is part of kspclib. */
5 
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
8 /* are met: */
9 
10 /* * Redistributions of source code must retain the above copyright */
11 /*   notice, this list of conditions and the following disclaimer. */
12 
13 /* * Redistributions in binary form must reproduce the above copyright */
14 /*   notice, this list of conditions and the following disclaimer in */
15 /*   the documentation and/or other materials provided with the */
16 /*   distribution. */
17 
18 /* * Neither the name of the phonopy project nor the names of its */
19 /*   contributors may be used to endorse or promote products derived */
20 /*   from this software without specific prior written permission. */
21 
22 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
23 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
24 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
25 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE */
26 /* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
27 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, */
28 /* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
29 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER */
30 /* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT */
31 /* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN */
32 /* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
33 /* POSSIBILITY OF SUCH DAMAGE. */
34 
35 #include "kgrid.h"
36 
37 static void get_all_grid_addresses(int grid_address[][3], const int mesh[3]);
38 static int get_grid_point_double_mesh(const int address_double[3],
39 				      const int mesh[3]);
40 static int get_grid_point_single_mesh(const int address[3], const int mesh[3]);
41 static void modulo_i3(int v[3], const int m[3]);
42 static void reduce_grid_address(int address[3], const int mesh[3]);
43 static void reduce_grid_address_double(int address[3], const int mesh[3]);
44 
kgd_get_all_grid_addresses(int grid_address[][3],const int mesh[3])45 void kgd_get_all_grid_addresses(int grid_address[][3], const int mesh[3])
46 {
47   get_all_grid_addresses(grid_address, mesh);
48 }
49 
kgd_get_grid_point_double_mesh(const int address_double[3],const int mesh[3])50 int kgd_get_grid_point_double_mesh(const int address_double[3],
51 				   const int mesh[3])
52 {
53   return get_grid_point_double_mesh(address_double, mesh);
54 }
55 
kgd_get_grid_address_double_mesh(int address_double[3],const int address[3],const int mesh[3],const int is_shift[3])56 void kgd_get_grid_address_double_mesh(int address_double[3],
57 				      const int address[3],
58 				      const int mesh[3],
59 				      const int is_shift[3])
60 {
61   int i;
62 
63   for (i = 0; i < 3; i++) {
64     address_double[i] = address[i] * 2 + (is_shift[i] != 0);
65   }
66   reduce_grid_address_double(address_double, mesh);
67 }
68 
get_all_grid_addresses(int grid_address[][3],const int mesh[3])69 static void get_all_grid_addresses(int grid_address[][3], const int mesh[3])
70 {
71   int i, j, k, grid_point;
72   int address[3];
73 
74   for (i = 0; i < mesh[0]; i++) {
75     address[0] = i;
76     for (j = 0; j < mesh[1]; j++) {
77       address[1] = j;
78       for (k = 0; k < mesh[2]; k++) {
79 	address[2] = k;
80 	grid_point = get_grid_point_single_mesh(address, mesh);
81 	grid_address[grid_point][0] = address[0];
82 	grid_address[grid_point][1] = address[1];
83 	grid_address[grid_point][2] = address[2];
84 	reduce_grid_address(grid_address[grid_point], mesh);
85       }
86     }
87   }
88 }
89 
get_grid_point_double_mesh(const int address_double[3],const int mesh[3])90 static int get_grid_point_double_mesh(const int address_double[3],
91 				      const int mesh[3])
92 {
93   int i, address[3];
94 
95   for (i = 0; i < 3; i++) {
96     if (address_double[i] % 2 == 0) {
97       address[i] = address_double[i] / 2;
98     } else {
99       address[i] = (address_double[i] - 1) / 2;
100     }
101   }
102   modulo_i3(address, mesh);
103 
104   return get_grid_point_single_mesh(address, mesh);
105 }
106 
get_grid_point_single_mesh(const int address[3],const int mesh[3])107 static int get_grid_point_single_mesh(const int address[3],
108 				      const int mesh[3])
109 {
110 #ifndef GRID_ORDER_XYZ
111   return address[2] * mesh[0] * mesh[1] + address[1] * mesh[0] + address[0];
112 #else
113   return address[0] * mesh[1] * mesh[2] + address[1] * mesh[2] + address[2];
114 #endif
115 }
116 
modulo_i3(int v[3],const int m[3])117 static void modulo_i3(int v[3], const int m[3])
118 {
119   int i;
120 
121   for (i = 0; i < 3; i++) {
122     v[i] = v[i] % m[i];
123 
124     if (v[i] < 0) {
125       v[i] += m[i];
126     }
127   }
128 }
129 
reduce_grid_address(int address[3],const int mesh[3])130 static void reduce_grid_address(int address[3], const int mesh[3])
131 {
132   int i;
133 
134   for (i = 0; i < 3; i++) {
135 #ifndef GRID_BOUNDARY_AS_NEGATIVE
136     address[i] -= mesh[i] * (address[i] > mesh[i] / 2);
137 #else
138     address[i] -= mesh[i] * (address[i] > (mesh[i] - 1) / 2);
139 #endif
140   }
141 }
142 
reduce_grid_address_double(int address[3],const int mesh[3])143 static void reduce_grid_address_double(int address[3], const int mesh[3])
144 {
145   int i;
146 
147   for (i = 0; i < 3; i++) {
148 #ifndef GRID_BOUNDARY_AS_NEGATIVE
149     address[i] -= 2 * mesh[i] * (address[i] > mesh[i]);
150 #else
151     address[i] -= 2 * mesh[i] * (address[i] > mesh[i] - 1);
152 #endif
153   }
154 }
155