# Hydrogen Wave Function




```python
#Import libraries
from numpy import *
import matplotlib.pyplot as plt
from sympy.physics.hydrogen import Psi_nlm
```

## Analytical Equation
$$
\psi_{n \ell m}(r, \theta, \varphi)=\sqrt{\left(\frac{2}{n a_{0}^{*}}\right)^{3} \frac{(n-\ell-1) !}{2 n(n+\ell) !}} e^{-\rho / 2} \rho^{\ell} L_{n-\ell-1}^{2 \ell+1}(\rho) Y_{\ell}^{m}(\theta, \varphi)
$$


```python
#Symbolic representation
from sympy import Symbol
```


```python
#Symbolic variables
R=Symbol("R", real=True, positive=True)
Φ=Symbol("Φ", real=True)
Θ=Symbol("Θ", real=True)
Z=Symbol("Z", positive=True, integer=True, nonzero=True)
```


```python
#Analytical wave function
Psi_nlm(1,0,0,R,Φ,Θ,Z) #First three entries are n,l and m
```




$\displaystyle \frac{Z^{\frac{3}{2}} e^{- R Z}}{\sqrt{\pi}}$




```python
#Numerical hydrogen wave function
#Points array contains r and θ for fixed ϕ=0
def wavefunc(point,n,l,m,Z=1):
    r=point[0]
    θ=point[1]
    ϕ=point[2]
    return abs(Psi_nlm(n,l,m,r,ϕ,θ,Z))
```


```python
#Transform polar coordinates into cartesian to plot
def polar2cart(r, theta):
    return (
         r * cos(theta),
         r * sin(theta)
    )
```


```python
%%timeit
wavefunc([1,.1,.2],4,3,2,Z=1)
```

    72.8 µs ± 276 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)



```python
import sympy
```


```python
n, l, m, r, phi, theta = sympy.symbols("n, l, m, r, phi, theta")

lam_psi = sympy.lambdify([n, l, m, r, phi, theta], Psi_nlm(n, l, m, r, phi, theta, Z=1))
```


```python
%%timeit
lam_psi(4,3,2,1,.1,.2)
```

    22.6 µs ± 70.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)



```python
#2D polar variables
# 0.01 < r < 5  &  0 < θ < 2π
r = linspace(0.01,5,10)
ϕ = linspace(0,pi,20)
θ = linspace(0,2*pi,20)
```


```python
rm , tm, pm = meshgrid(r,θ,ϕ)
```


```python
%%timeit
out = lam_psi(4,3,2,rm,pm,tm)
```

    419 µs ± 788 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)



```python
#Create polar grid
grid = []
for i in r:
    for j in θ:
            grid.append([i,j])
grid = array(grid)      
```


```python
#Cartesian grid
x, y = array((polar2cart(grid[:,0],grid[:,1])))
```


```python
#Import parallel libraries
from joblib import Parallel, delayed
import multiprocessing
num_cores = multiprocessing.cpu_count()
```


```python
#Call wavefunc in parallel 
n, l, m = 1 ,0 ,0
results = Parallel(n_jobs=num_cores)(delayed(wavefunc)(i,n,l,m) for i in grid)
```


```python
#Plot figure
fig = plt.figure()
plt.scatter(x, y,c=results,cmap="magma")
plt.colorbar()
plt.axis("equal")
plt.show()

```


```python

```

1c0219cd0030deedd6270184c1603444018b2c05
ipynb
Jupyter Notebook
hydrogen-wave-function.ipynb
sinansevim/EBT617E
0907846e09173b419dfb6c3a5eae20c3ef8548bb
MIT
2021-03-12T13:16:39.000Z
2021-03-12T13:16:39.000Z
hydrogen-wave-function.ipynb
sinansevim/EBT617E
0907846e09173b419dfb6c3a5eae20c3ef8548bb
MIT
hydrogen-wave-function.ipynb
sinansevim/EBT617E
0907846e09173b419dfb6c3a5eae20c3ef8548bb
MIT
Qwen/Qwen-72B
1. YES
2. YES