Skip to content

Commit 1dfc6fd

Browse files
Split Bretherton coefficients off from transforms
1 parent 6dfa7c6 commit 1dfc6fd

File tree

2 files changed

+71
-57
lines changed

2 files changed

+71
-57
lines changed

examples/hybrid/plane/inertial_gravity_wave.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ function linear_solution_cache(ᶜlocal_geometry, ᶠlocal_geometry)
280280
ᶠz = ᶠlocal_geometry.coordinates.z
281281
ρfb_init_array_params = ρfb_init_coefs_params()
282282
@time "ρfb_init_coefs!" IGWU.ρfb_init_coefs!(FT, ρfb_init_array_params)
283-
(; ρfb_init_array) = ρfb_init_array_params
283+
(; ρfb_init_array, ᶜρb_init_xz, unit_integral) = ρfb_init_array_params
284+
max_ikx, max_ikz = (size(ρfb_init_array) .- 1) 2
284285
ᶜp₀ = @. p₀(ᶜz)
285286
return (;
286287
# globals
@@ -319,10 +320,10 @@ function linear_solution_cache(ᶜlocal_geometry, ᶠlocal_geometry)
319320

320321
# Fourier coefficients of Bretherton transform of initial perturbation
321322
ρfb_init_array,
322-
323-
# Fourier transform factors
324-
ᶜfourier_factor = Fields.Field(Complex{FT}, axes(ᶜlocal_geometry)),
325-
ᶠfourier_factor = Fields.Field(Complex{FT}, axes(ᶠlocal_geometry)),
323+
unit_integral,
324+
ᶜρb_init_xz,
325+
max_ikx,
326+
max_ikz,
326327

327328
# Bretherton transform of final perturbation
328329
ᶜpb = Fields.Field(FT, axes(ᶜlocal_geometry)),

examples/hybrid/plane/intertial_gravity_wave_utils.jl

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -48,66 +48,33 @@ end
4848

4949
function Bretherton_transforms_original!(lin_cache, t, ::Type{FT}) where {FT}
5050
(; ᶜx, ᶠx, ᶜz, ᶠz) = lin_cache
51-
(; x_max, z_max, u₀, δ, cₛ², grav, f, ρₛ) = lin_cache
52-
(; ρfb_init_array, ᶜfourier_factor, ᶠfourier_factor) = lin_cache
51+
(; x_max, z_max, u₀) = lin_cache
52+
(; max_ikx, max_ikz) = lin_cache
5353
(; ᶜpb, ᶜρb, ᶜub, ᶜvb, ᶠwb) = lin_cache
5454

5555
ᶜpb .= FT(0)
5656
ᶜρb .= FT(0)
5757
ᶜub .= FT(0)
5858
ᶜvb .= FT(0)
5959
ᶠwb .= FT(0)
60-
max_ikx, max_ikz = (size(ρfb_init_array) .- 1) 2
61-
@inbounds for ikx in (-max_ikx):max_ikx, ikz in (-max_ikz):max_ikz
62-
kx = 2 * π / x_max * ikx
63-
kz = 2 * π / (2 * z_max) * ikz
64-
65-
# Fourier coefficient of ᶜρb_init (for current kx and kz)
66-
ρfb_init = ρfb_init_array[ikx + max_ikx + 1, ikz + max_ikz + 1]
67-
68-
# Fourier factors, shifted by u₀ * t along the x-axis
69-
@. ᶜfourier_factor = exp(im * (kx * (ᶜx - u₀ * t) + kz * ᶜz))
70-
@. ᶠfourier_factor = exp(im * (kx * (ᶠx - u₀ * t) + kz * ᶠz))
71-
72-
# roots of a₁(s)
73-
p₁ = cₛ² * (kx^2 + kz^2 + δ^2 / 4) + f^2
74-
q₁ = grav * kx^2 * (cₛ² * δ - grav) + cₛ² * f^2 * (kz^2 + δ^2 / 4)
75-
α² = p₁ / 2 - sqrt(p₁^2 / 4 - q₁)
76-
β² = p₁ / 2 + sqrt(p₁^2 / 4 - q₁)
77-
α = sqrt(α²)
78-
β = sqrt(β²)
79-
80-
# inverse Laplace transform of s^p/((s^2 + α^2)(s^2 + β^2)) for p ∈ -1:3
81-
if α == 0
82-
L₋₁ = (β² * t^2 / 2 - 1 + cos* t)) / β^4
83-
L₀ =* t - sin* t)) / β^3
84-
else
85-
L₋₁ =
86-
(-cos* t) / α² + cos* t) / β²) / (β² - α²) + 1 / (α² * β²)
87-
L₀ = (sin* t) / α - sin* t) / β) / (β² - α²)
60+
61+
@inbounds begin
62+
for ikx in (-max_ikx):max_ikx
63+
for ikz in (-max_ikz):max_ikz
64+
(; pfb, ρfb, ufb, vfb, wfb) =
65+
Bretherton_transform_coeffs(lin_cache, ikx, ikz, t, FT)
66+
# Fourier coefficient of ᶜρb_init (for current kx and kz)
67+
kx::FT = 2 * π / x_max * ikx
68+
kz::FT = 2 * π / (2 * z_max) * ikz
69+
70+
# Fourier factors, shifted by u₀ * t along the x-axis
71+
@. ᶜpb += real(pfb * exp(im * (kx * (ᶜx - u₀ * t) + kz * ᶜz)))
72+
@. ᶜρb += real(ρfb * exp(im * (kx * (ᶜx - u₀ * t) + kz * ᶜz)))
73+
@. ᶜub += real(ufb * exp(im * (kx * (ᶜx - u₀ * t) + kz * ᶜz)))
74+
@. ᶜvb += real(vfb * exp(im * (kx * (ᶜx - u₀ * t) + kz * ᶜz)))
75+
@. ᶠwb += real(wfb * exp(im * (kx * (ᶠx - u₀ * t) + kz * ᶠz)))
76+
end
8877
end
89-
L₁ = (cos* t) - cos* t)) / (β² - α²)
90-
L₂ = (-sin* t) * α + sin* t) * β) / (β² - α²)
91-
L₃ = (-cos* t) * α² + cos* t) * β²) / (β² - α²)
92-
93-
# Fourier coefficients of Bretherton transforms of final perturbations
94-
C₁ = grav * (grav - cₛ² * (im * kz + δ / 2))
95-
C₂ = grav * (im * kz - δ / 2)
96-
pfb = -ρfb_init * (L₁ + L₋₁ * f^2) * C₁
97-
ρfb =
98-
ρfb_init *
99-
(L₃ + L₁ * (p₁ + C₂) + L₋₁ * f^2 * (cₛ² * (kz^2 + δ^2 / 4) + C₂))
100-
ufb = ρfb_init * L₀ * im * kx * C₁ / ρₛ
101-
vfb = -ρfb_init * L₋₁ * im * kx * f * C₁ / ρₛ
102-
wfb = -ρfb_init * (L₂ + L₀ * (f^2 + cₛ² * kx^2)) * grav / ρₛ
103-
104-
# Bretherton transforms of final perturbations
105-
@. ᶜpb += real(pfb * ᶜfourier_factor)
106-
@. ᶜρb += real(ρfb * ᶜfourier_factor)
107-
@. ᶜub += real(ufb * ᶜfourier_factor)
108-
@. ᶜvb += real(vfb * ᶜfourier_factor)
109-
@. ᶠwb += real(wfb * ᶠfourier_factor)
110-
# The imaginary components should be 0 (or at least very close to 0).
11178
end
11279
return nothing
11380
end
@@ -151,4 +118,50 @@ function linear_solution!(Y, lin_cache, t, ::Type{FT}) where {FT}
151118
return nothing
152119
end
153120

121+
function Bretherton_transform_coeffs(args, ikx, ikz, t, ::Type{FT}) where {FT}
122+
(; ᶜρb_init_xz, unit_integral, ρfb_init_array) = args
123+
(; max_ikx, max_ikz) = args
124+
(; x_max, z_max, u₀, δ, cₛ², grav, f, ρₛ) = args
125+
126+
# Fourier coefficient of ᶜρb_init (for current kx and kz)
127+
kx::FT = 2 * π / x_max * ikx
128+
kz::FT = 2 * π / (2 * z_max) * ikz
129+
130+
ρfb_init = ρfb_init_array[ikx + max_ikx + 1, ikz + max_ikz + 1]
131+
132+
# roots of a₁(s)
133+
p₁ = cₛ² * (kx^2 + kz^2 + δ^2 / 4) + f^2
134+
q₁ = grav * kx^2 * (cₛ² * δ - grav) + cₛ² * f^2 * (kz^2 + δ^2 / 4)
135+
α² = p₁ / 2 - sqrt(p₁^2 / 4 - q₁)
136+
β² = p₁ / 2 + sqrt(p₁^2 / 4 - q₁)
137+
α = sqrt(α²)
138+
β = sqrt(β²)
139+
140+
# inverse Laplace transform of s^p/((s^2 + α^2)(s^2 + β^2)) for p ∈ -1:3
141+
if α == 0
142+
L₋₁ = (β² * t^2 / 2 - 1 + cos* t)) / β^4
143+
L₀ =* t - sin* t)) / β^3
144+
else
145+
L₋₁ = (-cos* t) / α² + cos* t) / β²) / (β² - α²) + 1 / (α² * β²)
146+
L₀ = (sin* t) / α - sin* t) / β) / (β² - α²)
147+
end
148+
L₁ = (cos* t) - cos* t)) / (β² - α²)
149+
L₂ = (-sin* t) * α + sin* t) * β) / (β² - α²)
150+
L₃ = (-cos* t) * α² + cos* t) * β²) / (β² - α²)
151+
152+
# Fourier coefficients of Bretherton transforms of final perturbations
153+
C₁ = grav * (grav - cₛ² * (im * kz + δ / 2))
154+
C₂ = grav * (im * kz - δ / 2)
155+
pfb = -ρfb_init * (L₁ + L₋₁ * f^2) * C₁
156+
ρfb =
157+
ρfb_init *
158+
(L₃ + L₁ * (p₁ + C₂) + L₋₁ * f^2 * (cₛ² * (kz^2 + δ^2 / 4) + C₂))
159+
ufb = ρfb_init * L₀ * im * kx * C₁ / ρₛ
160+
vfb = -ρfb_init * L₋₁ * im * kx * f * C₁ / ρₛ
161+
wfb = -ρfb_init * (L₂ + L₀ * (f^2 + cₛ² * kx^2)) * grav / ρₛ
162+
163+
# Bretherton transforms of final perturbations
164+
return (; pfb, ρfb, ufb, vfb, wfb)
165+
end
166+
154167
end # module

0 commit comments

Comments
 (0)