diff --git a/LoopStructural/modelling/features/builders/_base_builder.py b/LoopStructural/modelling/features/builders/_base_builder.py index bf52a7d3..a5957e5f 100644 --- a/LoopStructural/modelling/features/builders/_base_builder.py +++ b/LoopStructural/modelling/features/builders/_base_builder.py @@ -1,7 +1,16 @@ +from contextvars import ContextVar + from LoopStructural.utils import getLogger logger = getLogger(__name__) +# The builders already checked by the outermost up_to_date call. A feature depends +# on the builders of its faults, and a fault frame on the builders of the faults +# that cut it; without this set every path through that graph was checked again, +# so the check grew with the number of paths (millions in a network of cutting +# faults) and ran on every evaluation. +_CHECKED: ContextVar = ContextVar("loopstructural_builders_checked", default=None) + class BaseBuilder: def __init__(self, model, name: str = "Feature"): @@ -89,6 +98,17 @@ def up_to_date(self, callback=None): a function that is called when the feature is updated """ + checked = _CHECKED.get() + if checked is None: + token = _CHECKED.set(set()) + try: + return self.up_to_date(callback=callback) + finally: + _CHECKED.reset(token) + if id(self) in checked: + # this call already brought this builder up to date + return + checked.add(id(self)) logger.info(f'Feature: {self.name} up to date: {self._up_to_date}') for f in self.faults: f.builder.up_to_date(callback=callback) diff --git a/tests/unit/modelling/test_builder_up_to_date_once.py b/tests/unit/modelling/test_builder_up_to_date_once.py new file mode 100644 index 00000000..e05c6809 --- /dev/null +++ b/tests/unit/modelling/test_builder_up_to_date_once.py @@ -0,0 +1,55 @@ +"""One up_to_date check looks at each builder once. + +A feature depends on the builders of its faults, and a fault frame on the builders +of the faults that cut it. The check walked every path through that graph, so in a +network of cutting faults it grew with the number of paths and ran on every +evaluation (36 million calls to build one fault of a 94-fault model). +""" + +from collections import Counter + +import numpy as np +import pandas as pd + +from LoopStructural import GeologicalModel +from LoopStructural.interpolators import DiscreteInterpolator +from tests.fixtures.fault_models import COLUMNS, OLD, YOUNG, add_fault + + +def _model(): + 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]) + add_fault(model, "oldest", center=(0.5, 0.5, 0.7), dip=70, dip_azimuth=0, + displacement=0.08, steps=3, faults=[old, young]) + xyz = np.random.default_rng(1).uniform(0, 1, (100, 3)) + data = pd.DataFrame({"X": xyz[:, 0], "Y": xyz[:, 1], "Z": xyz[:, 2], "val": xyz[:, 2], + "nx": np.nan, "ny": np.nan, "nz": np.nan, "coord": np.nan, + "feature_name": "strat"})[COLUMNS] + strat = model.create_and_add_foliation("strat", data=data, nelements=2000, buffer=0.3) + model.update() + return strat + + +def test_each_interpolator_is_checked_once(monkeypatch): + strat = _model() + reads = Counter() + flag = DiscreteInterpolator.up_to_date + + def counted(self): + reads[id(self)] += 1 + return flag.fget(self) + + monkeypatch.setattr(DiscreteInterpolator, "up_to_date", property(counted, flag.fset)) + strat.builder.up_to_date() + # strat and the 3 coordinates of each of the 3 fault frames + assert len(reads) == 10 + assert max(reads.values()) == 1 + + +def test_a_stale_fault_is_still_rebuilt(): + strat = _model() + young = strat.faults[-1] if strat.faults[-1].name == "young" else strat.faults[0] + young.builder.builders[0]._up_to_date = False + strat.builder.up_to_date() + assert young.builder.builders[0]._up_to_date