Sunday, 11 May 2025

3D Simulated Fireball using Python


import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

n = 5000

r = np.random.rand(n) ** 0.5  

theta = np.random.uniform(0, 2 * np.pi, n)

phi = np.random.uniform(0, np.pi, n)

x = r * np.sin(phi) * np.cos(theta)

y = r * np.sin(phi) * np.sin(theta)

z = r * np.cos(phi)

intensity = 1 - rs

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

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

sc = ax.scatter(x, y, z, c=intensity, cmap='hot', s=2, alpha=0.8)

ax.set_title("3D Simulated Fireball")

ax.set_xlabel("X")

ax.set_ylabel("Y")

ax.set_zlabel("Z")

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

plt.tight_layout()

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Import Required Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
numpy: For math operations and random number generation.

matplotlib.pyplot: For plotting.

Axes3D: Enables 3D plotting using matplotlib.

2. Define Parameters and Generate Random Points in Spherical Coordinates
n = 5000  # Number of particles in the fireball
r = np.random.rand(n) ** 0.5  
Generates 5000 random radii from the origin.

** 0.5 makes particles denser toward the center, like a real fireball.

3. Generate Random Angular Coordinates
theta = np.random.uniform(0, 2 * np.pi, n)
phi = np.random.uniform(0, np.pi, n)
theta: Random azimuthal angle around the Z-axis (0 to 2π).

phi: Polar angle from the vertical axis (0 to π).

4. Convert Spherical to Cartesian Coordinates
x = r * np.sin(phi) * np.cos(theta)
y = r * np.sin(phi) * np.sin(theta)
z = r * np.cos(phi)
Converts spherical (r, theta, phi) to 3D Cartesian coordinates (x, y, z) to plot in 3D space.

5. Define Fireball Intensity (Brighter at Core)
intensity = 1 - r
Points closer to the center (r ≈ 0) are brighter (intensity ≈ 1).

Points near the edge fade out (intensity ≈ 0).

6. Set Up 3D Plotting Area
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
Sets up a 3D subplot with a square figure size.

7. Plot the Fireball with Color Mapping
sc = ax.scatter(x, y, z, c=intensity, cmap='hot', s=2, alpha=0.8)
scatter: Plots all points in 3D.

c=intensity: Colors depend on how close the point is to the center.

cmap='hot': Uses a red-orange-yellow gradient like real fire.

s=2: Small point size for a glowing cluster.

alpha=0.8: Slight transparency for blending.

8. Add Labels and Aesthetics
ax.set_title("3D Simulated Fireball")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
ax.set_box_aspect([1, 1, 1])
Title and axis labels.

set_box_aspect([1, 1, 1]): Ensures equal scaling in all directions (spherical symmetry).

9. Show the Plot
plt.tight_layout()
plt.show()
tight_layout(): Adjusts spacing.

show(): Displays the final plot.


 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) 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 (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) 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)