Skip to content

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

Closed

Description

@alok-108

Description

In grid.coulomb.coulomb_gaussian_p, the analytical formula used to evaluate the electrostatic Coulomb potential of a $p$-type radial Gaussian charge density is mathematically incorrect. Specifically, the potential returned by coulomb_gaussian_p violates Poisson's equation:

$$\nabla^2 V(\mathbf{r}) = -4\pi \rho(\mathbf{r})$$

The current implementation overestimates the potential near the origin, leading to a +150% error at the nucleus ($r \to 0$) and corrupts electrostatic potential calculations whenever $p$-type Gaussians are evaluated via coulomb_gaussian_p or coulomb_potential.


Mathematical Derivation & Proof of the Flaw

A normalized $p$-type radial Gaussian charge density is given by:

$$\rho(r) = \frac{2}{3} \frac{\alpha^{5/2}}{\pi^{3/2}} r^2 e^{-\alpha r^2}, \quad \text{such that } \int \rho(\mathbf{r}) d\mathbf{r} = 4\pi \int_0^\infty \rho(r) r^2 dr = 1$$

For any spherically symmetric charge density $\rho(r)$, the exact solution to Poisson's equation $\nabla^2 V = -4\pi \rho$ with boundary condition $V(\infty) = 0$ is derived from 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 each integral for $\rho(r') = \frac{2}{3} \frac{\alpha^{5/2}}{\pi^{3/2}} r'^2 e^{-\alpha r'^2}$:

1. First Integral (Interior charge contribution):

$$\frac{4\pi}{r} \int_0^r \rho(r') r'^2 dr' = \frac{8\alpha^{5/2}}{3\sqrt{\pi} r} \int_0^r r'^4 e^{-\alpha r'^2} dr'$$

Integrating by parts:
$$\int_0^r r'^4 e^{-\alpha r'^2} dr' = -\frac{r^3}{2\alpha} e^{-\alpha r^2} - \frac{3r}{4\alpha^2} e^{-\alpha r^2} + \frac{3\sqrt{\pi}}{8\alpha^{5/2}} \mathrm{erf}(\sqrt{\alpha} r)$$

Multiplying through by $\frac{8\alpha^{5/2}}{3\sqrt{\pi} r}$:
$$\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. Second Integral (Exterior shell contribution):

$$4\pi \int_r^\infty \rho(r') r' dr' = \frac{8\alpha^{5/2}}{3\sqrt{\pi}} \int_r^\infty r'^3 e^{-\alpha r'^2} dr'$$

Integrating by parts:
$$\int_r^\infty r'^3 e^{-\alpha r'^2} dr' = \frac{r^2}{2\alpha} e^{-\alpha r^2} + \frac{1}{2\alpha^2} e^{-\alpha r^2}$$

Multiplying through by $\frac{8\alpha^{5/2}}{3\sqrt{\pi}}$:
$$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}$$

3. Summing Both Integrals:

Notice that the $r^2 e^{-\alpha r^2}$ terms cancel out identically:
$$\left( -\frac{4\alpha^{3/2}}{3\sqrt{\pi}} + \frac{4\alpha^{3/2}}{3\sqrt{\pi}} \right) r^2 e^{-\alpha r^2} = 0$$

And the $e^{-\alpha r^2}$ terms combine as:
$$\left( -2 + \frac{4}{3} \right) \sqrt{\frac{\alpha}{\pi}} e^{-\alpha r^2} = -\frac{2}{3} \sqrt{\frac{\alpha}{\pi}} e^{-\alpha r^2}$$

Therefore, the exact Coulomb potential is:

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


Where the Existing Code Differs

In src/grid/coulomb.py (lines 110-111 & 147-150):

# CURRENT IMPLEMENTATION (INCORRECT):
term2 = (4.0 / 3.0) * (sqrt_alpha / np.sqrt(np.pi)) * np.exp(-alpha * r**2)
out = term1 + term2
out[r < _R_ZERO_THRESHOLD] = (10.0 / 3.0) * (sqrt_alpha / np.sqrt(np.pi))

The code omitted the $-2 \sqrt{\frac{\alpha}{\pi}} e^{-\alpha r^2}$ term from the interior integral, leaving only $+\frac{4}{3}$, which results in:

  1. Wrong sign and magnitude for the Gaussian tail: $+\frac{4}{3}$ instead of $-\frac{2}{3}$.
  2. Severe discrepancy at the nucleus ($r \to 0$):
    • Correct limit: $\lim_{r\to 0} V_{\text{exact}}(r) = 2\sqrt{\frac{\alpha}{\pi}} - \frac{2}{3}\sqrt{\frac{\alpha}{\pi}} = \mathbf{\frac{4}{3}\sqrt{\frac{\alpha}{\pi}}}$.
    • Current code: $\lim_{r\to 0} V_{\text{code}}(r) = 2\sqrt{\frac{\alpha}{\pi}} + \frac{4}{3}\sqrt{\frac{\alpha}{\pi}} = \mathbf{\frac{10}{3}\sqrt{\frac{\alpha}{\pi}}}$.
    • Relative error at the nucleus: $\frac{10/3 - 4/3}{4/3} = \mathbf{+150%}$.
  3. Unnormalized case:
    • Formula in docstring and code: $\frac{3\pi^{3/2}}{2\alpha^{5/2}} \frac{\mathrm{erf}(\sqrt{\alpha}r)}{r} + \frac{2\pi}{\alpha^2} e^{-\alpha r^2}$.
    • Correct physical formula: $\frac{3\pi^{3/2}}{2\alpha^{5/2}} \frac{\mathrm{erf}(\sqrt{\alpha}r)}{r} - \frac{\pi}{\alpha^2} e^{-\alpha r^2}$.
    • Correct $r\to 0$ limit: $\frac{2\pi}{\alpha^2}$ (current code erroneously returned $\frac{5\pi}{\alpha^2}$).

Graphical Proof & Poisson Equation Verification

Poisson Equation Verification and Potential Comparison

As demonstrated in the comparison plot above:

  1. Left Panel: The current potential deviates significantly from the exact solution, with a large artificial spike at $r=0$.
  2. Center Panel: Discrepancy peaks directly at the nucleus with $\Delta V(0) = 2 \sqrt{\frac{\alpha}{\pi}}$ Hartree.
  3. Right Panel: Evaluating the spherical Laplacian $\nabla^2 V = \frac{d^2 V}{dr^2} + \frac{2}{r} \frac{dV}{dr}$ reveals that:
    • $V_{\text{exact}}$ matches the target $-4\pi \rho(r)$ with numerical precision.
    • $V_{\text{current}}$ violently violates Poisson's equation ($\max |\nabla^2 V - (-4\pi\rho)| \approx 18.9$ for $\alpha=2.0$).

Reproduction Script

import numpy as np
from scipy.special import erf
from grid.coulomb import coulomb_gaussian_p

alpha = 2.0
r = np.linspace(0.01, 3.0, 1000)
dr = r[1] - r[0]

# Density
rho = (2.0 / 3.0) * (alpha**2.5 / np.pi**1.5) * (r**2) * np.exp(-alpha * r**2)
target_laplacian = -4.0 * np.pi * rho

# Potential from grid.coulomb
v = coulomb_gaussian_p(r, alpha, normalized=True)

# Numerical Laplacian d2V/dr2 + (2/r)*dV/dr
dv_dr = np.gradient(v, dr)
d2v_dr2 = np.gradient(dv_dr, dr)
lap_computed = d2v_dr2 + (2.0 / r) * dv_dr

# Check error against Poisson equation
residual = np.max(np.abs(lap_computed[10:-10] - target_laplacian[10:-10]))
print("Max residual against -4*pi*rho:", residual)
# Output: 18.91989... (Violates Poisson's equation!)

Solution

  1. In coulomb_gaussian_p:
    • Change term2 = -(2.0 / 3.0) * (sqrt_alpha / np.sqrt(np.pi)) * np.exp(-alpha * r**2).
    • Update $r \to 0$ limit to (4.0 / 3.0) * (sqrt_alpha / np.sqrt(np.pi)).
  2. Update docstrings in coulomb.py for both normalized and unnormalized definitions.
  3. Update unit tests in tests/test_coulomb.py with the correct reference values and add an explicit test asserting $\nabla^2 V = -4\pi \rho$.

Activity

  1. PaulWAyers commented on Oct 22, 2026

    @PaulWAyers
    Member

    If you don't get the PR approved in the next week, ping on the issue and I'll get it done myself.

  2. Ao-chuba commented on Oct 22, 2026

    @Ao-chuba
    Member

    @PaulWAyers since i dont have the access to approve.(only read) so either marco or you (or any one with the access) will have to approve. from my side i have approved and given my feedback in the PR discussion

  3. PaulWAyers commented on Oct 22, 2026

    @PaulWAyers
    Member

    Thanks @Ao-chuba . I have upgraded your status on this repository, too.

  4. alok-108 commented on Oct 22, 2026

    @alok-108
    ContributorAuthor

    Thanks both. #343 is approved now, so it just needs a merge. Happy to rebase if needed.

  5. added a commit that references this issue on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions