NVIDIA / modulus-sym

Framework providing pythonic APIs, algorithms and utilities to be used with Modulus core to physics inform model training as well as higher level abstraction for domain experts
https://developer.nvidia.com/modulus
Apache License 2.0
193 stars 73 forks source link

🐛[BUG]: NonDimensionalizer not compatible with numpy array based quantities. #113

Open domyam97 opened 9 months ago

domyam97 commented 9 months ago

Version

1.2.0

On which installation method(s) does this occur?

Docker

Describe the issue

When using a numpy array based quantity (eg. x = quantity(np.ndarray([[0.2]]),'m') the call to ndim fails due to using dimensionality to check that the dimensions have been reduced. This is becuase the ndim method uses an in-place division to non-dimensionalize the quantity. An in-place operation doesn't change the _dimensionality parameter of the quantity object in pint.

I recommend using PlainQuantity.dimensionless or PlainQuantity.unitless to check that the object has been non-dimensionalized. This will work for numpy array based quantities.

Or the operation in NonDimensionalizer.ndim() could be changed to not be an in-place division.

See related pint issue.(https://github.com/hgrecco/pint/issues/1938)

Minimum reproducible example

import numpy as np
from modulus.sym import quantity
from modulus.sym.eq.non_dim import NonDimensionalizer, Scaler

# create a numpy array based quantity
x = quantity(np.array([[20.0]],"m")
print(x.dimensionality) # returns {'[length]':1.0}

# divide by 1m to get a dimensionless quantity using in-place operation
# replicates the function x.ndim()
x /= quantity(1,"m")
print(x.dimensionality) # still returns {'[length]':1.0}

print(x.dimensionless) # returns True

Relevant log output

No response

Environment details

No response

Other/Misc.

No response