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

Fix precision issue with interpolation in profile helper #1192

Merged
merged 1 commit into from
Oct 4, 2019

Conversation

zbruick
Copy link
Contributor

@zbruick zbruick commented Oct 3, 2019

Description Of Changes

Ah, precision issues. This bug was due to the LCL pressure having rounding issues and being greater than the surface profile pressure in the helper function for parcel_profile_with_lcl. A simple rounding to take care of the precision issue solves this.

Checklist

@zbruick zbruick added Type: Bug Something is not working like it should Area: Calc Pertains to calculations labels Oct 3, 2019
@zbruick zbruick added this to the 0.12 milestone Oct 3, 2019
@zbruick zbruick requested a review from dopplershift as a code owner October 3, 2019 16:31
@dopplershift
Copy link
Member

Another question: so the problem is that the LCL is at the surface, but it’s returning a pressure slightly above the first?

@zbruick
Copy link
Contributor Author

zbruick commented Oct 3, 2019

Yeah, the LCL pressure is being returned as slightly greater than the surface pressure (in this case surface pressure is 900 hPa and LCL pressure is like 900.000000004 hPa. So another fix could just be making the LCL pressure equal to the surface pressure if temperature[0] == dewpoint[0].

@dopplershift
Copy link
Member

Yeah, the right fix might be to figure out why there’s any numerical precision in the LCL in that situation. At least, that seems like the fundamental problem to me here.

@dopplershift
Copy link
Member

One option might be to do something like:

If is_close(lcl_pressure, pressure[0]):
    return pressure[0]

@zbruick
Copy link
Contributor Author

zbruick commented Oct 3, 2019

Yup, that could work.

I went digging for where the precision issue traced back to and it looks like it's the np.log call in dewpoint

@dopplershift
Copy link
Member

We also determine where convergence is by considering two points “close”. Are we picking the wrong point of those two to return?

@dopplershift dopplershift merged commit 54fd284 into Unidata:master Oct 4, 2019
# Conditional needed due to precision error with np.log in dewpoint.
# Causes issues with parcel_profile_with_lcl if removed. Issue #1187
if np.isclose(lcl_p, pressure):
lcl_p = pressure
Copy link
Contributor

Choose a reason for hiding this comment

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

As I look at this change now, I don't understand it. Doesn't this return the entire pressure profile?

thanks, confused in Iowa.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lcl only takes scalar input variables (not arrays), so this will only return the pressure provided. The function will error out earlier than this line if an array is provided.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh thank you! I was confused as stated since lcl() in 0.11.0 takes arrays. Whew.

Copy link
Contributor

Choose a reason for hiding this comment

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

it appears to me that np.isclose is now where this errors out when passed an array. FWIW

  File "/home/akrherz/projects/MetPy/src/metpy/calc/thermo.py", line 365, in lcl
    if np.isclose(lcl_p, pressure):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well you've just won yourself a bug for the day. I'll open an issue, since if this conditional were not present, it would indeed return an array. We should be catching if arrays are fed in and either take the initial value or return an error. Where did you see it saying that lcl takes arrays in 0.11, because I don't see that in the release notes?

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't see it, I was just naively/ignorantly doing it. Just taking the same profile object and sequentially passing it to various sounding functions without thinking about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok thanks. I opened up #1199 to discuss this. If you have an opinion about how to handle this case, feel free to comment there.

@dopplershift dopplershift modified the milestones: 0.12, 0.11.1 Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Calc Pertains to calculations Type: Bug Something is not working like it should
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lcl() RuntimeError: failed to converge after 50 iterations, value is nan
3 participants