diff options
Diffstat (limited to 'src/symcoord.c')
| -rw-r--r-- | src/symcoord.c | 355 |
1 files changed, 355 insertions, 0 deletions
diff --git a/src/symcoord.c b/src/symcoord.c new file mode 100644 index 0000000..97a8d9a --- /dev/null +++ b/src/symcoord.c | |||
| @@ -0,0 +1,355 @@ | |||
| 1 | #include "symcoord.h" | ||
| 2 | |||
| 3 | static Cube antindex_coud_sym16(uint64_t ind); | ||
| 4 | static Cube antindex_cp_sym16(uint64_t ind); | ||
| 5 | static Cube antindex_eofbepos_sym16(uint64_t ind); | ||
| 6 | static Cube antindex_drud_sym16(uint64_t ind); | ||
| 7 | static Cube antindex_drudfin_noE_sym16(uint64_t ind); | ||
| 8 | static Cube antindex_khuge(uint64_t ind); | ||
| 9 | |||
| 10 | static uint64_t index_coud_sym16(Cube cube); | ||
| 11 | static uint64_t index_cp_sym16(Cube cube); | ||
| 12 | static uint64_t index_eofbepos_sym16(Cube cube); | ||
| 13 | static uint64_t index_drud_sym16(Cube cube); | ||
| 14 | static uint64_t index_drudfin_noE_sym16(Cube cube); | ||
| 15 | static uint64_t index_khuge(Cube cube); | ||
| 16 | |||
| 17 | static void gensym(SymData *sd); | ||
| 18 | static bool read_symdata_file(SymData *sd); | ||
| 19 | static bool write_symdata_file(SymData *sd); | ||
| 20 | |||
| 21 | /* Transformation groups and symmetry data ***********************************/ | ||
| 22 | |||
| 23 | static Trans | ||
| 24 | trans_group_udfix[16] = { | ||
| 25 | uf, ur, ub, ul, | ||
| 26 | df, dr, db, dl, | ||
| 27 | uf_mirror, ur_mirror, ub_mirror, ul_mirror, | ||
| 28 | df_mirror, dr_mirror, db_mirror, dl_mirror, | ||
| 29 | }; | ||
| 30 | |||
| 31 | static SymData | ||
| 32 | sd_coud_16 = { | ||
| 33 | .filename = "sd_coud_16", | ||
| 34 | .coord = &coord_coud, | ||
| 35 | .sym_coord = &coord_coud_sym16, | ||
| 36 | .ntrans = 16, | ||
| 37 | .trans = trans_group_udfix | ||
| 38 | }; | ||
| 39 | |||
| 40 | static SymData | ||
| 41 | sd_cp_16 = { | ||
| 42 | .filename = "sd_cp_16", | ||
| 43 | .coord = &coord_cp, | ||
| 44 | .sym_coord = &coord_cp_sym16, | ||
| 45 | .ntrans = 16, | ||
| 46 | .trans = trans_group_udfix | ||
| 47 | }; | ||
| 48 | |||
| 49 | static SymData | ||
| 50 | sd_eofbepos_16 = { | ||
| 51 | .filename = "sd_eofbepos_16", | ||
| 52 | .coord = &coord_eofbepos, | ||
| 53 | .sym_coord = &coord_eofbepos_sym16, | ||
| 54 | .ntrans = 16, | ||
| 55 | .trans = trans_group_udfix | ||
| 56 | }; | ||
| 57 | |||
| 58 | static int nsymdata = 3; | ||
| 59 | static SymData * all_sd[] = { | ||
| 60 | &sd_coud_16, | ||
| 61 | &sd_cp_16, | ||
| 62 | &sd_eofbepos_16, | ||
| 63 | }; | ||
| 64 | |||
| 65 | |||
| 66 | /* Coordinates and their implementation **************************************/ | ||
| 67 | |||
| 68 | Coordinate | ||
| 69 | coord_eofbepos_sym16 = { | ||
| 70 | .index = index_eofbepos_sym16, | ||
| 71 | .cube = antindex_eofbepos_sym16, | ||
| 72 | .ntrans = 16, | ||
| 73 | .trans = trans_group_udfix, | ||
| 74 | }; | ||
| 75 | |||
| 76 | Coordinate | ||
| 77 | coord_coud_sym16 = { | ||
| 78 | .index = index_coud_sym16, | ||
| 79 | .cube = antindex_coud_sym16, | ||
| 80 | .ntrans = 16, | ||
| 81 | .trans = trans_group_udfix, | ||
| 82 | }; | ||
| 83 | |||
| 84 | Coordinate | ||
| 85 | coord_cp_sym16 = { | ||
| 86 | .index = index_cp_sym16, | ||
| 87 | .cube = antindex_cp_sym16, | ||
| 88 | .ntrans = 16, | ||
| 89 | .trans = trans_group_udfix, | ||
| 90 | }; | ||
| 91 | |||
| 92 | Coordinate | ||
| 93 | coord_drud_sym16 = { | ||
| 94 | .index = index_drud_sym16, | ||
| 95 | .cube = antindex_drud_sym16, | ||
| 96 | .max = POW3TO7 * 64430, | ||
| 97 | .ntrans = 16, | ||
| 98 | .trans = trans_group_udfix, | ||
| 99 | }; | ||
| 100 | |||
| 101 | Coordinate | ||
| 102 | coord_drudfin_noE_sym16 = { | ||
| 103 | .index = index_drudfin_noE_sym16, | ||
| 104 | .cube = antindex_drudfin_noE_sym16, | ||
| 105 | .max = FACTORIAL8 * 2768, | ||
| 106 | .ntrans = 16, | ||
| 107 | .trans = trans_group_udfix, | ||
| 108 | }; | ||
| 109 | |||
| 110 | Coordinate | ||
| 111 | coord_khuge = { | ||
| 112 | .index = index_khuge, | ||
| 113 | .cube = antindex_khuge, | ||
| 114 | .max = POW3TO7 * FACTORIAL4 * 64430, | ||
| 115 | .ntrans = 16, | ||
| 116 | .trans = trans_group_udfix, | ||
| 117 | }; | ||
| 118 | |||
| 119 | /* Functions *****************************************************************/ | ||
| 120 | |||
| 121 | static Cube | ||
| 122 | antindex_coud_sym16(uint64_t ind) | ||
| 123 | { | ||
| 124 | return sd_coud_16.rep[ind]; | ||
| 125 | } | ||
| 126 | |||
| 127 | static Cube | ||
| 128 | antindex_cp_sym16(uint64_t ind) | ||
| 129 | { | ||
| 130 | return sd_cp_16.rep[ind]; | ||
| 131 | } | ||
| 132 | |||
| 133 | static Cube | ||
| 134 | antindex_eofbepos_sym16(uint64_t ind) | ||
| 135 | { | ||
| 136 | return sd_eofbepos_16.rep[ind]; | ||
| 137 | } | ||
| 138 | |||
| 139 | static Cube | ||
| 140 | antindex_drud_sym16(uint64_t ind) | ||
| 141 | { | ||
| 142 | Cube c; | ||
| 143 | |||
| 144 | c = antindex_eofbepos_sym16(ind/POW3TO7); | ||
| 145 | c.coud = ind % POW3TO7; | ||
| 146 | c.cofb = c.coud; | ||
| 147 | c.corl = c.coud; | ||
| 148 | |||
| 149 | return c; | ||
| 150 | } | ||
| 151 | |||
| 152 | static Cube | ||
| 153 | antindex_drudfin_noE_sym16(uint64_t ind) | ||
| 154 | { | ||
| 155 | Cube c1, c2; | ||
| 156 | |||
| 157 | c1 = coord_epud.cube(ind % FACTORIAL8); | ||
| 158 | c2 = antindex_cp_sym16(ind/FACTORIAL8); | ||
| 159 | c1.cp = c2.cp; | ||
| 160 | |||
| 161 | return c1; | ||
| 162 | } | ||
| 163 | |||
| 164 | static Cube | ||
| 165 | antindex_khuge(uint64_t ind) | ||
| 166 | { | ||
| 167 | Cube c; | ||
| 168 | |||
| 169 | c = antindex_eofbepos_sym16(ind/(FACTORIAL4*POW3TO7)); | ||
| 170 | c.epose = ((c.epose / 24) * 24) + ((ind/POW3TO7) % 24); | ||
| 171 | c.coud = ind % POW3TO7; | ||
| 172 | |||
| 173 | return c; | ||
| 174 | } | ||
| 175 | |||
| 176 | static uint64_t | ||
| 177 | index_coud_sym16(Cube cube) | ||
| 178 | { | ||
| 179 | return sd_coud_16.class[coord_coud.index(cube)]; | ||
| 180 | } | ||
| 181 | |||
| 182 | static uint64_t | ||
| 183 | index_cp_sym16(Cube cube) | ||
| 184 | { | ||
| 185 | return sd_cp_16.class[coord_cp.index(cube)]; | ||
| 186 | } | ||
| 187 | |||
| 188 | static uint64_t | ||
| 189 | index_drud_sym16(Cube cube) | ||
| 190 | { | ||
| 191 | Trans t; | ||
| 192 | Cube c; | ||
| 193 | |||
| 194 | t = sd_eofbepos_16.transtorep[coord_eofbepos.index(cube)]; | ||
| 195 | c = apply_trans(t, cube); | ||
| 196 | |||
| 197 | return index_eofbepos_sym16(c) * POW3TO7 + c.coud; | ||
| 198 | } | ||
| 199 | |||
| 200 | static uint64_t | ||
| 201 | index_drudfin_noE_sym16(Cube cube) | ||
| 202 | { | ||
| 203 | Trans t; | ||
| 204 | Cube c; | ||
| 205 | |||
| 206 | t = sd_cp_16.transtorep[coord_cp.index(cube)]; | ||
| 207 | c = apply_trans(t, cube); | ||
| 208 | |||
| 209 | return index_cp_sym16(c) * FACTORIAL8 + coord_epud.index(c); | ||
| 210 | } | ||
| 211 | |||
| 212 | static uint64_t | ||
| 213 | index_eofbepos_sym16(Cube cube) | ||
| 214 | { | ||
| 215 | return sd_eofbepos_16.class[coord_eofbepos.index(cube)]; | ||
| 216 | } | ||
| 217 | |||
| 218 | static uint64_t | ||
| 219 | index_khuge(Cube cube) | ||
| 220 | { | ||
| 221 | Trans t; | ||
| 222 | Cube c; | ||
| 223 | uint64_t a; | ||
| 224 | |||
| 225 | t = sd_eofbepos_16.transtorep[coord_eofbepos.index(cube)]; | ||
| 226 | c = apply_trans(t, cube); | ||
| 227 | a = (index_eofbepos_sym16(c) * 24) + (c.epose % 24); | ||
| 228 | |||
| 229 | return a * POW3TO7 + c.coud; | ||
| 230 | } | ||
| 231 | |||
| 232 | /* Other functions ***********************************************************/ | ||
| 233 | |||
| 234 | static void | ||
| 235 | gensym(SymData *sd) | ||
| 236 | { | ||
| 237 | uint64_t i, in, nreps = 0; | ||
| 238 | int j; | ||
| 239 | Cube c, d; | ||
| 240 | |||
| 241 | if (sd->generated) | ||
| 242 | return; | ||
| 243 | |||
| 244 | sd->class = malloc(sd->coord->max * sizeof(uint64_t)); | ||
| 245 | sd->rep = malloc(sd->coord->max * sizeof(Cube)); | ||
| 246 | sd->transtorep = malloc(sd->coord->max * sizeof(Trans)); | ||
| 247 | |||
| 248 | if (read_symdata_file(sd)) { | ||
| 249 | sd->generated = true; | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | |||
| 253 | fprintf(stderr, "Cannot load %s, generating it\n", sd->filename); | ||
| 254 | |||
| 255 | for (i = 0; i < sd->coord->max; i++) | ||
| 256 | sd->class[i] = sd->coord->max + 1; | ||
| 257 | |||
| 258 | for (i = 0; i < sd->coord->max; i++) { | ||
| 259 | if (sd->class[i] == sd->coord->max + 1) { | ||
| 260 | c = sd->coord->cube(i); | ||
| 261 | sd->rep[nreps] = c; | ||
| 262 | for (j = 0; j < sd->ntrans; j++) { | ||
| 263 | d = apply_trans(sd->trans[j], c); | ||
| 264 | in = sd->coord->index(d); | ||
| 265 | |||
| 266 | if (sd->class[in] == sd->coord->max + 1) { | ||
| 267 | sd->class[in] = nreps; | ||
| 268 | sd->transtorep[in] = | ||
| 269 | inverse_trans(sd->trans[j]); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | nreps++; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | sd->sym_coord->max = nreps; | ||
| 277 | sd->rep = realloc(sd->rep, nreps * sizeof(Cube)); | ||
| 278 | sd->generated = true; | ||
| 279 | |||
| 280 | fprintf(stderr, "Found %lu classes\n", nreps); | ||
| 281 | |||
| 282 | if (!write_symdata_file(sd)) | ||
| 283 | fprintf(stderr, "Error writing SymData file\n"); | ||
| 284 | |||
| 285 | return; | ||
| 286 | } | ||
| 287 | |||
| 288 | static bool | ||
| 289 | read_symdata_file(SymData *sd) | ||
| 290 | { | ||
| 291 | init_env(); | ||
| 292 | |||
| 293 | FILE *f; | ||
| 294 | char fname[strlen(tabledir)+100]; | ||
| 295 | uint64_t n = sd->coord->max, *sn = &sd->sym_coord->max; | ||
| 296 | bool r = true; | ||
| 297 | |||
| 298 | strcpy(fname, tabledir); | ||
| 299 | strcat(fname, "/"); | ||
| 300 | strcat(fname, sd->filename); | ||
| 301 | |||
| 302 | if ((f = fopen(fname, "rb")) == NULL) | ||
| 303 | return false; | ||
| 304 | |||
| 305 | r = r && fread(&sd->sym_coord->max, sizeof(uint64_t), 1, f) == 1; | ||
| 306 | r = r && fread(sd->rep, sizeof(Cube), *sn, f) == *sn; | ||
| 307 | r = r && fread(sd->class, sizeof(uint64_t), n, f) == n; | ||
| 308 | r = r && fread(sd->transtorep, sizeof(Trans), n, f) == n; | ||
| 309 | |||
| 310 | fclose(f); | ||
| 311 | return r; | ||
| 312 | } | ||
| 313 | |||
| 314 | static bool | ||
| 315 | write_symdata_file(SymData *sd) | ||
| 316 | { | ||
| 317 | init_env(); | ||
| 318 | |||
| 319 | FILE *f; | ||
| 320 | char fname[strlen(tabledir)+100]; | ||
| 321 | uint64_t n = sd->coord->max, *sn = &sd->sym_coord->max; | ||
| 322 | bool r = true; | ||
| 323 | |||
| 324 | strcpy(fname, tabledir); | ||
| 325 | strcat(fname, "/"); | ||
| 326 | strcat(fname, sd->filename); | ||
| 327 | |||
| 328 | if ((f = fopen(fname, "wb")) == NULL) | ||
| 329 | return false; | ||
| 330 | |||
| 331 | r = r && fwrite(&sd->sym_coord->max, sizeof(uint64_t), 1, f) == 1; | ||
| 332 | r = r && fwrite(sd->rep, sizeof(Cube), *sn, f) == *sn; | ||
| 333 | r = r && fwrite(sd->class, sizeof(uint64_t), n, f) == n; | ||
| 334 | r = r && fwrite(sd->transtorep, sizeof(Trans), n, f) == n; | ||
| 335 | |||
| 336 | fclose(f); | ||
| 337 | return r; | ||
| 338 | } | ||
| 339 | |||
| 340 | void | ||
| 341 | init_symcoord() | ||
| 342 | { | ||
| 343 | int i; | ||
| 344 | |||
| 345 | static bool initialized = false; | ||
| 346 | if (initialized) | ||
| 347 | return; | ||
| 348 | initialized = true; | ||
| 349 | |||
| 350 | init_coord(); | ||
| 351 | |||
| 352 | for (i = 0; i < nsymdata; i++) | ||
| 353 | gensym(all_sd[i]); | ||
| 354 | } | ||
| 355 | |||
