-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelprofile.cpp
More file actions
1425 lines (1162 loc) · 46.8 KB
/
Relprofile.cpp
File metadata and controls
1425 lines (1162 loc) · 46.8 KB
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of the RELXILL model code.
RELXILL is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RELXILL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License see
<http://www.gnu.org/licenses/>.
Copyright 2022 Thomas Dauser, Remeis Observatory & ECAP
*/
#include "Relprofile.h"
#include "Relcache.h"
#include "Rellp.h"
extern "C" {
#include "relutility.h"
#include "writeOutfiles.h"
#include "reltable.h"
}
cnode *cache_syspar = nullptr;
/** global parameters, which can be used for several calls of the model */
relTable *ptr_rellineTable = nullptr;
RelSysPar *cached_tab_sysPar = nullptr;
// precision to calculate gstar from [H:1-H] instead of [0:1]
const double GFAC_H = 5e-3;
// interpolate the table in the A-MU0 plane (for one value of radius)
static void interpol_a_mu0(int ii, double ifac_a, double ifac_mu0, int ind_a,
int ind_mu0, RelSysPar *sysPar, relTable *reltab) {
sysPar->gmin[ii] = interp_lin_2d_float(ifac_a, ifac_mu0,
reltab->arr[ind_a][ind_mu0]->gmin[ii],
reltab->arr[ind_a + 1][ind_mu0]->gmin[ii],
reltab->arr[ind_a][ind_mu0 + 1]->gmin[ii],
reltab->arr[ind_a + 1][ind_mu0 + 1]->gmin[ii]);
sysPar->gmax[ii] = interp_lin_2d_float(ifac_a, ifac_mu0,
reltab->arr[ind_a][ind_mu0]->gmax[ii],
reltab->arr[ind_a + 1][ind_mu0]->gmax[ii],
reltab->arr[ind_a][ind_mu0 + 1]->gmax[ii],
reltab->arr[ind_a + 1][ind_mu0 + 1]->gmax[ii]);
int jj;
for (jj = 0; jj < reltab->n_g; jj++) {
sysPar->trff[ii][jj][0] = interp_lin_2d_float(ifac_a, ifac_mu0,
reltab->arr[ind_a][ind_mu0]->trff1[ii][jj],
reltab->arr[ind_a + 1][ind_mu0]->trff1[ii][jj],
reltab->arr[ind_a][ind_mu0 + 1]->trff1[ii][jj],
reltab->arr[ind_a + 1][ind_mu0 + 1]->trff1[ii][jj]);
sysPar->trff[ii][jj][1] = interp_lin_2d_float(ifac_a, ifac_mu0,
reltab->arr[ind_a][ind_mu0]->trff2[ii][jj],
reltab->arr[ind_a + 1][ind_mu0]->trff2[ii][jj],
reltab->arr[ind_a][ind_mu0 + 1]->trff2[ii][jj],
reltab->arr[ind_a + 1][ind_mu0 + 1]->trff2[ii][jj]);
sysPar->cosne[ii][jj][0] = interp_lin_2d_float(ifac_a, ifac_mu0,
reltab->arr[ind_a][ind_mu0]->cosne1[ii][jj],
reltab->arr[ind_a + 1][ind_mu0]->cosne1[ii][jj],
reltab->arr[ind_a][ind_mu0 + 1]->cosne1[ii][jj],
reltab->arr[ind_a + 1][ind_mu0 + 1]->cosne1[ii][jj]);
sysPar->cosne[ii][jj][1] = interp_lin_2d_float(ifac_a, ifac_mu0,
reltab->arr[ind_a][ind_mu0]->cosne2[ii][jj],
reltab->arr[ind_a + 1][ind_mu0]->cosne2[ii][jj],
reltab->arr[ind_a][ind_mu0 + 1]->cosne2[ii][jj],
reltab->arr[ind_a + 1][ind_mu0 + 1]->cosne2[ii][jj]);
}
}
RelSysPar *new_relSysPar(int nr, int ng, int *status) {
auto *sysPar = (RelSysPar *) malloc(sizeof(RelSysPar));
CHECK_MALLOC_RET_STATUS(sysPar, status, nullptr)
sysPar->ng = ng;
sysPar->nr = nr;
sysPar->re = (double *) malloc(nr * sizeof(double));
CHECK_MALLOC_RET_STATUS(sysPar->re, status, sysPar)
sysPar->gmin = (double *) malloc(nr * sizeof(double));
CHECK_MALLOC_RET_STATUS(sysPar->gmin, status, sysPar)
sysPar->gmax = (double *) malloc(nr * sizeof(double));
CHECK_MALLOC_RET_STATUS(sysPar->gmax, status, sysPar)
sysPar->emis = nullptr;
sysPar->gstar = (double *) malloc(ng * sizeof(double));
CHECK_MALLOC_RET_STATUS(sysPar->gstar, status, sysPar)
// we already set the values as they are fixed
int ii;
int jj;
for (ii = 0; ii < ng; ii++) {
sysPar->gstar[ii] = GFAC_H + (1.0 - 2 * GFAC_H) / (ng - 1) * ((float) (ii));
}
sysPar->d_gstar = (double *) malloc(ng * sizeof(double));
CHECK_MALLOC_RET_STATUS(sysPar->gstar, status, sysPar)
for (ii = 0; ii < ng; ii++) {
if ((ii == 0) || (ii == (ng - 1))) {
sysPar->d_gstar[ii] = 0.5 * (sysPar->gstar[1] - sysPar->gstar[0]) + GFAC_H;
} else {
sysPar->d_gstar[ii] = sysPar->gstar[1] - sysPar->gstar[0];
}
}
sysPar->trff = (double ***) malloc(nr * sizeof(double **));
CHECK_MALLOC_RET_STATUS(sysPar->trff, status, sysPar)
sysPar->cosne = (double ***) malloc(nr * sizeof(double **));
CHECK_MALLOC_RET_STATUS(sysPar->cosne, status, sysPar)
for (ii = 0; ii < nr; ii++) {
sysPar->trff[ii] = (double **) malloc(ng * sizeof(double *));
CHECK_MALLOC_RET_STATUS(sysPar->trff[ii], status, sysPar)
sysPar->cosne[ii] = (double **) malloc(ng * sizeof(double *));
CHECK_MALLOC_RET_STATUS(sysPar->cosne[ii], status, sysPar)
for (jj = 0; jj < ng; jj++) {
sysPar->trff[ii][jj] = (double *) malloc(2 * sizeof(double));
CHECK_MALLOC_RET_STATUS(sysPar->trff[ii][jj], status, sysPar)
sysPar->cosne[ii][jj] = (double *) malloc(2 * sizeof(double));
CHECK_MALLOC_RET_STATUS(sysPar->cosne[ii][jj], status, sysPar)
}
}
sysPar->limb_law = 0;
return sysPar;
}
/* function interpolating the rel table values for rin,rout,mu0,incl */
static RelSysPar *interpol_relTable(double a, double incl, double rin, double rout,
int *status) {
// load tables
if (ptr_rellineTable == nullptr) {
print_version_number();
read_relline_table(RELTABLE_FILENAME, &ptr_rellineTable, status);
CHECK_STATUS_RET(*status, nullptr);
}
relTable *tab = ptr_rellineTable;
assert(tab != nullptr);
double rms = kerr_rms(a);
// make sure the desired rmin is within bounds and order correctly
assert(rout > rin);
assert(rin >= rms);
double mu0 = cos(incl);
/**************************************/
/** 1 ** Interpolate in A-MU0 plane **/
/**************************************/
// get a structure to store the values from the interpolation in the A-MU0-plane
if (cached_tab_sysPar == nullptr) {
cached_tab_sysPar = new_relSysPar(tab->n_r, tab->n_g, status);
CHECK_STATUS_RET(*status, nullptr);
}
int ind_a = binary_search_float(tab->a, tab->n_a, (float) a);
int ind_mu0 = binary_search_float(tab->mu0, tab->n_mu0, (float) mu0);
float ifac_a = ((float) a - tab->a[ind_a]) /
(tab->a[ind_a + 1] - tab->a[ind_a]);
float ifac_mu0 = ((float) mu0 - tab->mu0[ind_mu0]) /
(tab->mu0[ind_mu0 + 1] - tab->mu0[ind_mu0]);
// we perform tests that the input values are consistent when loading the parameters
// this is just to double check (otherwise bad things happen, like an interpol. mu0>1!)
assert(ifac_mu0 >= 0);
assert(ifac_mu0 <= 1);
assert(ifac_a >= 0);
assert(ifac_a <= 1);
/** get the radial grid (the radial grid only changes with A by the table definition) **/
assert(fabsf(tab->arr[ind_a][ind_mu0]->r[tab->n_r - 1]
- tab->arr[ind_a][ind_mu0]->r[tab->n_r - 1]) < 1e-6);
int ii;
for (ii = 0; ii < tab->n_r; ii++) {
cached_tab_sysPar->re[ii] = interp_lin_1d(ifac_a,
tab->arr[ind_a][ind_mu0]->r[ii], tab->arr[ind_a + 1][ind_mu0]->r[ii]);
}
// we have problems for the intermost radius due to linear interpolation (-> set to RISCO)
if ((cached_tab_sysPar->re[tab->n_r - 1] > kerr_rms(a)) &&
((cached_tab_sysPar->re[tab->n_r - 1] - kerr_rms(a)) / cached_tab_sysPar->re[tab->n_r - 1] < 1e-3)) {
// printf(" re-setting RIN from %.3f to %.3f (dr = %.2e)\n",cached_tab_sysPar->re[tab->n_r-1],kerr_rms(a),
// (cached_tab_sysPar->re[tab->n_r-1]-kerr_rms(a))/cached_tab_sysPar->re[tab->n_r-1]);
cached_tab_sysPar->re[tab->n_r - 1] = kerr_rms(a);
}
// get the extent of the disk (indices are defined such that tab->r[ind+1] <= r < tab->r[ind]
int ind_rmin = inv_binary_search(cached_tab_sysPar->re, tab->n_r, rin);
int ind_rmax = inv_binary_search(cached_tab_sysPar->re, tab->n_r, rout);
int jj;
int kk;
for (ii = 0; ii < tab->n_r; ii++) {
// TODO: SHOULD WE ONLY INTERPOLATE ONLY THE VALUES WE NEED??? //
// only interpolate values where we need them (radius is defined invers!)
if (ii <= ind_rmin || ii >= ind_rmax + 1) {
interpol_a_mu0(ii, ifac_a, ifac_mu0, ind_a, ind_mu0, cached_tab_sysPar, tab);
} else { // set everything we won't need to 0 (just to be sure)
cached_tab_sysPar->gmin[ii] = 0.0;
cached_tab_sysPar->gmax[ii] = 0.0;
for (jj = 0; jj < tab->n_g; jj++) {
for (kk = 0; kk < 2; kk++) {
cached_tab_sysPar->trff[ii][jj][kk] = 0.0;
cached_tab_sysPar->cosne[ii][jj][kk] = 0.0;
}
}
}
}
/****************************/
/** 2 ** Bin to Fine Grid **/
/****************************/
// need to initialize and allocate memory
RelSysPar *sysPar = new_relSysPar(N_FRAD, tab->n_g, status);
CHECK_STATUS_RET(*status, nullptr);
get_fine_radial_grid(rin, rout, sysPar->re, sysPar->nr);
/** we do not have rmax=1000.0 in the table, but just values close to it so let's do this trick**/
double rmax = 1000.0;
if (cached_tab_sysPar->re[ind_rmax] < rmax && cached_tab_sysPar->re[ind_rmax] * 1.01 > rmax) {
cached_tab_sysPar->re[ind_rmax] = rmax;
}
// let's try to be as efficient as possible here (note that "r" DEcreases)
assert(ind_rmin > 0); // as defined inverse, re[ind_rmin+1] is the lowest value
assert((cached_tab_sysPar->re[ind_rmin + 1] <= rin));
assert((cached_tab_sysPar->re[ind_rmin] >= rin));
assert((cached_tab_sysPar->re[ind_rmax + 1] <= rout));
assert((cached_tab_sysPar->re[ind_rmax] >= rout));
assert(ind_rmax <= ind_rmin);
assert(rout <= 1000.0);
double ifac_r;
int ind_tabr = ind_rmin;
for (ii = sysPar->nr - 1; ii >= 0; ii--) {
while ((sysPar->re[ii] >= cached_tab_sysPar->re[ind_tabr])) {
ind_tabr--;
if (ind_tabr < 0) { //TODO: construct table such that we don't need this?
if (sysPar->re[ii] - RELTABLE_MAX_R <= 1e-6) {
ind_tabr = 0;
break;
} else {
RELXILL_ERROR("interpolation of rel_table on fine radial grid failed due to corrupted grid", status);
printf(" --> radius %.4e ABOVE the maximal possible radius of %.4e \n",
sysPar->re[ii], RELTABLE_MAX_R);
CHECK_STATUS_RET(*status, nullptr);
}
}
}
ifac_r = (sysPar->re[ii] - cached_tab_sysPar->re[ind_tabr + 1])
/ (cached_tab_sysPar->re[ind_tabr] - cached_tab_sysPar->re[ind_tabr + 1]);
// assert(ifac_r>=0.0);
// we only allow extrapolation (i.e. ifac_r < 0) for the last bin
if (ifac_r > 1.0 && ind_tabr > 0) {
RELXILL_ERROR("interpolation of rel_table on fine radial grid failed due to corrupted grid", status);
printf(" --> radius %.4e not found in [%.4e,%.4e] \n",
sysPar->re[ii], cached_tab_sysPar->re[ind_tabr + 1], cached_tab_sysPar->re[ind_tabr]);
CHECK_STATUS_RET(*status, nullptr);
}
for (jj = 0; jj < sysPar->ng; jj++) {
for (kk = 0; kk < 2; kk++) {
sysPar->trff[ii][jj][kk] =
interp_lin_1d(ifac_r,
cached_tab_sysPar->trff[ind_tabr + 1][jj][kk],
cached_tab_sysPar->trff[ind_tabr][jj][kk]);
sysPar->cosne[ii][jj][kk] =
interp_lin_1d(ifac_r,
cached_tab_sysPar->cosne[ind_tabr + 1][jj][kk],
cached_tab_sysPar->cosne[ind_tabr][jj][kk]);
}
}
sysPar->gmin[ii] =
interp_lin_1d(ifac_r, cached_tab_sysPar->gmin[ind_tabr + 1], cached_tab_sysPar->gmin[ind_tabr]);
sysPar->gmax[ii] =
interp_lin_1d(ifac_r, cached_tab_sysPar->gmax[ind_tabr + 1], cached_tab_sysPar->gmax[ind_tabr]);
}
return sysPar;
}
/** calculate all relativistic system parameters, including interpolation
* of the rel-table, and the emissivity; caching is implemented
* Input: relParam* param Output: relSysPar* system_parameter_struct
*/
RelSysPar *get_system_parameters(relParam *param, int *status) {
CHECK_STATUS_RET(*status, nullptr);
inpar *sysinp = set_input_syspar(param, status);
CHECK_STATUS_RET(*status, nullptr);
cache_info *ca_info = cli_check_cache(cache_syspar, sysinp, check_cache_syspar, status);
CHECK_STATUS_RET(*status, nullptr);
RelSysPar *sysPar = nullptr;
if (ca_info->syscache == 1 ) {
// system parameter values are cached, so we can take it from there
sysPar = ca_info->store->data->relSysPar;
if (is_debug_run()) {
printf(" DEBUG: SYSPAR-Cache: re-using calculated values\n");
}
} else {
// NOT CACHED, so we need to calculate the system parameters
sysPar = interpol_relTable(param->a, param->incl, param->rin, param->rout, status);
CHECK_STATUS_RET(*status, nullptr);
sysPar->limb_law = param->limb;
// get emissivity profile
sysPar->emis = calc_emis_profile(sysPar->re, sysPar->nr, param, status);
CHECK_STATUS_RET(*status, nullptr);
// now add (i.e., prepend) the current calculation to the cache
set_cache_syspar(&cache_syspar, param, sysPar, status);
if (is_debug_run() && *status == EXIT_SUCCESS) {
printf(" DEBUG: Adding new SYSPAR values to cache; the count is %i \n", cli_count_elements(cache_syspar));
}
}
free(ca_info);
free(sysinp);
CHECK_RELXILL_DEFAULT_ERROR(status);
// make a sanity check for now
if (*status == EXIT_SUCCESS) {
assert(cache_syspar != nullptr);
assert(sysPar != nullptr);
}
return sysPar;
}
/** get new structure to store the relline spectrum (possibly for several zones)
important note: ener has n_ener+1 number of bins **/
relline_spec_multizone *new_rel_spec(int nzones, const int n_ener, int *status) {
auto *spec = new relline_spec_multizone;
CHECK_MALLOC_RET_STATUS(spec, status, nullptr)
spec->n_zones = nzones;
spec->n_ener = n_ener;
spec->flux = (double **) malloc(spec->n_zones * sizeof(double *));
CHECK_MALLOC_RET_STATUS(spec->flux, status, spec)
int ii;
for (ii = 0; ii < spec->n_zones; ii++) {
spec->flux[ii] = (double *) malloc(n_ener * sizeof(double));
CHECK_MALLOC_RET_STATUS(spec->flux[ii], status, spec)
}
spec->ener = (double *) malloc((spec->n_ener + 1) * sizeof(double));
CHECK_MALLOC_RET_STATUS(spec->ener, status, spec)
spec->rgrid = nullptr; // will be allocated later
spec->rel_cosne = nullptr; // will be allocated later (only if need)
return spec;
}
relline_spec_multizone_emis *new_rel_spec_emis(int nzones, const int n_ener,const int n_emis,int *status) {
auto *spec = new relline_spec_multizone_emis;
CHECK_MALLOC_RET_STATUS(spec, status, nullptr)
spec->n_zones = nzones;
spec->n_ener = n_ener;
spec->flux = (double ***) malloc(spec->n_zones * sizeof(double **));
CHECK_MALLOC_RET_STATUS(spec->flux, status, spec)
int ii;
int jj;
for (ii = 0; ii < spec->n_zones; ii++) {
spec->flux[ii] = (double **) malloc(n_emis * sizeof(double *));
CHECK_MALLOC_RET_STATUS(spec->flux[ii], status, spec)
for (jj=0; jj<n_emis;jj++){
spec->flux[ii][jj] = (double *) malloc(n_ener * sizeof(double));
CHECK_MALLOC_RET_STATUS(spec->flux[ii][jj], status, spec)
}
}
spec->ener = (double *) malloc((spec->n_ener + 1) * sizeof(double));
CHECK_MALLOC_RET_STATUS(spec->ener, status, spec)
spec->rgrid = nullptr; // will be allocated later
spec->rel_cosne = nullptr; // will be allocated later (only if need)
return spec;
}
/** get new structure to store the cosne distribution spectrum (possibly for several zones) **/
RelCosne *new_rel_cosne(int nzones, int n_incl, int *status) {
RelCosne *spec = (RelCosne *) malloc(sizeof(RelCosne));
CHECK_MALLOC_RET_STATUS(spec, status, nullptr)
spec->n_zones = nzones;
spec->n_cosne = n_incl;
spec->cosne = (double *) malloc(spec->n_cosne * sizeof(double));
CHECK_MALLOC_RET_STATUS(spec->cosne, status, spec)
spec->dist = (double **) malloc(spec->n_zones * sizeof(double *));
CHECK_MALLOC_RET_STATUS(spec->dist, status, spec)
int ii;
for (ii = 0; ii < spec->n_zones; ii++) {
spec->dist[ii] = (double *) malloc(spec->n_cosne * sizeof(double));
CHECK_MALLOC_RET_STATUS(spec->dist[ii], status, spec)
}
return spec;
}
/**
* Initialize the structure to store the relline spectra for multiple radial zones
**/
void init_relline_spec_multizone(relline_spec_multizone **spec, relParam *param, xillTable *xill_tab, const double *radial_zones,
double **pt_ener, const int n_ener, int *status) {
CHECK_STATUS_VOID(*status);
/** in case of the relxill-LP model multiple zones are used **/
int nzones = param->num_zones;
if ((*spec) == nullptr) {
(*spec) = new_rel_spec(nzones, n_ener, status);
} else {
// check if the number of zones changed or number of energy bins
if ((nzones != (*spec)->n_zones) || (n_ener != (*spec)->n_ener)) {
// -> if yes, we need to free memory and re-allocate the appropriate space
free_rel_spec((*spec));
(*spec) = new_rel_spec(nzones, n_ener, status);
}
}
for (int ii = 0; ii <= (*spec)->n_ener; ii++) {
(*spec)->ener[ii] = (*pt_ener)[ii];
}
if (xill_tab != nullptr) {
if ((*spec)->rel_cosne == nullptr) {
(*spec)->rel_cosne = new_rel_cosne(nzones, xill_tab->n_incl, status);
}
for (int ii = 0; ii < (*spec)->rel_cosne->n_cosne; ii++) {
(*spec)->rel_cosne->cosne[ii] = cos(xill_tab->incl[ii] * M_PI / 180);
}
}
// copy the radial grid to the structure
if ((*spec)->rgrid == nullptr) {
(*spec)->rgrid = new double[param->num_zones + 1];
for (int ii=0; ii<param->num_zones+1; ii++){
(*spec)->rgrid[ii] = radial_zones[ii];
}
}
CHECK_RELXILL_DEFAULT_ERROR(status);
}
void init_relline_spec_multizone_emis(relline_spec_multizone_emis **spec, relParam *param, xillTable *xill_tab, const double *radial_zones,
double **pt_ener, const int n_ener, int *status) {
CHECK_STATUS_VOID(*status);
/** in case of the relxill-LP model multiple zones are used **/
int nzones = param->num_zones;
if ((*spec) == nullptr) {
(*spec) = new_rel_spec_emis(nzones, n_ener, xill_tab->n_incl,status);
} else {
// check if the number of zones changed or number of energy bins
if ((nzones != (*spec)->n_zones) || (n_ener != (*spec)->n_ener)) {
// -> if yes, we need to free memory and re-allocate the appropriate space
free_rel_spec_emis((*spec));
(*spec) = new_rel_spec_emis(nzones, n_ener,xill_tab->n_incl, status);
}
}
for (int ii = 0; ii <= (*spec)->n_ener; ii++) {
(*spec)->ener[ii] = (*pt_ener)[ii];
}
if (xill_tab != nullptr) {
if ((*spec)->rel_cosne == nullptr) {
(*spec)->rel_cosne = new_rel_cosne(nzones, xill_tab->n_incl, status);
}
for (int ii = 0; ii < (*spec)->rel_cosne->n_cosne; ii++) {
(*spec)->rel_cosne->cosne[ii] = cos(xill_tab->incl[ii] * M_PI / 180);
}
}
// copy the radial grid to the structure
if ((*spec)->rgrid == nullptr) {
(*spec)->rgrid = new double[param->num_zones + 1];
for (int ii=0; ii<param->num_zones+1; ii++){
(*spec)->rgrid[ii] = radial_zones[ii];
}
}
CHECK_RELXILL_DEFAULT_ERROR(status);
}
static void zero_rel_spec_flux(relline_spec_multizone *spec) {
int ii;
int jj;
for (ii = 0; ii < spec->n_zones; ii++) {
for (jj = 0; jj < spec->n_ener; jj++) {
spec->flux[ii][jj] = 0.0;
}
if (spec->rel_cosne != nullptr) {
for (jj = 0; jj < spec->rel_cosne->n_cosne; jj++) {
spec->rel_cosne->dist[ii][jj] = 0.0;
}
}
}
}
static void zero_rel_spec_flux_emis(relline_spec_multizone_emis *spec) {
int ii;
int jj;
int kk;
for (ii = 0; ii < spec->n_zones; ii++) {
for (kk=0;kk<spec->rel_cosne->n_cosne;kk++){
for (jj = 0; jj < spec->n_ener; jj++) {
spec->flux[ii][kk][jj] = 0.0;
}
}
if (spec->rel_cosne != nullptr) {
for (jj = 0; jj < spec->rel_cosne->n_cosne; jj++) {
spec->rel_cosne->dist[ii][jj] = 0.0;
}
}
}
}
/** relat. transfer function, which we will need to integrate over the energy bin then **/
static str_relb_func *new_str_relb_func(RelSysPar *sysPar, int *status) {
str_relb_func *str = (str_relb_func *) malloc(sizeof(str_relb_func));
CHECK_MALLOC_RET_STATUS(str, status, nullptr)
str->gstar = sysPar->gstar;
str->ng = sysPar->ng;
str->limb_law = 0;
return str;
}
/** relat. function which we want to integrate **/
static double relb_func(double eg, int k, str_relb_func *str) {
// get the redshift from the energy
// double eg = e/line_energy;
double egstar = (eg - str->gmin) * str->del_g;
// find the indices in the original g-grid, but check first if they have already been calculated
int ind;
if (!((egstar >= str->gstar[str->save_g_ind]) && (egstar < str->gstar[str->save_g_ind + 1]))) {
str->save_g_ind = binary_search(str->gstar, str->ng, egstar);
}
ind = str->save_g_ind;
double inte = (egstar - str->gstar[ind]) / (str->gstar[ind + 1] - str->gstar[ind]);
double inte1 = 1.0 - inte;
double ftrf = inte * str->trff[ind][k] + inte1 * str->trff[ind + 1][k];
double val = pow(eg, 3) / ((str->gmax - str->gmin) * sqrt(egstar - egstar * egstar)) * ftrf * str->emis;
/** isotropic limb law by default (see Svoboda (2009)) **/
if (str->limb_law == 0) {
return val;
} else {
double fmu0 = inte * str->cosne[ind][k] + inte1 * str->cosne[ind + 1][k];
double limb = 1.0;
if (str->limb_law == 1) { // !Laor(1991)
limb = (1.0 + 2.06 * fmu0);
} else if (str->limb_law == 2) { // !Haardt (1993)
limb = log(1.0 + 1.0 / fmu0);
}
return val * limb;
}
}
/** Romberg Integration Routine **/
static double romberg_integration(double a, double b, int k, str_relb_func *str) {
const double prec = 0.02;
double obtprec = 1.0;
const int itermin = 0;
int itermax = 5;
enum {
maxiter = 6
};
double t[maxiter + 1][maxiter + 1];
int niter = 0;
if (itermax > maxiter) {
itermax = maxiter;
}
// check if this value has already been calculated
double r;
if (str->cached_relbf) {
r = str->cache_val_relb_func[k];
} else {
r = relb_func(a, k, str);
}
str->cache_val_relb_func[k] = relb_func(b, k, str);
str->cache_rad_relb_fun = str->re;
// rb(k) = RELB_FUNC(b,k);
int ii;
double ta = (r + str->cache_val_relb_func[k]) / 2.0;
double pas = b - a;
double pasm = 1.0;
double s;
t[0][0] = ta * pas;
while ((niter < itermin) || ((obtprec > prec) && (niter <= itermax))) {
niter++;
pas = pas / 2.0;
pasm = pasm / 2.0;
s = ta;
for (ii = 1; ii <= pow(2, niter) - 1; ii++) {
s += relb_func(a + pas * ii, k, str);
}
t[0][niter] = s * pas;
r = 1.0;
for (ii = 1; ii <= niter; ii++) {
r *= 4.0;
int jj = niter - ii;
t[ii][jj] = (r * t[ii - 1][jj + 1] - t[ii - 1][jj]) / (r - 1.0);
}
obtprec = fabs(t[niter][0] - t[niter - 1][0]) / t[niter][0];
}
return t[niter][0];
}
/** function which makes an approximated integration for gstar->0/1
this is only done within gstar=[0,H] and gstar[H,1-H]
input: bin_lo and bin_hi
output: area of the bin (= luminosity = E/dt/bin) ) **/
static double int_edge(double blo, double bhi, double h, str_relb_func *str, double line_energy) {
// get the value of the Luminosity on the point closest to the ones to be approximated ("H")
double hex;
double lo;
double hi;
// #1: upper or lower limit -> write the corresponding gstar-value to hex
if (blo <= 0.5) {
hex = h;
lo = blo;
hi = bhi;
} else {
hex = 1.0 - h;
/** variable transformation for upper limit x -> 1 - x
leads to the same integral
(if the correct normalization is chosen; hex keeps track of that) **/
lo = 1.0 - bhi;
hi = 1.0 - blo;
}
// #2: get the normalization value
int k = 0;
double norm = 0.0;
for (k = 0; k < 2; k++) {
norm = norm + relb_func(gstar2ener(hex, str->gmin, str->gmax, line_energy), k, str);
}
// #3: thanks to variable transformation:
// both cases are described by the one for [0,H]
norm = norm * sqrt(h);
return 2 * norm * (sqrt(hi) - sqrt(lo)) * line_energy * (str->gmax - str->gmin);
}
static double int_edge_emis(double blo, double bhi, double h, str_relb_func *str, double line_energy, int k) {
// get the value of the Luminosity on the point closest to the ones to be approximated ("H")
double hex;
double lo;
double hi;
// #1: upper or lower limit -> write the corresponding gstar-value to hex
if (blo <= 0.5) {
hex = h;
lo = blo;
hi = bhi;
} else {
hex = 1.0 - h;
/** variable transformation for upper limit x -> 1 - x
leads to the same integral
(if the correct normalization is chosen; hex keeps track of that) **/
lo = 1.0 - bhi;
hi = 1.0 - blo;
}
// #2: get the normalization value
double norm = 0.0;
norm = norm + relb_func(gstar2ener(hex, str->gmin, str->gmax, line_energy), k, str);
// #3: thanks to variable transformation:
// both cases are described by the one for [0,H]
norm = norm * sqrt(h);
return 2 * norm * (sqrt(hi) - sqrt(lo)) * line_energy * (str->gmax - str->gmin);
}
/** function which calculates the normal integral by romberg's method
(it is acutally jsut a wrapper which sets the correct parameters
and coordinates the integration over k=1,2)
input: bin_lo and bin_hi
output: area of the bin (= luminosity = E/dt/bin) ) **/
static double int_romb(double lo, double hi, str_relb_func *str, double line_energy) {
double flu = 0.0;
/** We are doing a trick here: for the "red wing" of the line, one can show that a simple trapez integration is enough,
* as the profile is very smooth. In order to avoid problems for narrow, double peaked lines, the red wing is defined
* to start at 0.95*line_energy **/
int k;
if (lo >= line_energy * 0.95) {
for (k = 0; k < 2; k++) {
flu += romberg_integration(lo, hi, k, str);
}
} else {
for (k = 0; k < 2; k++) {
flu += relb_func((hi + lo) / 2.0, k, str) * (hi - lo);
}
}
return flu;
}
static double int_romb_emis(double lo, double hi, str_relb_func *str, double line_energy,int k ) {
double flu = 0.0;
/** We are doing a trick here: for the "red wing" of the line, one can show that a simple trapez integration is enough,
* as the profile is very smooth. In order to avoid problems for narrow, double peaked lines, the red wing is defined
* to start at 0.95*line_energy **/
if (lo >= line_energy * 0.95) {
flu += romberg_integration(lo, hi, k, str);
} else {
flu += relb_func((hi + lo) / 2.0, k, str) * (hi - lo);
}
return flu;
}
/** integrate the flux bin (see Dauser+2010, MNRAS for details) **/
static double integ_relline_bin(str_relb_func *str, double rlo0, double rhi0) {
double line_ener = 1.0;
double flu = 0.0;
double gblo = (rlo0 / line_ener - str->gmin) * str->del_g;
if (gblo < 0.0) {
gblo = 0.0;
} else if (gblo > 1.0) {
gblo = 1.0;
}
double gbhi = (rhi0 / line_ener - str->gmin) * str->del_g;
if (gbhi < 0.0) {
gbhi = 0.0;
} else if (gbhi > 1.0) {
gbhi = 1.0;
}
if (gbhi == 0) {
return 0.0;
}
double rlo = rlo0;
double rhi = rhi0;
double hlo;
double hhi;
// #1: low approx. integration
if (gblo <= GFAC_H) {
// range of the low-app.-integration
hlo = gblo;
hhi = GFAC_H;
// begin of the 'real' integration
rlo = gstar2ener(GFAC_H, str->gmin, str->gmax, line_ener);
// .. but also check if this integration is necessary
if (gbhi <= GFAC_H) {
hhi = gbhi;
rlo = -1.0;
}
// approximated integration
flu = flu + int_edge(hlo, hhi, GFAC_H, str, line_ener);
}
// #2: upper limit approximation to be taken into account?
if (gbhi >= (1.0 - GFAC_H)) {
// range of the upper-app.-integration
hhi = gbhi;
hlo = 1.0 - GFAC_H;
// begin of the 'real' integration
rhi = gstar2ener(1 - GFAC_H, str->gmin, str->gmax, line_ener);
// .. but also check if this integration is necessary
if (gblo >= (1.0 - GFAC_H)) {
hlo = gblo;
rhi = -1.0;
}
/** the same approximated integration as in case #1 can be
applied, if one makes a variable transformation **/
flu = flu + int_edge(hlo, hhi, GFAC_H, str, line_ener);
}
// #3: real integration (only if necessary)
if ((rhi >= 0) && (rlo >= 0)) {
// has the function relb_func been calculated at the lower bin boundary before?
// (should've been upper bound before; also make sure we haven't changed the radial bin!)
if ((fabs(rlo - str->cache_bin_ener) < CACHE_LIMIT) && (fabs(str->re - str->cache_rad_relb_fun) < CACHE_LIMIT)) {
str->cached_relbf = 1;
} else {
str->cached_relbf = 0;
}
flu = flu + int_romb(rlo, rhi, str, line_ener);
}
return flu;
}
static double integ_relline_bin_emis(str_relb_func *str, double rlo0, double rhi0,int kk) {
double line_ener = 1.0;
double flu = 0.0;
double gblo = (rlo0 / line_ener - str->gmin) * str->del_g;
if (gblo < 0.0) {
gblo = 0.0;
} else if (gblo > 1.0) {
gblo = 1.0;
}
double gbhi = (rhi0 / line_ener - str->gmin) * str->del_g;
if (gbhi < 0.0) {
gbhi = 0.0;
} else if (gbhi > 1.0) {
gbhi = 1.0;
}
if (gbhi == 0) {
return 0.0;
}
double rlo = rlo0;
double rhi = rhi0;
double hlo;
double hhi;
// #1: low approx. integration
if (gblo <= GFAC_H) {
// range of the low-app.-integration
hlo = gblo;
hhi = GFAC_H;
// begin of the 'real' integration
rlo = gstar2ener(GFAC_H, str->gmin, str->gmax, line_ener);
// .. but also check if this integration is necessary
if (gbhi <= GFAC_H) {
hhi = gbhi;
rlo = -1.0;
}
// approximated integration
flu = flu + int_edge_emis(hlo, hhi, GFAC_H, str, line_ener,kk);
}
// #2: upper limit approximation to be taken into account?
if (gbhi >= (1.0 - GFAC_H)) {
// range of the upper-app.-integration
hhi = gbhi;
hlo = 1.0 - GFAC_H;
// begin of the 'real' integration
rhi = gstar2ener(1 - GFAC_H, str->gmin, str->gmax, line_ener);
// .. but also check if this integration is necessary
if (gblo >= (1.0 - GFAC_H)) {
hlo = gblo;
rhi = -1.0;
}
/** the same approximated integration as in case #1 can be
applied, if one makes a variable transformation **/
flu = flu + int_edge_emis(hlo, hhi, GFAC_H, str, line_ener,kk);
}
// #3: real integration (only if necessary)
if ((rhi >= 0) && (rlo >= 0)) {
// has the function relb_func been calculated at the lower bin boundary before?
// (should've been upper bound before; also make sure we haven't changed the radial bin!)
if ((fabs(rlo - str->cache_bin_ener) < CACHE_LIMIT) && (fabs(str->re - str->cache_rad_relb_fun) < CACHE_LIMIT)) {
str->cached_relbf = 1;
} else {
str->cached_relbf = 0;
}
flu = flu + int_romb_emis(rlo, rhi, str, line_ener, kk);
}
return flu;
}
static void set_str_relbf(str_relb_func *str, double re, double gmin, double gmax, double **trff,
double **cosne, double emis, int limb_law) {
str->re = re;
str->gmin = gmin;
str->gmax = gmax;
str->del_g = 1. / (gmax - gmin);
str->emis = emis;
str->trff = trff;
str->cosne = cosne;
str->cache_bin_ener = -1.0;
str->cache_rad_relb_fun = -1.0;
str->cached_relbf = 0;
str->limb_law = limb_law;
str->save_g_ind = 0;
}
/** function to properly re-normalize the calc_relline_profile **/
void renorm_relline_profile(relline_spec_multizone *spec, relParam *rel_param, const int *status) {
CHECK_STATUS_VOID(*status);
// normalize to 'cts/bin'
int ii;
int jj;
double sum = 0.0;
for (ii = 0; ii < spec->n_zones; ii++) {
for (jj = 0; jj < spec->n_ener; jj++) {
spec->flux[ii][jj] /= 0.5 * (spec->ener[jj] + spec->ener[jj + 1]);
sum += spec->flux[ii][jj];
}
}
/** only renormalize if not the relxill model or not a lamp post model **/
if (do_renorm_model(rel_param)) {
double relline_norm = 1;
if (is_relxill_model(rel_param->model_type) && rel_param->emis_type == EMIS_TYPE_BKN) {
relline_norm = norm_factor_semi_infinite_slab(rel_param->incl * 180.0 / M_PI);
}
if (is_debug_run()) {
printf(" DEBUG: re-normalizing output spectrum\n");
}
for (ii = 0; ii < spec->n_zones; ii++) {
for (jj = 0; jj < spec->n_ener; jj++) {
spec->flux[ii][jj] *= relline_norm / sum;
}
}