-
Notifications
You must be signed in to change notification settings - Fork 2
Fix type instability #157
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
base: main
Are you sure you want to change the base?
Fix type instability #157
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,15 @@ import ClimaCore.Fields: Adapt | |
import ClimaCore.Fields: ClimaComms | ||
|
||
import ClimaUtilities.Regridders | ||
import ClimaUtilities.Utils: unwrap | ||
|
||
struct InterpolationsRegridder{ | ||
SPACE <: ClimaCore.Spaces.AbstractSpace, | ||
FIELD <: ClimaCore.Fields.Field, | ||
BC, | ||
DT <: Tuple, | ||
DI <: Tuple, | ||
N, | ||
} <: Regridders.AbstractRegridder | ||
|
||
"""ClimaCore.Space where the output Field will be defined""" | ||
|
@@ -27,6 +30,12 @@ struct InterpolationsRegridder{ | |
"""Tuple of booleans signifying if the dimension is monotonically increasing. True for | ||
dimensions that are monotonically increasing, false for dimensions that are monotonically decreasing.""" | ||
dim_increasing::DT | ||
|
||
"""Tuple of integers indicating which dimensions to reverse in data""" | ||
decreasing_indices::DI | ||
|
||
"Number of dimensions of the target space" | ||
num_space_dims::N | ||
end | ||
|
||
# Note, we swap Lat and Long! This is because according to the CF conventions longitude | ||
|
@@ -74,23 +83,32 @@ function Regridders.InterpolationsRegridder( | |
isnothing(extrapolation_bc) && | ||
(extrapolation_bc = (Intp.Periodic(), Intp.Flat())) | ||
isnothing(dim_increasing) && (dim_increasing = (true, true)) | ||
num_space_dims = Val(2) | ||
elseif eltype(coordinates) <: ClimaCore.Geometry.LatLongZPoint | ||
isnothing(extrapolation_bc) && | ||
(extrapolation_bc = (Intp.Periodic(), Intp.Flat(), Intp.Throw())) | ||
isnothing(dim_increasing) && (dim_increasing = (true, true, true)) | ||
num_space_dims = Val(3) | ||
elseif eltype(coordinates) <: ClimaCore.Geometry.XYZPoint | ||
isnothing(extrapolation_bc) && | ||
(extrapolation_bc = (Intp.Flat(), Intp.Flat(), Intp.Throw())) | ||
isnothing(dim_increasing) && (dim_increasing = (true, true, true)) | ||
num_space_dims = Val(3) | ||
else | ||
error("Only lat-long, lat-long-z, and x-y-z spaces are supported") | ||
end | ||
|
||
decreasing_indices = | ||
!all(dim_increasing) ? | ||
Tuple([i for (i, d) in enumerate(dim_increasing) if !d]) : () | ||
|
||
return InterpolationsRegridder( | ||
target_space, | ||
coordinates, | ||
extrapolation_bc, | ||
dim_increasing, | ||
decreasing_indices, | ||
num_space_dims, | ||
) | ||
end | ||
|
||
|
@@ -102,17 +120,26 @@ Regrid the given data as defined on the given dimensions to the `target_space` i | |
This function is allocating. | ||
""" | ||
function Regridders.regrid(regridder::InterpolationsRegridder, data, dimensions) | ||
num_data_dims = ndims(data) | ||
num_dims = length(dimensions) | ||
num_space_dims = unwrap(regridder.num_space_dims) | ||
((num_space_dims != num_data_dims) || (num_space_dims != num_dims)) && | ||
error( | ||
"Number of dimensions of data ($num_data_dims) does not match the dimension of the space ($num_space_dims) or the number of dimensions passed in ($num_dims)", | ||
) | ||
|
||
FT = ClimaCore.Spaces.undertype(regridder.target_space) | ||
dimensions_FT = map(dimensions, regridder.dim_increasing) do dim, increasing | ||
!increasing ? reverse(FT.(dim)) : FT.(dim) | ||
end | ||
dimensions_FT = ntuple( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a check to check that the number of dimensions match between the given data and the space? |
||
i -> | ||
!regridder.dim_increasing[i] ? reverse(FT.(dimensions[i])) : | ||
FT.(dimensions[i]), | ||
regridder.num_space_dims, | ||
) | ||
Comment on lines
+133
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a more elegant way that we can write this? |
||
|
||
data_transformed = data | ||
# Reverse the data if needed. This allocates, so ideally it should be done in preprocessing | ||
if !all(regridder.dim_increasing) | ||
decreasing_indices = | ||
Tuple([i for (i, d) in enumerate(regridder.dim_increasing) if !d]) | ||
data_transformed = reverse(data, dims = decreasing_indices) | ||
data_transformed = reverse(data, dims = regridder.decreasing_indices) | ||
end | ||
# Make a linear spline | ||
itp = Intp.extrapolate( | ||
|
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.
Let's eliminate this, and instead write a function that computes this on the fly:
It'd be nice if we could do the same for the
decreasing_indices
, but I don't think it's possible since it depends on values of the input data.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.
This should also allow us to eliminate
unwrap
and its unit tests. We could add unit tests for this, of course, instead.