Forum Navigation
You need to log in to create posts and topics.

How to remove modifiers



i am trying to calculate hydrogen bonding by using this part of the script:

# Create hydrogen bonds here:

modifier_bond = CreateBondsModifier()
modifier_bond.mode = CreateBondsModifier.Mode.Pairwise
# Create bond between water oxygen and water hydrogen ( create water )
# Create bond between water oxygen and oxygen
# Create bond between water oxygen and nitrogen
modifier_bond.lower_cutoff = 0.0


def hydrogen_bond(frame, data):

#Get the particle type property
particle_types = data.particles.particle_types
#Get the particle molecule id
molecule_id = data.particles['Molecule Identifier']
#Get bonds topology and particle positions to compute all bond vectors present in your system
topology = data.particles.bonds.topology
positions = data.particles.positions
bond_vectors = positions[topology[:,1]] - positions[topology[:,0]]
bond_vectors +=[:3,:3], data.particles.bonds.pbc_vectors.T).T
#Store this information as custom bond property, so they appear in the Data inspector
data.particles_.bonds_.create_property("bond_vectors", data = bond_vectors)
# Set up the bonds enumerator object.
bonds_enum = BondsEnumerator(data.particles.bonds)
# Counter
bond_count = 0
#Hydrogen bonded particles
hb_particles = []
#Hydrogen bonded water oxygens
hb_water = []
displacement_magnitudes = data.particles['Displacement Magnitude']
data.attributes["MSD"] = np.sum(displacement_magnitudes ** 2) / len(displacement_magnitudes)
# Loop over atoms.
for particle_index in range(data.particles.count):
# If the particle is a Hw atom continue and loop over bonds of current atom.
if particle_types[particle_index] == 4:
#Make sure that the current particle is connected
bond_indices_list = [bond_index for bond_index in bonds_enum.bonds_of_particle(particle_index)]
if len(bond_indices_list) >= 1:

neighbor_particle_indices = []
for bond_index in bonds_enum.bonds_of_particle(particle_index):
# Obtain the indices of the two particles connected by the bond:
a = topology[bond_index, 0]
b = topology[bond_index, 1]
if a == particle_index:

#Get covalent bond to Hw of same water Molecule (Ow-Hw)

for i in range(len(neighbor_particle_indices)):
if molecule_id[neighbor_particle_indices[i]] == molecule_id[particle_index]:
cov_bond_index = bond_indices_list[i]
cov_oxygen = neighbor_particle_indices[i]
cov_bond_vector = bond_vectors[bond_indices_list[i]]

#Delete covalent bond from bond_indices_list
#Delete covalent Ow from neighbor_particle_indices

for i in range(len(bond_indices_list)):
#Calculate angle between Donator-Hw and Hw-Acceptor
hb_vector = bond_vectors[bond_indices_list[i]]
angle = angle_between(cov_bond_vector, hb_vector,True)
cov_oxygen_coord = positions[cov_oxygen]
hb_coord = positions[neighbor_particle_indices[i]]
distance = distance_finder(cov_oxygen_coord,hb_coord)

if np.degrees(angle) >= 120 :

data.attributes["Ow--Hw..N"] = bond_count
data.attributes["HB Particles"] = hb_particles
data.attributes["HB Water"] = hb_water
data.attributes["MSD Type HB"] = np.mean(displacement_magnitudes[hb_water]**2)
data.attributes["MSD Type 7"] = np.mean(displacement_magnitudes[particle_types == 7]**2)


export_file(pipeline, "Y:/SIMULATIONS/01_diffusion/Hw..N.txt",
format = "txt/attr",
columns = ["Timestep", "Ow--Hw..N", "MSD Type HB","MSD Type 7","HB Water", "HB Particles"],
multiple_frames = True)

In my next step i want to remove the bond modifier with pipeline.modifiers.remove(modifier_bond) and do another bond modifier for other particles and another hydrogen bond function but i am getting this:

KeyError: "Bonds data object does not contain the property 'Periodic Image'."




you asked how to remove a modifier. This can be done using Python’s del statement for deleting an element from a list, e.g.

del pipeline.modifiers[0]

deletes the first modifier from the list.

Alternatively, you can use the enabled attribute of a modifier instance to turn it off, e.g.

pipeline.modifiers[2].enabled = False