dschick / udkm1Dsim

A Python Simulation Toolkit for 1D Ultrafast Dynamics in Condensed Matter
http://udkm1dsim.readthedocs.io
MIT License
21 stars 16 forks source link

print(AmorphousLayer) and print(UnitCell) fail #158

Closed ghlqz closed 1 month ago

ghlqz commented 1 month ago

I am currently starting to work with udkm1Dsim and encountered a problem in following the examples in the documentation. Printing an amorphous layer or a unit cell throws an error, "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". Some debugging showed that an array "output" is generated in the respective __str__ functions of the AmorphousLayer and UnitCell classes which cannot be handled by the tabulate command (it also does not have the proper np.shape() ).

The problematic array entries are actually inherited from the __str__ of the Layer class. There, some variables are not converted to strings. An example is the Debye-Waller factor, which remains a quantity object with unit which leads to the error.

A possible workaround is to replace ['Debye Waller Factor', self.deb_wal_fac.to('meter**2')], by ['Debye Waller Factor', '{:.4~P}'.format(1.0*self.deb_wal_fac.to('meter**2'))], in __str__ of the class Layer (and analogous for the other affected variables; the 1.0 factor converts the default integer value to a float because otherwise the format throws an error).

Is this behavior due to some library version conflict, or actually a bug? I am currently using

Python 3.11.5 NumPy 1.26.4 udkm1Dsim 2.0.2 tabulate 0.9.0 Pint 0.24.3

Thanks!

dschick commented 1 month ago

Hi @ghlqz,

Many thanks for reporting this issue. I actually encountered the same error just this morning - so many thanks for starting the debugging!

I will check my latest commits and changes in the dependencies to nail this down asap.

Best

Daniel

dschick commented 1 month ago

If I remember correctly, this problem was introduced with one of the latest numpy version, which handles boolean operations on arrays differently. See also this issue in the tabulate project: https://github.com/astanin/python-tabulate/issues/339

I will try, if I can implement a simple workaround or otherwise switch to a more recent converter for tabular output.

Best

Daniel

dschick commented 1 month ago

@ghlqz I will follow your proposal and convert all quantities to strings before handing them over to the tabulate function.

dschick commented 1 month ago

I created an PR #159 which should fix this issue. Could you please test it?

dschick commented 1 month ago

closing