Skip to content

Commit c5c1e79

Browse files
author
Milan K
authored
Merge pull request #113 from milankl/mixedprec
Mixedprec
2 parents 5b0bd75 + a0a94c3 commit c5c1e79

17 files changed

+224
-146
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
environment:
22
matrix:
33
- julia_version: 1
4-
- julia_version: 1.2
4+
- julia_version: 1.3
55
- julia_version: nightly
66

77
platform:

.cirrus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ task:
55
env:
66
matrix:
77
- JULIA_VERSION: 1.0
8-
- JULIA_VERSION: 1.2
8+
- JULIA_VERSION: 1.3
99
- JULIA_VERSION: nightly
1010
install_script:
1111
- sh -c "$(fetch https://raw.githubusercontent.com/ararslan/CirrusCI.jl/master/bin/install.sh -o -)"

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ os:
55
- osx
66
julia:
77
- 1.0
8-
- 1.2
8+
- 1.3
99
- nightly
1010
notifications:
1111
email: false

src/Constants.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
struct Constants{T<:AbstractFloat}
1+
struct Constants{T<:AbstractFloat,Tprog<:AbstractFloat}
22

33
# RUNGE-KUTTA COEFFICIENTS 3rd/4th order including timestep Δt
4-
RKaΔt::Array{T,1}
5-
RKbΔt::Array{T,1}
4+
RKaΔt::Array{Tprog,1}
5+
RKbΔt::Array{Tprog,1}
66

77
# BOUNDARY CONDITIONS
8-
one_minus_α::T # tangential boundary condition for the ghost-point copy
8+
one_minus_α::Tprog # tangential boundary condition for the ghost-point copy
99

1010
# PHYSICAL CONSTANTS
1111
g::T # gravity
@@ -17,23 +17,23 @@ struct Constants{T<:AbstractFloat}
1717
rSST::T # tracer restoring timescale
1818
jSST::T # tracer consumption timescale
1919
SSTmin::T # tracer minimum
20-
ωyr::Float64 # # frequency [1/s] of Kelvin pumping (including 2π)
20+
ωyr::Float64 # frequency [1/s] of Kelvin pumping (including 2π)
2121
end
2222

2323
"""Generator function for the mutable struct Constants."""
24-
function Constants{T}(P::Parameter,G::Grid) where {T<:AbstractFloat}
24+
function Constants{T,Tprog}(P::Parameter,G::Grid) where {T<:AbstractFloat,Tprog<:AbstractFloat}
2525

2626
# Runge-Kutta 3rd/4th order coefficients including time step Δt
2727
# (which includes the grid spacing Δ too)
2828
if P.RKo == 3 # version 2
29-
RKaΔt = T.([1/4,0.,3/4]*G.dtint/G.Δ)
30-
RKbΔt = T.([1/3,2/3]*G.dtint/G.Δ)
29+
RKaΔt = Tprog.([1/4,0.,3/4]*G.dtint/G.Δ)
30+
RKbΔt = Tprog.([1/3,2/3]*G.dtint/G.Δ)
3131
elseif P.RKo == 4
32-
RKaΔt = T.([1/6,1/3,1/3,1/6]*G.dtint/G.Δ)
33-
RKbΔt = T.([.5,.5,1.]*G.dtint/G.Δ)
32+
RKaΔt = Tprog.([1/6,1/3,1/3,1/6]*G.dtint/G.Δ)
33+
RKbΔt = Tprog.([.5,.5,1.]*G.dtint/G.Δ)
3434
end
3535

36-
one_minus_α = T(1-P.α) # for the ghost point copy/tangential boundary conditions
36+
one_minus_α = Tprog(1-P.α) # for the ghost point copy/tangential boundary conditions
3737
g = T(P.g) # gravity - for Bernoulli potential
3838

3939
# BOTTOM FRICTION COEFFICENTS
@@ -57,5 +57,5 @@ function Constants{T}(P::Parameter,G::Grid) where {T<:AbstractFloat}
5757
# SURFACE FORCING
5858
ωyr = -2π*P.ωyr/24/365/3600
5959

60-
return Constants{T}(RKaΔt,RKbΔt,one_minus_α,g,cD,rD,γ,cSmag,νB,rSST,jSST,SSTmin,ωyr)
60+
return Constants{T,Tprog}(RKaΔt,RKbΔt,one_minus_α,g,cD,rD,γ,cSmag,νB,rSST,jSST,SSTmin,ωyr)
6161
end

