-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhombi.c
2480 lines (2173 loc) · 67.4 KB
/
rhombi.c
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include "mt19937.h"
/* Parameters */
#define pi 3.14159265358979323846
#define hr3 0.86602540378443864676
#define lambda 3.73205080756887729352
#define MAX_HAIR_LENGHT 10
#define LAT_SIZE_MAX 60000
#define NNLIST_SIZE 512
#define NNDEPTH 6 // should (at least) match biggest n-gon in the tiles
#define MAXERR 100
#define STABLE_EPS_RH 1.0
#define STABLE_GAMMA 4.0
#define ANNEALING_SWEEPS 6000000
// #define PROFILING
#define beta 1.0
// #define D 1
// Simulation state variables
int st_I = 0;
int st_D = 10;
double eps_gamma = 3.0;
double eps_sq = 0.0;
double eps_tr = 0;
double eps_rh = -0.75;
// Simulation runtime variables
int MCsweeps = 24000000; // Total number of sweeps
int output_interval = 15000; // writing interval
char datadir[256];
char measurementfilename[256];
int top = -1, old_top = -1, backup_top = -1;
int errcount = 0;
void myprintf(const char *format, ...)
{
#ifndef PROFILING
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
#endif
}
void myfprintf(FILE *f, const char *format, ...)
{
#ifndef PROFILING
va_list args;
va_start(args, format);
vfprintf(f, format, args);
va_end(args);
#endif
}
void init_datadir(void)
{
sprintf(datadir, "data_size_%d_%d_gamma_%lf_epsRh_%lf", st_D, st_I, eps_gamma, eps_rh);
if (mkdir(datadir, S_IRWXU | S_IRWXG | S_IROTH) == -1)
{
myprintf("Error: %s\n", strerror(errno));
}
}
void parse_cli(int argc, char *argv[])
{
// return;
if (argc != 5)
{
myprintf("Usage:\n ./rhombi <st_D> <st_I> <gamma> <eps_rh>\n");
exit(0);
}
st_D = atoi(argv[1]);
st_I = atoi(argv[2]);
eps_gamma = atof(argv[3]);
eps_rh = atof(argv[4]);
}
/* Data structures */
typedef struct double2_s
{
double x, y;
} double2;
typedef struct int2_s
{
int x, y;
} int2;
typedef struct int4_s
{
int n0, n1, n2, n3;
} int4;
typedef struct vertex_s
{
int index;
double2 par;
double2 perp;
int n0, n1, n2, n3;
int bonds[12];
} vertex;
typedef struct empty_house_s
{
/*
* tip
* / \
* / \
* lm-----rm
* | |
* | |
* lb---rb
*/
int tip, lm, rm, lb, rb;
int dir_lm_rm;
} empty_house;
typedef struct full_house_s
{
/*
* tip
* /|\
* / | \
* lm o rm
* | / \ |
* |/ \|
* lb---rb
*/
int tip, lm, rm, lb, rb, o;
int dir_o_tip;
} full_house;
/* Constants */
const double2 unit_par[4] = {{1, 0}, {hr3, .5}, {.5, hr3}, {0, 1}};
const double2 unit_perp[4] = {{1, 0}, {-hr3, -.5}, {.5, hr3}, {0, -1}};
const int4 dodecagon_vertices[18] = {
#include "dodecagon_vertices.txt"
};
const int4 unit_vecs[12] = {
#include "unit_vecs.txt"
};
/* Variables */
int N = 0;
int boundary_length = 0;
int new_boundary_length = 0;
int old_boundary_length = 0;
int backup_boundary_length = 0;
int n_sq, n_tr, n_rh;
int old_n_sq, old_n_tr, old_n_rh;
int backup_n_sq, backup_n_tr, backup_n_rh;
int sweep;
int shapes[3] = {0, 0, 0}; // squares, triangles, rhombi
int old_shapes[3] = {0, 0, 0}; // squares, triangles, rhombi
int backup_shapes[3] = {0, 0, 0}; // squares, triangles, rhombi
int shapes_1[3] = {0, 0, 0};
int old_shapes_1[3] = {0, 0, 0};
int backup_shapes_1[3] = {0, 0, 0};
int hist[12];
int countall = 0;
vertex lattice[LAT_SIZE_MAX];
vertex oldlattice[LAT_SIZE_MAX];
vertex backup_lattice[LAT_SIZE_MAX];
empty_house *empty_houses;
int n_empty_houses = 0;
full_house *full_houses;
int n_full_houses = 0;
// Given three collinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
int onSegment(double2 p, double2 q, double2 r)
{
if (q.x <= fmax(p.x, r.x) && q.x >= fmin(p.x, r.x) &&
q.y <= fmax(p.y, r.y) && q.y >= fmin(p.y, r.y))
return 1;
return 0;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are collinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int orientation(double2 p, double2 q, double2 r)
{
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0)
return 0; // collinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
// The main function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
int doIntersect(double2 p1, double2 q1, double2 p2, double2 q2)
{
// Find the four orientations needed for general and
// special cases
int o1 = orientation(p1, q1, p2);
int o2 = orientation(p1, q1, q2);
int o3 = orientation(p2, q2, p1);
int o4 = orientation(p2, q2, q1);
// General case
if (o1 != o2 && o3 != o4)
return 1;
// Special Cases
// p1, q1 and p2 are collinear and p2 lies on segment p1q1
if (o1 == 0 && onSegment(p1, p2, q1))
return 1;
// p1, q1 and q2 are collinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1))
return 1;
// p2, q2 and p1 are collinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2))
return 1;
// p2, q2 and q1 are collinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2))
return 1;
return 0; // Doesn't fall in any of the above cases
}
void write_phase_state(void)
{
char statefile[256];
sprintf(statefile, "%s/state.csv", datadir);
FILE *f = fopen(statefile, "w");
myfprintf(f, "#N, D, I, gamma, eps_sq, eps_tr, eps_rh\n%d,%d,%d,%.10e,%.10e,%.10e,%.10e\n", N, st_D, st_I,
eps_gamma, eps_sq, eps_tr, eps_rh);
fclose(f);
}
void verify_lattice(void)
{
for (int i = 0; i < N; i++)
{
for (int n = 0; n < 12; n++)
{
int j = lattice[i].bonds[n];
if (j == -1)
{
continue;
}
assert(lattice[j].bonds[(n + 6) % 12] == i);
}
}
}
void write_list(int id, int list[NNLIST_SIZE], int len)
{
char fname[256];
sprintf(fname, "%s/nnlist_%09d.dat", datadir, id);
FILE *f = fopen(fname, "w");
for (int i = 0; i < len; i++)
{
int n = list[i];
myfprintf(f, "%d %lf %lf\n", n, lattice[n].par.x, lattice[n].par.y);
}
fclose(f);
}
int is_member(int idx, int list[NNLIST_SIZE], int len)
{
for (int i = 0; i < len; i++)
{
if (list[i] == idx)
{
return 1;
}
}
return 0;
}
// Prints BFS traversal from a given source s
void Graph_BFS(int s, int max_dept, int visited[NNLIST_SIZE], int *len_visited)
{
// Create a queue for BFS
int level = 0;
int queue[NNLIST_SIZE];
uint8_t *visited_lut = calloc(N, sizeof(uint8_t));
int front = 0, rear = 0;
// double dist = sqrt(dist2);
// Mark the current node as visited and enqueue it
assert(*len_visited < NNLIST_SIZE);
visited[(*len_visited)++] = s;
visited_lut[s] = 1;
queue[rear++] = s;
while (front != rear)
{
int level_size = rear - front;
for (int _i = 0; _i < level_size; _i++)
{
// Dequeue a vertex from queue and print it
int ss = queue[front++];
// Get all adjacent vertices of the dequeued
// vertex s.
// If an adjacent has not been visited,
// then mark it visited and enqueue it
for (int adjacent = 0; adjacent < 12;
adjacent++)
{
int v = lattice[ss].bonds[adjacent];
if (v == -1)
{
continue;
}
int not_visited = 1 - visited_lut[v];
if (not_visited)
{
visited[(*len_visited)++] = v;
visited_lut[v] = 1;
if (rear < NNLIST_SIZE)
{
queue[rear++] = v;
}
}
if (*len_visited >= NNLIST_SIZE)
{
free(visited_lut);
return;
}
}
}
level++;
if (level == max_dept)
{
break;
}
}
free(visited_lut);
}
void makeHist(void)
{
for (int i = 0; i < 12; i++)
hist[i] = 0;
for (int n = 0; n < N; n++)
{
for (int i = 0; i < 12; i++)
{
if (lattice[n].bonds[i] > -1)
hist[i]++;
}
}
}
void init_measurement_file(void)
{
sprintf(measurementfilename, "%s/measurements.csv", datadir);
FILE *f = fopen(measurementfilename, "w");
myfprintf(f, "#MCSweep, n_square, n_triangle, n_rhombus, boundary_length, n_e1, n_e2, n_e3, n_e4, n_e5, n_e6, n_e7, n_e8, n_e9, n_e10, n_e11, n_e12\n");
fclose(f);
}
void write_measurement(int sweep)
{
FILE *f = fopen(measurementfilename, "a");
myfprintf(f, "%d,%d,%d,%d,%d", sweep, n_sq, n_tr, n_rh, boundary_length);
makeHist();
for (int i = 0; i < 12; i++)
{
myfprintf(f, ",%d", hist[i]);
}
myfprintf(f, "\n");
fclose(f);
}
void save_state(void)
{
old_boundary_length = boundary_length;
old_n_sq = n_sq;
old_n_tr = n_tr;
old_n_rh = n_rh;
old_top = top;
memcpy(old_shapes, shapes, 3 * sizeof(int));
memcpy(old_shapes_1, shapes_1, 3 * sizeof(int));
memcpy(oldlattice, lattice, N * sizeof(vertex));
}
void revert_state(void)
{
boundary_length = old_boundary_length;
n_sq = old_n_sq;
n_tr = old_n_tr;
n_rh = old_n_rh;
top = old_top;
memcpy(shapes, old_shapes, 3 * sizeof(int));
memcpy(shapes_1, old_shapes_1, 3 * sizeof(int));
memcpy(lattice, oldlattice, N * sizeof(vertex));
}
void revert_state_from_list(int list[NNLIST_SIZE], int len)
{
boundary_length = old_boundary_length;
n_sq = old_n_sq;
n_tr = old_n_tr;
n_rh = old_n_rh;
top = old_top;
memcpy(shapes, old_shapes, 3 * sizeof(int));
memcpy(shapes_1, old_shapes_1, 3 * sizeof(int));
for (int i = 0; i < len; i++)
{
int n = list[i];
memcpy(&lattice[n], &oldlattice[n], sizeof(vertex));
}
}
void save_backup_state(void)
{
backup_boundary_length = boundary_length;
backup_n_sq = n_sq;
backup_n_tr = n_tr;
backup_n_rh = n_rh;
backup_top = top;
memcpy(backup_shapes, shapes, 3 * sizeof(int));
memcpy(backup_shapes_1, shapes_1, 3 * sizeof(int));
memcpy(backup_lattice, lattice, N * sizeof(vertex));
}
void revert_backup_state(void)
{
boundary_length = backup_boundary_length;
n_sq = backup_n_sq;
n_tr = backup_n_tr;
n_rh = backup_n_rh;
top = backup_top;
memcpy(shapes, backup_shapes, 3 * sizeof(int));
memcpy(shapes_1, backup_shapes_1, 3 * sizeof(int));
memcpy(lattice, backup_lattice, N * sizeof(vertex));
}
void calculateParCoords(vertex *v)
{
v->par.x = v->n0 * unit_par[0].x + v->n1 * unit_par[1].x + v->n2 * unit_par[2].x + v->n3 * unit_par[3].x;
v->par.y = v->n0 * unit_par[0].y + v->n1 * unit_par[1].y + v->n2 * unit_par[2].y + v->n3 * unit_par[3].y;
return;
}
void calculatePerpCoords(vertex *v)
{
v->perp.x = v->n0 * unit_perp[0].x + v->n1 * unit_perp[1].x + v->n2 * unit_perp[2].x + v->n3 * unit_perp[3].x;
v->perp.y = v->n0 * unit_perp[0].y + v->n1 * unit_perp[1].y + v->n2 * unit_perp[2].y + v->n3 * unit_perp[3].y;
return;
}
/* Functions */
void writeLattice(int step)
{
char datafile1[128];
// For saving steps seperately
sprintf(datafile1, "%s/lattice%09d.dat", datadir, step);
FILE *data1 = fopen(datafile1, "w");
// // Format:
// (1)index (2)par-space coords (2)perp-space coords (4)4-space coords (12)neighbour indices
for (int n = 0; n < N; n++)
{
calculatePerpCoords(&lattice[n]);
myfprintf(data1, "%d %lf %lf %lf %lf %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", n, lattice[n].par.x, lattice[n].par.y, lattice[n].perp.x, lattice[n].perp.y, lattice[n].n0, lattice[n].n1, lattice[n].n2, lattice[n].n3, lattice[n].bonds[0], lattice[n].bonds[1], lattice[n].bonds[2], lattice[n].bonds[3], lattice[n].bonds[4], lattice[n].bonds[5], lattice[n].bonds[6], lattice[n].bonds[7], lattice[n].bonds[8], lattice[n].bonds[9], lattice[n].bonds[10], lattice[n].bonds[11]);
}
// fclose(data);
fclose(data1);
}
void writeoldLattice(int step)
{
char datafile1[128];
// For saving steps seperately
sprintf(datafile1, "%s/lattice%09d.dat", datadir, step);
FILE *data1 = fopen(datafile1, "w");
// // Format:
// (1)index (2)par-space coords (2)perp-space coords (4)4-space coords (12)neighbour indices
for (int n = 0; n < N; n++)
{
calculatePerpCoords(&oldlattice[n]);
myfprintf(data1, "%d %lf %lf %lf %lf %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", n, oldlattice[n].par.x, oldlattice[n].par.y, oldlattice[n].perp.x, oldlattice[n].perp.y, oldlattice[n].n0, oldlattice[n].n1, oldlattice[n].n2, oldlattice[n].n3, oldlattice[n].bonds[0], oldlattice[n].bonds[1], oldlattice[n].bonds[2], oldlattice[n].bonds[3], oldlattice[n].bonds[4], oldlattice[n].bonds[5], oldlattice[n].bonds[6], oldlattice[n].bonds[7], oldlattice[n].bonds[8], oldlattice[n].bonds[9], oldlattice[n].bonds[10], oldlattice[n].bonds[11]);
}
// fclose(data);
fclose(data1);
}
void genLattice(void)
{
// Generate a D x D square grid of vertices
for (int n = 0; n < st_D * st_D; n++)
{
int i = n % st_D;
int j = n / st_D;
lattice[n].n0 = i;
lattice[n].n1 = 0;
lattice[n].n2 = 0;
lattice[n].n3 = j;
lattice[n].index = n;
}
N += st_D * st_D;
return;
}
void genChevronLattice(void)
{
for (int i = 0; i < st_D; i++)
{
for (int j = 0; j < st_D; j++)
{
int n = i * st_D + j;
lattice[2 * n].n0 = i;
lattice[2 * n].n1 = 0;
lattice[2 * n].n2 = 0;
lattice[2 * n].n3 = j;
lattice[2 * n].index = 2 * n;
lattice[2 * n + 1].n0 = i;
lattice[2 * n + 1].n1 = 1;
lattice[2 * n + 1].n2 = 0;
lattice[2 * n + 1].n3 = j;
lattice[2 * n + 1].index = 2 * n + 1;
}
}
N = 2 * st_D * st_D;
return;
}
void genDodecagon1(vertex center)
{
// This generates a dodecagonal wheel of vertices around the vertex labelled 'center'
int exists;
for (int i = 0; i < 18; i++)
{
exists = 0;
// Check if vertex already exists
// D12vertices gives dodecagon vertex positions relative to center vertex
for (int j = 0; j < N; j++)
{
if (lattice[j].n0 == center.n0 + dodecagon_vertices[i].n0 && lattice[j].n1 == center.n1 + dodecagon_vertices[i].n1)
{
if (lattice[j].n2 == center.n2 + dodecagon_vertices[i].n2 && lattice[j].n3 == center.n3 + dodecagon_vertices[i].n3)
{
exists = 1;
break;
}
}
}
// Don't add it if it already exists
// (Due to neighbouring dodecagon)
if (exists == 1)
continue;
// Add new vertex
lattice[N].n0 = center.n0 + dodecagon_vertices[i].n0;
lattice[N].n1 = center.n1 + dodecagon_vertices[i].n1;
lattice[N].n2 = center.n2 + dodecagon_vertices[i].n2;
lattice[N].n3 = center.n3 + dodecagon_vertices[i].n3;
lattice[N].index = N;
N++;
}
return;
}
void genDodecagon2(vertex center)
{
// This generates a dodecagonal wheel of vertices around the vertex labelled 'center'
int exists;
for (int i = 0; i < 18; i++)
{
exists = 0;
// Check if vertex already exists
// D12vertices gives dodecagon vertex positions relative to center vertex
for (int j = 0; j < N; j++)
{
if (lattice[j].n0 == center.n0 + dodecagon_vertices[i].n3 && lattice[j].n1 == center.n1 + dodecagon_vertices[i].n2)
{
if (lattice[j].n2 == center.n2 + dodecagon_vertices[i].n1 && lattice[j].n3 == center.n3 + dodecagon_vertices[i].n0)
{
exists = 1;
break;
}
}
}
// Don't add it if it already exists
// (Due to neighbouring dodecagon)
if (exists == 1)
continue;
// Add new vertex
lattice[N].n0 = center.n0 + dodecagon_vertices[i].n3;
lattice[N].n1 = center.n1 + dodecagon_vertices[i].n2;
lattice[N].n2 = center.n2 + dodecagon_vertices[i].n1;
lattice[N].n3 = center.n3 + dodecagon_vertices[i].n0;
lattice[N].index = N;
N++;
}
return;
}
void inflateLattice(void)
{
// Performs one step of Stampfli inflation on the existing lattice
int n0, n1, n2, n3;
int max = N;
// Inflate existing vertices
for (int i = 0; i < max; i++)
{
n0 = 2 * lattice[i].n0 + lattice[i].n1 - lattice[i].n3;
n1 = 2 * lattice[i].n0 + 2 * lattice[i].n1 + lattice[i].n2;
n2 = lattice[i].n1 + 2 * lattice[i].n2 + 2 * lattice[i].n3;
n3 = -lattice[i].n0 + lattice[i].n2 + 2 * lattice[i].n3;
lattice[i].n0 = n0;
lattice[i].n1 = n1;
lattice[i].n2 = n2;
lattice[i].n3 = n3;
}
// Replace inflated vertices with dodecagons
for (int i = 0; i < max; i++)
{
if (dsfmt_genrand() < 0.5)
genDodecagon1(lattice[i]);
else
genDodecagon2(lattice[i]);
}
return;
}
void removeBondCross(vertex *v)
{
int n1, n2;
for (int n = 0; n < 12; n++)
{
if ((n1 = v->bonds[n]) != -1)
{
if ((n2 = v->bonds[(n + 2) % 12]) != -1 && lattice[n1].bonds[(n + 5) % 12] != -1)
{
v->bonds[(n + 2) % 12] = -1;
lattice[n2].bonds[(n + 8) % 12] = -1;
}
if ((n2 = v->bonds[(n + 10) % 12]) != -1 && lattice[n1].bonds[(n + 7) % 12] != -1)
{
v->bonds[(n + 10) % 12] = -1;
lattice[n2].bonds[(n + 4) % 12] = -1;
}
}
}
return;
}
int initNeighbours(vertex *vertex_i)
{
/* Finds the neighbours of 'vertex_i'
and stores their indices in the '.bonds list' */
int i;
int n0i, n1i, n2i, n3i;
int n0j, n1j, n2j, n3j;
int equal, diff1, diff2;
vertex *vertex_j;
// Reset neighbour list
for (int n = 0; n < 12; n++)
vertex_i->bonds[n] = -1;
n0i = vertex_i->n0;
n1i = vertex_i->n1;
n2i = vertex_i->n2;
n3i = vertex_i->n3;
for (int j = 0; j < N; j++)
{
i = vertex_i->index;
vertex_j = &lattice[j];
if (i == j)
continue;
n0j = vertex_j->n0;
n1j = vertex_j->n1;
n2j = vertex_j->n2;
n3j = vertex_j->n3;
equal = 0;
if (n0i == n0j)
equal += 1;
if (n1i == n1j)
equal += 10;
if (n2i == n2j)
equal += 100;
if (n3i == n3j)
equal += 1000;
if (equal < 101)
continue;
if (equal == 1111)
return 0;
if (equal == 1110)
{
// n0 is different
diff1 = n0j - n0i;
if (diff1 == 1)
{
vertex_i->bonds[0] = j;
vertex_j->bonds[6] = i;
}
else if (diff1 == -1)
{
vertex_i->bonds[6] = j;
vertex_j->bonds[0] = i;
}
}
else if (equal == 1101)
{
// n1 is different
diff1 = n1j - n1i;
if (diff1 == 1)
{
vertex_i->bonds[1] = j;
vertex_j->bonds[7] = i;
}
else if (diff1 == -1)
{
vertex_i->bonds[7] = j;
vertex_j->bonds[1] = i;
}
}
else if (equal == 1011)
{
// n2 is different
diff1 = n2j - n2i;
if (diff1 == 1)
{
vertex_i->bonds[2] = j;
vertex_j->bonds[8] = i;
}
else if (diff1 == -1)
{
vertex_i->bonds[8] = j;
vertex_j->bonds[2] = i;
}
}
else if (equal == 111)
{
// n3 is different
diff1 = n3j - n3i;
if (diff1 == 1)
{
vertex_i->bonds[3] = j;
vertex_j->bonds[9] = i;
}
else if (diff1 == -1)
{
vertex_i->bonds[9] = j;
vertex_j->bonds[3] = i;
}
}
else if (equal == 1010)
{
// n0 and n2 are different
diff1 = n0j - n0i;
diff2 = n2j - n2i;
if (diff1 == -1 && diff2 == 1)
{
vertex_i->bonds[4] = j;
vertex_j->bonds[10] = i;
}
else if (diff1 == 1 && diff2 == -1)
{
vertex_i->bonds[10] = j;
vertex_j->bonds[4] = i;
}
}
else if (equal == 101)
{
// n1 and n3 are different
diff1 = n1j - n1i;
diff2 = n3j - n3i;
if (diff1 == -1 && diff2 == 1)
{
vertex_i->bonds[5] = j;
vertex_j->bonds[11] = i;
}
else if (diff1 == 1 && diff2 == -1)
{
vertex_i->bonds[11] = j;
vertex_j->bonds[5] = i;
}
}
}
return 0;
}
int findNeighbours_from_list(vertex *vertex_i, int potential_nbs[NNLIST_SIZE], int len)
{
/* Finds the neighbours of 'vertex_i'
and stores their indices in the '.bonds list' */
int i;
int n0i, n1i, n2i, n3i;
int n0j, n1j, n2j, n3j;
int equal, diff1, diff2;
vertex *vertex_j;
// Reset neighbour list
for (int n = 0; n < 12; n++)
{
if (vertex_i->bonds[n] != -1 && is_member(vertex_i->bonds[n], potential_nbs, len))
{
lattice[vertex_i->bonds[n]].bonds[(n + 6) % 12] = -1;
vertex_i->bonds[n] = -1;
}
}
n0i = vertex_i->n0;
n1i = vertex_i->n1;
n2i = vertex_i->n2;
n3i = vertex_i->n3;
// for (int j = 0; j < N; j++)
for (int jj = 0; jj < len; jj++)
{
int j = potential_nbs[jj];
i = vertex_i->index;
if (i == j)
continue;
vertex_j = &lattice[j];
n0j = vertex_j->n0;
n1j = vertex_j->n1;
n2j = vertex_j->n2;
n3j = vertex_j->n3;
equal = 0;
if (n0i == n0j)
equal += 1;
if (n1i == n1j)
equal += 10;
if (n2i == n2j)
equal += 100;
if (n3i == n3j)
equal += 1000;
if (equal < 101)
continue;
if (equal == 1111)
return 0;
if (equal == 1110)
{
// n0 is different
diff1 = n0j - n0i;
if (diff1 == 1)
{
vertex_i->bonds[0] = j;
vertex_j->bonds[6] = i;
}
else if (diff1 == -1)
{
vertex_i->bonds[6] = j;
vertex_j->bonds[0] = i;
}
}
else if (equal == 1101)
{
// n1 is different
diff1 = n1j - n1i;
if (diff1 == 1)
{
vertex_i->bonds[1] = j;
vertex_j->bonds[7] = i;
}
else if (diff1 == -1)
{
vertex_i->bonds[7] = j;
vertex_j->bonds[1] = i;
}
}
else if (equal == 1011)
{
// n2 is different
diff1 = n2j - n2i;
if (diff1 == 1)
{
vertex_i->bonds[2] = j;
vertex_j->bonds[8] = i;
}
else if (diff1 == -1)
{
vertex_i->bonds[8] = j;
vertex_j->bonds[2] = i;
}
}
else if (equal == 111)
{
// n3 is different
diff1 = n3j - n3i;
if (diff1 == 1)
{
vertex_i->bonds[3] = j;
vertex_j->bonds[9] = i;
}
else if (diff1 == -1)
{
vertex_i->bonds[9] = j;
vertex_j->bonds[3] = i;
}
}
else if (equal == 1010)
{
// n0 and n2 are different
diff1 = n0j - n0i;
diff2 = n2j - n2i;
if (diff1 == -1 && diff2 == 1)
{
vertex_i->bonds[4] = j;
vertex_j->bonds[10] = i;
}
else if (diff1 == 1 && diff2 == -1)
{
vertex_i->bonds[10] = j;
vertex_j->bonds[4] = i;
}
}