error in Python script with LAMMPS

Dear all,

I am trying to use python command within lammps. But I am finding error with the command line. I am using command like this:

variable oscillate python myvariable

python myvariable input 1 myN return v_vec format ii here “”"

def myvariable(step):



return v_vec

So basically I am confuse with line “python myvariable input 1 myN return v_vec format ii here “”” " which i got from LAMMPS input script. what 1, myN, ii, here and “”" is doing here. This is not very clear to me from manual.

I hope for your help.

Thank you so much in advance

best regards,

Pankaj

Dear all,

I am trying to use python command within lammps. But I am finding error
with the command line. I am using command like this:

variable oscillate python myvariable

python myvariable input 1 myN return v_vec format ii here """

def myvariable(step):
...
...
...
return v_vec

So basically I am confuse with line "python myvariable input 1 myN
return v_vec format ii here """ " which i got from LAMMPS input script. what
1, myN, ii, here and """ is doing here. This is not very clear to me from
manual.

I hope for your help.

you're not providing sufficient information. there is:
- no mentioning of the LAMMPS version
- no mentioning of the error message
- no mentioning whether the provided example in examples/python works
correctly and reproduces the bundled reference output.
- no explanation of what is "not very clear" the manual.

it looks like you are guessing (and wrongly so, e.g. the "return
v_vec" statement is hugely suspicious) and it also looks as if you
didn't spend enough care reading the docs (or didn't read them
completely).

axel.

Dear Axel,

Thank you for your reply. Here, I am providing the information you required.

I am using LAMMPS version LAMMPS - 10 Aug 2015.
ERROR: Invalid python command (…/python.cpp:131)

What not clear from manual is in the following command line:

python myvariable input 1 myN return v_vec format ii here “”"

what is “1”?

what is “myN”?

what is “v_vec” ?

Yes you are right. I am not sure about v_vec which I tried to guess.

Thank you

best regards,

Pankaj

Dear Axel,

Thank you for your reply. Here, I am providing the information you
required.

I am using LAMMPS version LAMMPS - 10 Aug 2015.
ERROR: Invalid python command (../python.cpp:131)

What not clear from manual is in the following command line:

python myvariable input 1 myN return v_vec format ii here """

what is "1"?
what is "myN"?
what is "v_vec" ?

Yes you are right. I am not sure about v_vec which I tried to guess.

why guess, when you have a documentation?

http://lammps.sandia.gov/doc/python.html

axel.

Dear Axel,

Thank you for your reply. Here, I am providing the information you
required.

I am using LAMMPS version LAMMPS - 10 Aug 2015.

also, i strongly recommend to upgrade to the latest development
version of LAMMPS.
we're made significant improvements to python support over the last
years (including bugfixes, of course).

axel.

Dear Axel,

Thank you for replying. I made some new attempts. I am using now LAMMPS version LAMMPS (30 Jul 2016) .

After reading the manual, I wrote new python command:

variable fact python oscillate
python oscillate input 1 SELF return v_fact format -f here “”"
def oscillate(step):
f=open(‘efield.txt’)
lines=f.readlines()
field = lines[step]
return field
"""

as i understood from manual:

1 refers to number of inputs for the python code.

SELF refers to input from lammps variable itself, which is step in my case.

v_fact is the output which I want.

-f for floating point.

My error is ERROR: Invalid python command.

Could you please suggest something?

best regards,

Pankaj

Dear Axel,

After reading the manual even more carefully, I made some modification which are as follows:

variable fact python oscillate
python oscillate input 1 v_step return v_fact format if here “”"
def oscillate(step):
f=open(‘thz-pump.txt’)
lines=f.readlines()
field = lines[step]
return field
"""

Now it is giving me error in " return field " as

Dear Axel,

After reading the manual even more carefully, I made some modification which
are as follows:

variable fact python oscillate
python oscillate input 1 v_step return v_fact format if here """
def oscillate(step):
        f=open('thz-pump.txt')
        lines=f.readlines()
        field = lines[step]
        return field
  """

Now it is giving me error in " return field " as
SyntaxError: invalid syntax
ERROR: Could not process Python string (../python.cpp:180)

I think that I am making mistake in getting output. I am not able to figure
out the error. I hope for your comments.

no. this is python error, so you have a syntax error in your python code.

that said, what you are trying to do here is a *very* bad idea. you
will read and parse the *entire* file on *every* step for *all*
processors. that will result in a *massive* slowdown and potentially
overload your machine (e.g. if your cluster has a shared networked
file system), specifically for long simulations, where you file will
have to contain hundreds of thousands, if not millions of lines.

