Skip to content

Commit dcde2e8

Browse files
Move get_charge_array to ElectrostaticsCollection (#1407)
* REF: Move `get_charge_array` to base class * FIX: Fix type-checking * DOC: Update release history
1 parent 28eec2f commit dcde2e8

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

docs/releasehistory.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Please note that all releases prior to a version 1.0.0 are considered pre-releas
2929

3030
* #1396 Fixes charge ordering in Amber files
3131

32+
### Miscellaneous improvements
33+
34+
* #1407 `get_charge_array` is now defined on `ElectrostaticsCollection` and therefore accessible to subclasses `FoyerElectrostaticsHandler` and `BasicElectrostaticsCollection`. Previously, it was only defined on `SMIRNOFFElectrostaticsCollection`.
35+
3236
## 0.4.9 - 2025-11-06
3337

3438
### Behavior changes

openff/interchange/common/_nonbonded.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ def charges(
119119

120120
return self._charges
121121

122+
def get_charge_array(self, include_virtual_sites: bool = False) -> Quantity:
123+
"""
124+
Return a one-dimensional array-like of atomic charges, ordered topologically.
125+
126+
If virtual sites are present in the system, `NotImplementedError` is raised.
127+
"""
128+
if include_virtual_sites:
129+
raise NotImplementedError("Not yet implemented with virtual sites")
130+
131+
if VirtualSiteKey in {type(key) for key in self.key_map}:
132+
raise NotImplementedError(
133+
"Not yet implemented when virtual sites are present, even with `include_virtual_sites=False`.",
134+
)
135+
136+
return Quantity.from_list([q for _, q in sorted(self.charges.items(), key=lambda x: x[0].atom_indices)])
137+
122138
def _get_charges(
123139
self,
124140
include_virtual_sites: bool = False,

openff/interchange/interop/amber/export/_export.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def to_prmtop(interchange: "Interchange", file_path: Path | str):
486486

487487
prmtop.write("%FLAG CHARGE\n%FORMAT(5E16.8)\n")
488488
charges = (
489-
interchange["Electrostatics"] # type: ignore[attr-defined]
489+
interchange["Electrostatics"]
490490
.get_charge_array()
491491
.m_as(
492492
"elementary_charge",

openff/interchange/smirnoff/_nonbonded.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -343,22 +343,6 @@ def charges(
343343

344344
return self._charges
345345

346-
def get_charge_array(self, include_virtual_sites: bool = False) -> Quantity:
347-
"""
348-
Return a one-dimensional array-like of atomic charges, ordered topologically.
349-
350-
If virtual sites are present in the system, `NotImplementedError` is raised.
351-
"""
352-
if include_virtual_sites:
353-
raise NotImplementedError("Not yet implemented with virtual sites")
354-
355-
if VirtualSiteKey in {type(key) for key in self.key_map}:
356-
raise NotImplementedError(
357-
"Not yet implemented when virtual sites are present, even with `include_virtual_sites=False`.",
358-
)
359-
360-
return Quantity.from_list([q for _, q in sorted(self.charges.items(), key=lambda x: x[0].atom_indices)])
361-
362346
def _get_charges(
363347
self,
364348
include_virtual_sites: bool = True,

0 commit comments

Comments
 (0)