-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchspec.cxx
306 lines (239 loc) · 8.35 KB
/
chspec.cxx
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
/*
CHIANTI Model for XSPEC to fit the X-ray Spectrum.
It Uses the CHIANTI database version-10.0.1.
Notes:
This version of the model can be used for an instrument
response of lower energy bound 0.1 keV or above and a
bin size of an integer multiple of 1eV.
Calculated spectrum are valid in the energy rage of 0.1-30.0 keV.
The present version of the model is not efficient for the model
calculation beyond the mentioned energy range.
Above 30 keV it sets all the counts to zero.
Last Modified:
Aug,2020 : Biswajit
Oct,2020 : Santosh, Mithun (modified)
Mar,2021 : Biswajit (Updated for CHIANTI v10)
June,2021: Biswajit, (Generalized for the use of other instruments.)
Nov 2021 : Mithun: Fixed bug in generalization, rebinned table stored in memory to make the calculation
faster
Nov 2021 : Mithun: Spectra from Single Gaussian DEM model added
Sep 2022 : Mithun: Restructured the codes to have combined isothermal and gaussian DEM models into a
single chspec package
*/
#include "readTable.h"
//if(TABLE_READ_STATUS==0)
//{
// filehandle chfh;
// TABLE_READ_STATUS=1;
//}
extern "C"
void chisoth(const RealArray& energyArray, const RealArray& parms,
int spectrum, RealArray& flux, RealArray& fluxErr,
const string& init)
{
double T,T1, mf, ad, tL, tR, eL, eR, dydx,dTRL,dTL ;
float abund[I_n] ;
int nE, en, inL, inR,ene_ind;
int LowerBound_ind, Ebound_error;
T1=parms[0] ;// Log(T)
T=pow(10,T1); // in K
nE = energyArray.size()-1;
flux.resize(nE);
for (en = 0; en < nE; en++) flux[en]=0.0;
for (int p=1; p<I_n; p++) {abund[p]= parms[p];}
abund[0]=12.0;
inL = 0;
if (T >= chfh.temp[T_n - 2]) inL = T_n - 2;
else if (T<=chfh.temp[1]) inL=0;
else{
inL=(int)((T1-logTMin)/dlt)-1;
while ( T > chfh.temp[inL + 1] ) inL++;
}
inR=inL+1;
tL = chfh.temp[inL];
tR = chfh.temp[inR];
// For interpolation in linear T
//dTRL=tR - tL;
//dTL=T - tL;
// For interpolation in log T
dTRL=log10(tR/tL);
dTL=log10(T/tL);
// Check if energy bins match previous stored ones
int matchBinSpec=-1;
int enBinOffset=0;
for (int i=0;i<chfh.nBinSpec;i++)
{
if(nE==chfh.nEbin[i])
matchBinSpec=i;
enBinOffset+=chfh.nEbin[i];
}
if((enBinOffset+nE)>E_n)
{
printf("chspec Error: Too many data sets together. Cannot store rebinned models together\n");
return;
}
if(matchBinSpec==-1) // Response energy bins are not same as previously stored ones
{
printf("Rebinning table to response energy bins: NEW NBINS %d OLD NBINS %d\n",nE,chfh.nEbin[chfh.nBinSpec]);
LowerBound_ind = 0;
Ebound_error = 0;
for (LowerBound_ind = 0; LowerBound_ind < E_n;LowerBound_ind++)
{
if (fabs(chfh.ene[LowerBound_ind] - energyArray[0]) < 1.0e-6)
{
Ebound_error = 0;
break;
}
else
Ebound_error = 1;
}
if (int(Ebound_error) == 1)
{
printf("chspec Error: Lower bound of the energy response should be >= 100 eV or should be \
incremented as an integer multiple of 1 eV. ...Terminating the program.\n");
return;
}
// Rebin spectrum to required energy bins
//printf("Lower bound %d\n",LowerBound_ind);
enBinOffset=0;
for (int i=0;i<chfh.nBinSpec;i++) enBinOffset+=chfh.nEbin[i];
for (int i = 0; i < I_n; i++)
{
for (int iT=0;iT<T_n;iT++)
{
ene_ind = LowerBound_ind;
for (en = 0; en < nE; en++)
{
chfh.allspecBinned[i][iT][en+enBinOffset]=0;
while(chfh.ene[ene_ind] >= energyArray[en] and chfh.ene[ene_ind] < energyArray[en+1])
{
chfh.allspecBinned[i][iT][en+enBinOffset]+=chfh.allspec[i][iT][ene_ind];
ene_ind = ene_ind+1;
}
}
}
}
chfh.nEbin[chfh.nBinSpec]=nE;
matchBinSpec=chfh.nBinSpec;
chfh.nBinSpec+=1;
}
enBinOffset=0;
for (int i=0;i<matchBinSpec;i++) enBinOffset+=chfh.nEbin[i];
//printf("%ld\t%d\t%d\t%d\n",nE,chfh.nBinSpec,matchBinSpec,enBinOffset,chfh.nEbin[matchBinSpec]);
for (int i = 0; i < I_n; i++)
{
ad = abund[i] - abund[0];
mf = pow(10, ad);
for (en = 0; en < nE; en++)
{
eR=chfh.allspecBinned[i][inR][en+enBinOffset];
eL=chfh.allspecBinned[i][inL][en+enBinOffset];
dydx= (eR-eL)/dTRL;
flux[en]+= mf*(eL + dydx*dTL);
}
}
}
extern "C"
void chgausdem(const RealArray& energyArray, const RealArray& parms,
int spectrum, RealArray& flux, RealArray& fluxErr,
const string& init)
{
int nE,en,ene_ind;
float abund[I_n];
double ad,mfArray[I_n];
int LowerBound_ind, Ebound_error;
double totEM[T_n],maxEM=0;
float logT,sigma;
for (int i=0;i<T_n;i++) totEM[i]=0.0;
nE = energyArray.size()-1;
flux.resize(nE);
for (en = 0; en < nE; en++) flux[en]=0.0;
for (int p=0; p<I_n; p++) {abund[p]= parms[p];}
// for (int p=0; p<nGaus; p++) gausDEM[p]=parms[p+I_n];
logT=parms[I_n];
sigma=parms[I_n+1];
for (int i=0;i<T_n;i++)
{
totEM[i]=exp(-((chfh.logTemp[i]-logT)*(chfh.logTemp[i]-logT))/(2.0*sigma*sigma))*chfh.deltaTemp[i];
if(totEM[i]>maxEM) maxEM=totEM[i];
}
//for (int i=0;i<T_n;i++) printf("%lf\n",totEM[i]);
// Check if energy bins match previous stored ones
int matchBinSpec=-1;
int enBinOffset=0;
for (int i=0;i<chfh.nBinSpec;i++)
{
if(nE==chfh.nEbin[i])
matchBinSpec=i;
enBinOffset+=chfh.nEbin[i];
}
if((enBinOffset+nE)>E_n)
{
printf("chspec Error: Too many data sets together. Cannot store rebinned models together\n");
return;
}
if(matchBinSpec==-1) // Response energy bins are not same as previously stored ones
{
printf("Rebinning table to response energy bins\n");
LowerBound_ind = 0;
Ebound_error = 0;
for (LowerBound_ind = 0; LowerBound_ind < E_n;LowerBound_ind++)
{
if (fabs(chfh.ene[LowerBound_ind] - energyArray[0]) < 1.0e-6)
{
Ebound_error = 0;
break;
}
else
Ebound_error = 1;
}
if (int(Ebound_error) == 1)
{
printf("chspec Error: Lower bound of the energy response should be >= 100 eV or should be \
incremented as an integer multiple of 1 eV. ...Terminating the program.\n");
return;
}
// Rebin spectrum to required energy bins
//printf("Lower bound %d\n",LowerBound_ind);
enBinOffset=0;
for (int i=0;i<chfh.nBinSpec;i++) enBinOffset+=chfh.nEbin[i];
for (int i = 0; i < I_n; i++)
{
for (int iT=0;iT<T_n;iT++)
{
ene_ind = LowerBound_ind;
for (en = 0; en < nE; en++)
{
chfh.allspecBinned[i][iT][en+enBinOffset]=0;
while(chfh.ene[ene_ind] >= energyArray[en] and chfh.ene[ene_ind] < energyArray[en+1])
{
chfh.allspecBinned[i][iT][en+enBinOffset]+=chfh.allspec[i][iT][ene_ind];
ene_ind = ene_ind+1;
}
}
}
}
chfh.nEbin[chfh.nBinSpec]=nE;
matchBinSpec=chfh.nBinSpec;
chfh.nBinSpec+=1;
}
enBinOffset=0;
for (int i=0;i<matchBinSpec;i++) enBinOffset+=chfh.nEbin[i];
//for (en = 0; en < nE; en++) printf("%lf\n",chfh.allspecBinned[25][400][en]);
for (int i = 0; i < I_n; i++)
{
ad = abund[i] - abund[0];
mfArray[i] = pow(10, ad);
}
for (int iT=0;iT<T_n;iT++)
{
if((totEM[iT]/maxEM) > 1e-10)
{
for (int i = 0; i < I_n; i++)
{
for (en = 0; en < nE; en++)
flux[en]+=mfArray[i]*chfh.allspecBinned[i][iT][en+enBinOffset]*totEM[iT];
}
}
}
}