Friday, 2 May 2025

3D Plasma Wave Simulation using Python

 


import matplotlib.pyplot as plt

import numpy as np

from mpl_toolkits.mplot3d import Axes3D

x=np.linspace(-5,5,30)

y=np.linspace(-5,5,30)

z=np.linspace(-5,5,30)

X,Y,Z=np.meshgrid(x,y,z)

wave=np.sin(2*np.pi*X*10)*np.cos(2*np.pi*Y/10)*np.sin(2*np.pi*Z/10)

threshold=0.5

mask=np.abs(wave)>threshold

fig=plt.figure(figsize=(6,6))

ax=fig.add_subplot(111,projection='3d')

sc=ax.scatter(X[mask],Y[mask],Z[mask],c=wave[mask],cmap='plasma',s=15,alpha=0.8)

ax.set_title('3D Plasma Wave Simulation')

ax.set_xlabel('X axis')

ax.set_ylabel('Y axis')

ax.set_zlabel('Z axis')

ax.set_box_aspect([1,1,1])

fig.colorbar(sc,shrink=0.6,label='Wave Amplitude')

plt.tight_layout()

plt.show()

#source code --> clcoding.com

Code Explanation:

1. Import Libraries

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

numpy: For numerical operations and generating arrays.

 matplotlib.pyplot: For plotting.

 mpl_toolkits.mplot3d: Enables 3D plotting with Axes3D.

 2. Define 3D Coordinate Grids

x = np.linspace(-5, 5, 30)

y = np.linspace(-5, 5, 30)

z = np.linspace(-5, 5, 30)

Creates evenly spaced values from -5 to 5 along each axis (30 points).

 These serve as the spatial coordinates in the 3D space.

 3. Create 3D Meshgrid

X, Y, Z = np.meshgrid(x, y, z)

np.meshgrid converts the 1D arrays into 3D coordinate grids.

 Each point in the 3D volume now has corresponding (X, Y, Z) coordinates.

 4. Define the Plasma Wave Function

wave = np.sin(2 * np.pi * X / 10) * np.cos(2 * np.pi * Y / 10) * np.sin(2 * np.pi * Z / 10)

A mathematical expression to simulate a 3D plasma wave.

 Combines sine and cosine functions to simulate oscillating wave patterns in space.

 5. Apply Wave Threshold Mask

threshold = 0.5

mask = np.abs(wave) > threshold

Sets a cutoff (threshold) to visualize only strong wave amplitudes.

 mask is a boolean array selecting only points where wave amplitude exceeds 0.5.

 6. Set Up the 3D Plot

fig = plt.figure(figsize=(6, 6))

ax = fig.add_subplot(111, projection='3d')

Initializes a figure with a 3D subplot.

 7. Scatter Plot the Wave Points

sc = ax.scatter(X[mask], Y[mask], Z[mask], c=wave[mask], cmap='plasma', s=15, alpha=0.8)

Plots only the points that passed the threshold mask.

c=wave[mask]: Colors each point based on wave amplitude.

cmap='plasma': Uses a vibrant colormap.

 s=15: Sets the point size.

 alpha=0.8: Semi-transparent points for better 3D depth effect.

 8. Customize the Plot

ax.set_title('3D Plasma Wave Simulation')

ax.set_xlabel('X Axis')

ax.set_ylabel('Y Axis')

ax.set_zlabel('Z Axis')

ax.set_box_aspect([1, 1, 1])

Adds title and axis labels.

 set_box_aspect([1, 1, 1]): Ensures equal aspect ratio for proper 3D geometry.

 9. Add Colorbar and Show Plot

fig.colorbar(sc, shrink=0.6, label='Wave Amplitude')

plt.tight_layout()

plt.show()

Adds a color bar indicating amplitude values.

 tight_layout() adjusts spacing to prevent clipping.

 show() displays the final visualization.

 

 


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (161) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (254) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (299) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (226) Data Strucures (14) Deep Learning (76) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (49) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (198) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (12) PHP (20) Projects (32) Python (1222) Python Coding Challenge (902) Python Quiz (350) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)