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

Implementation of the simspec run-half loader (see simspec.h). More...

#include "simspec.h"
#include <ctype.h>
#include <float.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libfyaml.h>
#include "specexpr.h"
#include "rayleigh.h"
#include "sha256.h"
#include "canonbuf.h"
#include "groups.h"
Include dependency graph for simspec.c:

Data Structures

struct  ssim_ctx_t
 
struct  qpms_simspec_t
 

Macros

#define SSIM_WHERE_MAX   256
 
#define SSIM_AXIS_MAX   10000000L
 Maximum number of expanded axis values (sanity cap against accidental OOM).
 
#define SSIM_FF_MAX   100000L
 Far-field grid dimension cap (a 2-D map; no reason for more).
 
#define SSIM_KDEP_FIELD(field, expr, name, real_only)
 

Typedefs

typedef struct ssim_ctx_t ssim_ctx_t
 

Functions

static void ssim_fail (ssim_ctx_t *c, const char *fmt,...)
 
static int ssim_check (ssim_ctx_t *c)
 
static int ssim_check_keys (ssim_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 ssim_text (ssim_ctx_t *c, struct fy_node *n, const char *where, char *out, size_t outlen)
 Copies the (resolved) text of a scalar node.
 
static _Bool ssim_rayleigh_token (const char *s)
 
static void ssim_fail_number (ssim_ctx_t *c, const char *where, const char *buf, const char *err)
 
static int ssim_number_text (ssim_ctx_t *c, const char *buf, const char *where, _Complex double *out)
 Evaluates a numeric field text: plain number or specexpr expression.
 
static int ssim_number (ssim_ctx_t *c, struct fy_node *n, const char *where, _Complex double *out)
 Evaluates a numeric field: plain number or specexpr expression.
 
static int ssim_install_rayleigh (ssim_ctx_t *c, const double bloch[3])
 
static void ssim_uninstall_rayleigh (ssim_ctx_t *c)
 Uninstalls the Rayleigh-expression hooks of the current experiment.
 
static int ssim_real (ssim_ctx_t *c, struct fy_node *n, const char *where, double *out)
 Evaluates a numeric field that must be real (imaginary part rejected).
 
static int ssim_int (ssim_ctx_t *c, struct fy_node *n, const char *where, long *out)
 
static int ssim_positive_int (ssim_ctx_t *c, struct fy_node *n, const char *where, long *out)
 
static int ssim_bool (ssim_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 ssim_strdup_scalar (ssim_ctx_t *c, struct fy_node *n, const char *where, char **out)
 Duplicates a scalar node's text; fails on non-scalars and empty strings.
 
static int ssim_vec13 (ssim_ctx_t *c, struct fy_node *n, const char *where, double out[3])
 Parses a sequence of 1-3 real numbers (missing components default to 0).
 
static int ssim_real_seq_fixed (ssim_ctx_t *c, struct fy_node *n, const char *where, double *out, int count)
 Parses a sequence of exactly count real numbers into out.
 
static int ssim_int_seq_fixed (ssim_ctx_t *c, struct fy_node *n, const char *where, long *out, int count)
 Parses a sequence of exactly count integers into out.
 
static int ssim_axis_segment_read (ssim_ctx_t *c, struct fy_node *n, const char *where, _Bool require_real, _Complex double *start, _Complex double *stop, _Complex double *step)
 Reads a {start, stop, step} mapping into the three complex values.
 
static long ssim_axis_segment_count (ssim_ctx_t *c, const char *where, _Complex double start, _Complex double stop, _Complex double step)
 
static _Bool ssim_axis_vals_close (_Complex double a, _Complex double b)
 
static int ssim_axis (ssim_ctx_t *c, struct fy_node *n, const char *where, qpms_simspec_axis_t *out, _Bool require_real)
 Resolves one axis node (scalar | sequence | {start, stop, step} mapping).
 
static void ssim_axis_free (qpms_simspec_axis_t *a)
 
static void ssim_tmatrix_free (qpms_simspec_tmatrix_t *e)
 
static void ssim_cross_sections_free (qpms_simspec_cross_sections_t *e)
 
static void ssim_driving_free (qpms_simspec_driving_t *e)
 
static void ssim_resonances_free (qpms_simspec_resonances_t *e)
 
static void ssim_experiment_free (qpms_simspec_experiment_t *e)
 
static int ssim_parse_irreps (ssim_ctx_t *c, struct fy_node *n, const char *where, qpms_simspec_irreps_t *mode, char ***labels, size_t *nlabels)
 Parses the common irreps key ("all" | "full" | sequence of labels).
 
static int ssim_check_irreps_system (ssim_ctx_t *c, const qpms_simspec_irreps_t mode, const char *where)
 Validates the irreps selection against the parsed system half.
 
static int ssim_omega_forbidden (ssim_ctx_t *c, struct fy_node *node, const char *where)
 Rejects an omega axis for the kinds that do not use it (resonances).
 
static int ssim_omega_optional (ssim_ctx_t *c, struct fy_node *node, const char *where, qpms_simspec_axis_t *omega)
 Parses the common real omega axis; returns 0 if present, 1 if absent.
 
static int ssim_omega_single (ssim_ctx_t *c, struct fy_node *node, const char *where, qpms_simspec_axis_t *omega)
 Requires the omega axis to hold exactly one real value (driving).
 
static int ssim_parse_tmatrix (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_experiment_t *e)
 
static int ssim_parse_cross_sections (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_experiment_t *e)
 
static int ssim_parse_driving (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_experiment_t *e)
 
static int ssim_contour_field (ssim_ctx_t *c, struct fy_node *n, const char *where, _Bool kdep_allowed, _Bool real_only, _Complex double *value, char **expr)
 
static int ssim_parse_contour (ssim_ctx_t *c, struct fy_node *node, const char *where, qpms_simspec_resonances_t *r, _Bool kdep_allowed)
 
static int ssim_solver_posreal (ssim_ctx_t *c, struct fy_node *opts, const char *key, const char *where, double *out)
 Parses one positive-real solver option; returns 1 if absent.
 
static int ssim_solver_nonneg (ssim_ctx_t *c, struct fy_node *opts, const char *key, const char *where, long *out)
 Parses one non-negative-long solver option; returns 1 if absent.
 
static int ssim_parse_resonance_solver (ssim_ctx_t *c, struct fy_node *node, const char *kindname, qpms_simspec_resonances_t *r, _Bool kdep_allowed)
 
static int ssim_parse_resonances (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_experiment_t *e)
 
static int ssim_parse_mode_svd (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_experiment_t *e)
 
static int ssim_parse_bandstructure_svd (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_experiment_t *e)
 
static int ssim_resolve_kdep_at (const qpms_scatspec_t *spec, const qpms_specexpr_env_t *base_env, const qpms_simspec_resonances_t *r, cart3_t k, qpms_simspec_resonances_t *out, char *errbuf, size_t errlen)
 
qpms_errno_t qpms_simspec_resonances_resolve_kdep (const qpms_simspec_t *sim, const qpms_simspec_resonances_t *r, cart3_t k, qpms_simspec_resonances_t *out, char *errbuf, size_t errlen)
 Resolves the k-dependent contour expressions of a solver spec.
 
static int ssim_parse_bandstructure (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_experiment_t *e)
 
static int ssim_parse_transmission (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_experiment_t *e)
 
static int ssim_parse_output (ssim_ctx_t *c, struct fy_node *node, qpms_simspec_output_t *out)
 
static int ssim_parse_vars (ssim_ctx_t *c, struct fy_node *node)
 Parses the file's vars section into the run-half environment.
 
const char * qpms_simspec_kind_name (qpms_simspec_kind_t kind)
 Name of an experiment kind ("tmatrix", ...), or NULL for invalid values.
 
void qpms_simspec_free (qpms_simspec_t *s)
 
const qpms_scatspec_t * qpms_simspec_system (const qpms_simspec_t *s)
 The system half of the spec (borrowed pointer; owned by the simspec).
 
size_t qpms_simspec_experiment_count (const qpms_simspec_t *s)
 Number of experiments.
 
const qpms_simspec_experiment_t * qpms_simspec_experiment (const qpms_simspec_t *s, size_t i)
 The i'th experiment (borrowed pointer), or NULL if i is out of range.
 
const qpms_simspec_output_t * qpms_simspec_output (const qpms_simspec_t *s)
 The global output policy (borrowed pointer; owned by the simspec).
 
const char * qpms_simspec_path (const qpms_simspec_t *s)
 The path the spec was parsed from (borrowed; NULL for parse_string()).
 
static struct qpms_simspec_t * ssim_parse_document (struct fy_document *doc, const char *path, const char *sys_yaml, ssize_t sys_yaml_len, const qpms_scatspec_var_t *overrides, size_t noverrides, char *errbuf, size_t errlen)
 
static struct qpms_simspec_t * ssim_parse_generic (int(*setup)(void *ud, struct fy_parser *fyp), void *ud, const char *display_path, const char *sys_yaml, ssize_t sys_yaml_len, const qpms_scatspec_var_t *overrides, size_t noverrides, char *errbuf, size_t errlen)
 
static int ssim_setup_file (void *ud, struct fy_parser *fyp)
 
static int ssim_setup_string (void *ud, struct fy_parser *fyp)
 
qpms_simspec_t * qpms_simspec_parse_file (const char *path, const qpms_scatspec_var_t *overrides, size_t noverrides, char *errbuf, size_t errlen)
 Parses and validates a simspec file (system half + run half).
 
qpms_simspec_t * qpms_simspec_parse_string (const char *yaml, ssize_t len, const qpms_scatspec_var_t *overrides, size_t noverrides, char *errbuf, size_t errlen)
 As qpms_simspec_parse_file(), but parses from a NUL-terminated string.
 
int qpms_simspec_solver_fingerprint (const qpms_simspec_resonances_t *r, char hex[65])
 Computes a SHA-256 fingerprint of the k-independent solver specification.
 

Detailed Description

Implementation of the simspec run-half loader (see simspec.h).

The parsing conventions mirror scatspec.c (context-based error reporting, strict unknown-key rejection, specexpr-evaluated numeric fields); the system half of the document is delegated to the scatspec parser.

Macro Definition Documentation

◆ SSIM_KDEP_FIELD

#define SSIM_KDEP_FIELD (   field,
  expr,
  name,
  real_only 
)
Value:
do { \
if (expr) { \
char e2[256]; \
if (qpms_specexpr_eval(env, expr, &val, e2, sizeof(e2))) { \
snprintf(errbuf, errlen, "%s: %s", name, e2); \
goto out; \
} \
if (real_only) { \
if (cimag(val) != 0) { \
snprintf(errbuf, errlen, "%s: expected a real " \
"number", name); \
goto out; \
} \
out->field = creal(val); \
} else \
out->field = val; \
} \
} while (0)
int qpms_specexpr_eval(const qpms_specexpr_env_t *env, const char *expr, _Complex double *out, char *errbuf, size_t errlen)
Evaluates an arithmetic expression over _Complex double.
Definition specexpr.c:445

Function Documentation

◆ qpms_simspec_free()

void qpms_simspec_free ( qpms_simspec_t *  s)

Frees a simspec. Any qpms_scatsys_t built from its system half becomes invalid afterwards.

◆ qpms_simspec_parse_file()

qpms_simspec_t * qpms_simspec_parse_file ( const char *  path,
const qpms_scatspec_var_t *  overrides,
size_t  noverrides,
char *  errbuf,
size_t  errlen 
)

Parses and validates a simspec file (system half + run half).

The overrides semantics match qpms_scatspec_parse_file(): they are applied on top of (and take precedence over) the file's vars section, and are shared by both halves.

Parameters
pathPath to the YAML file.
overridesOptional variable overrides (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_simspec_resonances_resolve_kdep()

qpms_errno_t qpms_simspec_resonances_resolve_kdep ( const qpms_simspec_t *  sim,
const qpms_simspec_resonances_t *  r,
cart3_t  k,
qpms_simspec_resonances_t *  out,
char *  errbuf,
size_t  errlen 
)

Resolves the k-dependent contour expressions of a solver spec.

The kind: bandstructure experiments may parametrise their explicit contour with the Rayleigh-anomaly expressions (the rayleighN constants and the per-G-family rayleigh(i, j[, l]) function; see rayleigh.h): such expressions are stored verbatim in the *_expr fields of the solver spec and evaluated at every Bloch vector of the sweep, so that the contour can follow an anomaly curve \(\omega(\mathbf{k}) = \hbar c |\mathbf{k}+\mathbf{G}| / n_b\) continuously in k. This function evaluates the stored expressions at the Bloch vector k [rad/m] (with the spec's variables and the background phase index of the system) and copies r to out with the expression-derived fields filled in, contour_kdep cleared, and the contour invariants re-validated (positive half-axes; rectangle corners ordered). Fields without an expression are carried over verbatim; the expression strings themselves are borrowed (not set in out), so out is a transient shallow copy — do not free it separately and do not let it outlive r and sim.

Returns
QPMS_SUCCESS, or QPMS_ERROR with errbuf filled in (invalid arguments, contour_kdep not set in r, an expression evaluation failure, or a violated contour invariant).

◆ qpms_simspec_solver_fingerprint()

int qpms_simspec_solver_fingerprint ( const qpms_simspec_resonances_t *  r,
char  hex[65] 
)

Computes a SHA-256 fingerprint of the k-independent solver specification.

Serialises the solver selection, contour/band-sugar specification, solver options, irrep selection and eigenvector-saving policy of a resonances-style experiment struct (also used for the solver part of other k-swept kinds) in a canonical form and hashes it. The Bloch vector is deliberately excluded: the fingerprint identifies the k-independent part of the specification, so that a k sweep over the same solver settings yields one fingerprint.

Returns
QPMS_SUCCESS, or QPMS_ERROR if the arguments are NULL or on an out of memory error.

◆ ssim_axis()

static int ssim_axis ( ssim_ctx_t *  c,
struct fy_node *  n,
const char *  where,
qpms_simspec_axis_t *  out,
_Bool  require_real 
)
static

Resolves one axis node (scalar | sequence | {start, stop, step} mapping).

When require_real is true, imaginary axis values are rejected.

◆ ssim_axis_segment_count()

static long ssim_axis_segment_count ( ssim_ctx_t *  c,
const char *  where,
_Complex double  start,
_Complex double  stop,
_Complex double  step 
)
static

Validates a segment's reachability and returns the generated point count, or -1 (the failure already reported).

◆ ssim_axis_vals_close()

static _Bool ssim_axis_vals_close ( _Complex double  a,
_Complex double  b 
)
inlinestatic

Whether two axis values are the same point (the segment-junction dedup; far above any rounding of the shared endpoints, far below any sensible step).

◆ ssim_contour_field()

static int ssim_contour_field ( ssim_ctx_t *  c,
struct fy_node *  n,
const char *  where,
_Bool  kdep_allowed,
_Bool  real_only,
_Complex double *  value,
char **  expr 
)
static

Parses a solver contour ("ellipse" | "rectangle" mapping). Parses a numeric contour field (plain number or specexpr expression). With kdep_allowed set, an expression referencing the Rayleigh-anomaly names is stored verbatim in expr instead of being evaluated (the kind: bandstructure contours are re-evaluated at every Bloch vector at run time; the parse-time probe fills the value in). Returns 1 when the expression was stored (the caller must then skip the value validation), 0 when value was evaluated, -1 on error.

◆ ssim_fail_number()

static void ssim_fail_number ( ssim_ctx_t *  c,
const char *  where,
const char *  buf,
const char *  err 
)
static

Reports a specexpr evaluation failure, augmented by the Rayleigh-expression availability context when the expression references the rayleigh names.

◆ ssim_install_rayleigh()

static int ssim_install_rayleigh ( ssim_ctx_t *  c,
const double  bloch[3] 
)
static

Creates the fixed-k Rayleigh-expression context of the current experiment and installs its hooks into the shared environment. The hooks resolve the rayleighN constants and the per-G-family rayleigh() function at the given Bloch vector (rad/m, 1-3 components; the unused ones stay 0). No-op on finite systems.

◆ ssim_parse_resonance_solver()

static int ssim_parse_resonance_solver ( ssim_ctx_t *  c,
struct fy_node *  node,
const char *  kindname,
qpms_simspec_resonances_t *  r,
_Bool  kdep_allowed 
)
static

Parses the solver mapping, band sugar, contour/band consistency, irreps and eigenvector policy of a resonances-style experiment. Shared by kind: resonances and the solver block of kind: bandstructure; kindname (e.g. "resonances", "bandstructure") parameterises the error messages. The caller has zeroed r and set the kind-specific defaults (NaN solver tolerances, irrep mode, eigenvector policy). With kdep_allowed set (bandstructure only), contour fields may be parametrised by the Rayleigh-anomaly expressions; the expressions are then stored verbatim (the contour is derived per Bloch vector at run time).

◆ ssim_parse_vars()

static int ssim_parse_vars ( ssim_ctx_t *  c,
struct fy_node *  node 
)
static

Parses the file's vars section into the run-half environment.

Mirrors sspec_parse_vars(): overrides (already present in the env) win.

◆ ssim_rayleigh_token()

static _Bool ssim_rayleigh_token ( const char *  s)
static

Whether the expression text references the Rayleigh-anomaly names ("rayleighN" constants or the "rayleigh(...)" function); a scan of the identifier tokens (the specexpr identifiers cannot contain quotes or comments, so the scan is exact).

◆ ssim_resolve_kdep_at()

static int ssim_resolve_kdep_at ( const qpms_scatspec_t *  spec,
const qpms_specexpr_env_t *  base_env,
const qpms_simspec_resonances_t *  r,
cart3_t  k,
qpms_simspec_resonances_t *  out,
char *  errbuf,
size_t  errlen 
)
static

Evaluates the k-dependent contour expressions of r at the Bloch vector k (the shared implementation of the parse-time probe and qpms_simspec_resonances_resolve_kdep()). base_env supplies the spec's variables (borrowed; hooks, if any, are inherited into the temporary environment). out receives a shallow copy of r with the expression-derived fields evaluated, contour_kdep cleared and the expression strings NULLed (they stay owned by r).