Skip to content

add_condition refuses a value with no entry for a named component; shipped examples corrected (#1129) - #1139

Merged
lmoresi merged 7 commits into
developmentfrom
bugfix/short-bc-value-refused-1129
Nov 13, 2026
Merged

lmoresi merged 7 commits into
developmentfrom
bugfix/short-bc-value-refused-1129

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Fixes #1129. A boundary value shorter than the components it names is now refused at the call, and the shipped examples and docs that used that form are corrected.

The defect. add_dirichlet_bc(0.05, "Left", 1) and ((0.05,), "Left", (1,)) use the deprecated components= form with only the named value. They prescribed garbage, and the solve gave no sign of it. Measured on development 01dec8d, serial, on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH):

call u_y on Left
0.05, "Left", 1 (2-D) −2.7386, 0, 0.866 (asked 0.05); converged
(0.05,), "Left", (1,) (2-D) the same
0.05, "Left", 1 (3-D) down to −11.9
(None, 0.05) and (0.0, 0.05), components=(1,) 0.05, correct

Where the numbers come from. PETSc's essential-field callback fills the value array from the generated point function. A one-entry value writes only slot 0, which belongs to the free component. PETSc then reads the constrained component's slot, which the generated function never writes. The wrong values are the same in every process, so they are whatever the callback leaves in that slot, not random memory. So a short value naming component 0 happens to work, and one naming a later component does not.

The fix. add_condition (petsc_generic_snes_solvers.pyx) now raises ValueError when a component named by components has no entry in the value. It runs after the component list is resolved and before anything is recorded, so it covers essential and natural conditions on every solver class. The message gives the full-length form, e.g. (None, 0.05), only where it follows from the call: one entry per named component, the names distinct, each a component of the field, and each entry put at its own component. Otherwise it gives no example. Following a guessed example could free a constrained component, move a value to another component, or name a component the field lacks and be accepted while applying nothing.

  • Natural BCs: a short natural value that names a component it has no entry for used to fail later, with "Expecting reshape size to 1 but got prod((2,)) = 2". It now gets the same refusal at the call. Other natural values are unchanged.
  • Not refused: a short value naming only component 0, which works today, e.g. add_dirichlet_bc(0.0, "Left", 0). A test pins it.
  • No solver numerics change.

Why refuse rather than make the short form work. Making (0.05,) with components=(1,) mean "0.05 on component 1" would silently change what every such call has done until now. That would be a behaviour change through a deprecated form, which the deprecation exists to retire. The refusal changes no solve that works today, and it turns every one that didn't into an error naming the fix.

What it uncovered. Nine shipped files used the short form, and on development they were silently wrong:

  • five convection examples;
  • fluid_mechanics/basic/falling_sphere.py;
  • a deprecated disk example;
  • the tensor-variables example;
  • the developer cheat sheet for script writing, which taught it.

On a convection-style box, add_dirichlet_bc((0.0), "Top", (1)) puts 2.739 on u_y at Top and Bottom, where 0 was asked. All nine are rewritten to the full-length form, which gives 0 on every wall. So is a WIP developer example. Four WIP files that also pass a list of boundaries already fail the boundary-type check and are left alone. The examples parse gate passes (316 scripts, 27 notebooks). Four of the nine corrected scripts stop on unrelated errors before they reach their boundary conditions; that is #944's runtime half. For those four, the edit is checked by parsing only.

Tests. tests/test_1166_short_bc_value_with_components_refused.py, tier B:

  • short essential values, in both spellings, in 2-D and 3-D, are refused, naming the full-length form;
  • a short natural value is refused;
  • the refusal's example puts each entry at its own component (3-D, two entries for components 1 and 2), and there is no example where names and entries do not pair, or for a component the field lacks;
  • the full-length forms ((None, 0.05), and the deprecated form with a full-length value) still put 0.05 on the wall;
  • the short value naming only component 0 is still accepted and puts its value on the wall.

The refusal and example tests fail on development (the example tests also at 1601f77, this PR's earlier head) and pass here. The full-length and component-0 tests pass on both. Every test file that sets a boundary condition, 195 files, passes: 1530 passed, 7 skipped, 1 xfailed, 1 xpassed.

Relation to #1127 (merged). #1127 reads an essential condition's value at a node of a rotated wall. For the older form it falls back to one entry per named component (bc.fn[components.index(component)]). Its test test_the_older_components_form_of_an_essential_condition_still_solves called add_dirichlet_bc(0.05, "Left", 1), which this PR refuses. Development, with #1127, is merged in (a26acde), and that test is now test_the_older_short_components_form_is_refused_on_every_rank. It asserts the same refusal message on every rank, naming the missing entry and (None, 0.05). test_1066 and test_1166 pass serially (29), and the changed test passes at np 2. #1127's fallback for the short form now has no caller and can go in a later tidy.

Checklist (UW3 Style Charter — docs/developer/UW3_STYLE_CHARTER.md)

  • I have read the Style Charter and followed it over the surrounding code (S2)
  • No drive-by refactors, renames, or "while I was here" cleanups (S9): the example edits are the calls this change would refuse
  • Bug fixes ship the regression test, written first, with level_* and tier_* markers (S8)
  • No hedging names (maybe_ / try_ / do_) and no commented-out code (S3, S4)
  • Every exception swallow states its sanctioned failure mode in a comment (S4)
  • New data access uses .array / mesh.X.coords — no with ....access(...), no mesh.data (S7)
  • Parallel safety considered (S11): the check runs on the call's own arguments, identical on every rank, before any collective
  • No pixi.toml / pixi.lock or other dependency changes riding in an unrelated PR
  • AI-assisted work carries the attribution line below

Underworld development team with AI support from Claude Code

) [wip: examples next]

Underworld development team with AI support from Claude Code
…m becomes full-length (#1129)

Nine files used the deprecated form with a value shorter than the
components it names, e.g. add_dirichlet_bc((0.0), "Top", (1)). On
development that prescribes uninitialised memory for the named component.
On a convection-style box, u_y on Top and Bottom read 2.739 where 0 was
asked, and the solve diverged (reason -3). The full-length form gives 0
on every wall and converges.

Fixed:
- the convection examples: Cartesian-Swarm, SLCN-TdepVisc, SLCN-NL,
  SLCN-Yield and ThermoChem;
- fluid_mechanics/basic/falling_sphere.py;
- Ex_Stokes_Disk_CylCoords_DEPRECATED_NATIVE.py;
- utilities/advanced/Ex_Tensor_Variables_etc.py;
- docs/developer/ai-notes/UW3-SCRIPT-WRITING-CHEAT-SHEET.md, which taught
  the form.
The WIP developer example's three calls are in the previous commit. Four
other WIP files pass a list of boundaries, which already fails the
boundary-type check, and are left alone. The examples parse gate passes
(316 scripts, 27 notebooks).

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 13, 2026 06:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lmoresi

lmoresi commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Adversarial review at 1601f77

Reviewed by W1 (the integration session, which owns the solver code) with a fresh reviewing agent that was given the code and the PR, not our conclusions. Run on macOS (12-core Apple M4), serial only; nothing was run on more than one rank. Two builds in one environment were compared: "before" is c1eebf0 (the head of PR #1119, which contains development 01dec8d; it is not this PR's merge base a729719) and "after" is 1601f77. Between them the solver source differs by exactly this PR's 20 lines; five other Python files (mesh, swarm, multigrid) also differ, so the comparison does not isolate the 20 lines.

Result. The refusal does what the PR says: of 93 calls probed, 26 are now refused at the add call, none of those 26 worked before, and the other 67 are unchanged to the bit. Before merge we would have the example in the error message fixed: in three measured cases, following it gives a different condition or none, with no error (finding A). Whichever of this PR and #1127 merges second must first change a test in #1127 that this PR makes fail (finding B). The remaining findings do not block.

Findings

A. The example in the error message is wrong in three measured cases, and following it gives a wrong or absent condition with no exception. The message puts the value's first entry at the highest named component and None everywhere else. Measured on the after build:

refused call example in the message result of following it
(0.05,), "Left", (0, 1), 2-D Stokes (None, 0.05) u_y = 0.05; u_x on Left is free (0.0029 to 0.0236). The cheat-sheet call of this form was rewritten in this PR as (0.0, 0.0), not as the message advises
(0.01, 0.05), "Left", (1, 2), 3-D Stokes (None, None, 0.01) u_z = 0.01 (0.05 was the value given for it); u_y free (−0.0110 to 0.0117)
add_dirichlet_bc(0.0, "Top", 1), Poisson (one component) (None, 0.0) accepted; stored two entries, components [1]; T on Top stays 0.999998 to 1.000003; SNES reason 2. PETSc prints Constrained component[0] 1 not in [0, 1) components for field 0 on stderr; no Python exception

Other examples we read off the message: 3-D 0.05, "Left", 1 gives the two-entry (None, 0.05) under "one entry per field component" (it does work: u_y = 0.05); 0.05, "Left", 2 on a 2-D field gives (None, None, 0.05); uw.quantity(5.0, "cm/yr") gives (None, 1.5844043907014474e-09) (base-unit magnitude, units gone); np.float64(0.05) gives (None, np.float64(0.05)); a symbolic entry prints its LaTeX name; an empty value gives (None, value). The prefix is add_condition: whichever wrapper was called. A fix we would accept: print an example only when the value has as many entries as components names, with each entry at its own component; otherwise print none. The tests match only the 2-D single-component example. Blocks.

B. With this PR and #1127 both merged, a tier B test of #1127 fails; the body's "that fallback becomes unreachable. It is harmless" does not hold. #1127 (run at 0764729; the test is the same at its current head e4faf9b) has test_the_older_components_form_of_an_essential_condition_still_solves, which calls add_dirichlet_bc(0.05, "Left", 1). Run against the after build it fails with the new ValueError (1 failed). Neither PR's CI shows this unless rerun after the other merges. The fallback itself stays reachable: #1127's _essential_value, extracted and run on records this PR accepts, returns 0.0 for component 1 of 3-D (0.0, 0.05), "Left", 1 (the solve puts u_y = 0.05 there), and raises IndexError for components (1, 1, 0) with a two-entry value (caught by #1127's except). After this PR every named component has an entry at its own index, so the reader could use bc.fn[component] alone. git merge-tree of the two heads: no textual conflict. Does not block this PR; blocks whichever merges second.

C. components= with a long-enough value is still accepted in forms that give a wrong or absent condition (same on both builds). The check compares each named component with the value's length, not with the field's component count, and not with zero. Measured, 2-D Stokes, Left wall:

  • (0.01, 0.05), "Left", (1, 0): u_y = 0.05, u_x not held (−0.0849 to 0.0029, 0.01 asked); SNES reason 2.
  • (0.0, 0.05), "Left", -1 and (0, 0, 0.05), "Left", 2: no condition on Left (velocity hash equals the solve with no Left condition); PETSc prints Constrained component[0] -1 not in [0, 2) / 2 not in [0, 2) on stderr; no Python exception; SNES reason 2.
  • (0.01, 0.05), "Left", (0, 0, 1) and (0.05,), "Left", (-1, 0): the same no-condition hash.
  • (0.05, None), "Left", 1 and (None, 0.05), "Left", 0 (the named component's entry is None): accepted; SNES reason −4 at 0 iterations. The new code comment says a named component "must have its entry".
  • (0.05,), "Left", (): accepted; IndexError at solve.

None of this is in #1129's text and none of it changed. Does not block.

D. "Natural BCs … now gets the same refusal at the call" holds only when a component is named beyond the value's length. add_natural_bc(0.05, "Left") and add_natural_bc(0.05, "Left", 0) on a 2-D velocity are accepted on both builds and raise Expecting reshape size to 1 but got prod((2,)) = 2 at solve. Does not block; the body should say so.

E. The tests do not pin what the body promises stays accepted. test_1166 has six tests: four refusals (two essential spellings, 3-D, natural) and two full-length forms. Not covered: the short value naming component 0 on a vector field (the body's "Not refused"; five shipped examples still call (0.0), "Left", (0), and only test_0641, test_0764 and test_0112 pass components at all, none in that form); scalar fields; the (v,), (0, 1) form; list, Matrix and quantity values; add_essential_bc; the message's example beyond the 2-D case (the 3-D test matches only "component 1"). Does not block.

F. Two statements in the body we could not reproduce. (1) "uninitialised memory": the wrong values were the same in every process and for every value given (−2.73861 and 0.866025 in 2-D, velocity hash identical across twelve spellings and across 0 and 0.05). We did not trace what the slot holds. (2) "the solve diverges (reason −3)" on a convection-style box: on our box (unit square, cell size 0.1, free slip on four walls, buoyancy) the short form gave u_y from −2.73861 to 0.866025 on Top and Bottom with SNES reason 2 after one iteration. The body's driver is not in the PR. Does not block.

G. Four of the nine corrected scripts stop before their boundary-condition calls on the after build, on errors unrelated to this PR. Each script was truncated after its last corrected call and run. Five reach and pass the corrected calls (three convection examples, the thermochemical one, falling_sphere.py; stored [oo, 0.0], components [1]). Four stop earlier: the shear-band timing example (SwarmVariable.__init__() got an unexpected keyword argument 'num_components'), the swarm convection example ('SNES_AdvectionDiffusion' object has no attribute '_u_star_projector'), the deprecated disk example (module 'underworld3.meshing' has no attribute 'Mesh'), the tensor-variables example (a pyvista theme attribute). Their corrections are checked by reading and by the parse gate only. Does not block.

H. Housekeeping. The body says "H6's #1127" without saying what H6 is. The checklist ticks "no commented-out code" while the PR edits a commented-out call in the disk example and leaves a commented-out refused call in the shear-band timing example (line 328). The first commit's subject ends "[wip: examples next]". The new comment in the solver source partly narrates the old defect. The cheat sheet still teaches the deprecated short form for component 0 ((0.0,), "Left", (0,), lines 196 and 401–402) beside the corrected lines. #1129 carried no fixed-in-PR label; we have added it. Does not block.

What holds

  • Truth table, 93 calls, one process each, before against after. 67 records are identical: stored value and components, exception text, SNES reason, SNES and KSP iteration counts, and sha256 of the velocity and pressure arrays. 26 are now refused. No other difference. 55 calls solve on both builds with identical hashes and counts; 9 are refused on both with the same message (Unsupported BC conds for a numpy array or a bare sympy scalar; Unsupported BC 'components' argument for np.int64, array, str, float).
  • None of the 26 worked before. 18 solved without an exception and left the named component wrong on the wall: 2-D 0.05, "Left", 1 in twelve spellings (tuple, list, Matrix, np.float64, quantity, pint, add_essential_bc, keyword, value 0, symbolic) gave u_y from −2.73861 to 0.866025, SNES reason 2, 1 SNES and 1 KSP iteration; (0.05,), (0, 1) held u_x and not u_y; 3-D gave u_y down to −11.9059 and u_z down to −11.3413; (0.01, 0.05), (1, 2) in 3-D put 0.05 on u_y where 0.01 was asked; a vector projection gave the 2-D numbers. 4 applied no condition (component 2 of a 2-D field, component 1 of a Poisson unknown in both essential and natural form, component 1 of the pressure). 4 raised at solve (three natural forms with the reshape error; an empty value with ZeroDivisionError).
  • Accepted forms are unchanged. Short value naming component 0: u_x = 0.05 exactly, same hash on both builds. Every full-length form (None, oo, -oo, list, Matrix row and column, quantity, with and without components): u_y = 0.05 exactly, same hashes. Convection box with the full-length form: 0 on every wall, SNES reason 2, same hashes.
  • When and how. ValueError, raised at the add call after the DeprecationWarning, measured. It fires only when components= is given (by reading: with components=None the indices come from the value itself). By reading, it uses only the call's arguments, so it is the same on every rank and precedes any collective call. self.is_setup = False is set before the raise, so "before anything is recorded" is exact for the condition record but a refused call still marks the solver for set-up.
  • Tests. On the before build test_1166 gives 4 failed (each DID NOT RAISE ValueError) and 2 passed; on the after build 6 passed. Module mark level_1, each test tier_b. scripts/check_test_coverage.py: all 447 test files reachable. The three test files that pass components: 64 passed on both builds.
  • Examples and tree. scripts/check_examples_compile.py on the after tree: 316 scripts and 27 notebooks, 0 failures. Every rewritten call is the full-length form of what the old call named. A scan of the tree at the PR head (AST for scripts and notebooks, pattern search for text and Cython) finds no other call of the refused form in src, scripts, the guides or the notebooks, apart from the four WIP files with a list of boundaries that the body says it leaves. On the before build that list form is accepted and raises TypeError: unhashable type: 'list' at solve; on the after build it is refused at the add call.
  • Wider tests on the after build. We count 195 test files that set a boundary condition, as the body does. In two serial processes with -m "not level_3": 135 files run to completion, 1133 passed, 4 skipped, 1 xfailed, 0 failed (80 level_1 files: 762 passed, 3 skipped; 55 further files: 371 passed, 1 skipped, 1 xfailed). We stopped the second process on our time limit, so its counts are read from the progress output against a collect-only listing, not from a pytest summary line.
  • Conflicts. git merge-tree against development d66c7da and against Rotated free slip: warn on a nonzero essential value at a shared node; document the solve's result keys and time= (#939, #887, #410) #1127 at 0764729: no textual conflict. CI on the head: 9 jobs passed, 1 skipped.
  • Repository rules. Both commits and the body end with the attribution line; no Co-Authored-By, no emoji, no local paths, the machine is named by what it is, no variable named model. Changes outside the solver source and the test are the example and doc corrections.

Tolerances and coordinate matches

The library change introduces none. The test file selects wall nodes by coordinate (|x| < 1e-9 for Left, y > 1e-9 to leave out the Bottom corner) and asserts |u_y − 0.05| < 1e-12; we measured a difference of 0.0.

Not checked

  • Anything on more than one rank. Rank independence of the refusal is by reading.
  • Ten boundary-condition test files that launch, or may launch, subprocesses or MPI: test_0022, test_0079, test_0105, test_0839, test_0842, test_0844, test_0858, test_1020, test_1070_free_surface_plume, test_1072.
  • 32 boundary-condition test files not reached when we stopped (test_1060_stress_store_smoothing onward, through test_1174), and every level_3 test (18 of the files hold nothing else).
  • The wider test subset on the before build; we have no before count for it.
  • A before build at the PR's own merge base.
  • What PETSc holds in the slot the short value never writes.
  • Whether the four examples in finding G also stop early on the before build; the notebooks beyond the parse gate.
  • The body's convection driver and its reason −3; Linux.

Issue verdicts

Underworld development team with AI support from Claude Code

claude and others added 5 commits November 13, 2026 20:34
…he call (#1129)

The example in the refusal was built as "None up to the highest named component, then
the value's first entry". Following it changed the condition where names and entries do
not pair one to one: (0.05,) with components (0, 1) advised (None, 0.05), which frees u_x;
in 3-D (0.01, 0.05) with (1, 2) advised (None, None, 0.01), putting u_y's value on u_z; on
a one-component unknown, components 1 advised (None, 0.0), which is accepted and applies
nothing.

The example is now printed only when the value has one entry per named component, the
names are distinct, and each is a component of the field; each entry goes to its own
component. Otherwise the refusal gives no example. A repeated name is listed once.

Underworld development team with AI support from Claude Code
… prescribes its value (#1129)

Underworld development team with AI support from Claude Code
…1129, with #1127)

#1127's test called add_dirichlet_bc(0.05, "Left", 1), the one-entry value naming
component 1 that this branch refuses. It now asserts the refusal: the same message on
every rank, naming the missing entry and the full-length form (None, 0.05).

Underworld development team with AI support from Claude Code
@lmoresi
lmoresi merged commit ac49e70 into development Nov 13, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants