Skip to content

BUG: Fix `extxyz.py` for `LinearCombinationCalculator` etc.

Yuji Ikeda requested to merge yuzie007/ase:fix-extxyz-for-mix into master

As described in #1464 (closed), after 28a0a1f1 (!3257 (merged)), results of LinearCombinationCalculator cannot be dumped with write_extxyz.

from ase.build import bulk
from ase.calculators.emt import EMT
from ase.calculators.mixing import LinearCombinationCalculator

atoms = bulk("Cu")
atoms.calc = LinearCombinationCalculator([EMT()], [0.1])
atoms.get_potential_energy()
atoms.write("test.xyz")

gives

Traceback (most recent call last):
  File "/Users/ikeda/Documents/projects/ase/issues/1464/test.py", line 8, in <module>
    atoms.write("test.xyz")
  File "/Users/ikeda/codes/ase/ase/atoms.py", line 2019, in write
    write(filename, self, format, **kwargs)
  File "/Users/ikeda/codes/ase/ase/io/formats.py", line 693, in write
    return _write(filename, fd, format, io, images,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ikeda/codes/ase/ase/parallel.py", line 271, in new_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ikeda/codes/ase/ase/io/formats.py", line 729, in _write
    return io.write(fd, images, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ikeda/codes/ase/ase/io/formats.py", line 194, in _write_wrapper
    return function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ikeda/codes/ase/ase/utils/__init__.py", line 577, in iofunc
    obj = func(fd, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ikeda/codes/ase/ase/io/extxyz.py", line 914, in write_xyz
    comm, ncols, dtype, fmt = output_column_format(atoms,
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ikeda/codes/ase/ase/io/extxyz.py", line 752, in output_column_format
    dtype = array.dtype
            ^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'dtype'

The reason of this error is that, here all the other properties other than those in per_config_properties are treated as per-atom properties, but LinearCombinationCalculator has non-standard per-config properties like energy_contributions in calc.results.

The essentially same issue should actually occurs also for other calculators with non-standard calc.results keys like:

  • The present CastepCalculator stores, e.g., energy_without_dispersion_correction, hirshfeld_charges in calc.results
  • MOPAC has final_hof
  • Turbomole has many non-standard keys hessian matrix, normal modes, electric dipole moment, etc.

I indeed observed the same error also for Castep.

The present MR fixes this issue by explicitly re-defining standard per_atom_properties (deleted in a97e9630). A test for LinearCombinationCalculator + write_extxyz is also added.

cf. As I claimed in #1464 (closed) and !3308 (closed), while I basically like the additional functionality to write custom properties in .xyz files introduced in !3257 (merged), the implementation has a substantial performance deterioration; in write_xyz now by default it always create a copy of atoms just for dumping. I would like to have a separate MR for fixing this as well (by cutting it out from !3308 (closed)).

Checklist

Merge request reports