Skip to content

Fix simplex noise gradient lookups and permutation indices - #2003

Draft
sbryngelson wants to merge 2 commits into
MFlowCode:masterfrom
sbryngelson:fix/simplex-noise-bounds
Draft

sbryngelson wants to merge 2 commits into
MFlowCode:masterfrom
sbryngelson:fix/simplex-noise-bounds

Conversation

@sbryngelson

@sbryngelson sbryngelson commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

f_simplex2d indexed grad2(g + 1, :) with g already in 1..10, reading
past the end of the table (gfortran: "Index '11' of dimension 1 of
array 'grad2' above upper bound of 10"). f_simplex3d added + 1 to every
permutation lookup although p_vec is declared 0:511, which shifted the
hash and could index p_vec(512). Both gradient tables were also listed
row by row but reshaped in column-major order, so grad3 held duplicate
and missing directions instead of the 12 cube-edge vectors.

Index the tables directly, drop the extra offsets so the lookups match
Gustavson's reference (perm[ii + perm[jj + perm[kk]]] % 12), reshape
with order=[2, 1], and replace real(i + j, 8) with real(i + j, wp).

Verified against a Python port of the reference algorithm at 20000
random points (max difference 4e-16 in 2D, 1e-16 in 3D) with
-fcheck=all clean, and with 2D and 3D pre_process runs using simplex
density and velocity perturbation. No test or example in the suite
uses simplex perturbation (3D_IGR_33jet is skipped), so no goldens
change.

f_simplex2d indexed grad2(g + 1, :) with g already in 1..10, reading
past the end of the table (gfortran: "Index '11' of dimension 1 of
array 'grad2' above upper bound of 10"). f_simplex3d added + 1 to every
permutation lookup although p_vec is declared 0:511, which shifted the
hash and could index p_vec(512). Both gradient tables were also listed
row by row but reshaped in column-major order, so grad3 held duplicate
and missing directions instead of the 12 cube-edge vectors.

Index the tables directly, drop the extra offsets so the lookups match
Gustavson's reference (perm[ii + perm[jj + perm[kk]]] % 12), reshape
with order=[2, 1], and replace real(i + j, 8) with real(i + j, wp).

Verified against a Python port of the reference algorithm at 20000
random points (max difference 4e-16 in 2D, 1e-16 in 3D) with
-fcheck=all clean, and with 2D and 3D pre_process runs using simplex
density and velocity perturbation. No test or example in the suite
uses simplex perturbation (3D_IGR_33jet is skipped), so no goldens
change.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant