Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish between null and unspecified selectors #254

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

pablosanjose
Copy link
Owner

To reduce latency and type proliferation, a generic siteselector that gets applied to a lattice becomes an AppliedSiteSelector with minimal type parameters. That means that e.g. cells, which can be many things becomes a Vector{SVector{L,Int}}. If cells was not specified (it was missing), it becomes an empty Vector{SVector{L,Int}}. The codebase knows that if it encounters an empty applied cells, this means "unconstrained" cells. The other option was to introduce Union{Vector{SVector{L,Int}}, Missing}, which I don't trust to always behave under type inference, so it was discarded.

The problem comes when one passes a function to cells, cells = n -> is_cell_in_subset(n) for example. If that ends up empty upon application to lattice, we currently don't distinguish this from the unconstrained cells=missing case. This causes bugs in corner cases in different places. One particular example

h = LP.square() |> hopping(0) |> supercell(3) |> @hopping!((t, r, dr) -> 1; sublats = Symbol[])

This h should be zero, but before this PR it is not, because an empty sublats::Vector{Int} results after application, which is indistinguishable from sublats = missing (or simply absent sublats) above.

The solution taken in this PR is to make AppliedSiteSelector and AppliedHopSelector know if they are an empty selector by adding an isnull field that is checked before everything else. Therefore, an empty applied cells that does not come from cells = missing will cause isnull == true, and the selector will behave as expected. The solution is quite minimal and has no performance or complexity drawbacks.

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 93.93939% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 92.28%. Comparing base (2c6ebdc) to head (91f551d).

Files Patch % Lines
src/apply.jl 85.71% 1 Missing ⚠️
src/types.jl 80.00% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #254      +/-   ##
==========================================
- Coverage   92.31%   92.28%   -0.04%     
==========================================
  Files          36       36              
  Lines        5883     5898      +15     
==========================================
+ Hits         5431     5443      +12     
- Misses        452      455       +3     

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

@pablosanjose pablosanjose merged commit 39e41aa into master Mar 5, 2024
9 checks passed
@pablosanjose pablosanjose mentioned this pull request Apr 25, 2024
@pablosanjose pablosanjose deleted the nullselectors branch October 25, 2024 15:40
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.

2 participants