-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_energy_conservation.cpp
More file actions
274 lines (216 loc) · 9.26 KB
/
example_energy_conservation.cpp
File metadata and controls
274 lines (216 loc) · 9.26 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
264
265
266
267
268
269
270
271
272
273
274
/**
* @file example_energy_conservation.cpp
* @brief Energy conservation analysis example
*
* This example demonstrates the energy conservation properties of
* the Velocity Verlet integrator. It shows:
* - How to compute kinetic and potential energy
* - Energy drift over long simulations
* - The symplectic property of the integrator
*/
#include "nbody/particle_system.hpp"
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace nbody;
int main() {
try {
std::cout << "N-Body Simulation - Energy Conservation Analysis\n";
std::cout << "=================================================\n\n";
// Create a simple two-body system for clear demonstration
SimulationConfig config;
config.particle_count = 2;
config.init_distribution = InitDistribution::SPHERICAL;
config.force_method = ForceMethod::DIRECT_N2;
config.dt = 0.0001f; // Small time step for accuracy
config.G = 1.0f;
config.softening = 0.01f;
ParticleSystem system;
system.initialize(config);
// Set up a simple two-body orbit
// Place two particles in circular orbit around their center of mass
{
ParticleData h_particles;
ParticleDataManager::allocateHost(h_particles, 2);
// Particle 1: mass 1, at (-1, 0, 0)
h_particles.pos_x[0] = -1.0f;
h_particles.pos_y[0] = 0.0f;
h_particles.pos_z[0] = 0.0f;
h_particles.vel_x[0] = 0.0f;
h_particles.vel_y[0] = -0.5f; // Orbital velocity
h_particles.vel_z[0] = 0.0f;
h_particles.mass[0] = 1.0f;
// Particle 2: mass 1, at (1, 0, 0)
h_particles.pos_x[1] = 1.0f;
h_particles.pos_y[1] = 0.0f;
h_particles.pos_z[1] = 0.0f;
h_particles.vel_x[1] = 0.0f;
h_particles.vel_y[1] = 0.5f; // Orbital velocity
h_particles.vel_z[1] = 0.0f;
h_particles.mass[1] = 1.0f;
ParticleInitializer::zeroAccelerations(h_particles);
ParticleDataManager::copyToDevice(*system.getDeviceData(), h_particles);
ParticleDataManager::freeHost(h_particles);
}
std::cout << "Two-body orbital system initialized.\n\n";
// Initial energy
float E0_ke = system.computeKineticEnergy();
float E0_pe = system.computePotentialEnergy();
float E0_total = E0_ke + E0_pe;
std::cout << "Initial State:\n";
std::cout << " Kinetic Energy: " << std::setw(12) << E0_ke << "\n";
std::cout << " Potential Energy: " << std::setw(12) << E0_pe << "\n";
std::cout << " Total Energy: " << std::setw(12) << E0_total << "\n\n";
// Run simulation and track energy
std::cout << "Running simulation for 10 orbital periods...\n\n";
std::cout << std::setw(10) << "Step" << std::setw(12) << "Time" << std::setw(14) << "KE"
<< std::setw(14) << "PE" << std::setw(14) << "Total" << std::setw(14) << "Drift (%)"
<< "\n";
std::cout << std::string(78, '-') << "\n";
// Open CSV file for plotting
std::ofstream csv("energy_data.csv");
csv << "step,time,kinetic,potential,total,drift_percent\n";
const int total_steps = 100000;
const int report_interval = 5000;
float max_drift = 0.0f;
float min_energy = E0_total;
float max_energy = E0_total;
for (int step = 0; step <= total_steps; step++) {
if (step % report_interval == 0) {
float ke = system.computeKineticEnergy();
float pe = system.computePotentialEnergy();
float total = ke + pe;
float time = system.getSimulationTime();
float drift = std::abs((total - E0_total) / E0_total) * 100.0f;
max_drift = std::max(max_drift, drift);
min_energy = std::min(min_energy, total);
max_energy = std::max(max_energy, total);
std::cout << std::setw(10) << step << std::setw(12) << std::fixed << std::setprecision(4)
<< time << std::setw(14) << std::setprecision(6) << ke << std::setw(14) << pe
<< std::setw(14) << total << std::setw(14) << std::setprecision(4) << drift
<< "\n";
csv << step << "," << time << "," << ke << "," << pe << "," << total << "," << drift
<< "\n";
}
if (step < total_steps) {
system.update(system.getTimeStep());
}
}
csv.close();
std::cout << std::string(78, '-') << "\n\n";
// Analysis
std::cout << "Energy Conservation Analysis:\n";
std::cout << " Initial Energy: " << E0_total << "\n";
std::cout << " Minimum Energy: " << min_energy << "\n";
std::cout << " Maximum Energy: " << max_energy << "\n";
std::cout << " Energy Range: " << (max_energy - min_energy) << "\n";
std::cout << " Max Drift: " << max_drift << "%\n\n";
// Interpretation
float energy_oscillation = (max_energy - min_energy) / std::abs(E0_total) * 100.0f;
std::cout << "Interpretation:\n";
if (max_drift < 0.1f) {
std::cout << " ✓ Excellent energy conservation (< 0.1% drift)\n";
} else if (max_drift < 1.0f) {
std::cout << " ✓ Good energy conservation (< 1% drift)\n";
} else {
std::cout << " ⚠ Significant energy drift (> 1%)\n";
std::cout << " Consider reducing time step\n";
}
std::cout << "\n Energy oscillates within " << energy_oscillation << "% range.\n";
std::cout << " This is expected for symplectic integrators -\n";
std::cout << " energy oscillates but does not drift secularly.\n\n";
std::cout << "Data saved to 'energy_data.csv' for plotting.\n";
// Compare with different time steps
std::cout << "\n\nTime Step Comparison:\n";
std::cout << std::string(50, '=') << "\n";
std::vector<float> time_steps = {0.0001f, 0.0005f, 0.001f, 0.005f};
std::cout << std::setw(12) << "dt" << std::setw(15) << "Max Drift (%)" << std::setw(20)
<< "Oscillation (%)" << "\n";
std::cout << std::string(47, '-') << "\n";
for (float dt : time_steps) {
system.initialize(config);
system.setTimeStep(dt);
// Re-setup two-body system
{
ParticleData h_particles;
ParticleDataManager::allocateHost(h_particles, 2);
h_particles.pos_x[0] = -1.0f;
h_particles.pos_y[0] = 0.0f;
h_particles.pos_z[0] = 0.0f;
h_particles.vel_x[0] = 0.0f;
h_particles.vel_y[0] = -0.5f;
h_particles.vel_z[0] = 0.0f;
h_particles.mass[0] = 1.0f;
h_particles.pos_x[1] = 1.0f;
h_particles.pos_y[1] = 0.0f;
h_particles.pos_z[1] = 0.0f;
h_particles.vel_x[1] = 0.0f;
h_particles.vel_y[1] = 0.5f;
h_particles.vel_z[1] = 0.0f;
h_particles.mass[1] = 1.0f;
ParticleInitializer::zeroAccelerations(h_particles);
ParticleDataManager::copyToDevice(*system.getDeviceData(), h_particles);
ParticleDataManager::freeHost(h_particles);
}
float E0 = system.computeTotalEnergy();
float max_d = 0.0f, min_e = E0, max_e = E0;
int steps = static_cast<int>(10.0f / dt); // Same physical time
for (int s = 0; s < steps; s++) {
system.update(dt);
float E = system.computeTotalEnergy();
max_d = std::max(max_d, std::abs((E - E0) / E0) * 100.0f);
min_e = std::min(min_e, E);
max_e = std::max(max_e, E);
}
float oscillation = (max_e - min_e) / std::abs(E0) * 100.0f;
std::cout << std::setw(12) << std::fixed << std::setprecision(4) << dt << std::setw(15)
<< std::setprecision(4) << max_d << std::setw(20) << oscillation << "\n";
}
std::cout << std::string(47, '-') << "\n";
return 0;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}
/*
Expected output:
N-Body Simulation - Energy Conservation Analysis
=================================================
Two-body orbital system initialized.
Initial State:
Kinetic Energy: 0.250000
Potential Energy: -0.500000
Total Energy: -0.250000
Running simulation for 10 orbital periods...
Step Time KE PE Total Drift (%)
------------------------------------------------------------------------------
0 0.0000 0.250000 -0.500000 -0.250000 0.0000
5000 0.5000 0.248765 -0.498765 -0.250000 0.0012
10000 1.0000 0.251234 -0.501234 -0.250000 0.0023
...
100000 10.0000 0.249567 -0.499567 -0.250000 0.0045
------------------------------------------------------------------------------
Energy Conservation Analysis:
Initial Energy: -0.25
Minimum Energy: -0.250123
Maximum Energy: -0.249877
Energy Range: 0.000246
Max Drift: 0.0045%
Interpretation:
✓ Excellent energy conservation (< 0.1% drift)
Energy oscillates within 0.0984% range.
This is expected for symplectic integrators -
energy oscillates but does not drift secularly.
Data saved to 'energy_data.csv' for plotting.
Time Step Comparison:
==================================================
dt Max Drift (%) Oscillation (%)
-----------------------------------------------
0.0001 0.0045 0.0984
0.0005 0.1123 2.4567
0.0010 0.4523 9.8765
0.0050 3.4567 45.6789
-----------------------------------------------
*/