-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
304 lines (257 loc) · 11.6 KB
/
Copy pathmain.cpp
File metadata and controls
304 lines (257 loc) · 11.6 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <GLFW/glfw3.h>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl2.h"
struct Particle {
float x, y;
float vx, vy;
};
// Global Simulation State
float wallMomentumSum = 0.0f;
float timeAccumulator = 0.0f;
float gravity = 0.0f;
float restitution = 1.0f;
float globalRadius = 0.02f;
float particleMass = 1.0f;
float timeScale = 1.0f;
float boxWidth = 1.0f;
float boxHeight = 1.0f;
float tempScaleFactor = 18750000.0f;
// Colors
float particleColor[3] = {1.0f, 0.4f, 0.2f};
float bgColor[3] = {0.1f, 0.15f, 0.2f};
int main() {
if (!glfwInit()) return -1;
GLFWwindow* window = glfwCreateWindow(1024, 768, "Thermodynamics Sandbox", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL2_Init();
std::vector<Particle> particles;
for (int i = 0; i < 50; ++i) {
float x = ((rand() % 1000) / 500.0f) * 0.8f;
float y = ((rand() % 1000) / 500.0f) * 0.8f;
float vx = (((rand() % 1000) / 500.0f) - 1.0f) * 0.004f;
float vy = (((rand() % 1000) / 500.0f) - 1.0f) * 0.004f;
particles.push_back({x, y, vx, vy});
}
double lastTime = glfwGetTime();
float currentPressure = 0.0f;
bool lastMouseState = false;
int particleCountInput = 50;
float targetTempInput = 300.0f;
float targetPressureInput = 10.0f;
float targetEnergyInput = 1000.0f;
while (!glfwWindowShouldClose(window)) {
double currentTime = glfwGetTime();
float rawDelta = static_cast<float>(currentTime - lastTime);
lastTime = currentTime;
float deltaTime = rawDelta * timeScale;
timeAccumulator += rawDelta;
glfwPollEvents();
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
bool currentMouseState = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
if (!io.WantCaptureMouse && currentMouseState && !lastMouseState) {
double mx, my;
int ww, wh;
glfwGetCursorPos(window, &mx, &my);
glfwGetWindowSize(window, &ww, &wh);
float nx = (mx / (ww / 2.0f)) - 1.0f;
float ny = 1.0f - (my / (wh / 2.0f));
if (nx >= -boxWidth && nx <= boxWidth && ny >= -boxHeight && ny <= boxHeight) {
for (int i = 0; i < 5; ++i) {
float vx = (((rand() % 1000) / 500.0f) - 1.0f) * 0.004f;
float vy = (((rand() % 1000) / 500.0f) - 1.0f) * 0.004f;
particles.push_back({nx, ny, vx, vy});
}
particleCountInput = static_cast<int>(particles.size());
}
}
lastMouseState = currentMouseState;
glClearColor(bgColor[0], bgColor[1], bgColor[2], 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
wallMomentumSum = 0.0f;
// Physics Update
for (auto& p : particles) {
p.vy -= gravity * deltaTime;
p.x += p.vx * timeScale;
p.y += p.vy * timeScale;
if (p.x > boxWidth - globalRadius) p.x = boxWidth - globalRadius;
if (p.x < -boxWidth + globalRadius) p.x = -boxWidth + globalRadius;
if (p.y > boxHeight - globalRadius) p.y = boxHeight - globalRadius;
if (p.y < -boxHeight + globalRadius) p.y = -boxHeight + globalRadius;
if (p.x >= boxWidth - globalRadius || p.x <= -boxWidth + globalRadius) {
p.vx *= -restitution;
wallMomentumSum += std::abs(2.0f * particleMass * p.vx);
}
if (p.y >= boxHeight - globalRadius || p.y <= -boxHeight + globalRadius) {
p.vy *= -restitution;
wallMomentumSum += std::abs(2.0f * particleMass * p.vy);
}
}
// Collisions
for (size_t i = 0; i < particles.size(); ++i) {
for (size_t j = i + 1; j < particles.size(); ++j) {
float dx = particles[j].x - particles[i].x;
float dy = particles[j].y - particles[i].y;
float dist = std::sqrt(dx * dx + dy * dy);
float minDist = globalRadius * 2.0f;
if (dist < minDist) {
if (dist == 0.0f) { particles[j].x += 0.001f; continue; }
float overlap = 0.5f * (minDist - dist);
particles[i].x -= overlap * (dx / dist);
particles[i].y -= overlap * (dy / dist);
particles[j].x += overlap * (dx / dist);
particles[j].y += overlap * (dy / dist);
float nx = dx / dist;
float ny = dy / dist;
float kx = particles[i].vx - particles[j].vx;
float ky = particles[i].vy - particles[j].vy;
float p_impulse = (nx * kx + ny * ky);
particles[i].vx -= p_impulse * nx;
particles[i].vy -= p_impulse * ny;
particles[j].vx += p_impulse * nx;
particles[j].vy += p_impulse * ny;
}
}
}
if (timeAccumulator >= 0.5f) {
currentPressure = (wallMomentumSum / timeAccumulator) * 100.0f;
timeAccumulator = 0.0f;
}
// Calculations
float totalKE = 0.0f;
float totalSpeedSq = 0.0f;
for (const auto& p : particles) {
float vSq = p.vx * p.vx + p.vy * p.vy;
totalKE += 0.5f * particleMass * vSq;
totalSpeedSq += vSq;
}
size_t N = particles.size();
float avgKE = N == 0 ? 0.0f : totalKE / N;
float temperature = avgKE * tempScaleFactor;
float internalEnergy = totalKE * tempScaleFactor;
float vRMS = N == 0 ? 0.0f : std::sqrt(totalSpeedSq / N) * 1000.0f;
float containerArea = (2.0f * boxWidth) * (2.0f * boxHeight);
float meanFreePath = (N == 0 || globalRadius == 0.0f) ? 0.0f :
containerArea / (2.828f * (2.0f * globalRadius) * N);
float idealStateRatio = (temperature == 0.0f || N == 0) ? 0.0f :
(currentPressure * containerArea) / (N * temperature);
// Draw Container Box
glColor3f(0.3f, 0.5f, 0.7f);
glBegin(GL_LINE_LOOP);
glVertex2f(-boxWidth, -boxHeight);
glVertex2f(boxWidth, -boxHeight);
glVertex2f(boxWidth, boxHeight);
glVertex2f(-boxWidth, boxHeight);
glEnd();
// Draw Particles
glPointSize(globalRadius * 300.0f);
glBegin(GL_POINTS);
glColor3f(particleColor[0], particleColor[1], particleColor[2]);
for (const auto& p : particles) {
glVertex2f(p.x, p.y);
}
glEnd();
// Expanded GUI
ImGui::SetNextWindowSize(ImVec2(360, 720), ImGuiCond_FirstUseEver);
ImGui::Begin("Thermodynamics Dashboard");
ImGui::TextColored(ImVec4(0.2f, 0.8f, 1.0f, 1.0f), "State Telemetry");
ImGui::Text("Particle Count (N): %zu", N);
ImGui::Text("Temperature (T): %.1f K", temperature);
ImGui::Text("Pressure (P): %.2f P", currentPressure);
ImGui::Text("Container Area (A): %.2f u²", containerArea);
ImGui::Separator();
ImGui::TextColored(ImVec4(0.2f, 0.8f, 1.0f, 1.0f), "Derived Quantities");
ImGui::Text("Internal Energy (U): %.1f J", internalEnergy);
ImGui::Text("Avg Kinetic Energy: %.2f e-6 J", avgKE * 1e6f);
ImGui::Text("RMS Speed (v_rms): %.2f u/s", vRMS);
ImGui::Text("Mean Free Path (Lambda): %.3f u", meanFreePath);
ImGui::Text("Ideal Gas Ratio (PA/NT): %.4f", idealStateRatio);
ImGui::Separator();
ImGui::TextColored(ImVec4(0.2f, 0.8f, 1.0f, 1.0f), "State Adjustments");
ImGui::PushItemWidth(100.0f);
// Count Setter
if (ImGui::InputInt("Count", &particleCountInput)) {
if (particleCountInput < 0) particleCountInput = 0;
int currentN = static_cast<int>(particles.size());
if (particleCountInput > currentN) {
for (int i = 0; i < particleCountInput - currentN; ++i) {
float x = ((rand() % 1000) / 500.0f) * (boxWidth * 0.8f);
float y = ((rand() % 1000) / 500.0f) * (boxHeight * 0.8f);
float vx = (((rand() % 1000) / 500.0f) - 1.0f) * 0.004f;
float vy = (((rand() % 1000) / 500.0f) - 1.0f) * 0.004f;
particles.push_back({x, y, vx, vy});
}
} else if (particleCountInput < currentN) {
particles.resize(particleCountInput);
}
}
// Temperature Setter
ImGui::InputFloat("Temp(K)", &targetTempInput, 10.0f, 50.0f, "%.1f"); ImGui::SameLine();
if (ImGui::Button("Set T") && temperature > 0.001f && targetTempInput > 0.0f) {
float scale = std::sqrt(targetTempInput / temperature);
for (auto& p : particles) { p.vx *= scale; p.vy *= scale; }
}
// Internal Energy Setter
ImGui::InputFloat("Energy(J)", &targetEnergyInput, 100.0f, 500.0f, "%.1f"); ImGui::SameLine();
if (ImGui::Button("Set U") && internalEnergy > 0.001f && targetEnergyInput > 0.0f) {
float scale = std::sqrt(targetEnergyInput / internalEnergy);
for (auto& p : particles) { p.vx *= scale; p.vy *= scale; }
}
// Pressure Setter
ImGui::InputFloat("Pressure", &targetPressureInput, 1.0f, 5.0f, "%.2f"); ImGui::SameLine();
if (ImGui::Button("Set P") && currentPressure > 0.001f && targetPressureInput > 0.0f) {
float scale = std::sqrt(targetPressureInput / currentPressure);
for (auto& p : particles) { p.vx *= scale; p.vy *= scale; }
}
ImGui::Separator();
ImGui::TextColored(ImVec4(0.2f, 0.8f, 1.0f, 1.0f), "Environment & Container");
ImGui::SliderFloat("Time Scale", &timeScale, 0.0f, 5.0f, "%.2fx");
ImGui::SliderFloat("Box Width", &boxWidth, 0.1f, 1.0f, "%.2f");
ImGui::SliderFloat("Box Height", &boxHeight, 0.1f, 1.0f, "%.2f");
ImGui::Separator();
ImGui::TextColored(ImVec4(0.2f, 0.8f, 1.0f, 1.0f), "Physics Constants");
ImGui::InputFloat("Mass", &particleMass, 0.1f, 1.0f, "%.2f");
ImGui::InputFloat("Radius", &globalRadius, 0.005f, 0.01f, "%.3f");
ImGui::InputFloat("Gravity", &gravity, 0.01f, 0.1f, "%.3f");
ImGui::InputFloat("Restitution", &restitution, 0.05f, 0.1f, "%.2f");
ImGui::InputFloat("Temp Scalar", &tempScaleFactor, 100000.0f, 1000000.0f, "%.0f");
ImGui::Separator();
ImGui::TextColored(ImVec4(0.2f, 0.8f, 1.0f, 1.0f), "Visuals");
ImGui::ColorEdit3("Particle Color", particleColor, ImGuiColorEditFlags_NoInputs);
ImGui::ColorEdit3("Background", bgColor, ImGuiColorEditFlags_NoInputs);
ImGui::PopItemWidth();
if (ImGui::Button("Reset All", ImVec2(100, 30))) {
particles.clear();
particleCountInput = 0;
boxWidth = 1.0f;
boxHeight = 1.0f;
timeScale = 1.0f;
particleMass = 1.0f;
}
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
return 0;
}