forked from AFD-Illinois/igrmonty2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
par.c
66 lines (45 loc) · 1.5 KB
/
par.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
#include "par.h"
// sets default values for elements of params (if desired) and loads
// from par file 'fname'
void load_par (const char *fname, Params *params) {
char line[256];
FILE *fp = fopen(fname, "r");
if (fp == NULL) {
fprintf(stderr, "! unable to load parameter file '%s'. (%d: %s)\n", fname, errno, strerror(errno));
exit(errno);
}
// set default values here
params->TP_OVER_TE = 3.;
// modify parameters/types below
while (fgets(line, 255, fp) != NULL) {
if (line[0] == '#') continue;
read_param(line, "Ns", &(params->Ns), TYPE_DBL);
read_param(line, "MBH", &(params->MBH), TYPE_DBL);
read_param(line, "M_unit", &(params->M_unit), TYPE_DBL);
read_param(line, "TP_OVER_TE", &(params->TP_OVER_TE), TYPE_DBL);
read_param(line, "dump", (void *)(params->dump), TYPE_STR);
read_param(line, "spectrum", (void *)(params->spectrum), TYPE_STR);
}
fclose(fp);
params->loaded = 1;
}
// loads value -> (type *)*val if line corresponds to (key,value)
void read_param (const char *line, const char *key, void *val, int type) {
char word[256], value[256];
sscanf(line, "%s %s", word, value);
if (strcmp(word, key) == 0) {
switch (type) {
case TYPE_INT:
sscanf(value, "%d", (int *)val);
break;
case TYPE_DBL:
sscanf(value, "%lf", (double *)val);
break;
case TYPE_STR:
sscanf(value, "%s", (char *)val);
break;
default:
fprintf(stderr, "! attempt to load unknown type '%d'.\n", type);
}
}
}