Physics/Ellipsometry/VASE/Dispersion.pm untested

filter: Statementstmt 0.0% 0 / 235 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 92 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 425 CC 11 SCAR 19.3
CC
11
Cov
0%
_bspline_basis
20.0
cody_lorentz_nk
12.0
cauchy_nk
6.0
CRAP 6.9
1 package Physics::Ellipsometry::VASE::Dispersion;
2 use strict;
3 use warnings;
4 use PDL;
5 use PDL::NiceSlice;
6 use PDL::Constants qw(PI);
7 use Exporter 'import';
8  
9 our @EXPORT_OK = qw(cauchy_nk sellmeier_nk tauc_lorentz_nk drude_nk genosc_nk
10 drude_lorentz_nk forouhi_bloomer_nk cody_lorentz_nk
11 critical_point_nk gaussian_nk bspline_nk);
12  
13 our $VERSION = '1.04';
14  
15 =head1 NAME
16  
17 Physics::Ellipsometry::VASE::Dispersion - Optical dispersion models for
18 spectroscopic ellipsometry
19  
20 =head1 SYNOPSIS
21  
22 use PDL;
23 use Physics::Ellipsometry::VASE::Dispersion qw(
24 cauchy_nk sellmeier_nk tauc_lorentz_nk drude_nk genosc_nk
25 drude_lorentz_nk forouhi_bloomer_nk cody_lorentz_nk
26 critical_point_nk gaussian_nk bspline_nk
27 );
28  
29 my $lambda = sequence(500) * 2 + 300; # 300-1298 nm
30  
31 # Transparent dielectric (SiO2-like)
32 my ($n, $k) = cauchy_nk($lambda, 1.45, 0.003, 0.0);
33  
34 # Semiconductor with band gap (Ta2O5-like)
35 my ($n2, $k2) = tauc_lorentz_nk($lambda, 100, 4.0, 1.0, 3.5, 1.0);
36  
37 # Metal with interband transitions (Au-like)
38 my ($n3, $k3) = drude_lorentz_nk($lambda, 6.9, 9.03, 0.053,
39 [ [1.3, 2.68, 0.51], [1.0, 3.87, 1.12] ]);
40  
41 # Crystalline semiconductor (Si critical points)
42 my ($n4, $k4) = critical_point_nk($lambda, [
43 { C=>6, E=>3.38, Gamma=>0.08, phi=>-1.2, mu=>0 },
44 ], 1.0);
45  
46 =head1 DESCRIPTION
47  
48 Implements the standard parametric dispersion models used in spectroscopic
49 ellipsometry to describe the wavelength-dependent complex refractive index
50 N = n + ik of thin films and substrates.
51  
52 The complex refractive index relates to the dielectric function through
53  
54 N = sqrt(epsilon) where epsilon = epsilon_1 + i*epsilon_2
55  
56 The real part B<n> (refractive index) governs the phase velocity of light
57 in the material. The imaginary part B<k> (extinction coefficient)
58 describes optical absorption; k=0 for a perfectly transparent material.
59  
60 All functions accept wavelength in nanometres and return a pair of PDL
61 piddles C<($n, $k)>.
62  
63 B<Note:> Functions are called directly (not via closures) to avoid
64 PDL::NiceSlice source filter conflicts with the C<< $ref->() >> syntax.
65  
66 =head1 FUNCTIONS
67  
68 =head2 cauchy_nk
69  
70 my ($n, $k) = cauchy_nk($lambda_nm, $A, $B, $C,
71 k_amp => $k_amp, k_exp => $k_exp);
72  
73 The B<Cauchy model> is an empirical dispersion formula suitable for
74 transparent or weakly absorbing dielectrics in the visible and near-IR.
75 The refractive index is expressed as a power series in inverse
76 wavelength-squared:
77  
78 n(lambda) = A + B/lambda^2 + C/lambda^4
79  
80 where lambda is in micrometres. Parameters B and C are in units of
81 um^2 and um^4, respectively.
82  
83 When C<k_amp> is set, an B<Urbach absorption tail> is added:
84  
85 k(lambda) = k_amp * exp( k_exp * (1/lambda - 1/0.4) )
86  
87 modelling the exponential increase in absorption near the band edge.
88 If C<k_amp> is zero (the default) the material is treated as fully
89 transparent (k = 0).
90  
91 B<Typical use:> SiO2, Al2O3, MgF2, polymer films.
92  
93 # SiO2 in the visible
94 my ($n, $k) = cauchy_nk($lambda, 1.45, 0.003, 0.0);
95  
96 # Polymer with weak UV absorption tail
97 my ($n, $k) = cauchy_nk($lambda, 1.50, 0.005, 0.0,
98 k_amp => 0.01, k_exp => 2.0);
99  
100 =head2 sellmeier_nk
101  
102 my ($n, $k) = sellmeier_nk($lambda_nm, \@B_terms, \@C_terms);
103  
104 The B<Sellmeier equation> is derived from a classical harmonic-oscillator
105 model of the dielectric response. Each resonance contributes a term to
106 the squared refractive index:
107  
108 n^2(lambda) = 1 + sum_i B_i * lambda^2 / (lambda^2 - C_i)
109  
110 where lambda is in micrometres. C<C_i> values (um^2) locate the
111 resonance wavelengths (lambda_i = sqrt(C_i)), and C<B_i> values are the
112 oscillator strengths. The model is valid away from the resonances and
113 does not predict absorption (k = 0).
114  
115 B<Typical use:> Optical glasses (BK7, fused silica), crystals (CaF2,
116 sapphire), infrared windows.
117  
118 # BK7 glass (Schott catalogue coefficients)
119 my ($n, $k) = sellmeier_nk($lambda,
120 [1.0396, 0.2318, 1.0105], # B terms
121 [0.00600, 0.02002, 103.56], # C terms (um^2)
122 );
123  
124 =head2 tauc_lorentz_nk
125  
126 my ($n, $k) = tauc_lorentz_nk($lambda_nm, $A, $E0, $Gamma, $Eg,
127 $eps_inf);
128  
129 The B<Tauc-Lorentz model> (Jellison E<amp> Modine, I<Appl. Phys. Lett.>
130 B<69>, 371, 1996) is widely used for amorphous semiconductors and
131 dielectrics. It combines the Tauc joint density of states with a
132 Lorentz oscillator to give the imaginary dielectric function:
133  
134 epsilon_2(E) = [ A * E0 * Gamma * (E - Eg)^2 ]
135 / [ (E^2 - E0^2)^2 + Gamma^2 * E^2 ] / E
136 for E > Eg; 0 otherwise
137  
138 The real part epsilon_1 is obtained through a B<numerical Kramers-Kronig>
139 integration to ensure the model is physically self-consistent (causal).
140  
141 Parameters:
142  
143 =over 4
144  
145 =item C<$A> - oscillator amplitude (eV)
146  
147 =item C<$E0> - peak transition energy (eV)
148  
149 =item C<$Gamma> - oscillator broadening (eV)
150  
151 =item C<$Eg> - optical band gap energy (eV); epsilon_2 = 0 below this energy
152  
153 =item C<$eps_inf> - high-frequency dielectric constant (accounts for higher-energy transitions)
154  
155 =back
156  
157 B<Typical use:> Ta2O5, SiNx, TiO2, a-Si, amorphous oxides.
158  
159 # Ta2O5 thin film
160 my ($n, $k) = tauc_lorentz_nk($lambda, 100, 4.5, 1.2, 3.8, 1.0);
161  
162 # Amorphous silicon
163 my ($n, $k) = tauc_lorentz_nk($lambda, 200, 3.4, 2.5, 1.2, 1.0);
164  
165 =head2 drude_nk
166  
167 my ($n, $k) = drude_nk($lambda_nm, $eps_inf, $omega_p, $gamma);
168  
169 The B<Drude free-electron model> describes optical properties of metals
170 and heavily doped semiconductors. Free carriers produce a dielectric
171 response:
172  
173 epsilon(E) = eps_inf - omega_p^2 / (E^2 + i * E * gamma)
174  
175 where C<omega_p> is the plasma frequency (eV) and C<gamma> is the
176 carrier scattering rate (eV). Below the plasma frequency the real part
177 of epsilon goes negative, producing the high reflectivity characteristic
178 of metals.
179  
180 B<Typical use:> Au, Ag, Al, Cu, ITO, doped ZnO.
181  
182 # Gold (simplified)
183 my ($n, $k) = drude_nk($lambda, 1.0, 9.0, 0.07);
184  
185 # ITO (transparent conductor)
186 my ($n, $k) = drude_nk($lambda, 3.8, 1.5, 0.12);
187  
188 =head2 genosc_nk
189  
190 my ($n, $k) = genosc_nk($lambda_nm, \@oscillators, $eps_inf);
191  
192 The B<general oscillator> (GenOsc) model sums multiple Lorentz
193 oscillators to build an arbitrary dielectric function:
194  
195 epsilon(E) = eps_inf + sum_i A_i * E0_i^2
196 / (E0_i^2 - E^2 - i * Gamma_i * E)
197  
198 Each oscillator is specified as C<[A, E0, Gamma]> (amplitude, centre
199 energy in eV, broadening in eV). This is the most flexible built-in
200 model and can approximate complex band structures with multiple
201 absorption features.
202  
203 B<Typical use:> Compound semiconductors, organic films, metamaterials,
204 any material with multiple absorption bands.
205  
206 # Two-oscillator model for a compound semiconductor
207 my ($n, $k) = genosc_nk($lambda,
208 [ [80, 3.5, 0.5], [40, 5.0, 1.0] ], # oscillators
209 1.5, # eps_inf
210 );
211  
212 =head1 ENERGY-WAVELENGTH CONVERSION
213  
214 Several models (Tauc-Lorentz, Drude, GenOsc) work internally in photon
215 energy. The conversion used is:
216  
217 E (eV) = 1239.842 / lambda (nm)
218  
219 Parameters for these models (E0, Eg, Gamma, omega_p) are in eV, while
220 the input wavelength and returned n, k are in nm.
221  
222 =head2 drude_lorentz_nk
223  
224 my ($n, $k) = drude_lorentz_nk($lambda_nm, $eps_inf, $omega_p,
225 $gamma_d, $oscillators);
226  
227 The B<Drude-Lorentz hybrid> combines free-carrier (Drude) response with
228 bound-electron (Lorentz) interband transitions. The dielectric function is:
229  
230 epsilon(E) = eps_inf - omega_p^2/(E^2 + i*E*Gamma_d)
231 + sum_j A_j*E0_j^2 / (E0_j^2 - E^2 - i*Gamma_j*E)
232  
233 The first term describes metallic free carriers (plasma frequency C<omega_p>,
234 damping C<gamma_d>). The oscillator terms model interband absorption peaks.
235  
236 B<Parameters:>
237  
238 =over 4
239  
240 =item C<$eps_inf> - high-frequency dielectric constant (default 1.0)
241  
242 =item C<$omega_p> - plasma frequency [eV] (default 9.0)
243  
244 =item C<$gamma_d> - Drude damping rate [eV] (default 0.05)
245  
246 =item C<$oscillators> - arrayref of [A, E0, Gamma] for interband peaks
247  
248 =back
249  
250 B<Typical use:> Noble metals (Au, Ag, Cu), transparent conducting oxides
251 (ITO, ZnO:Al), transition metals.
252  
253 # Gold: Drude + 2 interband transitions
254 my ($n, $k) = drude_lorentz_nk($lambda,
255 6.9, # eps_inf
256 9.03, # omega_p [eV]
257 0.053, # Drude damping [eV]
258 [ [1.3, 2.68, 0.51], # L1 interband
259 [1.0, 3.87, 1.12] ], # L2 interband
260 );
261  
262 # ITO (transparent conductor)
263 my ($n, $k) = drude_lorentz_nk($lambda,
264 3.8, 1.8, 0.12, # lower omega_p than pure metal
265 [ [0.5, 4.2, 0.8] ], # UV absorption
266 );
267  
268 =head2 forouhi_bloomer_nk
269  
270 my ($n, $k) = forouhi_bloomer_nk($lambda_nm, \%params);
271  
272 The B<Forouhi-Bloomer model> (1986, 1988) provides an analytic form for both
273 k(E) and n(E) in amorphous semiconductors and dielectrics. Unlike
274 Tauc-Lorentz, the refractive index is derived analytically from
275 Kramers-Kronig without numerical integration:
276  
277 k(E) = A*(E - Eg)^2 / (E^2 - B*E + C) for E > Eg
278 n(E) = n_inf + (B0*E + C0) / (E^2 - B*E + C)
279  
280 where B0 and C0 are derived analytically from A, B, C, and Eg.
281  
282 B<Parameters (as hashref):>
283  
284 =over 4
285  
286 =item C<A> - absorption strength (default 0.5)
287  
288 =item C<B> - peak energy parameter [eV] (default 6.0)
289  
290 =item C<C> - peak width parameter [eV^2] (default 10.0); must satisfy 4C > B^2
291  
292 =item C<Eg> - optical bandgap [eV] (default 1.5)
293  
294 =item C<n_inf> - refractive index at infinite energy (default 1.5)
295  
296 =back
297  
298 B<Typical use:> a-Si:H, a-SiN, a-SiO2, amorphous carbon.
299  
300 # Amorphous silicon
301 my ($n, $k) = forouhi_bloomer_nk($lambda, {
302 A => 0.76, B => 7.53, C => 14.6,
303 Eg => 1.12, n_inf => 2.03,
304 });
305  
306 =head2 cody_lorentz_nk
307  
308 my ($n, $k) = cody_lorentz_nk($lambda_nm,
309 A => $A, E0 => $E0, Gamma => $Gamma,
310 Eg => $Eg, Ep => $Ep, Eu => $Eu,
311 eps_inf => $eps_inf);
312  
313 The B<Cody-Lorentz model> (Ferlauto et al., 2002) extends the Tauc-Lorentz
314 model with two improvements:
315  
316 =over 4
317  
318 =item 1. Replaces the Tauc absorption onset with the Cody form, which
319 better describes the near-gap absorption in amorphous materials.
320  
321 =item 2. Adds an exponential Urbach tail below the band edge to model
322 disorder-induced sub-gap absorption.
323  
324 =back
325  
326 The imaginary dielectric function:
327  
328 eps2(E) = G*L(E) * (E-Eg)^2 / ((E-Eg)^2 + Ep^2) for E > Et
329 eps2(E) = eps2(Et) * exp((E - Et)/Eu) for Eg < E <= Et
330  
331 where Et = Eg + Ep is the transition energy, G = A*E0*Gamma, and
332 L(E) is the Lorentz oscillator.
333  
334 B<Parameters:>
335  
336 =over 4
337  
338 =item C<A> - amplitude [eV] (default 100)
339  
340 =item C<E0> - resonance energy [eV] (default 4.0)
341  
342 =item C<Gamma> - broadening [eV] (default 1.0)
343  
344 =item C<Eg> - Cody optical gap [eV] (default 1.5)
345  
346 =item C<Ep> - transition energy width [eV] (default 1.0)
347  
348 =item C<Eu> - Urbach energy [eV] (default 0.05)
349  
350 =item C<eps_inf> - high-frequency offset (default 1.0)
351  
352 =back
353  
354 B<Typical use:> a-Si:H, amorphous chalcogenides (GeSbTe), polymer films.
355  
356 # a-Si:H with Urbach tail
357 my ($n, $k) = cody_lorentz_nk($lambda,
358 A => 200, E0 => 3.6, Gamma => 2.5,
359 Eg => 1.7, Ep => 0.8, Eu => 0.05,
360 eps_inf => 1.0,
361 );
362  
363 =head2 critical_point_nk
364  
365 my ($n, $k) = critical_point_nk($lambda_nm, \@critical_points, $eps_inf);
366  
367 The B<Critical Point model> (Adachi, 1987; Kim et al., 1992) describes the
368 dielectric function of crystalline semiconductors near van Hove
369 singularities (critical points in the joint density of states):
370  
371 epsilon(E) = eps_inf + sum_j C_j*exp(i*phi_j)*(E - E_j + i*Gamma_j)^(-mu_j)
372  
373 The exponent C<mu> determines the type of critical point:
374  
375 =over 4
376  
377 =item mu = 0.5 - 3D M0 (band edge, e.g., E0 of GaAs)
378  
379 =item mu = -0.5 - 3D M1 (saddle point)
380  
381 =item mu = 0 - 2D (logarithmic singularity, e.g., E1 of Si)
382  
383 =back
384  
385 B<Parameters:>
386  
387 Each critical point is a hashref with keys:
388  
389 =over 4
390  
391 =item C<C> - amplitude
392  
393 =item C<E> - critical point energy [eV]
394  
395 =item C<Gamma> - broadening [eV]
396  
397 =item C<phi> - phase angle [radians]
398  
399 =item C<mu> - exponent (0.5, -0.5, or 0)
400  
401 =back
402  
403 B<Typical use:> c-Si, GaAs, InP, Ge, III-V semiconductors.
404  
405 # Crystalline silicon (E1, E2 critical points)
406 my ($n, $k) = critical_point_nk($lambda, [
407 { C => 6.0, E => 3.38, Gamma => 0.08, phi => -1.2, mu => 0 }, # E1
408 { C => 3.0, E => 3.62, Gamma => 0.12, phi => -0.8, mu => 0 }, # E1'
409 { C => 20.0, E => 4.27, Gamma => 0.05, phi => -0.3, mu => 0.5 }, # E2
410 ], 1.0);
411  
412 =head2 gaussian_nk
413  
414 my ($n, $k) = gaussian_nk($lambda_nm, \@oscillators, $eps_inf);
415  
416 B<Gaussian oscillators> model absorption peaks with a Gaussian line shape
417 instead of the Lorentzian used in C<genosc_nk>. Gaussian profiles provide
418 better fits for inhomogeneously broadened transitions (e.g., amorphous
419 materials, organic films):
420  
421 eps2(E) = sum_j A_j * [ exp(-((E-E0j)/sigma_j)^2)
422 - exp(-((E+E0j)/sigma_j)^2) ]
423  
424 where sigma_j = Gamma_j / (2*sqrt(ln 2)) is the standard deviation
425 (Gamma_j is the FWHM). eps1 is obtained by numerical Kramers-Kronig
426 transformation.
427  
428 B<Parameters:>
429  
430 =over 4
431  
432 =item C<$oscillators> - arrayref of [A, E0, Gamma] triplets
433  
434 =item C<$eps_inf> - high-frequency dielectric constant (default 1.0)
435  
436 =back
437  
438 B<Typical use:> Organic semiconductors, polymers, dye films, biological
439 thin films, inhomogeneously broadened systems.
440  
441 # Organic semiconductor with two absorption bands
442 my ($n, $k) = gaussian_nk($lambda, [
443 [3.0, 2.8, 0.4], # HOMO-LUMO transition
444 [1.5, 4.5, 0.8], # higher excited state
445 ], 2.5);
446  
447 =head2 bspline_nk
448  
449 my ($n, $k) = bspline_nk($lambda_nm, \@knots, \@coeffs, $eps_inf);
450  
451 B<B-spline parameterization> provides a model-free (non-parametric)
452 description of the imaginary dielectric function eps2(E) using cubic
453 B-spline basis functions. The real part eps1(E) is derived from
454 numerical Kramers-Kronig to ensure causality.
455  
456 This approach is useful when:
457  
458 =over 4
459  
460 =item - No physical model adequately describes the material
461  
462 =item - You need maximum flexibility for exploratory fitting
463  
464 =item - The material has complex spectral features (multiple overlapping peaks)
465  
466 =back
467  
468 B<Parameters:>
469  
470 =over 4
471  
472 =item C<$knots> - arrayref of energy knot positions [eV] (must be sorted)
473  
474 =item C<$coeffs> - arrayref of B-spline coefficients (non-negative for physical eps2)
475  
476 =item C<$eps_inf> - eps1 offset (default 1.0)
477  
478 =back
479  
480 B<Typical use:> Unknown materials, complex alloys, metamaterials.
481  
482 # 7 knots spanning 1-6 eV with an absorption peak near 4 eV
483 my ($n, $k) = bspline_nk($lambda,
484 [1.0, 2.0, 3.0, 4.0, 5.0, 5.5, 6.0], # knots
485 [0.0, 0.1, 0.5, 2.0, 1.0, 0.3, 0.0], # coefficients
486 2.0, # eps_inf
487 );
488  
489 =head1 SEE ALSO
490  
491 L<Physics::Ellipsometry::VASE>,
492 L<Physics::Ellipsometry::VASE::TMM>,
493 L<Physics::Ellipsometry::VASE::Materials>
494  
495 G.E. Jellison and F.A. Modine, "Parameterization of the optical functions
496 of amorphous materials in the interband region", I<Appl. Phys. Lett.>
497 B<69>, 371 (1996).
498  
499 H. Fujiwara, I<Spectroscopic Ellipsometry: Principles and Applications>,
500 John Wiley E<amp> Sons, 2007, Chapters 5-6.
501  
502 =cut
503  
504 # Cauchy dispersion: n(λ) = A + B/λ² + C/λ⁴, k=0 (or Urbach tail)
505 # $lambda in nm, B in µm², C in µm⁴
506 sub cauchy_nk {
507 my ($lambda_nm, $A, $B, $C, %opts) = @_;
508 $A //= 2.0;
509 $B //= 0.01;
510 $C //= 0.0;
511 my $k_amp = $opts{k_amp} // 0;
512 my $k_exp = $opts{k_exp} // 0;
513  
514 my $lam_um = $lambda_nm / 1000.0;
515 my $n = $A + $B / $lam_um**2 + $C / $lam_um**4;
516 my $k;
517 if ($k_amp > 0) {
518 $k = $k_amp * exp($k_exp * (1.0/$lam_um - 1.0/0.4));
519 } else {
520 $k = zeroes($lambda_nm);
521 }
522 return ($n, $k);
523 }
524  
525 # Sellmeier dispersion: n²(λ) = 1 + Σ Bᵢλ²/(λ² - Cᵢ)
526 # $B_terms, $C_terms are arrayrefs; C in µm²
527 sub sellmeier_nk {
528 my ($lambda_nm, $B_terms, $C_terms) = @_;
529 $B_terms //= [1.0];
530 $C_terms //= [0.01];
531  
532 my $lam_um_sq = ($lambda_nm / 1000.0)**2;
533 my $n_sq = ones($lambda_nm) + 0.0;
534 for my $i (0 .. $#$B_terms) {
535 $n_sq += $B_terms->[$i] * $lam_um_sq / ($lam_um_sq - $C_terms->[$i]);
536 }
537 $n_sq = $n_sq->clip(0.01, 1e6);
538 my $n = sqrt($n_sq);
539 my $k = zeroes($lambda_nm);
540 return ($n, $k);
541 }
542  
543 # Tauc-Lorentz dispersion (Jellison & Modine, 1996)
544 sub tauc_lorentz_nk {
545 my ($lambda_nm, $A, $E0, $Gamma, $Eg, $eps_inf) = @_;
546 $A //= 100;
547 $E0 //= 4.0;
548 $Gamma //= 1.0;
549 $Eg //= 3.5;
550 $eps_inf //= 1.0;
551  
552 my $E = 1239.842 / $lambda_nm;
553  
554 # ε₂(E): Tauc-Lorentz imaginary part
555 my $eps2 = zeroes($lambda_nm);
556 my $above_gap = which($E > $Eg);
557 if ($above_gap->nelem > 0) {
558 my $Ea = $E->index($above_gap);
559 my $numer = $A * $E0 * $Gamma * ($Ea - $Eg)**2;
560 my $denom = (($Ea**2 - $E0**2)**2 + $Gamma**2 * $Ea**2) * $Ea;
561 $eps2->index($above_gap) .= $numer / $denom;
562 }
563  
564 # ε₁(E): numerical Kramers-Kronig
565 my $eps1 = _kk_transform($E, $eps2) + $eps_inf;
566  
567 my $eps = $eps1 + i() * $eps2;
568 my $N = sqrt($eps);
569 return ($N->re, $N->im->abs);
570 }
571  
572 # Drude model for metals: ε(E) = ε_inf - ωp²/(E² + iEΓ)
573 sub drude_nk {
574 my ($lambda_nm, $eps_inf, $omega_p, $gamma) = @_;
575 $eps_inf //= 1.0;
576 $omega_p //= 10.0;
577 $gamma //= 0.1;
578  
579 my $E = 1239.842 / $lambda_nm;
580 my $eps = $eps_inf - $omega_p**2 / ($E**2 + i() * $E * $gamma);
581 my $N = sqrt($eps);
582 return ($N->re, $N->im->abs);
583 }
584  
585 # General oscillator: sum of Lorentz oscillators
586 # $oscillators: arrayref of [A, E0, Gamma] triplets
587 sub genosc_nk {
588 my ($lambda_nm, $oscillators, $eps_inf) = @_;
589 $oscillators //= [];
590 $eps_inf //= 1.0;
591  
592 my $E = 1239.842 / $lambda_nm;
593 my $eps = ones($lambda_nm) * $eps_inf + i() * zeroes($lambda_nm);
594  
595 for my $osc (@$oscillators) {
596 my ($Ai, $E0i, $Gi) = @$osc;
597 $eps += $Ai * $E0i**2 / ($E0i**2 - $E**2 - i() * $Gi * $E);
598 }
599  
600 my $N = sqrt($eps);
601 return ($N->re, $N->im->abs);
602 }
603  
604 # Numerical Kramers-Kronig transform
605 sub _kk_transform {
606 my ($E, $eps2) = @_;
607 my $npts = $E->nelem;
608 my $eps1 = zeroes($npts);
609  
610 for my $j (0 .. $npts - 1) {
611 my $Ej = $E->at($j);
612 my $mask = abs($E - $Ej) > 0.001;
613 my $idx = which($mask);
614 next if $idx->nelem < 2;
615 my $E_prime = $E->index($idx);
616 my $eps2_p = $eps2->index($idx);
617 my $integrand = $E_prime * $eps2_p / ($E_prime**2 - $Ej**2);
618 my $dE = $E_prime->(1:) - $E_prime->(0:-2);
619 my $avg = ($integrand->(1:) + $integrand->(0:-2)) / 2;
620 $eps1->set($j, (2.0 / PI) * sum($dE * $avg)->sclr);
621 }
622 return $eps1;
623 }
624  
625 # Drude-Lorentz hybrid: Drude free-electron + Lorentz interband oscillators
626 # Combines metallic free-carrier response with bound-electron transitions
627 sub drude_lorentz_nk {
628 my ($lambda_nm, $eps_inf, $omega_p, $gamma_d, $oscillators) = @_;
629 $eps_inf //= 1.0;
630 $omega_p //= 9.0; # plasma frequency [eV]
631 $gamma_d //= 0.05; # Drude damping [eV]
632 $oscillators //= []; # arrayref of [A, E0, Gamma] for interband
633  
634 my $E = 1239.842 / $lambda_nm;
635  
636 # Drude (intraband free carriers)
637 my $eps = $eps_inf * ones($lambda_nm) - $omega_p**2 / ($E**2 + i() * $E * $gamma_d);
638  
639 # Lorentz oscillators (interband transitions)
640 for my $osc (@$oscillators) {
641 my ($Ai, $E0i, $Gi) = @$osc;
642 $eps += $Ai * $E0i**2 / ($E0i**2 - $E**2 - i() * $Gi * $E);
643 }
644  
645 my $N = sqrt($eps);
646 return ($N->re, $N->im->abs);
647 }
648  
649 # Forouhi-Bloomer model (1986, 1988)
650 # For amorphous semiconductors and dielectrics
651 # k(E) = A(E-Eg)^2 / (E^2 - BE + C) for E > Eg, else 0
652 # n(E) = n_inf + (B0*E + C0) / (E^2 - BE + C)
653 # where B0 and C0 are derived from A, B, C, Eg via KK analytically
654 sub forouhi_bloomer_nk {
655 my ($lambda_nm, $params) = @_;
656 $params //= {};
657 my $A = $params->{A} // 0.5;
658 my $B = $params->{B} // 6.0;
659 my $C = $params->{C} // 10.0;
660 my $Eg = $params->{Eg} // 1.5;
661 my $n_inf = $params->{n_inf} // 1.5;
662  
663 my $E = 1239.842 / $lambda_nm;
664  
665 # Extinction coefficient
666 my $k = zeroes($lambda_nm);
667 my $above = which($E > $Eg);
668 if ($above->nelem > 0) {
669 my $Ea = $E->index($above);
670 $k->index($above) .= $A * ($Ea - $Eg)**2 / ($Ea**2 - $B * $Ea + $C);
671 }
672 $k = $k->clip(0, 1e6);
673  
674 # Analytic KK-derived refractive index
675 my $Q = sqrt(4 * $C - $B**2) / 2.0;
676 my $B0 = $A / $Q * (-$B**2 / 2 + $Eg * $B - $Eg**2 + $C);
677 my $C0 = $A / $Q * (($B**2 / 2 - $C) * $Eg + $B * $C / 2 - $Eg**2 * $B / 2);
678  
679 my $n = $n_inf + ($B0 * $E + $C0) / ($E**2 - $B * $E + $C);
680  
681 return ($n, $k);
682 }
683  
684 # Cody-Lorentz model (Ferlauto et al., J. Appl. Phys. 92, 2424, 2002)
685 # Improved Tauc-Lorentz: adds Urbach tail below gap + Cody absorption shape
686 # eps2(E) = (E1/E) * exp((E - Et)/Eu) for E <= Et
687 # eps2(E) = (G * (E - Eg)^2) / ((E^2 - E0^2)^2 + G^2*E^2) * (E - Eg)^2 / E for E > Et
688 # G = A*E0*Gamma, Et = transition energy, Eu = Urbach energy
689 sub cody_lorentz_nk {
690 my ($lambda_nm, %params) = @_;
691 my $A = $params{A} // 100;
692 my $E0 = $params{E0} // 4.0;
693 my $Gamma = $params{Gamma} // 1.0;
694 my $Eg = $params{Eg} // 1.5;
695 my $Ep = $params{Ep} // 1.0; # transition width
696 my $Eu = $params{Eu} // 0.05; # Urbach energy [eV]
697 my $eps_inf = $params{eps_inf} // 1.0;
698  
699 my $E = 1239.842 / $lambda_nm;
700 my $eps2 = zeroes($lambda_nm);
701  
702 # Transition energy Et
703 my $Et = $Eg + $Ep;
704  
705 # Lorentz oscillator part for E > Et
706 my $above_et = which($E > $Et);
707 if ($above_et->nelem > 0) {
708 my $Ea = $E->index($above_et);
709 my $G = $A * $E0 * $Gamma;
710 my $lorentz = $G / (($Ea**2 - $E0**2)**2 + $Gamma**2 * $Ea**2);
711 my $cody = ($Ea - $Eg)**2 / (($Ea - $Eg)**2 + $Ep**2);
712 $eps2->index($above_et) .= $lorentz * $cody * $Ea;
713 }
714  
715 # Urbach tail for Eg < E <= Et
716 my $urbach_region = which(($E > $Eg) & ($E <= $Et));
717 if ($urbach_region->nelem > 0) {
718 my $Eu_e = $E->index($urbach_region);
719 # Match value at Et
720 my $G = $A * $E0 * $Gamma;
721 my $l_at_et = $G / (($Et**2 - $E0**2)**2 + $Gamma**2 * $Et**2);
722 my $c_at_et = ($Et - $Eg)**2 / (($Et - $Eg)**2 + $Ep**2);
723 my $eps2_et = $l_at_et * $c_at_et * $Et;
724 $eps2->index($urbach_region) .= $eps2_et * exp(($Eu_e - $Et) / $Eu);
725 }
726  
727 # KK transform for eps1
728 my $eps1 = _kk_transform($E, $eps2) + $eps_inf;
729  
730 my $eps = $eps1 + i() * $eps2;
731 my $N = sqrt($eps);
732 return ($N->re, $N->im->abs);
733 }
734  
735 # Critical Point model (Adachi / Kim et al.)
736 # Models van Hove singularities in crystalline semiconductors
737 # eps(E) = sum_j C_j * exp(i*phi_j) * (E - E_j + i*Gamma_j)^(-mu_j)
738 # mu = 0.5 for 3D M0 CP, -0.5 for 3D M1, 0 for 2D (logarithmic)
739 sub critical_point_nk {
740 my ($lambda_nm, $cps, $eps_inf) = @_;
741 $cps //= []; # arrayref of {C, E, Gamma, phi, mu}
742 $eps_inf //= 1.0;
743  
744 my $E = 1239.842 / $lambda_nm;
745 my $eps = $eps_inf * ones($lambda_nm) + i() * zeroes($lambda_nm);
746  
747 for my $cp (@$cps) {
748 my $C = $cp->{C} // 1.0;
749 my $Ecp = $cp->{E} // 3.4;
750 my $Gamma = $cp->{Gamma} // 0.1;
751 my $phi = $cp->{phi} // 0.0; # phase [radians]
752 my $mu = $cp->{mu} // 0.5; # exponent
753  
754 my $phase_factor = cos($phi) + i() * sin($phi);
755 my $z = $E - $Ecp + i() * $Gamma;
756  
757 if (abs($mu) < 0.01) {
758 # Logarithmic (2D) critical point: C * exp(i*phi) * ln(z)
759 # ln(complex) = ln|z| + i*arg(z)
760 my $ln_z = log(abs($z)) + i() * carg($z);
761 $eps += $C * $phase_factor * $ln_z;
762 } else {
763 # Power-law: C * exp(i*phi) * z^(-mu)
764 # z^(-mu) = exp(-mu * ln(z)) = |z|^(-mu) * exp(-i*mu*arg(z))
765 my $mag = abs($z)**(-$mu);
766 my $arg_part = -$mu * carg($z);
767 my $z_pow = $mag * (cos($arg_part) + i() * sin($arg_part));
768 $eps += $C * $phase_factor * $z_pow;
769 }
770 }
771  
772 my $N = sqrt($eps);
773 return ($N->re, $N->im->abs);
774 }
775  
776 # Gaussian oscillator model
777 # eps(E) = sum_j sigma_j(E) + i * eps2_j(E)
778 # eps2(E) = A * exp(-((E-E0)/(sigma))^2) - A * exp(-((E+E0)/(sigma))^2)
779 # eps1 from numerical KK
780 # sigma = Gamma / (2*sqrt(ln(2)))
781 sub gaussian_nk {
782 my ($lambda_nm, $oscillators, $eps_inf) = @_;
783 $oscillators //= []; # arrayref of [A, E0, Gamma] triplets
784 $eps_inf //= 1.0;
785  
786 my $E = 1239.842 / $lambda_nm;
787 my $eps2 = zeroes($lambda_nm);
788  
789 for my $osc (@$oscillators) {
790 my ($Ai, $E0i, $Gi) = @$osc;
791 my $sigma = $Gi / (2.0 * sqrt(log(2.0)));
792 # Gaussian line shape (positive + negative frequency contributions)
793 $eps2 += $Ai * exp(-(($E - $E0i) / $sigma)**2)
794 - $Ai * exp(-(($E + $E0i) / $sigma)**2);
795 }
796 $eps2 = $eps2->clip(0, 1e6);
797  
798 # KK transform for eps1
799 my $eps1 = _kk_transform($E, $eps2) + $eps_inf;
800  
801 my $eps = $eps1 + i() * $eps2;
802 my $N = sqrt($eps);
803 return ($N->re, $N->im->abs);
804 }
805  
806 # B-spline parameterized optical constants
807 # Uses cubic B-spline basis functions on a knot vector for eps2(E),
808 # then derives eps1 from numerical KK. Allows model-free fitting.
809 # $knots: arrayref of energy knot positions [eV]
810 # $coeffs: arrayref of B-spline coefficients for eps2
811 sub bspline_nk {
812 my ($lambda_nm, $knots, $coeffs, $eps_inf) = @_;
813 $knots //= [1.0, 2.0, 3.0, 4.0, 5.0];
814 $coeffs //= [0.0, 0.5, 1.0, 0.5, 0.0];
815 $eps_inf //= 1.0;
816  
817 my $E = 1239.842 / $lambda_nm;
818 my $eps2 = zeroes($lambda_nm);
819  
820 # Evaluate cubic B-spline
821 my $nknots = scalar @$knots;
822 my $ncoeffs = scalar @$coeffs;
823  
824 # Augmented knot vector for cubic (order 4)
825 my @aug = (($knots->[0]) x 3, @$knots, ($knots->[-1]) x 3);
826  
827 for my $i (0 .. $ncoeffs - 1) {
828 my $basis = _bspline_basis(3, $i, \@aug, $E);
829 $eps2 += $coeffs->[$i] * $basis;
830 }
831 $eps2 = $eps2->clip(0, 1e6);
832  
833 # KK for eps1
834 my $eps1 = _kk_transform($E, $eps2) + $eps_inf;
835  
836 my $eps = $eps1 + i() * $eps2;
837 my $N = sqrt($eps);
838 return ($N->re, $N->im->abs);
839 }
840  
841 # Recursive B-spline basis function evaluation (Cox-de Boor)
842 sub _bspline_basis {
843 my ($degree, $i, $knots, $E) = @_;
844  
845 if ($degree == 0) {
846 # Indicator: 1 where knot[i] <= E < knot[i+1]
847 return (($E >= $knots->[$i]) & ($E < $knots->[$i+1]))->double;
848 }
849  
850 my $left = zeroes($E);
851 my $right = zeroes($E);
852  
853 my $denom_l = $knots->[$i + $degree] - $knots->[$i];
854 if ($denom_l > 1e-10) {
855 $left = ($E - $knots->[$i]) / $denom_l
856 * _bspline_basis($degree - 1, $i, $knots, $E);
857 }
858  
859 my $denom_r = $knots->[$i + $degree + 1] - $knots->[$i + 1];
860 if ($denom_r > 1e-10) {
861 $right = ($knots->[$i + $degree + 1] - $E) / $denom_r
862 * _bspline_basis($degree - 1, $i + 1, $knots, $E);
863 }
864  
865 return $left + $right;
866 }
867  
868 1;