-
Notifications
You must be signed in to change notification settings - Fork 415
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
Prevent frontogenesis from returning nans #3696
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -567,7 +567,18 @@ def frontogenesis(potential_temperature, u, v, dx=None, dy=None, x_dim=-1, y_dim | |||||||||||||||||
|
||||||||||||||||||
# Compute the angle (beta) between the wind field and the gradient of potential temperature | ||||||||||||||||||
psi = 0.5 * np.arctan2(shrd, strd) | ||||||||||||||||||
beta = np.arcsin((-ddx_theta * np.cos(psi) - ddy_theta * np.sin(psi)) / mag_theta) | ||||||||||||||||||
arg = (-ddx_theta * np.cos(psi) - ddy_theta * np.sin(psi)) / mag_theta | ||||||||||||||||||
|
||||||||||||||||||
# A few problems may occur when calculating the argument to the arcsin function. | ||||||||||||||||||
# First, we may have divided by zero, since a constant theta field would mean | ||||||||||||||||||
# mag_theta is zero. To counter this, we set the argument to zero in this case. | ||||||||||||||||||
# Second, due to round-off error, the argument may be slightly outside the domain | ||||||||||||||||||
# of arcsin. To counter this, we use np.clip to force the argument to be an | ||||||||||||||||||
# acceptable value. With these adjustments, we can make sure beta doesn't end up | ||||||||||||||||||
# with nans somewhere. | ||||||||||||||||||
arg[mag_theta == 0] = 0 | ||||||||||||||||||
arg = np.clip(arg, -1, 1) | ||||||||||||||||||
beta = np.arcsin(arg) | ||||||||||||||||||
|
||||||||||||||||||
return 0.5 * mag_theta * (tdef * np.cos(2 * beta) - div) | ||||||||||||||||||
Comment on lines
+581
to
583
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remember the cosine double-angle identities correctly, this should be equivalent:
Suggested change
The If you want to declare the trigonometric identities out-of-scope for this PR, that is entirely understandable. |
||||||||||||||||||
|
||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One option would be something like this:
Given how
mag_theta
is calculated,arg
should be already zero wheremag_theta
is zero.While searching for the
where
argument tonp.divide
, I found a StackOverflow answer suggestingwhich is a single expresion and seems somewhat more explicit about what it is doing and why.