src/Continuity.jl

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Continuity equation's right-hand side with surface relaxation
22
-∂x(uh) - ∂y(vh) + γ*(η_ref - η)."""
3-
function continuity_surf_relax!::AbstractMatrix,
4-
Diag::DiagnosticVars,
5-
S::ModelSetup,
6-
t::Int) where {T<:AbstractFloat}
3+
function continuity_surf_relax!::Array{T,2},
4+
Diag::DiagnosticVars{T,Tprog},
5+
S::ModelSetup{T,Tprog},
6+
t::Int) where {T,Tprog}
77

88
@unpack= Diag.Tendencies
99
@unpack dUdx,dVdy = Diag.VolumeFluxes
@@ -18,17 +18,15 @@ function continuity_surf_relax!(η::AbstractMatrix,
1818

1919
@inbounds for j 1:n
2020
for i 1:m
21-
dη[i+1,j+1] = -(dUdx[i,j+1] + dVdy[i+1,j]) + γ*(η_ref[i,j]-η[i+1,j+1])
21+
dη[i+1,j+1] = -(Tprog(dUdx[i,j+1]) + Tprog(dVdy[i+1,j])) + Tprog(γ*(η_ref[i,j]-η[i+1,j+1]))
2222
end
2323
end
2424
end
2525

2626
"""Continuity equation's right-hand side with time&space dependent forcing."""
27-
function continuity_forcing!( ::Type{T},
28-
η::AbstractMatrix,
29-
Diag::DiagnosticVars,
30-
S::ModelSetup,
31-
t::Int) where {T<:AbstractFloat}
27+
function continuity_forcing!( Diag::DiagnosticVars{T,Tprog},
28+
S::ModelSetup{T,Tprog},
29+
t::Int) where {T,Tprog}
3230

3331
@unpack= Diag.Tendencies
3432
@unpack dUdx,dVdy = Diag.VolumeFluxes
@@ -45,17 +43,15 @@ function continuity_forcing!( ::Type{T},
4543

4644
@inbounds for j 1:n
4745
for i 1:m
48-
dη[i+1,j+1] = -(dUdx[i,j+1] + dVdy[i+1,j]) + Fηtt*Fη[i,j]
46+
dη[i+1,j+1] = -(Tprog(dUdx[i,j+1]) + Tprog(dVdy[i+1,j])) + Tprog(Fηtt*Fη[i,j])
4947
end
5048
end
5149
end
5250

5351
"""Continuity equation's right-hand side -∂x(uh) - ∂y(vh) without forcing."""
54-
function continuity_itself!(::Type{T},
55-
η::AbstractMatrix,
56-
Diag::DiagnosticVars,
57-
S::ModelSetup,
58-
t::Int) where {T<:AbstractFloat}
52+
function continuity_itself!(Diag::DiagnosticVars{T,Tprog},
53+
S::ModelSetup{T,Tprog},
54+
t::Int) where {T,Tprog}
5955

6056
@unpack= Diag.Tendencies
6157
@unpack dUdx,dVdy = Diag.VolumeFluxes
@@ -66,23 +62,23 @@ function continuity_itself!(::Type{T},
6662

6763
@inbounds for j 1:n
6864
for i 1:m
69-
dη[i+1,j+1] = -(dUdx[i,j+1] + dVdy[i+1,j])
65+
dη[i+1,j+1] = -(Tprog(dUdx[i,j+1]) + Tprog(dVdy[i+1,j]))
7066
end
7167
end
7268
end
7369

7470

7571
"""Transit function to call the specified continuity function."""
76-
function continuity!( η::Array{T,2},
72+
function continuity!( η::AbstractMatrix,
7773
Diag::DiagnosticVars,
7874
S::ModelSetup,
79-
t::Int) where {T<:AbstractFloat}
75+
t::Int)
8076

8177
if S.parameters.surface_relax
82-
continuity_surf_relax!(T,η,Diag,S,t)
78+
continuity_surf_relax!(η,Diag,S,t)
8379
elseif S.parameters.surface_forcing
84-
continuity_forcing!(T,η,Diag,S,t)
80+
continuity_forcing!(Diag,S,t)
8581
else
86-
continuity_itself!(T,η,Diag,S,t)
82+
continuity_itself!(Diag,S,t)
8783
end
8884
end

src/DefaultParameters.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
T::DataType=Float32 # number format
44

5+
Tprog::DataType=T # number format for prognostic variables
6+
Tcomm::DataType=T # number format for ghost-point copies
7+
58
# DOMAIN RESOLUTION AND RATIO
69
nx::Int=100 # number of grid cells in x-direction
710
Lx::Real=2000e3 # length of the domain in x-direction [m]
@@ -30,7 +33,7 @@
3033
surface_relax::Bool=false # yes?
3134
t_relax::Real=100. # time scale of the relaxation [days]
3235
η_refh::Real=5. # height difference [m] of the interface relaxation profile
33-
η_refw::Real=50e3 # width [m] of the tangent used for the interface relaxation
36+
η_refw::Real=50e3 # width [m] of the tangent used for the interface relaxation
3437

3538
# SURFACE FORCING (Currently only Kelvin wave pumping at Eq.)
3639
surface_forcing::Bool=false # yes?
@@ -73,11 +76,11 @@
7376
sst_initial::String="south" # "west", "south", "rect", "flat" or "restart"
7477
sst_rect_coords::Array{Float64,1}=[0.,0.15,0.,1.0]
7578
# (x0,x1,y0,y1) are the size of the rectangle in [0,1]
76-
Uadv::Real=0.15 # Velocity scale [m/s] for tracer advection
79+
Uadv::Real=0.25 # Velocity scale [m/s] for tracer advection
7780
SSTmax::Real=1. # tracer (sea surface temperature) max for restoring
7881
SSTmin::Real=0. # tracer (sea surface temperature) min for restoring
7982
τSST::Real=500. # tracer restoring time scale [days]
80-
jSST::Real=50*365. # tracer consumption [days]
83+
jSST::Real=365. # tracer consumption [days]
8184
SST_λ0::Real=222e3 # [m] transition position of relaxation timescale
8285
SST_λs::Real=111e3 # [m] transition width of relaxation timescale
8386
SST_γ0::Real=8.35 # [days] injection time scale

src/Diffusion.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ function viscous_tensor_constant!( Diag::DiagnosticVars,
218218
end
219219

220220
"""Update u with bottom friction tendency (Bu,Bv) and biharmonic viscosity."""
221-
function add_drag_diff_tendencies!( u::AbstractMatrix,
222-
v::AbstractMatrix,
223-
Diag::DiagnosticVars,
224-
S::ModelSetup)
221+
function add_drag_diff_tendencies!( u::Array{Tprog,2},
222+
v::Array{Tprog,2},
223+
Diag::DiagnosticVars{T,Tprog},
224+
S::ModelSetup{T,Tprog}) where {T,Tprog}
225225

226226
@unpack Bu,Bv = Diag.Bottomdrag
227227
@unpack LLu1,LLu2,LLv1,LLv2 = Diag.Smagorinsky
@@ -234,7 +234,7 @@ function add_drag_diff_tendencies!( u::AbstractMatrix,
234234

235235
@inbounds for j 1:n
236236
for i 1:m
237-
u[i+2,j+2] += Δt_diff*(Bu[i+1-ep,j+1] + LLu1[i,j+1] + LLu2[i+1-ep,j])
237+
u[i+2,j+2] += Δt_diff*(Tprog(Bu[i+1-ep,j+1]) + Tprog(LLu1[i,j+1]) + Tprog(LLu2[i+1-ep,j]))
238238
end
239239
end
240240

@@ -245,7 +245,7 @@ function add_drag_diff_tendencies!( u::AbstractMatrix,
245245

246246
@inbounds for j 1:n
247247
for i 1:m
248-
v[i+2,j+2] += Δt_diff*(Bv[i+1,j+1] + LLv1[i,j+1] + LLv2[i+1,j])
248+
v[i+2,j+2] += Δt_diff*(Tprog(Bv[i+1,j+1]) + Tprog(LLv1[i,j+1]) + Tprog(LLv2[i+1,j]))
249249
end
250250
end
251251
end

src/Grid.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@with_kw struct Grid{T<:AbstractFloat}
1+
@with_kw struct Grid{T<:AbstractFloat,Tprog<:AbstractFloat}
22

33
# Parameters taken from Parameter struct
44
nx::Int # number of grid cells in x-direction
@@ -71,7 +71,7 @@
7171
nt::Int = Int(ceil(Ndays*3600*24/dtint)) # number of time steps to integrate
7272
dt::T = T(dtint) # time step [s]
7373
Δt::T = T(dtint/Δ) # time step divided by grid spacing [s/m]
74-
Δt_diff::T = T(nstep_diff*dtint/Δ) # time step for diffusive terms
74+
Δt_diff::Tprog = Tprog(nstep_diff*dtint/Δ) # time step for diffusive terms
7575

7676
# TIME STEPS FOR ADVECTION
7777
nadvstep::Int = max(1,Int(floor/Uadv/dtint))) # advection each n time steps
@@ -80,7 +80,7 @@
8080
dtadvu::T = T(dtadvint*nx/Lx) # Rescaled advection time step for u [s/m]
8181
dtadvv::T = T(dtadvint*ny/Ly) # Rescaled advection time step for v [s/m]
8282
half_dtadvu::T = T(dtadvint*nx/Lx/2) # dtadvu/2
83-
half_dtadvv::T = T(dtadvint*ny/Ly/2) # dtadvv/2
83+
half_dtadvv::T = T(dtadvint*ny/Ly/2) # dtadvv/2
8484

8585
# N TIME STEPS FOR OUTPUT
8686
nout::Int = Int(floor(output_dt*3600/dtint)) # output every n time steps
@@ -99,24 +99,24 @@ end
9999
function yy_q(bc::String,x_q_halo::AbstractVector,y_q_halo::AbstractVector)
100100
if bc == "periodic"
101101
# points on the right edge needed too
102-
xx_q,yy_q = meshgrid(x_q_halo[3:end-1],y_q_halo[3:end-2])
102+
_,yy_q = meshgrid(x_q_halo[3:end-1],y_q_halo[3:end-2])
103103
else
104-
xx_q,yy_q = meshgrid(x_q_halo[3:end-2],y_q_halo[3:end-2])
104+
_,yy_q = meshgrid(x_q_halo[3:end-2],y_q_halo[3:end-2])
105105
end
106106

107107
return yy_q
108108
end
109109

110110

111111
"""Generator function for the Grid struct."""
112-
function Grid{T}(P::Parameter) where {T<:AbstractFloat}
112+
function Grid{T,Tprog}(P::Parameter) where {T<:AbstractFloat,Tprog<:AbstractFloat}
113113
@unpack nx,Lx,L_ratio = P
114114
@unpack bc,g,H,cfl = P
115115
@unpack Ndays,nstep_diff,nstep_advcor = P
116116
@unpack Uadv,output_dt = P
117117
@unpack ϕ,ω,R = P
118118

119-
return Grid{T}(nx=nx,Lx=Lx,L_ratio=L_ratio,bc=bc,g=g,H=H,cfl=cfl,Ndays=Ndays,
119+
return Grid{T,Tprog}(nx=nx,Lx=Lx,L_ratio=L_ratio,bc=bc,g=g,H=H,cfl=cfl,Ndays=Ndays,
120120
nstep_diff=nstep_diff,nstep_advcor=nstep_advcor,Uadv=Uadv,output_dt=output_dt,
121121
ϕ=ϕ,ω=ω,R=R)
122122
end

src/InitialConditions.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ struct PrognosticVars{T<:AbstractFloat}
1010
sst::Array{T,2} # tracer / sea surface temperature
1111
end
1212

13+
"""Zero generator function for Grid G as argument."""
14+
function PrognosticVars{T}(G::Grid) where {T<:AbstractFloat}
15+
@unpack nux,nuy,nvx,nvy,nx,ny = G
16+
@unpack halo,haloη,halosstx,halossty = G
17+
18+
u = zeros(T,nux+2halo,nuy+2halo)
19+
v = zeros(T,nvx+2halo,nvy+2halo)
20+
η = zeros(T,nx+2haloη,ny+2haloη)
21+
sst = zeros(T,nx+2halosstx,ny+2halossty)
22+
23+
return PrognosticVars{T}(u,v,η,sst)
24+
end
25+
1326
function initial_conditions(::Type{T},S::ModelSetup) where {T<:AbstractFloat}
1427

1528
## PROGNOSTIC VARIABLES U,V,η
@@ -49,7 +62,7 @@ function initial_conditions(::Type{T},S::ModelSetup) where {T<:AbstractFloat}
4962
NetCDF.close(ncη)
5063

5164
# remove singleton time dimension
52-
# and convert from Float32 to Numtype
65+
# and convert from Float32 to type T
5366
u = T.(reshape(u,size(u)[1:2]))
5467
v = T.(reshape(v,size(v)[1:2]))
5568
η = T.(reshape(η,size(η)[1:2]))

src/ModelSetup.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
struct ModelSetup{T<:AbstractFloat}
1+
struct ModelSetup{T<:AbstractFloat,Tprog<:AbstractFloat}
22
parameters::Parameter
3-
grid::Grid{T}
4-
constants::Constants{T}
3+
grid::Grid{T,Tprog}
4+
constants::Constants{T,Tprog}
55
forcing::Forcing{T}
66
end

0 commit comments

Comments
 (0)