Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify bond graph to include independent particle #1036

Merged
merged 13 commits into from
Jun 24, 2022

Conversation

daico007
Copy link
Member

PR Summary:

Currently, compound bond graph only include bonded particles. This PR modify mb.Compound so that its bond graph also include independent/non-bonded particle.

PR Checklist


  • Includes appropriate unit test(s)
  • Appropriate docstring(s) are added/updated
  • Code is (approximately) PEP8 compliant
  • Issue(s) raised/addressed?

@codecov
Copy link

codecov bot commented May 30, 2022

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.39%. Comparing base (a7ff062) to head (1da0474).
Report is 157 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1036   +/-   ##
=======================================
  Coverage   90.38%   90.39%           
=======================================
  Files          64       64           
  Lines        9029     9027    -2     
=======================================
- Hits         8161     8160    -1     
+ Misses        868      867    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bc118
Copy link
Contributor

bc118 commented Jun 21, 2022

This PR seems to be passing all the existing MOSDEF-GOMC-GMSO tests if you add the other PRs data to the compound.py file:

def group_by_molecules(self):
    """Create a restructured compound where each molecule is grouped together.

    This top down method looks through the children in a compound and identifies
    individual molecules looking until is_independent() returns False. Then,
    the parent of that child is added to a list of molecule and grouped into one
    master compound.

    Parameters
    ----------
    compound : mbuild.Compound with molecules in the hierarchy

    Returns
    -------
    grouped_compound : mb.Compound where each child are the uniquely named
    independent molecules in the syste,

    See Also
    --------
    mbuild.compound.Compound._recursive_id_molecules

    Examples
    --------
    Creating a per molecule compound:

    >>> import mbuild as mb
    >>> cpd1 = mb.load("CCCC", smiles=True)
    >>> cpd1.name = "Butane"
    >>> cpd2 = mb.load("CCCCO", smiles=True)
    >>> cpd2.name = "Butanol"
    >>> box = mb.packing.solvate(
        solvent = cpd1,
        solute = cpd2,
        box = mb.Box([5,5,5]),
        n_solvent = 100,
    )
    >>> box2 = mb.packing.fill_box(cpd1, box=mb.Box([5,5,5]), n_compounds=100)
    >>> box2.translate([5,0,0])
    >>> box.add(box2)
    >>> partitioned_box = box.group_by_molecules()
    >>> print(child.name for child in partitioned_box.children)
    Output
    ["Butane", "Butanol"]

    """
    grouped_compound = Compound()
    grouped_compound.box = self.box
    molecule_list = self._recursive_id_molecules()
    if molecule_list:
        subtops = {}
        for molecule in molecule_list:
            if subtops.get(molecule.name):
                subtops[molecule.name].add(clone(molecule))
            else:
                subtops[molecule.name] = Compound(name=molecule.name)
                subtops[molecule.name].add(clone(molecule))
        for subtop in subtops.values():
            grouped_compound.add(subtop)
        return grouped_compound
    else:
        msg = f"""No molecules were found in {self.name}. Return molecule as is."""
        warn(msg)
        return mb.clone(self)

def _recursive_id_molecules(self, molecule_list=None):
    """Iterate through the compound top down to identify independent structures."""
    if not molecule_list:
        molecule_list = []

    # Handle the case where self is a lone particle
    if not self.children and self.is_independent():
        molecule_list.append(self)
        return molecule_list

    for child in self.children:
        if not child.is_independent():
            molecule_list.append(self)
            return molecule_list
        else:
            molecule_list = child._recursive_id_molecules(
                molecule_list=molecule_list
            )

    return molecule_list

@daico007 daico007 merged commit 14ae8da into mosdef-hub:main Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants