#! /usr/bin/env python """ madelung.py is program which calculates the Madelung constant M of molecular structures in condensed matter physics is related to the attomic electric potential V_total = e/(4pi epsilon_o a) * M The program takes as input L, number of atoms per dimension of a symmetric NaCl lattice For an L of 200, the program takes a little over one minute to calculate and returns a value of M = -1.745. the true value for sodium chloride is -1.748 hpc-login-24 294% time madelung.py Enter number of atoms per dimension: 200 Madelung constant: -1.74468504217 48.447u 0.021s 0:50.56 95.8% 0+0k 0+0io 0pf+0w Paul Eugenio PHZ4151C Feb 6, 2019 """ # program header code from __future__ import division, print_function import timeit mysetup = ''' from math import sqrt # initalize variables M = 0.0 L = int(raw_input("Enter number of atoms per dimension: ")) ''' mycode = ''' # # Madelung constant for an atom in the center of a cubic lattice of # alternating +e and -e atoms for i in range(-L, L+1): for j in range(-L, L+1): for k in range(-L, L+1): if not( i==0 and j==0 and k==0): if(i+j+k)%2==0: # it's a positive ion M += 1/sqrt(i*i + j*j + k*k) else: # it's a negative ion M -= 1/sqrt(i*i + j*j + k*k) print("Madelung constant:",M) ''' print( "run time: {0:3.3} seconds".format( timeit.timeit(setup=mysetup, stmt=mycode, number=1) ) )