QPMS
Electromagnetic multiple scattering library and toolkit.
Loading...
Searching...
No Matches
Data Structures | Macros | Functions
spgsym.c File Reference

Space-group symmetry detection via the vendored spglib. More...

#include "spgsym.h"
#include "qpms_error.h"
#include "quaternions.h"
#include <math.h>
#include <complex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <spglib.h>
Include dependency graph for spgsym.c:

Data Structures

struct  match_ctx
 

Macros

#define QPMS_SPG_DETECT_1D_MAXOPS   96
 
#define ADD_DIR(d)
 
#define SPGSYM_LATTICE_ATOL   1e-9
 
#define VERIFY_1D(op)
 

Functions

static void quat_to_mat3 (const qpms_irot3_t *p, double R[3][3], double atol)
 
static qpms_irot3_t mat3_to_quat (const int R[3][3], int det)
 
static qpms_irot3_t dmat3_to_quat (const double R[3][3], int det)
 Shepperd's method for double proper rotation matrices.
 
static int imat3_close (const double A[3][3], const double B[3][3], double atol)
 Elementwise comparison of double matrices within atol.
 
static double dmat3_det (const double R[3][3])
 Determinant of a double matrix.
 
static cart3_t quat_axis (const qpms_irot3_t *p)
 
static int dmat3_order (const double R0[3][3], int maxorder)
 
static cart3_t dmat3_axis (const double R[3][3])
 Unit rotation axis of the proper part of a double (im)proper rotation.
 
static qpms_irot3_t arc_rotation (cart3_t u, cart3_t v)
 Shortest-arc rotation taking unit vector u to unit vector v.
 
static qpms_irot3_t conj_by (const qpms_irot3_t *x, const qpms_irot3_t *q)
 q^-1 . x . q
 
static int imat3_order (const int R[3][3], int maxorder)
 
static int imat3_equal (const int A[3][3], const int B[3][3])
 
static cart3_t imat3_axis (const int R[3][3])
 Rotation axis of a proper integer rotation matrix (nan-free).
 
static int imat3_det_sign (const int R[3][3])
 Determinant sign of an integer matrix.
 
static int transform_consistent (const qpms_irot3_t *g, const qpms_irot3_t *tri, const qpms_irot3_t *trj, const qpms_spg_kind_symmetry_t *ks, double atol)
 
static int frame_alignment_quat (qpms_irot3_t *out, cart3_t a1, cart3_t v1, cart3_t a2, cart3_t v2)
 
static int try_alignment (int *sigma, const struct match_ctx *ctx, const qpms_irot3_t *q)
 
static qpms_gmi_t mt_index (const qpms_finite_group_t *G, qpms_gmi_t a, qpms_gmi_t b)
 
static qpms_finite_group_t * build_oriented_group (const qpms_finite_group_t *Gcan, const struct match_ctx *ctx, const int *sigma)
 
void qpms_finite_group_free_runtime (qpms_finite_group_t *G)
 
static int mat3_inverse (const double A[3][3], double Ainv[3][3])
 Inverts a 3x3 matrix; returns 0 on success.
 
static cart3_t imat3_apply (const double R[3][3], cart3_t v)
 Applies the (im)proper operation (det-embedded matrix R) to a vector.
 
static int try_add_op (double(*ops)[3][3], int *nops, const double R[3][3], const cart3_t *rel, const qpms_irot3_t *transforms, const int *types, const qpms_spg_kind_symmetry_t *kind_sym, size_t n, double symprec)
 
static void axisangle_to_mat3 (double R[3][3], cart3_t a, double theta)
 
static void mirror_to_imat3 (int R[3][3], cart3_t n)
 Mirror matrix across the plane with the normal n̂ (unit vector).
 
static int close_under_multiplication (double(*ops)[3][3], int *nops)
 
static int axis_candidates (cart3_t *dirs, int maxdirs, const cart3_t *positions, size_t n)
 
static int spgsym_match_and_orient (qpms_spg_detection_t *out, const double(*ops)[3][3], int nops)
 
int qpms_spg_detect_pointgroup (qpms_spg_detection_t *out, const cart3_t *positions, const qpms_irot3_t *transforms, const int *types, const qpms_spg_kind_symmetry_t *kind_sym, size_t n, double symprec)
 Detects the point group symmetry of a finite particle arrangement.
 
int qpms_spg_match_pointgroup_ops (qpms_spg_detection_t *out, const qpms_irot3_t *ops, size_t nops)
 
void qpms_spg_detection_free (qpms_spg_detection_t *d)
 Frees the runtime-constructed parts of a detection result.
 
int qpms_spg_detection_filter_ops (qpms_spg_detection_t *d, const int *keep, size_t nkeep)
 
int qpms_spg_ops_from_hall_number (int(*rotations)[3][3], double(*translations)[3], int hall_number)
 Fetches the operations of a space group from the spglib database.
 
int qpms_spg_layer_ops (int(*rotations)[3][3], double(*translations)[3], char symbol[11], const double lattice[3][3], const double(*positions)[3], const int *types, int n, int aperiodic_axis, double symprec, int *number)
 
int qpms_spg_space_ops (int(*rotations)[3][3], double(*translations)[3], char symbol[11], const double lattice[3][3], const double(*positions)[3], const int *types, int n, double symprec, int *number)
 
static int lattice_reduce_mod_n (int *n, const cart3_t v, const cart3_t *ab, int latdim, double atol)
 
static int lattice_reduce_mod (int *n, const cart3_t v, const cart3_t ab[2], double atol)
 
static int lattice_preserved_1d (const double R[3][3], cart3_t a1)
 
static int spgsym_detect_pointgroup_periodic_1d (qpms_spg_detection_t *out, const cart3_t *lattice_basis, const cart3_t *positions, const qpms_irot3_t *transforms, const int *types, const qpms_spg_kind_symmetry_t *kind_sym, size_t n, double symprec)
 
int qpms_spg_detect_pointgroup_periodic (qpms_spg_detection_t *out, const cart3_t *lattice_basis, int latdim, const cart3_t *positions, const qpms_irot3_t *transforms, const int *types, const qpms_spg_kind_symmetry_t *kind_sym, size_t n, double symprec)
 Detects the point group symmetry of a 2D- or 3D-periodic arrangement.
 

Detailed Description

Space-group symmetry detection via the vendored spglib.

See spgsym.h. The orientation matching compares the detected point group against the known finite groups (groups.h): two finite groups are assumed conjugate in O(3) iff their rotation sets can be mapped onto each other by a rotation q that takes the axis of a maximal-order detected operation to the axis of a canonical operation of the same order (any other rotation doing the same differs from such a q by an element normalising the canonical group, which preserves the set match).

Macro Definition Documentation

◆ ADD_DIR

#define ADD_DIR (   d)
Value:
do { \
const double nn = sqrt((d).x*(d).x + (d).y*(d).y + (d).z*(d).z); \
if (nn > 1e-9 && ndirs < maxdirs) { \
cart3_t u = {(d).x/nn, (d).y/nn, (d).z/nn}; \
int dup = 0; \
for (int k = 0; k < ndirs && !dup; ++k) \
dup = (fabs(u.x - dirs[k].x) + fabs(u.y - dirs[k].y) \
+ fabs(u.z - dirs[k].z)) < 1e-6 \
|| (fabs(u.x + dirs[k].x) + fabs(u.y + dirs[k].y) \
+ fabs(u.z + dirs[k].z)) < 1e-6; \
if (!dup) dirs[ndirs++] = u; \
} \
} while(0)
double k[3]
Periodic Bloch vector [rad/m].
Definition resonance_k.cpp:91
3D cartesian coordinates. See also vectors.h.
Definition qpms_types.h:191

◆ QPMS_SPG_DETECT_1D_MAXOPS

#define QPMS_SPG_DETECT_1D_MAXOPS   96

