FiniteVolumeTransportPhenomena / PyFVTool

Finite volume toolbox in Python
GNU Lesser General Public License v2.1
13 stars 4 forks source link

Deprecation warnings from numpy 1.26.2 #24

Closed mhvwerts closed 9 months ago

mhvwerts commented 9 months ago

I needed to install a new Python stack and one of the effects is that I now have numpy version 1.26.2 (I still use Python 3.9). When running a quick pytest to see if my installation was OK for continuing to work, I discovered quite a list of new DeprecationWarnings.

I do not have time to look into this further now. I record some of the warnings here, so that we fix them in some near future.

pyfvtool\boundary.py:152: 14 warnings
tests/test_benchmark_1d.py: 3 warnings
  C:\Users\werts-moltech\Documents\GitHub-mhvwerts\PyFVTool\pyfvtool\boundary.py:152:
DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error 
in future. Ensure you extract a single element from your array before performing this operation. 
(Deprecated NumPy 1.25.)
    s[q] = BC.right.b/2 + BC.right.a/dx_end

pyfvtool\boundary.py:156: 14 warnings
tests/test_benchmark_1d.py: 3 warnings
  C:\Users\werts-moltech\Documents\GitHub-mhvwerts\PyFVTool\pyfvtool\boundary.py:156:
DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error
in future. Ensure you extract a single element from your array before performing this operation.
(Deprecated NumPy 1.25.)
    s[q] = BC.right.b/2 - BC.right.a/dx_end

pyfvtool\boundary.py:157: 14 warnings
tests/test_benchmark_1d.py: 3 warnings
  C:\Users\werts-moltech\Documents\GitHub-mhvwerts\PyFVTool\pyfvtool\boundary.py:157:
DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error
in future. Ensure you extract a single element from your array before performing this operation.
(Deprecated NumPy 1.25.)
    BCRHS[G[i]] = BC.right.c

pyfvtool\boundary.py:164: 14 warnings
tests/test_benchmark_1d.py: 3 warnings
  C:\Users\werts-moltech\Documents\GitHub-mhvwerts\PyFVTool\pyfvtool\boundary.py:164:
DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error
in future. Ensure you extract a single element from your array before performing this operation.
(Deprecated NumPy 1.25.)
    s[q] = -(BC.left.b/2 + BC.left.a/dx_1)

pyfvtool\boundary.py:168: 14 warnings
tests/test_benchmark_1d.py: 3 warnings
  C:\Users\werts-moltech\Documents\GitHub-mhvwerts\PyFVTool\pyfvtool\boundary.py:168:
DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error
in future. Ensure you extract a single element from your array before performing this operation.
(Deprecated NumPy 1.25.)
    s[q] = -(BC.left.b/2 - BC.left.a/dx_1)

pyfvtool\boundary.py:169: 14 warnings
tests/test_benchmark_1d.py: 3 warnings
  C:\Users\werts-moltech\Documents\GitHub-mhvwerts\PyFVTool\pyfvtool\boundary.py:169:
DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error
in future. Ensure you extract a single element from your array before performing this operation.
(Deprecated NumPy 1.25.)
    BCRHS[G[i]] = -BC.left.c
mhvwerts commented 9 months ago

A potential fix might be to use the array method .item() without any arguments, which should return a scalar from a single-element array.

https://numpy.org/doc/stable/reference/generated/numpy.ndarray.item.html

simulkade commented 9 months ago

I just checked it with the new numpy. It happens only with 1D arrays, when the array has only a single item. The easiest way is to index into a, b, and c as a[0], b[0], and c[0].

mhvwerts commented 9 months ago

Indeed, that gives the same result as the item() method, if I am correct. The advantage of .item() (without arguments) is that it will raise an error if the array somehow would not have a single item. This should not happen, of course, but it may be a good protection against programming errors that would otherwise go unnoticed and lead to unexpected results.

simulkade commented 9 months ago

Just pushed the changes directly to Main (will create a pull request next time; just tired now :-)). The test did not create any warnings on my machine with the new version of numpy.

mhvwerts commented 9 months ago

It works for me as well. Great work. Many thanks.