Skip to content

Commit 593953b

Browse files
lachlangroseclaude
andauthored
Draft: fault block fields and kinematics fixes (#409)
* feat(regions): pass a fault block to region checks Add the private module utils/_fault_block with the helpers that check a fault block (fault name to +1, -1 or 0) and put a fault frame value on one side of the fault. The sign regions evaluate their feature in the block, and a block that fixes the side of the region's fault also fixes the side outside the support of that fault. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * feat(features): evaluate a geological feature in one fault block GeologicalFeature.evaluate_value and evaluate_gradient take an optional keyword block. The block goes to the regions and to the faults of the feature, so each fault it names restores the points as if they were on the given side. The gradient does not refine its tetrahedra across a fault that the block names, because the block field has no jump there. Without a block the calls are the same as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * feat(fault): restore points in a fault block, frame and tip functions FaultSegment.apply_to_points, evaluate_value and evaluate_displacement take an optional keyword block. If the block names the fault, the frame coordinate gx is put on that side (side * max(|gx|, 1e-9)) wherever the fault is evaluated, so the displacement is that of the given wall, extended smoothly over the whole support; 0 leaves the points in place. The frame is evaluated in the same block, so a fault that younger faults displace follows the block of those faults. New provisional methods: - evaluate_frame_value / evaluate_frame_gradient: one frame coordinate without the fault regions, optionally in a block of the younger faults. - evaluate_tip_value / evaluate_tip_gradient: min(1 - |g|) over the frame coordinates whose displacement profile is zero at |g| = 1; inf for a fault without a tip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * feat(model): add evaluate_feature_block_value and _gradient Provisional GeologicalModel methods that evaluate a feature, by name, in one fault block. The block must name faults of the model. The regions of the feature itself are ignored by default, so the result is the smooth extension of the block over the whole support. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * test(fault): block fields, fault frames and tip functions Planar constant-slip fault (analytic offset between the blocks, equality inside each block, smooth gradient, no state, threads), finite fault (equal fields beyond the tip, displacement to zero at the tip, tip gradient), an abutting fault (absent in the footwall block of its host) and a displaced fault (block frames match the undisplaced fault). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * docs(api): list the fault block methods as provisional Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * test: move the small faulted models to tests/fixtures/fault_models.py The block-field tests and the kinematics tests share them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * fix(fault): apply_to_points honours reverse=True (LS-E2) The sign of reverse was applied to the displacement before the integration loop, and the loop recomputed the displacement without it, so reverse=True restored the points instead of moving them forward. The loop now applies the sign at every step. No caller inside LoopStructural passes reverse=True, so no model built by LoopStructural changes; external forward modelling (for example the fault benchmark's forward()) now moves points forward. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * fix(fault): restore a point through a displaced fault once (LS-E1) A feature keeps its faults youngest first. When a younger fault displaces an older fault, the frame of the older fault is built in the space restored by the younger fault, and its frame features list the younger fault among their own faults. Evaluating the feature restored the point through the younger fault, then the older fault evaluated its frame at that point and restored it through the younger fault a second time. The builder restored the data the same way, so the model was consistent with itself but its kinematics were wrong: the side and the slip of the older fault were read at a point moved twice by the younger fault. The faults of a list are now applied by one helper, restore_through, which tells each fault which faults the points have already been restored through; the fault evaluates its frame (value and slip gradient) without those faults. The new keyword restored_through on FaultSegment.apply_to_points / apply_to_vectors and on GeologicalFeature.evaluate_value / evaluate_gradient carries it; no state changes. Without the keyword every call is as before. THIS CHANGES THE RESULTS OF EXISTING MODELS: every model where a fault displaces a feature and is itself displaced by a younger fault that also displaces that feature (the series data and the evaluation both change). Models without displaced faults do not change. LambdaGeologicalFeature and AnalyticalFeature keep their own fault loops and are not changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * fix(fault): restore_through keeps the plain apply_to_points(points) call A fault-like object with only apply_to_points(points) (as in test_support_expansion_contains_restored_points) failed because the helper always passed reverse=. It now passes reverse, block and restored_through only when they are needed, as the old loops did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * feat(model): add get_feature_relations (LS-D1) Provisional GeologicalModel.get_feature_relations() returns the relations of a model as a plain dictionary: the event order (features in the order they were added), for each fault the faults that displace it, the faults it abuts with the side and the unconformities that truncate it, for each other feature the faults that displace it (restore order) and its truncations, and for each unconformity its series, value and what it truncates. It reads the builders, so it is the same before and after update(); it changes nothing. Logic in FeatureRelationshipManager.relations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * feat(fault): declare the tip of a displacement profile (LS-C2) FaultDisplacement.tip_ends gives, for gy (1) and gz (2), the ends where the profile is exactly zero, from a new tip_ends attribute of the profiles (FaultProfileFunction subclasses and smooth_peak). A profile that does not declare it is still tested numerically. FaultSegment.evaluate_tip_value reads it instead of testing profiles. CubicFunction now clamps to the value of a value constraint that sits exactly at its limit. The least squares fit left about 1.5e-14 where the constraint asks for 0, so the BaseFault / BaseFault3D displacement was 1.5e-14 * scale * displacement, not 0, beyond |gx| = 1. THIS CHANGES THE RESULTS OF EXISTING MODELS WITH BaseFault OR BaseFault3D PROFILES, by at most about 1e-14 of the displacement, at |gx| >= 1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * fix(loop_interpolation): CG uses the library rtol when a tol is given (LS-F1) scipy's cg stops at max(rtol*|b|, atol). solve_with_cg and solve_with_cg_normal_equations set atol=tol and left rtol unset, so scipy's default rtol=1e-5 decided the stop and the tol of a LoopStructural model (1e-10 of the box) had no effect; the library's own DEFAULT_CG_RTOL (1e-8) was used only without a tol. Both now go through _set_cg_tolerances: atol=tol if no atol is given, rtol=DEFAULT_CG_RTOL if no rtol is given. A caller's own atol is no longer replaced by tol. THIS CHANGES THE RESULTS OF EXISTING MODELS solved with cg: the solve runs to rtol 1e-8. Measured on the fault benchmark cases: the field moves by up to 1.4e-3 (planar_fault, in a data-free corner) and 3.2e-4 (fault_tip), the build takes 15 % to 70 % longer, and the values no longer depend on where scipy stopped (they agree with rtol 1e-12 to the printed digits). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * perf(loop_common): find cell indexes in one vectorised step (LS-G2) position_to_cell_index read origin, step_vector and nsteps_cells (each a computed property) once per axis and floored each axis separately. It is called several times per interpolator evaluation, so for small batches its fixed cost dominated. It now reads the geometry once and works on all axes at once; inside() keeps its overridable behaviour. Same results (loop_common tests unchanged, 421 passed). Measured on a 1-point block evaluation of the planar_fault benchmark: 0.70 ms to 0.55 ms per call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * docs(api): list get_feature_relations, tip_ends and restored_through Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * feat(loop_interpolation): add a cg stop in length units of the model (LS-F1) position_tolerance says how far the isosurfaces of a cg-solved field may be from the isosurfaces of the fully converged field, as a length in the units of the model (the support is moved and rotated but not scaled, so 1.0 is 1 m in a model in metres). It is a keyword of DiscreteInterpolator.solve_system, an attribute of GeologicalModel (default None) and a keyword of the create_and_add_* methods. None keeps the present stop on the residual; tol is not removed. The stop is an estimate, not a guarantee. cg_to_position_tolerance runs the iteration of scipy's cg (the same operations, so the same iterates) and, every 10 iterations once the residual has dropped by 1e-6, takes the size of the error from |r| / theta (theta: the smallest Ritz value of the Lanczos matrix of the cg steps, which bounds the 2-norm of the error once it has converged) and its distribution over the unknowns from the change of the solution over the last 50 iterations (the delay estimate of Strakos and Tichy). The interpolator turns that into a length with the median gradient norm at the support nodes. Measured on five models (faulted 1 unit and 10 km, Claudius FDI and PLI, Laurent2016), for tolerances of 1e-3 to 1e-5 of the box: the largest position error at a node is 6e-5 to 0.19 times the tolerance. A fixed rtol gives position errors that differ by 1e5 between solves, and a stop on the change alone is up to 1000 times over the tolerance on systems where cg stagnates. At 1e-4 of the box the build takes 1.04 to 2.6 times the build with scipy's rtol 1e-5. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * fix(model): an unconformity truncates the faults that are older than it (LS-D2) add_unconformity skipped faults: a fault that was already in the model kept its frame and its surface above the unconformity and moved points there. Only a fault created after the call took the region. Now every fault that is older than the unconformity takes its region, so the frame and the surface exist only below the unconformity and the fault moves no point above it. The features that the fault displaces are marked to be built again. Which faults are older: a new feature is displaced by every fault that the model has at that moment, unless its faults argument gives another list. A fault is younger if it displaces the series of the unconformity, or a series above an earlier unconformity; it is not changed. Every other fault is older: a fault added after the series (before or after the call), and a fault added before it that the faults argument of the series leaves out. A fault created after several unconformities takes the region of each of them, not only of the last one. The unconformity region of a fault that a younger fault displaces is read at the point restored once through the younger fault (keyword restored_through on UnconformityFeature.evaluate and __call__); it was read at the point restored twice. Onlap unconformities are not changed: no fault takes their region. THIS CHANGES THE RESULTS OF EXISTING MODELS (listed in COMPAT.md): where a fault is left out of the faults list of the series of an unconformity or is added between that series and the call; where a fault follows two or more unconformities; where a truncated fault is displaced by a younger fault. Models without an unconformity, faults that displace the series of the unconformity and onlap unconformities give bit-equal results. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * docs(compat): list the double restoration fix for the release notes (LS-E1) Owner decision D3: the fix of the double restoration (54371120, bb7ef627) goes in with no flag and is listed as a bug fix. CHANGELOG.md is written by release-please from the commit subjects. The two commits have the type fix, so the release gets a "Bug Fixes" entry "fault: restore a point through a displaced fault once (LS-E1)". The changelog does not take the commit body, so the text of the release notes is the new row in the behaviour table of COMPAT.md: what was wrong, which models change (a fault displaces a feature and is itself displaced by a younger fault that also displaces that feature), and that models without displaced faults do not change. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * feat(fault): continue the displacement of a block smoothly across its fault (LS-A2) Owner decision D1: the extension of a block across its fault is not the mirror image. A block field is evaluated on both sides of the fault; on the other side the displacement was the profile at side * |gx|. That is an even extension: it has a kink where the profile has a slope at the fault, so the faulted field had a sharp change of gradient there. The displacement on the other side is now the smooth continuation of the profile of the block's side across gx = 0 (_fault_function.block_displacement): * Ones, Zeros: the constant. * CubicFunction (BaseFault, BaseFault3D, user cubics): its own cubic at the signed coordinate, without its limits. Smooth to all orders. * any other gx profile, and a fault function that is not a FaultDisplacement: the second-order expansion at the fault, with one-sided finite differences of fourth order (step 1e-3 in gx). C2. Up to 0.25 in gx from the fault the continuation is exact; it is blended (C2) to a constant at 0.5, so it cannot grow without limit. The gy and gz profiles are not changed. On the block's own side every value is bit equal. evaluate_feature_block_gradient uses the same displacement. The side of a point for regions and abutting faults still uses side * |gx| (force_side). The mirror image stays as _fault_block.BLOCK_EXTENSION = "mirror", a module setting to compare with data made with it (the fault benchmark study). Constant-slip blocks do not change. For the library cubics the block field on the other side changes by 4 |gx|^3 times the displacement scale: measured up to 2.3e-4 within one cell of 1/48 and 4.4e-2 far from the fault (BaseFault3D, displacement 0.2, minor axis 0.5). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh * docs(api): list position_tolerance, the block continuation and the truncation of older faults Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XADRvLtMSHY4aXcrL452oh --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9fe00f5 commit 593953b

26 files changed

Lines changed: 3172 additions & 151 deletions

‎API.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,48 @@ exposed:
195195
- `getLogger` itself is promoted to enforced-**stable** (see below) as
196196
part of this work — its signature/behavior are unchanged, only
197197
tracked by the registry now.
198+
- Fault blocks: evaluation of a feature as it is in one fault block. A block
199+
maps fault names to `+1` (hanging wall), `-1` (footwall) or `0` (absent);
200+
each fault it names restores every point as if the point were on that side,
201+
so the result has no jump across those faults. The block is an argument
202+
only: no state changes, so calls with different blocks can run in parallel.
203+
On the other side of its fault a block takes the smooth continuation of the
204+
displacement profile of its side (C2 at the fault; exact up to 0.25 in gx,
205+
constant beyond 0.5), not the mirror image; the module setting
206+
`LoopStructural.utils._fault_block.BLOCK_EXTENSION = "mirror"` gives the
207+
mirror image for comparisons. See
208+
`tests/unit/modelling/test_fault_block_extension.py`.
209+
- `GeologicalModel.evaluate_feature_block_value(feature_name, xyz, block, *, ignore_regions=True)` /
210+
`GeologicalModel.evaluate_feature_block_gradient(...)`.
211+
- `FaultSegment.evaluate_frame_value(points, coordinate=0, *, block=None)` /
212+
`FaultSegment.evaluate_frame_gradient(...)` — one fault frame coordinate
213+
without the fault regions, optionally in a block of the younger faults.
214+
- `FaultSegment.evaluate_tip_value(points, *, block=None)` /
215+
`FaultSegment.evaluate_tip_gradient(...)` — `min(1 - |g|)` over the
216+
frame coordinates whose displacement profile is zero at `|g| = 1`.
217+
- The keyword `block` on `GeologicalFeature.evaluate_value` /
218+
`evaluate_gradient` and on `FaultSegment.apply_to_points` /
219+
`evaluate_value` / `evaluate_displacement` (both unregistered; the
220+
keyword is new and optional, so existing calls are unchanged).
221+
- See `tests/unit/modelling/test_fault_block_fields.py`.
222+
- `GeologicalModel.get_feature_relations()` — the relations of a model as a
223+
plain dict (event order, displaced-by, abutment with side, truncation by
224+
unconformities); reads the builders, same before and after `update()`.
225+
- `FaultDisplacement.tip_ends` (property) and the `tip_ends` attribute of
226+
the profiles — which frame coordinates carry a tip (unregistered).
227+
- The keyword `restored_through` on `FaultSegment.apply_to_points` /
228+
`apply_to_vectors` and `GeologicalFeature.evaluate_value` /
229+
`evaluate_gradient` — faults the points have already been restored
230+
through (a displaced fault is applied once; unregistered).
231+
- `GeologicalModel.add_unconformity` truncates the faults that are older
232+
than the unconformity (signature unchanged; the rule for "older" is in its
233+
doc string and the changed results are in `COMPAT.md`). The keyword
234+
`restored_through` is also on `UnconformityFeature.evaluate` / `__call__`.
235+
- `GeologicalModel.position_tolerance` (attribute, default None), the keyword
236+
`position_tolerance` of the `create_and_add_*` methods and of
237+
`DiscreteInterpolator.solve_system` — a cg stop on an estimate of the
238+
position error of the isosurfaces, in model length units (an estimate, not
239+
a guarantee; unregistered).
198240

199241
## Known internal-path consumers
200242

‎COMPAT.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ unchanged, but the new path is preferred going forward.
7070
| Known issue, not fixed in S6: the gradient of a model outside the support | Outside the support the FDI gradient is 0 and the PLI gradient is NaN. S6 test T095 pins this behavior (spec 005, stage S6) | 2026-11-06 |
7171
| Known issue, not fixed in S6: `P2Interpolator.add_gradient_constraints` for a P2 foliation with gradient rows | It raises `ValueError`. This is a library defect. S6 does not change it (spec 005, stage S6) | 2026-11-06 |
7272
| Known issue, not fixed in S6: `set_nelements` | It passes a cell count to the node setter. For example, `nelements=1000` gives 729 cells. This is old behavior, not a regression. S6 does not change it (spec 005, stage S6, S5 finding 3) | 2026-11-06 |
73+
| **Changes model results.** `add_unconformity` did not truncate a fault that was already in the model: the fault kept its frame and its surface above the unconformity and moved points there. Only a fault created after the call took the region of the unconformity | `add_unconformity` truncates every fault that is older than the unconformity: the fault takes the region of the unconformity, so its frame and surface exist only below it and it moves no point above it. A fault is younger (not changed) if it displaces the series of the unconformity, or a series above an earlier unconformity. Models change where a fault is added before the call and is left out of the `faults` list of the series of the unconformity, or is added between that series and the call. Models without an unconformity, models whose faults all displace the series of the unconformity, and onlap unconformities do not change (kiridashi requirement LS-D2) | 2026-11-16 |
74+
| **Changes model results.** A fault created after two or more unconformities took the region of the last one only | It takes the region of each erosional unconformity that is before it in the model (LS-D2) | 2026-11-16 |
75+
| **Changes model results.** The unconformity region of a fault that a younger fault displaces was read at a point restored twice through the younger fault (the same error as the double restoration of a displaced fault) | The region is read at the point restored once, so the older fault ends on the unconformity in the space in which its frame is built. Models change where a fault is truncated by an unconformity, is displaced by a younger fault, and both faults displace a series. `UnconformityFeature.evaluate` and `__call__` have a new keyword `restored_through` (LS-D2) | 2026-11-16 |
76+
| **Bug fix that changes model results.** A feature that a fault displaces was restored twice through a younger fault when the younger fault also displaces that fault: the frame of the older fault is built in the space restored by the younger fault, and its evaluation restored the point through the younger fault a second time. The side and the slip of the older fault were read at the wrong point (all versions up to 1.11.0) | Each point is restored once through each fault. There is no flag for the old behaviour. **Models change** where a fault displaces a feature and is itself displaced by a younger fault that also displaces that feature: the restored data and the evaluated field of that feature both change (in the test model by up to 0.059, for an offset of the older fault of 0.082). **Models without displaced faults do not change.** New keyword `restored_through` on `FaultSegment.apply_to_points` / `apply_to_vectors` and `GeologicalFeature.evaluate_value` / `evaluate_gradient`. `LambdaGeologicalFeature` and `AnalyticalGeologicalFeature` keep their own fault loops and are not changed (kiridashi requirement LS-E1) | 2026-11-16 |
7377

