cclib-mojo 0.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,338 @@
1
+ Metadata-Version: 2.5
2
+ Name: cclib-mojo
3
+ Version: 0.2.5
4
+ Summary: Fast electron-density and wavefunction-on-grid evaluation for cclib-style basis sets, powered by a Mojo kernel (pure-Python fallback included).
5
+ Project-URL: Repository, https://github.com/thyn-ai/mojo-kernels
6
+ Project-URL: Company, https://thyn.ai
7
+ Author: Thyn
8
+ Maintainer: Thyn
9
+ License-Expression: Apache-2.0
10
+ Keywords: cclib,computational-chemistry,cube-file,electron-density,gaussian-basis,mojo,wavefunction
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: Scientific/Engineering :: Chemistry
15
+ Classifier: Topic :: Scientific/Engineering :: Physics
16
+ Requires-Python: >=3.10
17
+ Requires-Dist: numpy>=1.21
18
+ Description-Content-Type: text/markdown
19
+
20
+ # cclib-mojo
21
+
22
+ **Fast electron-density and wavefunction-on-grid evaluation for
23
+ [cclib](https://cclib.github.io/)** — the computational-chemistry parsing
24
+ library — powered by a Mojo kernel, with a vendored NumPy fallback. It
25
+ accelerates the `cclib.method.volume` path (`Volume.wavefunction()` /
26
+ `electrondensity()`), whose PyQuante backend evaluates contracted Gaussian
27
+ basis functions one grid point at a time in pure Python. Prebuilt
28
+ per-platform binaries mean **no Mojo toolchain is ever required** on an end
29
+ user's machine, and results match the PyQuante reference to 1e-10 relative
30
+ (measured agreement is at the 1e-13–1e-16 level).
31
+
32
+ It works two ways:
33
+
34
+ 1. **Alongside cclib** — `cclib_mojo.cclib_integration` mirrors
35
+ `cclib.method.volume`'s functions (no monkeypatching): parse with cclib,
36
+ evaluate with cclib-mojo, write cube files with cclib.
37
+ 2. **Standalone** — `density_on_grid()` / `wavefunction_on_grid()` take
38
+ cclib-style `gbasis` + geometry directly and return NumPy arrays in
39
+ cclib's `Volume.data` layout.
40
+
41
+ ## Install
42
+
43
+ The wheel for your platform is a signed asset of the matching
44
+ [GitHub Release](https://github.com/thyn-ai/mojo-kernels/releases) (bash or
45
+ zsh; PyPI distribution follows once the registry is enabled, see
46
+ [RELEASING.md § Registries](../../RELEASING.md#registries)):
47
+
48
+ ```bash
49
+ V=0.2.5 # x-release-please-version
50
+ case "$(uname -sm)" in
51
+ "Darwin arm64") WHEEL="cclib_mojo-$V-py3-none-macosx_14_0_arm64.whl" ;;
52
+ "Linux x86_64") WHEEL="cclib_mojo-$V-py3-none-manylinux_2_35_x86_64.whl" ;;
53
+ esac
54
+ pip install "https://github.com/thyn-ai/mojo-kernels/releases/download/v$V/${WHEEL:?no prebuilt wheel for this platform}"
55
+ ```
56
+
57
+ Per-platform wheels (macOS arm64, Linux x86_64) carry the native kernel,
58
+ self-contained (the Mojo runtime is vendored into the wheel; nothing to
59
+ compile, no absolute rpaths). On any other platform — including Windows, where CI runs this package's fallback suite
60
+ ([`windows-fallback`](https://github.com/thyn-ai/mojo-kernels/actions/workflows/windows-fallback.yml)) —
61
+ the same wheel API runs on the vendored NumPy fallback, silently and
62
+ correctly. There is no sdist: a source tarball cannot rebuild the native
63
+ library.
64
+
65
+ ## Quickstart
66
+
67
+ ```python
68
+ import numpy as np
69
+ from cclib_mojo import density_on_grid, wavefunction_on_grid
70
+
71
+ # Inputs exactly as cclib parses them from a logfile:
72
+ # gbasis -- per-atom list of (shell, [(exponent, coefficient), ...])
73
+ # atomcoords -- (n_atoms, 3) in Angstrom
74
+ # mocoeffs -- (n_mo, n_bf) MO coefficients, e.g. ccdata.mocoeffs[0]
75
+ gbasis = [...]
76
+ atomcoords = np.array([...])
77
+
78
+ # One molecular orbital on a grid (cclib's wavefunction() equivalent):
79
+ psi = wavefunction_on_grid(gbasis, atomcoords, mocoeffs[3],
80
+ origin=(-5, -5, -5), step=(0.2, 0.2, 0.2),
81
+ shape=(51, 51, 51))
82
+
83
+ # Electron density, sum over occupied MOs of |psi|^2
84
+ # (cclib's electrondensity_spin(); multiply by 2 for a closed shell):
85
+ nocc = 5
86
+ rho = density_on_grid(gbasis, atomcoords, mocoeffs[:nocc],
87
+ origin=(-5, -5, -5), step=(0.2, 0.2, 0.2),
88
+ shape=(51, 51, 51))
89
+ # rho[i, j, k] is the density at
90
+ # (origin[0]+i*step[0], origin[1]+j*step[1], origin[2]+k*step[2]) — the
91
+ # exact layout cclib's Volume.data and its cube writer use.
92
+ ```
93
+
94
+ A runnable version is `quickstart.py` at the repository root (water,
95
+ STO-3G):
96
+
97
+ ```
98
+ python quickstart.py
99
+ ```
100
+
101
+ ## Using it from cclib
102
+
103
+ `cclib_mojo.cclib_integration` provides drop-in equivalents of cclib's
104
+ volume functions — same call shapes, same return convention (a copy of the
105
+ `Volume` with `data` replaced), no monkeypatching:
106
+
107
+ ```python
108
+ import cclib
109
+ from cclib.method.volume import Volume
110
+ from cclib_mojo import cclib_integration # instead of cclib.method.volume
111
+
112
+ data = cclib.io.ccread("benzene.log")
113
+ vol = Volume(origin=(-7, -7, -7), topcorner=(7, 7, 7), spacing=(0.2, 0.2, 0.2))
114
+
115
+ # cclib: from cclib.method.volume import wavefunction
116
+ wf = cclib_integration.wavefunction(data, vol, data.mocoeffs[0][3])
117
+ wf.writeascube("mo4.cube")
118
+
119
+ # cclib: from cclib.method.volume import electrondensity
120
+ nocc = data.homos[0] + 1
121
+ dens = cclib_integration.electrondensity(data, vol, [data.mocoeffs[0][:nocc]])
122
+ dens.writeascube("density.cube")
123
+ ```
124
+
125
+ Equivalence with cclib's own `Volume.wavefunction()` / `electrondensity()`
126
+ (on its pyquante2 backend) is asserted in the differential test suite —
127
+ same grid points, same normalization, same cube-file ordering.
128
+
129
+ ## Benchmark
130
+
131
+ Measured with `benchmarks/bench_gaussgrid.py` in this repository (run
132
+ `pixi run bench-cclib` to reproduce). Workload: **benzene, 6-31G\*** (12
133
+ atoms, 102 contracted Cartesian basis functions, 192 primitives — basis
134
+ data from PyQuante 1.6.5's basis library), seeded MO coefficients, median
135
+ of 5 runs, single-threaded. Environment: **Apple M4 Max, macOS 26.6.2
136
+ arm64, Python 3.12.14, numpy 2.5.3, Mojo 1.1.0**, 2026-09-19.
137
+
138
+ | workload | PyQuante1 path (s) | cclib-mojo (s) | speedup |
139
+ |---|---:|---:|---:|
140
+ | wavefunction 1 MO, 50³ | 32.23 | 0.0126 | 2567x |
141
+ | wavefunction 1 MO, 100³ | 345.37 | 0.0491 | 7033x |
142
+ | density 3 MOs, 50³ | 63.59 | 0.0335 | 1898x |
143
+ | wavefunction 1 MO, 50³ — NumPy fallback | 32.23 | 0.1691 | 191x |
144
+
145
+ Setup is per-call on both sides (cclib rebuilds its PyQuante basis objects
146
+ on every `wavefunction()` call; we rebuild our flat arrays on every call):
147
+
148
+ | setup (per call) | PyQuante1 getbfs (ms) | cclib-mojo flatten (ms) |
149
+ |---|---:|---:|
150
+ | basis construction | 2.09 | 1.48 |
151
+
152
+ Correctness gate (asserted before every timing run, element-wise vs the
153
+ PyQuante1 reference): max abs diff **1.5e-12** — the values are the same
154
+ numbers, not approximations.
155
+
156
+ Honest framing, two baselines (both measured, same machine and workload):
157
+
158
+ - **vs the PyQuante1 path** (the table above): ~1,900–7,000×. cclib's hot
159
+ loop with a PyQuante-1 backend (`pyamp()` → per-point `CGBF.amp(x, y, z)`
160
+ in pure Python) is Python-2 era code that cannot execute on Python 3, so
161
+ the benchmark drives a verbatim Python-3 transcription of that amplitude
162
+ path (`tests/pyquante1_oracle.py`, same formulas, same operation order,
163
+ same per-point interpreter cost profile) exactly the way cclib drives it.
164
+ - **vs cclib's pyquante2 backend** (NumPy-vectorized `cgbf.mesh`, the
165
+ fastest backend cclib ships today): cclib's real
166
+ `Volume.wavefunction()` takes **0.689 s** for the same 50³ single-MO
167
+ workload vs **0.0095 s** for cclib-mojo — **72×**, with max abs diff
168
+ 8.4e-13. The remaining gap over the PyQuante1 table is the numpy
169
+ vectorization in pyquante2, which still pays per-(basis-function, grid)
170
+ temporaries; the Mojo kernel pays one SIMD FMA per (point, primitive).
171
+
172
+ Why the gap is so large: the PyQuante path interprets ~5 Python operations
173
+ per (grid point × basis function × primitive) — hundreds of millions of
174
+ interpreter steps for a production cube grid. The Mojo kernel precomputes
175
+ the exactly-separable axis factors `exp(-a·dx²)·exp(-a·dy²)·exp(-a·dz²)`
176
+ once per call, then runs one SIMD fused multiply-add per (point, primitive)
177
+ in compiled float64, with the grid row under update kept cache-resident.
178
+
179
+ Why the gap is so large: the PyQuante path interprets ~5 Python operations
180
+ per (grid point × basis function × primitive) — hundreds of millions of
181
+ interpreter steps for a production cube grid. The Mojo kernel precomputes
182
+ the exactly-separable axis factors `exp(-a·dx²)·exp(-a·dy²)·exp(-a·dz²)`
183
+ once per call, then runs one SIMD fused multiply-add per (point, primitive)
184
+ in compiled float64, with the grid row under update kept cache-resident.
185
+
186
+ ## How it works
187
+
188
+ ```
189
+ pip install <cclib_mojo wheel from the GitHub Release>
190
+ │
191
+ ▼
192
+ cclib_mojo (thin Python wrapper)
193
+ │ validates gbasis/geometry, computes THO primitive norms and
194
+ │ contracted norms (shared by both backends), flattens to typed arrays
195
+ ▼
196
+ libgaussgridmojo.dylib / .so (Mojo kernel, C ABI v1)
197
+ │ gaussgridmojo_eval: whole grid per call, SIMD over grid points
198
+ ▼
199
+ float64 grid in cclib's Volume.data layout
200
+ ```
201
+
202
+ - **Batch-shaped C ABI**: one call evaluates the whole 3-D grid for one MO
203
+ (or accumulates the density over all MOs); FFI overhead is per-call, not
204
+ per-point.
205
+ - **Reference-matching arithmetic**: normalization constants are computed
206
+ in Python with PyQuante's exact formulas and operation order (THO
207
+ eq. 2.2 and 2.12); the kernel evaluates the textbook contracted
208
+ Cartesian Gaussian in IEEE-754 float64 and skips exactly-zero MO
209
+ coefficients, the same rule cclib uses.
210
+ - **ABI handshake**: the wrapper checks `gaussgridmojo_abi_version()`
211
+ before evaluating; a mismatch falls back cleanly.
212
+ - **Conventions verified against cclib**: centers converted Å→bohr with
213
+ cclib's `convertor` constant (×1.8897261245), grid axes divided by
214
+ cclib's bohr→Å constant (÷0.5291772109 — the two are not exact
215
+ reciprocals and we mirror cclib exactly), shell expansion order of
216
+ cclib's `sym2powerlist` (S/P/D/F), `Volume.data` C-ordering (x outer,
217
+ z inner), and the `abs(coeff) > 0.0` inclusion rule.
218
+
219
+ ## Fallback semantics
220
+
221
+ There is no Windows Mojo toolchain today, and a shared library can always
222
+ go missing — so the wrapper **falls back to a vendored NumPy reference**
223
+ (`cclib_mojo/_reference.py`, clean-room, NumPy-only):
224
+
225
+ - Resolution order: `$CCLIB_MOJO_NATIVE_LIB` → the library bundled in the
226
+ wheel → the repo development build output.
227
+ - `CCLIB_MOJO_DISABLE_NATIVE=1` forces the fallback (the test suite runs
228
+ this way as its second pass).
229
+ - Both backends share basis flattening and normalization in
230
+ `cclib_mojo/_basis.py`, so they cannot disagree about constants; the
231
+ differential suite asserts both against PyQuante.
232
+ - Inspect what's active: `cclib_mojo.backend_info()` and
233
+ `cclib_mojo.native_available()`.
234
+ - Wheels are **per-platform** (`py3-none-macosx_*_arm64`,
235
+ `py3-none-manylinux_*_x86_64`) and **wheel-only**. Each wheel is
236
+ **self-contained**: `delocate` (macOS) / `auditwheel repair` (Linux)
237
+ vendor the Mojo runtime libraries and rewrite load paths to be
238
+ wheel-relative. (Redistribution terms for Modular's runtime binaries
239
+ should be confirmed with Modular before any public release.) A pure
240
+ `py3-none-any` fallback wheel can be produced with
241
+ `CCLIB_MOJO_ALLOW_PURE_WHEEL=1` (the wheel CI builds and tests on Windows: [`windows-fallback`](https://github.com/thyn-ai/mojo-kernels/actions/workflows/windows-fallback.yml)).
242
+
243
+ ## Differential tests
244
+
245
+ ```
246
+ pixi run test-cclib # builds the kernel, then runs the suite twice:
247
+ # once native, once with CCLIB_MOJO_DISABLE_NATIVE=1
248
+ ```
249
+
250
+ The suite (`tests/test_gaussgrid_*.py`) compares cclib-mojo against the
251
+ PyQuante reference within 1e-10 relative on deterministic fixtures: s/p/d/f
252
+ shells (STO-3G H2O, 6-31G\* d on O, cc-pVTZ f on C), multiple atoms, MO
253
+ amplitude vs summed density, non-cubic anisotropic grids, grids offset from
254
+ the origin and from all atoms, exact-zero coefficients, grid-point
255
+ ordering, and validation errors — plus end-to-end equivalence with cclib's
256
+ actual `Volume.wavefunction()` / `electrondensity()` on their pyquante2
257
+ backend. Basis-set provenance is documented in
258
+ `tests/gaussgrid_fixtures.py` (all values from PyQuante 1.6.5's published
259
+ basis library). 19 tests pass per backend (38 per full run), and the
260
+ benchmark asserts the same 1e-10 gate before every timing run.
261
+
262
+ ## Input validation
263
+
264
+ `cclib_mojo` validates inputs fail-fast and raises structured errors:
265
+
266
+ - `BasisError` for malformed basis-set or geometry input.
267
+ - `GridError` for malformed grid or MO-coefficient input.
268
+
269
+ Both are `ValueError` subclasses and carry a descriptive message naming the
270
+ offending value. Gaussian exponents must lie inside
271
+ `[1e-100, 1e60] bohr^-2`; atom coordinates and grid coordinates must satisfy
272
+ `|x| <= 1e100` Angstrom. These bounds are tens of orders of magnitude beyond
273
+ anything a quantum-chemistry logfile can contain (real basis sets span roughly
274
+ `1e-3..1e6` bohr^-2), but they prevent the THO normalization arithmetic from
275
+ overflowing or underflowing.
276
+
277
+ The grid terms themselves are bounded too. For every basis function with a
278
+ non-zero MO coefficient,
279
+ `max(1, |c| N_c) * max(1, |x-cx|)^l * max(1, |y-cy|)^m * max(1, |z-cz|)^n * max(1, sum_p |w_p|)`
280
+ must not exceed `1e140`; otherwise `GridError` is raised. Here `|c|` is the
281
+ largest coefficient over the evaluated MO rows, `N_c` the contracted norm,
282
+ `w_p` the normalized primitive weights, and `|x-cx|` (likewise `y`, `z`) the
283
+ distance from the function's centre to the farthest grid point along that
284
+ axis **in bohr**: the check runs on the converted coordinates the backends
285
+ use (grid points divided by 0.5291772109 and centres multiplied by
286
+ 1.8897261245, cclib's constants), so an Angstrom distance `d` enters as
287
+ roughly `1.89 d`. The bound is evaluated in log10 space, so the check itself
288
+ cannot overflow. Realistic basis sets and grids stay below roughly `1e10`.
289
+
290
+ The bound prevents overflow: below it no intermediate product overflows in
291
+ either backend's evaluation order, and densities (`psi^2`) stay finite. It
292
+ does not make the two backends bit-identical. The native kernel sums the
293
+ terms with fused multiply-adds, factors each Gaussian per axis and uses
294
+ Mojo's `exp`, whose relative error is about 4e-13 times its argument; the
295
+ NumPy fallback sums each contraction first and evaluates one `exp` per
296
+ primitive. The backends therefore agree within a condition-aware rounding
297
+ bound. For an MO amplitude it scales with the sum of the absolute values of
298
+ the terms rather than with the result; for a density it is each MO's
299
+ amplitude bound times about twice that MO's amplitude, plus a few ulps of
300
+ the density. On physical inputs that sum is close to the result, and the
301
+ agreement is what the differential suite asserts (1e-10 relative, 1e-12
302
+ absolute). Where opposite-sign primitive or MO coefficients cancel
303
+ (for example a contraction with coefficients `+1e8` and `-1e8` on the same
304
+ exponent), the result can be many orders of magnitude smaller than its
305
+ terms; the backends can then differ by more than 1e-10 of the result while
306
+ both stay within the bound. The differential fuzzing (`fuzz/fuzz_cclib.py`)
307
+ enforces that bound and documents its derivation.
308
+
309
+ ## Scope and limitations
310
+
311
+ - Shells S/P/D/F (Cartesian), exactly matching cclib's `sym2powerlist`.
312
+ G shells and spherical-harmonic (5d/7f) functions are not supported —
313
+ cclib's volume path does not handle them either.
314
+ - Single-threaded kernel (determinism first); multithreading is future
315
+ work and would multiply the speedup.
316
+ - cclib is *not* a dependency: `cclib_mojo.cclib_integration` duck-types
317
+ `ccdata`/`Volume` objects. numpy is the only hard dependency.
318
+
319
+ ## Citing
320
+
321
+ If you use cclib-mojo in academic work, please cite both cclib and this
322
+ package:
323
+
324
+ > cclib-mojo: Mojo-accelerated electron-density and wavefunction-on-grid
325
+ > evaluation for cclib. thyn-ai, 2026. https://github.com/thyn-ai/mojo-kernels
326
+ > (citation file with DOI forthcoming)
327
+
328
+ cclib itself: N. M. O'Boyle, A. L. Tenderholt, K. M. Langner, *cclib: A
329
+ library for package-independent computational chemistry algorithms*,
330
+ J. Comput. Chem. 29 (2008) 839–845. PyQuante: R. P. Muller, *PyQuante:
331
+ Python Quantum Chemistry*.
332
+
333
+ ## License
334
+
335
+ Apache-2.0, © 2026 Algenta The kernel and wrapper are clean-room
336
+ implementations of the textbook contracted-Gaussian formulas (Taketa,
337
+ Huzinaga, O-ohata, J. Phys. Soc. Jap. 21, 2313 (1966)). cclib and PyQuante
338
+ are used only as test/benchmark references, never as runtime dependencies.
@@ -0,0 +1,9 @@
1
+ cclib_mojo/__init__.py,sha256=R3u6jvaGMIt8IQP0e-KIby67U6u38mfyFTWwqDbXv7Y,1489
2
+ cclib_mojo/_basis.py,sha256=VlEn5I-gX_RBBk3eX1Ck5QBtqZPNZr88psAqEICx69E,13287
3
+ cclib_mojo/_native.py,sha256=42fmAukKsTJG6884Rg2Y6Jcz9ccWqYn_wN6sGGrwJ_8,9032
4
+ cclib_mojo/_reference.py,sha256=-FavEwdVJG4BetfU09n9rR7Cmx2l19GOCNn1UaH5KW0,3162
5
+ cclib_mojo/cclib_integration.py,sha256=Y1S8HNsuY84PqYjabvjdf1B6nexHsOjDhaOXrP0oH3w,3371
6
+ cclib_mojo/core.py,sha256=UWhY2rICwK9FDQEAo9dekxx77hRVHGVCzX7sgXq6-7Q,11064
7
+ cclib_mojo-0.2.5.dist-info/METADATA,sha256=rDm-cnpu4tkkjv0rxnYjF52XjcL0pssqa0Sar5GMbqo,16378
8
+ cclib_mojo-0.2.5.dist-info/WHEEL,sha256=THafob7ofN-NsuMN7Mg4qZyHaQI7KkD-QlcQatYhXPo,87
9
+ cclib_mojo-0.2.5.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.3
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any