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

Completely overhaul grid utils + min_Δx/y/z -> minimum_spacing + move x/y/zspacing to Grids #2991

Merged
merged 39 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ad2d638
min_Δx/y/z -> minimum_spacing + move x/y/zspacing to Grids
navidcy Mar 21, 2023
67614bd
cleaner min_Δz for diffusive model
navidcy Mar 21, 2023
c89a30f
Completely overhaul grid utils
glwagner Mar 16, 2023
2165f2d
remove duplicates
navidcy Mar 21, 2023
4101a3f
no fallback for total_length
navidcy Mar 16, 2023
a9a0e01
Marie Kondo comes to Oceananigans
glwagner Mar 21, 2023
41be8df
new args for minimum_xyzspacing
navidcy Mar 21, 2023
fb4bb2a
use minimum_zspacing
navidcy Mar 21, 2023
4fa2d38
more docstrings
navidcy Mar 21, 2023
01b116f
use new total_size
navidcy Mar 21, 2023
5e903ea
export minimum xyzspacing
navidcy Mar 21, 2023
b026ede
fix tests
navidcy Mar 21, 2023
4a0d75d
some fixes
navidcy Mar 22, 2023
153ae03
move validate_mode_halo in Models + fix tests
navidcy Mar 22, 2023
2d54f82
Merge branch 'main' into ncc/miminum_spacing
navidcy Mar 22, 2023
b9156a6
instantiate more
navidcy Mar 22, 2023
ff2d992
Merge branch 'ncc/miminum_spacing' of github.com:CliMA/Oceananigans.j…
navidcy Mar 22, 2023
cdb6762
more fixes
navidcy Mar 22, 2023
b362d6b
fix fix fix
navidcy Mar 22, 2023
42630b3
some more fixes
navidcy Mar 22, 2023
334f417
more fix
navidcy Mar 22, 2023
21c7565
fix fix fix
navidcy Mar 22, 2023
8d3c0d1
fixes
navidcy Mar 22, 2023
bba6448
fix
navidcy Mar 22, 2023
9cc7205
more fixes
navidcy Mar 22, 2023
4291703
more fixes
navidcy Mar 22, 2023
ef8f344
more fixes
navidcy Mar 22, 2023
58f08b7
remove stray ]
navidcy Mar 22, 2023
e88a10b
back to normal
navidcy Mar 22, 2023
50c0f93
simpler minΔz
navidcy Mar 22, 2023
3c251d3
more instantiations
navidcy Mar 22, 2023
5272106
compute Nx/y_source_faces outside of kernel
navidcy Mar 22, 2023
bc32818
eliminate ::Type{Nothing}
navidcy Mar 22, 2023
997b55f
add allowscalar in equality between grids
navidcy Mar 22, 2023
531e62a
some more instantiations
navidcy Mar 22, 2023
c5cc7ad
ensure that figures are shown in docs
navidcy Mar 22, 2023
e764b13
fix mistake
navidcy Mar 23, 2023
2ddb509
Merge branch 'main' into ncc/miminum_spacing
glwagner Mar 23, 2023
34ec39a
Merge branch 'main' into ncc/miminum_spacing
navidcy Mar 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ version = "4.15.0"

[[deps.LLVMExtra_jll]]
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "Pkg", "TOML"]
git-tree-sha1 = "771bfe376249626d3ca12bcd58ba243d3f961576"
git-tree-sha1 = "7718cf44439c676bc0ec66a87099f41015a522d6"
uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
version = "0.0.16+0"
version = "0.0.16+2"

[[deps.LazyArtifacts]]
deps = ["Artifacts", "Pkg"]
Expand Down
6 changes: 1 addition & 5 deletions docs/src/appendix/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Modules = [Oceananigans.BoundaryConditions]
Private = false
```

## BuoyancyModels
## Buoyancy models

```@autodocs
Modules = [Oceananigans.BuoyancyModels]
Expand Down Expand Up @@ -127,7 +127,6 @@ Private = false
```@autodocs
Modules = [Oceananigans.Models.HydrostaticFreeSurfaceModels]
Private = false
]
```

### Shallow-water models
Expand All @@ -149,23 +148,20 @@ Private = false
```@autodocs
Modules = [Oceananigans.Operators]
Private = false
]
```

## Output readers

```@autodocs
Modules = [Oceananigans.OutputReaders]
Private = false
]
```

## Output writers

```@autodocs
Modules = [Oceananigans.OutputWriters]
Private = false
]
```

## Simulations
Expand Down
4 changes: 2 additions & 2 deletions examples/one_dimensional_diffusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ current_figure() # hide
# Next we set-up a `Simulation` that time-steps the model forward and manages output.

