molar_ff 2.2.0

GAFF/GAFF2 force-field atom typing for MolAR
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! espaloma-charge partial charges via a bundled ONNX graph network (feature `espaloma`).
//!
//! The GNN maps per-atom features `[n, 116]` + a row-mean-normalised bond adjacency
//! `[n, n]` to per-atom electronegativity `e` and hardness `s`; a closed-form charge
//! equilibration then yields partial charges summing to zero over the molecule.

use std::io::Cursor;
use std::sync::LazyLock;
use tract_onnx::prelude::*;

/// The bundled espaloma-charge model (bias-free, opset 18), exported from v0.0.8.
const MODEL: &[u8] = include_bytes!("../assets/espaloma_charge.onnx");

/// The runnable model, built once (keeps the atom count `N` symbolic so it runs on any molecule).
static PLAN: LazyLock<TypedRunnableModel<TypedModel>> = LazyLock::new(|| {
    tract_onnx::onnx()
        .model_for_read(&mut Cursor::new(MODEL))
        .expect("read espaloma onnx")
        .into_optimized()
        .expect("optimize")
        .into_runnable()
        .expect("runnable")
});

/// Run the GNN. `features` is row-major `[n, 116]`, `adj_mean` is row-major `[n, n]`
/// (`1/deg_i` for bonded pairs, no self-loop). Returns `(e, s)` per atom.
pub(crate) fn run_gnn(features: &[f32], adj_mean: &[f32], n: usize) -> TractResult<(Vec<f32>, Vec<f32>)> {
    let f = Tensor::from_shape(&[n, 116], features)?;
    let a = Tensor::from_shape(&[n, n], adj_mean)?;
    let out = PLAN.run(tvec!(f.into(), a.into()))?;
    let e = out[0].to_array_view::<f32>()?.iter().copied().collect();
    let s = out[1].to_array_view::<f32>()?.iter().copied().collect();
    Ok((e, s))
}

/// Standard atomic weight for the elements espaloma supports (matches RDKit `GetMass`).
fn mass_by_z(z: u8) -> f32 {
    match z {
        1 => 1.008, 6 => 12.011, 7 => 14.007, 8 => 15.999, 9 => 18.998,
        15 => 30.974, 16 => 32.06, 17 => 35.45, 35 => 79.904, 53 => 126.904,
        _ => 0.0,
    }
}

/// RDKit hybridization one-hot index: 0=SP,1=SP2,2=SP3,3=SP3D,4=SP3D2. `None` (all-zero)
/// for hydrogen (RDKit reports S/unspecified). `neighbor_conj` = does any neighbour carry a
/// multiple bond or is aromatic — a lone pair adjacent to such a π system conjugates into it,
/// so RDKit reports the atom as SP2 (amide N, ester/conjugated O) rather than SP3.
fn hybridization(z: u8, degree: usize, n_double: u32, n_triple: u32, aromatic: bool, neighbor_conj: bool) -> Option<usize> {
    if z == 1 {
        return None;
    }
    if aromatic {
        return Some(1); // SP2
    }
    if degree >= 6 {
        return Some(4); // SP3D2
    }
    if degree == 5 {
        return Some(3); // SP3D
    }
    if degree == 4 {
        return Some(2); // tetrahedral: sp3 C, ammonium N, sulfone/phosphate → SP3
    }
    // degree <= 3, non-aromatic:
    if n_triple >= 1 || n_double >= 2 {
        return Some(0); // SP: nitrile, allene / CO2 central atom
    }
    if n_double == 1 {
        return Some(1); // SP2: carbonyl, imine, ...
    }
    // lone-pair atom with no π bond of its own:
    if neighbor_conj && ((z == 7 && degree == 3) || (z == 8 && degree <= 2)) {
        return Some(1); // conjugated lone pair → SP2 (amide N, ester/conjugated O)
    }
    Some(2) // SP3
}

/// Outer-shell (valence) electron count for the elements espaloma supports.
fn n_outer_elec(z: u8) -> i32 {
    match z {
        1 => 1, 6 => 4, 7 => 5, 8 => 6, 9 => 7, 15 => 5, 16 => 6, 17 => 7, 35 => 7, 53 => 7,
        _ => 0,
    }
}

/// Pauling electronegativity, used to classify exocyclic double bonds: a double bond to a *more*
/// electronegative atom (C=O, C=N) pulls the π out of the ring plane (contributes 0, ring may
/// still be aromatic), whereas one to an equal/less electronegative atom (C=C — fulvene,
/// quinone-methide, thioxanthene ylidene) disrupts the ring and breaks aromaticity.
fn electronegativity(z: u8) -> f32 {
    match z {
        1 => 2.20, 6 => 2.55, 7 => 3.04, 8 => 3.44, 9 => 3.98, 15 => 2.19, 16 => 2.58,
        17 => 3.16, 35 => 2.96, 53 => 2.66, _ => 0.0,
    }
}

/// RDKit-style aromaticity perception. Rather than counting a specific Kekulé structure (which
/// mis-counts fused systems), each atom's π contribution is derived Kekulé-invariantly from its
/// element, formal charge and σ-connectivity, then Hückel's 4n+2 rule is applied to every
/// individual ring **and** every maximal fused ring system (so naphthalene, azulene, purine,
/// phenanthridine … resolve correctly).
///
/// Per-atom contribution: an sp3/hyper-valent atom (≥4 σ connections) or an in-ring triple bond
/// breaks aromaticity; an exocyclic multiple bond to a non-ring atom (carbonyl, exocyclic
/// methylene) donates 0 (its π sits outside the ring); otherwise `avail = n_outer − fc − σ`
/// leaves an odd count (one p electron → 1) or an even count (a lone pair → 2), with `avail ≤ 0`
/// (carbocation, quaternary N⁺) donating 0.
fn aromatic_atoms(z: &[u8], fc: &[i32], bonds: &[crate::gaff::LocalBond], rings: &[Vec<usize>]) -> Vec<bool> {
    let n = z.len();
    let mut inc: Vec<Vec<(usize, u8)>> = vec![Vec::new(); n];
    for b in bonds {
        inc[b.i].push((b.j, b.order));
        inc[b.j].push((b.i, b.order));
    }
    let mut in_ring = vec![false; n];
    for r in rings {
        for &a in r {
            in_ring[a] = true;
        }
    }
    // Kekulé-invariant per-atom π contribution (None = breaks aromaticity). σ is the explicit
    // connection count — molar carries no implicit hydrogens, and under-hydrogenated molecules
    // are excluded upstream. sp3/hyper-valent (≥4 σ) or an in-ring triple bond breaks. An
    // exocyclic multiple bond to a more-electronegative atom (C=O, C=N) contributes 0; one to an
    // equal/less-electronegative atom (C=C — fulvene/ylidene) breaks the ring. Otherwise
    // `n_outer − fc − σ` leaves an odd count (one p electron → 1) or even (a lone pair → 2), ≤0 → 0.
    let contrib: Vec<Option<i32>> = (0..n)
        .map(|a| {
            let sigma = inc[a].len() as i32;
            if sigma >= 4 || inc[a].iter().any(|&(j, o)| o == 3 && in_ring[j]) {
                return None;
            }
            let mut exocyclic_zero = false;
            for &(j, o) in &inc[a] {
                if o >= 2 && !in_ring[j] {
                    if electronegativity(z[j]) > electronegativity(z[a]) {
                        exocyclic_zero = true;
                    } else {
                        return None; // exocyclic C=C etc. disrupts the ring
                    }
                }
            }
            if exocyclic_zero {
                return Some(0);
            }
            let avail = n_outer_elec(z[a]) - fc[a] - sigma;
            Some(if avail <= 0 { 0 } else if avail % 2 == 1 { 1 } else { 2 })
        })
        .collect();
    let huckel = |atoms: &[usize]| -> bool {
        let mut pi = 0;
        for &a in atoms {
            match contrib[a] {
                Some(c) => pi += c,
                None => return false,
            }
        }
        pi % 4 == 2
    };

    let mut arom = vec![false; n];
    // Every individual ring, plus every maximal fused ring system (union of rings sharing a bond),
    // so fused aromatics resolve even when no single SSSR ring independently reaches 4n+2.
    for ring in rings {
        if huckel(ring) {
            for &a in ring {
                arom[a] = true;
            }
        }
    }
    let mut parent: Vec<usize> = (0..rings.len()).collect();
    fn find(parent: &mut [usize], x: usize) -> usize {
        let mut r = x;
        while parent[r] != r {
            r = parent[r];
        }
        let mut c = x;
        while parent[c] != r {
            let next = parent[c];
            parent[c] = r;
            c = next;
        }
        r
    }
    for i in 0..rings.len() {
        for j in (i + 1)..rings.len() {
            if rings[i].iter().filter(|a| rings[j].contains(a)).count() >= 2 {
                let (ri, rj) = (find(&mut parent, i), find(&mut parent, j));
                parent[ri] = rj;
            }
        }
    }
    let mut systems: std::collections::BTreeMap<usize, std::collections::BTreeSet<usize>> =
        Default::default();
    for i in 0..rings.len() {
        let root = find(&mut parent, i);
        systems.entry(root).or_default().extend(rings[i].iter().copied());
    }
    for atoms in systems.values() {
        let atoms: Vec<usize> = atoms.iter().copied().collect();
        if huckel(&atoms) {
            for &a in &atoms {
                arom[a] = true;
            }
        }
    }
    arom
}

/// Build the 116-dim atom feature matrix (row-major `[n,116]`) and the row-mean-normalised
/// adjacency (`[n,n]`) that the espaloma GNN consumes.
pub(crate) fn featurize(z: &[u8], fc: &[i32], bonds: &[crate::gaff::LocalBond]) -> (Vec<f32>, Vec<f32>) {
    let n = z.len();
    // One bonded-adjacency index over the local subgraph, shared by the ring search and the
    // neighbour walks below. Replaces a `build_con` call plus a full `Vec<Bond>` copy that
    // existed only to reach `sssr_rings`.
    let bond_adj = molar::prelude::BondAdjacency::build(n, bonds.iter().map(|b| [b.i, b.j]));
    // Rings from molar core SSSR (matches RDKit ring semantics and is H-independent, so aromatic
    // CH carbons written without explicit hydrogens are not lost the way gaff's antechamber-style
    // detection loses them). Bond order is irrelevant to ring finding, so connectivity suffices.
    let rings = molar::prelude::sssr_rings(&bond_adj);
    let mut rg = vec![[false; 11]; n]; // per-atom ring-size membership, sizes 3..=10
    for r in &rings {
        let sz = r.len().min(10);
        for &a in r {
            rg[a][sz] = true;
        }
    }
    let (mut nd, mut nt, mut val) = (vec![0u32; n], vec![0u32; n], vec![0u32; n]);
    for b in bonds {
        val[b.i] += b.order as u32;
        val[b.j] += b.order as u32;
        if b.order == 2 {
            nd[b.i] += 1;
            nd[b.j] += 1;
        } else if b.order == 3 {
            nt[b.i] += 1;
            nt[b.j] += 1;
        }
    }
    // RDKit-style Hückel aromaticity. A neighbour "conjugates" a lone pair if it is aromatic or
    // carries a multiple bond on carbon or nitrogen (C=O/C=C/C=N, amidine); a multiple bond on
    // S or P (sulfonamide S=O, phosphonate P=O) does NOT — RDKit leaves those N/O as SP3.
    let aromatic = aromatic_atoms(z, fc, bonds, &rings);
    let neighbor_conj: Vec<bool> = (0..n)
        .map(|i| {
            bond_adj.neighbors(i).iter().any(|nb| {
                let j = nb.atom();
                aromatic[j] || ((nd[j] > 0 || nt[j] > 0) && matches!(z[j], 6 | 7))
            })
        })
        .collect();
    let mut feat = vec![0f32; n * 116];
    for i in 0..n {
        let o = i * 116;
        if (z[i] as usize) < 100 {
            feat[o + z[i] as usize] = 1.0; // element one-hot by atomic number
        }
        let degree = bond_adj.neighbors(i).len();
        feat[o + 100] = degree as f32; // TotalDegree (explicit; molar carries no implicit H)
        feat[o + 101] = val[i] as f32; // TotalValence (explicit)
        feat[o + 102] = val[i] as f32; // ExplicitValence
        feat[o + 103] = if aromatic[i] { 1.0 } else { 0.0 };
        feat[o + 104] = mass_by_z(z[i]);
        for (k, sz) in (3..=8).enumerate() {
            feat[o + 105 + k] = if rg[i][sz] { 1.0 } else { 0.0 };
        }
        if let Some(h) = hybridization(z[i], degree, nd[i], nt[i], aromatic[i], neighbor_conj[i]) {
            feat[o + 111 + h] = 1.0;
        }
    }
    let mut adj = vec![0f32; n * n];
    for b in bonds {
        adj[b.i * n + b.j] = 1.0;
        adj[b.j * n + b.i] = 1.0;
    }
    for i in 0..n {
        let deg: f32 = adj[i * n..(i + 1) * n].iter().sum();
        if deg > 0.0 {
            for x in &mut adj[i * n..(i + 1) * n] {
                *x /= deg;
            }
        }
    }
    (feat, adj)
}

/// Featurize and run the GNN, returning the raw per-atom electronegativity/hardness
/// `(e, s)` before charge equilibration.
pub(crate) fn espaloma_e_s(
    z: &[u8],
    fc: &[i32],
    bonds: &[crate::gaff::LocalBond],
) -> TractResult<(Vec<f32>, Vec<f32>)> {
    let (feat, adj) = featurize(z, fc, bonds);
    run_gnn(&feat, &adj, z.len())
}

/// End-to-end: atomic numbers + formal charges + local bonds → espaloma partial charges.
///
/// The charges are equilibrated to sum to the molecule's **total formal charge**
/// `Σ_i fc_i`, matching upstream espaloma-charge (which takes `Q = Chem.GetFormalCharge(mol)`
/// when no explicit total is supplied). A cation therefore sums to +1, not 0.
pub(crate) fn espaloma_charges(z: &[u8], fc: &[i32], bonds: &[crate::gaff::LocalBond]) -> TractResult<Vec<f32>> {
    let (e, s) = espaloma_e_s(z, fc, bonds)?;
    Ok(equilibrate(&e, &s, fc.iter().sum::<i32>() as f32))
}

/// Espaloma charge equilibration over the whole molecule, constrained to total charge
/// `q_total`:
///
/// `q_i = -e_i/s_i + (1/s_i) · (q_total + Σ_j e_j/s_j) / (Σ_j 1/s_j)`
///
/// This is the Lagrange-multiplier solution of `U(q) = Σ_i (e_i q_i + ½ s_i q_i²)` under
/// `Σ_i q_i = q_total`, and matches upstream espaloma-charge
/// (`espaloma_charge/models.py::charge_equilibrium`, whose `sum_q` is the molecule's total
/// formal charge). Passing `q_total = 0.0` recovers the neutral-molecule special case.
pub(crate) fn equilibrate(e: &[f32], s: &[f32], q_total: f32) -> Vec<f32> {
    let inv: Vec<f32> = s.iter().map(|x| 1.0 / x).collect();
    let sum_inv: f32 = inv.iter().sum();
    let sum_eos: f32 = e.iter().zip(&inv).map(|(a, b)| a * b).sum();
    let lam = (q_total + sum_eos) / sum_inv;
    e.iter().zip(&inv).map(|(a, b)| -a * b + b * lam).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn rows_f32(v: &serde_json::Value) -> Vec<f32> {
        v.as_array()
            .unwrap()
            .iter()
            .flat_map(|row| row.as_array().unwrap().iter().map(|x| x.as_f64().unwrap() as f32))
            .collect()
    }
    fn col_f32(v: &serde_json::Value) -> Vec<f32> {
        v.as_array().unwrap().iter().map(|x| x.as_f64().unwrap() as f32).collect()
    }

    /// tract must reproduce the Python reference (e, s, charges) for the fixture molecule.
    #[test]
    fn tract_matches_python_fixture() {
        let txt = std::fs::read_to_string("tests/data/espaloma_fixture.json").unwrap();
        let v: serde_json::Value = serde_json::from_str(&txt).unwrap();
        let n = v["n"].as_u64().unwrap() as usize;
        let feats = rows_f32(&v["features"]);
        let adj = rows_f32(&v["adjacency_mean"]);
        let exp_e = col_f32(&v["e"]);
        let exp_q = col_f32(&v["charges"]);

        let (e, s) = run_gnn(&feats, &adj, n).expect("tract run");
        let de = e.iter().zip(&exp_e).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
        // The fixture molecule is neutral, so the reference charges are the `Q = 0` solution.
        let q = equilibrate(&e, &s, 0.0);
        let dq = q.iter().zip(&exp_q).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
        println!("tract vs python fixture: max|Δe|={de:.2e}  max|Δq|={dq:.2e}");
        assert!(de < 1e-4, "electronegativity mismatch: {de}");
        assert!(dq < 1e-4, "charge mismatch: {dq}");
    }

    /// End-to-end: Rust featurization + tract vs the Python espaloma reference over the corpus.
    /// The reference (`references_espaloma.json`) covers only molecules with fully explicit
    /// hydrogens — molar deliberately performs no implicit-H perception, so under-hydrogenated
    /// inputs (where RDKit would silently add H) are excluded from the benchmark by construction.
    #[test]
    fn corpus_rmse_vs_reference() {
        use molar::prelude::*;
        #[derive(serde::Deserialize)]
        struct RefFile {
            molecules: Vec<RefMol>,
        }
        #[derive(serde::Deserialize)]
        struct RefMol {
            name: String,
            charges: Vec<f32>,
        }
        let txt = std::fs::read_to_string("tests/data/gaff_ref/references_espaloma.json").unwrap();
        let refs: RefFile = serde_json::from_str(&txt).unwrap();
        let (mut se, mut nat, mut maxd) = (0f64, 0usize, 0f32);
        let (mut worst, mut load_err) = (String::new(), 0usize);
        for mol in &refs.molecules {
            let path = format!("tests/data/gaff_ref/sdf/{}.sdf", mol.name);
            let sys = match System::from_file(&path) {
                Ok(s) => s,
                Err(_) => {
                    load_err += 1;
                    continue;
                }
            };
            let z: Vec<u8> = sys.iter_atoms().map(|a| a.get_atomic_number()).collect();
            let fc: Vec<i32> = sys.iter_atoms().map(|a| a.get_formal_charge().unwrap_or(0)).collect();
            let mut bonds = Vec::new();
            for b in sys.iter_bonds() {
                let order = match b.order() {
                    BondOrder::Double => 2,
                    BondOrder::Triple => 3,
                    _ => 1,
                };
                bonds.push(crate::gaff::LocalBond { i: b.i1(), j: b.i2(), order });
            }
            // `references_espaloma.json` was generated with the total charge pinned at 0 for
            // every molecule: 274 of the 595 SDFs carry `M CHG` records (mostly protonated
            // amines, net +1) yet every reference charge set sums to exactly 0.0. So this
            // test compares against the same `Q = 0` equilibration, which still validates the
            // featurization, the GNN and the equilibration algebra. The physical
            // `Σq = Σ fc` path is covered by `total_formal_charge_is_preserved`.
            let (e, s) = espaloma_e_s(&z, &fc, &bonds).unwrap();
            let q = equilibrate(&e, &s, 0.0);
            if q.len() != mol.charges.len() {
                continue;
            }
            for (i, (a, b)) in q.iter().zip(&mol.charges).enumerate() {
                let d = (a - b).abs();
                se += (d as f64) * (d as f64);
                nat += 1;
                if d > maxd {
                    maxd = d;
                    worst = format!("{} atom={i} z={} (mine={a:.3} ref={b:.3})", mol.name, z[i]);
                }
            }
        }
        let rmse = (se / nat as f64).sqrt();
        println!("espaloma Rust vs reference: RMSE={rmse:.4}e  max|Δq|={maxd:.4}e  worst={worst}  atoms={nat}  load_err={load_err}");
        assert_eq!(load_err, 0);
        // The featurization reproduces RDKit exactly over this corpus, so the charges match the
        // Python espaloma reference to float precision (residual is f32 rounding, ~2e-4).
        assert!(rmse < 5e-4, "espaloma charge RMSE {rmse} regressed");
    }

    #[derive(serde::Deserialize)]
    struct RefFile {
        molecules: Vec<RefMol>,
    }
    #[derive(serde::Deserialize)]
    struct RefMol {
        name: String,
        charges: Vec<f32>,
    }

    fn load_refs() -> Vec<RefMol> {
        let txt = std::fs::read_to_string("tests/data/gaff_ref/references_espaloma.json").unwrap();
        serde_json::from_str::<RefFile>(&txt).unwrap().molecules
    }

    /// The public `ApplyCharges` API writes predicted charges into `atom.charge`, matching the
    /// reference and summing to ~0 over the molecule. Uses the first **neutral** reference
    /// molecule, since the reference was generated with the total charge pinned at 0 (see
    /// `corpus_rmse_vs_reference`) and so does not describe charged species.
    #[test]
    fn apply_charges_public_api() {
        use crate::{ApplyCharges, ChargeModel};
        use molar::prelude::*;

        let refs = load_refs();
        let (mol, mut sys) = refs
            .iter()
            .find_map(|m| {
                let sys =
                    System::from_file(format!("tests/data/gaff_ref/sdf/{}.sdf", m.name)).ok()?;
                let q_total: i32 =
                    sys.iter_atoms().map(|a| a.get_formal_charge().unwrap_or(0)).sum();
                (q_total == 0).then_some((m, sys))
            })
            .expect("corpus must contain a neutral molecule");

        sys.apply_charges(ChargeModel::Espaloma).unwrap();

        let got: Vec<f32> = sys.iter_atoms().map(|a| a.get_charge() as f32).collect();
        let maxd = got.iter().zip(&mol.charges).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
        let sum: f32 = got.iter().sum();
        println!("apply_charges({}): max|Δ|={maxd:.2e}  Σq={sum:.2e}", mol.name);
        assert!(maxd < 1e-3, "apply_charges disagrees with reference: {maxd}");
        assert!(sum.abs() < 1e-3, "charges should sum to ~0: {sum}");
    }

    /// Charges must sum to the molecule's **total formal charge**, not to zero. 274 of the
    /// corpus SDFs carry `M CHG` records (protonated amines, carboxylates, …); every one of
    /// them must equilibrate to its own net charge.
    #[test]
    fn total_formal_charge_is_preserved() {
        use crate::{ApplyCharges, ChargeModel};
        use molar::prelude::*;

        let (mut n_charged, mut n_neutral, mut worst) = (0usize, 0usize, 0f32);
        let mut worst_name = String::new();
        for m in load_refs() {
            let mut sys = match System::from_file(format!("tests/data/gaff_ref/sdf/{}.sdf", m.name))
            {
                Ok(s) => s,
                Err(_) => continue,
            };
            let q_total: i32 = sys.iter_atoms().map(|a| a.get_formal_charge().unwrap_or(0)).sum();
            sys.apply_charges(ChargeModel::Espaloma).unwrap();
            let sum: f32 = sys.iter_atoms().map(|a| a.get_charge() as f32).sum();
            let d = (sum - q_total as f32).abs();
            if d > worst {
                worst = d;
                worst_name = format!("{} (Q={q_total} Σq={sum:.4})", m.name);
            }
            if q_total == 0 {
                n_neutral += 1;
            } else {
                n_charged += 1;
            }
        }
        println!(
            "Σq vs Σfc: {n_charged} charged + {n_neutral} neutral molecules, \
             max|Σq − Q|={worst:.2e}  worst={worst_name}"
        );
        assert!(n_charged > 0, "corpus must contain charged molecules to exercise this path");
        assert!(worst < 1e-3, "total charge not preserved: max deviation {worst} ({worst_name})");
    }

    /// Aromatic input is kekulized rather than rejected. Before this, an SDF order-4 record — or
    /// any system run through `System::perceive` — could not be charged at all.
    ///
    /// Charging the aromatized structure must reproduce the Kekulé result: the resonance form
    /// kekulization picks is arbitrary, but espaloma's featurization of an aromatic ring is
    /// symmetric enough that the predicted charges land in the same place.
    #[test]
    fn aromatic_input_is_kekulized_not_rejected() {
        use crate::{ApplyCharges, ChargeModel};
        use molar::prelude::*;

        let mut checked = 0usize;
        let mut worst = 0f32;
        let mut worst_name = String::new();
        for m in load_refs().iter().take(80) {
            let path = format!("tests/data/gaff_ref/sdf/{}.sdf", m.name);
            let Ok(mut kek) = System::from_file(&path) else { continue };
            let Ok(mut arom) = System::from_file(&path) else { continue };

            // Only molecules that actually have an aromatic ring exercise the new path.
            arom.perceive();
            if !arom.iter_bonds().any(|b| b.order() == BondOrder::Aromatic) {
                continue;
            }

            kek.apply_charges(ChargeModel::Espaloma).unwrap();
            arom.apply_charges(ChargeModel::Espaloma)
                .unwrap_or_else(|e| panic!("{}: aromatic input rejected: {e}", m.name));

            let d = kek
                .iter_atoms()
                .zip(arom.iter_atoms())
                .map(|(a, b)| (a.get_charge() as f32 - b.get_charge() as f32).abs())
                .fold(0.0f32, f32::max);
            if d > worst {
                worst = d;
                worst_name = m.name.clone();
            }
            checked += 1;
        }
        println!("aromatic vs Kekulé charges over {checked} molecules: max|Δ|={worst:.2e} ({worst_name})");
        assert!(checked > 10, "corpus should supply aromatic molecules to check");
        assert!(worst < 1e-3, "aromatic path disagrees with Kekulé: {worst} ({worst_name})");
    }

    /// The equilibration is affine in the total charge: `q(Q) = q(0) + Q·(1/s_i)/Σ(1/s_j)`.
    /// This pins the exact relationship between the two conventions, which is what lets
    /// `corpus_rmse_vs_reference` keep using the `Q = 0` reference.
    #[test]
    fn equilibration_is_affine_in_total_charge() {
        let e = [0.3f32, -0.7, 1.1, 0.05, -0.2];
        let s = [1.7f32, 2.3, 0.9, 3.1, 1.2];
        let inv_sum: f32 = s.iter().map(|x| 1.0 / x).sum();

        let q0 = equilibrate(&e, &s, 0.0);
        for &q_total in &[-2.0f32, -1.0, 1.0, 3.0] {
            let q = equilibrate(&e, &s, q_total);
            let sum: f32 = q.iter().sum();
            assert!((sum - q_total).abs() < 1e-5, "Σq={sum} != Q={q_total}");
            for i in 0..e.len() {
                let expected = q0[i] + q_total * (1.0 / s[i]) / inv_sum;
                assert!(
                    (q[i] - expected).abs() < 1e-5,
                    "atom {i}: {} != {expected} at Q={q_total}",
                    q[i]
                );
            }
        }
    }
}