From a source to a potential
grid.poisson(source) returns a field \(V\) satisfying
The source \(s\) specifies the right-hand side of this equation as a density per physical volume, and FLODI includes the volume weights when performing the solve. Its physical interpretation comes from your model: a total density, a one-electron density, and an orbital-product source can have identical array shapes while representing different quantities. Establishing that meaning first helps you connect the returned potential to the expression in which you intend to use it.
A complete small example
import numpy as np
from flodi import RadialGrid
grid = RadialGrid.uniform(n_r=200, r_max=6.0)
source = grid.sample(lambda c: np.exp(-c.r**2) / np.pi**1.5)
potential = grid.poisson(source)
residual = grid.laplacian(potential) + 4 * np.pi * source
residual_norm = np.sqrt(grid.integrate(residual.conj(), residual).real)
print(grid.integrate(source), residual_norm)
Here the returned potential already carries the matched outer values needed by the residual’s Laplacian. Substituting it into the discrete equation checks the solve on this particular grid. The worked example below takes the next step by comparing with an independently known potential, so that you can also examine the spatial approximation and the effect of the omitted source tail.
Which boundary is used?
By default, FLODI matches to a source-free exterior that decays at infinity. A Coulomb potential generally remains nonzero at the finite outer face. The source field’s stored boundary describes the source; it does not prescribe the potential boundary. See Boundary conditions belong to the operation.
For a manufactured-solution test, outer_boundary can prescribe known
potential face values. Changing this argument changes the boundary-value
problem. Retain the default isolated condition when that is the problem
you intend to solve.
Signed sources
Poisson’s equation is linear. The source can change sign or be complex; normalizing it as though it were an orbital or a positive probability density would change the problem.
The potential is a field, not an interaction energy. Return to your energy expression to determine the source convention, occupations, and pair prefactors needed for a scalar energy. Check this bookkeeping separately from the Poisson residual.
Decay at infinity permits a nonzero value at the grid edge.
Gaussian potential
Compare the potential with \(\operatorname{erf}(r)/r\) and check the discrete Poisson residual separately.
Sample the normalized Gaussian source per unit volume.
Call poisson with its default isolated boundary.
Compare with \(\operatorname{erf}(r)/r\) and evaluate \(\nabla^2 V+4\pi\,\mathrm{source}\).
Plot the computed potential against the reference and inspect the potential error.
Maximum potential error: 2.812e-04
Maximum discrete residual: 2.966e-10
200 cells, r_max = 6.0. Final digits and iterative residuals may vary.
The reference includes the infinite Gaussian tail; the numerical source is truncated. Cell centres avoid r = 0.
The computed isolated Gaussian potential follows erf(r)/r and retains a nonzero tail at r equals six; the second panel shows the potential error.
Complete example
1"""Compare an isolated Gaussian potential with erf(r)/r and plot the error."""
2from pathlib import Path
3
4import matplotlib.pyplot as plt
5import numpy as np
6from scipy.special import erf
7
8from flodi import RadialGrid
9
10FIGURE_DIRECTORY = Path("figures")
11
12
13def main():
14 # 1. Sample a source per unit physical volume.
15 grid = RadialGrid.uniform(n_r=200, r_max=6.0)
16 source = grid.sample(lambda c: np.exp(-c.r**2) / np.pi**1.5)
17
18 # 2. Solve with the default isolated, decaying exterior.
19 potential = grid.poisson(source)
20
21 # 3. Check the potential error and discrete residual separately.
22 exact = grid.sample(lambda c: erf(c.r) / c.r)
23 error = potential - exact
24 residual = grid.laplacian(potential) + 4 * np.pi * source
25 print(f"Maximum potential error: {np.max(np.abs(error.values)):.3e}")
26 print(f"Maximum discrete residual: {np.max(np.abs(residual.values)):.3e}")
27
28 # 4. Plot the potential and its error; the residual is a separate check.
29 plot_result(grid.r, potential.values, exact.values, error.values)
30
31
32def plot_result(r, potential, exact, error):
33 with plt.rc_context({
34 "font.family": "sans-serif", "font.sans-serif": ["Liberation Sans", "DejaVu Sans"],
35 "font.size": 11, "mathtext.fontset": "stixsans", "axes.labelsize": 11,
36 "axes.spines.top": False, "axes.spines.right": False, "axes.linewidth": 0.7,
37 "axes.edgecolor": "#aaa69e", "axes.labelcolor": "#444444", "text.color": "#444444",
38 "xtick.color": "#77736b", "ytick.color": "#77736b",
39 "xtick.labelsize": 10, "ytick.labelsize": 10, "svg.fonttype": "path",
40 "figure.facecolor": "none", "axes.facecolor": "none",
41 }):
42 fig, (ax, difference) = plt.subplots(2, 1, figsize=(5.4, 3.8), sharex=True,
43 gridspec_kw={"height_ratios": [3, 1]}, layout="constrained")
44 ax.plot(r, exact, color="black", linewidth=1.5, label="Exact")
45 ax.plot(r[::6], potential[::6], "o", markersize=4, markerfacecolor="white",
46 color="#b8312f", label="FLODI")
47 ax.set(ylabel=r"$V/E_{\mathrm{h}}$", yticks=[0, 0.5, 1])
48 ax.legend(frameon=False, fontsize=10)
49 difference.axhline(0, color="#aaa69e", linewidth=0.7)
50 difference.plot(r, error, color="#b8312f", linewidth=1.4)
51 difference.set(xlabel=r"$r/a_0$", ylabel="Error", yticks=[0, 0.00025],
52 xticks=[0, 2, 4, 6], xlim=(0, 6))
53 difference.ticklabel_format(axis="y", style="sci", scilimits=(0, 0), useMathText=True)
54 FIGURE_DIRECTORY.mkdir(parents=True, exist_ok=True)
55 for extension in ("png", "svg"):
56 fig.canvas.draw()
57 fig.savefig(FIGURE_DIRECTORY / f"poisson_gaussian.{extension}", dpi=260,
58 transparent=True, bbox_inches="tight", pad_inches=0.12)
59 plt.close(fig)
60
61
62if __name__ == "__main__":
63 main()