Skip to content

Commit b270d2c

Browse files
committed
feat: fall back to the default solver for each coordinate, not each feature
The coordinates 1 and 2 of a fault frame have orthogonal rows that the native core cannot read, so all the faults used cg. Now the plugin finds the coordinates that the core refuses (with the support check of loop-interpolation), solves only those with the default solver, and keeps native for the others. It records the coordinates, so the next build does not try native for them again. If the check finds no coordinate, the full feature uses the default solver, as before. The tests for the ADMM solver of inequality rows now turn the native core off, because with it the native solver solves the inequalities.
1 parent 42c02bd commit b270d2c

5 files changed

Lines changed: 175 additions & 24 deletions

File tree

‎loopstructural/main/model_manager.py‎

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ def __init__(self, debug_manager=None):
251251
# features that the native solver could not solve; they use the
252252
# default solver (see `_solve_feature`)
253253
self._native_failed: Set[str] = set()
254+
# feature name -> indexes of the builders (coordinates) that use the
255+
# default solver because the native solver could not solve them
256+
self._native_fallbacks: Dict[str, Set[int]] = {}
254257
# features with constraints that need an inequality solver
255258
self._needs_inequalities: Set[str] = set()
256259

@@ -331,6 +334,7 @@ def reset(self):
331334
self.nelements_used = {}
332335
self._data_summaries = {}
333336
self._native_failed = set()
337+
self._native_fallbacks = {}
334338
self._needs_inequalities = set()
335339
self.dem_function = lambda x, y: 0
336340
self._topology_dirty = False
@@ -1055,31 +1059,53 @@ def _inequality_solver(self, name, solver):
10551059
return None if self._use_native(name) else solver
10561060

10571061
def _solve_feature(self, feature, rebuild=True):
1058-
"""Build `feature`. If the native solver cannot solve it, build it with
1059-
the default solver, and use the default solver for it from now on.
1062+
"""Build `feature`. A coordinate that the native solver cannot solve is
1063+
built with the default solver, and uses it from now on.
1064+
1065+
Each coordinate of a structural frame has its own builder. For a fault,
1066+
the coordinates 1 and 2 have orthogonal rows that the native core cannot
1067+
read, but the coordinate 0 can use it. If the check of loop-interpolation
1068+
finds no coordinate, the full feature uses the default solver.
10601069
10611070
`rebuild` builds the feature again; otherwise only if it is out of date.
10621071
"""
1063-
try:
1064-
if rebuild:
1065-
feature.builder.update()
1066-
else:
1067-
feature.builder.up_to_date()
1068-
except solver_choice.NATIVE_ERRORS as error:
1069-
if not solver_choice.uses_native(feature):
1070-
raise
1071-
name = feature.name
1072-
self._native_failed.add(name)
1073-
fallback = solver_choice.fallback_solver(name in self._needs_inequalities)
1074-
if self._debug_manager is not None:
1075-
self._debug_manager.log(
1076-
f"The native solver cannot solve '{name}' ({error}). "
1077-
f"It is solved with '{fallback}'.",
1078-
log_level=1,
1079-
)
1080-
for builder in solver_choice.builders(feature):
1081-
builder.update_build_arguments({'solver': fallback})
1082-
feature.builder.update()
1072+
name = feature.name
1073+
builders = solver_choice.builders(feature)
1074+
fallback = solver_choice.fallback_solver(name in self._needs_inequalities)
1075+
# the coordinates that failed in an earlier build
1076+
for index in self._native_fallbacks.get(name, ()):
1077+
if index < len(builders):
1078+
builders[index].update_build_arguments({'solver': fallback})
1079+
build = feature.builder.update if rebuild else feature.builder.up_to_date
1080+
# one coordinate (or more) changes to the fallback at each attempt
1081+
for _attempt in range(len(builders) + 1):
1082+
try:
1083+
build()
1084+
return
1085+
except solver_choice.NATIVE_ERRORS as error:
1086+
if not solver_choice.uses_native(feature):
1087+
raise
1088+
failed = solver_choice.unsupported_builders(builders)
1089+
if not failed:
1090+
self._native_failed.add(name)
1091+
failed = list(range(len(builders)))
1092+
self._native_fallbacks.setdefault(name, set()).update(failed)
1093+
if self._debug_manager is not None:
1094+
which = (
1095+
f"the coordinates {sorted(failed)} of '{name}'"
1096+
if len(builders) > 1
1097+
else f"'{name}'"
1098+
)
1099+
self._debug_manager.log(
1100+
f"The native solver cannot solve {which} ({error}). "
1101+
f"They are solved with '{fallback}'.",
1102+
log_level=1,
1103+
)
1104+
for index in failed:
1105+
builders[index].update_build_arguments({'solver': fallback})
1106+
# the coordinates that are built already stay as they are
1107+
build = feature.builder.up_to_date
1108+
build()
10831109

10841110
def default_nelements(self, name) -> tuple:
10851111
"""Return (number of elements, True if automatic) for the feature

‎loopstructural/main/solver_choice.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@ def builders(feature):
5555
return list(getattr(builder, 'builders', None) or [builder])
5656

5757

58+
def unsupported_builders(builders):
59+
"""Return the indexes of the builders that use the native solver but that
60+
its core cannot solve, for example the coordinates 1 and 2 of a fault
61+
frame, which have orthogonal rows that the core cannot read.
62+
63+
It uses the support check of loop-interpolation. Returns an empty list if
64+
the check is not available or finds no problem.
65+
"""
66+
try:
67+
from loop_interpolation._native import check_supported
68+
except ImportError:
69+
return []
70+
failed = []
71+
for index, builder in enumerate(builders):
72+
if (getattr(builder, 'build_arguments', None) or {}).get('solver') != NATIVE:
73+
continue
74+
interpolator = getattr(builder, 'interpolator', None)
75+
if interpolator is None:
76+
continue
77+
try:
78+
check_supported(interpolator, getattr(interpolator, '_setup_kwargs', {}), {})
79+
except NotImplementedError:
80+
failed.append(index)
81+
except Exception:
82+
# the check is for one solve; any other problem is not a reason to change
83+
continue
84+
return failed
85+
86+
5887
def uses_native(feature) -> bool:
5988
"""Return True if a builder of the feature uses the native solver."""
6089
return any(

‎tests/qgis/test_detach_feature.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from loopstructural.main.data_manager import ModellingDataManager
1717
from loopstructural.main.model_manager import GeologicalModelManager
18+
from loopstructural.main import solver_choice
1819

1920

2021
def _contact(x):
@@ -138,7 +139,9 @@ def test_added_rows_stay_when_a_feature_is_detached(self, manager):
138139
assert sorted(manager.detached[name]['X']) == [1.0, 2.0]
139140
assert _built_x(manager) == [1.0, 2.0, 7.0]
140141

141-
def test_an_inequality_row_gives_the_solver(self, manager):
142+
def test_an_inequality_row_gives_the_solver(self, manager, monkeypatch):
143+
# the default solver; with the native solver, see the next test
144+
monkeypatch.setattr(solver_choice, 'native_available', lambda: False)
142145
name = _group_name(manager)
143146
spec = self._extra(7.0)
144147
spec['data']['values'].update(
@@ -148,6 +151,18 @@ def test_an_inequality_row_gives_the_solver(self, manager):
148151
manager.update_foliation_features()
149152
assert manager._captured[-1][2]['solver'] == 'admm'
150153

154+
def test_the_native_solver_also_solves_an_inequality_row(self, manager, monkeypatch):
155+
monkeypatch.setattr(solver_choice, 'native_available', lambda: True)
156+
monkeypatch.setattr(solver_choice, 'use_native', lambda setting: True)
157+
name = _group_name(manager)
158+
spec = self._extra(7.0)
159+
spec['data']['values'].update(
160+
{'type': 'Inequality', 'lower_field': 'v', 'upper_field': 'v'}
161+
)
162+
manager.extra_constraints[name] = spec
163+
manager.update_foliation_features()
164+
assert manager._captured[-1][2]['solver'] == 'native'
165+
151166
def test_a_bad_row_is_skipped_and_the_other_rows_are_used(self, manager):
152167
name = _group_name(manager)
153168
spec = self._extra(7.0)

‎tests/qgis/test_manual_foliations.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from shapely.geometry import Point
1414

1515
from loopstructural.main.model_manager import GeologicalModelManager
16+
from loopstructural.main import solver_choice
1617
from loopstructural.toolbelt.preferences import PlgSettingsStructure
1718

1819

@@ -169,13 +170,24 @@ def test_a_constant_z_source_gives_one_z(self, manager):
169170
data, _ = self._rows(manager, layer)
170171
assert (data['Z'] == 12.5).all()
171172

172-
def test_a_pairwise_inequality_needs_the_admm_solver(self, manager):
173+
def test_a_pairwise_inequality_needs_the_admm_solver(self, manager, monkeypatch):
174+
# the default solver; the native solver also solves inequalities
175+
monkeypatch.setattr(solver_choice, 'native_available', lambda: False)
173176
df = _points(order=lambda p: 1 if p.x < 50 else 2)
174177
layer = {'type': 'Pairwise Inequality', 'pair_field': 'order', 'df': df}
175178
data, kwargs = self._rows(manager, layer)
176179
assert kwargs == {'solver': 'admm'}
177180
assert 'pair_id' in data
178181

182+
def test_with_the_native_solver_a_pairwise_inequality_needs_no_admm(
183+
self, manager, monkeypatch
184+
):
185+
monkeypatch.setattr(solver_choice, 'use_native', lambda setting: True)
186+
df = _points(order=lambda p: 1 if p.x < 50 else 2)
187+
layer = {'type': 'Pairwise Inequality', 'pair_field': 'order', 'df': df}
188+
_data, kwargs = self._rows(manager, layer)
189+
assert 'solver' not in kwargs
190+
179191
def test_a_feature_from_value_and_normal_layers_solves_without_a_column(self, manager):
180192
values = _value_layer()
181193
normals = {

‎tests/qgis/test_native_solver.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,72 @@ def update(self):
106106
feature.builder = _Failing('cg')
107107
with pytest.raises(NotImplementedError):
108108
manager._solve_feature(feature)
109+
110+
111+
class _FrameBuilder:
112+
"""Three coordinates. A coordinate that the native core cannot solve raises."""
113+
114+
def __init__(self, refused):
115+
self.builders = [_Builder(solver_choice.NATIVE) for _ in range(3)]
116+
self.refused = set(refused)
117+
self.built = set()
118+
119+
def _build(self, everything):
120+
for index, builder in enumerate(self.builders):
121+
if index in self.built and not everything:
122+
continue
123+
solver = builder.build_arguments.get('solver')
124+
builder.solvers.append(solver)
125+
if solver == solver_choice.NATIVE and index in self.refused:
126+
raise NotImplementedError(
127+
"solver='native' does not support the setting "
128+
"'add_constraints_to_least_squares'"
129+
)
130+
self.built.add(index)
131+
132+
def update(self):
133+
self.built = set()
134+
self._build(everything=True)
135+
136+
def up_to_date(self):
137+
self._build(everything=False)
138+
139+
140+
class _Fault:
141+
def __init__(self, name, refused=(1, 2)):
142+
self.name = name
143+
self.builder = _FrameBuilder(refused)
144+
145+
146+
def test_only_the_coordinates_that_native_cannot_solve_use_the_default(manager, monkeypatch):
147+
_use(monkeypatch, solver_choice.NATIVE)
148+
fault = _Fault('f1')
149+
monkeypatch.setattr(solver_choice, 'unsupported_builders', lambda builders: [1, 2])
150+
manager._solve_feature(fault)
151+
solvers = [b.build_arguments['solver'] for b in fault.builder.builders]
152+
assert solvers == ['native', 'cg', 'cg']
153+
# the coordinate 0 is built one time, with native
154+
assert fault.builder.builders[0].solvers == ['native']
155+
assert manager._native_fallbacks['f1'] == {1, 2}
156+
# the feature still gets native, so the coordinate 0 uses it next time
157+
assert manager.interpolator_arguments('f1', None)['solver'] == 'native'
158+
159+
160+
def test_the_next_build_uses_the_recorded_coordinates(manager, monkeypatch):
161+
_use(monkeypatch, solver_choice.NATIVE)
162+
manager._native_fallbacks['f1'] = {1, 2}
163+
fault = _Fault('f1')
164+
manager._solve_feature(fault)
165+
# no native attempt for the coordinates 1 and 2
166+
assert [b.solvers for b in fault.builder.builders] == [['native'], ['cg'], ['cg']]
167+
168+
169+
def test_without_a_coordinate_from_the_check_the_full_feature_uses_the_default(
170+
manager, monkeypatch
171+
):
172+
_use(monkeypatch, solver_choice.NATIVE)
173+
fault = _Fault('f1')
174+
monkeypatch.setattr(solver_choice, 'unsupported_builders', lambda builders: [])
175+
manager._solve_feature(fault)
176+
assert [b.build_arguments['solver'] for b in fault.builder.builders] == ['cg'] * 3
177+
assert 'f1' in manager._native_failed

0 commit comments

Comments
 (0)