-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle_interactions2.py
More file actions
191 lines (167 loc) · 6.59 KB
/
particle_interactions2.py
File metadata and controls
191 lines (167 loc) · 6.59 KB
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
import pygame
import random
import math
import csv
# --- Ustawienia symulacji ---
WIDTH, HEIGHT = 800, 600
PARTICLE_COUNT = 300
PARTICLE_MASS = 1.0
PARTICLE_RADIUS = 3
GRAVITY_STRENGTH = 5
REPULSION_STRENGTH = 1000
REPULSION_DISTANCE = 20
TIME_STEP = 0.1
BACKGROUND_COLOR = (0, 0, 0)
# --- Opcje dodatkowe ---
COLOR_BY_SPEED = True
SHOW_POTENTIAL_FIELD = False
SHOW_TOTAL_ENERGY = False
SHOW_SPEED_HISTOGRAM = False
RANDOM_MASS = True
MASS_MIN = 0.5
MASS_MAX = 2.0
SCALE_RADIUS_BY_MASS = True
# --- Klasa Cząsteczki ---
class Particle:
TYPES = {
"A": {"color": (255, 100, 100)},
"B": {"color": (100, 255, 100)},
"C": {"color": (100, 100, 255)},
}
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = random.uniform(-1, 1) * 0.5
self.vy = random.uniform(-1, 1) * 0.5
self.type = random.choice(list(Particle.TYPES.keys()))
self.color = Particle.TYPES[self.type]["color"]
self.mass = random.uniform(MASS_MIN, MASS_MAX) if RANDOM_MASS else PARTICLE_MASS
base_radius = PARTICLE_RADIUS
self.radius = int(base_radius * self.mass) if SCALE_RADIUS_BY_MASS else base_radius
def update(self):
self.x += self.vx * TIME_STEP
self.y += self.vy * TIME_STEP
def draw(self, screen):
if COLOR_BY_SPEED:
speed = math.sqrt(self.vx**2 + self.vy**2)
intensity = min(255, int(speed * 100))
color = tuple(min(255, int(c * (intensity / 255))) for c in self.color)
else:
color = self.color
pygame.draw.circle(screen, color, (int(self.x), int(self.y)), self.radius)
# --- Pole potencjału ---
def draw_potential_field(screen, particles, resolution=20):
for x in range(0, WIDTH, resolution):
for y in range(0, HEIGHT, resolution):
potential = 0
for p in particles:
dx = p.x - x
dy = p.y - y
dist_sq = dx**2 + dy**2
if dist_sq > 1:
potential += -GRAVITY_STRENGTH * p.mass / dist_sq
intensity = max(0, min(255, int(-potential * 100)))
color = (intensity, intensity, intensity)
pygame.draw.rect(screen, color, (x, y, resolution, resolution))
# --- Energia układu ---
def compute_total_energy(particles):
kinetic = sum(0.5 * p.mass * (p.vx**2 + p.vy**2) for p in particles)
potential = 0
for i in range(len(particles)):
for j in range(i + 1, len(particles)):
dx = particles[j].x - particles[i].x
dy = particles[j].y - particles[i].y
dist = math.sqrt(dx**2 + dy**2)
if dist > 1:
potential += -GRAVITY_STRENGTH * particles[i].mass * particles[j].mass / dist
return kinetic, potential
# --- Histogram prędkości ---
def draw_speed_histogram(screen, particles, bins=10, width=200, height=100, pos=(WIDTH - 210, 10)):
speeds = [math.sqrt(p.vx**2 + p.vy**2) for p in particles]
max_speed = max(speeds) if speeds else 1
bin_counts = [0] * bins
for s in speeds:
index = min(bins - 1, int(s / max_speed * bins))
bin_counts[index] += 1
max_count = max(bin_counts)
for i, count in enumerate(bin_counts):
bar_height = int(count / max_count * height)
x = pos[0] + i * (width // bins)
y = pos[1] + height - bar_height
pygame.draw.rect(screen, (0, 255, 0), (x, y, width // bins - 2, bar_height))
font = pygame.font.SysFont(None, 20)
screen.blit(font.render("Histogram prędkości", True, (255, 255, 255)), (pos[0], pos[1] + height + 5))
# --- Zapis do CSV ---
def save_particle_data(particles, filename="particles.csv"):
with open(filename, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["x", "y", "vx", "vy", "mass", "type"])
for p in particles:
writer.writerow([p.x, p.y, p.vx, p.vy, p.mass, p.type])
# --- Inicjalizacja Pygame ---
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Symulacja Oddziaływań Cząsteczek")
clock = pygame.time.Clock()
particles = [Particle(random.randint(0, WIDTH), random.randint(0, HEIGHT)) for _ in range(PARTICLE_COUNT)]
# --- Główna pętla ---
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
GRAVITY_STRENGTH += 0.1
elif event.key == pygame.K_DOWN:
GRAVITY_STRENGTH = max(0, GRAVITY_STRENGTH - 0.1)
elif event.key == pygame.K_RIGHT:
REPULSION_STRENGTH += 100
elif event.key == pygame.K_LEFT:
REPULSION_STRENGTH = max(0, REPULSION_STRENGTH - 100)
elif event.key == pygame.K_s:
save_particle_data(particles)
for i in range(PARTICLE_COUNT):
p1 = particles[i]
if p1.x <= p1.radius or p1.x >= WIDTH - p1.radius:
p1.vx *= -1
if p1.y <= p1.radius or p1.y >= HEIGHT - p1.radius:
p1.vy *= -1
for j in range(i + 1, PARTICLE_COUNT):
p2 = particles[j]
dx = p2.x - p1.x
dy = p2.y - p1.y
distance = math.sqrt(dx**2 + dy**2)
if distance < 1:
distance = 1
if distance < REPULSION_DISTANCE:
force = -REPULSION_STRENGTH / (distance**2)
force += random.uniform(-0.1, 0.1)
else:
force = GRAVITY_STRENGTH / (distance**2)
fx = force * dx / distance
fy = force * dy / distance
p1.vx += fx / p1.mass * TIME_STEP
p1.vy += fy / p1.mass * TIME_STEP
p2.vx -= fx / p2.mass * TIME_STEP
p2.vy -= fy / p2.mass * TIME_STEP
for p in particles:
p.update()
screen.fill(BACKGROUND_COLOR)
if SHOW_POTENTIAL_FIELD:
draw_potential_field(screen, particles)
if SHOW_TOTAL_ENERGY:
kinetic, potential = compute_total_energy(particles)
font = pygame.font.SysFont(None, 24)
text = font.render(f"E_kin={kinetic:.1f} E_pot={potential:.1f} E_tot={kinetic+potential:.1f}", True, (255, 255, 0))
screen.blit(text, (10, 10))
if SHOW_SPEED_HISTOGRAM:
draw_speed_histogram(screen, particles)
font = pygame.font.SysFont(None, 20)
param_text = font.render(f"G={GRAVITY_STRENGTH:.2f} R={REPULSION_STRENGTH:.0f}", True, (200, 200, 255))
screen.blit(param_text, (10, 35))
for p in particles:
p.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()