def __init__(self, system, term, other_terms, cart_penalty=1e-3 * angstrom): ''' A class deriving from the Yaff ForceField class to implement the strain of a molecular geometry associated with the term defined by term_index. **Arguments** system A Yaff System instance containing all system information. term a Term instance representing the term of the perturbation trajectory of the current strain other_terms a list of Term instances representing all other terms for ICs for which a strain contribution should be added **Keyword Arguments** cart_penalty Magnitude of an extra term added to the strain that penalises a deviation of the cartesian coordinates of each atom with respect to the equilibrium coordinates. This penalty is equal to norm(R-R_eq)**2/(2.0*3*Natoms*cart_penalty**2) and prevents global translations, global rotations as well as rotations of molecular fragments far from the IC under consideration. ''' self.coords0 = system.pos.copy() self.ndof = np.prod(self.coords0.shape) self.cart_penalty = cart_penalty self.cons_ic_atindexes = term.get_atoms() #construct main strain strain = ForcePartValence(system) for other in other_terms: if other.kind == 3: continue #no cross terms strain.add_term(Harmonic(1.0, None, other.ics[0])) #set the rest values to the equilibrium values strain.dlist.forward() strain.iclist.forward() for iterm in range(strain.vlist.nv): vterm = strain.vlist.vtab[iterm] ic = strain.iclist.ictab[vterm['ic0']] vterm['par1'] = ic['value'] ForceField.__init__(self, system, [strain]) #Abuse the Chebychev1 polynomial to simply get the value of q-1 and #implement the contraint constraint = ForcePartValence(system) constraint.add_term(Chebychev1(-2.0, term.ics[0])) self.constraint = ForceField(system, [constraint]) self.constrain_target = None self.constrain_value = None self.value = None
def get_ei_ff(name, system, charges, scales, radii=None, average=True, pbc=[0,0,0]): ''' A routine to construct a Yaff force field for the electrostatics **Arguments** name A string for the name of the force field. This name will show in possible plots visualizing contributions along perturbation trajectories system A Yaff System instance representing the system charges A numpy array containing the charge of each separate atom scales A list of 4 floats, representing scale1, scale2, scale3, scale4, i.e. the electrostatic scaling factors **Optional Arguments** radii A numpy array containing the gaussian radii of each separate atom. If this argument is omitted, point charges are used. average If set to True, the charges and radii will first be averaged over atom types. This is True by default. ''' if not (np.array(pbc)==0).all(): raise NotImplementedError('Periodic system not implemented in get_ei_ff') if average: qs = {} rs = {} ffatypes = [system.ffatypes[i] for i in system.ffatype_ids] for i, atype in enumerate(ffatypes): if atype in qs: qs[atype].append(charges[i]) else: qs[atype] = [charges[i]] if radii is not None: if atype in rs: rs[atype].append(radii[i]) else: rs[atype] = [radii[i]] for i, atype in enumerate(ffatypes): charges[i] = np.array(qs[atype]).mean() if radii is not None: radii[i] = np.array(rs[atype]).mean() if radii is None: radii = np.zeros(len(system.ffatype_ids), float) pair_pot = PairPotEI(charges.astype(np.float), 0.0, 50*angstrom, None, 1.0, radii.astype(np.float)) nlist = NeighborList(system, 0) scalings = Scalings(system, scale1=scales[0], scale2=scales[1], scale3=scales[2], scale4=scales[3]) part = ForcePartPair(system, nlist, scalings, pair_pot) ff = ForceField(system, [part], nlist=nlist) return YaffForceField(name, ff)
def get_hessian_contrib(self, index, fc=None): ''' Get the contribution to the covalent hessian of term with given index (and its slaves). If fc is given, set the fc of the master and its slave to the given fc. ''' val = ForcePartValence(self.system) kind = self.vlist.vtab[index]['kind'] masterslaves = [index] + self.terms[index].slaves if kind == 4: #Cosine m, k, rv = self.get_params(index) if fc is not None: k = fc for jterm in masterslaves: ics = self.terms[jterm].ics args = (m, k, rv) + tuple(ics) val.add_term(Cosine(*args)) elif kind == 3: #cross k, rv0, rv1 = self.get_params(index) if fc is not None: k = fc for jterm in masterslaves: ics = self.terms[jterm].ics args = (k, rv0, rv1) + tuple(ics) val.add_term(Cross(*args)) elif kind == 1: #Polyfour a0, a1, a2, a3 = list(self.get_params(index)) if fc is not None: a3 = 2.0 * fc a1 = -4.0 * fc * np.cos(a0)**2 for jterm in masterslaves: ics = self.terms[jterm].ics args = ([0.0, a1, 0.0, a3], ) + tuple(ics) val.add_term(PolyFour(*args)) elif kind == 0: #Harmonic: k, rv = self.get_params(index) if fc is not None: k = fc for jterm in masterslaves: ics = self.terms[jterm].ics args = (k, rv) + tuple(ics) val.add_term(Harmonic(*args)) else: raise ValueError('Term kind %i not supported' % kind) ff = ForceField(self.system, [val]) hcov = estimate_cart_hessian(ff) return hcov
def __init__(self, system, cons_ic, cons_ic_atindexes, ics, cart_penalty=1e-3 * angstrom): ''' A class deriving from the Yaff ForceField class to implement the strain of a molecular geometry associated with the term defined by term_index. **Arguments** system A Yaff System instance containing all system information. cons_ic An instance of Yaff Internal Coordinate representing the constrained term in the strain. cons_ic_atindexes A list of the atoms involved in the constrained IC. This is required for the implementation of the cartesian penalty. In principle this could be extracted from the information stored in cons_ic, but this is the lazy solution. ics A list of Yaff Internal Coordinate instances for which the strain needs to be minimized. cart_penalty Magnitude of an extra term added to the strain that penalises a deviation of the cartesian coordinates of each atom with respect to the equilibrium coordinates. This penalty is equal to norm(R-R_eq)**2/(2.0*3*Natoms*cart_penalty**2) and prevents global translations, global rotations as well as rotations of molecular fragments far from the IC under consideration. ''' self.coords0 = system.pos.copy() self.ndof = np.prod(self.coords0.shape) self.cart_penalty = cart_penalty self.cons_ic_atindexes = cons_ic_atindexes part = ForcePartValence(system) for ic in ics: part.add_term(Harmonic(1.0, None, ic)) #set the rest values to the equilibrium values part.dlist.forward() part.iclist.forward() for iterm in xrange(part.vlist.nv): term = part.vlist.vtab[iterm] ic = part.iclist.ictab[term['ic0']] term['par1'] = ic['value'] ForceField.__init__(self, system, [part]) #Abuse the Chebychev1 polynomial to simply get the value of q-1 and #implement the contraint part = ForcePartValence(system) part.add_term(Chebychev1(-2.0, cons_ic)) self.constraint = ForceField(system, [part]) self.constrain_target = None self.constrain_value = None self.value = None