Help! how do i create multiple pylammps instances WITHOUT any log output?

I want to create two PyLammps objects, here is my code:

# MD setting
cmdargs = ["-log", "none", "-screen", "none"] # args: https://docs.lammps.org/latest/Run_options.html

lmp1 = PyLammps(cmdargs = cmdargs)
lmp2 = PyLammps(cmdargs = cmdargs)
basis_timestep = 0.1
ntimestep = 10

but the code report a error as follows:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[8], line 5
      2 cmdargs = ["-log", "none", "-screen", "none"] # args: https://docs.lammps.org/latest/Run_options.html
      4 lmp1 = PyLammps(cmdargs = cmdargs)
----> 5 lmp2 = PyLammps(cmdargs = cmdargs)
      6 basis_timestep = 0.1
      7 ntimestep = 10

File ~/miniconda3/envs/lammps/lib/python3.11/site-packages/lammps/pylammps.py:453, in PyLammps.__init__(self, name, cmdargs, ptr, comm, verbose)
    451     self.lmp = lammps(name=name,cmdargs=cmdargs,ptr=ptr,comm=comm)
    452 else:
--> 453   self.lmp = lammps(name=name,cmdargs=cmdargs,ptr=None,comm=comm)
    454 print("LAMMPS output is captured by PyLammps wrapper")
    455 self._cmd_history = []

File ~/miniconda3/envs/lammps/lib/python3.11/site-packages/lammps/core.py:428, in lammps.__init__(self, name, cmdargs, ptr, comm)
    426 # check if library initilialization failed
    427 if not self.lmp:
--> 428   raise(RuntimeError("Failed to initialize LAMMPS object"))
    430 # optional numpy support (lazy loading)
    431 self._numpy = None

RuntimeError: Failed to initialize LAMMPS object

if i set the cmdargs to None in the code block, it works. what’s wrong with my code?

Using PyLammps for anything but demonstrations is not a good idea. You cannot turn of output in PyLammps since it captures is and uses the data for some its functionality (this is why it is so fragile and sometimes slow).
Better to use the regular lammps python module where turning off log and screen should not be a problem.

But after I changed Pylammps instance to lammps instance, the program still reported the same error.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[8], line 5
      2 cmdargs = ["-log", "none", "-screen", "none"] # args: https://docs.lammps.org/latest/Run_options.html
      4 lmp1 = lammps(cmdargs = cmdargs)
----> 5 lmp2 = lammps(cmdargs = cmdargs)
      6 basis_timestep = 0.1
      7 ntimestep = 10

File ~/miniconda3/envs/lammps/lib/python3.11/site-packages/lammps/core.py:428, in lammps.__init__(self, name, cmdargs, ptr, comm)
    426 # check if library initilialization failed
    427 if not self.lmp:
--> 428   raise(RuntimeError("Failed to initialize LAMMPS object"))
    430 # optional numpy support (lazy loading)
    431 self._numpy = None

RuntimeError: Failed to initialize LAMMPS object

it works for me with this:

Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from lammps import lammps
>>> args1 = ['-log', 'none', '-screen', 'none']
>>> lmp1 = lammps(cmdargs=args1)
>>> args2 = ['-log', 'none', '-screen', 'none']
>>> lmp2 = lammps(cmdargs=args2)
>>> lmp1.close()
>>> lmp2.close()
>>> 

OHHHHH,thanks for your help. i know where is the problem.
if i use cmdargs1 and cmdargs2 and feed two different instances respectively, it works.

cmdargs1 = ["-log", "none", "-screen", "none"] # args: https://docs.lammps.org/latest/Run_options.html
cmdargs2 = ["-log", "none", "-screen", "none"]
lmp1 = lammps(cmdargs = cmdargs1)
lmp2 = lammps(cmdargs = cmdargs2)

if i use cmdargs and feed into two different instances, it reported the Failed to initialize error.

# MD setting
cmdargs = ["-log", "none", "-screen", "none"] # args: https://docs.lammps.org/latest/Run_options.html 
lmp1 = lammps(cmdargs = cmdargs)
lmp2 = lammps(cmdargs = cmdargs)
basis_timestep = 0.1
ntimestep = 10

