Author Topic: how to create bonds accoding to bins  (Read 111 times)

fanli

  • Newbie
  • *
  • Posts: 5
how to create bonds accoding to bins
« on: March 23, 2018, 04:49:37 PM »
Dear Ovito users

I have to create bonds according the bins which are divided by z coordinates. The bonds should be only created for the atoms inside the same bins and there are  no bonds for the atoms in different bins. Then I have to access the bonds information in each bin of every frame for futher analysis. Can any one modify the script below a little bit to do so?

Code: [Select]
node = import_file("my.lammpstrj",multiple_frames = True)
node.add_to_scene()
compute_property_modifer=ComputePropertyModifier()
compute_property_modifer.output_property="bins"
compute_property_modifer.expressions=["rint(ReducedPosition.Z * 10 + 0.5)"]
create_bonds_modifier = CreateBondsModifier(cutoff=cutoff, mode=CreateBondsModifier.Mode.Pairwise)
create_bonds_modifier.set_pairwise_cutoff('Type 11', 'Type 12', cutoff)
node.modifiers.append(create_bonds_modifier)
node.modifiers.append(ComputeBondLengthsModifier())

fan li

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 348
Re: how to create bonds accoding to bins
« Reply #1 on: March 23, 2018, 09:08:13 PM »
I would make use of the intro_molecule_only option of the CreateBondsModifier. Normally, it is used to prevent bonds between different molecules. But you can abuse this option here, and make each bin a separate molecule first. This can be achieved by outputting the bin number computed by the ComputePropertyModifier to the "Molecule Identifier" particle property instead of the "bins" property:

Code: [Select]
node = import_file("my.lammpstrj",multiple_frames = True)
node.add_to_scene()
compute_property_modifer=ComputePropertyModifier()
compute_property_modifer.output_property="Molecule Identifier"
compute_property_modifer.expressions=["rint(ReducedPosition.Z * 10 + 0.5)"]
create_bonds_modifier = CreateBondsModifier(cutoff=cutoff, mode=CreateBondsModifier.Mode.Pairwise, intra_molecule_only=True)
create_bonds_modifier.set_pairwise_cutoff('Type 11', 'Type 12', cutoff)
node.modifiers.append(create_bonds_modifier)
node.modifiers.append(ComputeBondLengthsModifier())

-Alex
« Last Edit: March 23, 2018, 09:39:11 PM by Alexander Stukowski »

fanli

  • Newbie
  • *
  • Posts: 5
Re: how to create bonds accoding to bins
« Reply #2 on: March 26, 2018, 12:01:47 AM »
Hi -Alex
The way you used to divide bins would divide the all box (z direction) into to bins. However I just want to divide bins from Z2 to Z1. Using the expression
Code: [Select]
range(rint(Z2-Z1)+1)
 in python could do it.  But the compute_property_modifer.expressions does not support range function.

Can I do like this
Code: [Select]
k=range(rint(Z2-Z1)+1)
compute_property_modifer.expressions=["k"]

If not , are there other ways to do so?

Fan Li

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 348
Re: how to create bonds accoding to bins
« Reply #3 on: March 26, 2018, 09:48:39 AM »
Note that you can only use the math functions and operators listed in the table on this page in the compute expression:

http://www.ovito.org/manual/particles.modifiers.compute_property.html

You cannot use Python functions or variables within the formula. And, aside from that, I don't think Python's range() function would be the correct choice in this case anyway. It generates a sequence of integer numbers ranging from 0 to N-1.

Let's assume you want to divide the range of Z coordinates [Z1,Z2] into N bins of equal size, numbered 1 through N. Atoms outside of that Z range are to be assigned the special bin number 0. The following compute expression should do that for you:

Code: [Select]
Position.Z < Z1 ? 0 : (Position.Z >= Z2 ? 0 : rint((Position.Z - Z1) / (Z2-Z1) * N + 0.5) )

This formula maps Position.Z values from the [Z1,Z2] range to the [1,N] interval and performs rounding to integer numbers. Before doing so, the expression uses the ?: ternary operator to check if the Position.Z value is in the [Z1,Z2] range. If not, the result is set to 0. Note that you need to replace the constants N, Z1, Z2 with the desired numeric values in this expression string.