Skip to content

First draft of zridd-replacement, based on ideas from KL - #2584

Open
willend wants to merge 4 commits into
mainfrom
Phonon_simple_zridd_revamp
Open

willend wants to merge 4 commits into
mainfrom
Phonon_simple_zridd_revamp

Conversation

@willend

@willend willend commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Free-form text area

Please describe what your PR is adding in terms of features or bugfixes:

Replaces the zridd (Ridder's method, Numerical Recipes) root scan in Phonon_simple with a 3-point scan per sub-interval, following an idea by Kim Lefmann:

  • each sub-interval is sampled at its ends and midpoint; a half with a sign change is refined with the Illinois variant of regula falsi;
  • if all three samples share a sign, the parabola through them flags a possible hidden pair of roots around a dispersion extremum, which is accepted only if omega_q really changes sign at the parabola's vertex.

Numerical behaviour: the root set can differ from main where the old scan missed or split a pair of roots inside one sub-interval; roots agree within ROOTACC otherwise. Samples_Phonon (seeded, 2e6) and the Test_Dispersion_relation_TAS phonon %Test case give bit-identical output to main. The e_steps_low/e_steps_high defaults are lowered from 50 to 10, backed by a root-count comparison posted below; at the new default Samples_Phonon runs in 7.4 s instead of 17.0 s.


Declaration of use of AI-tools

  • Please add a checkmark here if you used AI-tools during the work for this contribution
  • Furter, please describe how / where and for what the tools were used:

Claude used to write revised algorithm based on inputs from Kim Lefmann in an email thread.


Development OS / boundary conditions

Please describe what OS you developed and tested your additions on, and if any special dependencies are required:

Developed on macOS Tahoe 26.x, Silicon/arm64


PR Checklist for contributing to McStas/McXtrace

For a coherent and useful contribution to McStas/McXtrace, please fill in relevant parts of the checklist:

  • My contribution includes patches to an existing component file

    • I have used the mcdoc utility and rendered a reasonable documentation page for the component (please attach as screenshot in comments!)
    • I have ensured that basic use of the component is OK (e.g. an instrument using it compiles?)
    • I have used the mctest utility to test one or more instruments making use of the component (please attach mcviewtest report as screenshot in comments)
    • I have used the mccode-clangformat tool to apply the standard McCode component indentation scheme
    • I have used the mcrun --c-lint "linter" and followed advice to remove most / all warnings that are raised

@willend

willend commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

mcdoc:
Screenshot 2026-08-03 at 21 36 07

mctest: (run by CI across platforms on 1st commit)
Screenshot 2026-08-03 at 21 44 06

@willend
willend force-pushed the Phonon_simple_zridd_revamp branch from ac44a99 to 6efd329 Compare August 4, 2026 16:30
@willend
willend marked this pull request as draft September 10, 2026 08:22
@willend

willend commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

TODO

Note: the branch has since been squashed into 5bc4d93 (root finder: 0b6891b, f680ef6, 1a848ac and the original draft) and 27ad31a (default e_steps 50 → 10, was f2910de). Hashes below refer to the pre-squash commits.

Review of the 3-point root finder. CI is green and the branch merges cleanly; the issues below were found with a standalone harness that runs the PR's find_roots_3pt logic unchanged (with f(x) in place of omega_q(neutron, phonon)), and with seeded Samples_Phonon runs (macOS/clang, mccode-antlr).

Blocking

Both fixed in 0b6891b.

  • The parabola branch adds roots that aren't roots. If the parabola through the three samples dips past zero but the real function doesn't, polish_root() (8 Newton steps, no check) still appends both results. For f = 0.002 + 50(x−0.3)⁴ (no roots) the harness returned x=0.513, where f=0.11, and x=−0.114, outside the interval. In the comp that means a wrong v_f, possibly negative, and a wrong nf in the weight. Fix: evaluate f at the parabola's vertex; if its sign differs from y2, bisect [sample, vertex] and [vertex, sample] with the existing bisect_root(), otherwise report no roots. This drops polish_root/polish_root_gpu, and every reported root is a real bracketed one.
  • A zero exactly on a sample point is counted twice. When y2 == 0, both halves return x2; the same happens across neighbouring sub-intervals. Fix: bisect_root() owns only its right endpoint (if (fh == 0) return x2; if (fl == 0 || fl*fh > 0) return UNUSED;), and the callers use s1*s2 < 0 || s2 == 0. The old zridd scan had the same issue across sub-intervals.

Should fix

Code items done in f680ef6.

  • Performance. Samples_Phonon, seeded, 2e6 rays: results are bit-identical to main at every e_steps, but the run takes 1.8–2.5× longer (e_steps=50: 17.0 s vs 30.2 s; 10: 6.3 s vs 14.1 s; 1: 3.9 s vs 10.0 s). Most of the cost is bisection, about 40 halvings to reach ROOTACC = 1e-8 m/s. Options: an Illinois/regula-falsi step (simple textbook method, not taken from Numerical Recipes) or a relative tolerance. Smaller savings: pass the known fl/fh into bisect_root, and reuse each sub-interval's end value as the next one's start. Done in f680ef6: Illinois refinement, end values passed on. Samples_Phonon 2e6: 19.8 s at e_steps=50 (main 17.0 s, before 30.2 s), 6.7 s at 10 (main 6.3 s), 3.3 s at 1 (main 3.9 s); output identical.
  • Back up the "markedly fewer sub-intervals" claim or drop it. Samples_Phonon can't show it, since main already gets identical results at e_steps=1. Either add a case with a real hidden root pair, or remove the claim from the header and the %P text. If it holds, lower the defaults (50) so the speed-up reaches users. Backed by a root-count comparison (table); defaults lowered to 10 in f2910de.
  • One code path for CPU and GPU. findroots is only ever called with omega_q, so drop the function pointer and the _gpu twins (bisect_root, polish_root, find_roots_3pt, findroots), keeping one #pragma acc routine version of each. Done in f680ef6.
  • Call out the numerical change in the PR text. Root sets can differ wherever the old scan missed or split root pairs. Added to the PR description.

Cleanup

  • Cut the 12-line "Modified by" entry in the header to one line; the algorithm is already described in the code comment. Done in 1a848ac.
  • Fill in the PR template: free-form text and development OS are empty. Free-form text filled in; development OS still open.
  • Squash the 13 "Merge branch 'main'" commits when merging. Done: the branch is rebuilt on main as two commits and force-pushed.
  • fatalerror_cpu: exit(1) → exit(-1). Resolved by removing fatalerror/fatalerror_cpu, which have no callers left (1a848ac).

@willend

willend commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Root-count comparison: old zridd scan vs 3-point scan

The intensity tests can't separate the two scans (only 0.1–0.7% of states have more than two roots), so this compares the root sets directly. A standalone harness runs the real omega_q and both scans, taken verbatim from the SHARE sections of main and this branch, with only a call counter added. For each neutron state, the reference root set comes from a fine sign-change scan with 20 000 sub-intervals per energy range. Each case uses 20 000 random states with Pb parameters (a = 4.95), final directions over 4π, and E_i drawn uniformly.

Missed roots, as a percentage of all reference roots (old / new):

e_steps c=8, E_i 3–40 c=10, E_i 3–40 c=8, E_i 3–150 c=10, random incident direction omega_q calls per state (old / new)
1 0.55 / 0.08 0.75 / 0.25 0.12 / 0.02 0.80 / 0.20 26 / 21
2 0.27 / 0.015 0.42 / 0.06 0.06 / 0.005 0.36 / 0.03 28 / 24
5 0.03 / 0 0.05 / 0.005 0.005 / 0 0.08 / 0 37 / 34
10 0 / 0 0.02 / 0 0.005 / 0 0.01 / 0 55 / 53
20 0 / 0 0.01 / 0 0 / 0 0 / 0 94 / 92
50 0 / 0 0.005 / 0 0 / 0 0 / 0 212 / 212
  • At the same e_steps, the new scan misses 3–7× fewer roots; neither scan ever reports an extra root.
  • The new scan has no misses from e_steps=10 in every case. With c=10 the old scan still missed roots at the old default of 50.
  • So the defaults are lowered to 10 in f2910de: about 4× fewer omega_q calls in the root search.
  • At the new default, Samples_Phonon (seeded, 2e6 rays) gives output identical to main in 7.4 s against 17.0 s. The Test_Dispersion_relation_TAS phonon %Test case is also identical (1.79066e-10).
  • Not covered: gap > 0, other lattice parameters, GPU.

willend and others added 2 commits November 15, 2026 13:41
Following an idea by Kim Lefmann, each sub-interval of the k_f scan is
sampled at its ends and midpoint:
- a half with a sign change is refined with the Illinois variant of
  regula falsi;
- if all three samples share a sign, the parabola through them flags a
  possible hidden pair of roots around a dispersion extremum, accepted
  only if omega_q really changes sign at the parabola's vertex.

This drops the Numerical Recipes zridd (Ridder's method), reports each
root once (also when it falls on a sample point), evaluates every grid
point once, and uses a single #pragma acc routine path for CPU and GPU.
Unused fatalerror helpers removed.

Root sets can differ from before where the old scan missed or split a
pair of roots within one sub-interval; otherwise roots agree within
ROOTACC. Samples_Phonon and the Test_Dispersion_relation_TAS phonon
test give identical output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A root-count comparison (real omega_q, 2e4 random states per case,
Pb-like parameters, against a 2e4-step reference scan) finds all roots
at 10 with the 3-point scan, while the old zridd scan still missed some
at 50. Default runs make ~4x fewer omega_q calls in the root search.
Samples_Phonon and the Test_Dispersion_relation_TAS phonon test give
identical output to main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@willend
willend force-pushed the Phonon_simple_zridd_revamp branch from f2910de to 27ad31a Compare November 15, 2026 12:41
@willend
willend marked this pull request as ready for review November 15, 2026 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant