Skip to content

Commit

Permalink
@unpack + import Paramters removed
Browse files Browse the repository at this point in the history
  • Loading branch information
milankl committed May 8, 2023
1 parent 8206d04 commit 1d4aec4
Show file tree
Hide file tree
Showing 27 changed files with 280 additions and 285 deletions.
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Expand All @@ -44,7 +43,6 @@ GenericFFT = "0.1"
JLD2 = "0.4"
KernelAbstractions = "0.7, 0.8"
NetCDF = "0.10, 0.11"
Parameters = "0.10, 0.11, 0.12"
Primes = "0.5"
ProgressMeter = "1.7"
UnicodePlots = "3.3.2"
Expand Down
1 change: 0 additions & 1 deletion src/RingGrids/RingGrids.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module RingGrids

import Parameters: @unpack
import Statistics: mean
import FastGaussQuadrature

Expand Down
16 changes: 8 additions & 8 deletions src/RingGrids/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function interpolate( A::AbstractGrid{NF}, # field to interpolate
I::AbstractInterpolator # indices in I are assumed to be calculated already!
) where NF # use number format from input data also for output

@unpack npoints = I.locator # number of points to interpolate onto
(; npoints ) = I.locator # number of points to interpolate onto
Aout = Vector{NF}(undef,npoints) # preallocate: onto θs,λs interpolated values of A
interpolate!(Aout,A,I) # perform interpolation, store in As
end
Expand All @@ -204,8 +204,8 @@ function interpolate!( Aout::Vector, # Out: interpolated values
interpolator::AnvilInterpolator{NF,Grid}, # geometry info and work arrays
) where {NF<:AbstractFloat,Grid<:AbstractGrid}

@unpack ij_as,ij_bs,ij_cs,ij_ds,Δabs,Δcds,Δys = interpolator.locator
@unpack npoints = interpolator.geometry
(; ij_as,ij_bs,ij_cs,ij_ds,Δabs,Δcds,Δys ) = interpolator.locator
(; npoints ) = interpolator.geometry

# 1) Aout's length must match the interpolator
# 2) input grid A must match the interpolator's geometry (Grids are checked with dispatch)
Expand Down Expand Up @@ -286,8 +286,8 @@ function update_locator!( I::AbstractInterpolator{NF,Grid}, # GridGeometry a
) where {NF<:AbstractFloat,Grid<:AbstractGrid}

# find latitude ring indices corresponding to interpolation points
@unpack latd = I.geometry # latitudes of rings including north and south pole
@unpack js,Δys = I.locator # to be updated: ring indices js, and meridional weights Δys
(; latd ) = I.geometry # latitudes of rings including north and south pole
(; js,Δys ) = I.locator # to be updated: ring indices js, and meridional weights Δys
find_rings!(js,Δys,θs,latd;unsafe) # next ring at or north of θ

# find grid incides ij for top, bottom and left, right grid points around (θ,λ)
Expand Down Expand Up @@ -354,9 +354,9 @@ end
function find_grid_indices!(I::AnvilInterpolator, # update indices arrays
λs::Vector) # based on new longitudes λ

@unpack js, ij_as, ij_bs, ij_cs, ij_ds = I.locator
@unpack Δabs, Δcds = I.locator
@unpack nlons, rings, lon_offsets, nlat = I.geometry
(; js, ij_as, ij_bs, ij_cs, ij_ds ) = I.locator
(; Δabs, Δcds ) = I.locator
(; nlons, rings, lon_offsets, nlat ) = I.geometry

@inbounds for (k,(λf,j)) in enumerate(zip(λs,js))

Expand Down
2 changes: 0 additions & 2 deletions src/SpeedyTransforms/SpeedyTransforms.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module SpeedyTransforms

import Parameters: @unpack

