Skip to content

Commit b1ca2cd

Browse files
committed
feat(interp): add P2Fold golden and accuracy regression coverage
1 parent 5646cbd commit b1ca2cd

10 files changed

Lines changed: 588 additions & 14 deletions

File tree

‎packages/loop_interpolation/tests/golden/README.md‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ python -m tests.golden.run --case fold_sparse --engine DFI --vti # one run + a
2626
| `planar_noisy` | same plane | z +-0.01, normals +-3 deg | smoothing versus over-fitting |
2727
| `dome` | Gaussian dome, relief 0.2 | 3 horizons, 9 boreholes, 5 normals | crest flattening, curved horizons |
2828
| `fold_section` | cylindrical fold (wavelength 0.6, amplitude 0.12) | one cross-section at y = 0.5 | plain interpolation along the fold axis |
29-
| `fold_sparse` | same fold | 2 value points + 1 normal | fold constraints (`DFI`, `FDFold`) against none (`FD`, `P1`, `P2`) |
29+
| `fold_sparse` | same fold | 2 value points + 1 normal | fold constraints (`DFI`, `FDFold`, `P2Fold`) against none (`FD`, `P1`, `P2`) |
3030
| `refolded_sparse` | F2 fold (axis y) refolded by F3 (axis x): dome and basin, `s = z - A sin(k2 x) - B sin(k3 y)` | 3 contacts + 6 normals | the `fold_axis_rotation` path: the hinge plunge changes along the axis (as at Bermagui) |
3131

32-
Engines: `FD` (17^3 nodes), `P1` (17^3 nodes), `P2` (8^3 cells, 4.4k unknowns), `DFI` (P1 + fold), `FDFold` (FD + fold).
32+
Engines: `FD` (17^3 nodes), `P1` (17^3 nodes), `P2` (8^3 cells, 4.4k unknowns), `DFI` (P1 + fold), `FDFold` (FD + fold), `P2Fold` (P2 + fold, 8^3 cells / 4401 unknowns).
3333
Fold frame is analytic (gx = x, gy = y), fold axis (0, -1, 0). With that axis `dgz` points against the
3434
normals, which is the library convention (`fold_norm = -1`).
3535

@@ -99,5 +99,60 @@ Choose bounds in `test_golden.py::BOUNDS` about 1.5x above what the code gives.
9999
## Not covered yet
100100

101101
Faults (equality constraints, review bug 1), inequality constraints (bug 2), tangent and interface
102-
constraints, regularisation variants, `fold_weights` options, P2 fold (no class yet),
102+
constraints, regularisation variants, additional `fold_weights` options,
103103
`mask_fn`, and meshes that are not unit cubes. DFI on a cube mesh only: no graded or unstructured mesh.
104+
105+
106+
## P2 fold coverage (Phase 3)
107+
108+
P2Fold is pinned on `fold_sparse` and `refolded_sparse`, including fields,
109+
gradients and all constraint-family fingerprints. Only the four new P2Fold
110+
baseline files were generated; existing golden files were preserved.
111+
The figures were reviewed: fold shape and gradient directions agree well, with
112+
a remaining vertical offset in the refolded section and spatially varying value
113+
error. Neither gauss4 result has inverted gradients in the inner evaluation box.
114+
Bounds are approximately 1.5 times the observed RMS and median direction errors;
115+
the refolded bound stays above the fold event's approximately 2 degree model floor.
116+
117+
| Case | Rule | Value RMS | Median gradient error | P90 gradient error |
118+
|---|---|---:|---:|---:|
119+
| fold_sparse | centroid | 0.00329 | 1.364 deg | 5.303 deg |
120+
| fold_sparse | gauss4 | 0.01652 | 0.856 deg | 2.124 deg |
121+
| refolded_sparse | centroid | 0.03068 | 4.422 deg | 12.571 deg |
122+
| refolded_sparse | gauss4 | 0.05507 | 2.179 deg | 5.795 deg |
123+
124+
These comparisons use the same fixed seed, explicit unit-cube mesh bounds
125+
(zero buffer), data, solver tolerance and 4401 unknowns. Gauss4 improves gradient
126+
direction in both cases, now checked by a regression test. It does **not** improve
127+
value RMS or fit to the sparse normals; no universal accuracy claim is made.
128+
Centroid also has one inverted inner probe on the refolded case.
129+
The DFI comparison uses a separate budget-matched mesh with 15 x 15 x 16
130+
cells (4352 unknowns) versus P2Fold's 4401, a 1.13% difference. These mesh
131+
families do not have identical node counts. Both supports have explicit unit-cube
132+
bounds and no buffer, asserted in the test, and use the same fixed seed. The
133+
existing, larger DFI golden mesh and its baseline are unchanged.
134+
135+
Unit tests pin total weighted operator energy (squared Frobenius norm) for each
136+
fold family between quadrature rules, weight sensitivity (10 to 50), fold norm
137+
sign against DFI and `resolve_fold_norm_target`, update persistence, constant-angle
138+
folds without orientation data, and dropped NaN row counts. The energy test does
139+
not assert equal energy for every field: normalising the shape-derivative rows
140+
changes the residual at different sample locations. There is no LinearOperator
141+
path here, so the optional adjoint test does not apply.
142+
Model integration checks folded-fold-frame construction without DFI fallback and
143+
preservation of the P2Fold interpolator type after save/load.
144+
145+
146+
Validation on Windows / Python 3.13 (shared environment, source paths explicitly
147+
pointed at this worktree): interpolation package **577 passed, 34 skipped,
148+
2 xfailed in 56.82 s**; golden suite **143 passed, 9 skipped, 2 xfailed in
149+
27.92 s**; P2Fold model integration **5 passed in 11.55 s**. The final golden run was standalone; the package and integration runs overlapped
150+
with the root suite. These are wall-clock observations rather than a benchmark.
151+
For sandboxed runs, pass `--basetemp=<worktree-local-directory>` when the default
152+
pytest temporary directory is inaccessible. Existing lower-boundary NaNs remain
153+
strict expected failures. The new P2Fold cases add two small 8^3-cell solves,
154+
plus weight-sensitivity, quadrature-comparison and budget-matched DFI checks.
155+
156+
The full root suite also passed: **727 passed, 3 skipped, 21 warnings in
157+
407.56 s (6 min 48 s)**, using a worktree-local pytest temporary directory.
158+
No test failures remain.

