Skip to content

Commit 362d3f1

Browse files
lachlangroseclaude
andcommitted
fix(builder): rotate data vectors through the faults in restore order
The builder rotated each data vector (normal, gradient, tangent) by every fault at its present-day position, with no restored_through. A fault frame displaced by younger faults then restored the vector's tetrahedron through its whole chain of cutting faults at each step, so building a fault deep in a chain cost about (chain length)^2 x steps fault applications: the deepest fault of a 5-deep chain made 1661 fault calls and took 9.6 s, and deeper faults of a 94-fault model took up to 920 s each. Each vector is now rotated by each fault at its position restored so far, youngest fault first (the order restore_through uses), and each fault is told which faults are already restored. The same build takes 1.0 s with 12-14 fault calls per fault. With one fault, or faults that do not move each other's data, the rotation is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx
1 parent 79f66bf commit 362d3f1

2 files changed

Lines changed: 93 additions & 13 deletions

File tree

‎LoopStructural/modelling/features/builders/_geological_feature_builder.py‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,28 @@ def add_data_to_interpolator(self, constrained=False, force_constrained=False, *
204204
mask_tangent = np.all(~np.isnan(data.loc[:, tangent_vec_names()].to_numpy(float)), axis=1)
205205
mask_vector = mask_gradient | mask_normal | mask_tangent
206206
if np.sum(mask_vector) > 0:
207-
# the same faults and order as restore_through below
207+
# Rotate each vector by each fault at its position restored so far, youngest
208+
# fault first, as restore_through below moves the points. Each fault is told
209+
# which faults are already restored, so a fault frame displaced by a younger
210+
# fault is read at the restored position and does not restore it again
211+
# (rotating at the present-day position made every fault restore through its
212+
# whole chain of cutting faults at each step).
213+
xyz = data.loc[:, xyz_names()].to_numpy(float)
214+
done = ()
208215
for f in cutters_first(with_cutters(self.faults)):
209-
if np.sum(mask_normal) > 0:
210-
data.loc[mask_normal, normal_vec_names()] = f.apply_to_vectors(
211-
data.loc[mask_normal, xyz_names() + normal_vec_names()].to_numpy(float)
212-
)
213-
if np.sum(mask_gradient) > 0:
214-
data.loc[mask_gradient, gradient_vec_names()] = f.apply_to_vectors(
215-
data.loc[mask_gradient, xyz_names() + gradient_vec_names()].to_numpy(float)
216-
)
217-
if np.sum(mask_tangent) > 0:
218-
data.loc[mask_tangent, tangent_vec_names()] = f.apply_to_vectors(
219-
data.loc[mask_tangent, xyz_names() + tangent_vec_names()].to_numpy(float)
220-
)
216+
kwargs = {"restored_through": done} if done else {}
217+
for mask, names in (
218+
(mask_normal, normal_vec_names()),
219+
(mask_gradient, gradient_vec_names()),
220+
(mask_tangent, tangent_vec_names()),
221+
):
222+
if np.sum(mask) > 0:
223+
data.loc[mask, names] = f.apply_to_vectors(
224+
np.hstack((xyz[mask], data.loc[mask, names].to_numpy(float))),
225+
**kwargs,
226+
)
227+
xyz = f.apply_to_points(xyz, **kwargs)
228+
done = done + (f,)
221229

222230
# Each fault once: a fault displaced by a younger fault of this list
223231
# evaluates its frame at the points already restored by that fault.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""The builder rotates data vectors through the faults in the order points are restored.
2+
3+
Each vector is rotated by each fault at its position restored so far, youngest fault
4+
first, and each fault is told which faults are already restored. Rotating at the
5+
present-day position made a fault frame displaced by younger faults restore the
6+
vector through its whole chain of cutting faults at each step.
7+
"""
8+
9+
import numpy as np
10+
import pandas as pd
11+
12+
from LoopStructural import GeologicalModel
13+
from LoopStructural.modelling.features.fault._fault_segment import FaultSegment
14+
from tests.fixtures.fault_models import COLUMNS, OLD, YOUNG, add_fault
15+
16+
17+
def _track_depth(monkeypatch):
18+
depth, deepest = [0], [0]
19+
original = FaultSegment.apply_to_points
20+
21+
def tracked(self, pts, *args, **kwargs):
22+
depth[0] += 1
23+
deepest[0] = max(deepest[0], depth[0])
24+
try:
25+
return original(self, pts, *args, **kwargs)
26+
finally:
27+
depth[0] -= 1
28+
29+
monkeypatch.setattr(FaultSegment, "apply_to_points", tracked)
30+
return deepest
31+
32+
33+
def test_building_a_frame_in_a_chain_of_cuts_does_not_nest(monkeypatch):
34+
model = GeologicalModel(np.zeros(3), np.ones(3))
35+
young = add_fault(model, "young", **YOUNG, steps=3)
36+
old = add_fault(model, "old", **OLD, steps=3, faults=[young])
37+
oldest = add_fault(model, "oldest", center=(0.5, 0.5, 0.7), dip=70, dip_azimuth=0,
38+
displacement=0.08, steps=3, faults=[old])
39+
young.builder.up_to_date()
40+
old.builder.up_to_date()
41+
deepest = _track_depth(monkeypatch)
42+
oldest.builder.up_to_date()
43+
# The data of oldest (with its normal vectors) go through young then old, and the
44+
# frame of old is read at points young already restored: no fault inside another.
45+
assert deepest[0] == 1
46+
47+
48+
def test_one_fault_rotates_vectors_at_their_present_position(monkeypatch):
49+
"""With one fault the vectors are rotated as before: at the data positions."""
50+
model = GeologicalModel(np.zeros(3), np.ones(3))
51+
fault = add_fault(model, "young", **YOUNG, steps=3)
52+
fault.builder.up_to_date()
53+
rng = np.random.default_rng(4)
54+
xyz = rng.uniform(0.1, 0.9, (40, 3))
55+
data = pd.DataFrame({"X": xyz[:, 0], "Y": xyz[:, 1], "Z": xyz[:, 2], "val": np.nan,
56+
"nx": 0.0, "ny": 0.0, "nz": 1.0, "coord": np.nan,
57+
"feature_name": "strat"})[COLUMNS]
58+
strat = model.create_and_add_foliation("strat", data=data, nelements=2000, buffer=0.3,
59+
faults=[fault])
60+
seen = []
61+
original = FaultSegment.apply_to_vectors
62+
63+
def recorded(self, vector, *args, **kwargs):
64+
seen.append((self.name, np.array(vector[:, :3]), kwargs.get("restored_through", ())))
65+
return original(self, vector, *args, **kwargs)
66+
67+
monkeypatch.setattr(FaultSegment, "apply_to_vectors", recorded)
68+
strat.builder.up_to_date()
69+
assert [name for name, _, _ in seen] == ["young"]
70+
_, positions, restored = seen[0]
71+
np.testing.assert_array_equal(positions, xyz)
72+
assert tuple(restored) == ()

0 commit comments

Comments
 (0)