def test_vectorToParams(self): from parmed.parameters import ParameterSet from parmed.topologyobjects import DihedralTypeList, DihedralType params = ParameterSet() dihlist = DihedralTypeList() for i in range(6): dihtype = DihedralType(float(i) + 10, i + 1, float(i) + 20) dihlist.append(dihtype) params.dihedral_types[('x', 'x', 'x', 'x')] = dihlist self.df.dihedrals = [[0, 1, 2, 3]] self.df.parameters = params self.df._dihedral_atomtypes = [('x', 'x', 'x', 'x')] vector = np.array([30, 31, 32, 33, 34, 35, 40, 41, 42, 43, 44, 45, 50]) new_params = self.df._vectorToParams(vector) self.assertFalse(params is new_params) self.assertEqual(len(new_params.dihedral_types[('x', 'x', 'x', 'x')]), 6) for i, param in enumerate(new_params.dihedral_types[('x', 'x', 'x', 'x')]): self.assertEqual(param.phi_k, i + 30) self.assertEqual(param.per, i + 1) self.assertAlmostEqual(np.deg2rad(param.phase), i + 40)
def test_paramsToVector(self): from parmed.parameters import ParameterSet from parmed.topologyobjects import DihedralTypeList, DihedralType params = ParameterSet() dihlist = DihedralTypeList() for i in range(6): dihtype = DihedralType(float(i) + 10, i + 1, float(i) + 20) dihlist.append(dihtype) params.dihedral_types[('x', 'x', 'x', 'x')] = dihlist self.df.dihedrals = [ (0, 0, 0, 0), ] self.df._dihedral_atomtypes = [('x', 'x', 'x', 'x')] vector = self.df._paramsToVector(params) self.assertListEqual(list(vector), [ 10, 11, 12, 13, 14, 15, np.deg2rad(20), np.deg2rad(21), np.deg2rad(22), np.deg2rad(23), np.deg2rad(24), np.deg2rad(25), 0. ])
def test_paramsToVector(self): from parmed.parameters import ParameterSet from parmed.topologyobjects import DihedralTypeList, DihedralType params = ParameterSet() dihlist = DihedralTypeList() for i in range(6): dihtype = DihedralType(float(i)+10, i+1, float(i)+20) dihlist.append(dihtype) params.dihedral_types[('x', 'x', 'x', 'x')] = dihlist self.df.dihedrals = [(0, 0, 0, 0),] vector = self.df._paramsToVector(params, [('x', 'x', 'x', 'x'),]) self.assertListEqual(list(vector), [10., 11., 12., 13., 14., 15., 20., 21., 22., 23., 24., 25., 0.])
def test_paramsToVector(self): from parmed.parameters import ParameterSet from parmed.topologyobjects import DihedralTypeList, DihedralType params = ParameterSet() dihlist = DihedralTypeList() for i in range(6): dihtype = DihedralType(float(i)+10, i+1, float(i)+20) dihlist.append(dihtype) params.dihedral_types[('x', 'x', 'x', 'x')] = dihlist self.df.dihedrals = [(0, 0, 0, 0),] vector = self.df._paramsToVector(params, [('x', 'x', 'x', 'x'),]) self.assertListEqual(list(vector), [10., 11., 12., 13., 14., 15., 20., 21., 22., 23., 24., 25., 0.])
def __init__(self, true_value=None, initial_value=None, n_increments=18, rj=True, sample_phase=False, continuous=False): self._param = CharmmParameterSet(get_fun('toy.str')) self._struct = CharmmPsfFile(get_fun('toy.psf')) self._pdb = app.PDBFile(get_fun('toy.pdb')) self._topology = md.load_psf(get_fun('toy.psf')) self.synthetic_energy = units.Quantity() self._positions = units.Quantity() self._platform = mm.Platform.getPlatformByName('Reference') # Replace ('CG331', 'CG321', 'CG321', 'CG331') torsion with true_value self._dih_type = ('CG331', 'CG321', 'CG321', 'CG331') original_torsion = self._param.dihedral_types[self._dih_type] if true_value is not None: if type(true_value) == DihedralTypeList: dih_tlist = true_value elif type(true_value) == DihedralType: dih_tlist = DihedralTypeList() dih_tlist.append(true_value) else: dih_tlist = self._randomize_dih_param(return_dih=True) self.true_value = copy.deepcopy(dih_tlist) self._param.dihedral_types[self._dih_type] = dih_tlist # parametrize toy self._struct.load_parameters(self._param, copy_parameters=False) self._struct.positions = self._pdb.positions # generate synthetic torsion scan self._torsion_scan(n_increments=n_increments) # initialize parameter if initial_value is not None: if type(initial_value) == DihedralTypeList: dih_tlist = initial_value if type(initial_value) == DihedralType: dih_tlist = DihedralTypeList() dih_tlist.append(initial_value) elif initial_value == 'cgenff': dih_tlist = original_torsion else: dih_tlist = self._randomize_dih_param(return_dih=True) self.initial_value = copy.deepcopy(dih_tlist) self._param.dihedral_types[self._dih_type] = dih_tlist # create torsionfit.TorsionScanSet torsions = np.zeros((len(self._positions), 4)) torsions[:] = [1, 2, 3, 4] direction = None steps = None self.scan_set = ScanSet.QMDataBase(positions=self._positions.value_in_unit(units.nanometers), topology=self._topology, structure=self._struct, torsions=torsions, steps=steps, directions=direction, qm_energies=self.synthetic_energy.value_in_unit(units.kilojoules_per_mole)) self.model = model.TorsionFitModel(param=self._param, frags=self.scan_set, platform=self._platform, param_to_opt=[self._dih_type], rj=rj, continuous_phase=continuous, sample_phase=sample_phase)
def test_vectorToParams(self): from parmed.parameters import ParameterSet from parmed.topologyobjects import DihedralTypeList, DihedralType params = ParameterSet() dihlist = DihedralTypeList() for i in range(6): dihtype = DihedralType(float(i)+10, i+1, float(i)+20) dihlist.append(dihtype) params.dihedral_types[('x', 'x', 'x', 'x')] = dihlist self.df.dihedrals = [[0, 1, 2, 3]] vector = np.array([30., 31., 32., 33., 34., 35., 40., 41., 42., 43., 44., 45., 50.]) self.df._vectorToParams(params, [('x', 'x', 'x', 'x'),], vector) self.assertEqual(len(params.dihedral_types[('x', 'x', 'x', 'x')]), 6) for i, param in enumerate(params.dihedral_types[('x', 'x', 'x', 'x')]): self.assertEqual(param.phi_k, i+30) self.assertEqual(param.per, i+1) self.assertEqual(param.phase, i+40)
def test_vectorToParams(self): from parmed.parameters import ParameterSet from parmed.topologyobjects import DihedralTypeList, DihedralType params = ParameterSet() dihlist = DihedralTypeList() for i in range(6): dihtype = DihedralType(float(i)+10, i+1, float(i)+20) dihlist.append(dihtype) params.dihedral_types[('x', 'x', 'x', 'x')] = dihlist self.df.dihedrals = [[0, 1, 2, 3]] vector = np.array([30., 31., 32., 33., 34., 35., 40., 41., 42., 43., 44., 45., 50.]) self.df._vectorToParams(params, [('x', 'x', 'x', 'x'),], vector) self.assertEqual(len(params.dihedral_types[('x', 'x', 'x', 'x')]), 6) for i, param in enumerate(params.dihedral_types[('x', 'x', 'x', 'x')]): self.assertEqual(param.phi_k, i+30) self.assertEqual(param.per, i+1) self.assertEqual(param.phase, i+40)
def _randomize_dih_param(self, return_dih=False): """ generates random dihedral parameters """ dih_tlist = DihedralTypeList() multiplicities = [1, 2, 3, 4, 6] terms = np.random.randint(1, 5+1) np.random.shuffle(multiplicities) for i in range(terms): k = np.random.uniform(0.0, 20.0) n = multiplicities[i] phase = np.random.randint(0, 1+1) if phase == 1: phase = 180 dih_tlist.append(DihedralType(k, n, phase, 1.00, 1.00)) self._param.dihedral_types[self._dih_type] = dih_tlist if return_dih: _dih_tlist = copy.deepcopy(dih_tlist) return _dih_tlist
def _process_dihedral_line(self, line, finished_diheds): rematch = _dihedre.match(line) if not rematch: raise ParameterError('Could not understand DIHEDRAL line ' '[%s]' % line) a1, a2, a3, a4, div, k, phi, per = rematch.groups() scee = [float(x) for x in _sceere.findall(line)] or [1.2] scnb = [float(x) for x in _scnbre.findall(line)] or [2.0] a1 = a1.strip(); a2 = a2.strip(); a3 = a3.strip(); a4 = a4.strip() per = float(per) typ = DihedralType(float(k)/float(div), abs(per), float(phi), scee[0], scnb[0]) key = (a1, a2, a3, a4) rkey = (a4, a3, a2, a1) if finished_diheds[key]: # This dihedral is already finished its definition, which means we # go ahead and add a new one to override it typs = DihedralTypeList() typs.append(typ) self.dihedral_types[key] = self.dihedral_types[rkey] = typs else: self.dihedral_types[key].append(typ) finished_diheds[key] = finished_diheds[rkey] = per >= 0
def from_dih_params(filename=None, dih_params=None, rj=False, continuous=False, n_increments=13, sample_phase=False): """ Parameters ---------- filename : str name of file with serialized dihedral parameters rj : bool Flag if using reversible jump. Default it True continuous : bool Flag if sampling continuous phase. Default is False n_increments : int incermentation of torsion drive sample_phase : bool Flag if sampling phase. Default is False (K is allowed to go negative when sample_phase is False) Returns ------- ToyModel with true and initial value from saved file. """ if filename is None and dih_params is None: msg = 'You must provide either an npy file or a numpy array with true and initial values for the toy model' raise Exception(msg) if filename is not None: dih_params = np.load(filename) dih_tlist_true = DihedralTypeList() dih_tlist_init = DihedralTypeList() true = dih_params[0] init = dih_params[1] for dih in true: if not np.isnan(dih[0]): dih_tlist_true.append(DihedralType(per=dih[0], phi_k=dih[1], phase=dih[2])) for dih in init: if not np.isnan(dih[0]): dih_tlist_init.append(DihedralType(per=dih[0], phi_k=dih[1], phase=dih[2])) return ToyModel(true_value=dih_tlist_true, initial_value=dih_tlist_init, rj=rj, continuous=continuous, n_increments=n_increments, sample_phase=sample_phase)
def createMultitermDihedralTypes(parameters, nterms=6, scee=1.2, scnb=2): from parmed.topologyobjects import DihedralTypeList, DihedralType from copy import deepcopy parameters = deepcopy(parameters) for key, val in parameters.dihedral_types.items(): dihlist = DihedralTypeList() for i in range(1, nterms + 1): found = False for d in val: # Check if this term already exists in the parameters. if d.per == i: dihlist.append(d) found = True break if not found: # Else create an unparametrized term dihtype = DihedralType(0, i, 0, scee=scee, scnb=scnb) dihlist.append(dihtype) parameters.dihedral_types[key] = dihlist return parameters
def from_structure(cls, struct, allow_unequal_duplicates=True): """ Extracts known parameters from a Structure instance Parameters ---------- struct : :class:`parmed.structure.Structure` The parametrized ``Structure`` instance from which to extract parameters into a ParameterSet allow_unequal_duplicates : bool, optional If True, if two or more unequal parameter types are defined by the same atom types, the last one encountered will be assigned. If False, an exception will be raised. Default is True Returns ------- params : :class:`ParameterSet` The parameter set with all parameters defined in the Structure Notes ----- The parameters here are copies of the ones in the Structure, so modifying the generated ParameterSet will have no effect on ``struct``. Furthermore, the *first* occurrence of each parameter will be used. If future ones differ, they will be silently ignored, since this is expected behavior in some instances (like with Gromacs topologies in the ff99sb-ildn force field) unless ``allow_unequal_duplicates`` is set to ``False`` Dihedrals are a little trickier. They can be multi-term, which can be represented either as a *single* entry in dihedrals with a type of DihedralTypeList or multiple entries in dihedrals with a DihedralType parameter type. In this case, the parameter is constructed from either the first DihedralTypeList found or the first DihedralType of each periodicity found if no matching DihedralTypeList is found. Raises ------ :class:`parmed.exceptions.ParameterError` if allow_unequal_duplicates is False and 2+ unequal parameters are defined between the same atom types. `NotImplementedError` if any AMOEBA potential terms are defined in the input structure """ params = cls() found_dihed_type_list = dict() for atom in struct.atoms: if atom.atom_type in (UnassignedAtomType, None): atom_type = AtomType(atom.type, None, atom.mass, atom.atomic_number) atom_type.set_lj_params(atom.epsilon, atom.rmin, atom.epsilon_14, atom.rmin_14) params.atom_types[atom.type] = atom_type else: atom_type = copy(atom.atom_type) params.atom_types[str(atom_type)] = atom_type if atom_type.number is not None: params.atom_types_int[int(atom_type)] = atom_type params.atom_types_tuple[(int(atom_type), str(atom_type))] =\ atom_type for bond in struct.bonds: if bond.type is None: continue key = (bond.atom1.type, bond.atom2.type) if key in params.bond_types: if (not allow_unequal_duplicates and params.bond_types[key] != bond.type): raise ParameterError('Unequal bond types defined between ' '%s and %s' % key) continue # pragma: no cover typ = copy(bond.type) key = (bond.atom1.type, bond.atom2.type) params.bond_types[key] = typ params.bond_types[tuple(reversed(key))] = typ for angle in struct.angles: if angle.type is None: continue key = (angle.atom1.type, angle.atom2.type, angle.atom3.type) if key in params.angle_types: if (not allow_unequal_duplicates and params.angle_types[key] != angle.type): raise ParameterError('Unequal angle types defined between ' '%s, %s, and %s' % key) continue # pragma: no cover typ = copy(angle.type) key = (angle.atom1.type, angle.atom2.type, angle.atom3.type) params.angle_types[key] = typ params.angle_types[tuple(reversed(key))] = typ if angle.funct == 5: key = (angle.atom1.type, angle.atom3.type) params.urey_bradley_types[key] = NoUreyBradley params.urey_bradley_types[tuple(reversed(key))] = NoUreyBradley for dihedral in struct.dihedrals: if dihedral.type is None: continue key = (dihedral.atom1.type, dihedral.atom2.type, dihedral.atom3.type, dihedral.atom4.type) if dihedral.improper: key = cls._periodic_improper_key( dihedral.atom1, dihedral.atom2, dihedral.atom3, dihedral.atom4, ) if key in params.improper_periodic_types: if (not allow_unequal_duplicates and params.improper_periodic_types[key] != dihedral.type): raise ParameterError('Unequal dihedral types defined ' 'between %s, %s, %s, and %s' % key) continue # pragma: no cover typ = copy(dihedral.type) params.improper_periodic_types[key] = typ else: # Proper dihedral. Look out for multi-term forms if (key in params.dihedral_types and found_dihed_type_list[key]): # Already found a multi-term dihedral type list if not allow_unequal_duplicates: if isinstance(dihedral.type, DihedralTypeList): if params.dihedral_types[key] != dihedral.type: raise ParameterError('Unequal dihedral types ' 'defined between %s, %s, %s, and %s' % key) elif isinstance(dihedral.type, DihedralType): for dt in params.dihedral_types[key]: if dt == dihedral.type: break else: raise ParameterError('Unequal dihedral types ' 'defined between %s, %s, %s, and %s' % key) continue # pragma: no cover elif key in params.dihedral_types: # We have one term of a potentially multi-term dihedral. if isinstance(dihedral.type, DihedralTypeList): # This is a full Fourier series list found_dihed_type_list[key] = True found_dihed_type_list[tuple(reversed(key))] = True typ = copy(dihedral.type) params.dihedral_types[key] = typ params.dihedral_types[tuple(reversed(key))] = typ else: # This *might* be another term. Make sure another term # with its periodicity does not already exist for t in params.dihedral_types[key]: if t.per == dihedral.type.per: if (not allow_unequal_duplicates and t != dihedral.type): raise ParameterError('Unequal dihedral ' 'types defined bewteen %s, %s, %s, ' 'and %s' % key) break else: # If we got here, we did NOT find this periodicity. # And since this is mutating a list in-place, it # automatically propagates to the reversed key typ = copy(dihedral.type) params.dihedral_types[key].append(typ) else: # New parameter. If it's a DihedralTypeList, assign it and # be done with it. If it's a DihedralType, start a # DihedralTypeList to be added to later. if isinstance(dihedral.type, DihedralTypeList): found_dihed_type_list[key] = True found_dihed_type_list[tuple(reversed(key))] = True typ = copy(dihedral.type) params.dihedral_types[key] = typ params.dihedral_types[tuple(reversed(key))] = typ else: found_dihed_type_list[key] = False found_dihed_type_list[tuple(reversed(key))] = False typ = DihedralTypeList() typ.append(copy(dihedral.type)) params.dihedral_types[key] = typ params.dihedral_types[tuple(reversed(key))] = typ for improper in struct.impropers: if improper.type is None: continue key = (improper.atom1.type, improper.atom2.type, improper.atom3.type, improper.atom4.type) if key in params.improper_types: if (not allow_unequal_duplicates and params.improper_types[key] != improper.type): raise ParameterError('Unequal improper types defined ' 'between %s, %s, %s, and %s' % key) continue # pragma: no cover params.improper_types[key] = copy(improper.type) for cmap in struct.cmaps: if cmap.type is None: continue key = (cmap.atom1.type, cmap.atom2.type, cmap.atom3.type, cmap.atom4.type, cmap.atom2.type, cmap.atom3.type, cmap.atom4.type, cmap.atom5.type) if key in params.cmap_types: if (not allow_unequal_duplicates and cmap.type != params.cmap_types[key]): raise ParameterError('Unequal CMAP types defined between ' '%s, %s, %s, %s, and %s' % (key[0], key[1], key[2], key[3], key[7])) continue # pragma: no cover typ = copy(cmap.type) params.cmap_types[key] = typ params.cmap_types[tuple(reversed(key))] = typ for urey in struct.urey_bradleys: if urey.type is None or urey.type is NoUreyBradley: continue key = (urey.atom1.type, urey.atom2.type) if key not in params.urey_bradley_types: warnings.warn('Angle corresponding to Urey-Bradley type not ' 'found') typ = copy(urey.type) params.urey_bradley_types[key] = typ params.urey_bradley_types[tuple(reversed(key))] = typ for adjust in struct.adjusts: if adjust.type is None: continue key = (adjust.atom1.type, adjust.atom2.type) if key in params.pair_types: if (not allow_unequal_duplicates and params.pair_types[key] != adjust.type): raise ParameterError('Unequal pair types defined between ' '%s and %s' % key) continue # pragma: no cover typ = copy(adjust.type) params.pair_types[key] = typ params.pair_types[tuple(reversed(key))] = typ # Trap for Amoeba potentials if (struct.trigonal_angles or struct.out_of_plane_bends or struct.torsion_torsions or struct.stretch_bends or struct.trigonal_angles or struct.pi_torsions): raise NotImplementedError('Cannot extract parameters from an ' 'Amoeba-parametrized system yet') return params
def _process_dihedral_line(self, line, finished_diheds, last_key): """ Processes a dihedral line, possibly part of a multi-term dihedral Parameters ---------- line : str Line of the file that contains a dihedral term finished_diheds : dict Dictionary of dihedral parameters whose final term has been read in already (which means additional terms will overwrite, not add) last_key : str or None If not None, this is the key for the last dihedral type that should be implied if the atom types are missing. Atom types seem to only be required for the first term in a multi-term torsion definition Returns ------- key or None If a negative periodicity indicates another term is coming, the current key is returned so it can be passed as key to the next _process_dihedral_call """ rematch = _dihedre.match(line) if not rematch and last_key is None: raise ParameterError('Could not understand DIHEDRAL line ' '[%s]' % line) elif not rematch: rematch = _dihed2re.match(line) if not rematch: raise ParameterError('Could not understand DIHEDRAL line ' '[%s]' % line) div, k, phi, per = rematch.groups() key = last_key rkey = tuple(reversed(key)) assert key in finished_diheds if finished_diheds[key]: raise AssertionError('Cannot have an implied torsion that ' 'has already finished!') else: a1, a2, a3, a4, div, k, phi, per = rematch.groups() a1, a2, a3, a4 = a1.strip(), a2.strip(), a3.strip(), a4.strip() key = (a1, a2, a3, a4) rkey = (a4, a3, a2, a1) if last_key is not None and (last_key != key and last_key != rkey): warnings.warn('Expecting next term in dihedral %r, got ' 'definition for dihedral %r' % (last_key, key), ParameterWarning) scee = [float(x) for x in _sceere.findall(line)] or [1.2] scnb = [float(x) for x in _scnbre.findall(line)] or [2.0] per = float(per) typ = DihedralType(float(k)/float(div), abs(per), float(phi), scee[0], scnb[0]) if finished_diheds[key]: # This dihedral is already finished its definition, which means # we go ahead and add a new one to override it typs = DihedralTypeList() typs.append(typ) self.dihedral_types[key] = self.dihedral_types[rkey] = typs else: self.dihedral_types[key].append(typ) finished_diheds[key] = finished_diheds[rkey] = per >= 0 if per < 0: return key
def from_structure(cls, struct, allow_unequal_duplicates=True): """ Extracts known parameters from a Structure instance Parameters ---------- struct : :class:`parmed.structure.Structure` The parametrized ``Structure`` instance from which to extract parameters into a ParameterSet allow_unequal_duplicates : bool, optional If True, if two or more unequal parameter types are defined by the same atom types, the last one encountered will be assigned. If False, an exception will be raised. Default is True Returns ------- params : :class:`ParameterSet` The parameter set with all parameters defined in the Structure Notes ----- The parameters here are copies of the ones in the Structure, so modifying the generated ParameterSet will have no effect on ``struct``. Furthermore, the *first* occurrence of each parameter will be used. If future ones differ, they will be silently ignored, since this is expected behavior in some instances (like with Gromacs topologies in the ff99sb-ildn force field) unless ``allow_unequal_duplicates`` is set to ``False`` Dihedrals are a little trickier. They can be multi-term, which can be represented either as a *single* entry in dihedrals with a type of DihedralTypeList or multiple entries in dihedrals with a DihedralType parameter type. In this case, the parameter is constructed from either the first DihedralTypeList found or the first DihedralType of each periodicity found if no matching DihedralTypeList is found. Raises ------ :class:`parmed.exceptions.ParameterError` if allow_unequal_duplicates is False and 2+ unequal parameters are defined between the same atom types. `NotImplementedError` if any AMOEBA potential terms are defined in the input structure """ params = cls() found_dihed_type_list = dict() for atom in struct.atoms: if atom.atom_type in (UnassignedAtomType, None): atom_type = AtomType(atom.type, None, atom.mass, atom.atomic_number) atom_type.set_lj_params(atom.epsilon, atom.rmin, atom.epsilon_14, atom.rmin_14) params.atom_types[atom.type] = atom_type else: atom_type = copy(atom.atom_type) params.atom_types[str(atom_type)] = atom_type if atom_type.number is not None: params.atom_types_int[int(atom_type)] = atom_type params.atom_types_tuple[(int(atom_type), str(atom_type))] =\ atom_type for bond in struct.bonds: if bond.type is None: continue key = (bond.atom1.type, bond.atom2.type) if key in params.bond_types: if (not allow_unequal_duplicates and params.bond_types[key] != bond.type): raise ParameterError('Unequal bond types defined between ' '%s and %s' % key) continue # pragma: no cover typ = copy(bond.type) key = (bond.atom1.type, bond.atom2.type) params.bond_types[key] = typ params.bond_types[tuple(reversed(key))] = typ for angle in struct.angles: if angle.type is None: continue key = (angle.atom1.type, angle.atom2.type, angle.atom3.type) if key in params.angle_types: if (not allow_unequal_duplicates and params.angle_types[key] != angle.type): raise ParameterError('Unequal angle types defined between ' '%s, %s, and %s' % key) continue # pragma: no cover typ = copy(angle.type) key = (angle.atom1.type, angle.atom2.type, angle.atom3.type) params.angle_types[key] = typ params.angle_types[tuple(reversed(key))] = typ if angle.funct == 5: key = (angle.atom1.type, angle.atom3.type) params.urey_bradley_types[key] = NoUreyBradley params.urey_bradley_types[tuple(reversed(key))] = NoUreyBradley for dihedral in struct.dihedrals: if dihedral.type is None: continue key = (dihedral.atom1.type, dihedral.atom2.type, dihedral.atom3.type, dihedral.atom4.type) if dihedral.improper: key = cls._periodic_improper_key( dihedral.atom1, dihedral.atom2, dihedral.atom3, dihedral.atom4, ) if key in params.improper_periodic_types: if (not allow_unequal_duplicates and params.improper_periodic_types[key] != dihedral.type): raise ParameterError('Unequal dihedral types defined ' 'between %s, %s, %s, and %s' % key) continue # pragma: no cover typ = copy(dihedral.type) params.improper_periodic_types[key] = typ else: # Proper dihedral. Look out for multi-term forms if (key in params.dihedral_types and found_dihed_type_list[key]): # Already found a multi-term dihedral type list if not allow_unequal_duplicates: if isinstance(dihedral.type, DihedralTypeList): if params.dihedral_types[key] != dihedral.type: raise ParameterError( 'Unequal dihedral types ' 'defined between %s, %s, %s, and %s' % key) elif isinstance(dihedral.type, DihedralType): for dt in params.dihedral_types[key]: if dt == dihedral.type: break else: raise ParameterError( 'Unequal dihedral types ' 'defined between %s, %s, %s, and %s' % key) continue # pragma: no cover elif key in params.dihedral_types: # We have one term of a potentially multi-term dihedral. if isinstance(dihedral.type, DihedralTypeList): # This is a full Fourier series list found_dihed_type_list[key] = True found_dihed_type_list[tuple(reversed(key))] = True typ = copy(dihedral.type) params.dihedral_types[key] = typ params.dihedral_types[tuple(reversed(key))] = typ else: # This *might* be another term. Make sure another term # with its periodicity does not already exist for t in params.dihedral_types[key]: if t.per == dihedral.type.per: if (not allow_unequal_duplicates and t != dihedral.type): raise ParameterError( 'Unequal dihedral ' 'types defined bewteen %s, %s, %s, ' 'and %s' % key) break else: # If we got here, we did NOT find this periodicity. # And since this is mutating a list in-place, it # automatically propagates to the reversed key typ = copy(dihedral.type) params.dihedral_types[key].append(typ) else: # New parameter. If it's a DihedralTypeList, assign it and # be done with it. If it's a DihedralType, start a # DihedralTypeList to be added to later. if isinstance(dihedral.type, DihedralTypeList): found_dihed_type_list[key] = True found_dihed_type_list[tuple(reversed(key))] = True typ = copy(dihedral.type) params.dihedral_types[key] = typ params.dihedral_types[tuple(reversed(key))] = typ else: found_dihed_type_list[key] = False found_dihed_type_list[tuple(reversed(key))] = False typ = DihedralTypeList() typ.append(copy(dihedral.type)) params.dihedral_types[key] = typ params.dihedral_types[tuple(reversed(key))] = typ for improper in struct.impropers: if improper.type is None: continue key = (improper.atom1.type, improper.atom2.type, improper.atom3.type, improper.atom4.type) if key in params.improper_types: if (not allow_unequal_duplicates and params.improper_types[key] != improper.type): raise ParameterError('Unequal improper types defined ' 'between %s, %s, %s, and %s' % key) continue # pragma: no cover params.improper_types[key] = copy(improper.type) for cmap in struct.cmaps: if cmap.type is None: continue key = (cmap.atom1.type, cmap.atom2.type, cmap.atom3.type, cmap.atom4.type, cmap.atom2.type, cmap.atom3.type, cmap.atom4.type, cmap.atom5.type) if key in params.cmap_types: if (not allow_unequal_duplicates and cmap.type != params.cmap_types[key]): raise ParameterError( 'Unequal CMAP types defined between ' '%s, %s, %s, %s, and %s' % (key[0], key[1], key[2], key[3], key[7])) continue # pragma: no cover typ = copy(cmap.type) params.cmap_types[key] = typ params.cmap_types[tuple(reversed(key))] = typ for urey in struct.urey_bradleys: if urey.type is None or urey.type is NoUreyBradley: continue key = (urey.atom1.type, urey.atom2.type) if key not in params.urey_bradley_types: warnings.warn('Angle corresponding to Urey-Bradley type not ' 'found') typ = copy(urey.type) params.urey_bradley_types[key] = typ params.urey_bradley_types[tuple(reversed(key))] = typ for adjust in struct.adjusts: if adjust.type is None: continue key = (adjust.atom1.type, adjust.atom2.type) if key in params.pair_types: if (not allow_unequal_duplicates and params.pair_types[key] != adjust.type): raise ParameterError('Unequal pair types defined between ' '%s and %s' % key) continue # pragma: no cover typ = copy(adjust.type) params.pair_types[key] = typ params.pair_types[tuple(reversed(key))] = typ # Trap for Amoeba potentials if (struct.trigonal_angles or struct.out_of_plane_bends or struct.torsion_torsions or struct.stretch_bends or struct.trigonal_angles or struct.pi_torsions): raise NotImplementedError('Cannot extract parameters from an ' 'Amoeba-parametrized system yet') return params