mosdef-hub / mbuild

A hierarchical, component based molecule builder
https://mbuild.mosdef.org
Other
173 stars 80 forks source link

condense function gets "confused" when a molecule contains unbound particles #1116

Closed chrisiacovella closed 1 year ago

chrisiacovella commented 1 year ago

Bug summary

I recently added the condense function that works similar to flatten, but retains connectivity information. However, ran into a case when dealing with tip4p where the condense function does not produce what I would like, due to the unconnected M site in the water.

Code to reproduce the behavior Consider a system of 40 tip4p molecules:

Compound, 40 particles, 20 bonds, 10 children
└── [WaterTIP4P x 10], 4 particles, 2 bonds, 4 children
    ├── [HW1 x 1], 1 particles, 1 bonds, 0 children
    ├── [HW2 x 1], 1 particles, 1 bonds, 0 children
    ├── [MW x 1], 1 particles, 0 bonds, 0 children
    └── [OW x 1], 1 particles, 2 bonds, 0 children

If we call condense we end up with:

Compound, 50 particles, 20 bonds, 20 children
├── [MW x 10], 1 particles, 0 bonds, 0 children
└── [WaterTIP4P x 10], 4 particles, 2 bonds, 4 children
    ├── [HW1 x 1], 1 particles, 1 bonds, 0 children
    ├── [HW2 x 1], 1 particles, 1 bonds, 0 children
    ├── [MW x 1], 1 particles, 0 bonds, 0 children
    └── [OW x 1], 1 particles, 2 bonds, 0 children

Here we end up with the tip4p molecules as we expect (since it creates a set of ancestors from the connected components), but it also adds in the unbound M site (named MW). The way the code is structured now, it will automatically add any particle that has no connections. I think this just needs to have two stages, where unbound particles are added after the molecules, so the code will check to ensure that the particle isn't part of a molecule that has already been considered before adding it.