Author Topic: Lammps data file bond property coloring  (Read 322 times)

btyukodi

  • Newbie
  • *
  • Posts: 6
Lammps data file bond property coloring
« on: December 29, 2018, 01:56:51 AM »
Dear Users,

Is there a possibility to color code bonds based on other property than "Bond Type", when loaded from a Lammps data file? Consider, for instance, the attached Lammps file. There I have 5 bond types, each of them have some property given in "Bond Coeffs". In Ovito, I add a "Color Coding" modifier, however, the only option to select for color coding is "Bond Type". I can modify my Lammps file if needed. What I ultimately need is a way to assign a floating number to each bond that I can then assign a color code to in Ovito.

Thank you very much for your help!
Botond

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 595
Re: Lammps data file bond property coloring
« Reply #1 on: December 29, 2018, 12:15:27 PM »
Hi,

Ovito's data file parser currently ignores the "Bond Coeffs" section in LAMMPS data files. There is no way to associate auxiliary information with bond types.

But I am wondering if this is really what you want anyway. It sounds like you would like to associate each bond with an individual property value, not each bond type. Right? Because that is also what would be needed as input for the "Color Coding" modifier. It operates on values of individual bonds, not bond types.

Let's say you first load in the bonds list from the LAMMPS data file, and you prepare a separate text file containing just the additional per-bond values which you would like to be available within Ovito. This second data file contains one line per bond and one numeric value per line. You can use a Python script modifier within Ovito to import this file (typically using the numpy.loadtxt() function) and assign it as a new property to the already loaded bonds. Subsequently, the new bond property will be available as input for the Color Coding modifier.

Do you think that approach would solve your problem? If so, I can help you with writing the Python script function for loading the bond property array and assigning it to the bonds.

-Alex

btyukodi

  • Newbie
  • *
  • Posts: 6
Re: Lammps data file bond property coloring
« Reply #2 on: December 29, 2018, 05:08:42 PM »
Hi again Alex,

That is correct, I'd like to associate each bond with an individual property value. I think your approach should work, my only concern is that property values change during the simulation. Currently I have a few thousands of Lammps files, each representing a frame. I assume then that I would need to assign to each frame a second data file containing bond properties. I could indeed use some help with such a script.

Alternatively, if you think this is easier, I could use another file type, it need not be Lammps data.
Thank you,
Botond

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 259
Re: Lammps data file bond property coloring
« Reply #3 on: January 04, 2019, 02:19:25 PM »
Hi Botond,
here is how you would do this with a python script modifier in the GUI of the latest developer version of OVITO. For each frame, this custom modifier will look for the corresponding text file called "bond_prop_<TIMESTEP>.dat" that has the information of the additional bond properties. So for the file "lammps_495000.dat" you uploaded it would have to be called "bond_prop_495000.dat" and would contain the following information according to your example case, right?

1 0.1
2 0.2
3 0.3
4 0.4
5 0.5
6 0.1
7 0.1
8 0.1
9 0.1
10 0.1
11 0.1

Here, the first column is the bond index and the second column the bond coefficient you specified.
After importing the additional information, you then only have to create a custom bond property which is then accessible to all modifiers that follow.

Code: [Select]
from ovito.data import *
import numpy as np

def modify(frame, data):
    #import bond_coeff file corresponding to the current frame
    myfile = "bond_prop_{}".format(data.attributes["SourceFile"].split("_")[1])
    bond_index, bond_coeffs = np.loadtxt(myfile, unpack = True)
   
    #create new bond property called 'coeff' and assign imported values
    data.bonds_.create_property('coeff', data = bond_coeffs)

Hope that helps,

Constanze
« Last Edit: January 04, 2019, 03:04:37 PM by Constanze Kalcher »

btyukodi

  • Newbie
  • *
  • Posts: 6
Re: Lammps data file bond property coloring
« Reply #4 on: January 04, 2019, 11:07:38 PM »
Hi Constanze,

Thank you for your answer. May I ask you which version of Ovito are you running? I'm running Ovito 2.9.0 and apparently have problems with your snippet.
When running as you provided, Python first complains  as follows:

Code: [Select]
The Python script has exited with an error.
modify() takes 2 positional arguments but 3 were given

So I modified the function signature and also print "data" for debugging:

Code: [Select]
from ovito.data import *
import numpy as np

def modify(frame, data, output):
    print(data)
    #import bond_coeff file corresponding to the current frame
    myfile = "bond_prop_{}".format(data.attributes["SourceFile"].split("_")[1])
    bond_index, bond_coeffs = np.loadtxt(myfile, unpack = True)
   
    #create new bond property called 'coeff' and assign imported values
    data.bonds_.create_property('coeff', data = bond_coeffs)

This returns the following:

Code: [Select]
DataCollection(['Simulation cell', 'Position', 'Particle Type', 'Particle Identifier', 'Bonds', 'Bond Type'])
The Python script has exited with an error.
Traceback (most recent call last):
  File "<string>", line 7, in modify
  File "/home/btyukodi/OVITO/bin/../lib/ovito/plugins/python/ovito/data/__init__.py", line 188, in __getitem__
    raise KeyError("DataCollection contains no attribute named '%s'." % key)
KeyError: "DataCollection contains no attribute named 'SourceFile'."

I suspect that the problem is that the data in the current version is loaded into "nodes" and "nodes" have a DataCollection attribute which is then passed to the modifier. The question is then how to access the node's data path from inside the modifier? I might be wrong though...

Thanks again,
Botond

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 259
Re: Lammps data file bond property coloring
« Reply #5 on: January 07, 2019, 10:50:22 AM »
Dear Botond,

as I said, it's the latest developer version available here:
http://ovito.org/index.php/download

Note that there have been some changes in the python API, which are documented here:
http://www.ovito.org/manual_testing/python/introduction/version_changes.html

In your case it's important to understand that OVITO 3.x no longer works with a half-bond representation, which will make it easier for you to handle the information of your custom bond property. More specifically, in OVITO 2.9 syntax you would have had to create the new bond property like this:
Code: [Select]
output.create_user_bond_property('coeff', data_type = "float", data = bond_coeffs)
where bond_coeffs, however, would have needed 22 entries instead of 11, since there are 22 half bonds e.g. [0 1], [1 0], [0 3], [3 0] ... etc. So if you insist on using OVITO 2.9 you'll need to take that into account when creating the additional files containing the bond property.
Another issue is that in OVITO 2.9 the file source is not an attribute of the DataCollection interface but was stored in FileSource.loaded_file which is however not accessible from within the python script modifier. So you will need to use a different way to load the correct bond_prop_*.dat file. If the numbering of your files follows the timestep, then you could for example calculate the current timestep from the current frame number input.attributes['SourceFrame'].

-Constanze





btyukodi

  • Newbie
  • *
  • Posts: 6
Re: Lammps data file bond property coloring
« Reply #6 on: January 08, 2019, 05:06:01 PM »
Dear Constanze,

My apologies for missing the version in your previous comment.
The problem with accessing my coeff files through the timestep is that this way I don't have the absolute data path and when running Ovito from a different directory, I always have to explicitly define my directory path.
I ended up switching to the latest dev version. It works better and your script works like a charm as well.

Thank you very much for all your help!
Best,
Botond