grid_convergence#

Checks whether a powder spectrum is converged with respect to the orientation grid by simulating it again on a finer grid and comparing the two.

torchspin.grid_convergence(sys, exp, opt=None, refine=4, cosine_tol=0.9999, amplitude_tol=0.01)[source]#

Check whether Options.GridSize is fine enough for a pepper simulation.

Runs torchspin.pepper() twice, once with the GridSize of opt and once with the coarse knot count multiplied by refine, and compares the two spectra (shape by cosine similarity, height by peak-amplitude ratio). Only Options.GridSize is changed between the two runs; every other option (GridSymmetry, Method, separate, …) is passed through unchanged.

Parameters:
  • sys (SpinSystem or list of SpinSystem) – Spin system(s), exactly as for torchspin.pepper(). Tensors must not require grad (see Raises).

  • exp (Experiment) – Experiment, exactly as for torchspin.pepper() (field or frequency sweep).

  • opt (Options, optional) – Options; its GridSize (int N or [N_coarse, N_interp]) is the grid under test. Default None = Options(), i.e. GridSize = [19, 4].

  • refine (int, default 4) – Factor applied to the coarse knot count for the reference run: N -> refine * N or [N, k] -> [refine * N, k]. Must be at least 2. The cost of the check is dominated by the refined simulation, whose orientation count grows roughly as refine**2 for 2-D grids.

  • cosine_tol (float, default 0.9999) – Minimum cosine similarity for converged.

  • amplitude_tol (float, default 0.01) – Maximum relative change of the peak amplitude for converged (0.01 = 1 %). Both defaults are a tenth of the repository’s parity tolerances, so that grid error stays well below the level the simulators are validated at.

Returns:

GridConvergence – Cosine similarity, amplitude ratio, the verdict, both grid sizes and the tolerances used.

Raises:
  • ValueError – If refine < 2, and anything torchspin.pepper() raises for these inputs.

  • RuntimeError – If an input tensor requires grad: pepper then returns a spectrum on the autograd graph, which cannot be converted to NumPy here. Pass detached values.

Return type:

GridConvergence

See also

GridConvergence

The result object.

torchspin.pepper

The simulator being checked.

torchspin.Options

GridSize and GridSymmetry.

Notes

A single spectrum does not reveal grid error, and a heuristic on the field step between knots is not a reliable substitute (see the module docstring); comparing with a refined grid is. The check is meaningful only when pepper uses a powder grid: with Experiment.Orientations the grid size is ignored by pepper and the two spectra are identical.

Examples

A Cu(II) powder spectrum with a narrow line is not converged at the default GridSize = [19, 4]:

>>> from torchspin import SpinSystem, Experiment, Options, grid_convergence
>>> cu = SpinSystem(S=0.5, g=[2.05, 2.05, 2.25], Nucs='63Cu',
...                 A=[50, 50, 500], lw=0.54)
>>> exp = Experiment(mwFreq=9.5, Range=[260, 360], nPoints=1024)
>>> c = grid_convergence(cu, exp, Options(GridSize=[19, 4]))
>>> c.converged, c.refined_grid_size
(False, [76, 4])
>>> round(c.amplitude_ratio, 2)
1.02
>>> print(c.report())
GridSize=[19, 4] is NOT converged: refining to [76, 4] changes the amplitude by 1.7 % ...
class torchspin.GridConvergence(cosine, amplitude_ratio, converged, grid_size, refined_grid_size, cosine_tol, amplitude_tol)[source]#

Result of grid_convergence(): a spectrum compared with a refined one.

Returned by grid_convergence(); not normally constructed by hand. str(result) gives a one-line summary and report() a sentence with a recommendation.

Parameters:
  • cosine (float) – Cosine similarity a.b / (|a| |b|) between the spectrum on the requested grid (a) and on the refined grid (b), both flattened (all rows when pepper returns a 2-D array). 1.0 means identical shape; 1.0 is also returned when either spectrum is all zero.

  • amplitude_ratio (float) – max|a| / max|b|: peak amplitude on the requested grid over that on the refined grid (1.0 when the refined spectrum is all zero). This is usually the first quantity to move, because the derivative extremum at a turning point is the most grid-sensitive number in a powder spectrum.

  • converged (bool) – cosine >= cosine_tol and abs(amplitude_ratio - 1) <= amplitude_tol.

  • grid_size (list of int) – The Options.GridSize under test as a list: [N] for an integer GridSize or [N_coarse, N_interp].

  • refined_grid_size (list of int) – The reference grid: [refine * N] or [refine * N_coarse, N_interp] (the interpolation factor is kept).

  • cosine_tol (float) – Cosine threshold used for converged.

  • amplitude_tol (float) – Relative amplitude threshold used for converged.

See also

grid_convergence

Produces this result.

report()[source]#

Return a sentence describing the result and what to do about it.

Returns:

str – For a converged grid, the size of the change on refinement; for an unconverged one, the change and the recommendation to use refined_grid_size (or finer) and re-check.

Return type:

str

See also

GridSize and GridSymmetry in Options.