diff --git a/docs/releasehistory.md b/docs/releasehistory.md index f809455b8..6dfc72304 100644 --- a/docs/releasehistory.md +++ b/docs/releasehistory.md @@ -29,6 +29,10 @@ Please note that all releases prior to a version 1.0.0 are considered pre-releas * #1396 Fixes charge ordering in Amber files +### Miscellaneous improvements + +* #1407 `get_charge_array` is now defined on `ElectrostaticsCollection` and therefore accessible to subclasses `FoyerElectrostaticsHandler` and `BasicElectrostaticsCollection`. Previously, it was only defined on `SMIRNOFFElectrostaticsCollection`. + ## 0.4.9 - 2025-11-06 ### Behavior changes diff --git a/openff/interchange/common/_nonbonded.py b/openff/interchange/common/_nonbonded.py index a6cb9a25f..30eaa1261 100644 --- a/openff/interchange/common/_nonbonded.py +++ b/openff/interchange/common/_nonbonded.py @@ -119,6 +119,22 @@ def charges( return self._charges + def get_charge_array(self, include_virtual_sites: bool = False) -> Quantity: + """ + Return a one-dimensional array-like of atomic charges, ordered topologically. + + If virtual sites are present in the system, `NotImplementedError` is raised. + """ + if include_virtual_sites: + raise NotImplementedError("Not yet implemented with virtual sites") + + if VirtualSiteKey in {type(key) for key in self.key_map}: + raise NotImplementedError( + "Not yet implemented when virtual sites are present, even with `include_virtual_sites=False`.", + ) + + return Quantity.from_list([q for _, q in sorted(self.charges.items(), key=lambda x: x[0].atom_indices)]) + def _get_charges( self, include_virtual_sites: bool = False, diff --git a/openff/interchange/interop/amber/export/_export.py b/openff/interchange/interop/amber/export/_export.py index 6419212ca..8a2faa8d1 100644 --- a/openff/interchange/interop/amber/export/_export.py +++ b/openff/interchange/interop/amber/export/_export.py @@ -486,7 +486,7 @@ def to_prmtop(interchange: "Interchange", file_path: Path | str): prmtop.write("%FLAG CHARGE\n%FORMAT(5E16.8)\n") charges = ( - interchange["Electrostatics"] # type: ignore[attr-defined] + interchange["Electrostatics"] .get_charge_array() .m_as( "elementary_charge", diff --git a/openff/interchange/smirnoff/_nonbonded.py b/openff/interchange/smirnoff/_nonbonded.py index 6041b97d6..b4e508ec3 100644 --- a/openff/interchange/smirnoff/_nonbonded.py +++ b/openff/interchange/smirnoff/_nonbonded.py @@ -343,22 +343,6 @@ def charges( return self._charges - def get_charge_array(self, include_virtual_sites: bool = False) -> Quantity: - """ - Return a one-dimensional array-like of atomic charges, ordered topologically. - - If virtual sites are present in the system, `NotImplementedError` is raised. - """ - if include_virtual_sites: - raise NotImplementedError("Not yet implemented with virtual sites") - - if VirtualSiteKey in {type(key) for key in self.key_map}: - raise NotImplementedError( - "Not yet implemented when virtual sites are present, even with `include_virtual_sites=False`.", - ) - - return Quantity.from_list([q for _, q in sorted(self.charges.items(), key=lambda x: x[0].atom_indices)]) - def _get_charges( self, include_virtual_sites: bool = True,