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

handle FT of 0-dim Array #484

Merged
merged 2 commits into from
May 2, 2024
Merged

handle FT of 0-dim Array #484

merged 2 commits into from
May 2, 2024

Conversation

juliasloan25
Copy link
Member

@juliasloan25 juliasloan25 commented Apr 30, 2024

Purpose

closes #482
bumps patch version so we can make a release with fix

tested in ClimaAtmos here: CliMA/ClimaAtmos.jl#2968 (passing build)

@@ -762,17 +762,17 @@ function LookUpCld(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
])
bounds = DA([
# liquid particle size lower bound for LUT interpolation
FT(Array(ds["radliq_lwr"])),
FT.(Array(ds["radliq_lwr"])),
Copy link
Member

Choose a reason for hiding this comment

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

Would this work?

    bounds = DA([
        # liquid particle size lower bound for LUT interpolation
        FT(ds["radliq_lwr"][]),
        # liquid particle size upper bound for LUT interpolation
        FT(ds["radliq_upr"][]),
        # factor for calculating LUT interpolation for liquid particle
        FT(ds["radliq_fac"][]),
        # ice particle size lower bound for LUT interpolation
        FT(ds["radice_lwr"][]),
        # ice particle size upper bound for LUT interpolation
        FT(ds["radice_upr"][]),
        # factor for calculating LUT interpolation for ice particle
        FT(ds["radice_fac"][]),
    ])

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so:

julia> Float64(rand(3))
ERROR: MethodError: no method matching Float64(::Vector{Float64})

Closest candidates are:
  Float64(::Int128)
   @ Base float.jl:200
  Float64(::Bool)
   @ Base float.jl:165
  Float64(::Int16)
   @ Base float.jl:159
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[8]:1

I would actually prefer using an array constructor, instead of recasting after the array has been made:

julia> Array{Float32}(rand(3,3))
3×3 Matrix{Float32}:
 0.101806  0.417171  0.951682
 0.249531  0.359648  0.873839
 0.586299  0.165479  0.341307

So, does Array{FT}(ds["radliq_lwr"]), work?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I missed the getindex call in @sriharshakandala's suggestion. That should work too, assuming the result is of the same type that we expect

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like that works for the cases we're currently testing. Would this work for an array of length > 1 though? I get:

julia> arr = Array{Float64}(undef, 2)
2-element Vector{Float64}:
 2.2080290545e-314
 2.2080785164e-314

julia> FT(arr[])
ERROR: BoundsError: attempt to access 2-element Vector{Float64} at index []
...

Also, do we still want to keep the Array conversion so we don't create a CuArray on GPU? Or would it create an Array anyways because it's reading from a NetCDF file?

Copy link
Member

Choose a reason for hiding this comment

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

In this specific case, these are all scalars!

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I think I wasn't clear, I meant for my suggestion to be:

    bounds = DA([
        # liquid particle size lower bound for LUT interpolation
        Array{FT}(ds["radliq_lwr"]),
        # liquid particle size upper bound for LUT interpolation
        Array{FT}(ds["radliq_upr"]),
        # factor for calculating LUT interpolation for liquid particle
        Array{FT}(ds["radliq_fac"]),
        # ice particle size lower bound for LUT interpolation
        Array{FT}(ds["radice_lwr"]),
        # ice particle size upper bound for LUT interpolation
        Array{FT}(ds["radice_upr"]),
        # factor for calculating LUT interpolation for ice particle
        Array{FT}(ds["radice_fac"]),
    ])

This whole thing does look odd, though. I think perhaps @sriharshakandala's suggestion is best assuming these are all 1-element arrays.

Copy link
Member Author

@juliasloan25 juliasloan25 May 1, 2024

Choose a reason for hiding this comment

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

It seems like ds["var"] is sometimes returning a 0-length array with the CommonDataModels release, so I'm not sure if we can rely on them being scalars. Though with Sriharsha's fix, the test that was previously failing does now pass, even though I get an error when trying to access a 0-length array with []

julia> arr
Float64[]

julia> arr[]
ERROR: BoundsError: attempt to access 0-element Vector{Float64} at index []
...

Copy link
Member

Choose a reason for hiding this comment

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

The changes to the LookUpTables.jl looks good to me. Thanks!

@juliasloan25 juliasloan25 changed the title broadcast FT over Array handle FT of 0-dim Array May 1, 2024
@juliasloan25
Copy link
Member Author

@sriharshakandala @charleskawczynski the MacOS test are hanging/timing out, but the ubuntu tests are passing. Is it okay to merge this anyway?

@charleskawczynski
Copy link
Member

@sriharshakandala @charleskawczynski the MacOS test are hanging/timing out, but the ubuntu tests are passing. Is it okay to merge this anyway?

Yeah, that's fine. I think there's been issues with GH runners lately.

@juliasloan25 juliasloan25 merged commit d618bd8 into main May 2, 2024
8 of 9 checks passed
@juliasloan25 juliasloan25 deleted the js/ft-broadcast branch May 2, 2024 19:16
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.

Array() now returns an Array for 0-dimensional input
3 participants