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

Units with offsets raise DimensionalityError in 0.23 #1949

Closed
zobac opened this issue Mar 7, 2024 · 2 comments · Fixed by #1952
Closed

Units with offsets raise DimensionalityError in 0.23 #1949

zobac opened this issue Mar 7, 2024 · 2 comments · Fixed by #1952

Comments

@zobac
Copy link

zobac commented Mar 7, 2024

Hi There,

I have a series of tests that have been passing since we started using Pint to handle units in our software (we adopted it in 2016). We recently updated to Pint 0.23 and these test are failing now, though (strangely) not in all similar cases.

We've defined some custom pressure units to handle gauge pressure (measurements using atmospheric pressure as its zero point). We want to (and have been able to historically) convert between absolute pressure and gauge pressure. Because of changes in 0.23 this no longer works.

We've defined our extra pressure units in our units.txt file as such:

# Pressure
[pressure] = [force] / [area]
pascal = newton / metre ** 2 = Pa
pascal_gauge = 1 * pascal; offset: 101325  = Pa_g = Pa_gauge
bar = 100000 * pascal
bar_gauge = 100000 * pascal; offset: 101325 = bar_g
atmosphere = 101325 * pascal = atm = standard_atmosphere
atmosphere_gauge = 101325 * pascal; offset: 101325 = standard_atmosphere_gauge = atm_g = atm_gauge
torr = atm / 760
torr_gauge = 133322.4 gram / metre / second ** 2; offset: 101325000 = torr_g
inch_Hg = inch * Hg * g_0 = inHg = in_Hg = inch_Hg_32F
in_Hg_gauge = 3386389 * gram / metre / second ** 2; offset:101325000 = inHg_g = in_Hg_g = inHg_gauge
pound_force_per_square_inch = pound * gravity / inch ** 2 = psi
pound_force_per_square_inch_gauge = 6894.75729 * pascal; offset: 101325 = psig = psi_g = psi_gauge

In version 0.22 I was able to do the following, but no longer can:

import pint
UREG = pint.UnitRegistry(r'path\to\units.txt')
UREG.Quantity('1 atm').to('inHg_gauge')

I get a DimensionalityError when I try this

pint.errors.DimensionalityError: Cannot convert from 'atmosphere' ([mass] / [length] / [time] ** 2) to '1 / second ** 2' (1 / [time] ** 2)

Interestingly though, I don't have that problem with Pa_gauge bar_gauge or psig. Only with inHg_gauge and torr_gauge.

Is this a bug, or is there something I should do to my unit definitions to accommodate changes in how pint handles things?

Thanks for your help!

@zobac
Copy link
Author

zobac commented Mar 8, 2024

@dalito
Copy link
Contributor

dalito commented Mar 8, 2024

This is a bug. The error message:

pint.errors.DimensionalityError: Cannot convert from 'standard_atmosphere' ([mass] / [length] / [time] ** 2) to '1 / second ** 2' (1 / [time] ** 2)

The reason why it fails for some gauge units but not for all, is caused by the differences in the definition. The code used only the last unit in the UnitsContainer of the "to"-unit. This works fine as long as the gauge unit is defined based on a single unit ("pascal"). However, if you use a composed unit "gram / metre / second ** 2" the code picked only the last element in the UnitContainer which is '1 / second ** 2'.

@zobac - I proposed a fix in #1952.

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 a pull request may close this issue.

2 participants