/
read_parameters.c
executable file
·340 lines (298 loc) · 12.6 KB
/
read_parameters.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
#include "dec.h"
#include "cosmic_rays.h"
#include "runge_kutta.h"
#include "read_parameters.h"
#include "synchrotron.h"
#include "jet.h"
/*************************************************************************/
float get_int_parameter(struct int_parameter param, FILE *f)
{
char line[100];
char string_value[100];
int string_equal = *"=";
int string_garden = *"#";
int ii = 0;
char *start;
char *stop;
/*Check for comment lines starting with '#'*/
stop = line;
while (stop - line == 0)
{
fgets(line, sizeof(line), f);
start = strchr(line, string_equal);
stop = strchr(line, string_garden);
if (ii > 10)
{
printf ("Error while reading comment lines in parameter file. Stop.\n");
exit (0);//something wrong
}
ii++;
}
strncpy(string_value, start + 1, stop - start);
strncpy(param.string_file, line, start - line - 1);
param.value = atof (string_value);
/* #if DEBUG == 2 */
/* printf("string_file = %s\n", param.string_file); */
/* printf("param.value = %g\n", param.value); */
/* printf("string start = %s stop = %s\n", start, stop); */
/* printf("string stop - start %d\n", stop - start); */
/* // printf ("c_light = %s", value); */
/* exit (0); */
/* #endif */
// }
#if DEBUG == 1
printf("%s = %d\n", param.string_search, param.value);
#endif
/* if (strstr(param.string_search, param.string_file) == NULL) */
/* { */
/* printf("Error while reading parameter %s.\n", */
/* param.string_search); */
/* printf("Tried to read %s in parameter file.\n", param.string_file); */
/* printf("Searched for %s\n", param.string_search); */
/* printf("Check, whether the string in the parameter file */
/* is correct!\n"); */
/* exit(0); */
/* } */
// fgets(param.string_file, sizeof(param.string_file), f);
// sscanf(param.string_file, "%g", ¶m.value);
return param.value;
}
/*************************************************************************/
float get_float_parameter(struct float_parameter param, FILE *f)
{
char line[100];
char string_value[100];
int string_equal = *"=";
int string_garden = *"#";
int ii = 0;
char *start;
char *stop;
/*Check for comment lines starting with '#'*/
stop = line;
while (stop - line == 0)
{
fgets(line, sizeof(line), f);
start = strchr(line, string_equal);
stop = strchr(line, string_garden);
if (ii > 10)
{
printf ("Error while reading comment lines in parameter file. Stop.\n");
exit (0);//something wrong
}
ii++;
}
strncpy(string_value, start + 1, stop - start);
strncpy(param.string_file, line, start - line);
param.value = atof (string_value);
/* #if DEBUG == 1 */
/* printf("ii = %d", ii); */
/* printf("string_file = %s\n", param.string_file); */
/* printf("param.value = %g\n", param.value); */
/* printf("string start = %s stop = %s\n", start, stop); */
/* printf("string stop - start %d\n", stop - start); */
/* printf("param.string_search = %s \n", param.string_search); */
/* printf("param.string_file = %s \n", param.string_file); */
/* exit(0); */
/* // printf ("c_light = %s", value); */
/* #endif */
// }
/* if (strstr(param.string_search, line) == NULL) */
/* { */
/* printf("Error while reading parameter %s.\n", */
/* param.string_search); */
/* printf("Tried to read %s in parameter file.\n", param.string_file); */
/* printf("Searched for %s\n", param.string_search); */
/* printf("output string search = %d\n", */
/* strstr(param.string_search, param.string_file)); */
/* printf("Check, whether the string in the parameter file */
/* is correct!\n"); */
/* exit(0); */
/* } */
// fgets(param.string_file, sizeof(param.string_file), f);
// sscanf(param.string_file, "%g", ¶m.value);
#if DEBUG == 1
printf("%s = %g\n", param.string_search, param.value);
#endif
return param.value;
}
/**************************************************************************/
/* Read parameters from parameter file */
void read_parameters(void)
{
FILE *f;
/* This stuff is no needed. However, it doesn't work without on my machine!*/
/*-------------------------------------------------------------------------*/
char line[100];
char dummy[100];
/*-------------------------------------------------------------------------*/
struct int_parameter parami;
struct float_parameter paramf;
printf("Input parameter file = '%s'\n", parameter_file_name);
/* The old version with a static parameter file name
f=fopen("./parameters", "r"); */
f=fopen(parameter_file_name, "r");
if (f == NULL)
{
printf("Cannot open parameter file. Stop.\n");
exit(0);
}
/* Include some fundamental constants */
c_light = 2.99792458e10;//[cm s^-1]
kpc = 3.085677582e21;//[cm]
pi = 3.141565492;
sigma_t = 6.6524616e-25;//[cm^2]
m_electron = 9.1093897e-28;//[g]
e_elem = 1.60217733e-19;
/*--------------------------------------------------------------------------*/
/*Setup of the 2-dimensional grid*/
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "grid_size");
grid_size = get_int_parameter (parami, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "nu_channel");
nu_channel = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "grid_delta");
grid_delta = get_int_parameter (parami, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "z_halo");
z_halo_kpc = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "first_data_point_at_0kpc");
first_data_point_at_0kpc = get_int_parameter (parami, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "normalize_intensities");
normalize_intensities = get_int_parameter (parami, f);
/*--------------------------------------------------------------------------*/
/*Output */
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "nu_1");
nu_1 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "nu_2");
nu_2 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "nu_3");
nu_3 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "nu_4");
nu_4 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "mode");
mode = get_int_parameter (parami, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "epsilon");
epsilon = get_int_parameter (parami, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "FWHM_effective_beam");
FWHM_effective_beam = get_float_parameter (paramf, f);
/*--------------------------------------------------------------------------*/
/*Setup of the advection and diffusion model*/
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "gamma_in");
gamma_in = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "rad_field");
rad_field = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "V0");
V0 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "velocity_field");
velocity_field = get_int_parameter (parami, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "h_V");
h_V = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "adiabatic_losses");
adiabatic_losses = get_int_parameter(parami, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "D0");
D0 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "mu_diff");
mu_diff = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "V_rot");
V_rot = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
/*Magnetic field setup*/
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "galaxy_mode");
galaxy_mode = get_int_parameter(parami, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "z1");
z1 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "B0");
B0 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "B1");
B1 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "h_B1");
h_B1 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "h_B2");
h_B2 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "beta");
beta = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "R0");
R0 = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
/*#Use a magnetic field model (needs edit of the source files)*/
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "model");
model = get_int_parameter(parami, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "initialze_model");
initialize_model = get_int_parameter(parami, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "model_north");
model_north = get_int_parameter(parami, f);
/*--------------------------------------------------------------------------*/
strcpy(parami.string_search, "update_model");
update_model = get_int_parameter(parami, f);
/*--------------------------------------------------------------------------*/
strcpy(paramf.string_search, "xi");
xi = get_float_parameter(paramf, f);
/*--------------------------------------------------------------------------*/
/****************************************************************************/
z_halo = z_halo_kpc * kpc;
if (nu_channel < 2)
{
printf("You need at least to separate frequencies to compute\n");
printf("the spectral index: nu_channel has to be >= 2! Stop.\n");
exit (0);
}
if (nu_2 <= nu_1)
{
printf ("Error: nu_2 <= nu_1. Stop.\n");
exit(0);
}
/* For diffusion the velocity field should be set to zero
so that the CRe number density is calculated correctly */
if (mode != 1)
velocity_field = 0;
if (model == 1 || initialize_model == 1)
{
if (model_north == 1)
number_of_data_points = set_radius_north ();
else
number_of_data_points = set_radius_south ();
if (update_model == 1)
read_intensity_model ();
if (mod[number_of_data_points].z > z_halo_kpc)
{
printf("Halo size is smaller than model size. Stop.\n");
printf("Halo_size = %g kpc, Model size = %g kpc\n", z_halo_kpc, mod[number_of_data_points].z);
exit(0);
}
}
grid_size = (int) 2.0 * grid_size;
R0 = R0 * kpc;
h_grav = 4.0 * R0 / kpc;
fclose(f);
}