Skip to content

Commit 50c9195

Browse files
authored
fix for spherical Bessel functions (sph_jn/yn) compatibility
fix for spherical Bessel functions (sph_jn/yn) compatibility between scipy ver. 0.x.x and 1.x.x
1 parent a704393 commit 50c9195

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

ch12/Program_12.2_qmscatt.ipynb

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"#\n",
12+
"# Program 12.2: Partial wave method (qmscatt.ipynb)\n",
13+
"# J Wang, Computational modeling and visualization with Python\n",
14+
"#\n",
15+
"\n",
16+
"import scipy\n",
17+
"if scipy.__version__[0] < '1': # sph. Bessel func changed after ver 1.0.0\n",
18+
" from scipy.special import sph_jn, sph_yn, lpn\n",
19+
"else:\n",
20+
" from scipy.special import spherical_jn as sph_jn, spherical_yn as sph_yn, lpn\n",
21+
"import matplotlib.pyplot as plt, numpy as np, ode\n",
22+
"%matplotlib notebook\n",
23+
"\n",
24+
"def V(r): # Yukawa potential\n",
25+
" Z, sa = 2., 1.0 # nuclear charge, screening length\n",
26+
" return -Z*np.exp(-r/sa)/r\n",
27+
" \n",
28+
"def f(r): # Sch eqn in Numerov form\n",
29+
" return 2*(E - V(r)) - L*(L+1)/(r*r)\n",
30+
"\n",
31+
"def wf(M, xm): # find w.f. and deriv at xm\n",
32+
" c = (h*h)/6.\n",
33+
" wfup, nup = ode.numerov(f, [0,.1], M, xL, h) # 1 step past xm\n",
34+
" dup = ((1+c*f(xm+h))*wfup[-1] - (1+c*f(xm-h))*wfup[-3])/(h+h)\n",
35+
" return wfup, dup/wfup[-2]\n",
36+
"\n",
37+
"xL, a, M = 0., 10., 200 # limits, matching point\n",
38+
"h, Lmax, E =(a-xL)/M, 15, 2. # step size, max L, energy\n",
39+
"\n",
40+
"k, ps = np.sqrt(2*E), np.zeros(Lmax+1) # wave vector, phase shift\n",
41+
"if scipy.__version__[0] < '1':\n",
42+
" jl, dj = sph_jn(Lmax, k*a) # (j_l, j_l') tuple \n",
43+
" nl, dn = sph_yn(Lmax, k*a) # (n_l, n_l')\n",
44+
"else:\n",
45+
" Lrange = range(Lmax + 1)\n",
46+
" jl, dj = sph_jn(Lrange, k*a, False), sph_jn(Lrange, k*a, True) # (j_l, j_l')\n",
47+
" nl, dn = sph_yn(Lrange, k*a, False), sph_yn(Lrange, k*a, True) # (n_l, n_l')\n",
48+
"\n",
49+
"for L in range(Lmax+1):\n",
50+
" u, g = wf(M, a) # g= u'/u\n",
51+
" x = np.arctan(((g*a-1)*jl[L] - k*a*dj[L])/ # phase shift \n",
52+
" ((g*a-1)*nl[L] - k*a*dn[L]))\n",
53+
" while (x < 0.0): x += np.pi # handle jumps by pi \n",
54+
" ps[L] = x\n",
55+
"\n",
56+
"theta, sigma = np.linspace(0., np.pi, 100), []\n",
57+
"cos, La = np.cos(theta), np.arange(1,2*Lmax+2,2)\n",
58+
"for x in cos: # calc cross section\n",
59+
" pl = lpn(Lmax, x)[0] # Legendre polynomial \n",
60+
" fl = La*np.exp(1j*ps)*np.sin(ps)*pl # amplitude \n",
61+
" sigma.append(np.abs(np.sum(fl))**2/(k*k))\n",
62+
" \n",
63+
"plt.figure() # plot phase shift vs L\n",
64+
"plt.plot(range(Lmax+1), ps, '-o')\n",
65+
"plt.xlabel('$l$'), plt.ylabel(r'$\\delta_l$', fontsize=16)\n",
66+
"\n",
67+
"plt.figure() \n",
68+
"plt.plot(theta*180/np.pi, sigma) # plot cross sections \n",
69+
"xtck = [0, 30, 60, 90, 120, 150, 180]\n",
70+
"plt.xticks(xtck, [repr(i) for i in xtck]) # custom ticks \n",
71+
"plt.xlabel(r'$\\theta$ (deg)')\n",
72+
"plt.ylabel(r'$\\sigma(\\theta)$ (a.u.)'), plt.semilogy()\n",
73+
"\n",
74+
"plt.show()\n"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {
81+
"collapsed": true
82+
},
83+
"outputs": [],
84+
"source": []
85+
}
86+
],
87+
"metadata": {
88+
"kernelspec": {
89+
"display_name": "Python 2",
90+
"language": "python",
91+
"name": "python2"
92+
},
93+
"language_info": {
94+
"codemirror_mode": {
95+
"name": "ipython",
96+
"version": 2
97+
},
98+
"file_extension": ".py",
99+
"mimetype": "text/x-python",
100+
"name": "python",
101+
"nbconvert_exporter": "python",
102+
"pygments_lexer": "ipython2",
103+
"version": "2.7.8"
104+
}
105+
},
106+
"nbformat": 4,
107+
"nbformat_minor": 1
108+
}

0 commit comments

Comments
 (0)