# ELECTRON IN CUBE - calculation of possible electron energies
# * EM-lectures: full justification of the equation + detailed comments
# * here: calculation of electron energies by means of Python/Sympy
# 1) we take Schrodinger equation for eln-in-cube: Ham[F(x,y,z)] = E * F(x,y,z)
# 2) we insert our previously derived solution F(x,y,z) into equation from (1)
# 3) we solve the equation for E which...
# ...gives us the possible energies of electron in cube
# ...confirms our solution F(x,y,z),
# because the equation Hamiltonian[F(x,y,x)] = E*F(x,y,z) holds,
# i.e. Hamiltonian[F(x,y,z)] gives const*F(x,y,z), where const=E
# (0) Initialize sympy
import sympy as sp
sp.init_printing()
# Prepare symbols for all future calculations
# (additional information, such as [real=True] is very useful
# (it should be inserted wherever possible - it can simplify solution significangly
(x,y,z) = sp.symbols('x y z', real=True)
(Nx,Ny,Nz) = sp.symbols('N_x N_y N_z', integer=True, postitive=True)
(A,L) = sp.symbols('A L', real=True, positive=True)
(E,m,h) = sp.symbols('E m_e h', real=True, positive=True)
# (1) Define our solution of Schrodinger equation
# (the solution has been derived in EM lectures
from sympy import pi,cos
# Define partial solutions (separate solutions for x,y,z)
def F1(x): return ( A*cos(pi*Nx*x/L) )
def F2(y): return ( A*cos(pi*Ny*y/L) )
def F3(z): return ( A*cos(pi*Nz*z/L) )
# Define complete solution f(x,y,z) = f1(x)*f2(y)*f3(z)
def F(x,y,z): return ( F1(x)*F2(y)*F3(z) )
# Display our complete solution
# (reminder: the solution was derived in EM lectures
# (here we have just defined the solution in the form of function suitable for SymPy
F(x,y,z)
# (2) Define and display left-hand-side = LHS of Schrodinger equation
Schr_LHS = -h**2/(2*m) * (sp.diff(F(x,y,z),x,2) + sp.diff(F(x,y,z),y,2) + sp.diff(F(x,y,z),z,2))
Schr_LHS
Schr_LHS = sp.simplify(Schr_LHS)
Schr_LHS
# (3) Define and display Right-hand-side = RHS of Schrodinger equation
Schr_RHS = E * F(x,y,z)
Schr_RHS
# (4) Define final Schrodinger equation
# (logically: Schr_RHS = Schr_LHS
Schr = sp.Eq(Schr_LHS, Schr_RHS)
Schr
# (5) Solve Schrodinger equation for E
# solving for E is surprisingly easy
# (because we have already inserted known solution for F
solution = sp.solve(Schr,E)
# sp.solve yields list of solutions
# (here we have just one solution = list containing just one element
solution
# (6) Slightly adjust and print the final solution
# We take the first (and the only) solution
# of Schr.equation from section (5) and print it as an equation
sp.Eq(E,solution[0])
# The quantum numbers Nx,Ny,Nz can be combined into one quantum number n,
# which gives the final relation for energy of an electron in a cube
n = sp.symbols('n', integer=True, positive=True)
sp.Eq(E,solution[0]).subs(Nx**2+Ny**2+Nz**2,n**2)
# CONCLUSIONS
# 1) We inserted (known) characteristic function F(x,y,z)
# into (known) Schrodinger equation of electron in a cube.
# 2) We solved the Schr.equation from (1) for E,
# which gave us characteristic values = possible energies E of eln in cube.
# 3) As the quantum numbers (Nx,Ny,Nz,n) are integers,
# the eln-in-cube cannot have arbitrary energy, but just some distinct values
# => this is called energy quantization
# => the electron energy is quantized