Long python strings get truncated in LAMMPS

Hello all,

When a string is “generated” from an embedded python function, it is truncated after its 64th character.

Here’s a MWE:
variable lmpstr python testPy
python testPy return v_lmpstr format s here “”"
def testPy():
pystr=“this is a very very very very extra long and even way longer string”
print “python: s" pystr
return pystr
“””
print “lammps: ${lmpstr}”

resulting in:
python: this is a very very very very extra long and even way longer string
lammps: this is a very very very very extra long and even way longer st

LAMMPS version: 17 Nov 2016, built with mpi

Python version: 2.7

Note: Actually, the string I need is a series of 50 pairs of bin/fraction, computed from a near-gaussian distribution. It is meant to be used as the “diam poly” argument in a “fix pour” command. e.g.:
fix ins all pour 3000 1 300719 vol 0.1 50 region slab diam poly 50 ${distr}
As a result, its length is typically larger than 1000.

Any advice/workaround would be appreciated.
Thank you.

Could it be due to the stdout buffer is not fully flushed?
If that’s the case, you can force it to flush by: sys.stdout.flush()

HTH,

Kasra.

The 64-len is hardwired in the code - see the top of src/PYTHON/python.cpp

and src/variable.cpp for the VALUELENGTH def.

It’s not as simple as changing that value b/c python-style variables were

really meant to mimic equal-style variables, and that 64-len field is where

the numeric result is represented as a string, which is always long enough.

Your use case is interesting, but not something we were thinking about.

What we probably need to do is allow an option, either with the variable

def, or the python command,

to specify how much space to alloc for that string (e.g. 1000 in your case).

Steve

Added a “length” keyword to the python command, so you

can specify the max returned string length from the Python func.

Your example now works.

Will be in the next patch, probably later today …

Thanks for posting a simple example script.

Steve

Works like a charm, thank you all!
Best whishes.