3x3 matrix of the (im)proper rotation, determinant included. Operation cap for the 1D (rod) geometric detection closure. Crystallographic point groups have at most 48 operations; the cap must also absorb the non-crystallographic closures (e.g. the C12-type generated by the 2/3/4/6-fold generators about the chain axis), whose failure to close below the cap is reported as "no match".

◆ SPGSYM_LATTICE_ATOL

#define SPGSYM_LATTICE_ATOL   1e-9

Tolerance for the floating-point comparisons of the lattice arithmetic (matrix conversion); the physical position tolerance is symprec.

◆ VERIFY_1D

#define VERIFY_1D (   op)
Value:
({ \
int rc_ = 0; \
for (size_t i_ = 0; i_ < n && !rc_; ++i_) { \
const cart3_t gpos_ = qpms_affop_apply(op, positions[i_]); \
size_t jhit_ = n; \
int nres_[3] = {0, 0, 0}; \
for (size_t jj_ = 0; jj_ < n; ++jj_) { \
const cart3_t d_ = cart3_substract(gpos_, positions[jj_]); \
if (0 == lattice_reduce_mod_n(nres_, d_, &a1, 1, symprec)) { \
jhit_ = jj_; \
break; \
} \
} \
if (jhit_ == n || types[jhit_] != types[i_] \
|| !transform_consistent(&(op).rot, &transforms[i_], \
&transforms[jhit_], &kind_sym[types[i_]], symprec)) \
rc_ = -5; \
} \
rc_; })
static cart3_t qpms_affop_apply(qpms_affop_t g, cart3_t r)
Applies an affine operation to a point.
Definition pergroups.h:70
size_t n
Block dimension.
Definition resonance_k.cpp:94
static int transform_consistent(const qpms_irot3_t *g, const qpms_irot3_t *tri, const qpms_irot3_t *trj, const qpms_spg_kind_symmetry_t *ks, double atol)
Definition spgsym.c:263
static int lattice_reduce_mod_n(int *n, const cart3_t v, const cart3_t *ab, int latdim, double atol)
Definition spgsym.c:1174
static cart3_t cart3_substract(const cart3_t a, const cart3_t b)
3D vector substraction.
Definition vectors.h:180

Function Documentation

◆ axis_candidates()

static int axis_candidates ( cart3_t *  dirs,
int  maxdirs,
const cart3_t *  positions,
size_t  n 
)
static

Unit direction candidates for rotation axes and mirror normals: centroid-particle directions, their pairwise sums and differences, and (for small n) triple sums, deduplicated up to the sign. Returns the number of the candidates written to dirs (at most maxdirs).

◆ axisangle_to_mat3()

static void axisangle_to_mat3 ( double  R[3][3],
cart3_t  a,
double  theta 
)
static

Proper rotation matrix about the axis â (unit vector) by the angle theta (Rodrigues formula, double precision).

◆ build_oriented_group()

static qpms_finite_group_t * build_oriented_group ( const qpms_finite_group_t *  Gcan,
const struct match_ctx *  ctx,
const int *  sigma 
)
static

Builds a runtime-constructed group whose operations are the detected ones, with the group structure inherited from the canonical group through the index map sigma (detected index -> canonical index).

◆ close_under_multiplication()

static int close_under_multiplication ( double(*)  ops[3][3],
int *  nops 
)
static

Closes the operation set under multiplication (bounded); returns 0 if the result is a group, non-zero if the cap was hit.

◆ dmat3_order()

static int dmat3_order ( const double  R0[3][3],
int  maxorder 
)
static

Multiplicative order of a double rotation matrix (0 if not periodic within the given number of multiplications).

◆ frame_alignment_quat()

static int frame_alignment_quat ( qpms_irot3_t *  out,
cart3_t  a1,
cart3_t  v1,
cart3_t  a2,
cart3_t  v2 
)
static

Builds the rotation taking the frame (a1, v1) to (a2, v2): a1 -> a2 and the projection of v1 (onto the plane perpendicular to a1) to the projection of v2. Returns 0 on success, non-zero if the vectors are degenerate.

