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

Implementation of the declarative YAML scattering system loader. More...

#include "scuffgen.h"
#include "scatspec.h"
#include "specexpr.h"
#include "tmatrices.h"
#include "tmatrix_hdf5.h"
#include "materials.h"
#include "quaternions.h"
#include "symmetries.h"
#include "spgsym.h"
#include "tolerances.h"
#include "indexing.h"
#include "sha256.h"
#include "canonbuf.h"
#include <libfyaml.h>
#include <gsl/gsl_spline.h>
#include <errno.h>
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for scatspec.c:

Data Structures

struct  sspec_owned_t
 
struct  sspec_kind_t
 
struct  sspec_material_t
 
struct  sspec_particle_t
 
struct  qpms_scatspec_t
 
struct  sspec_ctx_t
 
struct  sspec_ldname
 
struct  sspec_tmkey_t
 Key for deduplicating the (kind, transform) pairs at system build time. More...
 

Macros

#define SSF_WHERE_MAX   512
 

Functions

static void sspec_free_bspec (void *ptr)
 
static void sspec_free_scuff_interp (void *ptr)
 
static void sspec_free_spg_detection (void *ptr)
 
static int sspec_own (struct qpms_scatspec_t *spec, void *ptr, void(*dtor)(void *))
 Registers an owned resource; returns -1 (without taking ownership) on error.
 
void qpms_scatspec_free (qpms_scatspec_t *spec)
 Frees a spec. Any qpms_scatsys_t built from it becomes invalid afterwards.
 
static void ssf_fail (sspec_ctx_t *c, const char *fmt,...)
 
static int ssf_check (sspec_ctx_t *c)
 
static int ssf_check_keys (sspec_ctx_t *c, struct fy_node *map, const char *where, const char *const *allowed)
 Rejects mapping keys outside the allowed set (typo protection).
 
static int ssf_text (sspec_ctx_t *c, struct fy_node *n, const char *where, char *out, size_t outlen)
 Copies the (resolved) text of a scalar node.
 
static char * ssf_material_describe (struct fy_node *n)
 Renders a material node "as given" into a newly allocated string.
 
static int ssf_number (sspec_ctx_t *c, struct fy_node *n, const char *where, _Complex double *out)
 Evaluates a numeric field: plain number or specexpr expression.
 
static int ssf_int (sspec_ctx_t *c, struct fy_node *n, const char *where, long *out)
 
static int ssf_positive_int (sspec_ctx_t *c, struct fy_node *n, const char *where, long *out)
 
static int ssf_key_alias (struct fy_node *node, const char *key, const char *alias, struct fy_node **out)
 Looks up a shape parameter key with an alternative spelling.
 
static int ssf_bool (sspec_ctx_t *c, struct fy_node *n, const char *where, _Bool *out)
 Parses a boolean field; YAML 1.2 booleans ("true"/"false") accepted.
 
static int ssf_vec3 (sspec_ctx_t *c, struct fy_node *n, const char *where, cart3_t *out)
 Parses a vector of 2 or 3 numbers (2 -> z = 0).
 
static int ssf_const_refindex (sspec_ctx_t *c, _Complex double n, const char *where, qpms_epsmu_generator_t *out)
 Creates a constant epsmu generator with eps = n*n, mu = 1.
 
static int ssf_yaml_material (sspec_ctx_t *c, const char *path, const char *where, qpms_epsmu_generator_t *out)
 Creates an interpolator-based generator from a refractiveindex.info yml file.
 
static int ssf_ld_material (sspec_ctx_t *c, const char *name, const char *where, qpms_epsmu_generator_t *out)
 
static int ssf_material_node (sspec_ctx_t *c, struct fy_node *n, const char *where, qpms_epsmu_generator_t *out)
 
static int ssf_material_by_name (sspec_ctx_t *c, const char *name, const char *where, qpms_epsmu_generator_t *out)
 Resolves a material by name: named table entry, LD model, or yml file.
 
