-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.py
More file actions
263 lines (227 loc) · 11.4 KB
/
Copy pathbench.py
File metadata and controls
263 lines (227 loc) · 11.4 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import time
import numpy as np
from sim import DoublePendulum, IntegrationMethod, GravitationalSystem
class IntegratorBenchmark:
def __init__(self):
self.total_time = 30.0
self.max_energy = 1e6
# For timestep analysis
self.timesteps = np.logspace(-4, 0, num=30)
# For step count analysis
self.step_counts = np.logspace(1, 5, num=30, dtype=int)
self.pendulum = DoublePendulum()
self.results = {
'verlet': {
'timesteps': [],
'step_counts': [],
'timestep_energy_error': [],
'stepcount_energy_error': [],
'timestep_ops': [],
'stepcount_ops': [],
'timestep_error_std': [],
'timestep_error_min': [],
'timestep_error_max': [],
'stepcount_error_std': [],
'stepcount_error_min': [],
'stepcount_error_max': []
},
'rk4': {
'timesteps': [],
'step_counts': [],
'timestep_energy_error': [],
'stepcount_energy_error': [],
'timestep_ops': [],
'stepcount_ops': [],
'timestep_error_std': [],
'timestep_error_min': [],
'timestep_error_max': [],
'stepcount_error_std': [],
'stepcount_error_min': [],
'stepcount_error_max': []
},
'euler': {
'timesteps': [],
'step_counts': [],
'timestep_energy_error': [],
'stepcount_energy_error': [],
'timestep_ops': [],
'stepcount_ops': [],
'timestep_error_std': [],
'timestep_error_min': [],
'timestep_error_max': [],
'stepcount_error_std': [],
'stepcount_error_min': [],
'stepcount_error_max': []
}
}
def is_state_valid(self, energy):
return (np.isfinite(energy) and
abs(energy) < self.max_energy)
def count_derivative_flops(self):
if isinstance(self.pendulum, DoublePendulum):
# Double pendulum derivative includes:
# - 6 trigonometric operations (sin, cos)
# - ~15 multiplications
# - ~10 additions/subtractions
# - 2 divisions
return 6 * 10 + 15 + 10 + 2 # trig ops are ~10 FLOPs each
else: # GravitationalSystem
# Gravitational system derivative includes:
# - ~10 multiplications for distance calculations
# - ~8 additions/subtractions
# - 2 square roots
# - 3 divisions
return 10 + 8 + 2 * 20 + 3 # sqrt is ~20 FLOPs
def run_timestep_benchmark(self):
for dt in self.timesteps:
for method in ['verlet', 'rk4', 'euler']:
try:
self.pendulum = DoublePendulum()
initial_energy = self.pendulum.calculate_energy()
steps = int(self.total_time / dt)
energy_errors = []
# Count operations
deriv_flops = self.count_derivative_flops()
total_ops = steps * (deriv_flops + 5 if method == 'verlet' else 4 * deriv_flops + 10)
# Run simulation with fixed dt
for _ in range(steps):
try:
if method == 'verlet':
self.pendulum.verlet_step(dt)
elif method == 'rk4':
self.pendulum.rk4_step(dt)
else:
self.pendulum.euler_step(dt)
current_energy = self.pendulum.calculate_energy()
if self.is_state_valid(current_energy):
energy_errors.append(abs(current_energy - initial_energy))
except (RuntimeWarning, OverflowError):
continue
# Store results if enough valid steps
if len(energy_errors) > steps // 4:
self.results[method]['timesteps'].append(dt)
self.results[method]['timestep_ops'].append(total_ops)
self.results[method]['timestep_energy_error'].append(np.mean(energy_errors))
self.results[method]['timestep_error_std'].append(np.std(energy_errors))
self.results[method]['timestep_error_min'].append(np.min(energy_errors))
self.results[method]['timestep_error_max'].append(np.max(energy_errors))
except Exception as e:
print(f"Error in {method} at dt={dt:.6f}: {e}")
continue
def run_stepcount_benchmark(self):
for steps in self.step_counts:
for method in ['verlet', 'rk4', 'euler']:
try:
self.pendulum = DoublePendulum()
initial_energy = self.pendulum.calculate_energy()
dt = self.total_time / steps
energy_errors = []
deriv_flops = self.count_derivative_flops()
if (method == 'verlet'):
total_ops = steps * (deriv_flops + 5)
elif (method == 'euler'):
total_ops = steps * (deriv_flops + 14)
else: # rk4
total_ops = steps * (4 * deriv_flops + 10)
for _ in range(steps):
try:
if method == 'verlet':
self.pendulum.verlet_step(dt)
elif method == 'rk4':
self.pendulum.rk4_step(dt)
else:
self.pendulum.euler_step(dt)
current_energy = self.pendulum.calculate_energy()
if self.is_state_valid(current_energy):
energy_errors.append(abs(current_energy - initial_energy))
except (RuntimeWarning, OverflowError):
continue
if len(energy_errors) > steps//2:
self.results[method]['step_counts'].append(steps)
self.results[method]['stepcount_ops'].append(total_ops)
self.results[method]['stepcount_energy_error'].append(np.mean(energy_errors))
self.results[method]['stepcount_error_std'].append(np.std(energy_errors))
self.results[method]['stepcount_error_min'].append(np.min(energy_errors))
self.results[method]['stepcount_error_max'].append(np.max(energy_errors))
except Exception as e:
print(f"Error in {method} at steps={steps}: {e}")
continue
def plot_results(self):
import matplotlib.pyplot as plt
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 10))
colors = {'verlet': 'blue', 'rk4': 'red', 'euler': 'green'}
methods = ['verlet', 'rk4', 'euler']
# Timestep analysis plots
for method in methods:
ax1.plot(self.results[method]['timesteps'],
self.results[method]['timestep_ops'],
color=colors[method],
label=method.upper())
ax2.semilogy(self.results[method]['timesteps'],
self.results[method]['timestep_energy_error'],
color=colors[method],
label=method.upper())
ax2.fill_between(self.results[method]['timesteps'],
self.results[method]['timestep_error_min'],
self.results[method]['timestep_error_max'],
alpha=0.2,
color=colors[method])
ax1.set_xlabel('Timestep (s)')
ax1.set_ylabel('Operation Count (FLOP)')
ax1.set_title('Timestep vs Computational Cost')
ax1.legend()
ax2.set_xlabel('Timestep (s)')
ax2.set_ylabel('Energy Error (J)')
ax2.set_title('Timestep vs Energy Error')
ax2.legend()
# Step count analysis plots
for method in methods:
ax3.plot(self.results[method]['step_counts'],
self.results[method]['stepcount_ops'],
color=colors[method],
label=method.upper())
ax4.semilogy(self.results[method]['step_counts'],
self.results[method]['stepcount_energy_error'],
color=colors[method],
label=method.upper())
ax4.fill_between(self.results[method]['step_counts'],
self.results[method]['stepcount_error_min'],
self.results[method]['stepcount_error_max'],
alpha=0.2,
color=colors[method])
ax3.set_xlabel('Number of Steps')
ax3.set_ylabel('Operation Count (FLOP)')
ax3.set_title('Steps vs Computational Cost')
ax3.legend()
ax4.set_xlabel('Number of Steps')
ax4.set_ylabel('Energy Error (J)')
ax4.set_title('Steps vs Energy Error')
ax4.legend()
plt.tight_layout()
plt.show()
def plot_efficiency(self):
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
colors = {'verlet': 'blue', 'rk4': 'red', 'euler': 'green'}
for method, n in zip(['verlet', 'rk4', 'euler'],[1,2,3]):
# Calculate efficiency ratio (error/operations)
timestep_ratio = np.array(self.results[method]['timestep_energy_error']) / (self.total_time / np.array(self.results[method]['timestep_ops']))
# Calculate logarithmic efficiency scores
steps = self.total_time / np.array(self.results[method]['timestep_ops'])
timestep_score = np.mean(np.log10(self.results[method]['timestep_energy_error']) /
np.log10(steps))
print(f"{method.upper()} Efficiency Scores:")
print(f" Timestep S = {timestep_score:.4f}")
plt.loglog(self.total_time / np.array(self.results[method]['timesteps']), timestep_ratio,
color=colors[method], label=f"{method.upper()} (timestep)", linestyle='-')
plt.xlabel('Number of Steps')
plt.ylabel('Energy Error / Operation (J/FLOP)')
plt.title('Computational Efficiency Analysis')
plt.legend()
plt.grid(True)
plt.show()
if __name__ == "__main__":
benchmark = IntegratorBenchmark()
benchmark.run_timestep_benchmark()
benchmark.run_stepcount_benchmark()
benchmark.plot_efficiency()