Skip to content

Real Quantity with Units Module #1024

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

Merged
merged 40 commits into from
Nov 7, 2022
Merged

Conversation

hpohekar
Copy link
Collaborator

@hpohekar hpohekar commented Oct 21, 2022

Developed Real Quantity with Units Module.

Checked all test cases from C++ side units library and working fine.

@hpohekar hpohekar linked an issue Oct 21, 2022 that may be closed by this pull request
@hpohekar hpohekar changed the title Initial base interface and representation Real Quantity with Units Oct 21, 2022
@hpohekar hpohekar changed the title Real Quantity with Units Real Quantity with Units Module Oct 28, 2022
@hpohekar hpohekar marked this pull request as ready for review October 28, 2022 12:41
@jorgepiloto
Copy link
Member

Pinging here @RobPasMue. He's feedback may be useful for this, as he implemented a units module on top of pint for an internal project.

@hpohekar hpohekar requested a review from RobPasMue October 28, 2022 12:50
@hpohekar
Copy link
Collaborator Author

hpohekar commented Oct 28, 2022

Pinging here @RobPasMue. He's feedback may be useful for this, as he implemented a units module on top of pint for an internal project.

Thank you, @jorgepiloto

@hpohekar hpohekar requested a review from prmukherj October 28, 2022 12:55
@hpohekar
Copy link
Collaborator Author

hpohekar commented Oct 28, 2022

Developed Real Quantity with Units Module.

Checked all test cases from C++ side units library and working fine.

Only individual temperature conversion test case is remaining and will handle this after discussion.

Following illustration may help to understand the terminologies used in code.

image

@h-krishnan
Copy link
Contributor

>>> q
Quantity (5.0, "m")
>>> -q  # should return Quantity(-5.0, "m")
-5.0
>>>

@hpohekar
Copy link
Collaborator Author

hpohekar commented Nov 3, 2022

Some issues:

>>> a = Quantity (3.0, "m")
>>> c = Quantity(4.0, "")
>>> a + "a" # wrong error message
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "quantity.py", line 588, in __add__
    raise ValueError("Incompatible dimensions.")
ValueError: Incompatible dimensions.
>>> c + 2  # adding float to dimensionless number should be supported
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "quantity.py", line 589, in __add__
    return Quantity(temp_value, self._si_unit)
  File "quantity.py", line 483, in __init__
    self._dimension = Dimension(unit_str)
  File "quantity.py", line 322, in __init__
    self._parser(unit_str)
  File "quantity.py", line 353, in _parser
    unit_dim = self._get_dim(unit_str, term_power)
  File "quantity.py", line 370, in _get_dim
    raise ValueError("Not implemented")
ValueError: Not implemented
>>>

image

@hpohekar
Copy link
Collaborator Author

hpohekar commented Nov 3, 2022

>>> q
Quantity (5.0, "m")
>>> -q  # should return Quantity(-5.0, "m")
-5.0
>>>

Done. Thank you.

image

@h-krishnan
Copy link
Contributor

h-krishnan commented Nov 4, 2022

What is the float or si_value of Quantity(1, 'C')?

@hpohekar
Copy link
Collaborator Author

hpohekar commented Nov 4, 2022

What is the float or si_value of Quantity(1, 'C')?

@h-krishnan @seanpearsonuk @dnwillia-work @mkundu1

---------------------------------------------

The degree Celsius is itself used in SI units alongside the Kelvin -

https://en.wikipedia.org/wiki/Celsius

"The degree Celsius is the unit of [temperature] on the Celsius scale, one of 2 [temperature scales] used in the [International System of Units] alongside the [Kelvin scale]" (mentioned on Wikipedia)

---------------------------------------------

One Celsius degree is an interval of 1 K, and zero degrees Celsius is 273.15 K.

Above point is mentioned on official website of US government -
https://www.nist.gov/pml/owm/si-units-temperature#:~:text=One%20Celsius%20degree%20is%20an,on%20the%20Fahrenheit%20temperature%20scale.

Hence there are 2 parts now.

1st part - representation of Celsius in terms of Kelvin (includes SI multiplier as 1)

This gives float or si_value and si_unit of Quantity(1, 'C') as (1.0, K)

2nd part - conversion of Celsius to Kelvin (includes addition of an offset 273.15)

This gives conversion Quantity(1, 'C') to Kelvin as (274.15, "K")

It is also defined in units.cfx as follows -

image

Therefore we have also defined this mapping at 2 places in quantity.py.

1st is in conversion_map as follows -

image

2nd is in temperature_conversions as follows -

image

C++ units library does the same thing, so we are consistent with units.cfx as well.

We are also consistent with all conversions mentioned on following website -
https://www.engineeringtoolbox.com/specific-heat-capacity-converter-d_673.html

We have covered and verified conversions from above website, C++ units library and UnitSystems.ccl (including temperatures variance which involves K^2) in test_tempk_48 to test_temp_54 (from test_quantity.py).

Thank you.

@hpohekar
Copy link
Collaborator Author

hpohekar commented Nov 4, 2022

Pinging here @RobPasMue. He's feedback may be useful for this, as he implemented a units module on top of pint for an internal project.

@jorgepiloto @h-krishnan @seanpearsonuk @dnwillia-work @mkundu1

We have found 1 bug in pint module.

It does not convert temperatures properly.

See following case - Converting (-40000, milli-kelvin) to micro-degree-Celsius

image

C++ units library gives this -

image

and this current Real Quantity with Units module is also gives same result in comparison to C++ units library -

image

Thank you.

@seanpearsonuk
Copy link
Collaborator

-40K makes sense only as a temperature difference so the conversion to relative scales should be considered undefined. While pint seems to be inconsistent between C and microC, neither result is worse than the other.

@h-krishnan
Copy link
Contributor

What is the float or si_value of Quantity(1, 'C')?

@h-krishnan @seanpearsonuk @dnwillia-work @mkundu1

---------------------------------------------

The degree Celsius is itself used in SI units alongside the Kelvin -

https://en.wikipedia.org/wiki/Celsius

"The degree Celsius is the unit of [temperature] on the Celsius scale, one of 2 [temperature scales] used in the [International System of Units] alongside the [Kelvin scale]" (mentioned on Wikipedia)

---------------------------------------------

One Celsius degree is an interval of 1 K, and zero degrees Celsius is 273.15 K.

Above point is mentioned on official website of US government - https://www.nist.gov/pml/owm/si-units-temperature#:~:text=One%20Celsius%20degree%20is%20an,on%20the%20Fahrenheit%20temperature%20scale.

Hence there are 2 parts now.

1st part - representation of Celsius in terms of Kelvin (includes SI multiplier as 1)

This gives float or si_value and si_unit of Quantity(1, 'C') as (1.0, K)

2nd part - conversion of Celsius to Kelvin (includes addition of an offset 273.15)

This gives conversion Quantity(1, 'C') to Kelvin as (274.15, "K")

It is also defined in units.cfx as follows -

image

Therefore we have also defined this mapping at 2 places in quantity.py.

1st is in conversion_map as follows -

image

2nd is in temperature_conversions as follows -

image

C++ units library does the same thing, so we are consistent with units.cfx as well.

We are also consistent with all conversions mentioned on following website - https://www.engineeringtoolbox.com/specific-heat-capacity-converter-d_673.html

We have covered and verified conversions from above website, C++ units library and UnitSystems.ccl (including temperatures variance which involves K^2) in test_tempk_48 to test_temp_54 (from test_quantity.py).

Thank you.

My opinion:
If we say Quantity(1, "C") refers to temperature difference always, then the float value should be 1. If we however say that Quantity(1, "C") refers to the absolute temperature, the float value should 274.15.
cc @seanpearsonuk @dnwillia-work

@hpohekar
Copy link
Collaborator Author

hpohekar commented Nov 7, 2022

What is the float or si_value of Quantity(1, 'C')?

@h-krishnan @seanpearsonuk @dnwillia-work @mkundu1
---------------------------------------------
The degree Celsius is itself used in SI units alongside the Kelvin -
https://en.wikipedia.org/wiki/Celsius
"The degree Celsius is the unit of [temperature] on the Celsius scale, one of 2 [temperature scales] used in the [International System of Units] alongside the [Kelvin scale]" (mentioned on Wikipedia)
---------------------------------------------
One Celsius degree is an interval of 1 K, and zero degrees Celsius is 273.15 K.
Above point is mentioned on official website of US government - https://www.nist.gov/pml/owm/si-units-temperature#:~:text=One%20Celsius%20degree%20is%20an,on%20the%20Fahrenheit%20temperature%20scale.
Hence there are 2 parts now.
1st part - representation of Celsius in terms of Kelvin (includes SI multiplier as 1)
This gives float or si_value and si_unit of Quantity(1, 'C') as (1.0, K)
2nd part - conversion of Celsius to Kelvin (includes addition of an offset 273.15)
This gives conversion Quantity(1, 'C') to Kelvin as (274.15, "K")
It is also defined in units.cfx as follows -
image
Therefore we have also defined this mapping at 2 places in quantity.py.
1st is in conversion_map as follows -
image
2nd is in temperature_conversions as follows -
image
C++ units library does the same thing, so we are consistent with units.cfx as well.
We are also consistent with all conversions mentioned on following website - https://www.engineeringtoolbox.com/specific-heat-capacity-converter-d_673.html
We have covered and verified conversions from above website, C++ units library and UnitSystems.ccl (including temperatures variance which involves K^2) in test_tempk_48 to test_temp_54 (from test_quantity.py).
Thank you.

My opinion: If we say Quantity(1, "C") refers to temperature difference always, then the float value should be 1. If we however say that Quantity(1, "C") refers to the absolute temperature, the float value should 274.15. cc @seanpearsonuk @dnwillia-work

@h-krishnan @seanpearsonuk @dnwillia-work @mkundu1

The main point related to this is, the C++ units library uses same unit for both absolute temperature and temperature difference so we also follows this. (from UnitSystems.ccl)

image

image

image

image

image

image

image

C++ units library gives this -

image

Current Quantity module gives this -

image

pint handles degree_Celsius differently, it uses delta_degree_Celsius unit for temperature difference as follows -

image

pint does not allow to create degree Celsius quantity as absolute temperature, only allows to treat it as temperature difference

If we try to construct it as absolute temperature, it gives following error.

image

So we summarize above in following points -

  1. Quantity(1, "C") in both C++ units library and Quantity module refers to temperature difference always, so the float value is 1.
  2. Quantity(1, "C") in pint also always refers to temperature difference, but represented with delta_degree_Celsius unit.

The conclusion is as follows -

The C++ units library, Quantity module and pint, all of them always refers Quantity(1, "C") as temperature difference only, so the float value is 1.

Thank you.

@hpohekar hpohekar merged commit c513da1 into main Nov 7, 2022
@hpohekar hpohekar deleted the feat/latest_real_quantity_with_units branch November 7, 2022 07:59
@h-krishnan
Copy link
Contributor

@hpohekar @seanpearsonuk @dnwillia-work So, what does Quantity(2, "C")*Quantity(3, "m") give? In the absence of any other context, is Quantity(2, "C") a temperature difference or a temperature? I feel it should be "temperature" in which case Quantity(2, "C") should be 275.15.

@dnwillia-work
Copy link
Collaborator

I think this test confirms that it works as you suggest:
https://github.com/pyansys/pyfluent/blob/4114599d142afce0f678cc82f3d1d386a2a993f4/tests/test_quantity.py#L430
i.e. it's a temperature.

We could add some additional properties to the quantity object to make it clear what it is. Another one is intensive or extensive, and, if it's the former is it per unit area or pure unit volume.

@hpohekar
Copy link
Collaborator Author

hpohekar commented Nov 8, 2022

@hpohekar @seanpearsonuk @dnwillia-work So, what does Quantity(2, "C")*Quantity(3, "m") give? In the absence of any other context, is Quantity(2, "C") a temperature difference or a temperature? I feel it should be "temperature" in which case Quantity(2, "C") should be 275.15.

@h-krishnan @dnwillia-work @seanpearsonuk

Right, in absence of any other context, currently we can not differentiate between whether the Quantity(1, "C") is temperature or temperature difference and root cause of this is the use of same unit for both temperature and temperature difference.

We will have to handle this case differently.

Thank you.

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 this pull request may close these issues.

Proposal for units support for real entries
6 participants