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
|
/*
* symmetry.c
*
* Symmetry
*
* (c) 2006-2010 Thomas White <taw@physics.org>
*
* Part of CrystFEL - crystallography with a FEL
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "utils.h"
#ifdef DEBUG
#define SYM_DEBUG STATUS
#else /* DEBUG */
#define SYM_DEBUG(...)
#endif /* DEBUG */
/* Check if a reflection is in the asymmetric unit cell */
static int check_cond(signed int h, signed int k, signed int l, const char *sym)
{
if ( strcmp(sym, "1") == 0 )
return ( 1 );
if ( strcmp(sym, "-1") == 0 )
return ( 1 );
if ( strcmp(sym, "6") == 0 )
return ( ((h>0) && (k>=0)) || ((h==0) && (k==0)) );
if ( strcmp(sym, "6/m") == 0 )
return ( (((h>0) && (k>=0)) || ((h==0) && (k==0))) && (l>=0) );
if ( strcmp(sym, "6/mmm") == 0 )
return ( (((h>0) && (k>=0)) || ((h==0) && (k==0))) && (l>=0)
&& (h>=k) );
/* TODO: Add more groups here */
return 1;
}
/* Macros for checking the above conditions and returning if satisfied */
#define CHECK_COND(h, k, l, sym) \
if ( check_cond((h), (k), (l), (sym)) ) { \
*hp = (h); *kp = (k); *lp = (l); \
return; \
}
int num_equivs(signed int h, signed int k, signed int l, const char *sym)
{
/* 000 is always unique */
if ( (h==0) && (k==0) && (l==0) ) return 1;
if ( strcmp(sym, "1") == 0 ) return 1;
if ( strcmp(sym, "-1") == 0 ) return 2;
if ( strcmp(sym, "6") == 0 ) {
if ( (h==0) && (k==0) ) return 1; /* a */
return 6; /* b */
}
if ( strcmp(sym, "6/m") == 0 ) {
if ( (h==0) && (k==0) ) return 2; /* a */
if ( l == 0 ) return 6; /* b */
return 12; /* c */
}
if ( strcmp(sym, "6/mmm") == 0 ) {
if ( (h==0) && (k==0) ) return 2; /* a */
if ( (l==0) && (h==k) ) return 6; /* b */
if ( (l==0) && (k==0) ) return 6; /* c */
if ( h == k ) return 12; /* d */
if ( k == 0 ) return 12; /* e */
if ( l == 0 ) return 12; /* f */
return 24; /* g */
}
/* TODO: Add more groups here */
return 1;
}
void get_equiv(signed int h, signed int k, signed int l,
signed int *he, signed int *ke, signed int *le,
const char *sym, int idx)
{
signed int i = -h-k;
if ( strcmp(sym, "1") == 0 ) {
*he = h; *ke = k; *le = l; return;
}
if ( strcmp(sym, "-1") == 0 ) {
switch ( idx ) {
case 0 : *he = h; *ke = k; *le = l; return;
case 1 : *he = -h; *ke = -k; *le = -l; return;
}
}
if ( strcmp(sym, "6") == 0 ) {
/* a */
if ( (h==0) && (k==0) ) {
switch ( idx ) {
case 0 : *he = 0; *ke = 0; *le = l; return;
}
}
/* b */
switch ( idx ) {
case 0 : *he = h; *ke = k; *le = l; return;
case 1 : *he = i; *ke = h; *le = l; return;
case 2 : *he = k; *ke = i; *le = l; return;
case 3 : *he = -h; *ke = -k; *le = l; return;
case 4 : *he = -i; *ke = -h; *le = l; return;
case 5 : *he = -k; *ke = -i; *le = l; return;
}
}
if ( strcmp(sym, "6/m") == 0 ) {
/* a */
if ( (h==0) && (k==0) ) {
switch ( idx ) {
case 0 : *he = 0; *ke = 0; *le = l; return;
case 1 : *he = 0; *ke = 0; *le = -l; return;
}
}
/* b-c */
switch ( idx ) {
case 0 : *he = h; *ke = k; *le = l; return;
case 1 : *he = i; *ke = h; *le = l; return;
case 2 : *he = k; *ke = i; *le = l; return;
case 3 : *he = -h; *ke = -k; *le = l; return;
case 4 : *he = -i; *ke = -h; *le = l; return;
case 5 : *he = -k; *ke = -i; *le = l; return;
case 6 : *he = h; *ke = k; *le = -l; return;
case 7 : *he = i; *ke = h; *le = -l; return;
case 8 : *he = k; *ke = i; *le = -l; return;
case 9 : *he = -h; *ke = -k; *le = -l; return;
case 10 : *he = -i; *ke = -h; *le = -l; return;
case 11 : *he = -k; *ke = -i; *le = -l; return;
}
}
if ( strcmp(sym, "6/mmm") == 0 ) {
/* a */
if ( (h==0) && (k==0) ) {
switch ( idx ) {
case 0 : *he = 0; *ke = 0; *le = l; return;
case 1 : *he = 0; *ke = 0; *le = -l; return;
}
}
/* b-g */
switch ( idx ) {
case 0 : *he = h; *ke = k; *le = l; return;
case 1 : *he = i; *ke = h; *le = l; return;
case 2 : *he = k; *ke = i; *le = l; return;
case 3 : *he = -h; *ke = -k; *le = l; return;
case 4 : *he = -i; *ke = -h; *le = l; return;
case 5 : *he = -k; *ke = -i; *le = l; return;
case 6 : *he = k; *ke = h; *le = -l; return;
case 7 : *he = h; *ke = i; *le = -l; return;
case 8 : *he = i; *ke = k; *le = -l; return;
case 9 : *he = -k; *ke = -h; *le = -l; return;
case 10 : *he = -h; *ke = -i; *le = -l; return;
case 11 : *he = -i; *ke = -k; *le = -l; return;
case 12 : *he = -h; *ke = -k; *le = -l; return;
case 13 : *he = -i; *ke = -h; *le = -l; return;
case 14 : *he = -k; *ke = -i; *le = -l; return;
case 15 : *he = h; *ke = k; *le = -l; return;
case 16 : *he = i; *ke = h; *le = -l; return;
case 17 : *he = k; *ke = i; *le = -l; return;
case 18 : *he = -k; *ke = -h; *le = l; return;
case 19 : *he = -h; *ke = -i; *le = l; return;
case 20 : *he = -i; *ke = -k; *le = l; return;
case 21 : *he = k; *ke = h; *le = l; return;
case 22 : *he = h; *ke = i; *le = l; return;
case 23 : *he = i; *ke = k; *le = l; return;
}
}
/* TODO: Add more groups here */
ERROR("Unrecognised symmetry '%s'\n", sym);
abort();
}
void get_asymm(signed int h, signed int k, signed int l,
signed int *hp, signed int *kp, signed int *lp,
const char *sym)
{
int nequiv = num_equivs(h, k, l, sym);
int p;
SYM_DEBUG("------ %i %i %i\n", h, k, l);
for ( p=0; p<nequiv; p++ ) {
signed int he, ke, le;
get_equiv(h, k, l, &he, &ke, &le, sym, p);
SYM_DEBUG("%i : %i %i %i\n", p, he, ke, le);
CHECK_COND(he, ke, le, sym);
}
/* Should never reach here */
ERROR("No match found in %s for %i %i %i\n", sym, h, k, l);
abort();
}
static const char *get_holohedral(const char *sym)
{
/* Triclinic */
if ( strcmp(sym, "1") == 0 ) return "-1";
if ( strcmp(sym, "1") == 0 ) return "-1";
/* Hexagonal */
if ( strcmp(sym, "6") == 0 ) return "6/m";
if ( strcmp(sym, "6/m") == 0 ) return "6/mmm";
if ( strcmp(sym, "6/mmm") == 0 ) return "6/mmm";
/* TODO: Add more groups here */
ERROR("Couldn't find holohedral point group for '%s'\n", sym);
abort();
}
/* This is kind of like a "numerical" left coset decomposition.
* Given a reflection index and a point group, it returns the "idx-th"
* twinning possibility for the reflection. It returns "idx" if
* successful. To just count the number of possibilities, set idx=-1.
*
* The sequence of operators producing each possibility is guaranteed to
* be the same for any choice of indices given the same point group. */
static int coset_decomp(signed int hs, signed int ks, signed int ls,
signed int *hp, signed int *kp, signed int *lp,
const char *mero, signed int idx)
{
const char *holo = get_holohedral(mero);
int n_mero, n_holo;
int i;
signed int n_twins = 1;
signed int h, k, l;
ReflItemList *twins;
twins = new_items();
if ( idx == 0 ) {
/* Twin index zero is always the original orientation */
*hp = hs; *kp = ks; *lp = ls;
return 0;
}
get_asymm(hs, ks, ls, &h, &k, &l, mero);
/* How many equivalents in the holohedral point group are not
* equivalents according to the (possibly) merohedral group? */
n_holo = num_equivs(h, k, l, holo);
n_mero = num_equivs(h, k, l, mero);
for ( i=0; i<n_holo; i++ ) {
signed int h_holo, k_holo, l_holo;
signed int hs_holo, ks_holo, ls_holo;
/* Get equivalent according to the holohedral group */
get_equiv(h, k, l, &hs_holo, &ks_holo, &ls_holo, holo, i);
/* Put it into the asymmetric cell for the merohedral group */
get_asymm(hs_holo, ks_holo, ls_holo,
&h_holo, &k_holo, &l_holo, mero);
/* Is this the same reflection as we started with?
* If not, this reflection is 'equivalent by twinning' */
if ( (h_holo != h) || (k_holo != k) || (l_holo != l) ) {
if ( find_item(twins, h_holo, k_holo, l_holo) )
continue;
if ( n_twins == idx ) {
*hp = h_holo;
*kp = k_holo;
*lp = l_holo;
delete_items(twins);
return n_twins;
}
add_item(twins, h_holo, k_holo, l_holo);
n_twins++;
}
}
delete_items(twins);
return n_twins;
}
/* Get the number of twinned "equivalents" for this reflection */
int num_twins(signed int h, signed int k, signed int l, const char *sym)
{
return coset_decomp(h, k, l, NULL, NULL, NULL, sym, -1);
}
void get_twins(signed int h, signed int k, signed int l,
signed int *hp, signed int *kp, signed int *lp,
const char *sym, int idx)
{
if ( coset_decomp(h, k, l, hp, kp, lp, sym, idx) != idx ) {
ERROR("Failed coset decomposition for %i %i %i in %s\n",
h, k, l, sym);
abort();
}
}
|