fork download
  1. #Madelung constant gives the total electric potential felt by an atom in a solid.
  2. #Sodium chloride has atoms arranged in a cubic lattice, with alternating sodium and chloride atoms.
  3. #each position on the lattice is given by integer coordinates (i,j,k)
  4.  
  5. from math import sqrt
  6. l=100 #number of atoms in all direction around an atom at the origin
  7. M=0 #Madelung constant is 0
  8.  
  9. for i in range(-l,l+1):#this gives the values for i that are used to calculate the final M
  10. for j in range(-l,l+1): #this gives the values for j that are used to calculate the final M
  11. for k in range(-l,l+1): #this gives the values for k that are used to calculate the final M
  12. if not (i==0 and j==0 and k==0):#(i,j,k) cannot be (0,0,0) or the origin because if all three are zero at the same time then the denominator would be zero, causing there to be an asymptote at that point and also causing the total sum to be incalculable.
  13. M+=((-1)**(i+j+k+1))/sqrt(i*i+j*j+k*k) #this is the equation that gives the final sum of all the terms or M.
  14.  
  15. print("The required value of the Madelung constant for NaCl is", M)# your code goes here
Success #stdin #stdout 4.58s 7292KB
stdin
[asy]
import graph;

size(200);
defaultpen(linewidth(0.7));

real f(real x) {
return sin(x);
}

draw(graph(f,-2pi,2pi,n=150,join=operator ..),red);
draw(graph(f,-2pi+pi/4,2pi+pi/4,n=150,join=operator ..),blue);
draw(Circle((0,0),1));
draw(Circle((1,0),1));
draw((-1.2,0)--(1.2,0));
draw((0,-1.2)--(0,1.2));
draw((0.5,0.8)--(0.5,-0.8));
draw((0.8,0.5)--(-0.8,0.5));
label("1", (1.2,0), E);
label("2", (-1.2,0), W);
label("Stone 1", (0.7,0.3), NE);
label("Stone 2", (-0.7,0.3), NW);
[/asy]
stdout
('The required value of the Madelung constant for NaCl is', 1.7418198158396654)