forked from Niuskir/Electron-Orbitals-Blender-3D
/
Electron_Orbital_v1.py
273 lines (213 loc) · 8.6 KB
/
Electron_Orbital_v1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import bpy, bmesh
import numpy as np
import sympy
#"I" is sympy's imaginary number
from sympy import symbols,I,latex,pi,diff
from sympy.utilities.lambdify import lambdastr
from sympy import factorial as fac
from sympy.functions import Abs,sqrt,exp,cos,sin
from sympy import re, im, simplify
import warnings # in order to suppress divide_by_zero warnings...
#display the latex representation of a symbolic variable by default.
from sympy import init_printing
init_printing(use_unicode=True)
a_0,z,r=symbols("a_0,z,r")
n,m,l=symbols("n,m,l",integer=True)
int_m=symbols("int_m",integer=True)
theta,phi = symbols("\\theta,\\phi",real=True)
#The variables will used with lambdify...
angle_theta, angle_phi, radius = symbols("angle_theta,angle_phi,radius",real=True)
print("numpy version: %s"%np.__version__)
print("sympy version: %s"%sympy.__version__)
def createMesh(name, origin, verts, edges, faces):
# Create mesh and object
me = bpy.data.meshes.new(name+'Mesh')
ob = bpy.data.objects.new(name, me)
ob.location = origin
ob.show_name = True
# Link object to scene
bpy.context.scene.objects.link(ob)
# Create mesh from given verts, edges, faces. Either edges or
# faces should be [], or you ask for problems
me.from_pydata(verts, edges, faces)
# Update mesh with new data
me.update(calc_edges=True)
return ob
def P_l(l,theta): #valid for l greater than equal to zero
"""Legendre polynomial"""
if l>=0:
eq=diff((cos(theta)**2-1)**l,cos(theta),l)
else:
print("l must be an integer equal to 0 or greater")
raise ValueError
return 1/(2**l*fac(l))*eq
def P_l_m(m,l,theta):
"""Legendre polynomial"""
eq = diff(P_l(l,theta),cos(theta),Abs(m))
result = sin(theta)**Abs(m)*eq #note 1-cos^2(theta) = sin^2(theta)
return result
def Y_l_m(l,m,phi,theta):
"""Spherical harmonics"""
eq = P_l_m(m,l,theta)
if m>0:
pe=re(exp(I*m*phi))*sqrt(2)
elif m<0:
pe=im(exp(I*m*phi))*sqrt(2)
elif m==0:
pe=1
return abs(sqrt(((2*l+1)*fac(l-Abs(m)))/(4*pi*fac(l+Abs(m))))*pe*eq)
def L(l,n,rho):
"""Laguerre polynomial"""
_L = 0.
for i in range((n-l-1)+1): #using a loop to do the summation
_L += ((-i)**i*fac(n+l)**2.*rho**i)/(fac(i)*fac(n-l-1.-i)*\
fac(2.*l+1.+i))
return _L
def R(r,n,l,z=1.,a_0=1.):
"""Radial function"""
rho = 2.*z*r/(n*a_0)
_L = L(l,n,rho)
_R = (2.*z/(n*a_0))**(3./2.)*sqrt(fac(n-l-1.)/\
(2.*n*fac(n+l)**3.))*exp(-z/(n*a_0)*r)*rho**l*_L
return _R
def Psi(r,n,l,m,phi,theta,z=1,a_0=1):
"""Wavefunction"""
_Y = Y_l_m(l,m,phi,theta)
_R = R(r,n,l)
return _R*_Y
def P(r,n,l,m,phi,theta):
"""Returns the symbolic equation probability of the location
of an electron"""
return Psi(r,n,l,m,phi,theta)**2*r**2
def display_orbital(n,l,m_,no_of_contours = 16,Opaque=0.5):
"""Diplays a 3D view of electron orbitals"""
#The plot density settings (don't mess with unless you are sure)
rng = 12*n*1.5 #This determines the size of the box
_steps = 100j# (it needs to be bigger with n).
steps = _steps.imag
_x,_y,_z = np.ogrid[-rng:rng:_steps,-rng:rng:_steps,-rng:rng:_steps]
P_tex = "" #initialize the LaTex string of the probabilities
#Validate the quantum numbers
assert(n>=1), "n must be greater or equal to 1" #validate the value of n
assert(0<=l<=n-1), "l must be between 0 and n-1" #validate the value of l
assert(-l<=max(m_)<=l), "p must be between -l and l" #validate the value of p
assert(-l<=min(m_)<=l), "p must be between -l and l" #validate the value of p
for m in m_:
#Determine the probability equation symbolically and convert
#it to a string
prob = lambdastr((radius,angle_phi,angle_theta), P(radius,n,l,m,
angle_phi,
angle_theta))
#record the probability equation as a LaTex string
P_eq = simplify(P(r,n,l,m,phi,theta))
P_tex+="$$P ="+latex(P_eq)+"$$ \n\n "
if '(nan)' in prob: #Check for errors in the equation
print("There is a problem with the probability function.")
raise ValueError
#Convert the finctions in the probability equation from the sympy
#library to the numpy library to allow for the use of matrix
#calculations
prob = prob.replace('sin','np.sin') #convert to numpy
prob = prob.replace('cos','np.cos') #convert to numpy
prob = prob.replace('Abs','np.abs') #convert to numpy
prob = prob.replace('pi','np.pi') #convert to numpy
prob = prob.replace('exp','np.exp') #convert to numpy
#convert the converted string to a callable function
Prob = eval(prob)
#generate a set of data to plot the contours of.
w = Prob(r_fun(_x,_y,_z),phi_fun(_x,_y,_z),theta_fun(_x,_y,_z))
#Remove nan's in grid w
w[np.isnan(w)] = 0
#Determine minimum and maximum value (probability density) in the grid w
minw = np.nanmin(w)
maxw = np.nanmax(w)
# print(minw, maxw)
#Determine value slices
colorSlice = (maxw - minw) / (no_of_contours)
#Create contour lookup array
pr = np.linspace(minw, maxw, no_of_contours)
# print(pr)
buildGridAndCreate(int(steps), pr, w)
#########
#select object as active and change toggle to editmode
bpy.context.scene.objects.active = bpy.data.objects['Orbital']
bpy.ops.object.mode_set(mode='EDIT')
# Get the active mesh
ob = bpy.context.edit_object
me = ob.data
# Get a BMesh representation
bm = bmesh.from_edit_mesh(me)
# Modify the BMesh, can do anything here...
for v in bm.verts:
v.co.x += 1.0
# Show the updates in the viewport
# and recalculate n-gon tessellation.
bmesh.update_edit_mesh(me, True)
bpy.ops.object.mode_set(mode='OBJECT')
########
#Information used for the 2D slices below
limits = []
lengths = []
for cor in (_x,_y,_z):
limit = (np.min(cor),np.max(cor))
limits.append(limit)
#print(np.size(cor))
lengths.append(np.size(cor))
#print(limit)
return (limits, lengths, _x, _y, _z, P_tex)
def buildGridAndCreate(steps, pr, w):
# mesh arrays
verts = []
edges = []
faces = []
origin = (0,0,0)
# mesh variables
scale = 0.1
#fill verts array
for i in range (0,(steps)):
for j in range(0,(steps)):
for k in range(0,(steps)):
colorindex = np.searchsorted(pr, w[i,j,k])
if colorindex == 2:
vert = (i*scale,j*scale,k*scale)
verts.append(vert)
# numX = 50
# for i in range (0, numX):
# A = i
# B = i+1
# edge = (A,B)
# edges.append(edge)
#fill faces array
# numX = 2
# for i in range (0, numX):
# A = i
# B = i+1
# C = (i+2)
#
# face = (A,B,C)
# faces.append(face)
ob1 = createMesh('Orbital', origin, verts, [], faces)
# Move object to center
bpy.data.objects['Orbital'].select = True
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
bpy.ops.object.location_clear()
#Start the calculation
r_fun = lambda _x,_y,_z: (np.sqrt(_x**2+_y**2+_z**2))
theta_fun = lambda _x,_y,_z: (np.arccos(_z/r_fun(_x,_y,_z)))
phi_fun = lambda _x,_y,_z: (np.arctan(_y/_x)*(1+_z-_z))
#Delete generated Blender object
bpy.ops.object.select_all(action='DESELECT')
if bpy.data.objects.get("Orbital") is not None:
bpy.data.objects['Orbital'].select = True
bpy.ops.object.delete(use_global=False)
n = 4
l = 3
m_ = [1]
n_o_c = 64
opacity = 0.5
P_tex = "" #initialize the LaTex string of the probabilities
with warnings.catch_warnings():
warnings.simplefilter("ignore")
limits, lengths, _x, _y, _z, P_tex = display_orbital(n,l,m_,n_o_c,opacity)
txt = r"$$\textbf{The symbolic expression for the resulting probability equation is:}$$ "
print(txt+P_tex)