‎packages/loop_interpolation/tests/golden/conftest.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def pytest_addoption(parser):
2222

2323
_RECORDS: list[dict] = []
2424

25-
_FOLD_FOUR = [("fold_sparse", e, False) for e in ("FD", "P1", "DFI", "FDFold")]
25+
_FOLD_ENGINES = [("fold_sparse", e, False) for e in ("FD", "P1", "DFI", "FDFold", "P2Fold")]
2626
_BY_NAME = {
27-
"test_fold_constraints_help": _FOLD_FOUR,
28-
"test_refolded_fold_constraints_help": [("refolded_sparse", e, False) for e in ("FD", "P1", "DFI", "FDFold")],
27+
"test_fold_constraints_help": _FOLD_ENGINES,
28+
"test_refolded_fold_constraints_help": [("refolded_sparse", e, False) for e in ("FD", "P1", "DFI", "FDFold", "P2Fold")],
2929
"test_p2_not_worse_than_p1_on_smooth_dome": [("dome", "P1", False), ("dome", "P2", False)],
3030
"test_known_p2_normal_weight_has_effect": [("planar_noisy", "P2", False)],
3131
"test_dfi_is_repeatable_with_reseed": [("fold_sparse", "DFI", False)],

‎packages/loop_interpolation/tests/golden/engines.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
FiniteDifferenceInterpolator,
1212
FoldEvent,
1313
P1Interpolator,
14+
P2FoldInterpolator,
1415
P2Interpolator,
1516
P2UnstructuredTetMesh,
1617
StructuredGrid,
@@ -19,7 +20,7 @@
1920

2021
N_NODES = 17 # nodes per axis for FD and P1 (16 cells)
2122
P2_CELLS = 8 # P2 has ~8 nodes per cell: 8 cells gives ~4k unknowns, similar to FD/P1
22-
ENGINES = ("FD", "P1", "P2", "DFI", "FDFold")
23+
ENGINES = ("FD", "P1", "P2", "DFI", "FDFold", "P2Fold")
2324
# Pinned. The default CG stops early and still returns True (review bug 3), so the golden
2425
# fields use a converged solve. `solve_default` below reproduces what a user gets today.
2526
SOLVER = "cg"
@@ -70,8 +71,10 @@ def make_interpolator(engine: str, case):
7071
if engine == "P1":
7172
return P1Interpolator(mesh)
7273
return DiscreteFoldInterpolator(mesh, fold=make_fold_event(case))
73-
if engine == "P2":
74+
if engine in ("P2", "P2Fold"):
7475
mesh = P2UnstructuredTetMesh(origin=origin, step_vector=np.full(3, 1.0 / P2_CELLS), nsteps_cells=np.full(3, P2_CELLS))
76+
if engine == "P2Fold":
77+
return P2FoldInterpolator(mesh, fold=make_fold_event(case))
7578
return P2Interpolator(mesh)
7679
raise ValueError(engine)
7780

@@ -84,7 +87,7 @@ def add_data(interp, case):
8487

8588

8689
def setup_kwargs(engine: str) -> dict:
87-
if engine in ("DFI", "FDFold"):
90+
if engine in ("DFI", "FDFold", "P2Fold"):
8891
return {}
8992
return {}
9093

‎packages/loop_interpolation/tests/golden/geomodels.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def fold_sparse() -> Case:
203203
"The Bermagui situation: little data, strong fold. The fold constraints must supply the "
204204
"gradient direction everywhere. The FD and P1 columns have no fold constraint and show "
205205
"what is gained. A drop in DFI/FDFold accuracy is a fold-constraint regression.",
206-
s, g, values, np.c_[p, g(p)], fold=fold, engines=("FD", "P1", "P2", "DFI", "FDFold"),
206+
s, g, values, np.c_[p, g(p)], fold=fold, engines=("FD", "P1", "P2", "DFI", "FDFold", "P2Fold"),
207207
)
208208

