예제 #1
0
def _new_dihedral(old, a1, a2):
    """
    Create a new Dihedral object but one that replaces a1 with a2
    """
    if old.atom1 == a1:
        new = parmed.Dihedral(a2, old.atom2, old.atom3, old.atom4)
    elif old.atom2 == a1:
        new = parmed.Dihedral(old.atom1, a2, old.atom3, old.atom4)
    elif old.atom3 == a1:
        new = parmed.Dihedral(old.atom1, old.atom2, a2, old.atom4)
    elif old.atom4 == a1:
        new = parmed.Dihedral(old.atom1, old.atom2, old.atom3, a2)
    new.funct = 9
    return new
예제 #2
0
 def test_ep_exceptions(self):
     """ Test Nonbonded exception handling with virtual sites """
     # Analyze the exception parameters for bonding pattern
     #
     # E1 -- A1 -- A2 -- A3 -- A4 -- A5 -- E5
     #             |     |     |
     #             E2    E3    E4
     struct = pmd.Structure()
     ep1 = ExtraPoint(name='E1', type='EP', atomic_number=0, weights=[1, 2])
     ep2 = ExtraPoint(name='E2', type='EP', atomic_number=0)
     ep3 = ExtraPoint(name='E3', type='EP', atomic_number=0)
     ep4 = ExtraPoint(name='E4', type='EP', atomic_number=0)
     ep5 = ExtraPoint(name='E5', type='EP', atomic_number=0)
     self.assertIs(ep1.parent, None)
     self.assertEqual(ep1.bond_partners, [])
     self.assertEqual(ep1.angle_partners, [])
     self.assertEqual(ep1.dihedral_partners, [])
     self.assertEqual(ep1.tortor_partners, [])
     self.assertEqual(ep1.exclusion_partners, [])
     a1 = pmd.Atom(name='A1', type='AX', charge=0.1, atomic_number=6)
     a2 = pmd.Atom(name='A2', type='AY', charge=0.1, atomic_number=6)
     a3 = pmd.Atom(name='A3', type='AZ', charge=0.1, atomic_number=7)
     a4 = pmd.Atom(name='A4', type='AX', charge=0.1, atomic_number=6)
     a5 = pmd.Atom(name='A5', type='AY', charge=0.1, atomic_number=6)
     a1.rmin = a2.rmin = a3.rmin = a4.rmin = a5.rmin = 0.5
     a1.epsilon = a2.epsilon = a3.epsilon = a4.epsilon = a5.epsilon = 1.0
     bond_type = pmd.BondType(10.0, 1.0)
     bond_type2 = pmd.BondType(10.0, 2.0)
     bond_type3 = pmd.BondType(10.0, 0.5)
     bond_type4 = pmd.BondType(10.0, math.sqrt(2))
     angle_type = pmd.AngleType(10.0, 90)
     dihedral_type = pmd.DihedralType(10.0, 2, 0)
     struct.add_atom(a1, 'RES', 1)
     struct.add_atom(a2, 'RES', 1)
     struct.add_atom(a3, 'RES', 1)
     struct.add_atom(a4, 'RES', 1)
     struct.add_atom(a5, 'RES', 1)
     struct.add_atom(ep1, 'RES', 1)
     struct.add_atom(ep2, 'RES', 1)
     struct.add_atom(ep3, 'RES', 1)
     struct.add_atom(ep4, 'RES', 1)
     struct.add_atom(ep5, 'RES', 1)
     struct.bonds.extend([
         pmd.Bond(a1, ep1, type=bond_type),
         pmd.Bond(ep2, a2, type=bond_type),
         pmd.Bond(a3, ep3, type=bond_type3),
         pmd.Bond(a4, ep4, type=bond_type)
     ])
     struct.bonds.extend([
         pmd.Bond(a1, a2, type=bond_type),
         pmd.Bond(a4, a3, type=bond_type4),
         pmd.Bond(a3, a2, type=bond_type4),
         pmd.Bond(a4, a5, type=bond_type2),
         pmd.Bond(a5, ep5, type=bond_type)
     ])
     struct.angles.extend([
         pmd.Angle(a1, a2, a3, type=angle_type),
         pmd.Angle(a2, a3, a4, type=angle_type),
         pmd.Angle(a3, a4, a5, type=angle_type)
     ])
     struct.dihedrals.extend([
         pmd.Dihedral(a1, a2, a3, a4, type=dihedral_type),
         pmd.Dihedral(a2, a3, a4, a5, type=dihedral_type)
     ])
     struct.bond_types.extend(
         [bond_type, bond_type3, bond_type2, bond_type4])
     struct.angle_types.append(angle_type)
     struct.dihedral_types.append(dihedral_type)
     # Test exclusions now
     a1.exclude(a5)
     system = struct.createSystem()
예제 #3
0
def to_parmed(off_system: "System") -> pmd.Structure:
    """Convert an OpenFF System to a ParmEd Structure"""
    structure = pmd.Structure()
    _convert_box(off_system.box, structure)

    if "Electrostatics" in off_system.handlers.keys():
        has_electrostatics = True
        electrostatics_handler = off_system.handlers["Electrostatics"]
    else:
        has_electrostatics = False

    for topology_molecule in off_system.topology.topology_molecules:  # type: ignore[union-attr]
        for atom in topology_molecule.atoms:
            atomic_number = atom.atomic_number
            element = pmd.periodic_table.Element[atomic_number]
            mass = pmd.periodic_table.Mass[element]
            structure.add_atom(
                pmd.Atom(
                    atomic_number=atomic_number,
                    mass=mass,
                ),
                resname="FOO",
                resnum=0,
            )

    if "Bonds" in off_system.handlers.keys():
        bond_handler = off_system.handlers["Bonds"]
        bond_type_map: Dict = dict()
        for pot_key, pot in bond_handler.potentials.items():
            k = pot.parameters["k"].to(kcal_mol_a2).magnitude / 2
            length = pot.parameters["length"].to(unit.angstrom).magnitude
            bond_type = pmd.BondType(k=k, req=length)
            bond_type_map[pot_key] = bond_type
            structure.bond_types.append(bond_type)

        for top_key, pot_key in bond_handler.slot_map.items():
            idx_1, idx_2 = top_key.atom_indices
            bond_type = bond_type_map[pot_key]
            bond = pmd.Bond(
                atom1=structure.atoms[idx_1],
                atom2=structure.atoms[idx_2],
                type=bond_type,
            )
            structure.bonds.append(bond)

    structure.bond_types.claim()

    if "Angles" in off_system.handlers.keys():
        angle_handler = off_system.handlers["Angles"]
        angle_type_map: Dict = dict()
        for pot_key, pot in angle_handler.potentials.items():
            k = pot.parameters["k"].to(kcal_mol_rad2).magnitude / 2
            theta = pot.parameters["angle"].to(unit.degree).magnitude
            # TODO: Look up if AngleType already exists in struct
            angle_type = pmd.AngleType(k=k, theteq=theta)
            angle_type_map[pot_key] = angle_type
            structure.angle_types.append(angle_type)

        for top_key, pot_key in angle_handler.slot_map.items():
            idx_1, idx_2, idx_3 = top_key.atom_indices
            angle_type = angle_type_map[pot_key]
            structure.angles.append(
                pmd.Angle(
                    atom1=structure.atoms[idx_1],
                    atom2=structure.atoms[idx_2],
                    atom3=structure.atoms[idx_3],
                    type=angle_type,
                ))
            structure.angle_types.append(angle_type)

    structure.angle_types.claim()

    # ParmEd treats 1-4 scaling factors at the level of each DihedralType,
    # whereas SMIRNOFF captures them at the level of the non-bonded handler,
    # so they need to be stored here for processing dihedrals
    vdw_14 = off_system.handlers["vdW"].scale_14  # type: ignore[attr-defined]
    if has_electrostatics:
        coul_14 = off_system.handlers[
            "Electrostatics"].scale_14  # type: ignore[attr-defined]
    else:
        coul_14 = 1.0
    vdw_handler = off_system.handlers["vdW"]
    if "ProperTorsions" in off_system.handlers.keys():
        proper_torsion_handler = off_system.handlers["ProperTorsions"]
        proper_type_map: Dict = dict()
        for pot_key, pot in proper_torsion_handler.potentials.items():
            k = pot.parameters["k"].to(kcal_mol).magnitude
            periodicity = pot.parameters["periodicity"]
            phase = pot.parameters["phase"].magnitude
            proper_type = pmd.DihedralType(
                phi_k=k,
                per=periodicity,
                phase=phase,
                scnb=1 / vdw_14,
                scee=1 / coul_14,
            )
            proper_type_map[pot_key] = proper_type
            structure.dihedral_types.append(proper_type)

        for top_key, pot_key in proper_torsion_handler.slot_map.items():
            idx_1, idx_2, idx_3, idx_4 = top_key.atom_indices
            dihedral_type = proper_type_map[pot_key]
            structure.dihedrals.append(
                pmd.Dihedral(
                    atom1=structure.atoms[idx_1],
                    atom2=structure.atoms[idx_2],
                    atom3=structure.atoms[idx_3],
                    atom4=structure.atoms[idx_4],
                    type=dihedral_type,
                ))
            structure.dihedral_types.append(dihedral_type)

            key1 = TopologyKey(atom_indices=(idx_1, ))
            key4 = TopologyKey(atom_indices=(idx_4, ))
            vdw1 = vdw_handler.potentials[vdw_handler.slot_map[key1]]
            vdw4 = vdw_handler.potentials[vdw_handler.slot_map[key4]]
            sig1, eps1 = _lj_params_from_potential(vdw1)
            sig4, eps4 = _lj_params_from_potential(vdw4)
            sig = (sig1 + sig4) * 0.5
            eps = (eps1 * eps4)**0.5
            nbtype = pmd.NonbondedExceptionType(rmin=sig * 2**(1 / 6),
                                                epsilon=eps * vdw_14,
                                                chgscale=coul_14)
            structure.adjusts.append(
                pmd.NonbondedException(structure.atoms[idx_1],
                                       structure.atoms[idx_4],
                                       type=nbtype))
            structure.adjust_types.append(nbtype)

    structure.dihedral_types.claim()
    structure.adjust_types.claim()

    #    if False:  # "ImroperTorsions" in off_system.term_collection.terms:
    #        improper_term = off_system.term_collection.terms["ImproperTorsions"]
    #        for improper, smirks in improper_term.smirks_map.items():
    #            idx_1, idx_2, idx_3, idx_4 = improper
    #            pot = improper_term.potentials[improper_term.smirks_map[improper]]
    #            # TODO: Better way of storing periodic data in generally, probably need to improve Potential
    #            n = re.search(r"\d", "".join(pot.parameters.keys())).group()
    #            k = pot.parameters["k" + n].m  # kcal/mol
    #            periodicity = pot.parameters["periodicity" + n].m  # dimless
    #            phase = pot.parameters["phase" + n].m  # degree
    #
    #            dihedral_type = pmd.DihedralType(per=periodicity, phi_k=k, phase=phase)
    #            structure.dihedrals.append(
    #                pmd.Dihedral(
    #                    atom1=structure.atoms[idx_1],
    #                    atom2=structure.atoms[idx_2],
    #                    atom3=structure.atoms[idx_3],
    #                    atom4=structure.atoms[idx_4],
    #                    type=dihedral_type,
    #                )
    #            )

    vdw_handler = off_system.handlers["vdW"]
    for pmd_idx, pmd_atom in enumerate(structure.atoms):
        top_key = TopologyKey(atom_indices=(pmd_idx, ))
        smirks = vdw_handler.slot_map[top_key]
        potential = vdw_handler.potentials[smirks]
        element = pmd.periodic_table.Element[pmd_atom.element]
        sigma, epsilon = _lj_params_from_potential(potential)

        atom_type = pmd.AtomType(
            name=element + str(pmd_idx + 1),
            number=pmd_idx,
            atomic_number=pmd_atom.atomic_number,
            mass=pmd.periodic_table.Mass[element],
        )

        atom_type.set_lj_params(eps=epsilon, rmin=sigma * 2**(1 / 6) / 2)
        pmd_atom.atom_type = atom_type
        pmd_atom.type = atom_type.name
        pmd_atom.name = pmd_atom.type

    for pmd_idx, pmd_atom in enumerate(structure.atoms):
        if has_electrostatics:
            top_key = TopologyKey(atom_indices=(pmd_idx, ))
            partial_charge = electrostatics_handler.charges[
                top_key]  # type: ignore[attr-defined]
            unitless_ = partial_charge.to(unit.elementary_charge).magnitude
            pmd_atom.charge = float(unitless_)
            pmd_atom.atom_type.charge = float(unitless_)
        else:
            pmd_atom.charge = 0

    # Assign dummy residue names, GROMACS will not accept empty strings
    for res in structure.residues:
        res.name = "FOO"

    structure.positions = off_system.positions.to(
        unit.angstrom).magnitude  # type: ignore[attr-defined]
    for idx, pos in enumerate(structure.positions):
        structure.atoms[idx].xx = pos._value[0]
        structure.atoms[idx].xy = pos._value[1]
        structure.atoms[idx].xz = pos._value[2]

    return structure
예제 #4
0
파일: ParmEd.py 프로젝트: zemanj/mdanalysis
    def convert(self, obj):
        """Write selection at current trajectory frame to :class:`~parmed.structure.Structure`.

        Parameters
        -----------
        obj : AtomGroup or Universe or :class:`Timestep`
        """
        try:
            import parmed as pmd
        except ImportError:
            raise ImportError('ParmEd is required for ParmEdConverter but '
                              'is not installed. Try installing it with \n'
                              'pip install parmed')
        try:
            # make sure to use atoms (Issue 46)
            ag_or_ts = obj.atoms
        except AttributeError:
            if isinstance(obj, base.Timestep):
                raise ValueError("Writing Timesteps to ParmEd "
                                 "objects is not supported")
            else:
                raise_from(TypeError("No atoms found in obj argument"), None)

        # Check for topology information
        missing_topology = []
        try:
            names = ag_or_ts.names
        except (AttributeError, NoDataError):
            names = itertools.cycle(('X', ))
            missing_topology.append('names')
        try:
            resnames = ag_or_ts.resnames
        except (AttributeError, NoDataError):
            resnames = itertools.cycle(('UNK', ))
            missing_topology.append('resnames')

        if missing_topology:
            warnings.warn(
                "Supplied AtomGroup was missing the following attributes: "
                "{miss}. These will be written with default values. "
                "Alternatively these can be supplied as keyword arguments."
                "".format(miss=', '.join(missing_topology)))

        try:
            positions = ag_or_ts.positions
        except:
            positions = [None] * ag_or_ts.n_atoms

        try:
            velocities = ag_or_ts.velocities
        except:
            velocities = [None] * ag_or_ts.n_atoms

        atom_kwargs = []
        for atom, name, resname, xyz, vel in zip(ag_or_ts, names, resnames,
                                                 positions, velocities):
            akwargs = {'name': name}
            chain_seg = {'segid': atom.segid}
            for attrname in ('mass', 'charge', 'type', 'altLoc', 'tempfactor',
                             'occupancy', 'gbscreen', 'solventradius',
                             'nbindex', 'rmin', 'epsilon', 'rmin14',
                             'epsilon14', 'id'):
                try:
                    akwargs[MDA2PMD.get(attrname,
                                        attrname)] = getattr(atom, attrname)
                except AttributeError:
                    pass
            try:
                el = atom.element.lower().capitalize()
                akwargs['atomic_number'] = SYMB2Z[el]
            except (KeyError, AttributeError):
                try:
                    tp = atom.type.lower().capitalize()
                    akwargs['atomic_number'] = SYMB2Z[tp]
                except (KeyError, AttributeError):
                    pass
            try:
                chain_seg['chain'] = atom.chainID
            except AttributeError:
                pass
            try:
                chain_seg['inscode'] = atom.icode
            except AttributeError:
                pass
            atom_kwargs.append(
                (akwargs, resname, atom.resid, chain_seg, xyz, vel))

        struct = pmd.Structure()

        for akwarg, resname, resid, kw, xyz, vel in atom_kwargs:
            atom = pmd.Atom(**akwarg)
            if xyz is not None:
                atom.xx, atom.xy, atom.xz = xyz

            if vel is not None:
                atom.vx, atom.vy, atom.vz = vel

            atom.atom_type = pmd.AtomType(
                akwarg['name'],
                None,
                akwarg['mass'],
                atomic_number=akwargs.get('atomic_number'))
            struct.add_atom(atom, resname, resid, **kw)

        try:
            struct.box = ag_or_ts.dimensions
        except AttributeError:
            struct.box = None

        if hasattr(ag_or_ts, 'universe'):
            atomgroup = {
                atom: index
                for index, atom in enumerate(list(ag_or_ts))
            }
            get_atom_indices = functools.partial(get_indices_from_subset,
                                                 atomgroup=atomgroup,
                                                 universe=ag_or_ts.universe)
        else:
            get_atom_indices = lambda x: x

        # bonds
        try:
            params = ag_or_ts.bonds.atomgroup_intersection(ag_or_ts,
                                                           strict=True)
        except AttributeError:
            pass
        else:
            for p in params:
                atoms = [
                    struct.atoms[i] for i in map(get_atom_indices, p.indices)
                ]
                try:
                    for obj in p.type:
                        bond = pmd.Bond(*atoms, type=obj.type, order=obj.order)
                        struct.bonds.append(bond)
                    if isinstance(obj.type, pmd.BondType):
                        struct.bond_types.append(bond.type)
                        bond.type.list = struct.bond_types
                except (TypeError, AttributeError):
                    order = p.order if p.order is not None else 1
                    btype = getattr(p.type, 'type', None)

                    bond = pmd.Bond(*atoms, type=btype, order=order)
                    struct.bonds.append(bond)
                    if isinstance(bond.type, pmd.BondType):
                        struct.bond_types.append(bond.type)
                        bond.type.list = struct.bond_types

        # dihedrals
        try:
            params = ag_or_ts.dihedrals.atomgroup_intersection(ag_or_ts,
                                                               strict=True)
        except AttributeError:
            pass
        else:
            for p in params:
                atoms = [
                    struct.atoms[i] for i in map(get_atom_indices, p.indices)
                ]
                try:
                    for obj in p.type:
                        imp = getattr(obj, 'improper', False)
                        ign = getattr(obj, 'ignore_end', False)
                        dih = pmd.Dihedral(*atoms,
                                           type=obj.type,
                                           ignore_end=ign,
                                           improper=imp)
                        struct.dihedrals.append(dih)
                        if isinstance(dih.type, pmd.DihedralType):
                            struct.dihedral_types.append(dih.type)
                            dih.type.list = struct.dihedral_types
                except (TypeError, AttributeError):
                    btype = getattr(p.type, 'type', None)
                    imp = getattr(p.type, 'improper', False)
                    ign = getattr(p.type, 'ignore_end', False)
                    dih = pmd.Dihedral(*atoms,
                                       type=btype,
                                       improper=imp,
                                       ignore_end=ign)
                    struct.dihedrals.append(dih)
                    if isinstance(dih.type, pmd.DihedralType):
                        struct.dihedral_types.append(dih.type)
                        dih.type.list = struct.dihedral_types

        for param, pmdtype, trackedlist, typelist, clstype in (
            ('ureybradleys', pmd.UreyBradley, struct.urey_bradleys,
             struct.urey_bradley_types, pmd.BondType),
            ('angles', pmd.Angle, struct.angles, struct.angle_types,
             pmd.AngleType), ('impropers', pmd.Improper, struct.impropers,
                              struct.improper_types, pmd.ImproperType),
            ('cmaps', pmd.Cmap, struct.cmaps, struct.cmap_types,
             pmd.CmapType)):
            try:
                params = getattr(ag_or_ts, param)
                values = params.atomgroup_intersection(ag_or_ts, strict=True)
            except AttributeError:
                pass
            else:
                for v in values:
                    atoms = [
                        struct.atoms[i]
                        for i in map(get_atom_indices, v.indices)
                    ]

                    try:
                        for parmed_obj in v.type:
                            p = pmdtype(*atoms, type=parmed_obj.type)
                            trackedlist.append(p)
                            if isinstance(p.type, clstype):
                                typelist.append(p.type)
                                p.type.list = typelist
                    except (TypeError, AttributeError):
                        vtype = getattr(v.type, 'type', None)

                        p = pmdtype(*atoms, type=vtype)
                        trackedlist.append(p)
                        if isinstance(p.type, clstype):
                            typelist.append(p.type)
                            p.type.list = typelist
        return struct
예제 #5
0
파일: spinlabel.py 프로젝트: he-leon/meld
    def _add_particles(self, topol):
        err_msg = 'Unknown spin label type {{}}. Allowed values are: {}'
        err_msg = err_msg.format(', '.join(self.ALLOWED_TYPES))

        # we use the same radius and screen as for oxygen
        if not self.explicit:
            radius, screen = self._find_radius_and_screen(topol)

        # find all the unique types of spin labels
        types = set(self.params.values())

        # create the bond types
        bond_types = {}
        for t in types:
            bond_k, bond_r = self.bond_params[t]
            topol.bond_types.append(
                pmd.BondType(bond_k, bond_r, list=topol.bond_types))
            bt = topol.bond_types[-1]
            bond_types[t] = bt

        # create the angle types
        angle_types = {}
        for t in types:
            angle_k, angle_theta = self.angle_params[t]
            topol.angle_types.append(
                pmd.AngleType(angle_k, angle_theta, list=topol.angle_types))
            at = topol.angle_types[-1]
            angle_types[t] = at

        # create the torsion types
        tors_types = {}
        for t in types:
            tors_k, tors_per, tors_phase = self.tors_params[t]
            topol.dihedral_types.append(
                pmd.DihedralType(tors_k,
                                 tors_per,
                                 tors_phase,
                                 list=topol.dihedral_types))
            tt = topol.dihedral_types[-1]
            tors_types[t] = tt

        for key in self.params:
            if self.params[key] not in self.ALLOWED_TYPES:
                raise ValueError(err_msg.format(self.params[key]))

            # create the particle
            atom = pmd.Atom(None, 8, 'OND', 'OND', 0.0, 16.00)
            if not self.explicit:
                atom.radii = radius
                atom.screen = screen

            # add to system
            topol.add_atom_to_residue(atom, topol.residues[key])

            # find the other atoms
            ca = topol.view[':{},@CA'.format(key + 1)].atoms[0]
            cb = topol.view[':{},@CB'.format(key + 1)].atoms[0]
            n = topol.view[':{},@N'.format(key + 1)].atoms[0]

            # add bond
            topol.bonds.append(pmd.Bond(atom, ca,
                                        bond_types[self.params[key]]))

            # add angle
            topol.angles.append(
                pmd.Angle(cb, ca, atom, angle_types[self.params[key]]))

            # add torsion
            topol.dihedrals.append(
                pmd.Dihedral(n,
                             ca,
                             cb,
                             atom,
                             type=tors_types[self.params[key]]))

            # set position
            ca_pos = np.array((ca.xx, ca.xy, ca.xz))
            n_pos = np.array((n.xx, n.xy, n.xz))
            cb_pos = np.array((cb.xx, cb.xy, cb.xz))

            direction = np.linalg.norm(ca_pos - n_pos)
            new_pos = cb_pos - self.bond_params[
                self.params[key]][1].value_in_unit(u.angstrom) * direction

            atom.xx = new_pos[0]
            atom.xy = new_pos[1]
            atom.xz = new_pos[2]
        topol.remake_parm()

        # setup the new non-bonded parameters
        for t in types:
            indices = [
                index + 1 for index in self.params if self.params[index] == t
            ]
            selection_string = '(:{residue_mask})&(@{atom_name})'.format(
                residue_mask=','.join(str(i) for i in indices), atom_name=t)
            print topol.LJ_radius
            action = pmd.tools.addLJType(
                topol,
                selection_string,
                radius=self.lj_params[t][0].value_in_unit(u.angstrom),
                epsilon=self.lj_params[t][1].value_in_unit(
                    u.kilocalorie_per_mole))
            action.execute()
            print topol.LJ_radius
예제 #6
0
def _add_atom(parm, last_idx, template, htemplate):
    """
    Change the terminal hydrogen to a carbon and add three hydrogen atoms,
    also add bonded information

    Parameters
    ----------
    parm : parmed.Structure
        the topology
    last_idx : integer
        the index of the terminal carbon
    template : string
        the name prefix of the carbon atoms
    htemplate: string
        the name postfox of the hydrogen atoms
    """

    # Find all carbons preceeding the terminal carbon and the hydrogens
    # bonded to them
    carbons = []
    hydrogens = []
    for idx in range(last_idx - 2, last_idx + 1):
        carbons.append(_get_atom(parm, "%s%d" % (template, idx)))
        hydrogens.append(
            [_get_atom(parm, "H%d%s" % (idx, hstr)) for hstr in htemplate[:2]])

    # Change the charges of the carbon and hydrogens next to the terminal carbon
    carbons[1].charge = 0.0
    hydrogens[1][0].charge = 0.0
    hydrogens[1][1].charge = 0.0

    # Change the type and charge of the terminal carbon and its hydrogens
    carbons[2].charge = 0.0470
    carbons[2].type = "CTL2"
    for h in hydrogens[2]:
        h.charge = -0.0070
        h.type = "HAL2"

    # Find the final hydrogen and change it to a carbon
    hatom = _get_atom(parm, "H%d%s" % (last_idx, htemplate[2]))
    hatom.type = "CTL3"
    hatom.charge = -0.0810
    hatom.name = "%s%d" % (template, last_idx + 1)
    hatom.mass = 12.0110

    # Create 3 new hydrogen atoms and the topology information
    newatoms = []
    for i, hstr in enumerate(htemplate):
        newatom = parmed.Atom(list=parm.atoms,
                              name="H%d%s" % (last_idx + 1, hstr),
                              type="HAL3",
                              charge=0.0160,
                              mass=1.008000)
        newatom.residue = parm.residues[0]
        parm.residues[0].atoms.insert(hatom.idx + 1 + i, newatom)
        parm.atoms.insert(hatom.idx + 1 + i, newatom)
        parm.bonds.append(parmed.Bond(hatom, newatom))
        parm.adjusts.append(parmed.NonbondedException(carbons[1], newatom))
        parm.adjusts.append(parmed.NonbondedException(hydrogens[2][0],
                                                      newatom))
        parm.adjusts.append(parmed.NonbondedException(hydrogens[2][1],
                                                      newatom))
        parm.angles.append(parmed.Angle(carbons[2], hatom, newatom))
        parm.dihedrals.append(
            parmed.Dihedral(carbons[1], carbons[2], hatom, newatom))
        parm.dihedrals.append(
            parmed.Dihedral(hydrogens[2][0], carbons[2], hatom, newatom))
        parm.dihedrals.append(
            parmed.Dihedral(hydrogens[2][1], carbons[2], hatom, newatom))
        newatoms.append(newatom)

    # Add angles between thre three added hydrogens
    parm.angles.append(parmed.Angle(newatoms[0], hatom, newatoms[1]))
    parm.angles.append(parmed.Angle(newatoms[0], hatom, newatoms[2]))
    parm.angles.append(parmed.Angle(newatoms[1], hatom, newatoms[2]))

    # Correct the angle and dihedral types
    for angle in parm.angles[-12:]:
        angle.funct = 5
    for dihedral in parm.dihedrals[-9:]:
        dihedral.funct = 9