OVITO create bonds crossing periodic boundary conditions using python

Dear OVITO users and developers,

I am using ovito to create bonds across pbc, and then export LAMMPS DATA file.
In GUI, it is easy to be done by enabling x, y, z pbc under Simulation Cell. However, I have a lot of systems to build so it would be super tedious to click click click.
So I am trying to write a python script to do that, but I met a problem in enabling pbc in python. Here is my code:

pipeline = import_file(trjs)
create_bonds_modifier = CreateBondsModifier(mode=CreateBondsModifier.Mode.Pairwise)
create_bonds_modifier.set_pairwise_cutoff(type_a=6,type_b=7,cutoff=1.01)
pipeline.modifiers.append(create_bonds_modifier)
data = pipeline.compute()

print(data.cell_.pbc) # output: False, False, False
print(data.particles.bonds.count) # output: wrong bond number

I noticed that ovito manual mentions this pbc problem, bond vectors?


but it does not mention how to use this bond vectors to create bonds.
Anyone can help me? Thanks!

Bests,
Jay

def set_pbc(frame,data):
    data.cell_.pbc = (True,True,True)

def create_bonds_ovito():
    pipeline = import_file(trjs)
    pipeline.modifiers.append(set_pbc)
    create_bonds_modifier = CreateBondsModifier(mode=CreateBondsModifier.Mode.Pairwise)
    create_bonds_modifier.set_pairwise_cutoff(type_a=6,type_b=7,cutoff=1.01)
    pipeline.modifiers.append(create_bonds_modifier)
    data = pipeline.compute()
    
    print(data.cell_.pbc) # output: True, True, True
    print(data.particles.bonds.count) # output: correct bond number

    export_file(data,'filename.data','lammps/data',atom_style='full')

I made some minor formatting changes to your code so that it can be used for future reference. I assume you successfully solved your problem?

Yes, it worked! Thanks!

1 Like