-
Notifications
You must be signed in to change notification settings - Fork 23
Add assert_eltype
helper
#3819
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
Add assert_eltype
helper
#3819
Conversation
WalkthroughThis update adds runtime type assertions to velocity vector functions in Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (10)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/cache/precomputed_quantities.jl (1)
290-297
: Clean up commented code.The commented alternative implementation should be removed if it's no longer needed, as it adds clutter to the codebase.
- # sfc_u₃ = Fields.level(ᶠu₃, half) - # sfc_uₕ³ = Fields.level(ᶠuₕ³, half) - # w₃ = @. lazy(-sfc_uₕ³ / sfc_u₃) # are metric terms automatically applied here? -
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/cache/precomputed_quantities.jl
(3 hunks)src/utils/utilities.jl
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (12)
- GitHub Check: docbuild
- GitHub Check: test (1.10)
- GitHub Check: test (1.11)
- GitHub Check: ci 1.11 - windows-latest
- GitHub Check: ci 1.11 - macOS-latest
- GitHub Check: ci 1.10 - windows-latest
- GitHub Check: ci 1.10 - macOS-latest
- GitHub Check: evaluate
- GitHub Check: ci 1.10 - ubuntu-latest
- GitHub Check: ci 1.11 - ubuntu-latest
- GitHub Check: downstream ClimaCoupler.jl (1.10)
- GitHub Check: downstream ClimaCoupler.jl (1.11)
🔇 Additional comments (4)
src/utils/utilities.jl (1)
497-516
: Well-designed utility function with proper multiple dispatch.The
assert_eltype
implementation is clean and follows Julia best practices. The multiple dispatch design allows extensibility for different container types, and the error message is informative.src/cache/precomputed_quantities.jl (3)
259-259
: Good type safety addition.Adding the assertion ensures
ᶜuₕ
conforms to the expectedCovariant12Vector
type, catching potential type mismatches early.
285-286
: Proper input validation.These assertions verify the geometric vector types match expectations, which aligns with the PR's goal of catching type inconsistencies.
326-328
: Comprehensive type checking.All three assertions properly validate the expected geometric vector types for the velocity computation inputs and outputs.
6bbc0fc
to
f309f92
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trontrytel and I are confused about the purpose of this PR. Is there a bug that you have identified in surface_velocity
? I am not sure what that bug could be, as the surface vertical velocity follows what is in the dycore paper. And this function needs to return a scalar Field, given how it is used in set_velocity_at_surface!
.
Also, I am strongly opposed to adding type assertions throughout our code like this. They make it very challenging to properly use duck typing, e.g., to use Covariant1Vector
s instead of Covariant12Vector
s for horizontal velocity in 2D domains. Unnecessary type restrictions have been making it very difficult for me to implement automatic differentiation in ClimaAtmos, and I think we should avoid them wherever possible.
Potentially?
Shouldn't it return a
I don't think that this type assertion will restrict autodiff stuff. Actually, I'd also like to remove some type assertions, like I see type assertions as a tool to help ensure correctness. I agree that we shouldn't use them if they restrict behavior, and as far as I can tell, this one should not. |
The destination is actually a scalar field, not a ClimaAtmos.jl/src/cache/precomputed_quantities.jl Lines 273 to 274 in 5e6f601
So returning a And yes, this particular type assertion does not restrict autodiff; it only restricts the horizontal velocity type. I was just pointing out that most type assertions constrain the amount of duck typing we can perform, with the types of real numbers and horizontal velocities being two examples. If we want to check for correct types, we should do so through specific unit tests, instead of scattering restrictive type assertions around our code. |
I was under the assumption that the |
Yeah it would be more accurate to call it something like |
I'll close, as it seems that people don't want this. |
When working on #3817, I found some things that surprised me a bit. In particular, with the new helper (added in this PR), I believe that this will fail (let's see what CI says):
despite, where this function is called looks like
sfc_u₃ .= surface_velocity(Y.f.u₃, ᶠuₕ³)
. The variable being assigned appears to be aCovariant3Vector
, however, the RHS of that expression doesn't seem to be.I think the reason is because we reach into the components to get floats, and then the type of the output is actually a
Contravariant3Vector
, as it adopts the type ofg³³
.This doesn't error, however, because ClimaCore has conversion methods for this sort of thing. I'm not sure if this is intended behavior, or if this will yield the intended result or not.
Assuming ClimaCore does this automatically, I'm actually surprised that this method isn't simply written as
Also, the
assert_eltype
should work for DataLayouts, Fields, and Broadcasted objects, so we can safely remove some of the other type assertions, which currently prevents some of the lazy machinery from working.