Physics/Ellipsometry/VASE/TMM.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 136 CC 6 SCAR 17.5
CC
6
Cov
0%
tmm_reflect
12.0
tmm_interface
12.0
psi_delta
6.0
CRAP 5.8
1 package Physics::Ellipsometry::VASE::TMM;
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(tmm_reflect psi_delta tmm_graded tmm_interface);
10  
11 our $VERSION = '1.04';
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 Physics::Ellipsometry::VASE::TMM - Transfer Matrix Method for multilayer
18 thin film optics
19  
20 =head1 SYNOPSIS
21  
22 use PDL;
23 use Physics::Ellipsometry::VASE::TMM qw(tmm_reflect psi_delta
24 tmm_graded tmm_interface);
25 use Physics::Ellipsometry::VASE::Dispersion qw(cauchy_nk);
26  
27 my $lambda = sequence(500) * 2 + 300; # 300–1298 nm
28 my $theta = ones($lambda) * 70; # 70° incidence
29  
30 # --- Single-layer example: SiO2 on Si ---
31  
32 # Ambient (air)
33 my $N_air = ones($lambda) + i() * zeroes($lambda);
34  
35 # SiO2 film (Cauchy model, transparent)
36 my ($n_sio2, $k_sio2) = cauchy_nk($lambda, 1.46, 0.003, 0.0);
37 my $N_sio2 = $n_sio2 + i() * $k_sio2;
38  
39 # Si substrate (tabulated values, simplified here)
40 my $N_si = 3.87 * ones($lambda) + i() * 0.02 * ones($lambda);
41  
42 my ($psi, $delta) = psi_delta(
43 $lambda, $theta,
44 [ $N_air, $N_sio2, $N_si ], # media: ambient, film, substrate
45 [ 100.0 ], # thickness: 100 nm SiO2
46 );
47  
48 print "Psi range: ", $psi->min, " – ", $psi->max, " deg\n";
49 print "Delta range: ", $delta->min, " – ", $delta->max, " deg\n";
50  
51 # --- Raw reflection coefficients ---
52 my ($rp, $rs) = tmm_reflect(
53 $lambda, $theta,
54 [ $N_air, $N_sio2, $N_si ],
55 [ 100.0 ],
56 );
57 my $reflectance_s = abs($rs)**2;
58 my $reflectance_p = abs($rp)**2;
59  
60 =head1 DESCRIPTION
61  
62 Implements the 2×2 transfer matrix method (TMM) for computing the
63 optical response of planar multilayer thin-film stacks. This is the
64 standard algorithm used in spectroscopic ellipsometry to predict
65 Psi (Ψ) and Delta (Δ) from a physical model of the sample.
66  
67 =head2 Physical background
68  
69 When polarised light reflects from a surface, the B<p>-polarised
70 (parallel to the plane of incidence) and B<s>-polarised (perpendicular)
71 components experience different amplitude and phase changes described by
72 the complex Fresnel reflection coefficients C<rp> and C<rs>.
73  
74 Ellipsometry measures the ratio
75  
76 ρ = rp / rs = tan(Ψ) · exp(iΔ)
77  
78 For a simple ambient/substrate interface the Fresnel equations give
79 C<rp> and C<rs> directly. When one or more thin films are present,
80 multiple reflections within each layer create interference effects that
81 modify the overall reflection. The transfer matrix method handles
82 this systematically by representing each layer as a 2×2 matrix and
83 multiplying them together.
84  
85 =head2 Algorithm
86  
87 For a stack of N media (ambient = 0, layers 1..N-2, substrate = N-1):
88  
89 =over 4
90  
91 =item 1.
92  
93 B<Snell's law> determines the complex propagation angle in each layer:
94  
95 N_0 · sin(θ_0) = N_j · sin(θ_j) for all j
96  
97 =item 2.
98  
99 B<Fresnel coefficients> are calculated at every interface for both
100 polarisations.
101  
102 =item 3.
103  
104 Starting from the deepest interface (last layer → substrate), the
105 B<Airy formula> recursively combines each interface reflection with
106 the phase accumulated traversing the layer:
107  
108 r = (r_ij + r_below · e^{+2iβ}) / (1 + r_ij · r_below · e^{+2iβ})
109  
110 where β = (2π/λ) · N_j · d_j · cos(θ_j) is the single-pass phase
111 thickness of layer j.
112  
113 =item 4.
114  
115 The final C<rp> and C<rs> at the ambient surface give Ψ and Δ.
116  
117 =back
118  
119 =head1 CONVENTIONS
120  
121 =over 4
122  
123 =item B<Phase convention>
124  
125 Physics sign convention: e^{−iωt} time dependence, producing
126 e^{+2iβ} phase accumulation for forward propagation through a layer.
127  
128 =item B<Fresnel rp (Verdet convention)>
129  
130 rp = (N_f · cosθ_i − N_i · cosθ_f) / (N_f · cosθ_i + N_i · cosθ_f)
131  
132 This convention yields Δ ≈ 180° for bare dielectrics at moderate angles,
133 consistent with WVASE® and most modern ellipsometry software.
134  
135 =item B<Fresnel rs>
136  
137 rs = (N_i · cosθ_i − N_f · cosθ_f) / (N_i · cosθ_i + N_f · cosθ_f)
138  
139 =item B<Delta mapping>
140  
141 Δ = −arg(ρ) mapped to [0°, 360°)
142  
143 matching the convention used in WVASE® and RefEllips.
144  
145 =back
146  
147 =head1 FUNCTIONS
148  
149 =head2 tmm_reflect
150  
151 my ($rp, $rs) = tmm_reflect($lambda_nm, $theta_deg,
152 \@N_layers, \@d_nm);
153  
154 Calculates the complex Fresnel reflection coefficients for a complete
155 multilayer stack.
156  
157 Arguments:
158  
159 =over 4
160  
161 =item C<$lambda_nm>
162  
163 PDL piddle of wavelengths in nanometres (npts elements).
164  
165 =item C<$theta_deg>
166  
167 PDL piddle of angles of incidence in degrees (same size as C<$lambda_nm>).
168  
169 =item C<\@N_layers>
170  
171 Arrayref of complex refractive index piddles C<[N_0, N_1, ..., N_s]>,
172 one per medium. C<N_0> is the ambient (usually air, n=1) and C<N_s> is
173 the substrate. Each piddle must have the same length as C<$lambda_nm>.
174  
175 =item C<\@d_nm>
176  
177 Arrayref of layer thicknesses in nanometres C<[d_1, d_2, ..., d_{s-1}]>.
178 There is no thickness entry for the ambient or the substrate (semi-infinite
179 half-spaces), so this array has two fewer elements than C<\@N_layers>.
180  
181 =back
182  
183 Returns a list C<($rp, $rs)> of complex PDL piddles.
184  
185 # Two-layer stack: SiO2 (100 nm) + Ta2O5 (50 nm) on Si
186 my ($rp, $rs) = tmm_reflect($lambda, $theta,
187 [ $N_air, $N_ta2o5, $N_sio2, $N_si ],
188 [ 50.0, 100.0 ],
189 );
190  
191 =head2 psi_delta
192  
193 my ($psi, $delta) = psi_delta($lambda_nm, $theta_deg,
194 \@N_layers, \@d_nm,
195 delta_ref => $ref_delta);
196  
197 Convenience wrapper around L</tmm_reflect> that returns the ellipsometric
198 angles Ψ and Δ in degrees.
199  
200 Ψ = atan( |ρ| ) in [0°, 90°]
201 Δ = −arg(ρ) in [0°, 360°)
202  
203 The optional C<delta_ref> parameter provides a reference Δ piddle; the
204 calculated Δ is shifted by multiples of 360° to minimise the distance
205 to the reference, avoiding discontinuities at the 0°/360° boundary
206 during fitting.
207  
208 Returns a list C<($psi_deg, $delta_deg)> of real PDL piddles.
209  
210 # Multi-angle measurement
211 my @angles = (65, 70, 75);
212 for my $ang (@angles) {
213 my $theta = ones($lambda) * $ang;
214 my ($psi, $delta) = psi_delta(
215 $lambda, $theta,
216 [ $N_air, $N_film, $N_sub ],
217 [ $thickness ],
218 );
219 printf "Angle %d: Psi(500nm)=%.2f Delta(500nm)=%.2f\n",
220 $ang, $psi->at(100), $delta->at(100);
221 }
222  
223 =head2 tmm_graded
224  
225 my ($rp, $rs) = tmm_graded($lambda_nm, $theta_deg,
226 $N_ambient, $N_substrate,
227 $n_func, $d_total,
228 n_slices => 20);
229  
230 Models a B<graded layer> whose optical constants vary continuously with
231 depth. The layer is discretized into C<n_slices> sublayers, each with
232 uniform optical constants evaluated at the sublayer midpoint.
233  
234 This approach handles:
235  
236 =over 4
237  
238 =item - Composition gradients (e.g., SiO2 transitioning to Si3N4)
239  
240 =item - Thermal diffusion profiles
241  
242 =item - Ion-implanted layers with depth-dependent damage
243  
244 =back
245  
246 B<Parameters:>
247  
248 =over 4
249  
250 =item C<$n_func> - coderef C<sub($lambda, $z_frac)> returning complex N at
251 fractional depth z (0 = top, 1 = bottom)
252  
253 =item C<$d_total> - total graded layer thickness [nm]
254  
255 =item C<n_slices> - number of sublayers (default 20; increase for accuracy)
256  
257 =back
258  
259 B<Example:>
260  
261 use Physics::Ellipsometry::VASE::TMM qw(tmm_graded);
262 use Physics::Ellipsometry::VASE::Dispersion qw(cauchy_nk);
263  
264 # Linear gradient from SiO2 (n=1.46) to Si3N4 (n=2.0)
265 my $grad_func = sub {
266 my ($lambda, $z) = @_;
267 my $n = 1.46 + 0.54 * $z; # linear gradient
268 return pdl($n) + i() * pdl(0);
269 };
270  
271 my $lambda = sequence(100) * 10 + 300;
272 my ($rp, $rs) = tmm_graded($lambda, pdl(70),
273 pdl(1.0)+i()*pdl(0), # air ambient
274 pdl(3.94)+i()*pdl(0.02), # Si substrate
275 $grad_func, 200, # 200 nm graded layer
276 n_slices => 30,
277 );
278  
279 =head2 tmm_interface
280  
281 my ($rp, $rs) = tmm_interface($lambda_nm, $theta_deg,
282 $N_ambient, $N_substrate,
283 $N_a, $N_b, $d_interface,
284 n_slices => 10,
285 ema_model => 'bruggeman');
286  
287 Models a thin B<interface layer> (interlayer) between two materials
288 using an EMA-graded profile. The composition varies linearly from
289 100% material A at the top to 100% material B at the bottom.
290  
291 B<Physical motivation:> Real interfaces are rarely atomically sharp.
292 Interdiffusion, roughness, and reaction products create a transition
293 region that is better modeled as a graded interlayer than as an
294 abrupt boundary.
295  
296 B<Parameters:>
297  
298 =over 4
299  
300 =item C<$N_a>, C<$N_b> - complex refractive indices of the two materials
301 forming the interface
302  
303 =item C<$d_interface> - interface thickness [nm]
304  
305 =item C<ema_model> - mixing rule: 'bruggeman' (default), 'linear', or 'looyenga'
306  
307 =item C<n_slices> - number of sublayers (default 10)
308  
309 =back
310  
311 B<Example:>
312  
313 use Physics::Ellipsometry::VASE::TMM qw(tmm_interface);
314  
315 my $lambda = sequence(100) * 10 + 300;
316  
317 # Model a 3 nm Si/SiO2 interface transition
318 my ($rp, $rs) = tmm_interface($lambda, pdl(70),
319 pdl(1.0)+i()*pdl(0), # ambient
320 pdl(3.94)+i()*pdl(0.02), # Si substrate
321 pdl(1.46)+i()*pdl(0), # SiO2 side
322 pdl(3.94)+i()*pdl(0.02), # Si side
323 3.0, # 3 nm interface
324 ema_model => 'bruggeman',
325 );
326  
327 =head1 SEE ALSO
328  
329 L<Physics::Ellipsometry::VASE>,
330 L<Physics::Ellipsometry::VASE::Dispersion>,
331 L<Physics::Ellipsometry::VASE::Materials>,
332 L<Physics::Ellipsometry::VASE::EMA>
333  
334 H. Fujiwara, I<Spectroscopic Ellipsometry: Principles and Applications>,
335 John Wiley E<amp> Sons, 2007, Chapter 3.
336  
337 R.M.A. Azzam and N.M. Bashara, I<Ellipsometry and Polarized Light>,
338 North-Holland, 1987.
339  
340 =cut
341  
342 # Calculate reflection coefficients for a multilayer stack
343 # Input:
344 # $lambda_nm - wavelength piddle (npts)
345 # $theta_deg - angle of incidence piddle (npts), in degrees
346 # $N_layers - arrayref of complex refractive index piddles [N0, N1, ..., Ns]
347 # N0 = ambient, Ns = substrate
348 # $d_nm - arrayref of layer thicknesses in nm [d1, d2, ..., d_{s-1}]
349 # (no thickness for ambient or substrate)
350 # Returns: ($rp, $rs) complex reflection coefficients
351 sub tmm_reflect {
352 my ($lambda_nm, $theta_deg, $N_layers, $d_nm) = @_;
353  
354 my $n_media = scalar @$N_layers; # number of media (ambient + layers + substrate)
355 die "Need at least 2 media (ambient + substrate)" if $n_media < 2;
356 die "Need exactly N-2 thicknesses" if scalar @$d_nm != $n_media - 2;
357  
358 my $theta_rad = $theta_deg * (PI / 180.0);
359 my $cos_t0 = cos($theta_rad);
360 my $sin_t0 = sin($theta_rad);
361 my $N0 = $N_layers->[0];
362  
363 # Calculate cos(θ) in each layer via Snell's law
364 my @cos_t;
365 push @cos_t, $cos_t0;
366 for my $j (1 .. $n_media - 1) {
367 my $Nj = $N_layers->[$j];
368 $cos_t[$j] = sqrt(1.0 - ($N0 * $sin_t0)**2 / $Nj**2);
369 }
370  
371 # Build system by nested Airy formula (back-to-front)
372 # Start from the deepest interface and propagate backward
373 my $last = $n_media - 1;
374  
375 # Fresnel coefficients at last interface (layer n-2 → substrate)
376 my $rs = _fresnel_s($N_layers->[$last-1], $N_layers->[$last],
377 $cos_t[$last-1], $cos_t[$last]);
378 my $rp = _fresnel_p($N_layers->[$last-1], $N_layers->[$last],
379 $cos_t[$last-1], $cos_t[$last]);
380  
381 # Propagate backward through each layer
382 for (my $j = $last - 2; $j >= 0; $j--) {
383 # Phase thickness of layer j+1
384 my $d = $d_nm->[$j]; # thickness of layer j+1 (0-indexed in d_nm)
385 my $beta = (2 * PI / $lambda_nm) * $N_layers->[$j+1] * $d * $cos_t[$j+1];
386  
387 # Fresnel at interface j → j+1
388 my $r_s_ij = _fresnel_s($N_layers->[$j], $N_layers->[$j+1],
389 $cos_t[$j], $cos_t[$j+1]);
390 my $r_p_ij = _fresnel_p($N_layers->[$j], $N_layers->[$j+1],
391 $cos_t[$j], $cos_t[$j+1]);
392  
393 # Airy formula: r = (r_ij + r_below·e^{+2iβ}) / (1 + r_ij·r_below·e^{+2iβ})
394 my $phase = exp(2.0 * i() * $beta);
395 $rs = ($r_s_ij + $rs * $phase) / (1.0 + $r_s_ij * $rs * $phase);
396 $rp = ($r_p_ij + $rp * $phase) / (1.0 + $r_p_ij * $rp * $phase);
397 }
398  
399 return ($rp, $rs);
400 }
401  
402 # Calculate Psi and Delta from a layer stack
403 # Returns: ($psi_deg, $delta_deg) with Delta in [0, 360)
404 sub psi_delta {
405 my ($lambda_nm, $theta_deg, $N_layers, $d_nm, %opts) = @_;
406  
407 my ($rp, $rs) = tmm_reflect($lambda_nm, $theta_deg, $N_layers, $d_nm);
408  
409 my $rho = $rp / $rs;
410 my $psi = atan(abs($rho)) * (180.0 / PI);
411  
412 # Delta = -arg(ρ) mapped to [0°, 360°)
413 my $delta_rad = -carg($rho)->re;
414 $delta_rad += 2*PI * ($delta_rad < 0);
415 my $delta = $delta_rad * (180.0 / PI);
416  
417 # Optionally align to reference data (avoid 0/360 wrap)
418 if (my $delta_ref = $opts{delta_ref}) {
419 my $diff = $delta - $delta_ref;
420 $delta -= 360.0 * rint($diff / 360.0);
421 }
422  
423 return ($psi->re->double, $delta->double);
424 }
425  
426 # Fresnel s-polarization: rs = (Ni·cosθi - Nf·cosθf) / (Ni·cosθi + Nf·cosθf)
427 sub _fresnel_s {
428 my ($Ni, $Nf, $cos_ti, $cos_tf) = @_;
429 return ($Ni*$cos_ti - $Nf*$cos_tf) / ($Ni*$cos_ti + $Nf*$cos_tf);
430 }
431  
432 # Fresnel p-polarization (Verdet): rp = (Nf·cosθi - Ni·cosθf) / (Nf·cosθi + Ni·cosθf)
433 sub _fresnel_p {
434 my ($Ni, $Nf, $cos_ti, $cos_tf) = @_;
435 return ($Nf*$cos_ti - $Ni*$cos_tf) / ($Nf*$cos_ti + $Ni*$cos_tf);
436 }
437  
438 # Graded layer: slices a single layer with position-dependent optical
439 # constants into N sublayers and computes TMM for the full stack.
440 # $n_func: coderef ($lambda, $z_frac) -> complex N at fractional depth z
441 # $d_total: total layer thickness [nm]
442 # $n_slices: number of sublayers (default 20)
443 # $N_ambient, $N_substrate: complex refractive indices of bounding media
444 sub tmm_graded {
445 my ($lambda_nm, $theta_deg, $N_ambient, $N_substrate,
446 $n_func, $d_total, %opts) = @_;
447 my $n_slices = $opts{n_slices} // 20;
448  
449 my $d_slice = $d_total / $n_slices;
450  
451 # Build layer stack: [ambient, slice_1, ..., slice_N, substrate]
452 my @N_layers = ($N_ambient);
453 my @d_nm;
454  
455 for my $i (0 .. $n_slices - 1) {
456 my $z_frac = ($i + 0.5) / $n_slices; # midpoint of sublayer
457 my $N_slice = &$n_func($lambda_nm, $z_frac);
458 push @N_layers, $N_slice;
459 push @d_nm, $d_slice;
460 }
461 push @N_layers, $N_substrate;
462  
463 return tmm_reflect($lambda_nm, $theta_deg, \@N_layers, \@d_nm);
464 }
465  
466 # Interface layer model: models a thin intermixed region between two
467 # materials using EMA. Creates a layer with linearly graded composition
468 # from material A to material B.
469 # $N_a, $N_b: complex N of materials above and below the interface
470 # $d_interface: interface thickness [nm]
471 # $ema_model: mixing rule ('bruggeman', 'linear', 'looyenga'), default 'bruggeman'
472 # $n_slices: number of sublayers to represent the grading
473 sub tmm_interface {
474 my ($lambda_nm, $theta_deg, $N_ambient, $N_substrate,
475 $N_a, $N_b, $d_interface, %opts) = @_;
476 my $n_slices = $opts{n_slices} // 10;
477 my $ema_model = $opts{ema_model} // 'bruggeman';
478  
479 require Physics::Ellipsometry::VASE::EMA;
480  
481 my $d_slice = $d_interface / $n_slices;
482 my @N_layers = ($N_ambient);
483 my @d_nm;
484  
485 # Convert N to epsilon for EMA
486 my $eps_a = $N_a**2;
487 my $eps_b = $N_b**2;
488  
489 for my $i (0 .. $n_slices - 1) {
490 my $vf = ($i + 0.5) / $n_slices; # fraction of material B
491  
492 my $eps_eff;
493 if ($ema_model eq 'bruggeman') {
494 $eps_eff = Physics::Ellipsometry::VASE::EMA::ema_bruggeman($eps_a, $eps_b, $vf);
495 } elsif ($ema_model eq 'looyenga') {
496 $eps_eff = Physics::Ellipsometry::VASE::EMA::ema_looyenga($eps_a, $eps_b, $vf);
497 } else {
498 $eps_eff = Physics::Ellipsometry::VASE::EMA::ema_linear($eps_a, $eps_b, $vf);
499 }
500  
501 push @N_layers, sqrt($eps_eff);
502 push @d_nm, $d_slice;
503 }
504 push @N_layers, $N_substrate;
505  
506 return tmm_reflect($lambda_nm, $theta_deg, \@N_layers, \@d_nm);
507 }
508  
509 1;