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)
“”"