Passed
Push — master ( b71750...7ebf62 )
by Stefan
01:58
created

eppaurora.spectra.pflux_pow()   A

Complexity

Conditions 1

Size

Total Lines 30
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 4
dl 0
loc 30
rs 10
c 0
b 0
f 0
1
# coding: utf-8
2
# Copyright (c) 2020 Stefan Bender
3
#
4
# This file is part of pyeppaurora.
5
# pyeppaurora is free software: you can redistribute it or modify
6
# it under the terms of the GNU General Public License as published
7
# by the Free Software Foundation, version 2.
8
# See accompanying LICENSE file or http://www.gnu.org/licenses/gpl-2.0.html.
9
"""Particle precipitation spectra
10
11
Includes variants describing a normalized particle flux,
12
as well as variants describing a normalized energy flux.
13
"""
14
15
import numpy as np
16
17
__all__ = [
18
	"exp_general",
19
	"gaussian_general",
20
	"maxwell_general",
21
	"pow_general",
22
	"pflux_exp",
23
	"pflux_gaussian",
24
	"pflux_maxwell",
25
	"pflux_pow",
26
]
27
28
29
# General normalized spectra, standard distributions
30
def exp_general(en, en_0=10.):
31
	r"""Exponential number flux spectrum as in Aksnes et al., 2006 [0]
32
33
	Defined according to Aksnes et al., JGR 2006, Eq. (1),
34
	normalized to unit number flux, i.e.
35
	:math:`\int_0^\infty \phi(E) \text{d}E = 1`.
36
37
	Parameters
38
	----------
39
	en: float or array_like (N,)
40
		Energy in [keV]
41
	en_0: float, optional
42
		Characteristic energy in [keV] of the distribution.
43
		Default: 10 keV
44
45
	Returns
46
	-------
47
	phi: float or array_like (N,)
48
		Normalized differential hemispherical number flux at `en` in [keV-1 cm-2 s-1]
49
		([keV] or scaled by 1 keV-2 cm-2 s-1, e.g. ).
50
	"""
51
	return 1. / en_0 * np.exp(-en / en_0)
52
53
54
def gaussian_general(en, en_0=10., w=1.):
55
	r"""Gaussian number flux spectrum as in Fang2008 [1]
56
57
	Standard normal distribution with mu = en_0 and sigma = w / sqrt(2)
58
	for use in Fang et al., JGR 2008, Eq. (1).
59
	Almost normalized to unit number flux, i.e.
60
	:math:`\int_0^\infty \phi(E) \text{d}E = 1`
61
	(ignoring the negative tail).
62
63
	Parameters
64
	----------
65
	en: float or array_like (N,)
66
		Energy in [keV]
67
	en_0: float, optional
68
		Characteristic energy in [keV], i.e. mode of the distribution.
69
		Default: 10 keV
70
	w: float, optional
71
		Width of the Gaussian distribution, in [keV].
72
73
	Returns
74
	-------
75
	phi: float or array_like (N,)
76
		Normalized differential hemispherical number flux at `en` in [keV-1 cm-2 s-1]
77
		([keV] or scaled by 1 keV-2 cm-2 s-1, e.g.).
78
	"""
79
	return 1. / np.sqrt(np.pi * w**2) * np.exp(-(en - en_0)**2 / w**2)
80
81
82
def maxwell_general(en, en_0=10.):
83
	r"""Maxwell number flux spectrum as in Fang2008 [1]
84
85
	Defined in Fang et al., JGR 2008, Eq. (1),
86
	normalized to :math:`\int_0^\infty \phi(E) \text{d}E = 1`.
87
88
	Parameters
89
	----------
90
	en: float or array_like (N,)
91
		Energy in [keV]
92
	en_0: float, optional
93
		Characteristic energy in [keV], i.e. mode of the distribution.
94
		Default: 10 keV
95
96
	Returns
97
	-------
98
	phi: float or array_like (N,)
99
		Normalized differential hemispherical number flux at `en` in [keV-1 cm-2 s-1]
100
		([keV] or scaled by 1 keV-2 cm-2 s-1, e.g.).
101
	"""
102
	return en / en_0**2 * np.exp(-en / en_0)
103
104
105 View Code Duplication
def pow_general(en, en_0=10., gamma=-3., het=True):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
106
	r"""Power-law number flux spectrum as in Strickland1993 [3]
107
108
	Defined e.g. in Strickland et al., 1993,
109
	normalized to unit particle flux:
110
	:math:`\int_{E_0}^\infty \phi(E) \text{d}E = 1`
111
	for the high-energy tail version, and
112
	:math:`\int_0^{E_0} \phi(E) \text{d}E = 1`
113
	for the low-energy tail version.
114
115
	Parameters
116
	----------
117
	en: float or array_like (N,)
118
		Energy in [keV]
119
	en_0: float, optional
120
		Characteristic energy in [keV], i.e. mode of the distribution.
121
		Default: 10 keV
122
	gamma: float, optional
123
		Exponent of the power-law distribution, in [keV].
124
	het: bool, optional
125
		Return a high-energy tail (het, default: true) for en > en_0,
126
		or low-energy tail (false) for en < en_0.
127
		Adjusts the normalization accordingly.
128
129
	Returns
130
	-------
131
	phi: float or array_like (N,)
132
		Normalized differential hemispherical number flux at `en` in [keV-1 cm-2 s-1]
133
		([keV] or scaled by 1 keV-2 cm-2 s-1, e.g.).
134
	"""