is this a bug?

More likely an undocumented feature.

To be passed to C++ strings need to be “encoded” into byte sequences and you cannot encode the same string twice.

OHHH, exactly. after creating the lmp1, cmdargs list becomes [b'lammps', b'-log', b'none', b'-screen', b'none'].
THANK YOU SO MUCH. the problem can be closed.

I made some changes to the python/core.py file so that this transformation doesn’t happen anymore on the cmdargs argument but on a copy.

For good measure, I also allowed to pass byte arrays and set up the code to throw a TypeError in case an integer or float is (incorrectly) passed instead.

Ok. But i have another problem.
The problem is, if i set cmdargs to None and try to print simulation.system. i will get a correct system information about the simulation. some details is as follows:

# Code
def create_simulation(timestep = 0.2, cmdargs = None) -> IPyLammps:
    MDsimulation = IPyLammps(cmdargs = cmdargs)
    ....

cmdargs = None
simulation = create_simulation(0.02, cmdargs)
print(simulation.system)


# Output
LAMMPS (7 Feb 2024 - Update 1)
OMP_NUM_THREADS environment is not set. Defaulting to 1 thread. (src/comm.cpp:98)
  using 1 OpenMP thread(s) per MPI task
LAMMPS output is captured by PyLammps wrapper
System(dimensions=3, xlo=-13.913, ylo=-17.60585, zlo=-10.3312, xhi=13.913, yhi=17.60585, zhi=10.3312, triclinic_box=(27.826, 35.2117, 20.6624), nangles=1621, nangletypes=18, angle_style='harmonic', nbonds=1621, nbondtypes=18, bond_style='harmonic', ndihedrals=200, ndihedraltypes=40, dihedral_style='opls', nimpropers=40, nimpropertypes=8, improper_style='cvff', kspace_style='pppm', natoms=1232, ntypes=18, pair_style='lj/cut/coul/long', atom_style='full', units='real', atom_map='array', molecule_type='standard', boundaries='p,p p,p p,p')

but if i set cmdargs to ["-log", "none", "-screen", "none"], the programme will raise a error

# code
def create_simulation(timestep = 0.2, cmdargs = None) -> IPyLammps:
    MDsimulation = IPyLammps(cmdargs = cmdargs)
    ....
# cmdargs = None
cmdargs = ["-log", "none", "-screen", "none"]
simulation = create_simulation(0.02, cmdargs)
print(simulation.system)

# Output
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[9], line 4
      2 cmdargs = ["-log", "none", "-screen", "none"]
      3 simulation = create_simulation(0.02, cmdargs)
----> 4 print(simulation.system)

File ~/miniconda3/envs/lammps/lib/python3.11/site-packages/lammps/pylammps.py:627, in PyLammps.system(self)
    619 @property
    620 def system(self):
    621   """
    622   The system state of this LAMMPS instance
    623 
    624   :getter: Returns an object with properties storing the current system state
    625   :type: namedtuple
    626   """
--> 627   output = self.lmp_info("system")
    628   output = output[output.index("System information:")+1:]
    629   d = self._parse_info_system(output)

File ~/miniconda3/envs/lammps/lib/python3.11/site-packages/lammps/pylammps.py:822, in PyLammps.lmp_info(self, s)
    818 def lmp_info(self, s):
    819     # skip anything before and after Info-Info-Info
    820     # also skip timestamp line
    821     output = self.__getattr__("info")(s)
--> 822     indices = [index for index, line in enumerate(output) if line.startswith("Info-Info-Info-Info")]
    823     start = indices[0]
    824     end = indices[1]

TypeError: 'NoneType' object is not iterable

could you help me solve the problem?

I already told you that turning off the screen output is a no-no with PyLammps. It depends on this information and it is a bad idea to use PyLammps for anything but demonstrations.
Please note that I find it quite irritating to have to give the same advice multiple times.

i apologize for my mistake. Perhaps due to research pressure and working too late, I was unable to understand your meaning correctly at that time.

THANK YOU SO MUCH AGAIN.