Skip to content

Commit 67dbcc8

Browse files
lachlangroseclaude
andauthored
fix(fault): keep a step direction where a cutting fault leaves no frame gradient (#425)
Since #421 a gradient whose tetrahedron still crosses a fault after the refinement is NaN. A fault whose frame is cut by younger faults takes the step direction of apply_to_points from that gradient, so a point on a cutting fault was moved to NaN. A builder that restores a regular grid through such a fault then failed with "Cannot resize the grid" (seen with a chain of cuts: D cut by C, C by B, B by A; 215 of 10890 grid points of D were NaN). Where the frame of a cut fault has a NaN gradient at a finite point, use the gradient of the frame without the cutting faults there; a step only needs a direction. A frame without cutting faults keeps a NaN gradient, so its points stay NaN as before (test_fault_frame_validation). A point with a NaN coordinate is not moved. Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 315dff4 commit 67dbcc8

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎LoopStructural/modelling/features/fault/_fault_segment.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,15 @@ def apply_to_points(
579579
newp[mask, :], block, restored_through=restored_through
580580
)
581581
g = self.__getitem__(1).evaluate_gradient(newp[mask, :], ignore_regions=True, **kwargs)
582+
# A frame cut by younger faults has no single gradient on a cutting fault, so the
583+
# gradient is NaN there (#421). A step only needs a direction: use the gradient
584+
# of the frame without the cutting faults at that point. A frame without cutting
585+
# faults keeps a NaN gradient, so its points stay NaN (spec 005).
586+
missing = np.isnan(g).any(axis=1) & ~np.isnan(newp[mask, :]).any(axis=1)
587+
if np.any(missing) and getattr(self.__getitem__(1), "faults", None):
588+
g[missing] = self.__getitem__(1).interpolator.evaluate_gradient(
589+
newp[mask, :][missing]
590+
)
582591
gx, gy, gz = gx_m, gy_m, gz_m # alias for block below
583592
# # get the fault frame val/grad for the points
584593
# determine displacement magnitude, for constant displacement
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""apply_to_points of a fault whose frame is cut by another fault.
2+
3+
On a fault that cuts the frame of another fault, the frame has no single
4+
gradient, so evaluate_gradient gives NaN there (#421). The points must still
5+
move, with the direction of the frame without the cutting faults. A frame
6+
without cutting faults keeps the NaN (spec 005, test_fault_frame_validation).
7+
"""
8+
9+
import numpy as np
10+
11+
from LoopStructural import GeologicalModel
12+
from tests.fixtures.fault_models import OLD, YOUNG, add_fault, interior_points
13+
14+
15+
def _nan_on_every_other_row(member):
16+
original = member.evaluate_gradient
17+
18+
def gradient(points, *args, **kwargs):
19+
g = np.array(original(points, *args, **kwargs), dtype=float)
20+
g[::2] = np.nan
21+
return g
22+
23+
return gradient
24+
25+
26+
def _cut_pair():
27+
"""The old fault is cut by the young fault, so its frame members have a fault."""
28+
model = GeologicalModel(np.zeros(3), np.ones(3))
29+
young = add_fault(model, "young", **YOUNG, steps=3)
30+
old = add_fault(model, "old", **OLD, steps=3, faults=[young])
31+
model.update() # the frame members get the cutting fault when they are built
32+
assert old[1].faults
33+
return old
34+
35+
36+
def test_nan_gradient_of_a_cut_frame_still_moves_the_points(monkeypatch):
37+
old = _cut_pair()
38+
points = interior_points()
39+
expected = old.apply_to_points(points.copy())
40+
moved = ~np.isclose(expected, points).all(axis=1)
41+
assert moved.any(), "the fault must move some points"
42+
43+
monkeypatch.setattr(old[1], "evaluate_gradient", _nan_on_every_other_row(old[1]))
44+
restored = old.apply_to_points(points.copy())
45+
46+
assert not np.isnan(restored).any()
47+
# The fallback direction is close to the cut-frame direction, so the points move
48+
# about as far as before.
49+
np.testing.assert_allclose(
50+
np.linalg.norm(restored - points, axis=1),
51+
np.linalg.norm(expected - points, axis=1),
52+
rtol=0.05,
53+
atol=1e-9,
54+
)
55+
56+
57+
def test_point_with_a_nan_coordinate_is_not_moved(monkeypatch):
58+
old = _cut_pair()
59+
points = np.vstack((interior_points(n=20), [[np.nan, 0.5, 0.5]]))
60+
monkeypatch.setattr(old[1], "evaluate_gradient", _nan_on_every_other_row(old[1]))
61+
restored = old.apply_to_points(points.copy())
62+
np.testing.assert_array_equal(restored[-1], points[-1])
63+
assert not np.isnan(restored[:-1]).any()
64+
65+
66+
def test_frame_without_cutting_faults_keeps_a_nan_gradient(monkeypatch):
67+
"""Without cutting faults, a NaN frame gradient still gives NaN (no fallback)."""
68+
model = GeologicalModel(np.zeros(3), np.ones(3))
69+
fault = add_fault(model, "young", **YOUNG, steps=3)
70+
points = interior_points()
71+
moved = ~np.isclose(fault.apply_to_points(points.copy()), points).all(axis=1)
72+
monkeypatch.setattr(fault[1], "evaluate_gradient", _nan_on_every_other_row(fault[1]))
73+
restored = fault.apply_to_points(points.copy())
74+
assert np.isnan(restored[moved]).any()

0 commit comments

Comments
 (0)