Skip to content

Dimension Conversion Fails in Mixed Dimension Quantities #2072

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

Open
julian-belina opened this issue Oct 31, 2024 · 5 comments
Open

Dimension Conversion Fails in Mixed Dimension Quantities #2072

julian-belina opened this issue Oct 31, 2024 · 5 comments

Comments

@julian-belina
Copy link

Hi there,

Unfortunately I ran into a problem with converting mixed dimensions using contexts. I want to convert one dimension into another dimension in a mixed quantity like 1*[MyDimension]/[km] -->1.43*[MyOtherDimension]/[km]. If I only use my self defined dimensions 1*[MyDimension]] -->1.43*[MyOtherDimension], everything works as expected. However, if I mix the dimensions, the conversion fails. I have come up with the following minimal example:

import pathlib

import pint

# generate pint registry
unit_registry = pint.UnitRegistry(system="SI")
unit_registry.formatter.default_format = "~"

# add currency dimensions to pint registry
current_folder = pathlib.Path(__file__).parents[0]
path_to_registry_definition = current_folder.joinpath(r"unit_def_file.txt")
unit_registry.load_definitions(path_to_registry_definition)


# Create quantity from self defined unit in dimension 1
my_quantity = 1 * unit_registry.Unit("my_unit_1_dim_1")
# Convert to unit within the same dimension --> Works
print("Conversion 1", my_quantity.to("my_unit_2_dim_1"))

# Convert to unit in other dimension --> Works
print(
    "Conversion to other dim:", my_quantity.to("my_unit_1_dim_2", "context_dim_1_dim_2")
)
# Convert standard units using the the self defined context -->works
length_quantity = 1 * unit_registry.Unit("km")
print(length_quantity.to("m", "context_dim_1_dim_2"))

# Convert self from one self defined dimension to other self defined dimension with composite units --> Fails
my_composite_quantity = 1 * unit_registry.Unit("my_unit_1_dim_1/km")
print(my_composite_quantity.to("my_unit_1_dim_2/km", "context_dim_1_dim_2"))

Exception has occurred: DimensionalityError
Cannot convert from 'my_unit_1_dim_1 / kilometer' ([my_dimension_1] / [length]) to 'my_unit_1_dim_2 / kilometer' ([my_dimension_2] / [length]

This is the context of my definition file :unit_def_file.txt

my_unit_1_dim_1=[my_dimension_1]

my_unit_2_dim_1 = my_unit_1_dim_1 2.5 

my_unit_1_dim_2=[my_dimension_2]

my_unit_2_dim_2 = my_unit_1_dim_2 4

@context context_dim_1_dim_2
	[my_dimension_1] -> [my_dimension_2] : value * 3.5 * my_unit_1_dim_2/my_unit_1_dim_1
	[my_dimension_2] -> [my_dimension_1] : (value / 3.5) * my_unit_1_dim_1/my_unit_1_dim_2
@end

I would like to use these conversions to implement inflation rates and different currencies, while also dealing with other units. A typical use case would be to convert €/l to $/gallon.

Is this a bug, have I misconfigured something or is the functionality just not implemented?Any help would be appreciated.

@julian-belina julian-belina changed the title Units with comp Dimension Conversion Fails in Mixed Dimension Quantities Oct 31, 2024
@chrisjwin
Copy link

second

@mathbellev
Copy link

Hello,
Without creating a custom unit, I ran into a similar problem:
electron_volt dimensionality is [mass] * [length] ** 2 / [time] ** 2, as is Joule's.
However, eV / kg dimensionality is [temperature] * [length] ** 2 / [time] ** 3 / [current], whereas J / kg is [length] ** 2 / [time] ** 2.
So, while one can convert eV to Joules, we cannot convert eV/kg to J/kg.
Is [temperature]/[time]/[current] dimensionless ?

@julian-belina
Copy link
Author

Interesting, I am not sure if this is a bug, a missing feature or a misconfiguration. However it would be nice to make it work. Do you have any ideas on how to solve this?

@mathbellev
Copy link

Sorry, my fault.
I had introduced the unit C as an alias for degreeC, which was overwriting the Coulomb definition, hence the apparition of the [temperature] in the dimensionality computation.
By restoring the defintion of C, everything is back to normal.

@andrewgsavage
Copy link
Collaborator

I would like to use these conversions to implement inflation rates and different currencies, while also dealing with other units. A typical use case would be to convert €/l to $/gallon.

I haven't looked into your issue, however i note there's an example of doing this here
#2146

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

4 participants