Skip to content

Add some topology slicing methods.#961

Draft
chrisjonesBSU wants to merge 12 commits into
mosdef-hub:mainfrom
chrisjonesBSU:calc-charges
Draft

Add some topology slicing methods.#961
chrisjonesBSU wants to merge 12 commits into
mosdef-hub:mainfrom
chrisjonesBSU:calc-charges

Conversation

@chrisjonesBSU

Copy link
Copy Markdown
Contributor

PR Summary:

This is ultimately an effort to help out @JAGarcia03 with his project, but it should be a useful addition in general.

utils.py gets new slicing methods where the goal is to create a new topology object that contains a subset of another topology's molecules or residues. This should live in utils.py as we have been wanting to reduce the amount of methods and code in general within Topology, and this file will be a good place to add other, more creative slicing methods.

This also adds the ability to filter by number in addition to tag within iter_sites_by_molecule and iter_sites_by_residue.

For example, if you want to get just the sites belonging to the 0th "Benzene" (instead of all of them) you can now do:

for site in topology.iter_sites_by_molecule(molecule_tag="Benzene", molecule_number=0)

A quick example:

import mbuild as mb
import gmso
from gmso.core.topology import Topology
from gmso.utils.slicing import slice_topology_by_molecule

benzene = mb.load("c1ccccc1", smiles=True)
benzene.name = "Benzene"
ethane = mb.load("CC", smiles=True)
ethane.name = "Ethane"

system = mb.fill_box(compound=[benzene, ethane], n_compounds=[2, 2], box=[2,2,2])
topology = system.to_gmso()
topology.identify_connections()

single_benzene_top = slice_topology_by_molecule(topology=topology, molecule_tag="Benzene", molecule_number=0)

all_benzene_top = slice_topology_by_molecule(topology=topology, molecule_tag="Benzene", molecule_number=None)

I still need to make the slicing tests more thorough, and add new tests for the changes to iter_sites_by_*

PR Checklist


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

@chrisjonesBSU chrisjonesBSU added the enhancement New feature or request label Feb 18, 2026
Comment thread gmso/core/topology.py Fixed
Comment thread gmso/core/topology.py Fixed
@codecov

codecov Bot commented Feb 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 41.46341% with 48 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.88%. Comparing base (3720cd0) to head (d7b0755).

Files with missing lines Patch % Lines
gmso/external/convert_openmm.py 8.69% 21 Missing ⚠️
gmso/core/topology.py 51.72% 14 Missing ⚠️
gmso/utils/charges.py 0.00% 13 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #961      +/-   ##
==========================================
- Coverage   93.42%   92.88%   -0.55%     
==========================================
  Files          67       69       +2     
  Lines        8020     8096      +76     
==========================================
+ Hits         7493     7520      +27     
- Misses        527      576      +49     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread gmso/core/topology.py
molecules.add(site.molecule.name)

# Figure out how to handle the tags
for mol in molecules:
Comment thread gmso/utils/charges.py
molecules = set()
for site in topology.sites:
molecules.add(site.molecule.name)

Comment thread gmso/utils/charges.py
from gmso.utils.slicing import slice_topology_by_molecule


def calculate_charges(topology, method="am1bcc", slice_by="Molecule"):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants