aboutsummaryrefslogtreecommitdiff
path: root/src/symcoord.c
blob: cb105a9f2d52e8f323a317aa00a610b3961c45fd (plain)
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
#include "symcoord.h"

/* These constants have been computed generating the respective SymData */
#define CLASSES_CP_16        2768
#define CLASSES_EOFBEPOS_16  64430

static Cube        antindex_cp_sym16(uint64_t ind);
static Cube        antindex_eofbepos_sym16(uint64_t ind);
static Cube        antindex_drud_sym16(uint64_t ind);
static Cube        antindex_drudfin_noE_sym16(uint64_t ind);
/*static Cube        antindex_khuge(uint64_t ind);*/
static Cube        antindex_nxopt31(uint64_t ind);

static uint64_t    index_cp_sym16(Cube cube);
static uint64_t    index_eofbepos_sym16(Cube cube);
static uint64_t    index_drud_sym16(Cube cube);
static uint64_t    index_drudfin_noE_sym16(Cube cube);
/*static uint64_t    index_khuge(Cube cube);*/
static uint64_t    index_nxopt31(Cube cube);

static int         transfinder_drud_sym16(uint64_t ind, Trans *ret);
static int         transfinder_drudfin_noE_sym16(uint64_t ind, Trans *ret);
/*static int         transfinder_khuge(uint64_t ind, Trans *ret);*/
static int         transfinder_nxopt31(uint64_t ind, Trans *ret);

static void        gensym(SymData *sd);
static bool        read_symdata_file(SymData *sd);
static int         selfsims(SymData *sd, uint64_t ind, Trans *ret);
static bool        write_symdata_file(SymData *sd);

/* Transformation groups and symmetry data ***********************************/

static Trans
trans_group_udfix[16] = {
	uf, ur, ub, ul,
	df, dr, db, dl,
	uf_mirror, ur_mirror, ub_mirror, ul_mirror,
	df_mirror, dr_mirror, db_mirror, dl_mirror,
};

static SymData
sd_cp_16 = {
	.filename  = "sd_cp_16",
	.coord     = &coord_cp,
	.sym_coord = &coord_cp_sym16,
	.ntrans    = 16,
	.trans     = trans_group_udfix
};

static SymData
sd_eofbepos_16 = {
	.filename  = "sd_eofbepos_16",
	.coord     = &coord_eofbepos,
	.sym_coord = &coord_eofbepos_sym16,
	.ntrans    = 16,
	.trans     = trans_group_udfix
};

static int nsymdata = 2;
static SymData * all_sd[] = {
	&sd_cp_16,
	&sd_eofbepos_16,
};


/* Coordinates and their implementation **************************************/

Coordinate
coord_eofbepos_sym16 = {
	.index  = index_eofbepos_sym16,
	.cube   = antindex_eofbepos_sym16,
};

Coordinate
coord_cp_sym16 = {
	.index  = index_cp_sym16,
	.cube   = antindex_cp_sym16,
};

Coordinate
coord_drud_sym16 = {
	.index  = index_drud_sym16,
	.cube   = antindex_drud_sym16,
	.max    = POW3TO7 * CLASSES_EOFBEPOS_16,
	.trans  = transfinder_drud_sym16,
};

Coordinate
coord_drudfin_noE_sym16 = {
	.index  = index_drudfin_noE_sym16,
	.cube   = antindex_drudfin_noE_sym16,
	.max    = FACTORIAL8 * CLASSES_CP_16,
	.trans  = transfinder_drudfin_noE_sym16,
};

/*
Coordinate
coord_khuge = {
	.index  = index_khuge,
	.cube   = antindex_khuge,
	.max    = POW3TO7 * FACTORIAL4 * CLASSES_EOFBEPOS_16,
	.trans  = transfinder_khuge,
};
*/

Coordinate
coord_nxopt31 = {
	.index  = index_nxopt31,
	.cube   = antindex_nxopt31,
	.max    = POW3TO7 * BINOM8ON4 * CLASSES_EOFBEPOS_16 ,
	.trans  = transfinder_nxopt31,
};

/* Functions *****************************************************************/

static Cube
antindex_cp_sym16(uint64_t ind)
{
	return sd_cp_16.rep[ind];
}

static Cube
antindex_eofbepos_sym16(uint64_t ind)
{
	return sd_eofbepos_16.rep[ind];
}

static Cube
antindex_drud_sym16(uint64_t ind)
{
	Cube c;

	c = antindex_eofbepos_sym16(ind/POW3TO7);
	c.coud = ind % POW3TO7;
	c.cofb = c.coud;
	c.corl = c.coud;

	return c;
}

static Cube
antindex_drudfin_noE_sym16(uint64_t ind)
{
	Cube c1, c2;

	c1 = coord_epud.cube(ind % FACTORIAL8);
	c2 = antindex_cp_sym16(ind/FACTORIAL8);
	c1.cp = c2.cp;

	return c1;
}

/*
static Cube
antindex_khuge(uint64_t ind)
{
	Cube c;

	c = antindex_eofbepos_sym16(ind/(FACTORIAL4*POW3TO7));
	c.epose = ((c.epose / 24) * 24) + ((ind/POW3TO7) % 24);
	c.coud = ind % POW3TO7;

	return c;
}
*/

static Cube
antindex_nxopt31(uint64_t ind)
{
	Cube c;

	c = antindex_eofbepos_sym16(ind/(BINOM8ON4*POW3TO7));
	c.cp = coord_cpud_separate.cube(ind % BINOM8ON4).cp;
	c.coud = (ind / BINOM8ON4) % POW3TO7;

	return c;
}

static uint64_t
index_cp_sym16(Cube cube)
{
	return sd_cp_16.class[coord_cp.index(cube)];
}

static uint64_t
index_drud_sym16(Cube cube)
{
	Trans t;
	Cube c;

	t = sd_eofbepos_16.transtorep[coord_eofbepos.index(cube)];
	c = apply_trans(t, cube);

	return index_eofbepos_sym16(c) * POW3TO7 + c.coud;
}

static uint64_t
index_drudfin_noE_sym16(Cube cube)
{
	Trans t;
	Cube c;

	t = sd_cp_16.transtorep[coord_cp.index(cube)];
	c = apply_trans(t, cube);

	return index_cp_sym16(c) * FACTORIAL8 + coord_epud.index(c);
}

static uint64_t
index_eofbepos_sym16(Cube cube)
{
	return sd_eofbepos_16.class[coord_eofbepos.index(cube)];
}

/*
static uint64_t
index_khuge(Cube cube)
{
	Trans t;
	Cube c;
	uint64_t a;

	t = sd_eofbepos_16.transtorep[coord_eofbepos.index(cube)];
	c = apply_trans(t, cube);
	a = (index_eofbepos_sym16(c) * 24) + (c.epose % 24);

	return a * POW3TO7 + c.coud;
}
*/

static uint64_t
index_nxopt31(Cube cube)
{
	Trans t;
	Cube c;
	uint64_t a;

	t = sd_eofbepos_16.transtorep[coord_eofbepos.index(cube)];
	c = apply_trans(t, cube);
	a = (index_eofbepos_sym16(c)*POW3TO7) + c.coud;

	return a * BINOM8ON4 + coord_cpud_separate.index(c);
}

static int
transfinder_drud_sym16(uint64_t ind, Trans *ret)
{
	uint64_t i, trueind;
	int j;
	static bool initialized = false;
	static int naux[CLASSES_EOFBEPOS_16];
	static Trans retaux[CLASSES_EOFBEPOS_16][NTRANS];

	if (!initialized) {
		for (i = 0; i < CLASSES_EOFBEPOS_16; i++)
			naux[i] = selfsims(&sd_eofbepos_16, i, retaux[i]);

		initialized = true;
	}

	trueind = ind/POW3TO7;
	for (j = 0; j < naux[trueind]; j++)
		ret[j] = retaux[trueind][j];
	return naux[trueind];
}

static int
transfinder_drudfin_noE_sym16(uint64_t ind, Trans *ret)
{
	uint64_t i, trueind;
	int j;
	static bool initialized = false;
	static int naux[CLASSES_CP_16];
	static Trans retaux[CLASSES_CP_16][NTRANS];

	if (!initialized) {
		for (i = 0; i < CLASSES_CP_16; i++)
			naux[i] = selfsims(&sd_cp_16, i, retaux[i]);

		initialized = true;
	}

	trueind = ind/FACTORIAL8;
	for (j = 0; j < naux[trueind]; j++)
		ret[j] = retaux[trueind][j];
	return naux[trueind];
}

/*
static int
transfinder_khuge(uint64_t ind, Trans *ret)
{
	uint64_t i, trueind;
	int j;
	static bool initialized = false;
	static int naux[CLASSES_EOFBEPOS_16];
	static Trans retaux[CLASSES_EOFBEPOS_16][NTRANS];

	if (!initialized) {
		for (i = 0; i < CLASSES_EOFBEPOS_16; i++)
			naux[i] = selfsims(&sd_eofbepos_16, i, retaux[i]);

		initialized = true;
	}

	trueind = ind/(FACTORIAL4*POW3TO7);
	for (j = 0; j < naux[trueind]; j++)
		ret[j] = retaux[trueind][j];
	return naux[trueind];
}
*/

static int
transfinder_nxopt31(uint64_t ind, Trans *ret)
{
	uint64_t i, trueind;
	int j;
	static bool initialized = false;
	static int naux[CLASSES_EOFBEPOS_16];
	static Trans retaux[CLASSES_EOFBEPOS_16][NTRANS];

	if (!initialized) {
		for (i = 0; i < CLASSES_EOFBEPOS_16; i++)
			naux[i] = selfsims(&sd_eofbepos_16, i, retaux[i]);

		initialized = true;
	}

	trueind = ind/(BINOM8ON4*POW3TO7);
	for (j = 0; j < naux[trueind]; j++)
		ret[j] = retaux[trueind][j];
	return naux[trueind];
}

/* Other functions ***********************************************************/

static void
gensym(SymData *sd)
{
	uint64_t i, in, nreps = 0;
	int j;
	Cube c, d;

	if (sd->generated)
		return;

	sd->class      = malloc(sd->coord->max * sizeof(uint64_t));
	sd->rep        = malloc(sd->coord->max * sizeof(Cube));
	sd->transtorep = malloc(sd->coord->max * sizeof(Trans));

	if (read_symdata_file(sd)) {
		sd->generated = true;
		return;
	}

	fprintf(stderr, "Cannot load %s, generating it\n", sd->filename);

	for (i = 0; i < sd->coord->max; i++)
		sd->class[i] = sd->coord->max + 1;

	for (i = 0; i < sd->coord->max; i++) {
		if (sd->class[i] == sd->coord->max + 1) {
			c = sd->coord->cube(i);
			sd->rep[nreps] = c;
			for (j = 0; j < sd->ntrans; j++) {
				d = apply_trans(sd->trans[j], c);
				in = sd->coord->index(d);

				if (sd->class[in] == sd->coord->max + 1) {
					sd->class[in] = nreps;
					sd->transtorep[in] =
						inverse_trans(sd->trans[j]);
				}
			}
			nreps++;
		}
	}

	sd->sym_coord->max = nreps;
	sd->rep            = realloc(sd->rep, nreps * sizeof(Cube));
	sd->generated      = true;

	fprintf(stderr, "Found %" PRIu64 " classes\n", nreps);

	if (!write_symdata_file(sd))
		fprintf(stderr, "Error writing SymData file\n");

	return;
}

static bool
read_symdata_file(SymData *sd)
{
	init_env();

	FILE *f;
	char fname[strlen(tabledir)+100];
	uint64_t n = sd->coord->max, *sn = &sd->sym_coord->max;
	bool r = true;

	strcpy(fname, tabledir);
	strcat(fname, "/");
	strcat(fname, sd->filename);

	if ((f = fopen(fname, "rb")) == NULL)
		return false;

	r = r && fread(&sd->sym_coord->max, sizeof(uint64_t), 1,   f) == 1;
	r = r && fread(sd->rep,             sizeof(Cube),     *sn, f) == *sn;
	r = r && fread(sd->class,           sizeof(uint64_t), n,   f) == n;
	r = r && fread(sd->transtorep,      sizeof(Trans),    n,   f) == n;

	fclose(f);
	return r;
}

static int
selfsims(SymData *sd, uint64_t ind, Trans *ret)
{
	Cube cube, tcube;
	int i, n;
	uint64_t indnosym;

	cube = sd->sym_coord->cube(ind);
	indnosym = sd->coord->index(cube);
	n = 0;
	for (i = 0; i < sd->ntrans; i++) {
		tcube = apply_trans(sd->trans[i], cube);
		if (sd->coord->index(tcube) == indnosym)
			ret[n++] = sd->trans[i];
	}

	return n;
}

static bool
write_symdata_file(SymData *sd)
{
	init_env();

	FILE *f;
	char fname[strlen(tabledir)+100];
	uint64_t n = sd->coord->max, *sn = &sd->sym_coord->max;
	bool r = true;

	strcpy(fname, tabledir);
	strcat(fname, "/");
	strcat(fname, sd->filename);

	if ((f = fopen(fname, "wb")) == NULL)
		return false;

	r = r && fwrite(&sd->sym_coord->max, sizeof(uint64_t), 1,   f) == 1;
	r = r && fwrite(sd->rep,             sizeof(Cube),     *sn, f) == *sn;
	r = r && fwrite(sd->class,           sizeof(uint64_t), n,   f) == n;
	r = r && fwrite(sd->transtorep,      sizeof(Trans),    n,   f) == n;

	fclose(f);
	return r;
}

void
init_symcoord()
{
	int i;

	static bool initialized = false;
	if (initialized)
		return;
	initialized = true;

	init_coord();

	for (i = 0; i < nsymdata; i++)
		gensym(all_sd[i]);
}

Generated with cgit - Back to sebastiano.tronto.net