Skip to content

fix: correct analytical Coulomb potential for p-type Gaussians (#342) - #343

Merged
PaulWAyers merged 5 commits into
theochem:masterfrom
alok-108:fix-342-coulomb-gaussian-p
Oct 22, 2026
Merged

PaulWAyers merged 5 commits into
theochem:masterfrom
alok-108:fix-342-coulomb-gaussian-p

Conversation

@alok-108

Copy link
Copy Markdown
Contributor

Description

Fixes #342.

This PR corrects a fundamental mathematical flaw in grid.coulomb.coulomb_gaussian_p where the analytical electrostatic potential returned for $p$-type radial Gaussian charge densities violated Poisson's equation $\nabla^2 V(\mathbf{r}) = -4\pi \rho(\mathbf{r})$.


Root Cause & Mathematical Correction

For a normalized $p$-type radial Gaussian charge density:

$$\rho(r) = \frac{2}{3} \frac{\alpha^{5/2}}{\pi^{3/2}} r^2 e^{-\alpha r^2}$$

The exact solution to Poisson's equation with $V(\infty) = 0$ is obtained by radial integration of Gauss's law:

$$V(r) = \frac{4\pi}{r} \int_0^r \rho(r') r'^2 dr' + 4\pi \int_r^\infty \rho(r') r' dr'$$

Evaluating the integrals:

  1. Interior charge:
    $$\frac{4\pi}{r} \int_0^r \rho(r') r'^2 dr' = \frac{\mathrm{erf}(\sqrt{\alpha} r)}{r} - \frac{4\alpha^{3/2}}{3\sqrt{\pi}} r^2 e^{-\alpha r^2} - 2 \sqrt{\frac{\alpha}{\pi}} e^{-\alpha r^2}$$
  2. Exterior shell:
    $$4\pi \int_r^\infty \rho(r') r' dr' = \frac{4\alpha^{3/2}}{3\sqrt{\pi}} r^2 e^{-\alpha r^2} + \frac{4}{3} \sqrt{\frac{\alpha}{\pi}} e^{-\alpha r^2}$$

Summing both integrals cancels the $r^2 e^{-\alpha r^2}$ terms identically and leaves:

$$V_{\text{exact}}(r) = \frac{\mathrm{erf}(\sqrt{\alpha} r)}{r} - \frac{2}{3} \sqrt{\frac{\alpha}{\pi}} e^{-\alpha r^2}$$

The previous implementation in coulomb_gaussian_p had omitted the $-2\sqrt{\alpha/\pi} e^{-\alpha r^2}$ term from the interior integral, leaving an erroneous $+ \frac{4}{3} \sqrt{\frac{\alpha}{\pi}} e^{-\alpha r^2}$. This caused:

  • Wrong sign and magnitude for the Gaussian tail term.
  • A +150% error at the nucleus ($r \to 0$): $\frac{10}{3}\sqrt{\alpha/\pi}$ instead of the physical limit $\frac{4}{3}\sqrt{\alpha/\pi}$.
  • Unnormalized limit was erroneously $5\pi/\alpha^2$ instead of $2\pi/\alpha^2$.

Graphical Proof & Poisson Equation Verification

Poisson Equation Verification and Potential Comparison


Commit Structure (5 Atomic Commits)

This PR is organized into 5 logical commits:

  1. fix(coulomb): correct p-type Gaussian potential formula and nuclear limit
    • Corrected term2 sign and magnitude to $-(2/3) \sqrt{\alpha/\pi} e^{-\alpha r^2}$.
    • Updated $r \to 0$ threshold limit to $(4/3) \sqrt{\alpha/\pi}$.
  2. docs(coulomb): update p-type Gaussian Coulomb potential docstrings
    • Updated Sphinx docstrings with the exact analytical formulas and physical limits.
  3. test(coulomb): update unit tests for corrected p-type Gaussian potential
    • Updated existing test_coulomb_gaussian_p() test expectations for both normalized and unnormalized cases.
  4. test(coulomb): add Poisson equation numerical validation for p-type Gaussians
    • Added test_coulomb_gaussian_p_poisson_equation() testing $\nabla^2 V(r) = -4\pi \rho(r)$ via numerical spherical Laplacian differentiation across diverse $\alpha$ values.
  5. test(coulomb): add multi-center p-type Gaussian tests in coulomb_potential
    • Added test_coulomb_potential_with_p_gaussians() testing combined multi-center $s$-type and $p$-type Gaussian superposition at nuclear and off-nuclear points.

Verification

All 11 tests in tests/test_coulomb.py and the complete test suite pass. Ruff formatting and linting pass without errors.

@PaulWAyers
PaulWAyers requested review from Ao-chuba and marco-2023 and a balanced review from Copilot October 11, 2026 18:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The corrected formulas are mathematically consistent and adequately covered by focused regression tests.

0 open findings

What changed in this PR

Corrects the analytical Coulomb potential for p-type Gaussian charge densities to satisfy Poisson’s equation.

Changes:

  • Fixes normalized and unnormalized potential formulas and nuclear limits.
  • Updates mathematical documentation.
  • Adds direct, multi-center, and Poisson-equation tests.
File Description
src/​grid/​coulomb.py Corrects the p-type Gaussian potential formula and limit.
src/​grid/​tests/​test_coulomb.py Updates expectations and adds regression coverage.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@Ao-chuba Ao-chuba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per my understandings the fix (docstring, test ) and all updated mathematical values are correct and good to go.

One minor observation on test_coulomb_potential_with_p_gaussians, the expected value is built by calling coulomb_gaussian_s and coulomb_gaussian_p directly, which are the same functions coulomb_potential calls internally so the test cannot catch bugs internal to those functions themselves (though it does still test the aggregation/argument passing logic correctly). Consider using strict=True in the zip calls as well. but yes, these are optional changes.

What do you think, @marco-2023?

@PaulWAyers PaulWAyers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @alok-108

@PaulWAyers
PaulWAyers merged commit 5201937 into theochem:master Oct 22, 2026
10 checks passed
@alok-108

Copy link
Copy Markdown
Contributor Author

Thanks @PaulWAyers for merging, and @Ao-chuba for the review.

@alok-108
alok-108 deleted the fix-342-coulomb-gaussian-p branch October 22, 2026 20:41
@PaulWAyers

Copy link
Copy Markdown
Member

Always good to add more tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mathematical error in coulomb_gaussian_p: Potential violates Poisson's equation \nabla^2 V = -4\pi \rho

4 participants