Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hoist some getproperty calls inside broadcast expr #424

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/3dsphere/baroclinic_wave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function rhs!(dY, Y, _, t)
dw = dY.w
duₕ = dY.uₕ
dρe = dY.Yc.ρe
z = coords.z

# 0) update w at the bottom
# fw = -g^31 cuₕ/ g^33
Expand Down Expand Up @@ -275,7 +276,7 @@ function rhs!(dY, Y, _, t)
Geometry.Contravariant12Vector(Geometry.Covariant123Vector(cuₕ))

ce = @. cρe / cρ
cp = @. pressure(cρ, ce, norm(cuvw), coords.z)
cp = @. pressure(cρ, ce, norm(cuvw), z)

@. duₕ -= hgrad(cp) / cρ
vgradc2f = Operators.GradientC2F(
Expand All @@ -284,7 +285,7 @@ function rhs!(dY, Y, _, t)
)
@. dw -= vgradc2f(cp) / Ic2f(cρ)

cE = @. (norm(cuvw)^2) / 2 + Φ(coords.z)
cE = @. (norm(cuvw)^2) / 2 + Φ(z)
@. duₕ -= hgrad(cE)
@. dw -= vgradc2f(cE)

Expand Down
5 changes: 3 additions & 2 deletions examples/3dsphere/solid_body_rotation_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function rhs!(dY, Y, _, t)
dw = dY.w
duₕ = dY.uₕ
dρe = dY.Yc.ρe
z = c_coords.z

# # 0) update w at the bottom
# fw = -g^31 cuₕ/ g^33 ????????
Expand Down Expand Up @@ -170,7 +171,7 @@ function rhs!(dY, Y, _, t)
Geometry.Contravariant12Vector(Geometry.Covariant123Vector(cuₕ))

ce = @. cρe / cρ
cp = @. pressure(cρ, ce, norm(cuvw), c_coords.z)
cp = @. pressure(cρ, ce, norm(cuvw), z)

@. duₕ -= hgrad(cp) / cρ
vgradc2f = Operators.GradientC2F(
Expand All @@ -179,7 +180,7 @@ function rhs!(dY, Y, _, t)
)
@. dw -= vgradc2f(cp) / Ic2f(cρ)

cE = @. (norm(cuvw)^2) / 2 + Φ(c_coords.z)
cE = @. (norm(cuvw)^2) / 2 + Φ(z)
@. duₕ -= hgrad(cE)
@. dw -= vgradc2f(cE)

Expand Down
7 changes: 4 additions & 3 deletions examples/hybrid/bubble3d_invariant_totalenergy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function rhs_invariant!(dY, Y, _, t)
dw = dY.w
duₕ = dY.uₕ
dρe = dY.Yc.ρe
z = coords.z

# 0) update w at the bottom
# fw = -g^31 cuₕ/ g^33
Expand Down Expand Up @@ -219,8 +220,8 @@ function rhs_invariant!(dY, Y, _, t)
cω³ × Geometry.Contravariant12Vector(Geometry.Covariant123Vector(cuₕ))

ce = @. cρe / cρ
#cp = @. pressure(cρ, ce, cuvw, coords.z) #TODO: the pressure function doesn't work (broadcast error)
cI = @. ce - Φ(coords.z) - (norm(cuvw)^2) / 2
#cp = @. pressure(cρ, ce, cuvw, z) #TODO: the pressure function doesn't work (broadcast error)
cI = @. ce - Φ(z) - (norm(cuvw)^2) / 2
cT = @. cI / C_v + T_0
cp = @. cρ * R_d * cT

Expand All @@ -231,7 +232,7 @@ function rhs_invariant!(dY, Y, _, t)
)
@. dw -= vgradc2f(cp) / Ic2f(cρ)

cE = @. (norm(cuvw)^2) / 2 + Φ(coords.z)
cE = @. (norm(cuvw)^2) / 2 + Φ(z)
@. duₕ -= hgrad(cE)
@. dw -= vgradc2f(cE)

Expand Down
53 changes: 28 additions & 25 deletions examples/hybrid/bubble_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ function rhs!(dY, Y, _, t)
dYc = dY.Yc
dρuₕ = dY.ρuₕ
dρw = dY.ρw
ρ = Yc.ρ
ρθ = Yc.ρθ
dρθ = dYc.ρθ
dρ = dYc.ρ

# spectral horizontal operators
hdiv = Operators.Divergence()
Expand Down Expand Up @@ -201,34 +205,34 @@ function rhs!(dY, Y, _, t)
top = Operators.Extrapolate(),
)

uₕ = @. ρuₕ / Yc.ρ
w = @. ρw / If(Yc.ρ)
wc = @. Ic(ρw) / Yc.ρ
p = @. pressure(Yc.ρθ)
θ = @. Yc.ρθ / Yc.ρ
Yfρ = @. If(Yc.ρ)
uₕ = @. ρuₕ / ρ
w = @. ρw / If(ρ)
wc = @. Ic(ρw) / ρ
p = @. pressure(ρθ)
θ = @. ρθ / ρ
Yfρ = @. If(ρ)

### HYPERVISCOSITY
# 1) compute hyperviscosity coefficients
@. dYc.ρθ = hdiv(hgrad(θ))
@. dρθ = hdiv(hgrad(θ))
@. dρuₕ = hdiv(hgrad(uₕ))
@. dρw = hdiv(hgrad(w))
Spaces.weighted_dss!(dYc)
Spaces.weighted_dss!(dρuₕ)
Spaces.weighted_dss!(dρw)

κ₄ = 100.0 # m^4/s
@. dYc.ρθ = -κ₄ * hdiv(Yc.ρ * hgrad(dYc.ρθ))
@. dρuₕ = -κ₄ * hdiv(Yc.ρ * hgrad(dρuₕ))
@. dYc.ρθ = -κ₄ * hdiv(ρ * hgrad(dρθ))
@. dρuₕ = -κ₄ * hdiv(ρ * hgrad(dρuₕ))
@. dρw = -κ₄ * hdiv(Yfρ * hgrad(dρw))

# density
@. dYc.ρ = -∂(ρw)
@. dYc.ρ -= hdiv(ρuₕ)
@. = -∂(ρw)
@. -= hdiv(ρuₕ)

# potential temperature
@. dYc.ρθ += -(∂(ρw * If(Yc.ρθ / Yc.ρ)))
@. dYc.ρθ -= hdiv(uₕ * Yc.ρθ)
@. dρθ += -(∂(ρw * If(ρθ / ρ)))
@. dρθ -= hdiv(uₕ * ρθ)

# horizontal momentum
Ih = Ref(
Expand All @@ -241,41 +245,40 @@ function rhs!(dY, Y, _, t)
@. dρuₕ -= hdiv(ρuₕ ⊗ uₕ + p * Ih)

# vertical momentum
z = coords.z
@. dρw +=
B(
Geometry.transform(
Geometry.WAxis(),
-(∂f(p)) - If(Yc.ρ) * ∂f(Φ(coords.z)),
) - vvdivc2f(Ic(ρw ⊗ w)),
Geometry.transform(Geometry.WAxis(), -(∂f(p)) - If(ρ) * ∂f(Φ(z))) -
vvdivc2f(Ic(ρw ⊗ w)),
) + fcf(wc, ρw)
uₕf = @. If(ρuₕ / Yc.ρ) # requires boundary conditions
uₕf = @. If(ρuₕ / ρ) # requires boundary conditions
@. dρw -= hdiv(uₕf ⊗ ρw)

### UPWIND FLUX CORRECTION
upwind_correction = true
if upwind_correction
@. dYc.ρ += fcc(w, Yc.ρ)
@. dYc.ρθ += fcc(w, Yc.ρθ)
@. += fcc(w, ρ)
@. dρθ += fcc(w, ρθ)
@. dρuₕ += fcc(w, ρuₕ)
@. dρw += fcf(wc, ρw)
end

### DIFFUSION
κ₂ = 0.0 # m^2/s
# 1a) horizontal div of horizontal grad of horiz momentun
@. dρuₕ += hdiv(κ₂ * (Yc.ρ * hgrad(ρuₕ / Yc.ρ)))
@. dρuₕ += hdiv(κ₂ * (ρ * hgrad(ρuₕ / ρ)))
# 1b) vertical div of vertical grad of horiz momentun
@. dρuₕ += uvdivf2c(κ₂ * (Yfρ * ∂f(ρuₕ / Yc.ρ)))
@. dρuₕ += uvdivf2c(κ₂ * (Yfρ * ∂f(ρuₕ / ρ)))

# 1c) horizontal div of horizontal grad of vert momentum
@. dρw += hdiv(κ₂ * (Yfρ * hgrad(ρw / Yfρ)))
# 1d) vertical div of vertical grad of vert momentun
@. dρw += vvdivc2f(κ₂ * (Yc.ρ * ∂c(ρw / Yfρ)))
@. dρw += vvdivc2f(κ₂ * (ρ * ∂c(ρw / Yfρ)))

# 2a) horizontal div of horizontal grad of potential temperature
@. dYc.ρθ += hdiv(κ₂ * (Yc.ρ * hgrad(Yc.ρθ / Yc.ρ)))
@. dYc.ρθ += hdiv(κ₂ * (ρ * hgrad(ρθ / ρ)))
# 2b) vertical div of vertial grad of potential temperature
@. dYc.ρθ += ∂(κ₂ * (Yfρ * ∂f(Yc.ρθ / Yc.ρ)))
@. dYc.ρθ += ∂(κ₂ * (Yfρ * ∂f(ρθ / ρ)))

Spaces.weighted_dss!(dYc)
Spaces.weighted_dss!(dρuₕ)
Expand Down
61 changes: 33 additions & 28 deletions examples/hybrid/bubble_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ function rhs!(dY, Y, _, t)
Yc = Y.Yc
dYc = dY.Yc
dρw = dY.ρw
ρ = Yc.ρ
ρuₕ = Yc.ρuₕ
ρθ = Yc.ρθ
dρθ = dYc.ρθ
dρuₕ = dYc.ρuₕ
dρ = dYc.ρ

# spectral horizontal operators
hdiv = Operators.Divergence()
Expand Down Expand Up @@ -211,79 +217,78 @@ function rhs!(dY, Y, _, t)
top = Operators.Extrapolate(),
)

