Author Topic: Save the distance between two particles  (Read 922 times)

Ramón Gutiérrez Trujillo

  • Newbie
  • *
  • Posts: 6
Save the distance between two particles
« on: August 24, 2018, 07:13:12 PM »
Hi, good afternoon.

How I can save the distance between two particles in my movie?, I can to see the data, but I don't know if exist the form to save it to a file.



The Distance parameter in the image is the information for the file.
« Last Edit: August 24, 2018, 09:46:45 PM by ramonjgt »

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 113
Re: Save the distance between two particles
« Reply #1 on: August 27, 2018, 01:53:44 PM »
Hi ramonjgt,

you could use a Python script modifier to calculate the distance between the two particles, e.g. between particle 1 and 2
Code: [Select]
from ovito.data import *
import numpy as np
def modify(frame, input, output):
    distance = np.linalg.norm( input.particles["Position"][1]-input.particles["Position"][2] )
    print (distance)
    output.attributes["Distance"] = distance

and save that information as a global attribute.
Then this information is available in the Text label overlay function as [Distance].

-Constanze

Ramón Gutiérrez Trujillo

  • Newbie
  • *
  • Posts: 6
Re: Save the distance between two particles
« Reply #2 on: August 28, 2018, 02:44:27 AM »
Thanks for your answer, but exist the form to save the Distance in a file?
This is the important for me, because I have a thousands of frames, and I wanna to save the distance between two particles, in each frame, for after obtain the average of all them.

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 500
Re: Save the distance between two particles
« Reply #3 on: August 28, 2018, 07:23:00 AM »
Yes, global attributes like the one this Python function computes can be saved to a text file as a tabulated function of time. See here:

https://www.ovito.org/manual_testing/usage.export.html#usage.global_attributes

Ramón Gutiérrez Trujillo

  • Newbie
  • *
  • Posts: 6
Re: Save the distance between two particles
« Reply #4 on: August 29, 2018, 06:52:28 AM »
Thanks a lot   :D  ;D

I tried that in the past, but only appear the Distance attribute when run the script.

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 500
Re: Save the distance between two particles
« Reply #5 on: August 29, 2018, 07:51:16 AM »
I have attached a screenshot for you showing how to set up the Python script modifier. Please compare with your own setup.

The "Attributes" tab of the data inspector at the bottom of the main window lists all global attributes that have been produced by the current pipeline, including the the "Distance" attribute coming from our user-defined modifier function. All these global attributes are also available for export to a text file when you select the "Table of values" output format in the file selection dialog (not shown in the screenshot) that pops up first when you select "Export file" from the menu.

(Note: The screenshot image is very large and might not display well on this web page. Better click on the image filename to download the screenshot to your computer for viewing it there.)
« Last Edit: August 29, 2018, 07:54:13 AM by Alexander Stukowski »

Amir Dehghan

  • Newbie
  • *
  • Posts: 11
Re: Save the distance between two particles
« Reply #6 on: September 19, 2018, 08:48:27 PM »
Hi Alexander,
I used the instructions that you attached a screenshot.
But the results are different!
I have attached a screenshot for comparison.
I use this script:
from ovito.data import *
import numpy as np
def modify(frame, input, output):
    distance = np.linalg.norm( input.particles["Particle Identifier"][655]-input.particles["Particle Identifier"][679] )
    print (distance)
    output.attributes["Distance"] = distance
« Last Edit: September 19, 2018, 08:51:57 PM by Amir Dehghan »

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 500
Re: Save the distance between two particles
« Reply #7 on: September 19, 2018, 09:42:38 PM »
Amir,

Note that in your Python script you are subtracting two particle identifiers instead of two particle positions:

Code: [Select]
distance = np.linalg.norm( input.particles["Particle Identifier"][655] - input.particles["Particle Identifier"][679] )

The expression "input.particles["Particle Identifier"][655]" gives you the numeric ID of the 655th particle in the particles list. Obviously, it doesn't make sense to subtract two IDs. What you want, is the difference between two particle positions. XYZ coordinates are stored in the "Position" particle property:

Code: [Select]
distance = np.linalg.norm( input.particles["Position"][655] - input.particles["Position"][679] )

But note that this will subtract the position of the 679th particle from the 655th particle. But these are probably not the ones you are interested in. There is a difference between particle identifiers and particle indices. The ID is an explicit property of each particle, which sticks to a particle even if the storage order of particles changes or when you delete some particles. The particle index, on the other hand, is an implicit property since the index only specifies the position of the particle within the current list of particles. If you delete some particles using the Slice modifier or the Delete Selected modifier in OVITO, the indices of the remaining particles typically change, because the indices always form a consecutive sequence from 0 to N-1, where N is the current number of particles.

You can find the particle IDs in the "Particle Identifier" column of the particle data inspector (see your screenshot). Particle indices are shown in the left most column of the table.

(As a side note: The simulation code LAMMPS assigns unique IDs to atoms ranging from 1 to N. But during a simulation run, it tends to reorder particles and when they are written to a dump snapshot, the current ordering is typically random (you can change that behavior using the dump_modify sort command). That's why there is generally no well-defined relationship between the ID of a particle and its index, i.e. its position in the list of particles loaded from the dump file).

You want to compute the distance between the two particles with IDs 655 and 679. However, in Python you can access particles only by index! That means you first have to determine the indices of the two particles before you can access their position vectors. You do it as follows:

Code: [Select]
index1 = np.nonzero(input.particles["Particle Identifier"] == 655)[0][0]
index2 = np.nonzero(input.particles["Particle Identifier"] == 679)[0][0]
distance = np.linalg.norm( input.particles["Position"][index1] - input.particles["Position"][index2] )

Note that the np.nozero() function is the typical Numpy way of finding the first occurrence of a particular value (in this case the ID) in an array. In other words, it searched for the value 655 in the ID array and gives back the 0-based index. This index can then be used to access the corresponding value in the coordinates array.

Amir Dehghan

  • Newbie
  • *
  • Posts: 11
Re: Save the distance between two particles
« Reply #8 on: September 19, 2018, 11:39:32 PM »
Thanks alot.