static int ssf_layers_node (sspec_ctx_t *c, struct fy_node *n, struct qpms_scatspec_t *spec)
 Parse system.layers: a sequence of per-layer mappings (bottom to top).
 
static qpms_vswf_set_spec_t * sspec_bs_ball (struct qpms_scatspec_t *spec, long lMax)
 
static int ssf_parse_kind_constant (sspec_ctx_t *c, sspec_kind_t *k, struct fy_node *node, const char *where)
 
static _Bool ssf_shape_is_scuffgen (const char *shapename)
 Tells whether the shape name belongs to the scuffgen-only kinds.
 
static int ssf_pos_dim (sspec_ctx_t *c, struct fy_node *n, const char *name, const char *where, double *out)
 Parses a strictly positive real dimension.
 
static int ssf_nonneg_dim (sspec_ctx_t *c, struct fy_node *n, const char *name, const char *where, double *out)
 Parses a non-negative real dimension.
 
static int ssf_parse_kind (sspec_ctx_t *c, struct fy_node *node, const char *name, const char *where)
 Parses a single particle kind definition (a mapping node).
 
static int ssf_rotate (sspec_ctx_t *c, struct fy_node *node, const char *where, _Bool *has, qpms_irot3_t *out)
 Parses a rotation: {axis: [...], angle: ...} or a shorthand {x|y|z: angle}.
 
static int ssf_mirror (sspec_ctx_t *c, struct fy_node *node, const char *where, _Bool *has, qpms_irot3_t *out)
 Parses a mirror: "xy" (primitive z-flip), "xz", "yz", or true (= "xy").
 
static int sspec_transform_is_identity (qpms_irot3_t tr)
 Returns true if tr is (numerically) the identity transform.
 
static int sspec_add_particle (sspec_ctx_t *c, cart3_t pos, size_t kind, qpms_irot3_t tr)
 
static int ssf_place_particle_ref (sspec_ctx_t *c, struct fy_node *node, const char *where, size_t *kind)
 Resolves the 'particle' field: either a kind name or an inline definition.
 
static int ssf_place_array (sspec_ctx_t *c, struct fy_node *node, const char *where, size_t kind, qpms_irot3_t tr)
 Expands an 'array' placement into particles.
 
static int ssf_parse_place (sspec_ctx_t *c, struct fy_node *node, const char *where)
 Parses and expands a single element of the 'places' sequence.
 
static int ssf_parse_lattice (sspec_ctx_t *c, struct fy_node *node, const char *where)
 Parses the 'lattice' field; sets lattice_dimension and lattice_basis.
 
static struct qpms_scatspec_t * sspec_parse_document (struct fy_document *doc, const char *path, const qpms_scatspec_var_t *overrides, size_t noverrides, char *errbuf, size_t errlen)
 
static struct qpms_scatspec_t * sspec_parse_generic (int(*setup)(void *ud, struct fy_parser *fyp), void *ud, const char *display_path, const qpms_scatspec_var_t *overrides, size_t noverrides, char *errbuf, size_t errlen)
 
static int sspec_setup_file (void *ud, struct fy_parser *fyp)
 
static int sspec_setup_string (void *ud, struct fy_parser *fyp)
 
qpms_scatspec_t * qpms_scatspec_parse_file (const char *path, const qpms_scatspec_var_t *overrides, size_t noverrides, char *errbuf, size_t errlen)
 Parses and validates a spec file.
 
qpms_scatspec_t * qpms_scatspec_parse_string (const char *yaml, ssize_t len, const qpms_scatspec_var_t *overrides, size_t noverrides, char *errbuf, size_t errlen)
 As qpms_scatspec_parse_file(), but parses from a NUL-terminated string.
 
static int sspec_parse_vars (sspec_ctx_t *c, struct fy_node *node)
 
static int ssf_fp_epsmu_gen (qpms_canonbuf_t *b, const char *tag, const qpms_epsmu_generator_t *g)
 Serialises an optical-property generator into the canonical stream.
 
size_t qpms_scatspec_particle_count (const qpms_scatspec_t *spec)
 Number of particles after the placement expansion.
 
void qpms_scatspec_positions (const qpms_scatspec_t *spec, cart3_t *out)
 
size_t qpms_scatspec_kind_count (const qpms_scatspec_t *spec)
 Number of particle kinds defined in the spec.
 
const char * qpms_scatspec_kind_name (const qpms_scatspec_t *spec, size_t ki)
 Name of a particle kind, or NULL if ki is out of range (borrowed pointer).
 
int qpms_scatspec_kind_source (const qpms_scatspec_t *spec, size_t ki)
 T-matrix source kind of a particle kind, or -1 if ki is out of range.
 
const char * qpms_scatspec_kind_file (const qpms_scatspec_t *spec, size_t ki)
 
_Bool qpms_scatspec_kind_shapeinfo (const qpms_scatspec_t *spec, size_t ki, qpms_scatspec_shapeinfo_t *out)
 
const qpms_vswf_set_spec_t * qpms_scatspec_kind_bspec (const qpms_scatspec_t *spec, size_t ki)
 
void qpms_scatspec_particle_kinds (const qpms_scatspec_t *spec, size_t *out)
 
void qpms_scatspec_particle_transforms (const qpms_scatspec_t *spec, qpms_irot3_t *out)
 
qpms_epsmu_t qpms_scatspec_medium_eval (const qpms_scatspec_t *spec, _Complex double omega)
 Evaluates the background medium (system.medium) at an angular frequency.
 
int qpms_scatspec_lattice_dimension (const qpms_scatspec_t *spec)
 Lattice dimension (0 = finite system, 1-3 = periodic).
 
void qpms_scatspec_lattice_basis (const qpms_scatspec_t *spec, cart3_t *out)
 Copies the lattice basis vectors to out (which must have room for 3).
 
const qpms_finite_group_t * qpms_scatspec_symmetry (const qpms_scatspec_t *spec)
 
const qpms_spg_detection_t * qpms_scatspec_detection (const qpms_scatspec_t *spec)
 
int qpms_scatspec_has_omega (const qpms_scatspec_t *spec)
 Returns whether the spec defines the initial frequency (system.omega).
 
_Complex double qpms_scatspec_omega (const qpms_scatspec_t *spec)
 
static int sspec_proto_from_spec (const qpms_scatspec_t *spec, qpms_scatsys_t *orig, size_t *p_tmi, sspec_tmkey_t *tmkeys)
 
qpms_scatsys_t * qpms_scatspec_build_scatsys (const qpms_scatspec_t *spec, _Complex double omega)
 Builds a scattering system from the spec.
 
qpms_scatsys_t * qpms_scatspec_build_scatsys_with_group (const qpms_scatspec_t *spec, const qpms_finite_group_t *group, _Complex double omega)
 
static int ssf_fp_bspec (qpms_canonbuf_t *b, const qpms_vswf_set_spec_t *bs)
 Serialises a VSWF basis specification into the canonical stream.
 
static int ssf_fp_kind (qpms_canonbuf_t *b, const sspec_kind_t *k)
 
static int ssf_fp_irot3 (qpms_canonbuf_t *b, qpms_irot3_t t)
 
int qpms_scatspec_system_fingerprint (const qpms_scatspec_t *spec, char hex[65])
 Computes a SHA-256 fingerprint of the resolved physical system.
 

Variables

static const struct sspec_ldname sspec_ldnames []
 

Detailed Description

Implementation of the declarative YAML scattering system loader.

See scatspec.h (API) and scatspec.md (format description).

Function Documentation

◆ qpms_scatspec_build_scatsys()

qpms_scatsys_t * qpms_scatspec_build_scatsys ( const qpms_scatspec_t *  spec,
_Complex double  omega 
)

Builds a scattering system from the spec.

The T-matrix generators are evaluated at omega, which must lie within the validity ranges of all materials and T-matrix interpolators used by the spec. Returns NULL on failure.

Note
The returned qpms_scatsys_t references generator data owned by the spec; see the lifetime note in the file documentation.

◆ qpms_scatspec_build_scatsys_with_group()

qpms_scatsys_t * qpms_scatspec_build_scatsys_with_group ( const qpms_scatspec_t *  spec,
const qpms_finite_group_t *  group,
_Complex double  omega 
)

As qpms_scatspec_build_scatsys(), but applies an explicit group instead of the spec's own system.symmetry (NULL falls back to the spec's group, cf. qpms_scatspec_build_scatsys()). Used e.g. to build the scattering system with the little group of a given Bloch vector. The group operations must map the placed particles exactly onto placed particles of matching T-matrices (the group is verified, not used to generate the arrangement).

◆ qpms_scatspec_kind_bspec()

const qpms_vswf_set_spec_t * qpms_scatspec_kind_bspec ( const qpms_scatspec_t *  spec,
size_t  ki 
)

Basis spec of a particle kind, or NULL if ki is out of range (borrowed pointer; owned by the spec).

◆ qpms_scatspec_kind_file()

const char * qpms_scatspec_kind_file ( const qpms_scatspec_t *  spec,
size_t  ki 
)

T-matrix source file of a particle kind, or NULL if the kind is not file-based or ki is out of range (borrowed pointer, owned by the spec).

◆ qpms_scatspec_kind_shapeinfo()

_Bool qpms_scatspec_kind_shapeinfo ( const qpms_scatspec_t *  spec,
size_t  ki,
qpms_scatspec_shapeinfo_t *  out 
)

Collects the parameters of an analytic (sphere, cylinder, spheroid, cone, cut sphere, core-shell sphere or superellipsoid) particle kind. Returns false (leaving out untouched) if the kind is not analytic or ki is out of range.

◆ qpms_scatspec_omega()

_Complex double qpms_scatspec_omega ( const qpms_scatspec_t *  spec)

Returns the system.omega value of the spec (only meaningful if qpms_scatspec_has_omega() is true).

◆ qpms_scatspec_parse_file()

qpms_scatspec_t * qpms_scatspec_parse_file ( const char *  path,
const qpms_scatspec_var_t *  overrides,
size_t  noverrides,
char *  errbuf,
size_t  errlen 
)

Parses and validates a spec file.

Parameters
pathPath to the YAML spec file.
overridesOptional array of variable overrides applied on top of the vars section of the file (may also introduce new variables). May be NULL.
noverridesNumber of entries in overrides.
errbufBuffer for a human readable error message (may be NULL).
errlenSize of errbuf.
Returns
A new spec, or NULL on error (with errbuf filled in).

◆ qpms_scatspec_parse_string()

qpms_scatspec_t * qpms_scatspec_parse_string ( const char *  yaml,
ssize_t  len,
const qpms_scatspec_var_t *  overrides,
size_t  noverrides,
char *  errbuf,
size_t  errlen 
)

As qpms_scatspec_parse_file(), but parses from a NUL-terminated string.

If len is negative, strlen(yaml) is used.

◆ qpms_scatspec_particle_kinds()

void qpms_scatspec_particle_kinds ( const qpms_scatspec_t *  spec,
size_t *  out 
)

Copies the particle kind index of each expanded particle to out (which must have room for qpms_scatspec_particle_count() entries).

◆ qpms_scatspec_particle_transforms()

void qpms_scatspec_particle_transforms ( const qpms_scatspec_t *  spec,
qpms_irot3_t *  out 
)

Copies the placement transform (mirror, then rotation) of each expanded particle to out (which must have room for qpms_scatspec_particle_count() entries).

◆ qpms_scatspec_positions()

void qpms_scatspec_positions ( const qpms_scatspec_t *  spec,
cart3_t *  out 
)

Copies the expanded particle positions to out (which must have room for qpms_scatspec_particle_count() entries).

◆ qpms_scatspec_symmetry()

const qpms_finite_group_t * qpms_scatspec_symmetry ( const qpms_scatspec_t *  spec)

The point group symmetry (NULL if none set; use with qpms_scatspec_lattice_dimension() to distinguish "no symmetry requested" from periodic systems, which always require a trivial symmetry). With system.symmetry: auto this is the runtime-constructed oriented group detected from the placements; the detection result itself is available through qpms_scatspec_detection().

◆ qpms_scatspec_system_fingerprint()

int qpms_scatspec_system_fingerprint ( const qpms_scatspec_t *  spec,
char  hex[65] 
)

Computes a SHA-256 fingerprint of the resolved physical system.

Serialises the physics-defining state of the spec (background medium, named materials, particle kinds including file-based T-matrix sources hashed by content, particle placements, lattice, symmetry setting) in a canonical form and hashes it. Two specs yield the same fingerprint iff they describe the same physical system, independently of cosmetic differences of the source YAML files (whitespace, variable names). Intended for provenance records and append-reuse checks in the results container.

Returns
QPMS_SUCCESS, or QPMS_ERROR if the spec is NULL, on an out of memory error, if a T-matrix source file cannot be read, or if the spec uses an optical-property generator not covered by the canonical serialisation (currently only constant, tabulated-interpolator and Lorentz-Drude generators are supported).

◆ ssf_fp_epsmu_gen()

static int ssf_fp_epsmu_gen ( qpms_canonbuf_t *  b,
const char *  tag,
const qpms_epsmu_generator_t *  g 
)
static

Serialises an optical-property generator into the canonical stream.

Canonical serialisation of an optical-property generator (defined near the system fingerprint; forward-declared for the layer-material equivalence classes of the auto-detection filter).

Returns
0 on success, 1 if the generator function is not recognised (the caller turns this into an error), -1 on a buffer error.

◆ ssf_fp_irot3()

static int ssf_fp_irot3 ( qpms_canonbuf_t *  b,
qpms_irot3_t  t 
)
static

Serialises an improper rotation in canonical form (quaternions q and -q denote the same rotation; the sign is fixed by the first nonzero component of the internal (a, b) pair representation).

◆ ssf_fp_kind()

static int ssf_fp_kind ( qpms_canonbuf_t *  b,
const sspec_kind_t *  k 
)
static

Serialises the canonical (physics-defining) parameters of one particle kind into the stream;

Returns
0 ok, 1 unsupported generator, -1 error.

◆ ssf_key_alias()

static int ssf_key_alias ( struct fy_node *  node,
const char *  key,
const char *  alias,
struct fy_node **  out 
)
static

Looks up a shape parameter key with an alternative spelling.

Writes the node found under key or (failing that) under alias to *out (NULL if neither is present). Returns nonzero if both spellings are present, which the caller shall report as an error.

◆ ssf_layers_node()

static int ssf_layers_node ( sspec_ctx_t *  c,
struct fy_node *  n,
struct qpms_scatspec_t *  spec 
)
static

Parse system.layers: a sequence of per-layer mappings (bottom to top).

Creates a VSWF basis spec containing all transversal waves up to lMax (electric block first, then magnetic; l from 1 to lMax, m from -l to l), matching BaseSpec(lMax=...) of the Python API. Parse system.layers: a sequence of per-layer mappings (bottom to top). Each entry: material (any material node; required) and thickness (optional, defaults to 0; the first and last layers are semi-infinite). Each entry: material (any material node; required) and thickness (optional, defaults to 0; the first and last layers are semi-infinite).

◆ ssf_material_describe()

static char * ssf_material_describe ( struct fy_node *  n)
static

Renders a material node "as given" into a newly allocated string.

Scalars are copied verbatim; mappings are rendered as comma-separated key=value pairs; anything else as "<complex>". Used by the read-only accessors to expose the analytic particle kinds' materials. Returns NULL only on memory error.

◆ ssf_parse_kind_constant()

static int ssf_parse_kind_constant ( sspec_ctx_t *  c,
sspec_kind_t *  k,
struct fy_node *  node,
const char *  where 
)
static

Parses a shape: constant particle kind: a fabricated, frequency- independent T-matrix (tmatrix; square matrix of complex expressions) acting in an explicit VSWF set given either as raw codes (vswf; the encoding of qpms_tmn2uvswfi(), compatible with the legacy -w option) or as [type, l, m] triples (tlm; type one of M, N).

◆ sspec_proto_from_spec()

static int sspec_proto_from_spec ( const qpms_scatspec_t *  spec,
qpms_scatsys_t *  orig,
size_t *  p_tmi,
sspec_tmkey_t *  tmkeys 
)
static

Assembles the "proto" scattering system (particles, T-matrix recipes, lattice) from the spec, without applying any symmetry; p_tmi is an array of spec->np entries mapping each particle to its orig.tm index. Returns 0 on success, nonzero on error (with the partially allocated pieces of orig freed).

Variable Documentation

◆ sspec_ldnames

const struct sspec_ldname sspec_ldnames[]
static
Initial value:
= {
{ "Ag", &QPMS_LDPARAMS_AG }, { "Au", &QPMS_LDPARAMS_AU },
{ "Cu", &QPMS_LDPARAMS_CU }, { "Al", &QPMS_LDPARAMS_AL },
{ "Cr", &QPMS_LDPARAMS_CR }, { "Ti", &QPMS_LDPARAMS_TI },
{ "Be", &QPMS_LDPARAMS_BE }, { "Ni", &QPMS_LDPARAMS_NI },
{ "Pd", &QPMS_LDPARAMS_PD }, { "Pt", &QPMS_LDPARAMS_PT },
{ "W", &QPMS_LDPARAMS_W }, { NULL, NULL },
}
const qpms_ldparams_t *const QPMS_LDPARAMS_CR
Lorentz-Drude parameters from for chromium.
Definition drudeparam_data.c:85
const qpms_ldparams_t *const QPMS_LDPARAMS_AU
Lorentz-Drude parameters from for gold.
Definition drudeparam_data.c:28
const qpms_ldparams_t *const QPMS_LDPARAMS_AG
Lorentz-Drude parameters from for silver.
Definition drudeparam_data.c:43
const qpms_ldparams_t *const QPMS_LDPARAMS_NI
Lorentz-Drude parameters from for nickel.
Definition drudeparam_data.c:127
const qpms_ldparams_t *const QPMS_LDPARAMS_W
Lorentz-Drude parameters from for tungsten.
Definition drudeparam_data.c:169
const qpms_ldparams_t *const QPMS_LDPARAMS_TI
Lorentz-Drude parameters from for titanium.
Definition drudeparam_data.c:99
const qpms_ldparams_t *const QPMS_LDPARAMS_CU
Lorentz-Drude parameters from for copper.
Definition drudeparam_data.c:57
const qpms_ldparams_t *const QPMS_LDPARAMS_BE
Lorentz-Drude parameters from for beryllium.
Definition drudeparam_data.c:113
const qpms_ldparams_t *const QPMS_LDPARAMS_PD
Lorentz-Drude parameters from for polonium.
Definition drudeparam_data.c:141
const qpms_ldparams_t *const QPMS_LDPARAMS_PT
Lorentz-Drude parameters from for platinum.
Definition drudeparam_data.c:155
const qpms_ldparams_t *const QPMS_LDPARAMS_AL
Lorentz-Drude parameters from for aluminium.
Definition drudeparam_data.c:71