axel.

Dear Axel,

I agree that simulation will be slowed down. However, I don’t have very big system and not long simulation with field, so I can take risk with this.

Now to the point of python error, same script of python is working properly to read a particular line of file. So, it seems some problem of reading step input and giving output from the code.

Hope for some solution to that.

regards,

Pankaj

Dear Axel,

I agree that simulation will be slowed down. However, I don't have very big
system and not long simulation with field, so I can take risk with this.

this doesn't agree with my experience of the behavior of such systems.
the impact of any electric field with a meaningful strength is
typically hidden by local fluctuations and thus you'll require a large
system and long trajectories for meaningful results.

Now to the point of python error, same script of python is working properly
to read a particular line of file. So, it seems some problem of reading step
input and giving output from the code.

no. your output already *shows* the problematic command.

at any rate, this is not a LAMMPS problem.

axel.

Dear Axel,

Sorry for not putting correctly. I meant input problem in python script which I introduced, not in lammps. However I made one more attempt:

variable fact python oscillate
python oscillate input 1 v_step return v_fact format if here “”"
def oscillate(step):
f=open(‘thz-pump.txt’)
lines=f.readlines()
return lines[step]
"""

Now the error is “ERROR: Could not evaluate Python function input variable (…/python.cpp:220)”. Do you think that step which is a global lammps variable, is being read here ? That’s what I meant by input problem.

regards,

Pankaj

Dear Axel,

Sorry for not putting correctly. I meant input problem in python script
which I introduced, not in lammps. However I made one more attempt:

variable fact python oscillate
python oscillate input 1 v_step return v_fact format if here """
def oscillate(step):
        f=open('thz-pump.txt')
        lines=f.readlines()
        return lines[step]
"""

Now the error is "ERROR: Could not evaluate Python function input variable
(../python.cpp:220)". Do you think that step which is a global lammps
variable, is being read here ? That's what I meant by input problem.

step is *NOT* a "global LAMMPS input variable". there is no such thing
in LAMMPS.

axel.

Dear Axel,

As mentioned in
http://lammps.sandia.gov/doc/thermo_style.html

step = timestep.

Dear Axel,

As mentioned in
http://lammps.sandia.gov/doc/thermo_style.html
step = timestep.

these are *NOT* variables but "thermo keywords". while they can be
used inside of equal or atom style variable expressions, they are not
variables.

you can see the list of currently defined variables by putting

info variables

into your input.

axel.

Hi Axel,

Thank you for your suggestions. That works for me. So I defined a new variable for steps. However, I stuck with a new problem. Now my simulations look like:

variable nsteps equal step
variable fact python oscillate
python oscillate input 1 v_nsteps return v_fact format if here “”"
def oscillate(nsteps):
f=open(‘test.txt’, ‘r’)
lines = f.readlines()
field = lines[nsteps]
print field
return field
“”"

thermo_style custom step temp press v_nsteps v_fact

Now if I see the “print field”, it is printing the right value from the file. But when I am using the variable “v_fact” in thermo_style, it is printing a random number “-1” only. I am finding where I am doing wrong. Hope for your suggestion.

Thank you

regards,

Panakj

Hi Axel,

Thank you for your suggestions. That works for me. So I defined a new
variable for steps. However, I stuck with a new problem. Now my simulations
look like:

variable nsteps equal step
variable fact python oscillate
python oscillate input 1 v_nsteps return v_fact format if here """
def oscillate(nsteps):
        f=open('test.txt', 'r')
        lines = f.readlines()
        field = lines[nsteps]
        print field
        return field
"""

thermo_style custom step temp press v_nsteps v_fact

Now if I see the "print field", it is printing the right value from the
file. But when I am using the variable "v_fact" in thermo_style, it is
printing a random number "-1" only. I am finding where I am doing wrong.
Hope for your suggestion.

your python function is returning a string, but you tell LAMMPS, that
it will receive a floating point number.

axel.

Dear Axel,

field is a floating point number. So as I am understanding, LAMMPS receives a floating point.

regards,

Pankaj

Dear Axel,

field is a floating point number. So as I am understanding, LAMMPS receives
a floating point.

it is a string representation of a floating point number. that is not the same.
add to your function:

print type(field)

and see for yourself.

axel.

Hi Axel,

Thanks a lot for help. I converted string to floating point and everything works fine.

regards,

Pankaj