uₕ = @. Yc.ρuₕ / Yc.ρ
w = @. ρw / If(Yc.ρ)
wc = @. Ic(ρw) / Yc.ρ
p = @. pressure(Yc.ρθ)
θ = @. Yc.ρθ / Yc.ρ
Yfρ = @. If(Yc.ρ)
uₕ = @. ρuₕ / ρ
w = @. ρw / If(ρ)
wc = @. Ic(ρw) / ρ
p = @. pressure(ρθ)
θ = @. ρθ / ρ
Yfρ = @. If(ρ)

### HYPERVISCOSITY
# 1) compute hyperviscosity coefficients
@. dYc.ρθ = hdiv(hgrad(θ))
@. dYc.ρuₕ = hdiv(hgrad(uₕ))
@. dρθ = hdiv(hgrad(θ))
@. dρuₕ = hdiv(hgrad(uₕ))
@. dρw = hdiv(hgrad(w))
Spaces.weighted_dss!(dYc)
Spaces.weighted_dss!(dρw)

κ₄ = 100.0 # m^4/s
@. dYc.ρθ = -κ₄ * hdiv(Yc.ρ * hgrad(dYc.ρθ))
@. dYc.ρuₕ = -κ₄ * hdiv(Yc.ρ * hgrad(dYc.ρuₕ))
@. dρθ = -κ₄ * hdiv(ρ * hgrad(dρθ))
@. dρuₕ = -κ₄ * hdiv(ρ * hgrad(dρuₕ))
@. dρw = -κ₄ * hdiv(Yfρ * hgrad(dρw))

# density
@. dYc.ρ = -∂(ρw)
@. dYc.ρ -= hdiv(Yc.ρuₕ)
@. = -∂(ρw)
@. -= hdiv(ρuₕ)

# potential temperature
@. dYc.ρθ += -(∂(ρw * If(Yc.ρθ / Yc.ρ)))
@. dYc.ρθ -= hdiv(uₕ * Yc.ρθ)
@. dρθ += -(∂(ρw * If(ρθ / ρ)))
@. dρθ -= hdiv(uₕ * ρθ)

# Horizontal momentum
@. dYc.ρuₕ += -uvdivf2c(ρw ⊗ If(uₕ))
@. dρuₕ += -uvdivf2c(ρw ⊗ If(uₕ))
Ih = Ref(
Geometry.Axis2Tensor(
(Geometry.UVAxis(), Geometry.UVAxis()),
@SMatrix [1.0 0.0; 0.0 1.0]
),
)
@. dYc.ρuₕ -= hdiv(Yc.ρuₕ ⊗ uₕ + p * Ih)
@. dρuₕ -= hdiv(ρuₕ ⊗ uₕ + p * Ih)

# vertical momentum
z = coords.z
@. dρw += B(
Geometry.transform(
Geometry.WAxis(),
-(∂f(p)) - If(Yc.ρ) * ∂f(Φ(coords.z)),
) - vvdivc2f(Ic(ρw ⊗ w)),
Geometry.transform(Geometry.WAxis(), -(∂f(p)) - If(ρ) * ∂f(Φ(z))) -
vvdivc2f(Ic(ρw ⊗ w)),
)
uₕf = @. If(Yc.ρuₕ / Yc.ρ) # requires boundary conditions
uₕf = @. If(ρuₕ / ρ) # requires boundary conditions
@. dρw -= hdiv(uₕf ⊗ ρw)

### UPWIND FLUX CORRECTION
upwind_correction = true
if upwind_correction
@. dYc.ρ += fcc(w, Yc.ρ)
@. dYc.ρθ += fcc(w, Yc.ρθ)
@. dYc.ρuₕ += fcc(w, Yc.ρuₕ)
@. += fcc(w, ρ)
@. dρθ += fcc(w, ρθ)
@. dρuₕ += fcc(w, ρuₕ)
@. dρw += fcf(wc, ρw)
end

### DIFFUSION
κ₂ = 0.0 # m^2/s
# 1a) horizontal div of horizontal grad of horiz momentun
@. dYc.ρuₕ += hdiv(κ₂ * (Yc.ρ * hgrad(Yc.ρuₕ / Yc.ρ)))
@. dρuₕ += hdiv(κ₂ * (ρ * hgrad(ρuₕ / ρ)))
# 1b) vertical div of vertical grad of horiz momentun
@. dYc.ρuₕ += uvdivf2c(κ₂ * (Yfρ * ∂f(Yc.ρuₕ / Yc.ρ)))
@. dρuₕ += uvdivf2c(κ₂ * (Yfρ * ∂f(ρuₕ / ρ)))

# 1c) horizontal div of horizontal grad of vert momentum
@. dρw += hdiv(κ₂ * (Yfρ * hgrad(ρw / Yfρ)))
# 1d) vertical div of vertical grad of vert momentun
@. dρw += vvdivc2f(κ₂ * (Yc.ρ * ∂c(ρw / Yfρ)))

# 2a) horizontal div of horizontal grad of potential temperature
@. dYc.ρθ += hdiv(κ₂ * (Yc.ρ * hgrad(Yc.ρθ / Yc.ρ)))
@. dρθ += hdiv(κ₂ * (Yc.ρ * hgrad(ρθ / ρ)))
# 2b) vertical div of vertial grad of potential temperature
@. dYc.ρθ += ∂(κ₂ * (Yfρ * ∂f(Yc.ρθ / Yc.ρ)))
@. dρθ += ∂(κ₂ * (Yfρ * ∂f(ρθ / ρ)))

Spaces.weighted_dss!(dYc)
Spaces.weighted_dss!(dρw)
Expand Down
6 changes: 4 additions & 2 deletions examples/hybrid/bubble_invariant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ function rhs_invariant!(dY, Y, _, t)
dw = dY.w
duₕ = dY.uₕ
dρθ = dY.Yc.ρθ
ρθ = Yc.ρθ
z = coords.z


# 0) update w at the bottom
Expand Down Expand Up @@ -209,13 +211,13 @@ function rhs_invariant!(dY, Y, _, t)
)
@. dw -= vgradc2f(cp) / Ic2f(cρ)

cE = @. (norm(cuw)^2) / 2 + Φ(coords.z)
cE = @. (norm(cuw)^2) / 2 + Φ(z)
@. duₕ -= hgrad(cE)
@. dw -= vgradc2f(cE)

# 3) potential temperature

@. dρθ -= hdiv(cuw * Yc.ρθ)
@. dρθ -= hdiv(cuw * ρθ)
@. dρθ -= vdivf2c(fw * Ic2f(cρθ))
@. dρθ -= vdivf2c(Ic2f(cuₕ * cρθ))

Expand Down