Skip to content

Instantly share code, notes, and snippets.

@certik
Created May 9, 2013 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save certik/5549650 to your computer and use it in GitHub Desktop.
Save certik/5549650 to your computer and use it in GitHub Desktop.
from numpy import empty, pi, meshgrid, linspace, sum, sin
from numpy.fft import fftn, fftfreq
E_exact = 3*pi/8
print "Hartree Energy (exact): %.15f" % E_exact
f = open("conv.txt", "w")
for N in range(3, 180, 10):
print "N =", N
L = 2.
x1d = linspace(0, L, N)
x, y, z = meshgrid(x1d, x1d, x1d)
nr = 3*pi*sin(pi*x)*sin(pi*y)*sin(pi*z)/4
ng = fftn(nr) / N**3
G1d = N * fftfreq(N) * 2*pi/L
kx, ky, kz = meshgrid(G1d, G1d, G1d)
G2 = kx**2+ky**2+kz**2
G2[0, 0, 0] = 1 # omit the G=0 term
tmp = 2*pi*abs(ng)**2 / G2
tmp[0, 0, 0] = 0 # omit the G=0 term
E = sum(tmp) * L**3
print "Hartree Energy (calculated): %.15f" % E
f.write("%d %.15f\n" % (N, E))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment