Skip to content

Support boolean comparison operations on fields#5309

Merged
glwagner merged 11 commits intomainfrom
glw/boolean-ops
Feb 20, 2026
Merged

Support boolean comparison operations on fields#5309
glwagner merged 11 commits intomainfrom
glw/boolean-ops

Conversation

@glwagner
Copy link
Copy Markdown
Member

And infer eltype from AbstractOperations in Field building.

Register comparison operators (>, <, >=, <=) as binary operations on fields, and use Base.promote_op to infer the result type of all AbstractOperation constructors instead of hardcoding eltype(grid). Field(op) now allocates data matching the operation's eltype, so boolean operations produce Bool arrays.

Closes #5307

cc @mmr0 @navidcy

… operations

Register comparison operators (>, <, >=, <=) as binary operations on fields,
and use Base.promote_op to infer the result type of all AbstractOperation
constructors instead of hardcoding eltype(grid). Field(op) now allocates data
matching the operation's eltype, so boolean operations produce Bool arrays.

Closes #5307

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@glwagner glwagner requested a review from navidcy February 18, 2026 03:01
@glwagner glwagner requested a review from bgroenks96 February 18, 2026 03:01
- Suppress Set output in @binary doctest (non-deterministic ordering)
- Wrap comparison `isa` tests in parentheses to fix precedence
  (`u > v isa T` parses as `u > (v isa T)`, not `(u > v) isa T`)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@navidcy navidcy added the feature 🌟 Something new and shiny label Feb 18, 2026
glwagner and others added 2 commits February 18, 2026 05:34
…ration

Base.promote_op can return Union{} or non-concrete types (like Any) for
complex kernel functions (e.g. ForcingKernelFunction, buoyancy_perturbation).
This caused MethodErrors when trying to allocate data arrays. Fall back to
eltype(grid) when promote_op cannot infer a concrete return type.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Adding >, <, >=, <= as binary operations on fields introduces 10 new
method ambiguities with Base's definitions.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Comment on lines +64 to +66
if T === Union{} || !isconcretetype(T)
T = eltype(grid)
end
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

is it also possible to just evaluate the kernel? eg something like

k = @allowscalar kernel_function(1, 1, 1, grid, arguments...)
T = typeof(k)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

since kernel functions are supposed to be relatively cheap (?), I think this would be fine?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yea agree

glwagner and others added 6 commits February 18, 2026 05:55
…er level

- All operation inner constructors now accept `::Type{T}` as an optional
  final argument with a sensible default, making them clean passthroughs
- BinaryOperation, UnaryOperation, MultiaryOperation: default T via
  `Base.promote_op` (reliable for simple eltypes)
- KernelFunctionOperation inner constructor takes `(kernel_function, grid,
  arguments::Tuple, T=eltype(grid))` — no argument splatting, usable as
  internal interface. Outer convenience constructor splats + infers T.
- adapt_structure, on_architecture, and multi-region getregion pass
  `eltype(op)` explicitly, avoiding redundant re-inference

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Kernel functions are too complex for Base.promote_op to reliably infer
return types. For example, SeawaterPolynomials.ρ with a Float32 grid
but Float64 EOS coefficients causes promote_op to return Float64,
changing the KFO eltype from Float32 to Float64 and breaking
Float32 seawater density tests.

Revert to always using eltype(grid) for KFO, as on main. The
promote_op eltype inference is still used for Binary, Unary, and
Multiary operations where it works reliably.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
promote_op inferred Float64 for TEOS10 density on Float32 grids
(because EOS polynomial coefficients are Float64), causing precision
mismatches in seawater density tests. The inner constructor already
defaults to eltype(grid); users can override T explicitly via the
inner constructor when needed.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.25%. Comparing base (a081c26) to head (a4f25d9).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5309      +/-   ##
==========================================
+ Coverage   70.95%   73.25%   +2.30%     
==========================================
  Files         389      397       +8     
  Lines       22287    22460     +173     
==========================================
+ Hits        15813    16453     +640     
+ Misses       6474     6007     -467     
Flag Coverage Δ
buildkite 68.62% <100.00%> (+2.00%) ⬆️
julia 68.62% <100.00%> (+2.00%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@glwagner glwagner merged commit 43e1f11 into main Feb 20, 2026
80 checks passed
@glwagner glwagner deleted the glw/boolean-ops branch February 20, 2026 00:20
briochemc added a commit that referenced this pull request Feb 20, 2026
…ield

* origin/main:
  Add DynamicSmagorinsky nonhydrostatic regression tests (#5310)
  Support boolean comparison operations on fields (#5309)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature 🌟 Something new and shiny

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support boolean comparison operations on fields (>, <, >=, <=, ==)

3 participants