Skip to main content

pvlib/
iam.rs

1/// ASHRAE Incidence Angle Modifier (IAM) model.
2/// 
3/// `iam = 1 - b0 * (1/cos(aoi) - 1)`
4/// 
5/// # Arguments
6/// * `aoi` - Angle of incidence in degrees.
7/// * `b0` - IAM parameter (typically 0.05).
8/// 
9/// # Returns
10/// IAM multiplier (dimensionless).
11pub fn ashrae(aoi: f64, b0: f64) -> f64 {
12    let aoi_rad = aoi.to_radians();
13    if aoi_rad >= std::f64::consts::PI / 2.0 {
14        return 0.0;
15    }
16    let cos_aoi = aoi_rad.cos();
17    if cos_aoi <= 0.0 {
18        return 0.0;
19    }
20    
21    let iam = 1.0 - b0 * (1.0 / cos_aoi - 1.0);
22    iam.clamp(0.0, 1.0)
23}
24
25/// Martin and Ruiz Incidence Angle Modifier (IAM) model.
26/// 
27/// # Arguments
28/// * `aoi` - Angle of incidence in degrees.
29/// * `a_r` - Angular losses coefficient.
30/// 
31/// # Returns
32/// IAM multiplier (dimensionless).
33pub fn martin_ruiz(aoi: f64, a_r: f64) -> f64 {
34    if aoi >= 90.0 {
35        return 0.0;
36    }
37    let aoi_rad = aoi.to_radians();
38    // IAM = (1 - exp(-cos(aoi) / a_r)) / (1 - exp(-1 / a_r))
39    let numerator = 1.0 - (-aoi_rad.cos() / a_r).exp();
40    let denominator = 1.0 - (-1.0 / a_r).exp();
41    if denominator == 0.0 {
42        return 1.0;
43    }
44    (numerator / denominator).clamp(0.0, 1.0)
45}
46
47/// Physical IAM model using Fresnel/Snell's law.
48///
49/// Calculates transmission through a glass cover accounting for
50/// reflection and absorption losses.
51///
52/// # Arguments
53/// * `aoi` - Angle of incidence in degrees.
54/// * `n` - Effective index of refraction (default 1.526 for glass).
55/// * `k` - Glazing extinction coefficient in 1/meters (default 4.0).
56/// * `l` - Glazing thickness in meters (default 0.002).
57///
58/// # Returns
59/// IAM multiplier (dimensionless).
60pub fn physical(aoi: f64, n: f64, k: f64, l: f64) -> f64 {
61    if aoi.is_nan() {
62        return f64::NAN;
63    }
64    if aoi.abs() >= 90.0 {
65        return 0.0;
66    }
67
68    let aoi_rad = aoi.to_radians();
69    let cos_aoi = aoi_rad.cos().max(0.0);
70    let sin_aoi = (1.0 - cos_aoi * cos_aoi).sqrt();
71
72    // Snell's law: refraction angle
73    let sin_refr = sin_aoi / n;
74    let cos_refr = (1.0 - sin_refr * sin_refr).sqrt();
75
76    // Fresnel reflectance for s and p polarization
77    let n1_cos1 = cos_aoi; // n1=1
78    let n2_cos1 = n * cos_aoi;
79    let n1_cos2 = cos_refr; // n1=1
80    let n2_cos2 = n * cos_refr;
81
82    let rho_s = ((n1_cos1 - n2_cos2) / (n1_cos1 + n2_cos2)).powi(2);
83    let rho_p = ((n1_cos2 - n2_cos1) / (n1_cos2 + n2_cos1)).powi(2);
84    let rho_0 = ((1.0 - n) / (1.0 + n)).powi(2);
85
86    // Transmittance through the interface
87    let tau_s = 1.0 - rho_s;
88    let tau_p = 1.0 - rho_p;
89    let tau_0 = 1.0 - rho_0;
90
91    // Absorption through the glass
92    let tau_s = tau_s * (-k * l / cos_refr).exp();
93    let tau_p = tau_p * (-k * l / cos_refr).exp();
94    let tau_0 = tau_0 * (-k * l).exp();
95
96    // IAM = average of s and p, normalized by normal incidence
97    (tau_s + tau_p) / (2.0 * tau_0)
98}
99
100/// Schlick approximation of Fresnel reflection as IAM.
101///
102/// `iam = 1 - (1 - cos(aoi))^5`
103///
104/// # Arguments
105/// * `aoi` - Angle of incidence in degrees.
106///
107/// # Returns
108/// IAM multiplier (dimensionless).
109pub fn schlick(aoi: f64) -> f64 {
110    if aoi.abs() >= 90.0 {
111        return 0.0;
112    }
113    let cos_aoi = aoi.to_radians().cos();
114    1.0 - (1.0 - cos_aoi).powi(5)
115}
116
117/// Diffuse IAM using the Schlick model (analytically integrated).
118///
119/// Returns (iam_sky, iam_ground) for isotropic diffuse irradiance
120/// on a tilted surface.
121///
122/// # Arguments
123/// * `surface_tilt` - Surface tilt angle in degrees from horizontal.
124///
125/// # Returns
126/// Tuple of (iam_sky, iam_ground).
127pub fn schlick_diffuse(surface_tilt: f64) -> (f64, f64) {
128    let cos_b = surface_tilt.to_radians().cos();
129    let sin_b = surface_tilt.to_radians().sin();
130    let beta_rad = surface_tilt.to_radians();
131    let pi = std::f64::consts::PI;
132
133    // Eq 4 from Xie et al. (2022): relative transmittance of sky diffuse
134    let cuk = (2.0 / (pi * (1.0 + cos_b)))
135        * ((30.0 / 7.0) * pi - (160.0 / 21.0) * beta_rad - (10.0 / 3.0) * pi * cos_b
136            + (160.0 / 21.0) * cos_b * sin_b
137            - (5.0 / 3.0) * pi * cos_b * sin_b.powi(2)
138            + (20.0 / 7.0) * cos_b * sin_b.powi(3)
139            - (5.0 / 16.0) * pi * cos_b * sin_b.powi(4)
140            + (16.0 / 105.0) * cos_b * sin_b.powi(5));
141
142    // Eq 6: relative transmittance of ground-reflected radiation
143    let cug = if surface_tilt < 1e-6 {
144        0.0
145    } else {
146        40.0 / (21.0 * (1.0 - cos_b)) - (1.0 + cos_b) / (1.0 - cos_b) * cuk
147    };
148
149    (cuk, cug)
150}
151
152/// Diffuse IAM using the Martin and Ruiz model.
153///
154/// Returns (iam_sky, iam_ground) for diffuse irradiance.
155///
156/// # Arguments
157/// * `surface_tilt` - Surface tilt angle in degrees from horizontal.
158/// * `a_r` - Angular losses coefficient (default 0.16).
159///
160/// # Returns
161/// Tuple of (iam_sky, iam_ground).
162pub fn martin_ruiz_diffuse(surface_tilt: f64, a_r: f64) -> (f64, f64) {
163    let pi = std::f64::consts::PI;
164
165    // Avoid undefined results for horizontal or upside-down surfaces
166    let tilt = if surface_tilt == 0.0 {
167        1e-6
168    } else if surface_tilt == 180.0 {
169        180.0 - 1e-6
170    } else {
171        surface_tilt
172    };
173
174    let c1 = 4.0 / 3.0 / pi; // 0.4244
175    let c2 = 0.5 * a_r - 0.154;
176
177    let beta = tilt.to_radians();
178    let cos_beta = beta.cos();
179    let sin_beta = if tilt < 90.0 {
180        beta.sin()
181    } else {
182        (pi - beta).sin()
183    };
184
185    let trig_sky = sin_beta + (pi - beta - sin_beta) / (1.0 + cos_beta);
186    let trig_gnd = sin_beta + (beta - sin_beta) / (1.0 - cos_beta);
187
188    let iam_sky = 1.0 - (-(c1 + c2 * trig_sky) * trig_sky / a_r).exp();
189    let iam_gnd = 1.0 - (-(c1 + c2 * trig_gnd) * trig_gnd / a_r).exp();
190
191    (iam_sky, iam_gnd)
192}
193
194/// Sandia Array Performance Model (SAPM) Incidence Angle Modifier.
195/// 
196/// # References
197/// King, D. et al., 2004, "Sandia Photovoltaic Array Performance Model".
198#[allow(clippy::too_many_arguments)]
199pub fn sapm(aoi: f64, b0: f64, b1: f64, b2: f64, b3: f64, b4: f64, b5: f64) -> f64 {
200    if !(0.0..=90.0).contains(&aoi) { return 0.0; }
201    
202    let iam = b0 
203            + b1 * aoi 
204            + b2 * aoi.powi(2) 
205            + b3 * aoi.powi(3) 
206            + b4 * aoi.powi(4) 
207            + b5 * aoi.powi(5);
208            
209    iam.clamp(0.0, 1.0)
210}