Skip to content

Commit 6bbc0fc

Browse files
Add assert_eltype helper
1 parent e743a79 commit 6bbc0fc

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/cache/precomputed_quantities.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ end
256256

257257
# Interpolates the third contravariant component of Y.c.uₕ to cell faces.
258258
function compute_ᶠuₕ³(ᶜuₕ, ᶜρ)
259+
assert_eltype(ᶜuₕ, Geometry.Covariant12Vector)
259260
ᶜJ = Fields.local_geometry_field(ᶜρ).J
260261
return @. lazy(ᶠwinterp(ᶜρ * ᶜJ, CT3(ᶜuₕ)))
261262
end
@@ -281,10 +282,19 @@ function set_velocity_at_surface!(Y, ᶠuₕ³, turbconv_model)
281282
end
282283

283284
function surface_velocity(ᶠu₃, ᶠuₕ³)
285+
assert_eltype(ᶠu₃, Geometry.Covariant3Vector)
286+
assert_eltype(ᶠuₕ³, Geometry.Contravariant3Vector)
284287
sfc_u₃ = Fields.level(ᶠu₃.components.data.:1, half)
285288
sfc_uₕ³ = Fields.level(ᶠuₕ³.components.data.:1, half)
286289
sfc_g³³ = g³³_field(sfc_u₃)
287-
return @. lazy(-sfc_uₕ³ / sfc_g³³) # u³ = uₕ³ + w³ = uₕ³ + w₃ * g³³
290+
w₃ = @. lazy(-sfc_uₕ³ / sfc_g³³) # u³ = uₕ³ + w³ = uₕ³ + w₃ * g³³
291+
292+
# sfc_u₃ = Fields.level(ᶠu₃, half)
293+
# sfc_uₕ³ = Fields.level(ᶠuₕ³, half)
294+
# w₃ = @. lazy(-sfc_uₕ³ / sfc_u₃) # are metric terms automatically applied here?
295+
296+
assert_eltype(w₃, Geometry.Covariant3Vector)
297+
return w₃
288298
end
289299

290300
"""
@@ -313,6 +323,9 @@ end
313323
# This is used to set the grid-scale velocity quantities ᶜu, ᶠu³, ᶜK based on
314324
# ᶠu₃, and it is also used to set the SGS quantities based on ᶠu₃⁰ and ᶠu₃ʲ.
315325
function set_velocity_quantities!(ᶜu, ᶠu³, ᶜK, ᶠu₃, ᶜuₕ, ᶠuₕ³)
326+
assert_eltype(ᶠu₃, Geometry.Covariant3Vector)
327+
assert_eltype(ᶠuₕ³, Geometry.Contravariant3Vector)
328+
assert_eltype(ᶠu³, Geometry.Contravariant3Vector)
316329
@. ᶜu = C123(ᶜuₕ) + ᶜinterp(C123(ᶠu₃))
317330
@. ᶠu³ = ᶠuₕ³ + CT3(ᶠu₃)
318331
ᶜK .= compute_kinetic(ᶜuₕ, ᶠu₃)

src/utils/utilities.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,24 @@ function issphere(space)
493493
return Meshes.domain(Spaces.topology(Spaces.horizontal_space(space))) isa
494494
Domains.SphereDomain
495495
end
496+
497+
"""
498+
assert_eltype(x, ::Type{T}) where {T}
499+
500+
Assert that the eltype of `x` is of type `T`
501+
"""
502+
function assert_eltype end
503+
504+
assert_eltype(bc::Base.AbstractBroadcasted, ::Type{T}) where {T} =
505+
assert_eltype(eltype(bc), T)
506+
assert_eltype(f::Fields.Field, ::Type{T}) where {T} =
507+
assert_eltype(Fields.field_values(f), T)
508+
assert_eltype(data::DataLayouts.AbstractData, ::Type{T}) where {T} =
509+
assert_eltype(eltype(data), T)
510+
511+
function assert_eltype(::Type{S}, ::Type{T}) where {S, T}
512+
if !(S <: T)
513+
error("Type $S should be a subtype of $T")
514+
end
515+
return nothing
516+
end

0 commit comments

Comments
 (0)