135
	spec = (gamma + 1) / en_0 * (en / en_0)**gamma
136
	if het:
137
		spec[en < en_0] = 0.
138
		return -spec
139
	spec[en > en_0] = 0.
140
	return spec
141
142
143
def pflux_exp(en, en_0=10.):
144
	r"""Exponential particle flux spectrum
145
146
	Normalized to unit energy flux:
147
	:math:`\int_0^\infty \phi(E) E \text{d}E = 1`.
148
149
	Parameters
150
	----------
151
	en: float or array_like (N,)
152
		Energy in [keV]
153
	en_0: float, optional
154
		Characteristic energy in [keV], i.e. mode of the distribution.
155
		Default: 10 keV.
156
157
	Returns
158
	-------
159
	phi: float or array_like (N,)
160
		Hemispherical differential particle flux at `en` in [keV-1 cm-2 s-1]
161
		([kev-2] scaled by unit energy flux).
162
	"""
163
	return exp_general(en, en_0=en_0) / en_0
164
165
166
def pflux_gaussian(en, en_0=10., w=1):
167
	r"""Gaussian particle flux spectrum
168
169
	Defined in Fang et al., JGR 2008, Eq. (1).
170
171
	Normalized to :math:`\int_0^\infty \phi(E) E \text{d}E = 1`.
172
	(ignoring the negative tail).
173
	Parameters
174
	----------
175
	en: float or array_like (N,)
176
		Energy in [keV]
177
	en_0: float, optional
178
		Characteristic energy in [keV], i.e. mode of the distribution.
179
		Default: 10 keV.
180
181
	Returns
182
	-------
183
	phi: float or array_like (N,)
184
		Hemispherical differential particle flux at `en` in [keV-1 cm-2 s-1]
185
		([kev-2] scaled by unit energy flux).
186
	"""
187
	return gaussian_general(en, en_0=en_0, w=w) / en_0
188
189
190
def pflux_maxwell(en, en_0=10.):
191
	r"""Maxwell particle flux spectrum as in Fang2008 [1]
192
193
	Defined in Fang et al., JGR 2008, Eq. (1).
194
	The total precipitating energy flux is fixed to 1 keV cm-2 s-1,
195
	multiply by Q_0 [keV cm-2 s-1] to scale the particle flux.
196
197
	Normalized to :math:`\int_0^\infty \phi(E) E \text{d}E = 1`.
198
199
	Parameters
200
	----------
201
	en: float or array_like (N,)
202
		Energy in [keV]
203
	en_0: float, optional
204
		Characteristic energy in [keV], i.e. mode of the distribution.
205
		Default: 10 keV.
206
207
	Returns
208
	-------
209
	phi: float or array_like (N,)
210
		Hemispherical differential particle flux at `en` in [keV-1 cm-2 s-1]
211
		([kev-2] scaled by unit energy flux).
212
	"""
213
	return 0.5 / en_0 * maxwell_general(en, en_0)
214
215
216
def pflux_pow(en, en_0=10., gamma=-3., het=True):
217
	r"""Power-law particle flux spectrum
218
219
	Defined e.g. in Strickland et al., 1993.
220
	Normalized to :math:`\int_{E_0}^\infty \phi(E) E \text{d}E = 1`
221
	for the high-energy tail version, and to
222
	:math:`\int_0^{E_0} \phi(E) E \text{d}E = 1`
223
	for the low-energy tail version.
224
225
	Parameters
226
	----------
227
	en: float or array_like (N,)
228
		Energy in [keV]
229
	en_0: float, optional
230
		Characteristic energy in [keV], i.e. mode of the distribution.
231
		Default: 10 keV
232
	gamma: float, optional
233
		Exponent of the power-law distribution, in [keV].
234
	het: bool, optional (default True)
235
		Return a high-energy tail (true) for en > en_0,
236
		or low-energy tail (false) for en < en_0.
237
		Adjusts the normalization accordingly.
238
239
	Returns
240
	-------
241
	phi: float or array_like (N,)
242
		Hemispherical differential particle flux at `en` in [keV-1 cm-2 s-1]
243
		([kev-2] scaled by unit energy flux).
244
	"""
245
	return (gamma + 2) / (gamma + 1) / en_0 * pow_general(en, en_0=en_0, gamma=gamma, het=het)
246