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

Improve design of nonorographic gravity wave parameterization #3191

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

Xiaoyang-Xie
Copy link
Contributor

@Xiaoyang-Xie Xiaoyang-Xie commented Jul 12, 2024

Purpose

Part of #897

To-do

  • Remove use of getindex.
  • Remove all instances of Fields.bycolumn and parent .
  • Refactor the non_orographic_gravity_wave_forcing function to be a pointwise function.

Content

  • Reduce allocations.
  • Remove use of append!
  • removed many instances of Fields.bycolumn and parent.
  • Remove use of findlast and findfirst
  • Refactor non_orographic_gravity_wave_forcing to be purely functional .

  • I have read and checked the items on the review checklist.

Comment on lines 335 to 338
B0 = @. sign(c_hat0) * (
Bw * exp(-log(2.0) * ((c * flag + c_hat0 * (1 - flag) - c0) / cw)^2) +
Bn * exp(-log(2.0) * ((c * flag + c_hat0 * (1 - flag) - c0) / cn)^2)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
B0 = @. sign(c_hat0) * (
Bw * exp(-log(2.0) * ((c * flag + c_hat0 * (1 - flag) - c0) / cw)^2) +
Bn * exp(-log(2.0) * ((c * flag + c_hat0 * (1 - flag) - c0) / cn)^2)
)
@. B0 = sign(c_hat0) * (
Bw * exp(-log(2.0) * ((c * flag + c_hat0 * (1 - flag) - c0) / cw)^2) +
Bn * exp(-log(2.0) * ((c * flag + c_hat0 * (1 - flag) - c0) / cn)^2)
)

The form B0 = @. sign(c_hat0) ... points B0 to new memory. the form I suggested will mutate B0 in-place

Comment on lines 457 to 467
function reduce_fun1(a, b)
return ifelse(a[1] < b[1], a, b)
end

function reduce_fun2(a, b)
return ifelse(b[1] < 0, a, b)
end

function reduce_fun3(a, b)
return ifelse(a[1] > 0, b, a)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function reduce_fun1(a, b)
return ifelse(a[1] < b[1], a, b)
end
function reduce_fun2(a, b)
return ifelse(b[1] < 0, a, b)
end
function reduce_fun3(a, b)
return ifelse(a[1] > 0, b, a)
end
@inline reduce_fun1(a, b) = ifelse(a[1] < b[1], a, b)
@inline reduce_fun2(a, b) = ifelse(b[1] < 0, a, b)
@inline reduce_fun3(a, b) = ifelse(a[1] > 0, b, a)

Comment on lines 1 to 2
[topo-info]
git-tree-sha1 = "9970989c13c2ad015ab5738d42fd9f5b320f1451"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to add this to the commits?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not, let's remove it.

Copy link
Member

@charleskawczynski charleskawczynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a few suggestions, but this is a good step forward.

@Xiaoyang-Xie Xiaoyang-Xie force-pushed the xxy/gravity_waves branch 4 times, most recently from 3d65dc9 to 2d0612e Compare July 15, 2024 21:09
Copy link
Member

@szy21 szy21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good step forward. @charleskawczynski @dennisYatunin Could you take a look again?

Comment on lines 450 to 452
@inline reduce_fun1(a, b) = ifelse(a[1] < b[1], a, b)
@inline reduce_fun2(a, b) = ifelse(b[1] < 0, a, b)
@inline reduce_fun3(a, b) = ifelse(a[1] > 0, b, a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you come up with some more informative names for these functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is these names OK?:
@inline min_distance(a, b) = ifelse(a[1] < b[1], a, b)
@inline positive_selector(a, b) = ifelse(b[1] < 0, a, b)
@inline negative_selector(a, b) = ifelse(a[1] > 0, b, a)

end
source_level = source_level_z.:2

Operators.column_mapreduce!(sign, +, damp_level, ᶜz)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dennisYatunin Would something like @. damp_level = Spaces.nlevels(axes(ᶜz)) work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it and found that @. damp_level = Spaces.nlevels(axes(ᶜz)) could not work, but we can use fill!(damp_level, Spaces.nlevels(axes(ᶜz))) instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, damp_level .= Spaces.nlevels(axes(ᶜz)) or fill!(damp_level, Spaces.nlevels(axes(ᶜz)) would be better here

@@ -388,3 +446,7 @@ end
function calc_intermitency(ρ_source_level, source_ampl, nk, Bsum)
return (source_ampl / ρ_source_level / nk) / Bsum
end

@inline reduce_fun1(a, b) = ifelse(a[1] < b[1], a, b)
@inline reduce_fun2(a, b) = ifelse(b[1] < 0, a, b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@inline reduce_fun2(a, b) = ifelse(b[1] < 0, a, b)
@inline reduce_fun2(a, b) = ifelse(b[1] <= 0, a, b)

(I think this is exactly the same as the previous logic, although it probably doesn't matter much)


@inline reduce_fun1(a, b) = ifelse(a[1] < b[1], a, b)
@inline reduce_fun2(a, b) = ifelse(b[1] < 0, a, b)
@inline reduce_fun3(a, b) = ifelse(a[1] > 0, b, a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@inline reduce_fun3(a, b) = ifelse(a[1] > 0, b, a)
@inline reduce_fun3(a, b) = ifelse(a[1] >= 0, b, a)

append!(ᶜρ, ᶜρ[end] * ᶜρ[end] / ᶜρ[end - 1])
append!(ᶜbf, ᶜbf[end])
append!(ᶜz, FT(2) * ᶜz[end] - ᶜz[end - 1])
ᶜu = vcat(old_ᶜu, FT(2) * old_ᶜu[end] - old_ᶜu[end - 1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to note, vcat is not really fixing the issue here, but the change is fine.

@szy21
Copy link
Member

szy21 commented Jul 18, 2024

Please feel free to rebase and merge, thanks!

@Xiaoyang-Xie Xiaoyang-Xie added this pull request to the merge queue Jul 18, 2024
Merged via the queue into main with commit b476f7d Jul 19, 2024
11 of 14 checks passed
@Xiaoyang-Xie Xiaoyang-Xie deleted the xxy/gravity_waves branch July 19, 2024 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants