# Python script to calculate the Madelung constant of an infinite lattice

During my Diploma thesis in Physics I had to calculate the Madelung constant for a lattice of ZnO supercells. For this purpose, I implemented a small Python programm. The only prerequisite is SciPy. The script calculates the Madelung constant for an infinite lattice of point charges, neuralized by a counter-charged jellium background. All documentation is inside the file madelung.py. There’s another script sc_mad.py (supercell madelung constant) in there which demonstrates reading a POSCAR file describing an atomic lattice and calculating the Madelung constant for it.

Maybe someone can so something useful with the stuff. I would be happy about feedback whether the implementation actually works for your specific case 🙂

# Quick Modulo for 2^n

For the case that you’re interested in

mod = a % b

with $b = 2^n, n \in \mathbb{N}$ and $a \ge 0$ there’s a simple method

mod = a & (b-1);

For negative a it gets more complicated

mod = (a & (b-1)) | ~(b-1);

# Graphical Raytracing using CarMetal

In optics, the law of refraction by Snellius is widely known.

$n_1 \cdot sin(\alpha_1) = n_2 \cdot sin(\alpha_2)$

The calculation of  $sin(\alpha_2)$ can actually be done using geometry exclusively.

mere geometric calculation of the refraction of a ray

I have created this nice image using CarMetal, a geometry program. You can download the CarMetal-file brechung_regulaerundtotal.zir and play around with the angles yourself. While you change $\alpha_1$, f0r example, CarMetal maintains the introduced relationships like „… being parallel to …“ or „… goes through the intersection of … and … „. The green ray shows near total reflection which appears as soon as there’s no intersection of the vertical line with the inner circle.