Skip to content

Commit 4a4b185

Browse files
committed
fix: give the native solver the iteration limit of cg, and fall back when it does not converge
The native core stops after 5 000 iterations by default; cg allows 10 x the number of nodes. On a large grid the native solve stopped before it converged. loop-interpolation then keeps the old field, which had the size of the old grid, so the surfaces failed ("cannot assign 51000 values to array of shape 87232"). - Before each solve, give each native builder maxiter = 10 x its number of nodes. - After the build, a coordinate whose native solve did not converge is solved again with the default solver, and uses it from then on. The native limit is not given to the default solver.
1 parent c814c46 commit 4a4b185

4 files changed

Lines changed: 140 additions & 17 deletions

File tree

‎loopstructural/main/model_manager.py‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,10 @@ def _solve_feature(self, feature, rebuild=True):
10671067
read, but the coordinate 0 can use it. If the check of loop-interpolation
10681068
finds no coordinate, the full feature uses the default solver.
10691069
1070+
A native solve that does not converge is also solved again with the
1071+
default solver: loop-interpolation then keeps the old field, which can
1072+
have the size of an old grid.
1073+
10701074
`rebuild` builds the feature again; otherwise only if it is out of date.
10711075
"""
10721076
name = feature.name
@@ -1075,37 +1079,64 @@ def _solve_feature(self, feature, rebuild=True):
10751079
# the coordinates that failed in an earlier build
10761080
for index in self._native_fallbacks.get(name, ()):
10771081
if index < len(builders):
1078-
builders[index].update_build_arguments({'solver': fallback})
1082+
self._set_fallback(builders[index], fallback)
1083+
self._set_native_limits(builders)
10791084
build = feature.builder.update if rebuild else feature.builder.up_to_date
10801085
# one coordinate (or more) changes to the fallback at each attempt
10811086
for _attempt in range(len(builders) + 1):
10821087
try:
10831088
build()
1084-
return
1089+
break
10851090
except solver_choice.NATIVE_ERRORS as error:
10861091
if not solver_choice.uses_native(feature):
10871092
raise
10881093
failed = solver_choice.unsupported_builders(builders)
10891094
if not failed:
10901095
self._native_failed.add(name)
10911096
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})
1097+
self._use_fallback(name, builders, failed, fallback, f"not supported ({error})")
11061098
# the coordinates that are built already stay as they are
11071099
build = feature.builder.up_to_date
1108-
build()
1100+
else:
1101+
build()
1102+
unconverged = solver_choice.unconverged_builders(builders)
1103+
if unconverged:
1104+
self._use_fallback(name, builders, unconverged, fallback, "did not converge")
1105+
feature.builder.up_to_date()
1106+
1107+
@staticmethod
1108+
def _set_fallback(builder, fallback):
1109+
# the native iteration limit is not for the other solvers
1110+
builder.update_build_arguments({'solver': fallback, 'solver_kwargs': {}})
1111+
1112+
@staticmethod
1113+
def _set_native_limits(builders):
1114+
"""Give each native builder the iteration limit of its grid.
1115+
1116+
Only a change of the limit marks the builder out of date.
1117+
"""
1118+
for builder in builders:
1119+
if (builder.build_arguments or {}).get('solver') != solver_choice.NATIVE:
1120+
continue
1121+
limits = solver_choice.native_solver_kwargs(builder)
1122+
if limits and builder.build_arguments.get('solver_kwargs') != limits:
1123+
builder.update_build_arguments({'solver_kwargs': limits})
1124+
1125+
def _use_fallback(self, name, builders, indexes, fallback, reason):
1126+
"""Solve the builders `indexes` of the feature `name` with `fallback` from now on."""
1127+
self._native_fallbacks.setdefault(name, set()).update(indexes)
1128+
if self._debug_manager is not None:
1129+
which = (
1130+
f"the coordinates {sorted(indexes)} of '{name}'"
1131+
if len(builders) > 1
1132+
else f"'{name}'"
1133+
)
1134+
self._debug_manager.log(
1135+
f"Native solver, {which}: {reason}. Solved with '{fallback}'.",
1136+
log_level=1,
1137+
)
1138+
for index in indexes:
1139+
self._set_fallback(builders[index], fallback)
11091140

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

‎loopstructural/main/solver_choice.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
# The errors that tell that the native solver cannot solve a feature
2323
NATIVE_ERRORS = (NotImplementedError, ImportError)
24+
# The iteration limit of a native solve, for each node of the grid (as cg)
25+
NATIVE_ITERATIONS_PER_NODE = 10
2426

2527

2628
def native_available() -> bool:
@@ -84,6 +86,35 @@ def unsupported_builders(builders):
8486
return failed
8587

8688

89+
def native_solver_kwargs(builder):
90+
"""Return the solver_kwargs for a native solve of `builder`, or None.
91+
92+
The core stops after 5 000 iterations by default. The cg solver of scipy
93+
allows 10 x the number of nodes. With the default, a large grid can stop
94+
before it converges, so give the native solver the same limit as cg.
95+
"""
96+
support = getattr(getattr(builder, 'interpolator', None), 'support', None)
97+
n_nodes = getattr(support, 'n_nodes', None)
98+
if not n_nodes:
99+
return None
100+
return {'maxiter': int(NATIVE_ITERATIONS_PER_NODE * n_nodes)}
101+
102+
103+
def unconverged_builders(builders):
104+
"""Return the indexes of the builders whose native solve did not converge.
105+
106+
Then loop-interpolation keeps the old field of the interpolator, which can
107+
have the size of an old grid.
108+
"""
109+
return [
110+
index
111+
for index, builder in enumerate(builders)
112+
if (getattr(builder, 'build_arguments', None) or {}).get('solver') == NATIVE
113+
and getattr(builder, 'interpolator', None) is not None
114+
and not getattr(builder.interpolator, 'up_to_date', True)
115+
]
116+
117+
87118
def uses_native(feature) -> bool:
88119
"""Return True if a builder of the feature uses the native solver."""
89120
return any(

‎tests/qgis/test_native_solver.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
the fallback, not LoopStructural.
55
"""
66

7+
from types import SimpleNamespace
8+
79
import pytest
810

911
from loopstructural.main import model_manager as model_manager_module
@@ -175,3 +177,43 @@ def test_without_a_coordinate_from_the_check_the_full_feature_uses_the_default(
175177
manager._solve_feature(fault)
176178
assert [b.build_arguments['solver'] for b in fault.builder.builders] == ['cg'] * 3
177179
assert 'f1' in manager._native_failed
180+
181+
182+
class _GridBuilder(_Builder):
183+
"""A builder with an interpolator. The native solve converges if `converges`."""
184+
185+
def __init__(self, converges=True, n_nodes=1000):
186+
super().__init__(solver_choice.NATIVE)
187+
self.converges = converges
188+
self.interpolator = SimpleNamespace(
189+
support=SimpleNamespace(n_nodes=n_nodes), up_to_date=False
190+
)
191+
self.kwargs_at_solve = []
192+
193+
def update(self):
194+
solver = self.build_arguments.get('solver')
195+
self.solvers.append(solver)
196+
self.kwargs_at_solve.append(dict(self.build_arguments.get('solver_kwargs') or {}))
197+
self.interpolator.up_to_date = solver != solver_choice.NATIVE or self.converges
198+
199+
up_to_date = update
200+
201+
202+
def test_the_native_solve_gets_the_iteration_limit_of_its_grid(manager, monkeypatch):
203+
_use(monkeypatch, solver_choice.NATIVE)
204+
feature = _Feature('s1')
205+
feature.builder = _GridBuilder(n_nodes=87232)
206+
manager._solve_feature(feature)
207+
assert feature.builder.kwargs_at_solve == [{'maxiter': 872320}]
208+
209+
210+
def test_a_native_solve_that_does_not_converge_is_solved_with_the_default(manager, monkeypatch):
211+
_use(monkeypatch, solver_choice.NATIVE)
212+
feature = _Feature('Group_0')
213+
feature.builder = _GridBuilder(converges=False)
214+
manager._solve_feature(feature)
215+
assert feature.builder.solvers == ['native', 'cg']
216+
# the native limit is not given to cg
217+
assert feature.builder.kwargs_at_solve[-1] == {}
218+
assert manager._native_fallbacks['Group_0'] == {0}
219+
assert any('did not converge' in m for m in manager._debug_manager.messages)

‎tests/unit/test_solver_choice.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,22 @@ def test_uses_native():
4949

5050
def test_every_choice_has_a_label():
5151
assert set(solver_choice.LABELS) == set(solver_choice.CHOICES)
52+
53+
54+
def _native_builder(n_nodes=1000, up_to_date=True, solver='native'):
55+
interpolator = SimpleNamespace(support=SimpleNamespace(n_nodes=n_nodes), up_to_date=up_to_date)
56+
return SimpleNamespace(build_arguments={'solver': solver}, interpolator=interpolator)
57+
58+
59+
def test_the_native_limit_is_ten_iterations_for_each_node():
60+
assert solver_choice.native_solver_kwargs(_native_builder(87232)) == {'maxiter': 872320}
61+
assert solver_choice.native_solver_kwargs(SimpleNamespace()) is None
62+
63+
64+
def test_unconverged_builders():
65+
builders = [
66+
_native_builder(),
67+
_native_builder(up_to_date=False),
68+
_native_builder(up_to_date=False, solver='cg'),
69+
]
70+
assert solver_choice.unconverged_builders(builders) == [1]

0 commit comments

Comments
 (0)