-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsortlambda.c
542 lines (442 loc) · 17.1 KB
/
sortlambda.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
/* ------- file: -------------------------- sortlambda.c ------------
Version: rh2.0
Author: Han Uitenbroek ([email protected])
Last modified: Wed Apr 22 09:02:18 2009 --
-------------------------- ----------RH-- */
/* --- Sorts wavelengths and determines what transitions are active at
which wavelength. -- -------------- */
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "rh.h"
#include "atom.h"
#include "atmos.h"
#include "spectrum.h"
#include "background.h"
#include "constant.h"
#include "inputs.h"
#include "error.h"
#include "statistics.h"
#include "xdr.h"
/* --- Function prototypes -- -------------- */
/* --- Global variables -- -------------- */
extern Atmosphere atmos;
extern Spectrum spectrum;
extern InputData input;
extern CommandLine commandline;
extern char messageStr[];
/* ------- begin -------------------------- SortLambda.c ------------ */
void SortLambda()
{
const char routineName[] = "SortLambda";
register int kr, n, m, nspect, la, nact;
bool_t hunt, unique, result;
int Nred, Nspectrum, Nlambda_original, Z, i, j, Nwave;
double *alpha_original, gbf_0, n_eff, *wavetable;
ActiveSet *as;
Atom *atom;
Molecule *molecule;
AtomicLine *line;
AtomicContinuum *continuum;
MolecularLine *mrt;
FILE *fp_wavetable;
XDR xdrs;
getCPU(2, TIME_START, NULL);
/* --- First read the wavelength table if specified -- ------------ */
result = TRUE;
if (strcmp(input.wavetable_input, "none")) {
if (input.wavetable == NULL) {
if ((fp_wavetable = fopen(input.wavetable_input, "r")) == NULL) {
sprintf(messageStr, "Unable to open input file %s",
input.wavetable_input);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
xdrstdio_create(&xdrs, fp_wavetable, XDR_DECODE);
result &= xdr_int(&xdrs, &Nwave);
wavetable = (double *) malloc(Nwave * sizeof(double));
result &= xdr_vector(&xdrs, (char *) wavetable, Nwave,
sizeof(double), (xdrproc_t) xdr_double);
if (!result) {
sprintf(messageStr, "Unable to read from input file %s",
input.wavetable_input);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
xdr_destroy(&xdrs);
fclose(fp_wavetable);
input.wavetable = wavetable;
input.Nxwave = Nwave;
} else {
wavetable = input.wavetable;
Nwave = input.Nxwave;
}
} else
Nwave = 0;
/* --- Add reference wavelength if necessary -- -------------- */
Nspectrum = (atmos.lambda_ref > 0.0) ? Nwave + 1 : Nwave;
/* --- Go through the active atoms to collect number of wavelengths
for the lines and continua treated in detail -- ------------ */
atmos.Nactiveatom = 0;
for (n = 0; n < atmos.Natom; n++) {
atom = &atmos.atoms[n];
if (atom->active) {
for (kr = 0; kr < atom->Ncont; kr++)
Nspectrum += atom->continuum[kr].Nlambda;
for (kr = 0; kr < atom->Nline; kr++)
Nspectrum += atom->line[kr].Nlambda;
atom->activeindex = atmos.Nactiveatom;
atmos.Nactiveatom++;
}
}
/* --- Store the pointers to the active atoms, so that they can be
enumerated -- -------------- */
if (atmos.Nactiveatom > 0) {
atmos.activeatoms = (Atom **) malloc(atmos.Nactiveatom *
sizeof(Atom *));
for (n = 0; n < atmos.Natom; n++) {
atom = &atmos.atoms[n];
if (atom->active)
atmos.activeatoms[atom->activeindex] = atom;
}
} else
atmos.activeatoms = NULL;
/* --- Add number of wavelengths for lines in active molecules -- - */
atmos.Nactivemol = 0;
for (n = 0; n < atmos.Nmolecule; n++) {
molecule = &atmos.molecules[n];
if (molecule->active) {
for (kr = 0; kr < molecule->Nrt; kr++)
Nspectrum += molecule->mrt[kr].Nlambda;
molecule->activeindex = atmos.Nactivemol;
atmos.Nactivemol++;
}
}
/* --- Store the pointers to the active molecules, so that they can be
enumerated -- -------------- */
if (atmos.Nactivemol > 0) {
atmos.activemols = (Molecule **) malloc(atmos.Nactivemol *
sizeof(Molecule *));
for (n = 0; n < atmos.Nmolecule; n++) {
molecule = &atmos.molecules[n];
if (molecule->active)
atmos.activemols[molecule->activeindex] = molecule;
}
} else
atmos.activemols = NULL;
/* --- Fill the wavelength array -- -------------- */
nspect = 0;
spectrum.lambda = (double *) malloc(Nspectrum * sizeof(double));
/* --- First the referenece wavelength if specified -- ------------ */
if (atmos.lambda_ref > 0.0)
spectrum.lambda[nspect++] = atmos.lambda_ref;
/* --- Then the wavelength table -- -------------- */
for (kr = 0; kr < Nwave; kr++)
spectrum.lambda[nspect++] = wavetable[kr];
/* --- Finally, all the detailed radiative transitions -- --------- */
atmos.NPRDactive = 0;
for (nact = 0; nact < atmos.Nactiveatom; nact++) {
atom = atmos.activeatoms[nact];
for (kr = 0; kr < atom->Ncont; kr++) {
continuum = &atom->continuum[kr];
for (la = 0; la < continuum->Nlambda; la++)
spectrum.lambda[nspect++] = continuum->lambda[la];
}
for (kr = 0; kr < atom->Nline; kr++) {
line = &atom->line[kr];
for (la = 0; la < line->Nlambda; la++)
spectrum.lambda[nspect++] = line->lambda[la];
if (line->PRD) atmos.NPRDactive++;
}
}
/* --- Active molecular lines -- -------------- */
for (nact = 0; nact < atmos.Nactivemol; nact++) {
molecule = atmos.activemols[nact];
for (kr = 0; kr < molecule->Nrt; kr++) {
mrt = &molecule->mrt[kr];
for (la = 0; la < mrt->Nlambda; la++)
spectrum.lambda[nspect++] = mrt->lambda[la];
}
}
/* --- Sort the wavelengths in ascending order -- -------------- */
qsort(spectrum.lambda, Nspectrum, sizeof(double), qsascend);
/* --- Check for duplicate wavelengths -- -------------- */
spectrum.Nspect = 1;
for (nspect = 1; nspect < Nspectrum; nspect++) {
if (spectrum.lambda[nspect] > spectrum.lambda[nspect-1]) {
spectrum.lambda[spectrum.Nspect] = spectrum.lambda[nspect];
spectrum.Nspect++;
}
}
sprintf(messageStr, "\n %s: Found %d unique wavelengths\n",
routineName, spectrum.Nspect);
Error(MESSAGE, routineName, messageStr);
if (spectrum.Nspect < Nspectrum) {
sprintf(messageStr, " %s: Eliminated %d duplicate wavelengths\n\n",
routineName, Nspectrum - spectrum.Nspect);
Error(MESSAGE, routineName, messageStr);
}
/* --- Allocate space for wavelength array and active sets -- ----- */
spectrum.lambda = (double *) realloc(spectrum.lambda,
spectrum.Nspect*sizeof(double));
spectrum.as = (ActiveSet *) malloc(spectrum.Nspect * sizeof(ActiveSet));
/* --- Go through each established wavelength and gather active
transitions -- -------------- */
for (nspect = 0; nspect < spectrum.Nspect; nspect++) {
as = &spectrum.as[nspect];
/* --- as->art and as->mrt store the arrays of active
transitions for each active atom
and molecule at this wavelength seperately -- ------------ */
if (atmos.Nactiveatom > 0) {
as->Nactiveatomrt =
(int *) malloc(atmos.Nactiveatom * sizeof(int));
as->art = (AtomicTransition **)
malloc(atmos.Nactiveatom * sizeof(AtomicTransition *));
for (nact = 0; nact < atmos.Nactiveatom; nact++) {
as->Nactiveatomrt[nact] = 0;
as->art[nact] = (AtomicTransition *)
malloc(N_MAX_OVERLAP * sizeof(AtomicTransition));
}
} else {
as->Nactiveatomrt = NULL;
as->art = NULL;
}
if (atmos.Nactivemol > 0) {
as->Nactivemolrt = (int *)
malloc(atmos.Nactivemol * sizeof(int));
as->mrt = (MolTransition **)
malloc(atmos.Nactivemol * sizeof(MolTransition *));
for (nact = 0; nact < atmos.Nactivemol; nact++) {
as->Nactivemolrt[nact] = 0;
as->mrt[nact] = (MolTransition *)
malloc(N_MAX_OVERLAP * sizeof(MolTransition));
}
} else {
as->Nactivemolrt = NULL;
as->mrt = NULL;
}
}
/* --- Determine what transitions are active at which
wavelengths and store the pointers to those transitions. - - */
for (nact = 0; nact < atmos.Nactiveatom; nact++) {
atom = atmos.activeatoms[nact];
/*--- Go through the continua first -- -------------- */
Nred = 0;
for (kr = 0; kr < atom->Ncont; kr++) {
continuum = &atom->continuum[kr];
/* --- Store the original wavelength array size -- ------------ */
Nlambda_original = continuum->Nlambda;
/* --- Find the indices of the lowest (Nblue) and highest (Nred)
wavelength of the current transition -- ------------ */
Hunt(spectrum.Nspect, spectrum.lambda,
continuum->lambda[0], &continuum->Nblue);
Hunt(spectrum.Nspect, spectrum.lambda,
continuum->lambda[continuum->Nlambda-1], &Nred);
continuum->Nlambda = Nred - continuum->Nblue + 1;
/* --- Store the pointer to the current transition in the
active set (as) at each wavelength covered by the current
transition. Calculate wavelength integration weights - - */
for (nspect = continuum->Nblue; nspect <= Nred; nspect++) {
as = &spectrum.as[nspect];
nact = atom->activeindex;
as->art[nact][as->Nactiveatomrt[nact]].type = ATOMIC_CONTINUUM;
as->art[nact][as->Nactiveatomrt[nact]].ptype.continuum = continuum;
as->Nactiveatomrt[nact]++;
if (as->Nactiveatomrt[nact] == N_MAX_OVERLAP) {
sprintf(messageStr,
"\n Too many overlapping transitions (> %d) "
"for atom %s and nspect = %d\n",
as->Nactiveatomrt[nact], atom->ID, nspect);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
}
/* --- In case of Bound-Free transition compute absorption
cross-section if wavelength dependence is hydrogenic,
interpolate if wavelength dependence is given
explicitly -- -------------- */
if (continuum->hydrogenic) {
free(continuum->lambda);
continuum->lambda = spectrum.lambda + continuum->Nblue;
continuum->alpha =
(double *) realloc(continuum->alpha,
continuum->Nlambda*sizeof(double));
Z = atom->stage[continuum->j];
n_eff = Z * sqrt(E_RYDBERG /
(atom->E[continuum->j] - atom->E[continuum->i]));
gbf_0 = Gaunt_bf(continuum->lambda0, n_eff, Z);
for (la = 0; la < continuum->Nlambda; la++) {
continuum->alpha[la] = continuum->alpha0 *
Gaunt_bf(continuum->lambda[la], n_eff, Z) / gbf_0 *
CUBE(continuum->lambda[la]/continuum->lambda0);
}
} else {
alpha_original = continuum->alpha;
splineCoef(Nlambda_original, continuum->lambda, alpha_original);
continuum->alpha =
(double *) malloc(continuum->Nlambda * sizeof(double));
splineEval(continuum->Nlambda, spectrum.lambda + continuum->Nblue,
continuum->alpha, hunt=TRUE);
free(continuum->lambda);
continuum->lambda = spectrum.lambda + continuum->Nblue;
free(alpha_original);
}
}
/* --- Then go through the bound-bound transitions -- ----------- */
for (kr = 0; kr < atom->Nline; kr++) {
line = &atom->line[kr];
/* --- Store the original wavelength array size -- ----------- */
Nlambda_original = line->Nlambda;
/* --- Find the indices of the lowest (Nblue) and highest (Nred)
wavelength of the current transition -- ----------- */
Hunt(spectrum.Nspect, spectrum.lambda, line->lambda[0],
&line->Nblue);
Hunt(spectrum.Nspect, spectrum.lambda,
line->lambda[line->Nlambda-1], &Nred);
line->Nlambda = Nred - line->Nblue + 1;
/* --- Store the pointer to the current transition in the
active set (as) at each wavelength covered by the current
transition. Calculate wavelength integration weights - - */
for (nspect = line->Nblue; nspect <= Nred; nspect++) {
as = &spectrum.as[nspect];
nact = atom->activeindex;
as->art[nact][as->Nactiveatomrt[nact]].type = ATOMIC_LINE;
as->art[nact][as->Nactiveatomrt[nact]].ptype.line = line;
as->Nactiveatomrt[nact]++;
if (as->Nactiveatomrt[nact] == N_MAX_OVERLAP) {
sprintf(messageStr,
"\n Too many overlapping transitions (> %d) "
"for atom %s and nspect = %d\n",
as->Nactiveatomrt[nact], atom->ID, nspect);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
}
free(line->lambda);
line->lambda = spectrum.lambda + line->Nblue;
}
}
for (nact = 0; nact < atmos.Nactivemol; nact++) {
molecule = atmos.activemols[nact];
Nred = 0;
for (kr = 0; kr < molecule->Nrt; kr++) {
mrt = &molecule->mrt[kr];
/* --- Find the indices of the lowest (Nblue) and highest
wavelength of the current transition and allocate memory
for the wavelength integration weights -- -------------- */
Hunt(spectrum.Nspect, spectrum.lambda,
mrt->lambda[0], &mrt->Nblue);
Hunt(spectrum.Nspect, spectrum.lambda,
mrt->lambda[mrt->Nlambda-1], &Nred);
mrt->Nlambda = Nred - mrt->Nblue + 1;
/* --- Repoint to proper position in wavelength array -- ------ */
free(mrt->lambda);
mrt->lambda = spectrum.lambda + mrt->Nblue;
/* --- Store the transition number of the current transition
in the active molecular set (as) at each wavelength
covered by the current transition. Calculate wavelength
integration weights -- ------------ */
for (nspect = mrt->Nblue;
nspect < mrt->Nblue+mrt->Nlambda; nspect++) {
as = &spectrum.as[nspect];
nact = molecule->activeindex;
as->mrt[nact][as->Nactivemolrt[nact]].type = mrt->type;
as->mrt[nact][as->Nactivemolrt[nact]].ptype.vrline = mrt;
as->Nactivemolrt[nact]++;
if (as->Nactivemolrt[nact] == N_MAX_OVERLAP) {
sprintf(messageStr,
"\n Too many overlapping transitions (> %d) "
"for molecule %s and nspect = %d\n",
as->Nactivemolrt[nact], molecule->ID, nspect);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
}
}
}
for (nspect = 0; nspect < spectrum.Nspect; nspect++) {
as = &spectrum.as[nspect];
/* --- For each wavelength in the spectrum gather the unique set of
lower and upper levels involved in active atomic transitions at that
wavelength. These are needed in the calculation of the cross
coupling coefficients in the approximate lambda iteration -- */
as->Nlower = (int *) malloc(atmos.Nactiveatom * sizeof(int));
as->Nupper = (int *) malloc(atmos.Nactiveatom * sizeof(int));
as->upper_levels =
(int **) malloc(atmos.Nactiveatom * sizeof(int *));
as->lower_levels =
(int **) malloc(atmos.Nactiveatom * sizeof(int *));
for (nact = 0; nact < atmos.Nactiveatom; nact++) {
atom = atmos.activeatoms[nact];
/* --- First, for each wavelength in the spectrum gather
the unique set of lower levels of active transitions
within each atomic model -- -------------- */
as->Nlower[nact] = 0;
as->Nupper[nact] = 0;
if (as->Nactiveatomrt[nact] > 0) {
as->lower_levels[nact] =
(int *) malloc(as->Nactiveatomrt[nact] * sizeof(int));
as->upper_levels[nact] =
(int *) malloc(as->Nactiveatomrt[nact] * sizeof(int));
} else {
as->lower_levels[nact] = NULL;
as->upper_levels[nact] = NULL;
}
for (n = 0; n < as->Nactiveatomrt[nact]; n++) {
unique = TRUE;
switch (as->art[nact][n].type) {
case ATOMIC_LINE:
i = as->art[nact][n].ptype.line->i;
j = as->art[nact][n].ptype.line->j;
break;
case ATOMIC_CONTINUUM:
i = as->art[nact][n].ptype.continuum->i;
j = as->art[nact][n].ptype.continuum->j;
break;
default:;
}
for (m = 0; m < as->Nlower[nact]; m++) {
if (i == as->lower_levels[nact][m]) {
unique = FALSE;
break;
}
}
if (unique) {
as->lower_levels[nact][as->Nlower[nact]] = i;
as->Nlower[nact]++;
}
/* --- Then add the upper level if unique -- -------------- */
unique = TRUE;
for (m = 0; m < as->Nupper[nact]; m++) {
if (j == as->upper_levels[nact][m]) {
unique = FALSE;
break;
}
}
if (unique) {
as->upper_levels[nact][as->Nupper[nact]] = j;
as->Nupper[nact]++;
}
}
}
/* --- Reallocate space for the atomic transition arrays -- ------- */
for (nact = 0; nact < atmos.Nactiveatom; nact++) {
if (as->Nactiveatomrt[nact] > 0) {
as->art[nact] = (AtomicTransition *)
realloc(as->art[nact],
as->Nactiveatomrt[nact] * sizeof(AtomicTransition));
as->lower_levels[nact] =
(int *) realloc(as->lower_levels[nact],
as->Nlower[nact] * sizeof(int));
as->upper_levels[nact] =
(int *) realloc(as->upper_levels[nact],
as->Nupper[nact] * sizeof(int));
}
}
/* --- Reallocate space for the molecular transition arrays -- -- */
for (nact = 0; nact < atmos.Nactivemol; nact++) {
if (as->Nactivemolrt[nact] > 0) {
as->mrt[nact] = (MolTransition *)
realloc(as->mrt[nact],
as->Nactivemolrt[nact] * sizeof(MolTransition));
}
}
}
getCPU(2, TIME_POLL, "SortLambda");
}
/* ------- end ---------------------------- SortLambda.c ------------ */