예제 #1
0
def _bond_types_from_pmd(structure, bond_types_members_map=None):
    """ Helper function to convert GMSO BondType

    This function take in a Parmed Structure, iterate through its
    bond_types, create a corresponding GMSO.BondType, and finally
    return a dictionary containing all pairs of pmd.BondType
    and GMSO.BondType

    Parameters
    ----------
    structure: pmd.Structure
        Parmed Structure that needed to be converted.
    bond_types_members_map: optional, dict, default=None
        The member types (atomtype string) for each atom associated with the bond_types the structure

    Returns
    -------
    pmd_top_bondtypes : dict
        A dictionary linking a pmd.BondType object to its
        corresponding GMSO.BondType object.
    """
    pmd_top_bondtypes = dict()
    bond_types_members_map = _assert_dict(bond_types_members_map,
                                          'bond_types_members_map')
    for btype in structure.bond_types:
        bond_params = {
            'k': (2 * btype.k * u.Unit('kcal / (angstrom**2 * mol)')),
            'r_eq': btype.req * u.angstrom
        }

        member_types = bond_types_members_map.get(id(btype))
        top_bondtype = gmso.BondType(parameters=bond_params,
                                     member_types=member_types)
        pmd_top_bondtypes[btype] = top_bondtype
    return pmd_top_bondtypes
예제 #2
0
def _bond_types_from_pmd(structure):
    """ Helper function to convert GMSO BondType

    This function take in a Parmed Structure, iterate through its
    bond_types, create a corresponding GMSO.BondType, and finally
    return a dictionary containing all pairs of pmd.BondType
    and GMSO.BondType

    Parameter
    ----------
        structure: pmd.Structure
            Parmed Structure that needed to be converted.

    Return
    ------
        pmd_top_bondtypes : dict
            A dictionary linking a pmd.BondType object to its
            corresponding GMSO.BondType object.
    """
    pmd_top_bondtypes = dict()
    for btype in structure.bond_types:
        bond_params = {
            'k': (2 * btype.k * u.Unit('kcal / (angstrom**2 * mol)')),
            'r_eq': btype.req * u.angstrom
        }

        top_bondtype = gmso.BondType(parameters=bond_params)
        pmd_top_bondtypes[btype] = top_bondtype
    return pmd_top_bondtypes
예제 #3
0
def from_parmed(structure):
    """Convert a parmed.Structure to a gmso.Topology

    Convert a parametrized or un-parametrized parmed.Structure object to a topology.Topology.
    Specifically, this method maps Structure to Topology and Atom to Site.
    At this point, this method can only convert AtomType, BondType and AngleType.
    Conversion of DihedralType will be implement in the near future.

    Parameters
    ----------
    structure : parmed.Structure
        parmed.Structure instance that need to be converted.

    Returns
    -------
    top : gmso.Topology
    """
    msg = ("Provided argument that is not a Parmed Structure")
    assert isinstance(structure, pmd.Structure), msg

    top = gmso.Topology(name=structure.title)
    site_map = dict()
    for atom in structure.atoms:
        if isinstance(atom.atom_type, pmd.AtomType):
            atom_type = gmso.AtomType(
                name=atom.atom_type.name,
                charge=atom.atom_type.charge * u.elementary_charge,
                parameters={
                    'sigma': (atom.sigma * u.angstrom).in_units(u.nm),
                    'epsilon': atom.epsilon * u.Unit('kcal / mol')
                })
            site = gmso.Site(
                name=atom.name,
                charge=atom.charge * u.elementary_charge,
                position=([atom.xx, atom.xy, atom.xz] * u.angstrom).in_units(
                    u.nm),
                atom_type=atom_type)
        else:
            site = gmso.Site(
                name=atom.name,
                charge=atom.charge * u.elementary_charge,
                position=([atom.xx, atom.xy, atom.xz] * u.angstrom).in_units(
                    u.nm),
                atom_type=None)
        site_map[atom] = site
        top.add_site(site)
    top.update_topology()

    if np.all(structure.box):
        # This is if we choose for topology to have abox
        top.box = gmso.Box(
            (structure.box[0:3] * u.angstrom).in_units(u.nm),
            angles=u.degree * structure.box[3:6])

    for bond in structure.bonds:
        # Generate bond parameters for BondType that gets passed
        # to Bond
        if isinstance(bond.type, pmd.BondType):
            bond_params = {
                'k': (2 * bond.type.k * u.Unit('kcal / (nm**2 * mol)')),
                'r_eq': (bond.type.req * u.angstrom).in_units(u.nm)
            }
            new_connection_type = gmso.BondType(parameters=bond_params)
            top_connection = gmso.Bond(connection_members=[site_map[bond.atom1],
                site_map[bond.atom2]],
                connection_type=new_connection_type)

        # No bond parameters, make Connection with no connection_type
        else:
            top_connection = gmso.Bond(connection_members=[site_map[bond.atom1],
                site_map[bond.atom2]],
                connection_type=None)

        top.add_connection(top_connection, update_types=False)
    top.update_topology()
    print(top.n_bonds)

    for angle in structure.angles:
        # Generate angle parameters for AngleType that gets passed
        # to Angle
        if isinstance(angle.type, pmd.AngleType):
            angle_params = {
                'k': (2 * angle.type.k * u.Unit('kcal / (rad**2 * mol)')),
                'theta_eq': (angle.type.theteq * u.degree)
            }
            new_connection_type = gmso.AngleType(parameters=angle_params)
            top_connection = gmso.Angle(connection_members=[site_map[angle.atom1],
                site_map[angle.atom2], site_map[angle.atom3]],
                connection_type=new_connection_type)

        # No bond parameters, make Connection with no connection_type
        else:
            top_connection = gmso.Angle(connection_members=[site_map[angle.atom1],
                site_map[angle.atom2], site_map[angle.atom3]],
                connection_type=None)

        top.add_connection(top_connection, update_types=False)
    top.update_topology()

    # TODO: Dihedrals


    return top