Advertisement
Guest User

bioenthalpy5.py

a guest
Feb 20th, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4. from matplotlib.animation import FuncAnimation
  5. from matplotlib.widgets import Slider
  6.  
  7. # Define the 3D wavefunction in spherical coordinates
  8. def wavefunction(theta, phi, r, k_theta, k_phi, k_r, omega, t):
  9.     return np.exp(1j * (k_theta * theta + k_phi * phi + k_r * r - omega * t))
  10.  
  11. # Define parameters
  12. k_theta, k_phi, k_r = 1.0, 1.0, 1.0
  13. omega = 1.0
  14.  
  15. # Create the figure and slider axes
  16. fig = plt.figure(figsize=(12, 10))
  17. ax = fig.add_subplot(111, projection='3d')
  18. slider_ax = plt.axes([0.2, 0.02, 0.6, 0.03])  # Slider position (left, bottom, width, height)
  19.  
  20. # Initialize the slider for controlling the number of displayed data points
  21. num_points_slider = Slider(slider_ax, 'Points', valmin=10, valmax=30, valinit=30, valstep=1)
  22.  
  23. # Animation update function
  24. def update(frame):
  25.     ax.clear()
  26.  
  27.     # Adjust the number of points based on slider value
  28.     num_points = int(num_points_slider.val)
  29.     theta = np.linspace(0, 2 * np.pi, num_points)
  30.     phi = np.linspace(0, 2 * np.pi, num_points)
  31.     r = np.linspace(0.5, 5.0, num_points)
  32.     Theta, Phi, R = np.meshgrid(theta, phi, r)
  33.  
  34.     # Convert to Cartesian coordinates for visualization
  35.     X = R * np.cos(Phi) * np.sin(Theta)
  36.     Y = R * np.sin(Phi) * np.sin(Theta)
  37.     Z = R * np.cos(Theta)
  38.  
  39.     # Compute the wavefunction and density matrix
  40.     t = frame / 20000.0  # Speed increased by a factor of 20,000x
  41.     Psi = wavefunction(Theta, Phi, R, k_theta, k_phi, k_r, omega, t)
  42.     rho = np.abs(Psi)**2  # Density matrix (real values)
  43.  
  44.     # Compute entropy (S) and bio-enthalpy (H_B)
  45.     S = -rho * np.log(rho + 1e-9)  # Adding a small term to avoid log(0)
  46.     H_B = rho + np.abs(np.gradient(Psi.real)[0])**2 + np.abs(np.gradient(Psi.real)[1])**2 + np.abs(np.gradient(Psi.real)[2])**2
  47.  
  48.     # Normalize S and H_B for visualization purposes
  49.     S_normalized = S / np.max(S)
  50.     H_B_normalized = H_B / np.max(H_B)
  51.  
  52.     # Plot entropy and bio-enthalpy together using color mapping
  53.     scatter_entropy = ax.scatter(
  54.         X.flatten(), Y.flatten(), Z.flatten(),
  55.         c=S_normalized.flatten(), cmap='twilight', alpha=0.6,
  56.         label="Entropy"
  57.     )
  58.    
  59.     scatter_enthalpy = ax.scatter(
  60.         X.flatten(), Y.flatten(), Z.flatten(),
  61.         c=H_B_normalized.flatten(), cmap='hsv', alpha=0.4,
  62.         label="Bio-Enthalpy"
  63.     )
  64.  
  65. #     # Add vector arrows (gradient of Psi) - COMMENTED OUT
  66. #     # ax.quiver(
  67. #     #     X[::3].flatten(), Y[::3].flatten(), Z[::3].flatten(),
  68. #     #     grad_Psi_x[::3].flatten(), grad_Psi_y[::3].flatten(), grad_Psi_z[::3].flatten(),
  69. #     #     length=0.5, normalize=True,
  70. #     #     color="blue", alpha=0.7,
  71. #     #     label="Gradient Arrows"
  72. #     # )
  73.  
  74.     # Add titles and labels
  75.     ax.set_title(f'Entropy vs Bio-Enthalpy at t={t:.6f}')
  76.     ax.set_xlabel('X')
  77.     ax.set_ylabel('Y')
  78.     ax.set_zlabel('Z')
  79.  
  80. # Load and animate the first 10 seconds with increased speed
  81. def load_and_animate(duration=10, fps=30):
  82.     frames = int(fps * duration)  # Total number of frames
  83.    
  84.     ani = FuncAnimation(fig, update, frames=frames, interval=1000 / fps)  # Interval in milliseconds
  85.     plt.show()
  86.  
  87. # Connect slider interaction to update function
  88. def slider_update(val):
  89.     update(0)  # Update with initial time t=0 to reflect new slider value
  90.  
  91. num_points_slider.on_changed(slider_update)
  92.  
  93. # Run animation with interactive controls
  94. load_and_animate(duration=10)  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement