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
|
/* Headers. */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include "microui.h"
#include "renderer.h"
#include "unit.h"
#include "compressor.h"
#include "widget.h"
#include "engine.h"
#include "ui.h"
#include "color.h"
#include "eprintf.h"
/* Macros. */
#define nelem(arr) (sizeof(arr)/sizeof(arr[0]))
/* Constants. */
static const char TITLE[] = "volute";
enum window {
WIN_OPTS = MU_OPT_NOINTERACT | MU_OPT_NOTITLE | MU_OPT_AUTOSIZE | MU_OPT_NOFRAME,
};
static const mu_Color COLOR_TEXT = BLACK;
static const mu_Color COLOR_BORDER = BLACK;
static const mu_Color COLOR_WINDOWBG = WHITE;
static const mu_Color COLOR_TITLEBG = LIGHT_GRAY;
static const mu_Color COLOR_TITLETEXT = COLOR_TEXT;
static const mu_Color COLOR_PANELBG = COLOR_WINDOWBG;
static const mu_Color COLOR_BUTTON = WHITE;
static const mu_Color COLOR_BUTTONHOVER = LIGHT_GRAY;
static const mu_Color COLOR_BUTTONFOCUS = DARK_GRAY;
static const mu_Color COLOR_BASE = WHITE;
static const mu_Color COLOR_BASEHOVER = COLOR_BASE;
static const mu_Color COLOR_BASEFOCUS = COLOR_BASE;
static const mu_Color COLOR_SCROLLBASE = WHITE;
static const mu_Color COLOR_SCROLLTHUMB = WHITE;
/* Function declarations. */
static void set_style(mu_Context *ctx);
static void main_loop(mu_Context *ctx, UI *ui);
static void process_frame(mu_Context *ctx, UI *ui);
static void main_window(mu_Context *ctx, UI *ui);
static void displacement_row(mu_Context *ctx, UI *ui);
static void ambient_temperature_row(mu_Context *ctx, UI *ui);
static void ambient_pressure_row(mu_Context *ctx, UI *ui);
static void global_input_row(mu_Context *ctx, UI *ui, const char *label, w_Field *input, void (*callback)(UI *ui), w_Select *unit, void (*unit_callback)(UI *ui));
static void rpm_row(mu_Context *ctx, UI *ui);
static void map_row(mu_Context *ctx, UI *ui);
static void ve_row(mu_Context *ctx, UI *ui);
static void comp_efficiency_row(mu_Context *ctx, UI *ui);
static void intercooler_efficiency_row(mu_Context *ctx, UI *ui);
static void intercooler_deltap_row(mu_Context *ctx, UI *ui);
static void input_row(mu_Context *ctx, UI *ui, const char *label, w_Select *unit, void (*unit_callback)(UI *ui), w_Field inputs[], void (*callback)(UI *ui, int idx));
static void input_row_static_unit(mu_Context *ctx, UI *ui, const char *label, const char *unit, w_Field inputs[], void (*callback)(UI *ui, int idx));
static void dup_del_row(mu_Context *ctx, UI *ui);
static void pressure_ratio_row(mu_Context *ctx, UI *ui);
static void comp_outlet_temperature_row(mu_Context *ctx, UI *ui);
static void manifold_temperature_row(mu_Context *ctx, UI *ui);
static void volume_flow_rate_row(mu_Context *ctx, UI *ui);
static void mass_flow_rate_row(mu_Context *ctx, UI *ui);
static void mass_flow_rate_corrected_row(mu_Context *ctx, UI *ui);
static void comp_select(mu_Context *ctx, UI *ui);
static void comp_img(mu_Context *ctx, UI *ui);
static void scale_preserve_aspect_ratio(mu_Rect *r, int wmax, int hmax);
static void output_row(mu_Context *ctx, UI *ui, const char *label, w_Select *unit, w_Number outputs[]);
static void hpad(mu_Context *ctx, int w);
static void vpad(mu_Context *ctx, int h);
/* Function Definitions. */
int
main(void) {
/* Init microui. */
static mu_Context ctx;
mu_init(&ctx);
r_init(&ctx, TITLE);
set_style(&ctx);
/* Init data structures. */
static UI ui;
if (init_ui(&ui) != 0) {
return 1;
}
main_loop(&ctx, &ui);
free_ui(&ui);
r_free();
return 0;
}
static void
set_style(mu_Context *ctx) {
ctx->style->colors[MU_COLOR_TEXT] = COLOR_TEXT;
ctx->style->colors[MU_COLOR_BORDER] = COLOR_BORDER;
ctx->style->colors[MU_COLOR_WINDOWBG] = COLOR_WINDOWBG;
ctx->style->colors[MU_COLOR_TITLEBG] = COLOR_TITLEBG;
ctx->style->colors[MU_COLOR_TITLETEXT] = COLOR_TITLETEXT;
ctx->style->colors[MU_COLOR_PANELBG] = COLOR_PANELBG;
ctx->style->colors[MU_COLOR_BUTTON] = COLOR_BUTTON;
ctx->style->colors[MU_COLOR_BUTTONHOVER] = COLOR_BUTTONHOVER;
ctx->style->colors[MU_COLOR_BUTTONFOCUS] = COLOR_BUTTONFOCUS;
ctx->style->colors[MU_COLOR_BASE] = COLOR_BASE;
ctx->style->colors[MU_COLOR_BASEHOVER] = COLOR_BASEHOVER;
ctx->style->colors[MU_COLOR_BASEFOCUS] = COLOR_BASEFOCUS;
ctx->style->colors[MU_COLOR_SCROLLBASE] = COLOR_SCROLLBASE;
ctx->style->colors[MU_COLOR_SCROLLTHUMB] = COLOR_SCROLLTHUMB;
}
static void
main_loop(mu_Context *ctx, UI *ui) {
for (;;) {
r_input(ctx);
process_frame(ctx, ui);
r_render(ctx);
}
}
static void
process_frame(mu_Context *ctx, UI *ui) {
mu_begin(ctx);
main_window(ctx, ui);
mu_end(ctx);
}
static void
main_window(mu_Context *ctx, UI *ui) {
int w, h;
r_get_window_size(&w, &h);
if (!mu_begin_window_ex(ctx, TITLE, mu_rect(0, 0, w, h), WIN_OPTS)) {
exit(EXIT_FAILURE);
}
displacement_row(ctx, ui);
ambient_temperature_row(ctx, ui);
ambient_pressure_row(ctx, ui);
vpad(ctx, 0);
rpm_row(ctx, ui);
map_row(ctx, ui);
ve_row(ctx, ui);
comp_efficiency_row(ctx, ui);
intercooler_efficiency_row(ctx, ui);
intercooler_deltap_row(ctx, ui);
dup_del_row(ctx, ui);
vpad(ctx, 0);
pressure_ratio_row(ctx, ui);
comp_outlet_temperature_row(ctx, ui);
manifold_temperature_row(ctx, ui);
volume_flow_rate_row(ctx, ui);
mass_flow_rate_row(ctx, ui);
mass_flow_rate_corrected_row(ctx, ui);
vpad(ctx, 0);
comp_select(ctx, ui);
comp_img(ctx, ui);
mu_end_window(ctx);
}
static void
displacement_row(mu_Context *ctx, UI *ui) {
global_input_row(ctx, ui,
"Displacement:",
&ui->displacement, set_displacement,
&ui->displacement_unit, set_displacement_unit);
}
static void
ambient_temperature_row(mu_Context *ctx, UI *ui) {
global_input_row(ctx, ui,
"Ambient T:",
&ui->ambient_temperature, set_ambient_temperature,
&ui->ambient_temperature_unit, set_ambient_temperature_unit);
}
static void
ambient_pressure_row(mu_Context *ctx, UI *ui) {
global_input_row(ctx, ui,
"Ambient P:",
&ui->ambient_pressure, set_ambient_pressure,
&ui->ambient_pressure_unit, set_ambient_pressure_unit);
}
static void
global_input_row(mu_Context *ctx, UI *ui,
const char *label,
w_Field *input, void (*callback)(UI *ui),
w_Select *unit, void (*unit_callback)(UI *ui)
) {
mu_layout_row(ctx, 3, (int[]) {LABEL_WIDTH, FIELD_WIDTH, UNIT_WIDTH}, 0);
mu_label(ctx, label);
if (w_field(ctx, input) & MU_RES_CHANGE) {
callback(ui);
compute_all(ui);
}
if (w_select(ctx, unit) & MU_RES_CHANGE) {
unit_callback(ui);
}
}
static void
rpm_row(mu_Context *ctx, UI *ui) {
input_row_static_unit(ctx, ui,
"Speed:",
"(rpm)",
ui->rpm, set_rpm);
}
static void
map_row(mu_Context *ctx, UI *ui) {
input_row(ctx, ui,
"Manifold P:",
&ui->map_unit, set_map_unit,
ui->map, set_map);
}
static void
ve_row(mu_Context *ctx, UI *ui) {
input_row_static_unit(ctx, ui,
"Volumetric η:",
"(%)",
ui->ve, set_ve);
}
static void
comp_efficiency_row(mu_Context *ctx, UI *ui) {
input_row_static_unit(ctx, ui,
"Compressor η:",
"(%)",
ui->comp_efficiency, set_comp_efficiency);
}
static void
intercooler_efficiency_row(mu_Context *ctx, UI *ui) {
input_row_static_unit(ctx, ui,
"Intercooler η:",
"(%)",
ui->intercooler_efficiency, set_intercooler_efficiency);
}
static void
intercooler_deltap_row(mu_Context *ctx, UI *ui) {
input_row(ctx, ui,
"Intercooler ΔP:",
&ui->intercooler_deltap_unit, set_intercooler_deltap_unit,
ui->intercooler_deltap, set_intercooler_deltap);
}
static void
input_row(mu_Context *ctx, UI *ui,
const char *label,
w_Select *unit, void (*unit_callback)(UI *ui),
w_Field inputs[], void (*callback)(UI *ui, int idx)
) {
int i;
mu_layout_row(ctx, 0, NULL, 0);
mu_layout_width(ctx, LABEL_WIDTH);
mu_label(ctx, label);
mu_layout_width(ctx, UNIT_WIDTH);
if (w_select(ctx, unit) & MU_RES_CHANGE) {
unit_callback(ui);
}
mu_layout_width(ctx, FIELD_WIDTH);
for (i = 0; i < ui->npoints; i++) {
if (w_field(ctx, &inputs[i])) {
callback(ui, i);
compute(ui, i);
}
}
}
static void
input_row_static_unit(mu_Context *ctx, UI *ui,
const char *label,
const char *unit,
w_Field inputs[], void (*callback)(UI *ui, int idx)
) {
int i;
mu_layout_row(ctx, 0, NULL, 0);
mu_layout_width(ctx, LABEL_WIDTH);
mu_label(ctx, label);
mu_layout_width(ctx, UNIT_WIDTH);
mu_label(ctx, unit);
mu_layout_width(ctx, FIELD_WIDTH);
for (i = 0; i < ui->npoints; i++) {
if (w_field(ctx, &inputs[i])) {
callback(ui, i);
compute(ui, i);
}
}
}
static void
dup_del_row(mu_Context *ctx, UI *ui) {
int i;
mu_layout_row(ctx, 0, NULL, 0);
hpad(ctx, LABEL_WIDTH);
hpad(ctx, UNIT_WIDTH);
mu_layout_width(ctx, (FIELD_WIDTH - ctx->style->spacing)/2);
for (i = 0; i < ui->npoints; i++) {
mu_push_id(ctx, &i, sizeof(i));
if (mu_button(ctx, "Dup")) {
insert_point(ui, i);
}
if (mu_button(ctx, "Del")) {
remove_point(ui, i);
}
mu_pop_id(ctx);
}
}
static void
pressure_ratio_row(mu_Context *ctx, UI *ui) {
int i;
mu_layout_row(ctx, 0, NULL, 0);
mu_layout_width(ctx, LABEL_WIDTH);
mu_label(ctx, "Pressure ratio:");
hpad(ctx, UNIT_WIDTH);
mu_layout_width(ctx, FIELD_WIDTH);
for (i = 0; i < ui->npoints; i++) {
w_number(ctx, ui->pressure_ratio[i]);
}
}
static void
comp_outlet_temperature_row(mu_Context *ctx, UI *ui) {
output_row(ctx, ui,
"Compressor T:",
&ui->comp_outlet_temperature_unit,
ui->comp_outlet_temperature);
}
static void
manifold_temperature_row(mu_Context *ctx, UI *ui) {
output_row(ctx, ui,
"Manifold T:",
&ui->manifold_temperature_unit,
ui->manifold_temperature);
}
static void
volume_flow_rate_row(mu_Context *ctx, UI *ui) {
output_row(ctx, ui,
"Volume flow:",
&ui->volume_flow_rate_unit,
ui->volume_flow_rate);
}
static void
mass_flow_rate_row(mu_Context *ctx, UI *ui) {
output_row(ctx, ui,
"Mass flow:",
&ui->mass_flow_rate_unit,
ui->mass_flow_rate);
}
static void
mass_flow_rate_corrected_row(mu_Context *ctx, UI *ui) {
output_row(ctx, ui,
"Mass flow at STP:",
&ui->mass_flow_rate_corrected_unit,
ui->mass_flow_rate_corrected);
}
static void
comp_select(mu_Context *ctx, UI *ui) {
const Compressor *comp;
if (w_select_compressor(ctx, &ui->comp_select) & MU_RES_CHANGE) {
comp = &ui->comps[ui->comp_select.idx];
if (w_canvas_set_bg(&ui->comp_img, comp->imgfile) != 0) {
weprintf("failed to set compressor image: %s", comp->imgfile);
}
compute_all(ui);
}
}
static void
comp_img(mu_Context *ctx, UI *ui) {
mu_Rect win, area, img;
win.x = win.y = 0;
r_get_window_size(&win.w, &win.h);
mu_layout_row(ctx, 1, &win.w, win.h);
area = mu_layout_next(ctx);
area.w = win.w - area.x - ctx->style->padding;
area.h = win.h - area.y - ctx->style->padding;
img.x = area.x;
img.y = area.y;
r_get_icon_size(ui->comp_img.id, &img.w, &img.h);
scale_preserve_aspect_ratio(&img, area.w, area.h);
mu_layout_set_next(ctx, img, 0);
w_canvas(ctx, &ui->comp_img);
}
static void
scale_preserve_aspect_ratio(mu_Rect *r, int wmax, int hmax) {
double wscale, hscale, scale;
wscale = (double) wmax / (double) r->w;
hscale = (double) hmax / (double) r->h;
scale = mu_min(wscale, hscale);
r->w *= scale;
r->h *= scale;
}
static void
output_row(mu_Context *ctx, UI *ui, const char *label, w_Select *unit, w_Number outputs[]) {
int i;
mu_layout_row(ctx, 0, NULL, 0);
mu_layout_width(ctx, LABEL_WIDTH);
mu_label(ctx, label);
mu_layout_width(ctx, UNIT_WIDTH);
if (w_select(ctx, unit) & MU_RES_CHANGE) {
compute_all(ui);
}
mu_layout_width(ctx, FIELD_WIDTH);
for (i = 0; i < ui->npoints; i++) {
w_number(ctx, outputs[i]);
}
}
static void
hpad(mu_Context *ctx, int w) {
mu_layout_width(ctx, w);
mu_label(ctx, "");
}
static void
vpad(mu_Context *ctx, int h) {
mu_layout_row(ctx, 0, NULL, h);
mu_label(ctx, "");
}
|