## Time-scale for diffusion across a grid cell
using Oceananigans.Grids: min_Δz
diffusion_time_scale = min_Δz(model.grid)^2 / model.closure.κ.T
min_Δz = minimum_zspacing(model.grid)
diffusion_time_scale = min_Δz^2 / model.closure.κ.T

simulation = Simulation(model, Δt = 0.1 * diffusion_time_scale, stop_iteration = 1000)

Expand Down
3 changes: 1 addition & 2 deletions examples/tilted_bottom_boundary_layer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ model = NonhydrostaticModel(; grid, buoyancy, coriolis, closure,
# conservatively, based on the smallest grid size of our domain and set-up a

using Oceananigans.Units
using Oceananigans.Grids: min_Δz

simulation = Simulation(model, Δt = 0.5 * min_Δz(grid) / V∞, stop_time = 2days)
simulation = Simulation(model, Δt = 0.5 * minimum_zspacing(grid) / V∞, stop_time = 2days)

# We use `TimeStepWizard` to adapt our time-step and print a progress message,

Expand Down
15 changes: 9 additions & 6 deletions src/AbstractOperations/at.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,19 @@ end
interpolate_index(::Colon, ::Colon, args...) = Colon()
interpolate_index(::Colon, b::UnitRange, args...) = b

instantiate(T::Type) = T()
instantiate(t) = t

function interpolate_index(a::UnitRange, ::Colon, loc, new_loc)
a = corrected_index(a, loc, new_loc)
a = corrected_index(a, instantiate(loc), instantiate(new_loc))

# Abstract operations that require an interpolation of a sliced fields are not supported!
first(a) > last(a) && throw(ArgumentError("Cannot interpolate a sliced field from $loc to $(new_loc)!"))
return a
end

function interpolate_index(a::UnitRange, b::UnitRange, loc, new_loc)
a = corrected_index(a, loc, new_loc)
a = corrected_index(a, instantiate(loc), instantiate(new_loc))

# Abstract operations that require an interpolation of a sliced fields are not supported!
first(a) > last(a) && throw(ArgumentError("Cannot interpolate a sliced field from $loc to $(new_loc)!"))
Expand All @@ -100,7 +103,7 @@ end

# Windowed fields interpolated from `Center`s to `Face`s lose the first index.
# Viceverse, windowed fields interpolated from `Face`s to `Center`s lose the last index
corrected_index(a, ::Type{Face}, ::Type{Face}) = UnitRange(first(a), last(a))
corrected_index(a, ::Type{Center}, ::Type{Center}) = UnitRange(first(a), last(a))
corrected_index(a, ::Type{Face}, ::Type{Center}) = UnitRange(first(a), last(a)-1)
corrected_index(a, ::Type{Center}, ::Type{Face}) = UnitRange(first(a)+1, last(a))
corrected_index(a, ::Face, ::Face) = UnitRange(first(a), last(a) )
corrected_index(a, ::Center, ::Center) = UnitRange(first(a), last(a) )
corrected_index(a, ::Face, ::Center) = UnitRange(first(a), last(a)-1)
corrected_index(a, ::Center, ::Face) = UnitRange(first(a)+1, last(a) )
1 change: 1 addition & 0 deletions src/Advection/Advection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ include("topologically_conditional_interpolation.jl")
include("momentum_advection_operators.jl")
include("tracer_advection_operators.jl")
include("positivity_preserving_tracer_advection_operators.jl")
include("cell_advection_timescale.jl")

end # module
19 changes: 19 additions & 0 deletions src/Advection/cell_advection_timescale.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Oceananigans.AbstractOperations: KernelFunctionOperation
using Oceananigans.Grids: topology, min_Δx, min_Δy, min_Δz

function cell_advection_timescale(grid, velocities)
u, v, w = velocities
τ = KernelFunctionOperation{Center, Center, Center}(cell_advection_timescaleᶜᶜᶜ, grid, u, v, w)
return minimum(τ)
end

@inline function cell_advection_timescaleᶜᶜᶜ(i, j, k, grid, u, v, w)
Δx = Δxᶠᶜᶜ(i, j, k, grid)
Δy = Δyᶜᶠᶜ(i, j, k, grid)
Δz = Δzᶜᶜᶠ(i, j, k, grid)

return @inbounds min(Δx / abs(u[i, j, k]),
Δy / abs(v[i, j, k]),
Δz / abs(w[i, j, k]))
end

9 changes: 5 additions & 4 deletions src/CubedSpheres/conformal_cubed_sphere_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,11 @@ const OSSG = OrthogonalSphericalShellGrid
##### Grid utils
#####

Base.size(grid::ConformalCubedSphereGrid) = (size(grid.faces[1])..., length(grid.faces))
Base.size(loc, grid::ConformalCubedSphereGrid) = size(loc, grid.faces[1])
Base.size(grid::ConformalCubedSphereGrid, i) = size(grid)[i]
halo_size(ccsg::ConformalCubedSphereGrid) = halo_size(first(ccsg.faces)) # hack
Base.size(grid::ConformalCubedSphereGrid) = (size(grid.faces[1])..., length(grid.faces))
Base.size(grid::ConformalCubedSphereGrid, loc::Tuple) = size(grid.faces[1], loc)
Base.size(grid::ConformalCubedSphereGrid, i::Int) = size(grid)[i]

halo_size(ccsg::ConformalCubedSphereGrid) = halo_size(first(ccsg.faces)) # hack

Base.eltype(grid::ConformalCubedSphereGrid{FT}) where FT = FT

Expand Down
87 changes: 45 additions & 42 deletions src/CubedSpheres/cubed_sphere_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,95 +11,98 @@ using Oceananigans.Grids:
##### Viewing halos
#####

instantiate(T::Type) = T()
instantiate(t) = t

west_halo(f::AbstractField{LX, LY, LZ}; include_corners=true) where {LX, LY, LZ} =
include_corners ? view(f.data, left_halo_indices(LX, topology(f, 1), f.grid.Nx, f.grid.Hx), :, :) :
view(f.data, left_halo_indices(LX, topology(f, 1), f.grid.Nx, f.grid.Hx),
interior_indices(LY, topology(f, 2), f.grid.Ny),
interior_indices(LZ, topology(f, 3), f.grid.Nz))
include_corners ? view(f.data, left_halo_indices(instantiate(LX), instantiate(topology(f, 1)), f.grid.Nx, f.grid.Hx), :, :) :
view(f.data, left_halo_indices(instantiate(LX), instantiate(topology(f, 1)), f.grid.Nx, f.grid.Hx),
interior_indices(instantiate(LY), instantiate(topology(f, 2)), f.grid.Ny),
interior_indices(instantiate(LZ), instantiate(topology(f, 3)), f.grid.Nz))

east_halo(f::AbstractField{LX, LY, LZ}; include_corners=true) where {LX, LY, LZ} =
include_corners ? view(f.data, right_halo_indices(LX, topology(f, 1), f.grid.Nx, f.grid.Hx), :, :) :
view(f.data, right_halo_indices(LX, topology(f, 1), f.grid.Nx, f.grid.Hx),
interior_indices(LY, topology(f, 2), f.grid.Ny),
interior_indices(LZ, topology(f, 3), f.grid.Nz))
include_corners ? view(f.data, right_halo_indices(instantiate(LX), instantiate(topology(f, 1)), f.grid.Nx, f.grid.Hx), :, :) :
view(f.data, right_halo_indices(instantiate(LX), instantiate(topology(f, 1)), f.grid.Nx, f.grid.Hx),
interior_indices(instantiate(LY), instantiate(topology(f, 2)), f.grid.Ny),
interior_indices(instantiate(LZ), instantiate(topology(f, 3)), f.grid.Nz))

south_halo(f::AbstractField{LX, LY, LZ}; include_corners=true) where {LX, LY, LZ} =
include_corners ? view(f.data, :, left_halo_indices(LY, topology(f, 2), f.grid.Ny, f.grid.Hy), :) :
view(f.data, interior_indices(LX, topology(f, 1), f.grid.Nx),
left_halo_indices(LY, topology(f, 2), f.grid.Ny, f.grid.Hy),
interior_indices(LZ, topology(f, 3), f.grid.Nz))
include_corners ? view(f.data, :, left_halo_indices(instantiate(LY), instantiate(topology(f, 2)), f.grid.Ny, f.grid.Hy), :) :
view(f.data, interior_indices(instantiate(LX), instantiate(topology(f, 1)), f.grid.Nx),
left_halo_indices(instantiate(LY), instantiate(topology(f, 2)), f.grid.Ny, f.grid.Hy),
interior_indices(instantiate(LZ), instantiate(topology(f, 3)), f.grid.Nz))

north_halo(f::AbstractField{LX, LY, LZ}; include_corners=true) where {LX, LY, LZ} =
include_corners ? view(f.data, :, right_halo_indices(LY, topology(f, 2), f.grid.Ny, f.grid.Hy), :) :
view(f.data, interior_indices(LX, topology(f, 1), f.grid.Nx),
right_halo_indices(LY, topology(f, 2), f.grid.Ny, f.grid.Hy),
interior_indices(LZ, topology(f, 3), f.grid.Nz))
include_corners ? view(f.data, :, right_halo_indices(instantiate(LY), instantiate(topology(f, 2)), f.grid.Ny, f.grid.Hy), :) :
view(f.data, interior_indices(instantiate(LX), instantiate(topology(f, 1)), f.grid.Nx),
right_halo_indices(instantiate(LY), instantiate(topology(f, 2)), f.grid.Ny, f.grid.Hy),
interior_indices(instantiate(LZ), instantiate(topology(f, 3)), f.grid.Nz))

bottom_halo(f::AbstractField{LX, LY, LZ}; include_corners=true) where {LX, LY, LZ} =
include_corners ? view(f.data, :, :, left_halo_indices(LZ, topology(f, 3), f.grid.Nz, f.grid.Hz)) :
view(f.data, interior_indices(LX, topology(f, 1), f.grid.Nx),
interior_indices(LY, topology(f, 2), f.grid.Ny),
left_halo_indices(LZ, topology(f, 3), f.grid.Nz, f.grid.Hz))
include_corners ? view(f.data, :, :, left_halo_indices(instantiate(LZ), instantiate(topology(f, 3)), f.grid.Nz, f.grid.Hz)) :
view(f.data, interior_indices(instantiate(LX), instantiate(topology(f, 1)), f.grid.Nx),
interior_indices(instantiate(LY), instantiate(topology(f, 2)), f.grid.Ny),
left_halo_indices(instantiate(LZ), instantiate(topology(f, 3)), f.grid.Nz, f.grid.Hz))

top_halo(f::AbstractField{LX, LY, LZ}; include_corners=true) where {LX, LY, LZ} =
include_corners ? view(f.data, :, :, right_halo_indices(LZ, topology(f, 3), f.grid.Nz, f.grid.Hz)) :
view(f.data, interior_indices(LX, topology(f, 1), f.grid.Nx),
interior_indices(LY, topology(f, 2), f.grid.Ny),
right_halo_indices(LZ, topology(f, 3), f.grid.Nz, f.grid.Hz))
include_corners ? view(f.data, :, :, right_halo_indices(instantiate(LZ), instantiate(topology(f, 3)), f.grid.Nz, f.grid.Hz)) :
view(f.data, interior_indices(instantiate(LX), instantiate(topology(f, 1)), f.grid.Nx),
interior_indices(instantiate(LY), instantiate(topology(f, 2)), f.grid.Ny),
right_halo_indices(instantiate(LZ), instantiate(topology(f, 3)), f.grid.Nz, f.grid.Hz))

underlying_west_halo(f, grid, location, topo=topology(grid, 1)) =
view(f.parent, underlying_left_halo_indices(location, topo, grid.Nx, grid.Hx), :, :)
view(f.parent, underlying_left_halo_indices(instantiate(location), instantiate(topo), grid.Nx, grid.Hx), :, :)

underlying_east_halo(f, grid, location, topo=topology(grid, 1)) =
view(f.parent, underlying_right_halo_indices(location, topo, grid.Nx, grid.Hx), :, :)
view(f.parent, underlying_right_halo_indices(instantiate(location), instantiate(topo), grid.Nx, grid.Hx), :, :)

underlying_south_halo(f, grid, location, topo=topology(grid, 2)) =
view(f.parent, :, underlying_left_halo_indices(location, topo, grid.Ny, grid.Hy), :)
view(f.parent, :, underlying_left_halo_indices(instantiate(location), instantiate(topo), grid.Ny, grid.Hy), :)

underlying_north_halo(f, grid, location, topo=topology(grid, 2)) =
view(f.parent, :, underlying_right_halo_indices(location, topo, grid.Ny, grid.Hy), :)
view(f.parent, :, underlying_right_halo_indices(instantiate(location), instantiate(topo), grid.Ny, grid.Hy), :)

underlying_bottom_halo(f, grid, location, topo=topology(grid, 3)) =
view(f.parent, :, :, underlying_left_halo_indices(location, topo, grid.Nz, grid.Hz))
view(f.parent, :, :, underlying_left_halo_indices(instantiate(location), instantiate(topo), grid.Nz, grid.Hz))

underlying_top_halo(f, grid, location, topo=topology(grid, 3)) =
view(f.parent, :, :, underlying_right_halo_indices(location, topo, grid.Nz, grid.Hz))
view(f.parent, :, :, underlying_right_halo_indices(instantiate(location), instantiate(topo), grid.Nz, grid.Hz))

#####
##### Viewing boundary grid points (used to fill other halos)
#####

left_boundary_indices(loc, topo, N, H) = 1:H
left_boundary_indices(::Type{Nothing}, topo, N, H) = 1:0 # empty
left_boundary_indices(::Nothing, topo, N, H) = 1:0 # empty

right_boundary_indices(loc, topo, N, H) = N-H+1:N
right_boundary_indices(::Type{Face}, ::Type{Bounded}, N, H) = N-H:N+1
right_boundary_indices(::Type{Nothing}, topo, N, H) = 1:0 # empty
right_boundary_indices(::Face, ::Bounded, N, H) = N-H:N+1
right_boundary_indices(::Nothing, topo, N, H) = 1:0 # empty

underlying_left_boundary_indices(loc, topo, N, H) = 1+H:2H
underlying_left_boundary_indices(::Type{Nothing}, topo, N, H) = 1:0 # empty
underlying_left_boundary_indices(::Nothing, topo, N, H) = 1:0 # empty

underlying_right_boundary_indices(loc, topo, N, H) = N+1:N+H
underlying_right_boundary_indices(::Type{Face}, ::Type{Bounded}, N, H) = N+2:N+H+1
underlying_right_boundary_indices(::Type{Nothing}, topo, N, H) = 1:0 # empty
underlying_right_boundary_indices(::Face, ::Bounded, N, H) = N+2:N+H+1
underlying_right_boundary_indices(::Nothing, topo, N, H) = 1:0 # empty

underlying_west_boundary(f, grid, location, topo=topology(grid, 1)) =
view(f.parent, underlying_left_boundary_indices(location, topo, grid.Nx, grid.Hx), :, :)
view(f.parent, underlying_left_boundary_indices(instantiate(location), instantiate(topo), grid.Nx, grid.Hx), :, :)

underlying_east_boundary(f, grid, location, topo=topo = topology(grid, 1)) =
view(f.parent, underlying_right_boundary_indices(location, topo, grid.Nx, grid.Hx), :, :)
view(f.parent, underlying_right_boundary_indices(instantiate(location), instantiate(topo), grid.Nx, grid.Hx), :, :)

underlying_south_boundary(f, grid, location, topo=topology(grid, 2)) =
view(f.parent, :, underlying_left_boundary_indices(location, topo, grid.Ny, grid.Hy), :)
view(f.parent, :, underlying_left_boundary_indices(instantiate(location), instantiate(topo), grid.Ny, grid.Hy), :)

underlying_north_boundary(f, grid, location, topo=topology(grid, 2)) =
view(f.parent, :, underlying_right_boundary_indices(location, topo, grid.Ny, grid.Hy), :)
view(f.parent, :, underlying_right_boundary_indices(instantiate(location), instantiate(topo), grid.Ny, grid.Hy), :)

underlying_bottom_boundary(f, grid, location, topo=topology(grid, 3)) =
view(f.parent, :, :, underlying_left_boundary_indices(location, topo, grid.Nz, grid.Hz))
view(f.parent, :, :, underlying_left_boundary_indices(instantiate(location), instantiate(topo), grid.Nz, grid.Hz))

underlying_top_boundary(f, grid, location, topo=topology(grid, 3)) =
view(f.parent, :, :, underlying_right_boundary_indices(location, topo, grid.Nz, grid.Hz))
view(f.parent, :, :, underlying_right_boundary_indices(instantiate(location), instantiate(topo), grid.Nz, grid.Hz))

#####
##### Convenience functions
Expand Down
2 changes: 1 addition & 1 deletion src/Diagnostics/cfl.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Oceananigans.Utils: cell_advection_timescale
using Oceananigans.Advection: cell_advection_timescale
using Oceananigans.TurbulenceClosures: cell_diffusion_timescale

"""
Expand Down
3 changes: 2 additions & 1 deletion src/Distributed/distributed_fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ function Field((LX, LY, LZ)::Tuple, grid::DistributedGrid, data, old_bcs, indice
indices = validate_indices(indices, (LX, LY, LZ), grid)
new_bcs = inject_halo_communication_boundary_conditions(old_bcs, arch.local_rank, arch.connectivity)
buffers = FieldBoundaryBuffers(grid, data, new_bcs)

return Field{LX, LY, LZ}(grid, data, new_bcs, indices, op, status, buffers)
end

const DistributedField = Field{<:Any, <:Any, <:Any, <:Any, <:DistributedGrid}
const DistributedFieldTuple = NamedTuple{S, <:NTuple{N, DistributedField}} where {S, N}

# TODO: make sure the definition of architecture is consistent
architecture(f::DistributedField) = child_architecture(architecture(f.grid))
architecture(f::DistributedField) = child_architecture(architecture(f.grid))
Loading