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

How to remove modifiers

Hello,

 

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 )
#modifier.set_pairwise_cutoff(4,7,1.1)
# Create bond between water oxygen and oxygen
#modifier.set_pairwise_cutoff(4,6,3.5)
# Create bond between water oxygen and nitrogen
modifier_bond.set_pairwise_cutoff(4,5,2.3)
modifier_bond.lower_cutoff = 0.0
pipeline.modifiers.append(modifier_bond)

pipeline.modifiers.append(CalculateDisplacementsModifier(minimum_image_convention=False))

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 += np.dot(data.cell[: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:
neighbor_particle_indices.append(b)
else:
neighbor_particle_indices.append(a)

#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
bond_indices_list.remove(cov_bond_index)
#Delete covalent Ow from neighbor_particle_indices
neighbor_particle_indices.remove(cov_oxygen)

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 :
bond_count+=1
hb_particles.append(neighbor_particle_indices[i])
hb_water.append(cov_oxygen)

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)

pipeline.modifiers.append(hydrogen_bond)

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'."

 

 

Hi,

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