-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolar.c
549 lines (454 loc) · 16.5 KB
/
solar.c
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include <gtk/gtk.h>
#include <gio/gio.h>
struct body
{
double x0;
double y0;
int show_tip;
char name[16];
double r[3];
double v[3];
double m;
// color
double cr;
double cg;
double cb;
// radius
double rad;
};
struct preset {
const char* name;
const char* input_file;
int method;
double dt;
};
struct context {
int nbodies;
struct body bodies[10000];
GtkLabel* r[3];
GtkLabel* v[3];
int active_body;
GtkWidget* body_selector;
GtkWidget* drawing_area;
guint timer_id;
double zoom;
double zoom_initial;
GtkStringList* kernels;
// kernel settings
struct preset* presets;
int active_preset;
int method;
char input_file[100];
double dt;
// controls
GtkWidget* method_selector;
GtkEntryBuffer* input_file_entry;
GtkWidget* dt_selector;
// child
GSubprocess* subprocess;
GInputStream* input;
GCancellable* cancel_read;
GDataInputStream* line_input;
int header_processed;
int suspend;
};
void draw(GtkDrawingArea* da, cairo_t *cr, int w, int h, void* user_data)
{
struct context* ctx = user_data;
for (int i = 0; i < ctx->nbodies; ++i)
{
struct body* body = &ctx->bodies[i];
double x = body->r[0] * w * ctx->zoom + w / 2.0;
double y = body->r[1] * w * ctx->zoom + h / 2.0;
if (ctx->active_body == i) {
cairo_set_source_rgb(cr, 1, 0, 0);
} else {
cairo_set_source_rgb(cr, body->cr, body->cg, body->cb);
}
cairo_arc(cr, x, y, 2*body->rad, 0, 2 * M_PI);
cairo_fill(cr);
body->x0 = x;
body->y0 = y;
if (body->show_tip)
{
cairo_set_font_size(cr, 13);
cairo_move_to(cr, x, y);
cairo_show_text(cr, body->name);
}
}
}
int get_body(double x, double y, struct context* ctx) {
double mindist = -1;
int argmin = -1;
int i;
for (i = 0; i < ctx->nbodies; i = i + 1)
{
struct body* body = &ctx->bodies[i];
double dist = (body->x0 - x) * (body->x0 - x) +
(body->y0 - y) * (body->y0 - y);
if (argmin < 0 || dist < mindist)
{
mindist = dist;
argmin = i;
}
}
if (argmin >= 0 && mindist * mindist < 100)
{
return argmin;
} else {
return -1;
}
}
void button_press(GtkGestureClick* self, int npress, double x, double y, struct context* ctx)
{
int index = get_body(x, y, ctx);
if (index >= 0)
{
gtk_drop_down_set_selected(GTK_DROP_DOWN(ctx->body_selector), index);
ctx->active_body = index;
}
}
void motion_notify(GtkEventControllerMotion* self, double x, double y, struct context* ctx)
{
int i;
int argmin = get_body(x, y, ctx);
for (i = 0; i < ctx->nbodies; i = i + 1)
{
ctx->bodies[i].show_tip = 0;
}
if (argmin >= 0)
{
ctx->bodies[argmin].show_tip = 1;
}
}
void stop_kernel(struct context* ctx) {
if (ctx->subprocess) {
g_cancellable_cancel(ctx->cancel_read);
g_subprocess_force_exit(ctx->subprocess);
g_input_stream_close(G_INPUT_STREAM(ctx->line_input), NULL, NULL);
g_input_stream_close(ctx->input, NULL, NULL);
g_object_unref(ctx->input);
g_object_unref(ctx->line_input);
g_object_unref(ctx->cancel_read);
// g_object_unref(ctx->subprocess);
ctx->subprocess = NULL;
}
}
void close_window(GtkWidget* widget, struct context* ctx)
{
if (ctx->timer_id > 0)
{
g_source_remove(ctx->timer_id);
ctx->timer_id = 0;
}
stop_kernel(ctx);
}
void read_child(struct context* ctx);
void update_all(struct context* ctx) {
char buf[1024];
int i = ctx->active_body;
if (i >= 0 && i < ctx->nbodies) {
for (int j = 0; j < 3; j = j + 1)
{
snprintf(buf, sizeof(buf), "<tt>r<sub>%c</sub> = % .8le</tt>", 'x'+j, ctx->bodies[i].r[j]);
gtk_label_set_label(ctx->r[j], buf);
snprintf(buf, sizeof(buf), "<tt>v<sub>%c</sub> = % .8le</tt>", 'x'+j, ctx->bodies[i].v[j]);
gtk_label_set_label(ctx->v[j], buf);
}
}
gtk_widget_queue_draw(ctx->drawing_area);
}
void on_new_data(GObject* input, GAsyncResult* res, gpointer user_data) {
struct context* ctx = user_data;
gsize size;
char* line = g_data_input_stream_read_line_finish(G_DATA_INPUT_STREAM(input), res, &size, NULL);
if (line) {
if (*line == 't') {
// skip column names
} else if (*line == '#' && ctx->nbodies < sizeof(ctx->bodies)/sizeof(struct body)) {
// header
struct body* body = &ctx->bodies[ctx->nbodies++];
body->cr = body->cg = body->cb = 0.0;
body->rad = 1.0;
char color[12];
double rad;
int a = sscanf(line, "# %15s %lf %10s %lf", body->name, &body->m, color, &rad);
if (a >= 3) {
// parse color
int number = strtol(color, NULL, 16);
double r, g, b;
b = ((number >> 0) & 0xff) / 256.;
g = ((number >> 8) & 0xff) / 256.;
r = ((number >> 16) & 0xff) / 256.;
body->cr = r;
body->cg = g;
body->cb = b;
}
if (a >= 4) {
body->rad = rad;
}
} else if (!ctx->header_processed) {
ctx->header_processed = 1;
GtkStringList* strings = GTK_STRING_LIST(gtk_drop_down_get_model(GTK_DROP_DOWN(ctx->body_selector)));
for (int i = 0; i < ctx->nbodies; i++) {
gtk_string_list_append(strings, ctx->bodies[i].name);
}
ctx->active_body = 0;
}
if (ctx->header_processed) {
// parse line
const char* sep = " ";
char* p = line;
p = strtok(p, sep); // skip time
printf("time=%s\n", p);
for (int i = 0; p && i < ctx->nbodies; i++) {
if ((p = strtok(NULL, sep))) ctx->bodies[i].r[0] = atof(p);
if ((p = strtok(NULL, sep))) ctx->bodies[i].r[1] = atof(p);
if ((p = strtok(NULL, sep))) ctx->bodies[i].r[2] = atof(p);
if ((p = strtok(NULL, sep))) ctx->bodies[i].v[0] = atof(p);
if ((p = strtok(NULL, sep))) ctx->bodies[i].v[1] = atof(p);
if ((p = strtok(NULL, sep))) ctx->bodies[i].v[2] = atof(p);
}
update_all(ctx);
ctx->suspend = 1;
}
g_free(line); // performance issue
if (!ctx->suspend) {
read_child(ctx);
}
}
}
void read_child(struct context* ctx) {
g_data_input_stream_read_line_async(
ctx->line_input,
/*priority*/ 0, ctx->cancel_read,
on_new_data, ctx);
}
gboolean timeout(struct context* ctx)
{
if (ctx->header_processed && ctx->suspend) {
ctx->suspend = 0;
read_child(ctx);
}
return ctx->timer_id > 0;
}
void active_changed(GtkDropDown* self, GtkStateFlags flags, struct context* ctx)
{
int active = gtk_drop_down_get_selected(self);
ctx->active_body = active;
}
void spawn(struct context* ctx) {
const gchar* exe = ctx->method == 0
? "./euler.exe"
: "./verlet.exe";
gchar dt[40];
snprintf(dt, sizeof(dt), "%.16e", ctx->dt);
const gchar* argv[] = {
exe,
"--input", ctx->input_file,
"--dt", dt,
"--T", "1e20",
NULL};
ctx->subprocess = g_subprocess_newv(argv, G_SUBPROCESS_FLAGS_STDOUT_PIPE, NULL);
ctx->input = g_subprocess_get_stdout_pipe(ctx->subprocess);
ctx->line_input = g_data_input_stream_new(ctx->input);
ctx->cancel_read = g_cancellable_new();
}
void start_kernel(struct context* ctx) {
stop_kernel(ctx);
GtkStringList* strings = GTK_STRING_LIST(gtk_drop_down_get_model(GTK_DROP_DOWN(ctx->body_selector)));
gtk_string_list_splice(strings, 0, ctx->nbodies, NULL);
ctx->nbodies = 0;
ctx->active_body = -1;
ctx->header_processed = 0;
ctx->suspend = 0;
spawn(ctx);
read_child(ctx);
}
void method_changed(GtkDropDown* self, GtkStateFlags flags, struct context* ctx)
{
int active = gtk_drop_down_get_selected(self);
if (active != ctx->method) {
ctx->method = active;
start_kernel(ctx);
}
}
void preset_changed(GtkDropDown* self, GtkStateFlags flags, struct context* ctx)
{
int active = gtk_drop_down_get_selected(self);
if (active != ctx->active_preset) {
ctx->active_preset = active;
struct preset* preset = &ctx->presets[active];
ctx->method = preset->method;
ctx->dt = preset->dt;
strncpy(ctx->input_file, preset->input_file, sizeof(ctx->input_file)-1);
ctx->input_file[sizeof(ctx->input_file)-1] = 0;
gtk_drop_down_set_selected(GTK_DROP_DOWN(ctx->method_selector), preset->method);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(ctx->dt_selector), preset->dt);
gtk_entry_buffer_set_text(ctx->input_file_entry, preset->input_file, strlen(preset->input_file));
start_kernel(ctx);
}
}
void input_file_changed(GtkEntry* self, struct context* ctx) {
GtkEntryBuffer* buffer = gtk_entry_get_buffer(self);
const char* text = gtk_entry_buffer_get_text(buffer);
if (strcmp(text, ctx->input_file)) {
strncpy(ctx->input_file, text, sizeof(ctx->input_file)-1);
ctx->input_file[sizeof(ctx->input_file)-1] = 0;
start_kernel(ctx);
}
}
void dt_changed(GtkSpinButton* self, struct context* ctx)
{
double value = gtk_spin_button_get_value(self);
if (value != ctx->dt) {
ctx->dt = value;
start_kernel(ctx);
}
}
void zoom_begin(GtkGesture* gesture, GdkEventSequence* sequence, struct context* ctx)
{
ctx->zoom_initial = ctx->zoom;
}
void zoom_scale_changed(GtkGestureZoom* z, gdouble scale, struct context* ctx)
{
ctx->zoom = ctx->zoom_initial * scale;
gtk_widget_queue_draw(GTK_WIDGET(ctx->drawing_area));
}
void mouse_scroll(GtkEventControllerScroll* self, double dx, double dy, struct context* ctx)
{
if (dy > 0) {
ctx->zoom /= 1.1;
} else if (dy < 0) {
ctx->zoom *= 1.1;
}
gtk_widget_queue_draw(GTK_WIDGET(ctx->drawing_area));
}
GtkWidget* control_widget(struct context* ctx) {
GtkWidget* frame = gtk_frame_new("Controls");
GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_frame_set_child(GTK_FRAME(frame), box);
gtk_box_append(GTK_BOX(box), gtk_label_new("Preset:"));
const char* presets[] = {"2 Bodies", "3 Bodies", "Solar", "Saturn", NULL};
GtkWidget* preset_selector = gtk_drop_down_new_from_strings(presets);
g_signal_connect(preset_selector, "state-flags-changed", G_CALLBACK(preset_changed), ctx);
gtk_box_append(GTK_BOX(box), preset_selector);
const char* methods[] = {"Euler", "Verlet", NULL};
gtk_box_append(GTK_BOX(box), gtk_label_new("Method:"));
GtkWidget* method_selector = ctx->method_selector = gtk_drop_down_new_from_strings(methods);
g_signal_connect(method_selector, "state-flags-changed", G_CALLBACK(method_changed), ctx);
gtk_box_append(GTK_BOX(box), method_selector);
gtk_box_append(GTK_BOX(box), gtk_label_new("Input:"));
GtkWidget* entry = gtk_entry_new();
g_signal_connect(entry, "activate", G_CALLBACK(input_file_changed), ctx);
GtkEntryBuffer* buffer = ctx->input_file_entry = gtk_entry_get_buffer(GTK_ENTRY(entry));
gtk_entry_buffer_set_text(buffer, ctx->input_file, strlen(ctx->input_file));
gtk_box_append(GTK_BOX(box), entry);
gtk_box_append(GTK_BOX(box), gtk_label_new("dt:"));
GtkWidget* dt = ctx->dt_selector = gtk_spin_button_new_with_range(1e-14, 0.1, 0.00001);
gtk_spin_button_set_digits(GTK_SPIN_BUTTON(dt), 8);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(dt), ctx->dt);
g_signal_connect(dt, "value_changed", G_CALLBACK(dt_changed), ctx);
gtk_box_append(GTK_BOX(box), dt);
return frame;
}
GtkWidget* info_widget(struct context* ctx) {
GtkWidget* frame = gtk_frame_new("Info");
GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_frame_set_child(GTK_FRAME(frame), box);
const char* strings[] = { NULL };
GtkWidget* body_selector = ctx->body_selector = gtk_drop_down_new_from_strings(strings);
gtk_drop_down_set_selected(GTK_DROP_DOWN(body_selector), 0);
g_signal_connect(body_selector, "state-flags-changed", G_CALLBACK(active_changed), ctx);
gtk_box_append(GTK_BOX(box), body_selector);
for (int i = 0; i < 3; i++) {
GtkWidget* x = gtk_label_new("-");
gtk_box_append(GTK_BOX(box), x);
ctx->r[i] = GTK_LABEL(x);
gtk_label_set_width_chars(ctx->r[i], 30);
gtk_label_set_use_markup(ctx->r[i], TRUE);
}
for (int i = 0; i < 3; i++) {
GtkWidget* vx = gtk_label_new("-");
gtk_box_append(GTK_BOX(box), vx);
ctx->v[i] = GTK_LABEL(vx);
gtk_label_set_width_chars(ctx->v[i], 30);
gtk_label_set_use_markup(ctx->v[i], TRUE);
}
return frame;
}
GtkWidget* right_pane(struct context* ctx) {
GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_append(GTK_BOX(box), control_widget(ctx));
gtk_box_append(GTK_BOX(box), info_widget(ctx));
gtk_widget_set_halign(box, GTK_ALIGN_END);
gtk_widget_set_valign(box, GTK_ALIGN_START);
return box;
}
void activate(GtkApplication* app, gpointer user_data)
{
struct context* ctx = user_data;
GtkCssProvider* css_provider = gtk_css_provider_new();
gtk_css_provider_load_from_path(css_provider, "style.css");
gtk_style_context_add_provider_for_display(gdk_display_get_default(), GTK_STYLE_PROVIDER(css_provider), 0);
GtkWidget* window = gtk_application_window_new(app);
gtk_window_set_title (GTK_WINDOW (window), "N-Body");
gtk_window_set_default_size(GTK_WINDOW(window), 1024, 768);
GtkWidget* drawing_area = gtk_drawing_area_new();
gtk_widget_set_vexpand(drawing_area, TRUE);
gtk_widget_set_hexpand(drawing_area, TRUE);
GtkWidget* overlay = gtk_overlay_new();
gtk_window_set_child(GTK_WINDOW(window), overlay);
gtk_overlay_set_child(GTK_OVERLAY(overlay), drawing_area);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), right_pane(ctx));
gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(drawing_area), draw, ctx, NULL);
GtkEventController* motion = gtk_event_controller_motion_new();
g_signal_connect(motion, "motion", G_CALLBACK(motion_notify), ctx);
gtk_event_controller_set_propagation_phase(motion, GTK_PHASE_CAPTURE);
gtk_widget_add_controller(drawing_area, motion);
GtkGesture* gclick = gtk_gesture_click_new();
g_signal_connect(gclick, "pressed", G_CALLBACK(button_press), ctx);
gtk_event_controller_set_propagation_phase(
GTK_EVENT_CONTROLLER(gclick), GTK_PHASE_CAPTURE);
gtk_widget_add_controller(drawing_area, GTK_EVENT_CONTROLLER(gclick));
GtkEventController* scroll = gtk_event_controller_scroll_new(
GTK_EVENT_CONTROLLER_SCROLL_VERTICAL);
g_signal_connect(scroll, "scroll", G_CALLBACK(mouse_scroll), ctx);
gtk_event_controller_set_propagation_phase(scroll, GTK_PHASE_CAPTURE);
gtk_widget_add_controller(drawing_area, scroll);
GtkGesture* zoom = gtk_gesture_zoom_new();
gtk_event_controller_set_propagation_phase(
GTK_EVENT_CONTROLLER(zoom), GTK_PHASE_CAPTURE);
gtk_widget_add_controller(drawing_area, GTK_EVENT_CONTROLLER(zoom));
g_signal_connect(zoom, "begin", G_CALLBACK(zoom_begin), ctx);
g_signal_connect(zoom, "scale-changed", G_CALLBACK(zoom_scale_changed), ctx);
g_signal_connect(window, "destroy", G_CALLBACK(close_window), ctx);
ctx->drawing_area = drawing_area;
ctx->timer_id = g_timeout_add(16, (GSourceFunc)timeout, ctx);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc, char **argv)
{
struct preset presets[] = {
{"2 Bodies", "2bodies.txt", 1, 0.00005},
{"3 Bodies", "3bodies.txt", 1, 0.00001},
{"Solar", "solar.txt", 1, 0.005},
{"Saturn", "saturn.txt", 1, 0.00001}
};
struct context ctx;
GtkApplication* app;
int status;
memset(&ctx, 0, sizeof(ctx));
ctx.zoom = ctx.zoom_initial = 0.1;
ctx.active_preset = -1;
ctx.method = -1;
strncpy(ctx.input_file, "2bodies.txt", sizeof(ctx.input_file));
ctx.dt = 1e-5;
ctx.presets = presets;
gtk_disable_setlocale();
app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activate), &ctx);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}