Skip to content

Commit e15ef87

Browse files
authored
Add default Ω_Earth and R_Earth for FPlane and BetaPlane (#550)
Add default Ω_Earth and R_Earth for FPlane and BetaPlane
2 parents 93c7a96 + 938eb44 commit e15ef87

File tree

2 files changed

+63
-50
lines changed

2 files changed

+63
-50
lines changed

src/coriolis.jl

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
using Oceananigans.Operators: ℑxyᶜᶠᵃ, ℑxyᶠᶜᵃ
22

3+
#####
4+
##### Physical constants
5+
#####
6+
7+
const Ω_Earth = 7.292115e-5 # [s⁻¹] https://en.wikipedia.org/wiki/Earth%27s_rotation#Angular_speed
8+
const R_Earth = 6371.0e3 # Mean radius of the Earth [m] https://en.wikipedia.org/wiki/Earth
9+
310
#####
411
##### Functions for non-rotating models
512
#####
@@ -23,26 +30,32 @@ struct FPlane{FT} <: AbstractRotation
2330
end
2431

2532
"""
26-
FPlane([FT=Float64;] f=nothing, rotation_rate=nothing, latitude=nothing)
33+
FPlane([FT=Float64;] f=nothing, rotation_rate=Ω_Earth, latitude=nothing)
2734
2835
Returns a parameter object for constant rotation at the angular frequency
2936
`f/2`, and therefore with background vorticity `f`, around a vertical axis.
3037
If `f` is not specified, it is calculated from `rotation_rate` and
3138
`latitude` according to the relation `f = 2*rotation_rate*sind(latitude).
3239
40+
By default, `rotation_rate` is assumed to be Earth's.
41+
3342
Also called `FPlane`, after the "f-plane" approximation for the local effect of
34-
Earth's rotation in a planar coordinate system tangent to the Earth's surface.
43+
a planet's rotation in a planar coordinate system tangent to the planet's surface.
3544
"""
36-
function FPlane(FT::DataType=Float64; f=nothing, rotation_rate=nothing, latitude=nothing)
45+
function FPlane(FT::DataType=Float64; f=nothing, rotation_rate=Ω_Earth, latitude=nothing)
3746

38-
if f == nothing && rotation_rate != nothing && latitude != nothing
39-
return FPlane{FT}(2rotation_rate*sind(latitude))
40-
elseif f != nothing && rotation_rate == nothing && latitude == nothing
47+
use_f = !isnothing(f)
48+
use_planet_parameters = !isnothing(latitude)
49+
50+
if !xor(use_f, use_planet_parameters)
51+
throw(ArgumentError("Either both keywords rotation_rate and latitude must be " *
52+
"specified, *or* only f must be specified."))
53+
end
54+
55+
if use_f
4156
return FPlane{FT}(f)
42-
else
43-
throw(ArgumentError("Either both keywords rotation_rate and
44-
latitude must be specified, *or* only f
45-
must be specified."))
57+
elseif use_planet_parameters
58+
return FPlane{FT}(2rotation_rate*sind(latitude))
4659
end
4760
end
4861

@@ -57,8 +70,7 @@ end
5770
"""
5871
BetaPlane{T} <: AbstractRotation
5972
60-
A parameter object for meridionally increasing Coriolis
61-
parameter (`f = f₀ + βy`).
73+
A parameter object for meridionally increasing Coriolis parameter (`f = f₀ + βy`).
6274
"""
6375
struct BetaPlane{T} <: AbstractRotation
6476
f₀ :: T
@@ -67,26 +79,28 @@ end
6779

6880
"""
6981
BetaPlane([T=Float64;] f₀=nothing, β=nothing,
70-
rotation_rate=nothing, latitude=nothing, radius=nothing)
82+
rotation_rate=Ω_Earth, latitude=nothing, radius=R_Earth)
7183
7284
A parameter object for meridionally increasing Coriolis parameter (`f = f₀ + βy`).
7385
74-
The user may specify both `f₀` and `β`, or the three parameters
75-
`rotation_rate`, `latitude`, and `radius` that specify the rotation rate and radius
76-
of a planet, and the central latitude at which the `β`-plane approximation is to be made.
86+
The user may specify both `f₀` and `β`, or the three parameters `rotation_rate`,
87+
`latitude`, and `radius` that specify the rotation rate and radius of a planet, and
88+
the central latitude at which the `β`-plane approximation is to be made.
89+
90+
By default, the `rotation_rate` and planet `radius` is assumed to be Earth's.
7791
"""
7892
function BetaPlane(T=Float64; f₀=nothing, β=nothing,
79-
rotation_rate=nothing, latitude=nothing, radius=nothing)
93+
rotation_rate=Ω_Earth, latitude=nothing, radius=R_Earth)
8094

81-
f_and_β = f₀ != nothing && β != nothing
82-
planet_parameters = rotation_rate != nothing && latitude != nothing && radius != nothing
95+
use_f_and_β = !isnothing(f₀) && !isnothing(β)
96+
use_planet_parameters = !isnothing(latitude)
8397

84-
(f_and_β && all(Tuple(p === nothing for p in (rotation_rate, latitude, radius)))) ||
85-
(planet_parameters && all(Tuple(p === nothing for p in (f₀, β)))) ||
86-
throw(ArgumentError("Either both keywords f₀ and β must be specified,
87-
*or* all of rotation_rate, latitude, and radius."))
98+
if !xor(use_f_and_β, use_planet_parameters)
99+
throw(ArgumentError("Either both keywords f₀ and β must be specified, " *
100+
"*or* all of rotation_rate, latitude, and radius."))
101+
end
88102

89-
if planet_parameters
103+
if use_planet_parameters
90104
f₀ = 2rotation_rate * sind(latitude)
91105
β = 2rotation_rate * cosd(latitude) / radius
92106
end

test/test_coriolis.jl

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
1-
function instantiate_fplane_1(T)
2-
coriolis = FPlane(T, f=π)
3-
return coriolis.f == T(π)
1+
function instantiate_fplane_1(FT)
2+
coriolis = FPlane(FT, f=π)
3+
return coriolis.f == FT(π)
44
end
55

6-
function instantiate_fplane_2(T)
7-
coriolis = FPlane(T, rotation_rate=2, latitude=30)
8-
return coriolis.f == T(2)
6+
function instantiate_fplane_2(FT)
7+
coriolis = FPlane(FT, rotation_rate=2, latitude=30)
8+
return coriolis.f == FT(2)
99
end
1010

11-
function instantiate_betaplane_1(T)
12-
coriolis = BetaPlane(T, f₀=π, β=2π)
13-
return coriolis.f₀ == T(π)
11+
function instantiate_betaplane_1(FT)
12+
coriolis = BetaPlane(FT, f₀=π, β=2π)
13+
return coriolis.f₀ == FT(π)
1414
end
1515

16-
function instantiate_betaplane_2(T)
17-
coriolis = BetaPlane(T, latitude=70, radius=2π, rotation_rate=3π)
18-
return coriolis.f₀ == T(6π * sind(70))
16+
function instantiate_betaplane_2(FT)
17+
coriolis = BetaPlane(FT, latitude=70, radius=2π, rotation_rate=3π)
18+
return coriolis.f₀ == FT(6π * sind(70))
1919
end
2020

2121
@testset "Coriolis" begin
2222
println("Testing Coriolis...")
2323

2424
@testset "Coriolis" begin
25-
for T in float_types
26-
@test instantiate_fplane_1(T)
27-
@test instantiate_fplane_2(T)
28-
@test instantiate_betaplane_1(T)
29-
@test instantiate_betaplane_2(T)
25+
for FT in float_types
26+
@test instantiate_fplane_1(FT)
27+
@test instantiate_fplane_2(FT)
28+
@test instantiate_betaplane_1(FT)
29+
@test instantiate_betaplane_2(FT)
30+
3031
# Test that FPlane throws an ArgumentError
31-
@test_throws ArgumentError FPlane(T, rotation_rate=7e-5)
32-
@test_throws ArgumentError FPlane(T, latitude=40)
33-
@test_throws ArgumentError FPlane(T, f=1, rotation_rate=7e-5)
34-
@test_throws ArgumentError FPlane(T, f=1, latitude=40)
35-
@test_throws ArgumentError FPlane(T, f=1, rotation_rate=7e-5, latitude=40)
32+
@test_throws ArgumentError FPlane(FT, rotation_rate=7e-5)
33+
@test_throws ArgumentError FPlane(FT, f=1, latitude=40)
34+
@test_throws ArgumentError FPlane(FT, f=1, rotation_rate=7e-5, latitude=40)
35+
3636
# Non-exhaustively test that BetaPlane throws an ArgumentError
37-
@test_throws ArgumentError BetaPlane(T, f₀=1)
38-
@test_throws ArgumentError BetaPlane(T, β=1)
39-
@test_throws ArgumentError BetaPlane(T, latitude=70)
40-
@test_throws ArgumentError BetaPlane(T, f₀=1e-4, β=1e-11, latitude=70)
37+
@test_throws ArgumentError BetaPlane(FT, f₀=1)
38+
@test_throws ArgumentError BetaPlane(FT, β=1)
39+
@test_throws ArgumentError BetaPlane(FT, f₀=1e-4, β=1e-11, latitude=70)
4140
end
4241
end
4342
end

0 commit comments

Comments
 (0)