Author Topic: Problem with export_file  (Read 191 times)

leila

  • Newbie
  • *
  • Posts: 3
Problem with export_file
« on: June 14, 2018, 04:21:03 PM »
Hi OVITO users,

I am very new to ovito. I tried to export the displacement magnitude of about 16000 atoms into txt file with this command:
export_file(node, "msd_data.%i.txt"%i,format = "txt", columns = ["Timestep", "MSD"], frame=i)

The problem is that it is just dumping 3 first and last numbers. For example:

# "Timestep" "MSD"
100 [ 0.2056347   0.39410666  0.37800705 ...,  0.33246541  0.34883693
  0.42290157]

I 'd appreciate your help.

Regards,
Leila

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 463
Re: Problem with export_file
« Reply #1 on: June 14, 2018, 05:10:15 PM »
Dear Leila,

Please say something about the "MSD" array. How is it generated? And does it represent per-particle information or something else?

It seems that the "MSD" field currently is a global attribute and not a particle property. That is because the "txt" file export format only allows to export global attributes but not per-particle data. If you would like to export particle/=-based information, then you have make sure that "MSD" is a particle property first, and then use the "xyz" export format.

-Alex

leila

  • Newbie
  • *
  • Posts: 3
Re: Problem with export_file
« Reply #2 on: June 14, 2018, 08:55:16 PM »
Thank you so much Alex.
You are right. I was trying to export global attribute. It is working now :)

Regards,
Leila

leila

  • Newbie
  • *
  • Posts: 3
Re: Problem with export_file
« Reply #3 on: June 14, 2018, 09:36:07 PM »
Dear Alex,

I got another error.
I am trying to calculate displacement of each atom with respect to previous step.  It is working well for small time steps. However, my simulation has 4*10^5 time steps and I dumped every 100 steps. SO in total I have 4000 dump file. It just prints out 505 files and then aborts with this error
RuntimeError: Could not detect the file format. The format might not be supported.
ERROR: The Python script 'samp.py' has exited with an error.

I was wondering whether I can print out all the displacements in 1 file? Now I am printing them in separate files.
My code is :

Code: [Select]
from ovito.io import import_file, export_file
from ovito.modifiers import PythonScriptModifier, CalculateDisplacementsModifier
import numpy
import os
import glob
import re
# Load input data and create an ObjectNode with a data pipeline.
path='/home/leila/Leila_sndhard/codes/gb_mobility_test/gb_mob/results_dE0.01/gb_ts100_T800/f.010/zdump'
node1 = import_file("/home/leila/Leila_sndhard/codes/gb_mobility_test/gb_mob/results_dE0.01/gb_ts100_T800/f.010/zdump/zdump.*.out", multiple_frames = True)
filenames = sorted(glob.glob('/home/leila/Leila_sndhard/codes/gb_mobility_test/gb_mob/results_dE0.01/gb_ts100_T800/f.010/zdump/zdump.*.out'))
def tryint(s):
    try:
        return int(s)
    except:
        return s

def alphanum_key(s):
    """ Turn a string into a list of string and number chunks.
        "z23a" -> ["z", 23, "a"]
    """
    return [ tryint(c) for c in re.split('([0-9]+)', s) ]

def sort_nicely(l):
    """ Sort the given list in the way that humans expect.
    """
    l.sort(key=alphanum_key)
sort_nicely(filenames)

n_f=node1.source.num_frames
print(n_f)
#print(filenames)
for i in range(n_f):
    node = import_file (filenames[i+1])

# Calculate per-particle displacements with respect to initial simulation frame:
    dmod = CalculateDisplacementsModifier()
    dmod.reference.load(filenames[i])
    node.modifiers.append(dmod)
    node.compute()
# Export calculated MSD value to a text file and let OVITO's data pipeline do the rest:
    export_file(node, "msd_data.%i.txt"%i,format = "xyz", columns = [ "Particle Identifier", "Particle Type", 'Displacement Magnitude'], frame=i)
Thanks in advance for your help,

Regards,
Leila
« Last Edit: June 15, 2018, 12:14:08 PM by Alexander Stukowski »

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 463
Re: Problem with export_file
« Reply #4 on: June 15, 2018, 12:24:18 PM »
Leila,

I noticed the following in your script:

You first import an entire file sequence, but then you import each individual file again in the for-loop, it seems:

Code: [Select]
node1 = import_file("/home/leila/Leila_sndhard/codes/gb_mobility_test/gb_mob/results_dE0.01/gb_ts100_T800/f.010/zdump/zdump.*.out", multiple_frames = True)
...
for i in range(n_f):
    node = import_file (filenames[i+1])

OVITO 2.9.0 contains a software bug, which leads to an I/O error when you call import_file() too often from a script (typically a few hundred times). This may be why you got the error message. The problem is fixed in the newer development versions.

But importing the file sequence just once should be enough in your case. Simply remove the import_file() call from the loop and use the existing pipeline node1 instead for processing each frame.

In fact, you don't even need the for-loop in the present case. The following lines should be enough (and will generate one big output file with all displacements for all frames, which you asked for):

Code: [Select]
node1 = import_file("/home/leila/Leila_sndhard/codes/gb_mobility_test/gb_mob/results_dE0.01/gb_ts100_T800/f.010/zdump/zdump.*.out", multiple_frames = True)
dmod = CalculateDisplacementsModifier()
dmod.reference.load(filenames[i])
node1.modifiers.append(dmod)
export_file(node1, "msd_data.txt",format = "xyz", columns = [ "Particle Identifier", "Particle Type", 'Displacement Magnitude'], multiple_frames = True)