# NUMERICS
import AssociatedLegendrePolynomials as Legendre
import AbstractFFTs
Expand Down
8 changes: 4 additions & 4 deletions src/SpeedyTransforms/spectral_gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function _divergence!( kernel,
@boundscheck size(u) == size(div) || throw(BoundsError)
@boundscheck size(v) == size(div) || throw(BoundsError)

@unpack grad_y_vordiv1,grad_y_vordiv2 = S
(; grad_y_vordiv1,grad_y_vordiv2 ) = S
@boundscheck size(grad_y_vordiv1) == size(div) || throw(BoundsError)
@boundscheck size(grad_y_vordiv2) == size(div) || throw(BoundsError)
lmax,mmax = size(div) .- (2,1) # 0-based lmax,mmax
Expand Down Expand Up @@ -122,7 +122,7 @@ function UV_from_vor!( U::LowerTriangularMatrix{Complex{NF}},
S::SpectralTransform{NF}
) where {NF<:AbstractFloat}

@unpack vordiv_to_uv_x,vordiv_to_uv1,vordiv_to_uv2 = S
(; vordiv_to_uv_x,vordiv_to_uv1,vordiv_to_uv2 ) = S
lmax,mmax = size(vor) .- (2,1) # 0-based lmax,mmax

@boundscheck lmax == mmax || throw(BoundsError)
Expand Down Expand Up @@ -196,7 +196,7 @@ function UV_from_vordiv!( U::LowerTriangularMatrix{Complex{NF}},
S::SpectralTransform{NF}
) where {NF<:AbstractFloat}

@unpack vordiv_to_uv_x,vordiv_to_uv1,vordiv_to_uv2 = S
(; vordiv_to_uv_x,vordiv_to_uv1,vordiv_to_uv2 ) = S
lmax,mmax = size(vor) .- (2,1) # 0-based lmax,mmax
@boundscheck lmax == mmax || throw(BoundsError)
@boundscheck size(div) == size(vor) || throw(BoundsError)
Expand Down Expand Up @@ -344,7 +344,7 @@ function ∇!(dpdx::LowerTriangularMatrix{Complex{NF}}, # Output: zonal gr
@boundscheck size(p) == size(dpdx) || throw(BoundsError)
@boundscheck size(p) == size(dpdy) || throw(BoundsError)

@unpack grad_y1, grad_y2 = S
(; grad_y1, grad_y2 ) = S

lm = 0
@inbounds for m in 1:mmax # 1-based l,m, skip last column
Expand Down
22 changes: 11 additions & 11 deletions src/SpeedyTransforms/spectral_transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ function spectral_transform_for_full_grid(S::SpectralTransform{NF}) where NF
FullGrid = full_grid(S.Grid) # corresponding full grid

# unpack everything that does not have to be recomputed for the full grid
@unpack nresolution, lmax, mmax, nfreq_max, nlon_max, nlat, nlat_half = S
@unpack colat, cos_colat, sin_colat, norm_sphere, recompute_legendre, Λ, Λs = S
@unpack ϵlms, grad_x, grad_y1, grad_y2, grad_y_vordiv1, grad_y_vordiv2 = S
@unpack vordiv_to_uv_x, vordiv_to_uv1, vordiv_to_uv2, eigenvalues, eigenvalues⁻¹ = S
(; nresolution, lmax, mmax, nfreq_max, nlon_max, nlat, nlat_half ) = S
(; colat, cos_colat, sin_colat, norm_sphere, recompute_legendre, Λ, Λs ) = S
(; ϵlms, grad_x, grad_y1, grad_y2, grad_y_vordiv1, grad_y_vordiv2 ) = S
(; vordiv_to_uv_x, vordiv_to_uv1, vordiv_to_uv2, eigenvalues, eigenvalues⁻¹ ) = S

# recalculate what changes on the full grid: FFT and offsets (always 1)
nlons = [get_nlon_per_ring(FullGrid,nlat_half,j) for j in 1:nlat_half]
Expand Down Expand Up @@ -343,10 +343,10 @@ function gridded!( map::AbstractGrid{NF}, # gridded output
unscale_coslat::Bool=false # unscale with cos(lat) on the fly?
) where {NF<:AbstractFloat} # number format NF

@unpack nlat, nlons, nlat_half, nfreq_max = S
@unpack cos_colat, sin_colat, lon_offsets = S
@unpack recompute_legendre, Λ, Λs, m_truncs = S
@unpack brfft_plans = S
(; nlat, nlons, nlat_half, nfreq_max ) = S
(; cos_colat, sin_colat, lon_offsets ) = S
(; recompute_legendre, Λ, Λs, m_truncs ) = S
(; brfft_plans ) = S

recompute_legendre && @boundscheck size(alms) == size(Λ) || throw(BoundsError)
recompute_legendre || @boundscheck size(alms) == size(Λs[1]) || throw(BoundsError)
Expand Down Expand Up @@ -451,9 +451,9 @@ function spectral!( alms::LowerTriangularMatrix{Complex{NF}}, # output: spectr
S::SpectralTransform{NF}
) where {NF<:AbstractFloat}

@unpack nlat, nlat_half, nlons, nfreq_max, cos_colat = S
@unpack recompute_legendre, Λ, Λs, solid_angles = S
@unpack rfft_plans, lon_offsets, m_truncs = S
(; nlat, nlat_half, nlons, nfreq_max, cos_colat ) = S
(; recompute_legendre, Λ, Λs, solid_angles ) = S
(; rfft_plans, lon_offsets, m_truncs ) = S

recompute_legendre && @boundscheck size(alms) == size(Λ) || throw(BoundsError)
recompute_legendre || @boundscheck size(alms) == size(Λs[1]) || throw(BoundsError)
Expand Down
2 changes: 1 addition & 1 deletion src/SpeedyWeather.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SpeedyWeather

# STRUCTURE
import Parameters: @unpack
using DocStringExtensions

# NUMERICS
import Random
Expand Down
20 changes: 10 additions & 10 deletions src/dynamics/boundaries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ end
struct NoOrography <: AbstractOrography end

function Base.zeros(::Type{Orography},S::SpectralTransform{NF}) where NF
@unpack Grid, nlat_half, lmax, mmax = S
(;Grid, nlat_half, lmax, mmax) = S
orography = zeros(Grid{NF},nlat_half)
geopot_surf = zeros(LowerTriangularMatrix{Complex{NF}},lmax+2,mmax+1)
return Orography(orography,geopot_surf)
Expand Down Expand Up @@ -67,10 +67,10 @@ function initialize_orography!( orog::Orography,
S::SpectralTransform,
G::Geometry) where {M<:Union{ShallowWater,PrimitiveEquation}}

@unpack orography, geopot_surf = orog
@unpack orography_path, orography_file = P
@unpack gravity = P.planet
@unpack lmax, mmax = S
(;orography, geopot_surf) = orog
(;orography_path, orography_file) = P
(;gravity) = P.planet
(;lmax, mmax) = S

# LOAD NETCDF FILE
if orography_path == ""
Expand Down Expand Up @@ -104,11 +104,11 @@ function initialize_orography!( orog::Orography,
S::SpectralTransform,
G::Geometry) where {M<:Union{ShallowWater,PrimitiveEquation}}

@unpack gravity, rotation, radius = P.planet
@unpack lmax, mmax = S
(;gravity, rotation, radius) = P.planet
(;lmax, mmax) = S

@unpack orography, geopot_surf = orog
@unpack η₀, u₀ = coefs
(;orography, geopot_surf) = orog
(;η₀, u₀) = coefs

ηᵥ = (1-η₀)*π/2 # ηᵥ-coordinate of the surface [1]
A = u₀*cos(ηᵥ)^(3/2) # amplitude [m/s]
Expand All @@ -132,7 +132,7 @@ end
function scale_orography!( orog::Orography,
P::Parameters)

@unpack orography, geopot_surf = orog
(;orography, geopot_surf) = orog
orography .*= P.orography_scale
geopot_surf .*= P.orography_scale
return nothing
Expand Down
18 changes: 9 additions & 9 deletions src/dynamics/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ Generator function for a DynamicsConstants struct.
function DynamicsConstants(P::Parameters)

# PHYSICAL CONSTANTS
@unpack R_dry, R_vapour, cₚ = P
@unpack radius, rotation,gravity = P.planet
@unpack layer_thickness = P
(;R_dry, R_vapour, cₚ) = P
(;radius, rotation,gravity) = P.planet
(;layer_thickness) = P
H₀ = layer_thickness*1000 # ShallowWater: convert from [km]s to [m]
ξ = R_dry/R_vapour # Ratio of gas constants: dry air / water vapour [1]
μ_virt_temp = (1-ξ)/ξ # used in Tv = T(1+μq), for conversion from humidity q
# and temperature T to virtual temperature Tv
κ = R_dry/cₚ # = 2/7ish for diatomic gas

# TIME INTEGRATION CONSTANTS
@unpack robert_filter, williams_filter = P
@unpack trunc, Δt_at_T31, n_days, output_dt = P
(;robert_filter, williams_filter) = P
(;trunc, Δt_at_T31, n_days, output_dt) = P

# PARAMETRIZATION CONSTANTS
@unpack RH_thresh_pbl_lsc, RH_thresh_range_lsc, RH_thresh_max_lsc, humid_relax_time_lsc = P # Large-scale condensation
@unpack pres_thresh_cnv, RH_thresh_pbl_cnv, RH_thresh_trop_cnv, humid_relax_time_cnv,
max_entrainment, ratio_secondary_mass_flux = P # Convection
(;RH_thresh_pbl_lsc, RH_thresh_range_lsc, RH_thresh_max_lsc, humid_relax_time_lsc) = P # Large-scale condensation
(;pres_thresh_cnv, RH_thresh_pbl_cnv, RH_thresh_trop_cnv, humid_relax_time_cnv,
max_entrainment, ratio_secondary_mass_flux) = P # Convection

Δt = round(60*Δt_at_T31*(32/(trunc+1)))# scale time step Δt to specified resolution, [min] → [s]
Δt_sec = convert(Int,Δt) # encode time step Δt [s] as integer
Expand All @@ -82,7 +82,7 @@ function DynamicsConstants(P::Parameters)
# drag_strat = 1/(P.diffusion.time_scale_stratosphere*3600)

# interface relaxation forcing
@unpack interface_relax_time = P
(;interface_relax_time) = P
interface_relax_time *= 3600/radius # convert from hours to seconds

# SCALING
Expand Down
12 changes: 6 additions & 6 deletions src/dynamics/define_diffusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ function HorizontalDiffusion( scheme::HyperDiffusion,
C::DynamicsConstants,
G::Geometry,
S::SpectralTransform{NF}) where NF
@unpack lmax,mmax = S
@unpack radius = P.planet
@unpack power, time_scale, resolution_scaling = scheme
@unpack power_stratosphere, tapering_σ = scheme
@unpack Δt = C
@unpack nlev, σ_levels_full = G
(;lmax,mmax) = S
(;radius) = P.planet
(;power, time_scale, resolution_scaling )= scheme
(;power_stratosphere, tapering_σ) = scheme
(;Δt) = C
(;nlev, σ_levels_full) = G

# Reduce diffusion time scale (=increase diffusion) with resolution
# times 1/radius because time step Δt is scaled with 1/radius
Expand Down
18 changes: 9 additions & 9 deletions src/dynamics/diagnostic_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function Base.zeros(::Type{Tendencies},
G::Geometry{NF},
S::SpectralTransform{NF}) where NF

@unpack Grid, nlat_half = G
@unpack lmax, mmax = S
(;Grid, nlat_half) = G
(;lmax, mmax) = S
LTM = LowerTriangularMatrix

# use one more l for size compat with vector quantities
Expand Down Expand Up @@ -57,7 +57,7 @@ end

function Base.zeros(::Type{GridVariables},G::Geometry{NF}) where NF

@unpack Grid, nlat_half = G
(;Grid, nlat_half) = G
vor_grid = zeros(Grid{NF},nlat_half) # vorticity
div_grid = zeros(Grid{NF},nlat_half) # divergence
temp_grid = zeros(Grid{NF},nlat_half) # absolute temperature
Expand Down Expand Up @@ -103,8 +103,8 @@ function Base.zeros(::Type{DynamicsVariables},
G::Geometry{NF},
S::SpectralTransform{NF}) where NF

@unpack lmax, mmax = S
@unpack Grid, nlat_half = G
(;lmax, mmax) = S
(;Grid, nlat_half) = G

# MULTI-PURPOSE VECTOR (a,b), work array to be reused in various places
a = zeros(LowerTriangularMatrix{Complex{NF}},lmax+2,mmax+1)
Expand Down Expand Up @@ -141,7 +141,7 @@ function Base.zeros(::Type{DiagnosticVariablesLayer},
S::SpectralTransform{NF};
k::Integer=0) where NF # use k=0 (i.e. unspecified) as default

@unpack npoints = G
(;npoints) = G

tendencies = zeros(Tendencies,G,S)
grid_variables = zeros(GridVariables,G)
Expand Down Expand Up @@ -173,8 +173,8 @@ function Base.zeros(::Type{SurfaceVariables},
G::Geometry{NF},
S::SpectralTransform{NF}) where NF

@unpack Grid, nlat_half, npoints = G
@unpack lmax, mmax = S
(;Grid, nlat_half, npoints) = G
(;lmax, mmax) = S

# log of surface pressure and tendency thereof
pres_grid = zeros(Grid{NF},nlat_half)
Expand Down Expand Up @@ -219,7 +219,7 @@ function Base.zeros(::Type{DiagnosticVariables},
G::Geometry{NF},
S::SpectralTransform{NF}) where NF

@unpack nlev, npoints, n_stratosphere_levels = G
(;nlev, npoints, n_stratosphere_levels) = G
layers = [zeros(DiagnosticVariablesLayer,G,S;k) for k in 1:nlev]
surface = zeros(SurfaceVariables,G,S)

Expand Down
18 changes: 9 additions & 9 deletions src/dynamics/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ Generator function to create the Geometry struct from parameters in `P`.
"""
function Geometry(P::Parameters,Grid::Type{<:AbstractGrid})

@unpack trunc, dealiasing, nlev = P # grid type, spectral truncation, # of vertical levels
@unpack radius, rotation, gravity = P.planet # radius of planet, angular frequency, gravity
@unpack R_dry, cₚ = P # gas constant for dry air, heat capacity
@unpack σ_tropopause = P # number of vertical levels used for stratosphere
@unpack temp_ref, temp_top, lapse_rate = P # for reference atmosphere
@unpack ΔT_stratosphere = P # used for stratospheric temperature increase
(;trunc, dealiasing, nlev) = P # grid type, spectral truncation, # of vertical levels
(;radius, rotation, gravity) = P.planet # radius of planet, angular frequency, gravity
(;R_dry, cₚ) = P # gas constant for dry air, heat capacity
(;σ_tropopause) = P # number of vertical levels used for stratosphere
(;temp_ref, temp_top, lapse_rate) = P # for reference atmosphere
(;ΔT_stratosphere) = P # used for stratospheric temperature increase

# RESOLUTION PARAMETERS
# resolution parameter nlat_half (= number of lat rings on one hemisphere (Equator incl)
Expand Down Expand Up @@ -139,7 +139,7 @@ function Geometry(P::Parameters,Grid::Type{<:AbstractGrid})
lapserate_corr = lapserate_correction(σ_levels_full,σ_levels_half,Δp_geopot_full)

# Compute the entrainment coefficients for the convection parameterization.
@unpack max_entrainment = P
(;max_entrainment) = P
entrainment_profile = zeros(nlev)
for k = 2:nlev-1
entrainment_profile[k] = max(0, (σ_levels_full[k] - 0.5)^2)
Expand Down Expand Up @@ -173,7 +173,7 @@ The first half level is at 0 the last at 1. Evaluate a generalised logistic func
coefficients in `P` for the distribution of values in between. Default coefficients follow
the L31 configuration historically used at ECMWF."""
function vertical_coordinates(P::Parameters)
@unpack nlev,GLcoefs,σ_levels_half = P
(;nlev,GLcoefs,σ_levels_half) = P

if length(σ_levels_half) == 0 # choose σ levels automatically
z = range(0,1,nlev+1) # normalised = level/nlev
Expand All @@ -195,6 +195,6 @@ end
Generator function for a SpectralTransform struct pulling in parameters from a Parameters struct."""
function SpeedyTransforms.SpectralTransform(P::Parameters)
@unpack NF, Grid, trunc, dealiasing, recompute_legendre, legendre_shortcut = P
(;NF, Grid, trunc, dealiasing, recompute_legendre, legendre_shortcut) = P
return SpectralTransform(NF,Grid,trunc,recompute_legendre;legendre_shortcut,dealiasing)
end
Loading

0 comments on commit 1d4aec4

Please sign in to comment.