Fix ValueError when passing selective_dynamics to Poscar - #2951
Conversation
|
Pls provide a unit test to confirm the new functionality. Thanks. |
|
@chiang-yuan Thanks! The test should go into |
|
Thanks @janosh ! I followed the original style to test by comparing two primitive python lists and converted numpy array back to list to assign variables. Since the system can be large and comparing two nested python lists is somewhat tricky, (For example, |
selective_dynamics to Poscar
| if selective_dynamics is not None: | ||
| selective_dynamics = np.array(selective_dynamics) | ||
| if not selective_dynamics.all(): | ||
| site_properties["selective_dynamics"] = selective_dynamics |
There was a problem hiding this comment.
@chiang-yuan I simplified the code some. Was there a reason to convert np.array to list before assigning to site_props?
There was a problem hiding this comment.
@janosh I converted np.array back to list because the other unit test in io/vasp/tests/test_inputs.py compare selective_dyanmics with list, which will yield the error in pytest like the one after your revision.
> assert poscar.selective_dynamics == [[True, True, True], [False, False, False]]
E ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
/home/runner/work/pymatgen/pymatgen/pymatgen/io/vasp/tests/test_inputs.py:87: ValueError
I bypass them by converting it back to list (which is what the original code intended to do) and adding another unit test comparing by list as well. Do you think it is better to revise the previous unit test and compare all of them in the format of nd.array like I suggested earlier?
There was a problem hiding this comment.
I think we should make the test datatype agnostic so that it handles both arrays and lists. It's better for tests to care only about semantics and not equivalent ways of encoding the same meaning.
Before this fix, users could not directly add a
selective_dynamicsarray to create aPoscarobject:and the code would produce an error:
A similar issue was reported previously about manually using
add_site_property.The minimal example reproducing the error and the bug fix is demonstrated in the attached notebook: poscar_debug.md
Summary
Include a summary of major changes in bullet points:
selective_dynamicsboolean array is given for creating aPoscarobjectvelocitiesandpredictor_corrector