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

Specifying the floating precision of unit powers #1487

Open
iiwolf opened this issue Mar 27, 2022 · 2 comments
Open

Specifying the floating precision of unit powers #1487

iiwolf opened this issue Mar 27, 2022 · 2 comments

Comments

@iiwolf
Copy link

iiwolf commented Mar 27, 2022

Hello,

I am working with convection coefficients and they have some funky floating powers. Is there a way to specify precision of the units when reducing the units? For example I have the following equation

h_c = 0.023 * V ** 0.8 * k ** 0.6 * (rho * cp) ** 0.4 / (D ** 0.2 * nu ** 0.4)

That I can properly reduced to base units

h_c.to_base_units()
<Quantity(15710.5834, 'kilogram / kelvin ** 0.6 / meter ** 5.55112e-17 / second ** 2.6')>

But notice the meter ** 5.55112e-17. So that if I try to reduce to the units I want ("W/(m ** 2 * K)") or just to_reduced_units() I get the following error.

(0.023 * (V ** 0.8 * k ** 0.6 * (rho * cp) ** 0.4) / (D ** 0.2 * nu ** 0.4)).to_reduced_units()
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ijw/.local/lib/python3.8/site-packages/pint/quantity.py", line 738, in to_reduced_units
    newq.ito_reduced_units()
  File "/home/ijw/.local/lib/python3.8/site-packages/pint/quantity.py", line 728, in ito_reduced_units
    return self.ito(newunits)
  File "/home/ijw/.local/lib/python3.8/site-packages/pint/quantity.py", line 637, in ito
    self._magnitude = self._convert_magnitude(other, *contexts, **ctx_kwargs)
  File "/home/ijw/.local/lib/python3.8/site-packages/pint/quantity.py", line 616, in _convert_magnitude
    return self._REGISTRY.convert(
  File "/home/ijw/.local/lib/python3.8/site-packages/pint/registry.py", line 947, in convert
    return self._convert(value, src, dst, inplace)
  File "/home/ijw/.local/lib/python3.8/site-packages/pint/registry.py", line 1830, in _convert
    return super()._convert(value, src, dst, inplace)
  File "/home/ijw/.local/lib/python3.8/site-packages/pint/registry.py", line 1433, in _convert
    return super()._convert(value, src, dst, inplace)
  File "/home/ijw/.local/lib/python3.8/site-packages/pint/registry.py", line 980, in _convert
    raise DimensionalityError(src, dst, src_dim, dst_dim)
pint.errors.DimensionalityError: Cannot convert from 'kilogram ** 0.4 * watt ** 0.6 / centimeter ** 0.2 / kelvin ** 0.6 / meter / second ** 0.8' ([mass] / [length] ** 5.55112e-17 / [temperature] ** 0.6 / [time] ** 2.6) to 'kilogram ** 0.4 * watt ** 0.6 / centimeter ** 1.2 / kelvin ** 0.6 / second ** 0.8' ([mass] / [temperature] ** 0.6 / [time] ** 2.6)

Ultimately I think I just want to be able to truncate the powers of the units to 16 or so to be able to reduce. I tried a few different things and searched around, but could come up with anything clean.

Thanks so much! Love the library and I use it extensively.

@jules-ch
Copy link
Collaborator

I'd advise you to use Fraction for unit ratio. Thus unit powers should be reduced accordingly.

Can you update your minimal example with definition for V rho cp D & nu.
I can guess rho & cp but not the other, just for the sake of reproductibility.

About Fraction, I think we need to push this to use Fraction everywhere for unit powers or at least for float ratio.

@jules-ch
Copy link
Collaborator

jules-ch commented Mar 28, 2022

xref #1197 (comment), #757 (comment)
#60 (comment)
#681 (comment).

Last comment was about performance also, it would be good to have a benchmark on performance when using Fraction, seems weird it affects performance no ?

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

No branches or pull requests

2 participants