importnumpyasnpfromscipy.specialimportgenlaguerre,factorialfromscipy.constantsimportphysical_constantsimportmatplotlib.pyplotasplt# Get the Bohr radius a_0a_0=physical_constants['Bohr radius'][0]# Calculate the normalization constant N_{n,l}defnormalization_constant(n,l):num=(2/(n*a_0))**3*factorial(n-l-1)denom=2*n*factorial(n+l)returnnp.sqrt(num/denom)# Define the radial wavefunction R_{n,l}(r)defradial_wavefunction(n,l,r):rho=2*r/(n*a_0)# Compute the dimensionless variable rholaguerre_polynomial=genlaguerre(n-l-1,2*l+1)# Generalized Laguerre polynomialnormalization=normalization_constant(n,l)# Normalization constantradial_part=rho**l*np.exp(-rho/2)*laguerre_polynomial(rho)# Main part of the radial wavefunctionreturnnormalization*radial_part# Function to plot the radial wavefunction for given n and ldefplot_radial_wavefunction(n,l):# Define the range of r valuesr_values=np.linspace(0,50*a_0,1000)# From 0 to 50 * a_0# Compute the radial wavefunctionR_values=radial_wavefunction(n,l,r_values)# Plot the resultplt.plot(r_values/a_0,R_values,label=f"R_{n},{l}(r)")plt.xlabel(r"$r / a_0$")plt.ylabel(r"$R_{n,l}(r)$")plt.title(f"Radial Wavefunction for n={n}, l={l}")plt.grid(True)plt.legend()plt.show()# Example usage: Plot for n = 2, l = 0plot_radial_wavefunction(2,0)
importnumpyasnpimportmatplotlib.pyplotaspltfrommpl_toolkits.mplot3dimportAxes3Dfromscipy.specialimportsph_harm# Function to visualize both the magnitude and real part of the spherical harmonicdefplot_spherical_harmonic(l,m):# Generate the range of anglestheta=np.linspace(0,np.pi,100)# Polar angle θ from 0 to πphi=np.linspace(0,2*np.pi,100)# Azimuthal angle φ from 0 to 2πtheta,phi=np.meshgrid(theta,phi)# Create a meshgrid of angles# Compute the spherical harmonic Y_l^m(θ, φ)Y_lm=sph_harm(m,l,phi,theta)# Calculate the magnitude |Y_l^m| (for one plot)r_magnitude=np.abs(Y_lm)# Calculate the real part Re(Y_l^m) (for the other plot)r_real=np.real(Y_lm)# Convert spherical coordinates to Cartesian coordinates for magnitude plotx_mag=r_magnitude*np.sin(theta)*np.cos(phi)y_mag=r_magnitude*np.sin(theta)*np.sin(phi)z_mag=r_magnitude*np.cos(theta)# Convert spherical coordinates to Cartesian coordinates for real part plotx_real=r_real*np.sin(theta)*np.cos(phi)y_real=r_real*np.sin(theta)*np.sin(phi)z_real=r_real*np.cos(theta)# Create a figure with two subplotsfig=plt.figure(figsize=(16,6))# Subplot 1: Magnitude |Y_l^m|ax1=fig.add_subplot(121,projection='3d')ax1.plot_surface(x_mag,y_mag,z_mag,facecolors=plt.cm.viridis(r_magnitude/r_magnitude.max()),rstride=1,cstride=1,alpha=0.6,edgecolor='none')ax1.set_title(f"Spherical Harmonic Magnitude: $|Y_{l}^{m}|$")ax1.set_xlim([-0.5,0.5])ax1.set_ylim([-0.5,0.5])ax1.set_zlim([-0.5,0.5])ax1.set_xlabel('X')ax1.set_ylabel('Y')ax1.set_zlabel('Z')# Subplot 2: Real part Re(Y_l^m)ax2=fig.add_subplot(122,projection='3d')ax2.plot_surface(x_real,y_real,z_real,facecolors=plt.cm.viridis((r_real-r_real.min())/(r_real.max()-r_real.min())),rstride=1,cstride=1,alpha=0.6,edgecolor='none')ax2.set_title(f"Spherical Harmonic Real Part: $Re(Y_{l}^{m})$")ax2.set_xlim([-0.5,0.5])ax2.set_ylim([-0.5,0.5])ax2.set_zlim([-0.5,0.5])ax2.set_xlabel('X')ax2.set_ylabel('Y')ax2.set_zlabel('Z')# Show the figureplt.tight_layout()plt.show()# Example: Plot both magnitude and real part for l = 3, m = 2plot_spherical_harmonic(3,2)
importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.specialimportsph_harm,genlaguerre,factorial# Constantsa0=1.0# Bohr radius (in atomic units)# Radial wavefunction R_{n,l}(r) for hydrogen atomdefhydrogen_radial_wavefunction(n,l,r):# Radial part of the wavefunction for hydrogen atomrho=2*r/(n*a0)# Normalization constantnorm=np.sqrt((2/(n*a0))**3*factorial(n-l-1)/(2*n*factorial(n+l)))# Radial wavefunctionradial_part=norm*np.exp(-rho/2)*rho**l*genlaguerre(n-l-1,2*l+1)(rho)returnradial_part# Probability density function for hydrogen atomdefhydrogen_wavefunction_squared(n,l,m,r,theta,phi):# Radial partR_nl=hydrogen_radial_wavefunction(n,l,r)# Angular part (spherical harmonics)Y_lm=sph_harm(m,l,phi,theta)# Return the square of the wavefunction (probability density)returnnp.abs(R_nl*Y_lm)**2# Generate random points in spherical coordinatesdefrandom_points_in_sphere(num_points,n,l,m):# Generate random spherical coordinatesr=np.random.rand(num_points)*10*a0# Random radius within a reasonable rangetheta=np.arccos(2*np.random.rand(num_points)-1)# Uniformly distributed cos(theta)phi=np.random.rand(num_points)*2*np.pi# Uniformly distributed phi# Compute the probability density at each pointprob_density=hydrogen_wavefunction_squared(n,l,m,r,theta,phi)# Normalize the probabilitiesprob_density/=prob_density.max()# Filter points based on probability density (Monte Carlo sampling)mask=np.random.rand(num_points)<prob_density# Keep only the points that passed the filterreturnr[mask],theta[mask],phi[mask]# Convert spherical to Cartesian coordinatesdefspherical_to_cartesian(r,theta,phi):x=r*np.sin(theta)*np.cos(phi)y=r*np.sin(theta)*np.sin(phi)z=r*np.cos(theta)returnx,y,z# Main function to plot the hydrogen atom electron clouddefplot_hydrogen_cloud(n,l,m,num_points=100000):# Generate random points in spherical coordinatesr,theta,phi=random_points_in_sphere(num_points,n,l,m)# Convert spherical coordinates to Cartesianx,y,z=spherical_to_cartesian(r,theta,phi)# Plot the pointsfig=plt.figure(figsize=(10,10))ax=fig.add_subplot(111,projection='3d')ax.scatter(x,y,z,color='blue',s=0.1,alpha=0.6)ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')ax.set_title(f'Hydrogen Atom Electron Cloud: n={n}, l={l}, m={m}')plt.show()# Example: Plot the electron cloud for the 2p orbital (n=2, l=1, m=0)plot_hydrogen_cloud(n=2,l=1,m=0)
importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.specialimportsph_harm,factorial# Constantsa0=1.0# Bohr radius (in atomic units)# Radial wavefunction R_{n,l}(r) for hydrogen atomdefhydrogen_radial_wavefunction(n,l,r):rho=2*r/(n*a0)norm=np.sqrt((2/(n*a0))**3*factorial(n-l-1)/(2*n*factorial(n+l)))radial_part=norm*np.exp(-rho/2)*rho**lreturnradial_part# Probability density function for hydrogen atomdefhydrogen_wavefunction_squared(n,l,m,r,theta,phi):R_nl=hydrogen_radial_wavefunction(n,l,r)Y_lm=sph_harm(m,l,phi,theta)returnnp.abs(R_nl*Y_lm)**2# Generate random points in spherical coordinatesdefrandom_points_in_sphere(num_points,n,l,m):r=np.random.rand(num_points)*10*a0# Random radius within a reasonable rangetheta=np.arccos(2*np.random.rand(num_points)-1)# Uniformly distributed cos(theta)phi=np.random.rand(num_points)*2*np.pi# Uniformly distributed phiprob_density=hydrogen_wavefunction_squared(n,l,m,r,theta,phi)# Check if max(prob_density) is non-zero before normalizingmax_density=prob_density.max()ifmax_density>0:# Avoid division by zeroprob_density/=max_density# Loosen filtering conditionmask=np.random.rand(num_points)<prob_density*5# Loosen the filtering conditionprint(f"Number of points before filtering: {num_points}, after filtering: {np.sum(mask)}")returnr[mask],theta[mask],phi[mask]# Convert spherical to Cartesian coordinatesdefspherical_to_cartesian(r,theta,phi):x=r*np.sin(theta)*np.cos(phi)y=r*np.sin(theta)*np.sin(phi)z=r*np.cos(theta)returnx,y,z# Real spherical harmonics (p_x, p_y, p_z orbitals)defreal_spherical_harmonics(l,m,theta,phi):ifl==1:ifm=='p_x':# p_x: Re(Y_1^1 + Y_1^-1)return(sph_harm(1,1,phi,theta).real+sph_harm(-1,1,phi,theta).real)elifm=='p_y':# p_y: Im(Y_1^1 - Y_1^-1)return(sph_harm(1,1,phi,theta).imag-sph_harm(-1,1,phi,theta).imag)elifm=='p_z':# p_z: Re(Y_1^0)returnsph_harm(0,1,phi,theta).realelifl==2:ifm=='d_xy':returnsph_harm(2,2,phi,theta).imagelifm=='d_xz':returnsph_harm(1,2,phi,theta).realelifm=='d_yz':returnsph_harm(1,2,phi,theta).imagelifm=='d_z2':returnsph_harm(0,2,phi,theta).realelifm=='d_x2_y2':returnsph_harm(2,2,phi,theta).realelse:raiseValueError("Unsupported orbital")# Main function to plot the hydrogen atom real orbitalsdefplot_real_hydrogen_orbital(n,l,m,num_points=100000):r,theta,phi=random_points_in_sphere(num_points,n,l,0)# m=0, radial sampling# Real spherical harmonicsY_lm_real=real_spherical_harmonics(l,m,theta,phi)prob_density=np.abs(hydrogen_radial_wavefunction(n,l,r)*Y_lm_real)**2# Check if max(prob_density) is non-zero before normalizingmax_density=prob_density.max()ifmax_density>0:# Avoid division by zeroprob_density/=max_density# Convert spherical to Cartesian coordinatesx,y,z=spherical_to_cartesian(r,theta,phi)# Plot the pointsfig=plt.figure(figsize=(10,10))ax=fig.add_subplot(111,projection='3d')ax.scatter(x,y,z,color='blue',s=0.1,alpha=0.6)ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')ax.set_title(f'Hydrogen Atom Real Orbital: n={n}, l={l}, m={m}')plt.show()# Example: Plot the real orbital for the p_x orbital (n=2, l=1, m='p_x')plot_real_hydrogen_orbital(n=2,l=1,m='p_x')