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
//!
//! # Planck laws & black body
//! 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

use super::constant as cst;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// # Frequency Planck's law
/// ## Definition
/// This computes the following equation:
/// $$
/// B_\nu(\nu,T) = \frac{2h\nu^3}{c^2} \frac{1}{\exp\left(\frac{h\nu}{k_BT}\right) - 1}
/// $$
/// Where $h$ is the Planck constant, $c$ is the speed of light and $k_B$ is the Boltzmann constant.
/// 
/// ## Inputs
/// - `temperature`: temperature of the black body ($T$), in kelvin ($K$)
/// - `nu`: the frequency to compute ($\nu$), in meters ($m$)
/// 
/// Returns the corresponding irradiance.
/// 
/// ## Example
/// ```
/// # use scilib::planck;
/// let res = planck::frequency(5700.0, 650e12);
/// assert!((res - 1.708e-8).abs() < 1.0e-10);
/// ```
pub fn frequency(temperature: f64, nu: f64) -> f64 {

    let factor: f64 = 2.0 * nu.powi(3) * cst::H / cst::C.powi(2);

    factor / ((cst::H * nu / (cst::K_B * temperature)).exp() - 1.0)
}

/// # Frequency Planck's law (spectrum)
/// ## Definition
/// This computes the following equation:
/// $$
/// B_\nu(\nu,T) = \frac{2h\nu^3}{c^2} \frac{1}{\exp\left(\frac{h\nu}{k_BT}\right) - 1}
/// $$
/// Where $h$ is the Planck constant, $c$ is the speed of light and $k_B$ is the Boltzmann constant.
/// 
/// ## Inputs
/// - `temperature`: temperature of the black body ($T$), in kelvin ($K$)
/// - `nu`: the frequencies to compute ($\nu$), in meters ($m$)
/// 
/// Returns the corresponding irradiances.
/// 
/// ## Example
/// ```
/// # use scilib::range;
/// # use scilib::planck;
/// let freq = range::linear(0.0, 2000e12, 15);
/// let temp: f64 = 5700.0;
/// let res = planck::frequency_vec(temp, &freq);
/// assert!((res[8] - 1.458e-9).abs() < 1.0e-10);
/// ```
pub fn frequency_vec(temperature: f64, nu: &[f64]) -> Vec<f64> {

    let factor: f64 = 2.0 * cst::H / cst::C.powi(2);
    let pre: f64 = cst::H / (cst::K_B * temperature);

    nu.iter().map(|val| factor * val.powi(3) / ((pre * val).exp() - 1.0) ).collect()
}

/// # Wavelength Planck's law
/// ## Definition
/// This computes the following equation:
/// $$
/// B_\lambda(\lambda,T) = \frac{2hc^2}{\lambda^5} \frac{1}{\exp\left(\frac{hc}{\lambda k_BT}\right) - 1}
/// $$
/// Where $h$ is the Planck constant, $c$ is the speed of light and $k_B$ is the Boltzmann constant.
/// 
/// ## Inputs
/// - `temperature`: temperature of the black body ($T$), in kelvin ($K$)
/// - `lambda`: the wavelength to compute ($\lambda$), in meters ($m$)
/// 
/// Returns the corresponding irradiance.
/// 
/// ## Example
/// ```
/// # use scilib::planck;
/// let res = planck::wavelength(5700.0, 0.4e-6);
/// assert!((res - 2.118e13).abs() <= 1.0e10);
/// ```
pub fn wavelength(temperature: f64, lambda: f64) -> f64 {

    let factor: f64 = 2.0 * cst::H * cst::C.powi(2) / lambda.powi(5);

    factor / ((cst::H * cst::C / (lambda * cst::K_B * temperature)).exp() - 1.0)
}

/// # Wavelength Planck's law (spectrum)
/// ## Definition
/// This computes the following equation:
/// $$
/// B_\lambda(\lambda,T) = \frac{2hc^2}{\lambda^5} \frac{1}{\exp\left(\frac{hc}{\lambda k_BT}\right) - 1}
/// $$
/// Where $h$ is the Planck constant, $c$ is the speed of light and $k_B$ is the Boltzmann constant.
/// 
/// ## Inputs
/// - `temperature`: temperature of the black body ($T$), in kelvin ($K$)
/// - `lambda`: the wavelengths to compute ($\lambda$), in meters ($m$)
/// 
/// Returns the corresponding irradiances.
/// 
/// ```
/// # use scilib::range;
/// # use scilib::planck;
/// let wave = range::linear(0.0, 5e-6, 15);
/// let temp: f64 = 5700.0;
/// let res = planck::wavelength_vec(temp, &wave);
/// assert!((res[8] - 4.408e11).abs() < 1.0e8);
/// ```
pub fn wavelength_vec(temperature: f64, lambda: &[f64]) -> Vec<f64> {

    let factor: f64 = 2.0 * cst::H * cst::C.powi(2);
    let pre: f64 = cst::H * cst::C / (cst::K_B * temperature);

    lambda.iter().map(|val| factor / (val.powi(5) * ( (pre / val).exp() - 1.0 )) ).collect()
}

/// # Wavenumber Planck's law
/// ## Definition
/// This computes the following equation:
/// $$
/// B_{\tilde{n}}(\tilde{n},T) = 2hc^2\tilde{n}^3 \frac{1}{\exp\left(\frac{hc\tilde{n}}{k_BT}\right) - 1}
/// $$
/// Where $h$ is the Planck constant, $c$ is the speed of light and $k_B$ is the Boltzmann constant.
/// 
/// ## Inputs
/// - `temperature`: temperature of the black body ($T$), in kelvin ($K$)
/// - `number`: the wavenumber to compute ($\tilde{n}$), in 1/meters ($m^{-1}$)
/// 
/// Returns the corresponding irradiance.
/// 
/// ## Example
pub fn wavenumber(temperature: f64, number: f64) -> f64 {

    let factor: f64 = 2.0 * cst::H * cst::C.powi(2) * number.powi(3);

    factor / ((cst::H * cst::C * number / (cst::K_B * temperature)).exp() - 1.0)
}

/// # Wavenumber Planck's law (spectrum)
/// ## Definition
/// This computes the following equation:
/// $$
/// B_{\tilde{n}}(\tilde{n},T) = 2hc^2\tilde{n}^3 \frac{1}{\exp\left(\frac{hc\tilde{n}}{k_BT}\right) - 1}
/// $$
/// Where $h$ is the Planck constant, $c$ is the speed of light and $k_B$ is the Boltzmann constant.
/// 
/// ## Inputs
/// - `temperature`: temperature of the black body ($T$), in kelvin ($K$)
/// - `number`: the wavenumbers to compute ($\tilde{n}$), in 1/meters ($m^{-1}$)
/// 
/// Returns the corresponding irradiances.
/// 
/// ## Example
pub fn wavenumber_vec(temperature: f64, number: &[f64]) -> Vec<f64> {

    let factor: f64 = 2.0 * cst::H * cst::C.powi(2);
    let pre: f64 = cst::H * cst::C / (cst::K_B * temperature);

    number.iter().map(|val| factor * val.powi(3) / ( (pre * val).exp() - 1.0) ).collect()
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// # Maximum emission of a black body (wavelength)
/// ## Definition
/// This is Wien's displacement law, applied to wavelength:
/// $$
/// \lambda_\mathrm{peak} = \frac{b}{T}
/// $$
/// Where $b$ is Wien's constant and $T$ is the temperature of the black body.
/// 
/// ## Inputs
/// - `temperature`: the temperature of the black body ($T$), in kelvin ($K$)
/// 
/// Returns the wavelength peak of the black body.
/// 
/// ## Example
/// ```
/// # use scilib::planck::peak_wave;
/// # use scilib::constant;
/// let sun_max = peak_wave(constant::SUN_TEFF);    // Max is greenish
/// assert!((sun_max - 5.0203949324324324e-7).abs() < 1.0e-16);
/// ```
pub fn peak_wave(temperature: f64) -> f64 {
    cst::WIEN_B / temperature
}

/// # Maximum emission of a black body (frequency)
/// ## Definition
/// This is Wien's displacement law, applied to frequency:
/// $$
/// \nu_\mathrm{peak} = b'T
/// $$
/// Where $b'$ is Wien's constant for frequency and $T$ is the temperature of the black body.
/// 
/// ## Inputs
/// - `temperature`: the temperature of the black body ($T$), in kelvin ($K$)
/// 
/// Returns the frequency peak of the black body.
/// 
/// ## Example
/// ```
/// # use scilib::planck::peak_freq;
/// # use scilib::constant;
/// let sun_max = peak_freq(constant::SUN_TEFF);    // Max is greenish
/// assert!((sun_max - 339331594694040.0).abs() < 1.0e-16);
/// ```
pub fn peak_freq(temperature: f64) -> f64 {
    cst::WIEN_B_FREQ * temperature
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////