# -*- coding: utf-8 -*-
"""Contains a class for using the shortest bond length to a group of atoms as a CV."""
import numpy as np
from .. import constants as c
from . import cvfunctions as cvf
from .cv import cv
[docs]class cv_shortestbond(cv):
"""
Shortest bond length between pairs of atoms as collective variable, inherits from \
`metafalcon.cvs.cv.cv`.
Parameters
----------
idx : int
index of collective variable in the input file
symbols : list of str
atomic symbols of the molecule
**kwargs :
See below
Keyword Arguments
-----------------
atom1 : int
fixed atom index for the calculation of the bond length
atom2 : list of int
list of atom indices to be used as second atoms for the calculation of the bond length
"""
def __init__(self, idx, symbols, **kwargs):
"""Construct shortest bond length CV object."""
cv.__init__(self, idx, symbols, **kwargs)
self.atom1 = self.get_kwargs("atom1") # atom index
self.atom2 = self.get_kwargs("atom2") # list of atom indices
self.x = np.linspace(0.5, 2.5, 200)
self.ticks = None
with open("bond.dat", "w") as f:
f.write("# first index: %d, second index:\n" % self.atom1)
self.au2unit = c.a0
self.reset_width()
[docs] def set_s_and_ds(self, coords):
"""
Calculate bond length and its gradient for the current set of coordinates.
Parameters
----------
coords : np.2darray
cartesian coordinates of the molecule (shape: (N, 3))
"""
bonds = [cvf.bond(coords[[self.atom1, at2]]) for at2 in self.atom2]
bond = min(bonds)
at2 = self.atom2[bonds.index(bond)]
with open("bond.dat", "a") as f:
f.write(str(at2)+"\n")
dbond = np.zeros(coords.shape)
dbond[[self.atom1, at2]] = cvf.dbond(coords[[self.atom1, at2]])
self.s = bond
self.ds_dr = dbond