mosdef-hub / mbuild

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

Add a function that will flatten a Compound, but group connected components #1108

Closed chrisiacovella closed 1 year ago

chrisiacovella commented 1 year ago

Flattening a Compound removes all hierarchy; it would be good to have a function that will still perform a similar operation but provide an additional level associated with which molecules are connected.

For example, take the following compound:

Compound, 28 particles, 18 bonds, 4 children

> ├── [C x 1], 1 particles, 0 bonds, 0 children
> ├── [Compound x 1], 3 particles, 2 bonds, 1 children
> │   └── [tip3p x 1], 3 particles, 2 bonds, 3 children
> │       ├── [H2 x 1], 1 particles, 1 bonds, 0 children
> │       ├── [H3 x 1], 1 particles, 1 bonds, 0 children
> │       └── [O1 x 1], 1 particles, 2 bonds, 0 children
> └── [Compound x 2], 12 particles, 8 bonds, 4 children
>     └── [Compound x 4], 3 particles, 2 bonds, 1 children
>         └── [tip3p x 1], 3 particles, 2 bonds, 3 children
>             ├── [H2 x 1], 1 particles, 1 bonds, 0 children
>             ├── [H3 x 1], 1 particles, 1 bonds, 0 children
>             └── [O1 x 1], 1 particles, 2 bonds, 0 children

And condense it to:

Compound, 28 particles, 18 bonds, 10 children

> ├── [C x 1], 1 particles, 0 bonds, 0 children
> └── [tip3p x 9], 3 particles, 2 bonds, 3 children
>     ├── [H2 x 1], 1 particles, 1 bonds, 0 children
>     ├── [H3 x 1], 1 particles, 1 bonds, 0 children
>     └── [O1 x 1], 1 particles, 2 bonds, 0 children

This could be very useful for complex construction, where the hierarchy is useful for simplifying construction, but makes it harder to manipulate the Compound later on.