Why the "ase.io.read()" doesn't work?

Hi,
I am trying to open the “dump” type file via the ase.io module, as follows:

system = ase.io.read(solvent_polycrystal,format='lammps-dump-text')

However, there is always an unboundlocal error. Can somebody tell me how to solve it? Many thanks in advance.

E:\Anaconda3\envs\res\lib\site-packages\ase\io\lammpsrun.py in read_lammps_dump_text(fileobj, index, **kwargs)
    315             datarows = [lines.popleft() for _ in range(n_atoms)]
    316             data = np.loadtxt(datarows, dtype=str)
--> 317             out_atoms = lammps_data_to_ase_atoms(
    318                 data=data,
    319                 colnames=colnames,

E:\Anaconda3\envs\res\lib\site-packages\ase\io\lammpsrun.py in lammps_data_to_ase_atoms(data, colnames, cell, celldisp, pbc, atomsobj, order, specorder, prismobj, units)
    212                                 dtype='float')
    213 
--> 214     return out_atoms
    215 
    216 

UnboundLocalError: local variable 'out_atoms' referenced before assignment

Hi Jason,

Can you please post the LAMMPS dump file you’re trying to read?

Thanks,
Dan

Hi Dan,

Thanks for your reply.
The file I used is posted as below (new users can’t upload files…):

Best,
Jason

ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
483425
ITEM: BOX BOUNDS pp pp pp
0.0 200.7890015
0.0 200.7890015
0.0 200.7890015
ITEM: ATOMS x y z id type
21.8274994 197.4550019 44.6883011 1 1
21.7682991 197.1600037 40.7210007 2 1
22.8279 195.852005 46.8930016 3 1
18.2180996 195.4859925 44.9457016 4 1
19.8955994 196.0820007 38.7895012 5 1
21.6170998 196.9219972 36.6444016 6 1
18.0837994 194.9889984 36.8506012 7 1
19.8141003 195.8200073 34.7117005 8 1
18.0203991 194.723999 32.7635994 9 1
17.9657994 194.4600067 28.6474991 10 1
16.3405991 194.1529999 38.9826012 11 1
16.2854996 193.8970032 34.8989984 12 1
14.4982004 192.8079987 32.9371986 13 1
16.2362995 193.6349945 30.7966995 14 1

Since your dump file was so large, I see that it was truncated (either by you or the forum software) after 14 atoms were listed. So, I changed the number of atoms from 483425 → 14:

ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
14
ITEM: BOX BOUNDS pp pp pp
0.0 200.7890015
0.0 200.7890015
0.0 200.7890015
ITEM: ATOMS x y z id type
21.8274994 197.4550019 44.6883011 1 1
21.7682991 197.1600037 40.7210007 2 1
22.8279 195.852005 46.8930016 3 1
18.2180996 195.4859925 44.9457016 4 1
19.8955994 196.0820007 38.7895012 5 1
21.6170998 196.9219972 36.6444016 6 1
18.0837994 194.9889984 36.8506012 7 1
19.8141003 195.8200073 34.7117005 8 1
18.0203991 194.723999 32.7635994 9 1
17.9657994 194.4600067 28.6474991 10 1
16.3405991 194.1529999 38.9826012 11 1
16.2854996 193.8970032 34.8989984 12 1
14.4982004 192.8079987 32.9371986 13 1
16.2362995 193.6349945 30.7966995 14 1

I was able to successfully load this file into ase version 3.22.0b1 via a file object:

Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ase.io
>>> with open("lammps.dump") as lammps_dump_fl_obj:
...     atoms = ase.io.read(lammps_dump_fl_obj, format="lammps-dump-text")
... 
>>> print(atoms)
Atoms(symbols='H14', pbc=True, cell=[200.7890015, 200.7890015, 200.7890015])

Supplying a the file path as a string also worked:

Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ase.io
>>> atoms = ase.io.read("lammps.dump",format='lammps-dump-text')
>>> print(atoms)
Atoms(symbols='H14', pbc=True, cell=[200.7890015, 200.7890015, 200.7890015])

The failure you’re observing is probably due to your solvent_polycrystal variable not being a file object or file path, or maybe due to an old (possibly broken) version of ASE or by something else further down in the dump file that got truncated. You’d need to provide a more complete minimal working example to we can reproduce this.

That being said, UnboundLocalError is always a sign of improperly written source code. You can see in the latest commit on master that there’s a if-else statement that serves to define the variable out_atoms in the local namespace. In your case, somehow none of the if conditions triggered, and so out_atoms never got assigned. I’ll open a MR to add an exception to catch this case and give a more expressive error message.