◆ imat3_order()

static int imat3_order ( const int  R[3][3],
int  maxorder 
)
static

Multiplicative order of an integer matrix within a group of the given size (0 if the powers never return to identity).

◆ lattice_preserved_1d()

static int lattice_preserved_1d ( const double  R[3][3],
cart3_t  a1 
)
static

True iff the linear map R maps the 1D lattice a1 onto itself (R a1 = +- a1 within the tolerance).

◆ lattice_reduce_mod()

static int lattice_reduce_mod ( int *  n,
const cart3_t  v,
const cart3_t  ab[2],
double  atol 
)
static

Returns 0 iff v is a lattice vector of the 2D basis within atol (the integer coefficients go to n when non-NULL).

◆ lattice_reduce_mod_n()

static int lattice_reduce_mod_n ( int *  n,
const cart3_t  v,
const cart3_t *  ab,
int  latdim,
double  atol 
)
static

Returns 0 iff v is a lattice vector of the n-dimensional basis within atol (the integer coefficients go to n when non-NULL; only the first latdim entries are written). A latdim-generic generalisation of lattice_reduce_mod for the detection verification.

◆ mat3_to_quat()

static qpms_irot3_t mat3_to_quat ( const int  R[3][3],
int  det 
)
static

Shepperd's method: proper rotation matrix -> quaternion (a = w + i*z, b = y + i*x); det is passed through.

◆ qpms_finite_group_free_runtime()

void qpms_finite_group_free_runtime ( qpms_finite_group_t *  G)

Frees a runtime-constructed finite group (as returned by qpms_spg_detect_pointgroup() in oriented_group).

◆ qpms_spg_detect_pointgroup()

int qpms_spg_detect_pointgroup ( qpms_spg_detection_t *  out,
const cart3_t *  positions,
const qpms_irot3_t *  transforms,
const int *  types,
const qpms_spg_kind_symmetry_t *  kind_sym,
size_t  n,
double  symprec 
)

Detects the point group symmetry of a finite particle arrangement.

A candidate operation is accepted iff it maps every particle position onto another particle of the same kind and carries the particle's T-matrix onto its partner's: with transforms giving the per-particle T-matrix orientations (the rotate/mirror transforms of the placement) and kind_sym the internal symmetry class of each kind (indexed like types), the composition transforms[j]^-1 . g . transforms[i] must lie in the internal symmetry group of the kind. Pass transforms = NULL for all-identity transforms and kind_sym = NULL for QPMS_SPGSYM_INTERNAL_ANY (a purely geometric detection, appropriate only when all kinds are rotation-symmetric, e.g. spheres).

The accepted operation set is closed under multiplication and matched against the known finite groups (cf. groups.h) including the orientation: if successful, out->oriented_group contains a runtime-constructed group whose 3D representation is aligned with the actual arrangement and which can be used directly with the scattering-system symmetry machinery (qpms_scatsys_apply_symmetry()).

positions are cartesian particle positions, types are 0-based "species" indices; symprec is the position tolerance in cartesian length units (pick it proportional to the cluster size, e.g. 1e-4 * cluster radius).

Only crystallographic point groups can be detected; non-crystallographic arrangements yield a (possibly trivial) crystallographic subgroup. Returns 0 on success (even if no known group was matched), non-zero on failure.

◆ qpms_spg_detect_pointgroup_periodic()

int qpms_spg_detect_pointgroup_periodic ( qpms_spg_detection_t *  out,
const cart3_t *  lattice_basis,
int  latdim,
const cart3_t *  positions,
const qpms_irot3_t *  transforms,
const int *  types,
const qpms_spg_kind_symmetry_t *  kind_sym,
size_t  n,
double  symprec 
)

Detects the point group symmetry of a 2D- or 3D-periodic arrangement.

