-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschec.py
executable file
·532 lines (392 loc) · 13.5 KB
/
schec.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 7 10:12:31 2018
Implements Schechter function models in Sherpa
@author: loveday
"""
import math
import numpy as np
from sherpa.models import model
__all__ = ('SchecMag', 'SchecMass', 'SaundersMag', 'SaundersMass')
ln10 = math.log(10)
def _lognormal(pars, M):
"""Evaluate a normal function in magnitude or log-mass space.
Parameters
----------
pars: sequence of 3 numbers
The order is M_c, sigma_c, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of magnitudes or log masses
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(M_c, sigma_c, lgps) = pars
phi = 10**lgps * np.exp(-(M - M_c)**2 / (2*sigma_c**2))
return phi
class LogNormal(model.ArithmeticModel):
"""A normal function in magnitude or log-mass space.
The model parameters are:
M_c
The central magnitude.
sigma_c
The standard deviation.
lgps
log10(phi*).
"""
def __init__(self, name='lognormal'):
self.M_c = model.Parameter(name, 'M_c', -21.5, min=-23, max=-19)
self.sigma_c = model.Parameter(name, 'sigma_c', 0.5, min=0.1, max=1.0)
self.lgps = model.Parameter(name, 'lgps', -4, min=-7, max=-2)
model.ArithmeticModel.__init__(self, name,
(self.M_c, self.sigma_c, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _lognormal(pars, x)
def _schecmag(pars, M):
"""Evaluate a Schechter function in magnitude space.
Parameters
----------
pars: sequence of 3 numbers
The order is Mstar, alpha, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of magnitudes
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(Mstar, alpha, lgps) = pars
L = 10**(0.4*(Mstar - M))
phi = 0.4*ln10 * 10**lgps * L**(1+alpha) * np.exp(-L)
return phi
class SchecMag(model.ArithmeticModel):
"""A Schechter function in magnitude space.
The model parameters are:
Mstar
The characteristic magnitude.
alpha
The faint-end slope.
lgps
log10(phi*).
"""
def __init__(self, name='schecmag'):
self.Mstar = model.Parameter(name, 'Mstar', -20.2, min=-22, max=-19)
self.alpha = model.Parameter(name, 'alpha', -1.2, min=-2, max=1)
self.lgps = model.Parameter(name, 'lgps', -2, min=-8, max=0)
model.ArithmeticModel.__init__(self, name,
(self.Mstar, self.alpha, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _schecmag(pars, x)
def _schecmagsq(pars, M):
"""Evaluate a modified Schechter function in magnitude space.
See Yang+2008 eqn 5.
Parameters
----------
pars: sequence of 3 numbers
The order is Mstar, alpha, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of magnitudes
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(Mstar, alpha, lgps) = pars
L = 10**(0.4*(Mstar - M))
phi = 0.4*ln10 * 10**lgps * L**(1+alpha) * np.exp(-L**2)
return phi
class SchecMagSq(model.ArithmeticModel):
"""A modified Schechter function in magnitude space.
The model parameters are:
Mstar
The characteristic magnitude.
alpha
The faint-end slope.
lgps
log10(phi*).
"""
def __init__(self, name='schecmagsq'):
self.Mstar = model.Parameter(name, 'Mstar', -20.2, min=-22, max=-19)
self.alpha = model.Parameter(name, 'alpha', -1.2, min=-2, max=1)
self.lgps = model.Parameter(name, 'lgps', -2, min=-8, max=0)
model.ArithmeticModel.__init__(self, name,
(self.Mstar, self.alpha, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _schecmagsq(pars, x)
def _schecmaggen(pars, M):
"""Evaluate a generalised Schechter function in magnitude space.
Within the exponent, L/L* is raised to the power beta.
Parameters
----------
pars: sequence of 4 numbers
The order is Mstar, alpha, beta, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of magnitudes
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(Mstar, alpha, beta, lgps) = pars
L = 10**(0.4*(Mstar - M))
phi = 0.4*ln10 * 10**lgps * L**(1+alpha) * np.exp(-L**beta)
return phi
class SchecMagGen(model.ArithmeticModel):
"""A generalised Schechter function in magnitude space.
The model parameters are:
Mstar
The characteristic magnitude.
alpha
The faint-end slope.
beta
The power to which L/L* is raised within the exponent
lgps
log10(phi*).
"""
def __init__(self, name='schecmaggen'):
self.Mstar = model.Parameter(name, 'Mstar', -20.2, min=-22, max=-19)
self.alpha = model.Parameter(name, 'alpha', -1.2, min=-2, max=1)
self.beta = model.Parameter(name, 'beta', 1, min=0, max=3)
self.lgps = model.Parameter(name, 'lgps', -2, min=-8, max=0)
model.ArithmeticModel.__init__(
self, name, (self.Mstar, self.alpha, self.beta, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _schecmaggen(pars, x)
def _schecmassgen(pars, M):
"""Evaluate a generalised Schechter function in log-mass space.
Within the exponent, M/M** is raised to the power beta.
Parameters
----------
pars: sequence of 4 numbers
The order is Mstar, alpha, beta, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of magnitudes
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(Mstar, alpha, beta, lgps) = pars
L = 10**(M - Mstar)
phi = ln10 * 10**lgps * L**(1+alpha) * np.exp(-L**beta)
return phi
class SchecMassGen(model.ArithmeticModel):
"""A generalised Schechter function in log-mass space.
The model parameters are:
Mstar
The characteristic mass.
alpha
The faint-end slope.
beta
The power to which L/L* is raised within the exponent
lgps
log10(phi*).
"""
def __init__(self, name='schecmassgen'):
self.Mstar = model.Parameter(name, 'Mstar', 10.5, min=9, max=11)
self.alpha = model.Parameter(name, 'alpha', -1.2, min=-2, max=1)
self.beta = model.Parameter(name, 'beta', 1, min=0, max=2.5)
self.lgps = model.Parameter(name, 'lgps', -2, min=-8, max=0)
model.ArithmeticModel.__init__(
self, name, (self.Mstar, self.alpha, self.beta, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _schecmassgen(pars, x)
def _schecmass(pars, M):
"""Evaluate a Schechter function in log-mass space.
Parameters
----------
pars: sequence of 3 numbers
The order is log Mstar, alpha, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of log masses
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(Mstar, alpha, lgps) = pars
L = 10**(M - Mstar)
phi = ln10 * 10**lgps * L**(1+alpha) * np.exp(-L)
return phi
class SchecMass(model.ArithmeticModel):
"""A Schechter function in log mass space.
The model parameters are:
Mstar
The characteristic log mass.
alpha
The low-mass slope.
lgps
log10(phi*).
"""
def __init__(self, name='schecmass'):
self.Mstar = model.Parameter(name, 'Mstar', 10.5, min=9, max=12)
self.alpha = model.Parameter(name, 'alpha', -1.2, min=-2, max=1)
self.lgps = model.Parameter(name, 'lgps', -2, min=-8, max=0)
# model.RegriddableModel1D.__init__(self, name,
model.ArithmeticModel.__init__(self, name,
(self.Mstar, self.alpha, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _schecmass(pars, x)
def _schecmasssq(pars, M):
"""Evaluate a modified Schechter function in log-mass space.
See Yang+2009 eqn 16.
Parameters
----------
pars: sequence of 3 numbers
The order is log Mstar, alpha, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of log masses
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(Mstar, alpha, lgps) = pars
L = 10**(M - Mstar)
phi = ln10 * 10**lgps * L**(1+alpha) * np.exp(-L**2)
return phi
class SchecMassSq(model.ArithmeticModel):
"""A modified Schechter function in log mass space.
The model parameters are:
Mstar
The characteristic log mass.
alpha
The low-mass slope.
lgps
log10(phi*).
"""
def __init__(self, name='schecmasssq'):
self.Mstar = model.Parameter(name, 'Mstar', 10.5, min=9, max=12)
self.alpha = model.Parameter(name, 'alpha', -1.2, min=-2, max=1)
self.lgps = model.Parameter(name, 'lgps', -2, min=-8, max=0)
model.ArithmeticModel.__init__(self, name,
(self.Mstar, self.alpha, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _schecmasssq(pars, x)
def _saundersmass(pars, M):
"""Evaluate Saunders SMF in log-mass space.
See Saunders+1990 eqn 6.1.
Parameters
----------
pars: sequence of 4 numbers
The order is log Mstar, alpha, sigma, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of log masses
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(Mstar, alpha, sigma, lgps) = pars
L = 10**(M - Mstar)
phi = (ln10 * 10**lgps * L**(1+alpha) *
np.exp(-np.log10(1+L)**2/(2.0*sigma**2)))
return phi
class SaundersMass(model.ArithmeticModel):
"""Saunders SMF in log mass space.
The model parameters are:
Mstar
The characteristic log mass.
alpha
The low-mass slope.
sigma
Width of the Gaussian.
lgps
log10(phi*).
"""
def __init__(self, name='saundersmass'):
self.Mstar = model.Parameter(name, 'Mstar', 10.5, min=9, max=12)
self.alpha = model.Parameter(name, 'alpha', -1.2, min=-2, max=1)
self.sigma = model.Parameter(name, 'sigma', 1, min=0.01, max=10)
self.lgps = model.Parameter(name, 'lgps', -2, min=-8, max=0)
model.ArithmeticModel.__init__(
self, name, (self.Mstar, self.alpha, self.sigma, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _saundersmass(pars, x)
def _saundersmag(pars, M):
"""Evaluate Saunders LF in magnitude space.
See Saunders+1990 eqn 6.1.
Parameters
----------
pars: sequence of 4 numbers
The order is log Mstar, alpha, sigma, lgps (log_10 phi*).
These numbers are assumed to be valid.
M: sequence of magnitudes
The grid on which to evaluate the model. It is expected
to be a floating-point type.
Returns
-------
phi: sequence of numbers
The model evaluated on the input grid.
Notes
-----
"""
(Mstar, alpha, sigma, lgps) = pars
L = 10**(0.4*(Mstar - M))
phi = (0.4*ln10 * 10**lgps * L**(1+alpha) *
np.exp(-np.log10(1+L)**2/(2.0*sigma**2)))
return phi
class SaundersMag(model.ArithmeticModel):
"""Saunders LF in log magnitude space.
The model parameters are:
Mstar
The characteristic log mass.
alpha
The low-mass slope.
sigma
Width of the Gaussian.
lgps
log10(phi*).
"""
def __init__(self, name='saundersmag'):
self.Mstar = model.Parameter(name, 'Mstar', -20.2, min=-22, max=-19)
self.alpha = model.Parameter(name, 'alpha', -1.2, min=-2, max=1)
self.sigma = model.Parameter(name, 'sigma', 1, min=0.001, max=10)
self.lgps = model.Parameter(name, 'lgps', -2, min=-8, max=0)
model.ArithmeticModel.__init__(
self, name, (self.Mstar, self.alpha, self.sigma, self.lgps))
def calc(self, pars, x, *args, **kwargs):
"""Evaluate the model"""
return _saundersmag(pars, x)