LAMMPS WWW Site - LAMMPS Documentation - LAMMPS Mailing List Archives
[lammps-users] error when running python script to call lammps
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lammps-users] error when running python script to call lammps


From: Shenli Zhang <shlzhang@...584...>
Date: Mon, 18 Sep 2017 22:38:24 -0700

Dear Lammps users,

I've wrote a python script (attached) to call Lammps and do some analysis. However, I found the script runs fine if I directly use:
python script.py 

but will generate the following error if I use:
python script.py>script.output

Error message:

File "regional_diffusion_lammps_Pyversion.py", line 55, in <module>
    extract_y(12, 40.129, 64.129, 92.258, 2)
  File "regional_diffusion_lammps_Pyversion.py", line 21, in extract_y
    y=L.atoms[i].position[1]
  File "/share/apps/lammps-11Aug17/python/lammps.py", line 643, in atoms
    return AtomList(self)
  File "/share/apps/lammps-11Aug17/python/lammps.py", line 465, in __init__
    self.natoms = self.lmp.system.natoms
  File "/share/apps/lammps-11Aug17/python/lammps.py", line 648, in system
    d = self._parse_info_system(output)
  File "/share/apps/lammps-11Aug17/python/lammps.py", line 699, in _parse_info_system
    lines = output[6:-2]
TypeError: 'NoneType' object has no attribute '__getitem__'

Could you please help with this problem? Thank you!
--
Shenli Zhang

Graduate Student of Materials Science
University of California, Davis
# python function in Lammps to analyze MSD in different regions
# Lammps version for regional_diffusion.py
# Shenli Zhang, created 09/13/2017


# r1(2)l, r1(2)u marks the first(second) bulk boundary
def extract_y(r1l, r1u, r2l, r2u, looptime):
    import numpy as np
    from lammps import lammps, PyLammps
    lmp = lammps()
    L=PyLammps(ptr=lmp)
    L.file("lammps_continue_python.input")
    natoms = lmp.get_natoms()
    # generate a msd array with [id,type,yprevious,ydist,times,tot_times]
    msd = np.zeros((natoms, 6))
    outfile = "extract_ymsd.txt"
    f = open(outfile, 'a')
    for a in range(0, looptime):
        lmp.command("run 1")
        for i in range(0, msd.shape[0]):
            y=L.atoms[i].position[1]
            msd[i][0] = i+1
            msd[i][1] = L.atoms[i].type

            yp = msd[i][2]
            ydist = msd[i][3]
            times = msd[i][4]
            tot_times = msd[i][5]
            if (r1l < y < r1u or r2l < y < r2u) and yp != 0:
                msd[i][5] += msd[i][4]
                msd[i][4] = 0
                msd[i][2] = 0
            if y < r1l or (r1u < y < r2l) or y > r2u:
                if yp == 0:
                    msd[i][2] = y
                else:
                    msd[i][3] += abs(yp - y)
                    msd[i][4] += 1
                    msd[i][2] = y
            if a==looptime-1:
            	msd[i][5] += msd[i][4]

        if a % 1 == 0:
            f.write("ITEM: TIMESTEP \n")
            f.write("%d \n" % a)
            f.write("ITEM: ATOMS id type ydist tot_times \n")
            for item in msd:
                f.write("%d %d %.5f %d \n" %
                        (item[0], item[1], item[3],item[5]))
    f.close()
    lmp.command('write_restart production_723K_msd1.restart')
    lmp.command('print "all done!"')
    return

extract_y(12, 40.129, 64.129, 92.258, 2)