209209

@@ -245,7 +245,7 @@ def axis_rotation(gy):
245245
"The Bermagui geometry in miniature: the hinge plunge changes along the axis, so a constant "
246246
"axis is wrong. Tests the fold_axis_rotation path of the fold interpolators (DFI, FDFold). "
247247
"FD, P1 and P2 have no fold constraint and show what the fold event adds.",
248-
s, g, values, np.c_[p, g(p)], fold=fold, engines=("FD", "P1", "P2", "DFI", "FDFold"),
248+
s, g, values, np.c_[p, g(p)], fold=fold, engines=("FD", "P1", "P2", "DFI", "FDFold", "P2Fold"),
249249
)
250250

251251

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
{
2+
"case": "fold_sparse",
3+
"constraints": {
4+
"fold axis": {
5+
"A_fro": 101.19288512538814,
6+
"A_rowmoment": 3.76099151822018e-11,
7+
"A_sum": 1.9095836023552692e-14,
8+
"WA_fro": 505.9644256269407,
9+
"b_norm": 0.0,
10+
"b_sum": 0.0,
11+
"nnz": 102400,
12+
"shape": [
13+
10240,
14+
4401
15+
],
16+
"w_max": 5.0,
17+
"w_min": 5.0
18+
},
19+
"fold normalisation": {
20+
"A_fro": 101.19288512538814,
21+
"A_rowmoment": -1.2675105409698517e-10,
22+
"A_sum": 2.2593038551121936e-14,
23+
"WA_fro": 50.59644256269407,
24+
"b_norm": 4.718587877993954,
25+
"b_sum": -469.33398723375257,
26+
"nnz": 102400,
27+
"shape": [
28+
10240,
29+
4401
30+
],
31+
"w_max": 0.5,
32+
"w_min": 0.5
33+
},
34+
"fold orientation": {
35+
"A_fro": 101.19288512538814,
36+
"A_rowmoment": -4.589750801642367e-11,
37+
"A_sum": 2.919886554764162e-14,
38+
"WA_fro": 505.9644256269407,
39+
"b_norm": 0.0,
40+
"b_sum": 0.0,
41+
"nnz": 102400,
42+
"shape": [
43+
10240,
44+
4401
45+
],
46+
"w_max": 5.0,
47+
"w_min": 5.0
48+
},
49+
"gradsteepness_0": {
50+
"A_fro": 43.3589667773576,
51+
"A_rowmoment": -5.4001247917767614e-12,
52+
"A_sum": 2.8310687127941492e-14,
53+
"WA_fro": 0.43358966777357605,
54+
"b_norm": 0.0,
55+
"b_sum": 0.0,
56+
"nnz": 18800,
57+
"shape": [
58+
1880,
59+
4401
60+
],
61+
"w_max": 0.01,
62+
"w_min": 0.01
63+
},
64+
"gradsteepness_1": {
65+
"A_fro": 43.3589667773576,
66+
"A_rowmoment": 5.5990767577895895e-12,
67+
"A_sum": -4.440892098500626e-16,
68+
"WA_fro": 0.43358966777357605,
69+
"b_norm": 0.0,
70+
"b_sum": 0.0,
71+
"nnz": 18800,
72+
"shape": [
73+
1880,
74+
4401
75+
],
76+
"w_max": 0.01,
77+
"w_min": 0.01
78+
},
79+
"gradsteepness_2": {
80+
"A_fro": 43.3589667773576,
81+
"A_rowmoment": 3.410605131648481e-12,
82+
"A_sum": -1.482147737874584e-14,
83+
"WA_fro": 0.43358966777357605,
84+
"b_norm": 0.0,
85+
"b_sum": 0.0,
86+
"nnz": 18800,
87+
"shape": [
88+
1880,
89+
4401
90+
],
91+
"w_max": 0.01,
92+
"w_min": 0.01
93+
},
94+
"gradsteepness_3": {
95+
"A_fro": 43.3589667773576,
96+
"A_rowmoment": -2.3590018827235326e-12,
97+
"A_sum": 3.419486915845482e-14,
98+
"WA_fro": 0.43358966777357605,
99+
"b_norm": 0.0,
100+
"b_sum": 0.0,
101+
"nnz": 18800,
102+
"shape": [
103+
1880,
104+
4401
105+
],
106+
"w_max": 0.01,
107+
"w_min": 0.01
108+
},
109+
"gradsteepness_4": {
110+
"A_fro": 43.3589667773576,
111+
"A_rowmoment": -6.536993168992922e-13,
112+
"A_sum": -9.658940314238862e-15,
113+
"WA_fro": 0.43358966777357605,
114+
"b_norm": 0.0,
115+
"b_sum": 0.0,
116+
"nnz": 18800,
117+
"shape": [
118+
1880,
119+
4401
120+
],
121+
"w_max": 0.01,
122+
"w_min": 0.01
123+
},
124+
"gradsteepness_5": {
125+
"A_fro": 43.3589667773576,
126+
"A_rowmoment": -5.4569682106375694e-12,
127+
"A_sum": 1.532107773982716e-14,
128+
"WA_fro": 0.43358966777357605,
129+
"b_norm": 0.0,
130+
"b_sum": 0.0,
131+
"nnz": 18800,
132+
"shape": [
133+
1880,
134+
4401
135+
],
136+
"w_max": 0.01,
137+
"w_min": 0.01
138+
},
139+
"norm": {
140+
"A_fro": 1.7320508075688774,
141+
"A_rowmoment": 0.0,
142+
"A_sum": 1.1102230246251565e-16,
143+
"WA_fro": 1.7320508075688774,
144+
"b_norm": 0.028951884986248407,
145+
"b_sum": 0.00911159165725945,
146+
"nnz": 30,
147+
"shape": [
148+
3,
149+
4401
150+
],
151+
"w_max": 1.0,
152+
"w_min": 1.0
153+
},
154+
"shared element jump cp0": {
155+
"A_fro": 79.16745742910703,
156+
"A_rowmoment": -6.94768687026226e-11,
157+
"A_sum": 1.2323475573339238e-14,
158+
"WA_fro": 1.3194576238184506,
159+
"b_norm": 0.0,
160+
"b_sum": 0.0,
161+
"nnz": 66304,
162+
"shape": [
163+
4736,
164+
4401
165+
],
166+
"w_max": 0.016666666666666666,
167+
"w_min": 0.016666666666666666
168+
},
169+
"shared element jump cp1": {
170+
"A_fro": 78.44037628402191,
171+
"A_rowmoment": 5.865530283699627e-12,
172+
"A_sum": -4.274358644806853e-15,
173+
"WA_fro": 1.3073396047336985,
174+
"b_norm": 0.0,
175+
"b_sum": 0.0,
176+
"nnz": 66304,
177+
"shape": [
178+
4736,
179+
4401
180+
],
181+
"w_max": 0.016666666666666666,
182+
"w_min": 0.016666666666666666
183+
},
184+
"shared element jump cp2": {
185+
"A_fro": 79.16745742910703,
186+
"A_rowmoment": -4.163069888818427e-11,
187+
"A_sum": -1.7541523789077473e-14,
188+
"WA_fro": 1.3194576238184506,
189+
"b_norm": 0.0,
190+
"b_sum": 0.0,
191+
"nnz": 66304,
192+
"shape": [
193+
4736,
194+
4401
195+
],
196+
"w_max": 0.016666666666666666,
197+
"w_min": 0.016666666666666666
198+
},
199+
"value": {
200+
"A_fro": 1.4142135623730951,
201+
"A_rowmoment": 4.114398198182916,
202+
"A_sum": 2.7429321321219446,
203+
"WA_fro": 1.4142135623730951,
204+
"b_norm": 0.9697729554789508,
205+
"b_sum": 1.3714660660609719,
206+
"nnz": 20,
207+
"shape": [
208+
2,
209+
4401
210+
],
211+
"w_max": 1.0,
212+
"w_min": 1.0
213+
}
214+
},
215+
"dof": 4401,
216+
"engine": "P2Fold",
217+
"metrics": {
218+
"angle_max": 5.4422019707379885,
219+
"angle_median": 0.8563840084691864,
220+
"angle_p90": 2.1238213384424816,
221+
"data_normal_angle_max": 3.781538372444907,
222+
"data_value_rms": 0.00019301391264059387,
223+
"flipped_fraction": 0.0,
224+
"grad_norm_ratio_median": 0.9328958214011491,
225+
"nan_fraction": 0.0,
226+
"value_max": 0.03401787240323284,
227+
"value_rms": 0.016515178787970264
228+
}
229+
}
Binary file not shown.

0 commit comments

Comments
 (0)