nikolasibalic / ARC-Alkali-Rydberg-Calculator

Object-oriented Python library for computation of properties of highly-excited Rydbeg states of alkali and divalent atoms.
https://atomcalc.org
BSD 3-Clause "New" or "Revised" License
90 stars 78 forks source link

Code crashes with "interactionsUpTo=2" #165

Closed rydburger closed 4 months ago

rydburger commented 7 months ago

Hello,

The code crashes after specifying "interactionsUpTo=2" and cannot be recovered. This happens with both the installation via pip and the precompiled downloadable release (v3.4.0)

calc = PairStateInteractions(..., interactionsUpTo=2)

The errors occur after I call calc.defineBasis(...) calc.diagonalise(...)

On closer inspection, the angularMatrix is not generated correctly and/or saved. Calling calc.savedAngularMatrix_matrix returns [], an empty list. The relevant part of the code seems to be

fileHandle = gzip.GzipFile( os.path.join(self.dataFolder, self.angularMatrixFile_meta), "wb" ) np.save(fileHandle, data) fileHandle.close()

calc.dataFolder returns 'C:\Users\(myname)\.arc-data', which does have angularMatrix.npy. But the error messages from calc.defineBasis(...) point to '~\miniconda3\envs\rydbergDefaultARC\Lib\site-packages\arc\calculations_atom_pairstate.py', which has its own angularMatrix.npy that is apparently updated to an empty list whenever calc.diagonalise is run.

This seems to be the source of the issue.

Could you please suggest a solution?

glsmail commented 6 months ago

I believe this issue is related to the NumPy 1.24.2 update (released Feb. 2023) that changed the behavior of NumPy arrays. The fix that works for me is to modify one line in __updateAngularMatrixElementsFile (line 555 of calculations_atom_pairstate.py), changing https://github.com/nikolasibalic/ARC-Alkali-Rydberg-Calculator/blob/36b86d1a43b3d24f93621ed669926b975494479c/arc/calculations_atom_pairstate.py#L555 to np.save(fileHandle, np.array(self.savedAngularMatrix_matrix,dtype=object))

This behavior is occurring because PairStateInteractions.savedAngularMatrix_matrix is a list of arrays with different lengths. When passed to numpy.save(), NumPy calls numpy.array() to convert the list to an NumPy array before saving. After the 1.24.2 update, NumPy arrays are no longer allowed to be inhomogenous or have different types within the array, so numpy.array() throws an unhandled ValueError exception when passed an inhomogenous list of arrays. The solution is to cast savedAngularMatrix_matrix as an array of objects, which recovers the intended behavior. I would also add code to handle ValueError exceptions in case this behavior is broken in future NumPy updates.

See also: https://stackoverflow.com/questions/60199316/how-to-save-a-list-of-numpy-arrays-into-a-single-file-and-load-file-back-to-orig

nikolasibalic commented 6 months ago

@glsmail this sounds very plausible. Thank you very much for digging into this! I will try to verify and make an update next weekend. I will update on the progress here.

rydburger commented 6 months ago

Thank you all.

Indeed, we had suspected that some kind of Numpy update might be responsible, as our (lazy) workaround was to revert to an older ARC release (from 2 years ago) and use a version of Numpy from that period.

Thanks for clarifying and pinpointing the problem. It'd be nice if this could be fixed in the official release.

nikolasibalic commented 4 months ago

Should be fixed in version 3.4.1 released now. Many thanks @gismail for resolving this issue.