How to write data in binary file with python

Dear all

I try to write results in a binary file by using a python function (see below the function - file = open(fichier,“ab”)). However, the file is written in ascii format. Same problem if I use the command open(fichier,“b”) and open(fichier,“wb”).
The string “aw” seems not be recognized. “a” alone must be used to write data in ascii at the end of a file.

I use Lammps version 2Aug2023 with gcc8.1, openmpi 3.1.4 and python 3.8.6 in virtual environment.

python function :

python end_of_step_callback here “”"

from future import print_function
from lammps import lammps

def end_of_step_callback(lmp):

try:

import os
import struct


pid = os.getpid()

fich_1 = 'resultat_'
fich_2 = str(pid)
fichier = fich_1+fich_2+'.dat'


L = lammps(ptr=lmp)
t = L.extract_global("ntimestep", 0)
print(pid_prefix, "### END_OF_STEP ###", t)


nlocal = L.extract_global("nlocal")

id    = L.extract_atom("id")
types = L.extract_atom("type")
x     = L.extract_atom("x")
v     = L.extract_atom("v")
c_dsp = L.extract_compute("dsp",1,2)

print("ouverture fichier binaire" , fichier)
file = open(fichier,"ab")

n_he = 0
for i in range(nlocal):
  if (types[i] == 5):
    n_he = n_he + 1   

file.write( struct.pack(“l”,t )

file.write( struct.pack(“l”,n_he )

valeur_t = " %d d \n " (t,n_he)

file.write(valeur_t)

for i in range(nlocal):
  if (types[i] == 5):
  
    print(pid,id[i],types[i],x[i][0],x[i][1],x[i][2],v[i][0],v[i][1],v[i][2])
    print(pid,id[i],types[i],c_dsp[i][0],c_dsp[i][1],c_dsp[i][2])
    
    file.write( struct.pack("l",id[i]) )
    file.write( struct.pack("l",types[i]) )
    file.write( struct.pack("d",c_dsp[i][0]) )
    file.write( struct.pack("d",c_dsp[i][1]) )
    file.write( struct.pack("d",c_dsp[i][2]) )
    file.write( struct.pack("d",v[i][0]) )
    file.write( struct.pack("d",v[i][1]) )
    file.write( struct.pack("d",v[i][2]) )

valeurs = " %d %d %16.10f %16.10f %16.10f %16.10f %16.10f 16.10f \n " (id[i],types[i],c_dsp[i][0],c_dsp[i][1],c_dsp[i][2],v[i][0],v[i][1],v[i][2])

file.write(valeurs)

file.close()

except Exception as e:
print(e)

“”"

A few comments:

  • you are not properly quoting your input, so the forum software makes a mess of it trying to interpret special characters as formatting instructions. You need to use triple backquotes to avoid that (```). See the forum guidelines.
  • your problem is not really a LAMMPS problem, but a python problem.
  • you need to keep in mind that the data returned from the LAMMPS module functions is provided as ctypes style data types and specifically arrays. You are treating them like python lists and that may be a mistake. I am not enough of a python/ctypes expert to tell you for certain, but just me pointing this out should help you look for the correct documentation and help in the correct places.

P.S.: your code would be more readable to non-native speakers, if everything in it was written in English (comments, print statements, variable names). This is an international forum using English as the language for communication. This should be in your own interest: the better people can understand what you are trying to do, the bigger are your chances to get useful and constructive advice.

The display problem comes from the use of notepad++ to insert the python function part in the mail. I do not know how to avoid these typing errors.

The data written with dump and with the python function in ascii format are the same. I will go through the documentation to get informations. However, I do not think that it is a problem of treating the data.

Another problem : when using the example in.fix_python invoke_neighlist, Lammps writes the message " No module named ‘numpy’ ".

No, it is not notepad++, it is you not quoting text correctly. I have briefly said how to correct this and a long explanation is here Please Read This First: Guidelines and Suggestions for posting LAMMPS questions

I disagree. You are not paying attention to what I explained.

Well, the way to resolve this is very straightforward: you must install the NumPy python module into your virtual environment (since the LAMMPS Python module uses NumPy)

Update: I cannot reproduce this. I just created a new virtual environment, installed the LAMMPS module and checked that NumPy was not installed in the virtual environment, and the in.fix_python_invoke example completes without an error. You must have made some modifications somewhere.

In fact, in my script I used a jump to an input file which writes data in ascii format with a python function.

After correction and using the command “file = open(‘file_name’,‘b’)” in the python function without writing anything in the file, I get the following message: ‘Must have exactly one of create/read/write/append mode and at most one plus’. It seems, but I may be wrong, that the binary mode is not implemented.

I have to repeat: this is a python problem and not a LAMMPS problem and thus you are asking your questions in the wrong forum.