7478
The following stable methods remain part of the documented public API surface after the package extraction work and are covered by the snapshot-based contract test: add_onlap_unconformity, add_unconformity, create_and_add_domain_fault, create_and_add_fault, create_and_add_fold_frame, create_and_add_folded_fold_frame, create_and_add_folded_foliation, create_and_add_foliation, evaluate_model, evaluate_model_gradient, from_file, get_fault_surfaces, get_feature_by_name, get_stratigraphic_surfaces, rescale, save, scale.
7579

‎LoopStructural/modelling/core/_model_feature_factory.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from ...modelling.features.fold import FoldEvent, FoldFrame
3232
from ...modelling.intrusions import IntrusionBuilder, IntrusionFeature, IntrusionFrameBuilder
3333
from ...utils import getLogger
34+
from ._model_relationships import FeatureRelationshipManager
3435

3536
if TYPE_CHECKING:
3637
from numpy.typing import ArrayLike
@@ -775,10 +776,7 @@ def build_fault(
775776
fault.displacement = displacement_scaled
776777
fault.faultfunction = faultfunction
777778

778-
for f in reversed(model.features):
779-
if f.type == FeatureType.UNCONFORMITY:
780-
fault.add_region(f)
781-
break
779+
FeatureRelationshipManager.add_unconformities_above_fault(model, fault, index=index)
782780
if displacement == 0:
783781
fault.type = FeatureType.INACTIVEFAULT
784782
model._add_feature(fault, index=index)

‎LoopStructural/modelling/core/_model_relationships.py‎

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,135 @@ def add_unconformity(
174174
continue
175175
else:
176176
f.add_region(uc_feature)
177+
# the faults that are older than the unconformity end on it
178+
for f in list(model.features):
179+
if f.type not in (FeatureType.FAULT, FeatureType.INACTIVEFAULT):
180+
continue
181+
if FeatureRelationshipManager.fault_is_older_than(model, f, feature):
182+
FeatureRelationshipManager.truncate_fault(model, f, uc_feature)
177183
# now add the unconformity to the feature list
178184
model._add_feature(uc_feature, index=index)
179185
return uc_feature
180186

187+
@staticmethod
188+
def _displaced_by(feature: GeologicalFeature) -> list:
189+
"""The faults that displace a feature (read from its builder)."""
190+
builder = getattr(feature, "builder", None)
191+
builders = getattr(builder, "builders", None)
192+
if builders is not None:
193+
# a structural frame: the three coordinates have the same faults
194+
builder = builders[0]
195+
faults = getattr(builder, "faults", None)
196+
if faults is None:
197+
faults = getattr(feature, "faults", [])
198+
return list(faults)
199+
200+
@staticmethod
201+
def fault_is_older_than(
202+
model: GeologicalModel, fault: GeologicalFeature, feature: GeologicalFeature
203+
) -> bool:
204+
"""Whether a fault is older than the unconformity at the base of a feature.
205+
206+
Call it before the unconformity is added to the model.
207+
208+
The features of a model are in the order in which they were added,
209+
and a new feature is displaced by every fault that the model has at
210+
that moment, unless its ``faults`` argument gives another list. An
211+
unconformity truncates the features that the model has back to the
212+
last unconformity or domain fault; the features before that one lie
213+
above an earlier unconformity and are younger. So the rule is:
214+
215+
A fault is **younger** than the unconformity if it displaces
216+
``feature`` (the series whose isovalue is the unconformity), or a
217+
feature that lies above an earlier unconformity. Every other fault
218+
is **older**: a fault added after ``feature``, and a fault added
219+
before it that the ``faults`` argument of ``feature`` leaves out.
220+
221+
Parameters
222+
----------
223+
model : GeologicalModel
224+
fault : FaultSegment
225+
feature : GeologicalFeature
226+
the series that defines the unconformity
227+
228+
Returns
229+
-------
230+
bool
231+
"""
232+
displaced_by = FeatureRelationshipManager._displaced_by
233+
if any(d is fault for d in displaced_by(feature)):
234+
return False
235+
# the features that this unconformity does not truncate: all that
236+
# are before the last unconformity or domain fault
237+
boundary = None
238+
for i in reversed(range(len(model.features))):
239+
if model.features[i].type in (FeatureType.UNCONFORMITY, FeatureType.DOMAINFAULT):
240+
boundary = i
241+
break
242+
if boundary is None:
243+
return True
244+
skip = (
245+
FeatureType.FAULT,
246+
FeatureType.INACTIVEFAULT,
247+
FeatureType.UNCONFORMITY,
248+
FeatureType.ONLAPUNCONFORMITY,
249+
)
250+
for f in model.features[:boundary]:
251+
if f.type not in skip and any(d is fault for d in displaced_by(f)):
252+
return False
253+
return True
254+
255+
@staticmethod
256+
def truncate_fault(
257+
model: GeologicalModel, fault: GeologicalFeature, unconformity: UnconformityFeature
258+
) -> bool:
259+
"""Limit a fault to the region below an unconformity.
260+
261+
The fault frame gets the region of the unconformity: the frame, the
262+
fault surface and the displacement exist only where the series of
263+
the unconformity is below its value. A point above the unconformity
264+
is not moved by the fault, so the fault does not displace the
265+
features above it. The features that the fault displaces are marked
266+
to be built again.
267+
268+
Returns
269+
-------
270+
bool
271+
False if the fault already has this region.
272+
"""
273+
for r in fault.regions:
274+
if (
275+
isinstance(r, UnconformityFeature)
276+
and r.parent is unconformity.parent
277+
and r.value == unconformity.value
278+
and r.sign == unconformity.sign
279+
):
280+
return False
281+
logger.info(f"Adding {unconformity.name} as unconformity to fault {fault.name}")
282+
fault.add_region(unconformity)
283+
for f in model.features:
284+
if any(d is fault for d in FeatureRelationshipManager._displaced_by(f)):
285+
builder = getattr(f, "builder", None)
286+
if builder is not None:
287+
builder.set_not_up_to_date(fault)
288+
return True
289+
290+
@staticmethod
291+
def add_unconformities_above_fault(
292+
model: GeologicalModel, fault: GeologicalFeature, index: int | None = None
293+
) -> None:
294+
"""Truncate a new fault by the unconformities that are younger than it.
295+
296+
A fault that is added after an unconformity is older than it. The
297+
fault takes the region of every erosional unconformity that is
298+
before its place in the model (``index``, or the end of the list).
299+
Onlap unconformities are not used.
300+
"""
301+
features = model.features if index is None else model.features[:index]
302+
for f in reversed(features):
303+
if f.type == FeatureType.UNCONFORMITY:
304+
FeatureRelationshipManager.truncate_fault(model, fault, f)
305+
181306
@staticmethod
182307
def add_onlap_unconformity(
183308
model: GeologicalModel, feature: GeologicalFeature, value: float, index: int | None = None
@@ -216,3 +341,108 @@ def add_onlap_unconformity(
216341
model._add_feature(uc_feature.inverse(), index=index)
217342

218343
return uc_feature
344+
345+
@staticmethod
346+
def relations(model: GeologicalModel) -> dict:
347+
"""Read the relations between the features of a model.
348+
349+
The relations are read from the builders, which hold them from the
350+
moment a feature is created, so the result does not depend on whether
351+
the model has been built. Nothing is changed.
352+
353+
Parameters
354+
----------
355+
model : GeologicalModel
356+
357+
Returns
358+
-------
359+
dict
360+
``events``: the features in the order they were added
361+
(``{"name", "type"}``, ``type`` a :class:`FeatureType` name). A
362+
feature added later is older; an unconformity entry follows the
363+
series that defines it.
364+
365+
``faults``: for each fault, ``displaced_by`` (the faults that
366+
displace its frame, youngest first), ``abuts`` (``{"fault", "side"}``:
367+
side +1 if the fault exists where the host gx > 0, -1 where it is
368+
< 0), ``truncated_by`` (see below) and ``displacement``.
369+
370+
``features``: for each other feature that is not an unconformity,
371+
``type``, ``faults`` (the faults that displace it, in restore
372+
order: youngest first) and ``truncated_by``.
373+
374+
``unconformities``: for each unconformity, ``feature`` (the series
375+
whose isovalue it is), ``value``, ``onlap`` and ``truncates`` (the
376+
features and faults whose ``truncated_by`` names it).
377+
378+
A ``truncated_by`` entry is ``{"unconformity", "feature", "value",
379+
"keep"}``: the feature exists only where ``feature`` is ``<= value``
380+
(``keep == "below"``) or ``>= value`` (``"above"``).
381+
"""
382+
from ...modelling.features.fault import FaultSegment
383+
from ...utils.regions import NegativeRegion, PositiveRegion
384+
385+
def faults_of(feature):
386+
builder = getattr(feature, "builder", None)
387+
faults = getattr(builder, "faults", None)
388+
if faults is None:
389+
faults = feature.faults
390+
return [f.name for f in faults]
391+
392+
def truncations(regions):
393+
out = []
394+
for r in regions:
395+
if isinstance(r, UnconformityFeature):
396+
out.append(
397+
{
398+
"unconformity": r.name,
399+
"feature": r.parent.name,
400+
"value": float(r.value),
401+
"keep": "below" if r.sign else "above",
402+
}
403+
)
404+
return out
405+
406+
events, faults, features, unconformities = [], {}, {}, {}
407+
for feature in model.features:
408+
events.append({"name": feature.name, "type": FeatureType(feature.type).name})
409+
if isinstance(feature, FaultSegment):
410+
abuts = []
411+
for host, region in feature.abut.items():
412+
if isinstance(region, PositiveRegion):
413+
side = 1
414+
elif isinstance(region, NegativeRegion):
415+
side = -1
416+
else:
417+
side = 0
418+
abuts.append({"fault": host, "side": side})
419+
faults[feature.name] = {
420+
"displaced_by": faults_of(feature[0]),
421+
"abuts": abuts,
422+
"truncated_by": truncations(feature[0].regions),
423+
"displacement": float(feature.displacement),
424+
}
425+
elif isinstance(feature, UnconformityFeature):
426+
unconformities[feature.name] = {
427+
"feature": feature.parent.name,
428+
"value": float(feature.value),
429+
"onlap": feature.type == FeatureType.ONLAPUNCONFORMITY,
430+
"truncates": [],
431+
}
432+
else:
433+
features[feature.name] = {
434+
"type": FeatureType(feature.type).name,
435+
"faults": faults_of(feature),
436+
"truncated_by": truncations(feature.regions),
437+
}
438+
for name, entry in list(faults.items()) + list(features.items()):
439+
for t in entry["truncated_by"]:
440+
for u in unconformities.values():
441+
if u["feature"] == t["feature"] and u["value"] == t["value"]:
442+
u["truncates"].append(name)
443+
return {
444+
"events": events,
445+
"faults": faults,
446+
"features": features,
447+
"unconformities": unconformities,
448+
}

0 commit comments

Comments
 (0)