symcrys 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -6
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/pointGroups/properties.d.ts +8 -3
- package/dist/pointGroups/properties.d.ts.map +1 -1
- package/dist/pointGroups/properties.js +11 -3
- package/dist/pointGroups/properties.js.map +1 -1
- package/dist/siteSymmetry.d.ts +10 -4
- package/dist/siteSymmetry.d.ts.map +1 -1
- package/dist/siteSymmetry.js +83 -16
- package/dist/siteSymmetry.js.map +1 -1
- package/dist/tensors/rowEchelon.d.ts +18 -0
- package/dist/tensors/rowEchelon.d.ts.map +1 -0
- package/dist/tensors/rowEchelon.js +49 -0
- package/dist/tensors/rowEchelon.js.map +1 -0
- package/dist/tensors/tensorForm.d.ts +82 -0
- package/dist/tensors/tensorForm.d.ts.map +1 -0
- package/dist/tensors/tensorForm.js +222 -0
- package/dist/tensors/tensorForm.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -2
- package/src/index.ts +12 -1
- package/src/pointGroups/properties.ts +14 -3
- package/src/siteSymmetry.ts +93 -16
- package/src/tensors/rowEchelon.ts +43 -0
- package/src/tensors/tensorForm.ts +282 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Symmetry-reduced forms of property tensors: which Cartesian components of a tensor of rank 0 to 4
|
|
3
|
+
* a point group allows, and how they depend on each other. Results are plain numbers (coefficient
|
|
4
|
+
* matrices and index lists); formulas, LaTeX and diagrams stay in the applications (CLAUDE.md,
|
|
5
|
+
* "Tensors").
|
|
6
|
+
*
|
|
7
|
+
* Components are numbered by their flat index: for indices (i₁, …, iₙ) with x, y, z = 0, 1, 2 the
|
|
8
|
+
* flat index is i₁·3ⁿ⁻¹ + … + iₙ, so the first index varies slowest (row-major, the order of a
|
|
9
|
+
* NumPy array of shape (3,)*n in C order). Moved from birss-app (`tensorForms.ts`,
|
|
10
|
+
* `tensorProjection.ts`, `linalg.ts`, revision 2be5fe3).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { det, type Mat3 } from '../linalg.js';
|
|
14
|
+
import { closeGroup, type Operation } from '../operations.js';
|
|
15
|
+
import { operations, settings, type PointGroupKey, type SettingOptions } from '../pointGroups/pointGroups.js';
|
|
16
|
+
import { EPSILON, SNAP_EPSILON } from '../tolerances.js';
|
|
17
|
+
import { reducedRowEchelon } from './rowEchelon.js';
|
|
18
|
+
|
|
19
|
+
/** Rank of a tensor: 0 (scalar) to 4. */
|
|
20
|
+
export type TensorRank = 0 | 1 | 2 | 3 | 4;
|
|
21
|
+
|
|
22
|
+
/** `'polar'`: transforms with R; `'axial'`: with det(R)·R (pseudotensor). */
|
|
23
|
+
export type TensorParity = 'polar' | 'axial';
|
|
24
|
+
|
|
25
|
+
/** `'i'`: time-even (invariant under time reversal); `'c'`: time-odd (changes sign). */
|
|
26
|
+
export type TimeParity = 'i' | 'c';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Symmetry of the tensor under permutation of its own indices, independent of the crystal:
|
|
30
|
+
* `'none'`; `'ij'`: the first two indices are interchangeable; `'jk'`: the last two (for rank 3 the
|
|
31
|
+
* index symmetry of second-harmonic generation and the piezoelectric effect); `'ij_kl'` (rank 4):
|
|
32
|
+
* each of the two pairs symmetric, the pairs not interchangeable (e.g. electric-quadrupole SHG,
|
|
33
|
+
* photoelasticity); `'voigt'` (rank 4): `'ij_kl'` and the pairs interchangeable (elastic
|
|
34
|
+
* stiffness). At rank 2, `'ij'` and `'jk'` both mean a symmetric tensor.
|
|
35
|
+
*/
|
|
36
|
+
export type IntrinsicSymmetry = 'none' | 'ij' | 'jk' | 'ij_kl' | 'voigt';
|
|
37
|
+
|
|
38
|
+
/** The kind of tensor, as in birss-app. */
|
|
39
|
+
export interface TensorSpec {
|
|
40
|
+
readonly rank: TensorRank;
|
|
41
|
+
readonly parity: TensorParity;
|
|
42
|
+
readonly timeParity: TimeParity;
|
|
43
|
+
readonly intrinsic: IntrinsicSymmetry;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The form of a tensor under a point group: the subspace of tensors invariant under every
|
|
48
|
+
* operation and with the requested index symmetry. Every entry is a plain number; a vector of
|
|
49
|
+
* components has length 3^rank (flat index, see the module documentation).
|
|
50
|
+
*/
|
|
51
|
+
export interface TensorForm {
|
|
52
|
+
readonly rank: TensorRank;
|
|
53
|
+
/**
|
|
54
|
+
* A basis of the allowed tensors in reduced row echelon form: one row per free parameter, with 1
|
|
55
|
+
* at the parameter's own component and 0 at every other parameter's component. Every allowed
|
|
56
|
+
* tensor is Σₖ pₖ·basis[k], where pₖ is the value of its component `parameters[k]`. Empty if the
|
|
57
|
+
* tensor vanishes.
|
|
58
|
+
*/
|
|
59
|
+
readonly basis: readonly (readonly number[])[];
|
|
60
|
+
/**
|
|
61
|
+
* The free parameters, as the index lists of their components (e.g. `[0, 1, 1]` for xyy), one per
|
|
62
|
+
* basis row and in its order: each is the lowest component independent of those before it.
|
|
63
|
+
*/
|
|
64
|
+
readonly parameters: readonly (readonly number[])[];
|
|
65
|
+
/**
|
|
66
|
+
* The constraints: one row r per component that is not a parameter, in ascending flat index,
|
|
67
|
+
* with r·t = 0 for every allowed tensor t. Row c reads t_c − Σₖ basis[k][c]·t_{parameters[k]} = 0;
|
|
68
|
+
* a row with a single 1 says that the component vanishes.
|
|
69
|
+
*/
|
|
70
|
+
readonly relations: readonly (readonly number[])[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const RANKS: readonly number[] = [0, 1, 2, 3, 4];
|
|
74
|
+
|
|
75
|
+
/** Checks a specification and returns the index permutations that generate its intrinsic symmetry. */
|
|
76
|
+
function checkSpec(spec: TensorSpec): number[][] {
|
|
77
|
+
const { rank, parity, timeParity, intrinsic } = spec;
|
|
78
|
+
if (!RANKS.includes(rank)) throw new RangeError(`Tensor rank must be 0 to 4, not ${JSON.stringify(rank)}`);
|
|
79
|
+
if (parity !== 'polar' && parity !== 'axial')
|
|
80
|
+
throw new RangeError(`Unknown tensor parity: ${JSON.stringify(parity)}`);
|
|
81
|
+
if (timeParity !== 'i' && timeParity !== 'c')
|
|
82
|
+
throw new RangeError(`Unknown time parity: ${JSON.stringify(timeParity)}`);
|
|
83
|
+
const swap = (a: number, b: number) => {
|
|
84
|
+
const p = Array.from({ length: rank }, (_, k) => k);
|
|
85
|
+
[p[a], p[b]] = [p[b], p[a]];
|
|
86
|
+
return p;
|
|
87
|
+
};
|
|
88
|
+
switch (intrinsic) {
|
|
89
|
+
case 'none':
|
|
90
|
+
return [];
|
|
91
|
+
case 'ij':
|
|
92
|
+
case 'jk':
|
|
93
|
+
if (rank < 2) throw new RangeError(`Intrinsic symmetry '${intrinsic}' needs rank 2 or more, not ${rank}`);
|
|
94
|
+
return [intrinsic === 'ij' ? swap(0, 1) : swap(rank - 2, rank - 1)];
|
|
95
|
+
case 'ij_kl':
|
|
96
|
+
case 'voigt':
|
|
97
|
+
if (rank !== 4) throw new RangeError(`Intrinsic symmetry '${intrinsic}' needs rank 4, not ${rank}`);
|
|
98
|
+
return intrinsic === 'ij_kl' ? [swap(0, 1), swap(2, 3)] : [swap(0, 1), swap(2, 3), [2, 3, 0, 1]];
|
|
99
|
+
default:
|
|
100
|
+
throw new RangeError(`Unknown intrinsic symmetry: ${JSON.stringify(intrinsic)}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Throws unless `m` is orthogonal (mᵀm = 1 within `EPSILON`): the code assumes Cartesian matrices. */
|
|
105
|
+
function checkOrthogonal(m: Mat3, what: string): void {
|
|
106
|
+
for (let i = 0; i < 3; i++) {
|
|
107
|
+
for (let j = 0; j < 3; j++) {
|
|
108
|
+
const dot = m[0][i] * m[0][j] + m[1][i] * m[1][j] + m[2][i] * m[2][j];
|
|
109
|
+
if (Math.abs(dot - (i === j ? 1 : 0)) > EPSILON) {
|
|
110
|
+
throw new RangeError(`${what} is not an orthogonal (Cartesian) matrix: ${JSON.stringify(m)}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The indices (x, y, z = 0, 1, 2) of every flat index at a rank, first index first. */
|
|
117
|
+
const digitTables = new Map<number, readonly (readonly number[])[]>();
|
|
118
|
+
function digits(rank: number): readonly (readonly number[])[] {
|
|
119
|
+
let table = digitTables.get(rank);
|
|
120
|
+
if (!table) {
|
|
121
|
+
table = Array.from({ length: 3 ** rank }, (_, flat) =>
|
|
122
|
+
Array.from({ length: rank }, (_, k) => Math.floor(flat / 3 ** (rank - 1 - k)) % 3),
|
|
123
|
+
);
|
|
124
|
+
digitTables.set(rank, table);
|
|
125
|
+
}
|
|
126
|
+
return table;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The flat indices reached from `flat` by the index permutations (`out[k] = in[perm[k]]`). */
|
|
130
|
+
function orbit(flat: number, rank: number, perms: readonly number[][]): number[] {
|
|
131
|
+
const d = digits(rank);
|
|
132
|
+
const seen = new Set([flat]);
|
|
133
|
+
const stack = [flat];
|
|
134
|
+
while (stack.length > 0) {
|
|
135
|
+
const cur = stack.pop()!;
|
|
136
|
+
for (const p of perms) {
|
|
137
|
+
let next = 0;
|
|
138
|
+
for (let k = 0; k < rank; k++) next = next * 3 + d[cur][p[k]];
|
|
139
|
+
if (!seen.has(next)) {
|
|
140
|
+
seen.add(next);
|
|
141
|
+
stack.push(next);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return [...seen];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function freezeRows(rows: readonly (readonly number[])[]): readonly (readonly number[])[] {
|
|
149
|
+
return Object.freeze(rows.map((r) => Object.freeze([...r])));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The form for a group given as its full list of operations. The group average of each seed
|
|
154
|
+
* (a unit tensor on one orbit of the intrinsic symmetry) projects it onto the invariant subspace;
|
|
155
|
+
* the projections span it, and the reduced row echelon form makes the basis minimal and canonical.
|
|
156
|
+
*/
|
|
157
|
+
function project(group: readonly Operation[], spec: TensorSpec, perms: number[][]): TensorForm {
|
|
158
|
+
const { rank } = spec;
|
|
159
|
+
const dim = 3 ** rank;
|
|
160
|
+
const d = digits(rank);
|
|
161
|
+
const ops = group.map((op) => ({
|
|
162
|
+
m: op.matrix.flat(),
|
|
163
|
+
sign:
|
|
164
|
+
(spec.parity === 'axial' ? Math.sign(det(op.matrix)) : 1) * (spec.timeParity === 'c' && op.timeReversal ? -1 : 1),
|
|
165
|
+
}));
|
|
166
|
+
|
|
167
|
+
const spanning: number[][] = [];
|
|
168
|
+
for (let seed = 0; seed < dim; seed++) {
|
|
169
|
+
const members = orbit(seed, rank, perms);
|
|
170
|
+
if (seed !== Math.min(...members)) continue;
|
|
171
|
+
const averaged = new Array<number>(dim).fill(0);
|
|
172
|
+
for (const { m, sign } of ops) {
|
|
173
|
+
for (let out = 0; out < dim; out++) {
|
|
174
|
+
const o = d[out];
|
|
175
|
+
let acc = 0;
|
|
176
|
+
for (const s of members) {
|
|
177
|
+
const i = d[s];
|
|
178
|
+
let p = 1;
|
|
179
|
+
for (let r = 0; r < rank; r++) p *= m[o[r] * 3 + i[r]];
|
|
180
|
+
acc += p;
|
|
181
|
+
}
|
|
182
|
+
averaged[out] += sign * acc;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
for (let k = 0; k < dim; k++) averaged[k] /= ops.length;
|
|
186
|
+
if (averaged.some((x) => Math.abs(x) >= EPSILON)) spanning.push(averaged);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const basis = reducedRowEchelon(spanning, dim).map((row) => row.map(snapEntry));
|
|
190
|
+
const pivots = basis.map((row) => row.findIndex((x) => x !== 0));
|
|
191
|
+
const relations: number[][] = [];
|
|
192
|
+
for (let c = 0; c < dim; c++) {
|
|
193
|
+
if (pivots.includes(c)) continue;
|
|
194
|
+
const row = new Array<number>(dim).fill(0);
|
|
195
|
+
row[c] = 1;
|
|
196
|
+
basis.forEach((b, k) => {
|
|
197
|
+
if (b[c] !== 0) row[pivots[k]] = -b[c];
|
|
198
|
+
});
|
|
199
|
+
relations.push(row);
|
|
200
|
+
}
|
|
201
|
+
return Object.freeze({
|
|
202
|
+
rank,
|
|
203
|
+
basis: freezeRows(basis),
|
|
204
|
+
parameters: freezeRows(pivots.map((p) => d[p])),
|
|
205
|
+
relations: freezeRows(relations),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* An entry within 10⁻⁹ of a multiple of 1/12 is set to it, so that the exact values of the
|
|
211
|
+
* crystallographic forms (0, ±1/2, ±1, ±2 for the 32 classes in all settings) come out exact instead
|
|
212
|
+
* of with rounding errors of the order of 10⁻¹⁵; other values (e.g. for a rotated frame) are kept.
|
|
213
|
+
*/
|
|
214
|
+
function snapEntry(x: number): number {
|
|
215
|
+
const r = Math.round(x * 12) / 12;
|
|
216
|
+
return Math.abs(x - r) < SNAP_EPSILON ? r + 0 : x;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function specKey(spec: TensorSpec): string {
|
|
220
|
+
return `${spec.rank}:${spec.parity}:${spec.timeParity}:${spec.intrinsic}`;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const formCache = new Map<string, TensorForm>();
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The form of a tensor under a point group in the requested setting (default: the default setting
|
|
227
|
+
* of the convention, `'itc'` unless given). The convention matters only through the setting: the
|
|
228
|
+
* same setting index gives the same form in both conventions (docs/conventions.md, "Tensors").
|
|
229
|
+
*/
|
|
230
|
+
export function tensorForm(key: PointGroupKey, spec: TensorSpec, options?: SettingOptions): TensorForm {
|
|
231
|
+
const perms = checkSpec(spec);
|
|
232
|
+
const ops = operations(key, options); // checks key, convention and setting
|
|
233
|
+
const index = options?.setting ?? settings(key, options?.convention).find((s) => s.isDefault)!.index;
|
|
234
|
+
const cacheKey = `${key}#${index}#${specKey(spec)}`;
|
|
235
|
+
let form = formCache.get(cacheKey);
|
|
236
|
+
if (!form) {
|
|
237
|
+
form = project(ops, spec, perms);
|
|
238
|
+
formCache.set(cacheKey, form);
|
|
239
|
+
}
|
|
240
|
+
return form;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The form of a tensor under the group generated by `operations`, e.g. the site symmetry of a
|
|
245
|
+
* surface normal (`siteSymmetry`) or the operations of a distorted crystal. The operations are
|
|
246
|
+
* Cartesian and orthogonal; a list that is not closed is closed first.
|
|
247
|
+
*/
|
|
248
|
+
export function tensorFormOf(operations: readonly Operation[], spec: TensorSpec): TensorForm {
|
|
249
|
+
const perms = checkSpec(spec);
|
|
250
|
+
operations.forEach((op, i) => checkOrthogonal(op.matrix, `Operation ${i}`));
|
|
251
|
+
return project(closeGroup(operations), spec, perms);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The components of `tensor` (length 3^rank, flat index) in a frame rotated by the orthogonal
|
|
256
|
+
* matrix `rotation`: t′_{i…} = f·Σ R_{ia}…R_{…}·t_{a…}, with f = det(R) for an axial tensor and 1
|
|
257
|
+
* for a polar one. To express a tensor in the frame of a surface, pass the matrix whose rows are
|
|
258
|
+
* the new axes in the old frame. Time parity plays no part.
|
|
259
|
+
*/
|
|
260
|
+
export function rotateTensor(tensor: readonly number[], rotation: Mat3, spec: TensorSpec): number[] {
|
|
261
|
+
checkSpec(spec);
|
|
262
|
+
checkOrthogonal(rotation, 'The rotation');
|
|
263
|
+
const { rank } = spec;
|
|
264
|
+
const dim = 3 ** rank;
|
|
265
|
+
if (tensor.length !== dim)
|
|
266
|
+
throw new RangeError(`A tensor of rank ${rank} has ${dim} components, not ${tensor.length}`);
|
|
267
|
+
const d = digits(rank);
|
|
268
|
+
const m = rotation.flat();
|
|
269
|
+
const f = spec.parity === 'axial' ? Math.sign(det(rotation)) : 1;
|
|
270
|
+
const out = new Array<number>(dim).fill(0);
|
|
271
|
+
for (let o = 0; o < dim; o++) {
|
|
272
|
+
let acc = 0;
|
|
273
|
+
for (let s = 0; s < dim; s++) {
|
|
274
|
+
if (tensor[s] === 0) continue;
|
|
275
|
+
let p = tensor[s];
|
|
276
|
+
for (let r = 0; r < rank; r++) p *= m[d[o][r] * 3 + d[s][r]];
|
|
277
|
+
acc += p;
|
|
278
|
+
}
|
|
279
|
+
out[o] = f * acc;
|
|
280
|
+
}
|
|
281
|
+
return out;
|
|
282
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Version of symcrys. Kept equal to `version` in package.json (checked by version.test.ts). */
|
|
2
|
-
export const VERSION = '0.
|
|
2
|
+
export const VERSION = '0.4.0';
|