Skip to main content
Chemistry LibreTexts

Hydrogen Orbitals (Python Notebook)

  • Page ID
    281075
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Source: https://github.com/DalInar/schroding...eckpoint.ipynb

    (Notes: Different version of scikit-image is used. The right version should be https://pypi.org/project/scikit-image/0.12.3/#files)

    import skimage
    print(skimage.__version__)
    import numpy
    import math
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Slider, Button, RadioButtons
    import scipy.special
    from scipy.special import sph_harm
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.colors import ListedColormap
    import skimage
    
    def hydrogen_wf(n,l,m,X,Y,Z):
        R = numpy.sqrt(X**2+Y**2+Z**2)
        Theta = numpy.arccos(Z/R)
        Phi = numpy.arctan2(Y,X)
        
        rho = 2.*R/n
        s_harm=sph_harm(m, l, Phi, Theta)
        l_poly = scipy.special.genlaguerre(n-l-1,2*l+1)(rho)
        
        prefactor = numpy.sqrt((2./n)**3*math.factorial(n-l-1)/(2.*n*math.factorial(n+l)))
        wf = prefactor*numpy.exp(-rho/2.)*rho**l*s_harm*l_poly
        wf = numpy.nan_to_num(wf)
        return wf
      
    dz=0.5
    zmin=-10
    zmax=10
    x = numpy.arange(zmin,zmax,dz)
    y = numpy.arange(zmin,zmax,dz)
    z = numpy.arange(zmin,zmax,dz)
    X,Y,Z = numpy.meshgrid(x,y,z) #X, Y, Z are 3d arrays that tell us the values of x, y, and z at every point in space
    
    #Change these to change which orbital to plot
    n=4
    l=2
    m=0
    
    data = hydrogen_wf(n,l,m,X,Y,Z)
    data = abs(data)**2
    
    R = numpy.sqrt(X**2+Y**2+Z**2)
    
    fig, ax = plt.subplots()
    plt.subplots_adjust(left=0.15, bottom=0.15)
    im = plt.imshow(data[int((0-zmin)/dz),:,:], vmin=0, vmax = numpy.max(data), extent=[zmin,zmax,zmin,zmax])
    plt.colorbar()
    sli = Slider(plt.axes([0.25, 0.025, 0.65, 0.03]), "Y", z[0], z[len(z)-1], valinit=0)
    ax.set_title("Hydrogen Orbital xz Slice (y="+str("%.2f"%sli.val)+"): n="+str(n)+", l="+str(l)+", m="+str(m))
    
    def update(val):
        index = int((sli.val-zmin) / dz)
        im.set_data(data[index,:,:])
        ax.set_title("Hydrogen Orbital xz Slice (y="+str("%.2f"%sli.val)+"): n="+str(n)+", l="+str(l)+", m="+str(m))
           
    sli.on_changed(update)
    plt.show()
    import numpy
    import math
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Slider, Button, RadioButtons
    import scipy.special
    from scipy.special import sph_harm
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.colors import ListedColormap
    import skimage
    from skimage import measure
    
    def hydrogen_wf(n,l,m,X,Y,Z):
        R = numpy.sqrt(X**2+Y**2+Z**2)
        Theta = numpy.arccos(Z/R)
        Phi = numpy.arctan2(Y,X)
        
        rho = 2.*R/n
        s_harm=sph_harm(m, l, Phi, Theta)
        l_poly = scipy.special.genlaguerre(n-l-1,2*l+1)(rho)
        
        prefactor = numpy.sqrt((2./n)**3*math.factorial(n-l-1)/(2.*n*math.factorial(n+l)))
        wf = prefactor*numpy.exp(-rho/2.)*rho**l*s_harm*l_poly
        wf = numpy.nan_to_num(wf)
        return wf
      
    dz=0.5
    zmin=-10
    zmax=10
    x = numpy.arange(zmin,zmax,dz)
    y = numpy.arange(zmin,zmax,dz)
    z = numpy.arange(zmin,zmax,dz)
    X,Y,Z = numpy.meshgrid(x,y,z) #X, Y, Z are 3d arrays that tell us the values of x, y, and z at every point in space
    n=4
    l=2
    m=0
    data = hydrogen_wf(n,l,m,X,Y,Z)
    data = abs(data)**2
    R = numpy.sqrt(X**2+Y**2+Z**2)
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.set_xlim([0,len(x)])
    ax.set_ylim([0,len(y)])
    ax.set_zlim([0,len(z)])
    max_val = numpy.max(data)
    
    verts, faces, _, _ = measure.marching_cubes(data, max_val/2, spacing = (1,1,1))
    result=ax.plot_trisurf(verts[:,0], verts[:,1], faces, verts[:,2], cmap ='Spectral', lw=0)
    
    sli = Slider(plt.axes([0.25, 0.025, 0.65, 0.03]), "iso", 0, max_val, valinit=max_val/2)
    ax.set_title("Hydrogen Orbital Isosurface ("+str("%.5f"%sli.val)+"): n="+str(n)+", l="+str(l)+", m="+str(m))
    
    def update(val):
        ax.clear()
        verts, faces = measure.marching_cubes(data, sli.val, spacing = (1,1,1))
        result = ax.plot_trisurf(verts[:,0], verts[:,1], faces, verts[:,2], cmap ='Spectral', lw=0)
        ax.set_xlim([0,len(x)])
        ax.set_ylim([0,len(y)])
        ax.set_zlim([0,len(z)])
        ax.set_title("Hydrogen Orbital Isosurface ("+str("%.5f"%sli.val)+"): n="+str(n)+", l="+str(l)+", m="+str(m))
               
    sli.on_changed(update)
    plt.show()
    import numpy
    import math
    import matplotlib.pyplot as plt
    import scipy.special
    from scipy.special import sph_harm
    
    def hydrogen_wf(n,l,m,X,Y,Z):
        R = numpy.sqrt(X**2+Y**2+Z**2)
        Theta = numpy.arccos(Z/R)
        Phi = numpy.arctan2(Y,X)
        
        rho = 2.*R/n
        s_harm=sph_harm(m, l, Phi, Theta)
        l_poly = scipy.special.genlaguerre(n-l-1,2*l+1)(rho)
        
        prefactor = numpy.sqrt((2./n)**3*math.factorial(n-l-1)/(2.*n*math.factorial(n+l)))
        wf = prefactor*numpy.exp(-rho/2.)*rho**l*s_harm*l_poly
        wf = numpy.nan_to_num(wf)
        return wf
      
    dz=0.5
    zmin=-10
    zmax=10
    x = numpy.arange(zmin,zmax,dz)
    y = numpy.arange(zmin,zmax,dz)
    z = numpy.arange(zmin,zmax,dz)
    X,Y,Z = numpy.meshgrid(x,y,z) #X, Y, Z are 3d arrays that tell us the values of x, y, and z at every point in space
    n=3
    l=1
    m=0
    data = hydrogen_wf(n,l,m,X,Y,Z)
    data = abs(data)**2
    R = numpy.sqrt(X**2+Y**2+Z**2)
    
    plt.figure()
    plt.plot(z, data[int(len(z)/2),int(len(z)/2),:])
    plt.title("$|\psi_{nlm}|^2$(x=0,y=0,z): n="+str(n)+", l="+str(l)+", m="+str(m))
    plt.xlabel('z')
    plt.ylabel("$|\psi_{nlm}|^2$")
    plt.show()

     


    Hydrogen Orbitals (Python Notebook) is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?