Hi Oliver,
Thank you very much for your answer
I think I almost solve the problem. I attach you my code (see below), but there is an error that I can not solve easily. I'm using "guess_bonds()" to obtain the topology of the system, and this method return a tuple, but the hbond.run() try to access to u._topology.bonds.values that not exists and return the error below.
Anybody knows if is there a way to guess the bonds/topology of my system with the proper structure to apply hbond.run() ?
Thanks for your time
ciao
José
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_7860/3301902831.py in <module>
78 update_selections=False)
79
---> 80 hbonds.run(
81 start=None,
82 stop=None,
~/anaconda3/lib/python3.8/site-packages/MDAnalysis/analysis/base.py in run(self, start, stop, step, frames, verbose)
427 step=step, frames=frames)
428
logger.info("Starting preparation")
--> 429 self._prepare()
430
logger.info("Starting analysis loop over %d trajectory frames",
431 self.n_frames)
~/anaconda3/lib/python3.8/site-packages/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py in _prepare(self)
602 self._acceptors = self.u.select_atoms(self.acceptors_sel,
603 updating=self.update_selections)
--> 604 self._donors, self._hydrogens = self._get_dh_pairs()
605
606 def _single_frame(self):
~/anaconda3/lib/python3.8/site-packages/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py in _get_dh_pairs(self)
528 # This is because u.bonds also calculates properties of each bond (e.g bond length).
529 # See
https://github.com/MDAnalysis/mdanalysis/issues/2396#issuecomment-596251787--> 530 if not (hasattr(self.u._topology, 'bonds') and len(self.u._topology.bonds.values) != 0):
531 raise NoDataError('Cannot assign donor-hydrogen pairs via topology as no bond information is present. '
532 'Please either: load a topology file with bond information; use the guess_bonds() '
AttributeError: 'tuple' object has no attribute 'values'
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
from MDAnalysis.analysis.hydrogenbonds import HydrogenBondAnalysis
import MDAnalysis as mda
import matplotlib.pyplot as plt
%matplotlib inline
##############################
######## Tables of values ####
##############################
mda.topology.tables.vdwradii= {'1':1.1, '2':1.52, 'H1':1.1, 'H2':1.1, 'O': 1.52,}
mda.topology.tables.masses= {'1': 1.00794, '2': 15.9994, 'H1': 1.00794, 'H2': 1.00794, 'O': 15.999}
##############################
######## Universe variable ###
##############################
u = mda.Universe("lammps.data",topology_format="LAMMPSDUMP", dt=0.005, atom_style="id type xs ys zs")
all_atoms = u.select_atoms("type 1 2")
Hidrogenos = u.select_atoms("type 1")
Oxigenos = u.select_atoms("type 2")
###############################
########### Charges #########
###############################
u.add_TopologyAttr("charges")
Hidrogenos.charges = 0.41
Oxigenos.charges = -0.82
###############################
########### Masses #########
###############################
u.add_TopologyAttr("masses")
Hidrogenos.masses = 1.00794
Oxigenos.masses = 15.9994
###############################
########### Names #########
###############################
nameH=[]
cont=2
for ii in range(0,len(Hidrogenos)):
if(cont==2):
nameH.append('H1')
cont=1
else:
nameH.append('H2')
cont=2
nameO=[]
for ii in range(0,len(Oxigenos)):
nameO.append('O')
u.add_TopologyAttr("names")
Hidrogenos.names = nameH
Oxigenos.names = nameO
###############################
########### Residues ########
###############################
u.add_TopologyAttr("resnames")
all_atoms.residues.resnames='HOH'
###############################
########### Guess Bonds #####
###############################
u._topology.bonds=mda.topology.guessers.guess_bonds(atoms=all_atoms,coords=u.trajectory.ts.positions,box=u.dimensions)
###############################
####### Compute H bonds #######
###############################
hbonds = HydrogenBondAnalysis(universe=u,
donors_sel=None,
hydrogens_sel="name H1 H2",
acceptors_sel="name HOH",
d_a_cutoff=3.0,
d_h_a_angle_cutoff=150,
update_selections=False)
hbonds.run(
start=None,
stop=None,
step=None,
verbose=True
)
plt.plot(hbonds.times, hbonds.count_by_time(), lw=2)
plt.title("Number of hydrogon bonds over time", weight="bold")
plt.xlabel("Time (ps)")
plt.ylabel(r"$N_{HB}$")
plt.show()