Like qpms_spg_detect_pointgroup(), but for the contents of a periodic unit cell: the candidate operations are taken from the spglib layer dataset (latdim = 2) or space-group dataset (latdim = 3) of the given cell (so they respect the lattice by construction), verified against the cell particles (positions, kinds, placement transforms, kinds' internal symmetries), and matched against the known finite groups including orientation; out->oriented_group can be used directly with qpms_scatsys_apply_symmetry(). On success out->international carries the layer-group symbol and out->spacegroup_number its number (1..80).

The detected affine operations are stored in out->affops (owned); their point parts map the cell contents onto itself modulo the lattice (verified, including the kinds and T-matrix-transform consistency), so nonsymmorphic settings (glides, screws) and origin-shifted cells are supported on the pergroups code path (the origin-fixed-point restriction of the orbit machinery does not apply there). lattice_basis are the two real-space basis vectors (expected to span the xy plane; z is the aperiodic axis), positions the Cartesian cell particle positions, and the rest matches qpms_spg_detect_pointgroup(). symprec is the position tolerance in Cartesian length units (scale it with the lattice constant).

Error codes: -1 bad arguments, -2 allocation failure, -3 the spglib layer detection failed, -5 an operation breaks the cell contents.

◆ qpms_spg_detection_filter_ops()

int qpms_spg_detection_filter_ops ( qpms_spg_detection_t *  d,
const int *  keep,
size_t  nkeep 
)

Filters a detection result down to the subgroup spanned by the given operations (in-place detection surgery). keep lists the indices of the retained operations (a subset of the current nops; the caller decides which operations to keep — e.g. the layer-preserving subgroup for a layered background, cf. qpms_scatspec); it must be closed under the detected group's multiplication (verified here). The affine operation list is subset in the same order and the oriented group is re-matched from the retained point parts, so the Schoenflies symbol and the irrep data refer to the subgroup afterwards. Returns the number of the dropped operations (nops − nkeep), or a negative value on error (including a non-closed keep set or a subgroup matching no known group). A detection without an oriented group is returned unchanged (the caller proceeds without symmetry).

◆ qpms_spg_layer_ops()

int qpms_spg_layer_ops ( int(*)  rotations[3][3],
double(*)  translations[3],
char  symbol[11],
const double  lattice[3][3],
const double(*)  positions[3],
const int *  types,
int  n,
int  aperiodic_axis,
double  symprec,
int *  number 
)

Fetches the operations of a layer (wallpaper) group detected in a quasi-2D periodic cell. The cell is described like for spg_get_layer_dataset(): lattice rows are the basis vectors, positions are fractional coordinates, and aperiodic_axis (0, 1 or 2) is the non-periodic direction. The found operations (at most QPMS_SPG_MAXOPS) are stored to rotations and translations (either may be NULL); symbol receives the international symbol of the layer group (if non-NULL), and number its number 1..80 (if non-NULL). Returns the number of operations or a negative value on error.

◆ qpms_spg_match_pointgroup_ops()

int qpms_spg_match_pointgroup_ops ( qpms_spg_detection_t *  out,
const qpms_irot3_t *  ops,
size_t  nops 
)

Matches a closed set of point-group operations against the known finite groups and fills a detection result accordingly. ops are the operations as quaternion + determinant (qpms_irot3_t); the set must be closed under multiplication (as produced e.g. by the symmetry detections or by filtering a group). On success (return 0), out->oriented_group is a runtime-constructed group usable directly with qpms_scatsys_apply_symmetry(), out->canonical_group its canonically oriented namesake and out->schoenflies its name; the other fields of out are left untouched. Returns 1 when no known group matches, negative on allocation failure.

◆ qpms_spg_ops_from_hall_number()

int qpms_spg_ops_from_hall_number ( int(*)  rotations[3][3],
double(*)  translations[3],
int  hall_number 
)

Fetches the operations of a space group from the spglib database.

rotations (nops entries) receive the integer rotation matrices and translations the fractional translations (in the basis of the standardised cell of the setting); either may be NULL if not needed. hall_number selects the setting (1..530). Returns the number of the operations or a negative value on error.

◆ qpms_spg_space_ops()

int qpms_spg_space_ops ( int(*)  rotations[3][3],
double(*)  translations[3],
char  symbol[11],
const double  lattice[3][3],
const double(*)  positions[3],
const int *  types,
int  n,
double  symprec,
int *  number 
)

The spglib space-group operations of a 3D-periodic cell (thin wrapper around spg_get_dataset(), cf. qpms_spg_layer_ops()). \p lattice rows are the basis vectors, \p positions fractional coordinates. The found operations (at most QPMS_SPG_MAXOPS) go to \p rotations and \p translations (either may be NULL); \p symbol receives the international symbol (if non-NULL) and \p number the space-group number 1..230 (if non-NULL). Returns the number of the operations or a negative value on error.

◆ quat_axis()

static cart3_t quat_axis ( const qpms_irot3_t *  p)
static

Unit rotation axis of the proper part of p (nan-free for identity: returns (0,0,1) for an identity-like element).

◆ spgsym_detect_pointgroup_periodic_1d()

static int spgsym_detect_pointgroup_periodic_1d ( qpms_spg_detection_t *  out,
const cart3_t *  lattice_basis,
const cart3_t *  positions,
const qpms_irot3_t *  transforms,
const int *  types,
const qpms_spg_kind_symmetry_t *  kind_sym,
size_t  n,
double  symprec 
)
static

1D-periodic (rod) detection without spglib: geometric candidate enumeration + affine verification + known-group matching. spglib has no rod-group API, so the candidate operations are generated from the cell geometry: rotations by the crystallographic angles (2 pi / N, N = 2, 3, 4, 6) about, and mirrors with normals along, the axis candidates (the finite-system axis_candidates() directions plus the chain direction), closed under composition with every new operation verified against the cell contents modulo the lattice (positions, kinds, T-matrix-transform consistency) as in the spglib-based 2D/3D path. The verified set is matched against the known finite groups (the rod point groups are crystallographic point groups). Continuous (e.g. D_infinity_h-type) symmetries have no crystallographic representation; if the verified closure does not converge below the cap, no match is reported (the caller's declared-group path is the way to impose a specific finite subgroup).

Returns 0 on success, 1 when no known group matched, negative on errors (-5 = an operation breaks the cell contents, -2 = allocation, -1 = bad input), following the qpms_spg_detect_pointgroup_periodic() conventions.

◆ spgsym_match_and_orient()

static int spgsym_match_and_orient ( qpms_spg_detection_t *  out,
const double(*)  ops[3][3],
int  nops 
)
static

Matches a closed set of Cartesian operations (determinants included) against the known finite groups and builds the oriented group into out (whose nops field must be set by the caller). Returns 0 when a match was found, 1 when no known group matched, and a negative value on allocation failure.

◆ transform_consistent()

static int transform_consistent ( const qpms_irot3_t *  g,
const qpms_irot3_t *  tri,
const qpms_irot3_t *  trj,
const qpms_spg_kind_symmetry_t *  ks,
double  atol 
)
static

Checks whether the candidate operation g (as an irot3) carries the T-matrix of particle i onto the one of particle j: the composition W = transforms[j]^-1 . g . transforms[i] must lie in the internal symmetry class of the kind. See spgsym.h for the class semantics.

◆ try_add_op()

static int try_add_op ( double(*)  ops[3][3],
int *  nops,
const double  R[3][3],
const cart3_t *  rel,
const qpms_irot3_t *  transforms,
const int *  types,
const qpms_spg_kind_symmetry_t *  kind_sym,
size_t  n,
double  symprec 
)
static

Checks whether the operation R maps the (centroid-relative) particle set onto itself, respecting the types; appends it to ops (with dedup) and returns 1 if it does, 0 otherwise.

◆ try_alignment()

static int try_alignment ( int *  sigma,
const struct match_ctx *  ctx,
const qpms_irot3_t *  q 
)
static

Tries to match the detected set against the canonical set by conjugation with q; on success fills sigma (detected index -> canonical index) and returns 0.