Skip to content

Instantly share code, notes, and snippets.

@tmstr
Created March 22, 2015 12:13
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 tmstr/7c75f0edfadb08d72b6e to your computer and use it in GitHub Desktop.
Save tmstr/7c75f0edfadb08d72b6e to your computer and use it in GitHub Desktop.
import math
proc simulateIon(x: float, y: float, z: float): float =
let charge = math.pow(-1.0, x + y + z) # NaCl - FCC
let distance = math.sqrt(x * x + y * y + z * z)
return charge / distance
proc madelung(N): float =
var M: float = 0
for x in -N..N + 1:
for y in -N..N + 1:
for z in -N..N + 1:
if x != 0 or y != 0 or z != 0:
M += simulateIon(float(x), float(y), float(z))
return M
const N = 80
var mValues: seq[float] = @[]
# for i in N - 3 .. N + l:
for i in 0..N:
if i == 0:
mValues.add(0.0)
else:
mValues.add(madelung(i))
let mean = (mValues[i] + mValues[i-1]) / 2
# echo("M(", i, ") = ", mValues[i], " M(", i-1, ") = ", mValues[i-1], " Mean = ", mean)
echo(i, ", ", mValues[i], ", ", mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment