Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions LoopStructural/modelling/features/fault/_fault_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@ def apply_to_points(
newp[mask, :], block, restored_through=restored_through
)
g = self.__getitem__(1).evaluate_gradient(newp[mask, :], ignore_regions=True, **kwargs)
# A frame cut by younger faults has no single gradient on a cutting fault, so the
# gradient is NaN there (#421). A step only needs a direction: use the gradient
# of the frame without the cutting faults at that point. A frame without cutting
# faults keeps a NaN gradient, so its points stay NaN (spec 005).
missing = np.isnan(g).any(axis=1) & ~np.isnan(newp[mask, :]).any(axis=1)
if np.any(missing) and getattr(self.__getitem__(1), "faults", None):
g[missing] = self.__getitem__(1).interpolator.evaluate_gradient(
newp[mask, :][missing]
)
gx, gy, gz = gx_m, gy_m, gz_m # alias for block below
# # get the fault frame val/grad for the points
# determine displacement magnitude, for constant displacement
Expand Down
74 changes: 74 additions & 0 deletions tests/unit/modelling/test_cut_fault_step_direction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""apply_to_points of a fault whose frame is cut by another fault.

On a fault that cuts the frame of another fault, the frame has no single
gradient, so evaluate_gradient gives NaN there (#421). The points must still
move, with the direction of the frame without the cutting faults. A frame
without cutting faults keeps the NaN (spec 005, test_fault_frame_validation).
"""

import numpy as np

from LoopStructural import GeologicalModel
from tests.fixtures.fault_models import OLD, YOUNG, add_fault, interior_points


def _nan_on_every_other_row(member):
original = member.evaluate_gradient

def gradient(points, *args, **kwargs):
g = np.array(original(points, *args, **kwargs), dtype=float)
g[::2] = np.nan
return g

return gradient


def _cut_pair():
"""The old fault is cut by the young fault, so its frame members have a fault."""
model = GeologicalModel(np.zeros(3), np.ones(3))
young = add_fault(model, "young", **YOUNG, steps=3)
old = add_fault(model, "old", **OLD, steps=3, faults=[young])
model.update() # the frame members get the cutting fault when they are built
assert old[1].faults
return old


def test_nan_gradient_of_a_cut_frame_still_moves_the_points(monkeypatch):
old = _cut_pair()
points = interior_points()
expected = old.apply_to_points(points.copy())
moved = ~np.isclose(expected, points).all(axis=1)
assert moved.any(), "the fault must move some points"

monkeypatch.setattr(old[1], "evaluate_gradient", _nan_on_every_other_row(old[1]))
restored = old.apply_to_points(points.copy())

assert not np.isnan(restored).any()
# The fallback direction is close to the cut-frame direction, so the points move
# about as far as before.
np.testing.assert_allclose(
np.linalg.norm(restored - points, axis=1),
np.linalg.norm(expected - points, axis=1),
rtol=0.05,
atol=1e-9,
)


def test_point_with_a_nan_coordinate_is_not_moved(monkeypatch):
old = _cut_pair()
points = np.vstack((interior_points(n=20), [[np.nan, 0.5, 0.5]]))
monkeypatch.setattr(old[1], "evaluate_gradient", _nan_on_every_other_row(old[1]))
restored = old.apply_to_points(points.copy())
np.testing.assert_array_equal(restored[-1], points[-1])
assert not np.isnan(restored[:-1]).any()


def test_frame_without_cutting_faults_keeps_a_nan_gradient(monkeypatch):
"""Without cutting faults, a NaN frame gradient still gives NaN (no fallback)."""
model = GeologicalModel(np.zeros(3), np.ones(3))
fault = add_fault(model, "young", **YOUNG, steps=3)
points = interior_points()
moved = ~np.isclose(fault.apply_to_points(points.copy()), points).all(axis=1)
monkeypatch.setattr(fault[1], "evaluate_gradient", _nan_on_every_other_row(fault[1]))
restored = fault.apply_to_points(points.copy())
assert np.isnan(restored[moved]).any()
Loading