Physics/Ellipsometry/VASE/Anisotropy.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 58 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 271 CC 9 SCAR 24.2
CC
9
Cov
0%
_to_mc
30.0
berreman_4x4
12.0
uniaxial_epsilon
6.0
CRAP 11.3
1 package Physics::Ellipsometry::VASE::Anisotropy;
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(uniaxial_epsilon biaxial_epsilon tensor_epsilon berreman_4x4);
10  
11 our $VERSION = '1.04';
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 Physics::Ellipsometry::VASE::Anisotropy - Anisotropic optical models
18 and 4×4 Berreman transfer matrix
19  
20 =head1 SYNOPSIS
21  
22 use PDL;
23 use Physics::Ellipsometry::VASE::Anisotropy qw(
24 uniaxial_epsilon biaxial_epsilon tensor_epsilon berreman_4x4
25 );
26  
27 my $lambda = sequence(100) * 10 + 300;
28  
29 # Uniaxial crystal (e.g., sapphire, quartz, TiO2 rutile)
30 my $eps_o = pdl(3.1) + i() * pdl(0.0); # ordinary
31 my $eps_e = pdl(3.4) + i() * pdl(0.0); # extraordinary
32 my $tensor = uniaxial_epsilon($eps_o, $eps_e, theta => 45);
33  
34 # Compute Psi/Delta for anisotropic film
35 my $result = berreman_4x4($lambda, 70, $tensor, 100,
36 1.0, pdl(3.94)+i()*pdl(0.02));
37 my $psi = $result->{psi};
38 my $delta = $result->{delta};
39  
40 =head1 DESCRIPTION
41  
42 This module provides tools for modeling optically anisotropic thin films
43 where the dielectric response depends on the direction of the electric
44 field. In isotropic media, the permittivity is a scalar; in anisotropic
45 media it becomes a 3×3 tensor:
46  
47 D = epsilon_0 * epsilon_tensor * E
48  
49 The module supports three levels of anisotropy:
50  
51 =over 4
52  
53 =item B<Uniaxial> - Two independent constants (ordinary and extraordinary).
54 The material has one optic axis. Examples: sapphire, quartz, TiO2,
55 calcite, liquid crystals.
56  
57 =item B<Biaxial> - Three independent constants along three principal axes.
58 Examples: mica, aragonite, orthorhombic crystals, organic films.
59  
60 =item B<General tensor> - Arbitrary 3×3 complex tensor with off-diagonal
61 elements. Required for magneto-optic materials, tilted crystals, or
62 materials under stress.
63  
64 =back
65  
66 The B<Berreman 4×4 formalism> extends the standard 2×2 transfer matrix
67 to handle anisotropic layers. Instead of treating s- and p-polarizations
68 independently, it uses a 4-component field vector [Ex, Hy, Ey, -Hx] and
69 4×4 transfer matrices. This naturally handles polarization conversion
70 (rps, rsp ≠ 0) that occurs in anisotropic systems.
71  
72 =head1 FUNCTIONS
73  
74 =head2 uniaxial_epsilon
75  
76 my $tensor = uniaxial_epsilon($eps_ordinary, $eps_extraordinary,
77 theta => $tilt_deg,
78 phi => $azimuth_deg);
79  
80 Constructs the dielectric tensor for a uniaxial crystal. In the
81 principal frame:
82  
83 epsilon = diag(eps_o, eps_o, eps_e)
84  
85 Optional rotation parameters tilt the optic axis (c-axis) away from the
86 surface normal:
87  
88 =over 4
89  
90 =item C<theta> - tilt angle of c-axis from surface normal [deg] (default 0)
91  
92 =item C<phi> - azimuthal rotation about surface normal [deg] (default 0)
93  
94 =back
95  
96 Returns a hashref C<{xx, yy, zz, xy, xz, yz}> representing the tensor
97 in the laboratory frame.
98  
99 # TiO2 rutile, c-axis perpendicular to surface
100 my $t = uniaxial_epsilon(pdl(6.0)+i()*0, pdl(8.4)+i()*0);
101  
102 # Same crystal, c-axis tilted 30° from normal
103 my $t = uniaxial_epsilon(pdl(6.0)+i()*0, pdl(8.4)+i()*0, theta=>30);
104  
105 =head2 biaxial_epsilon
106  
107 my $tensor = biaxial_epsilon($eps_x, $eps_y, $eps_z,
108 phi => $phi, theta => $theta, psi => $psi);
109  
110 Constructs the dielectric tensor for a biaxial crystal with three
111 distinct principal values. Optional ZYZ Euler angles rotate from the
112 crystal frame to the laboratory frame.
113  
114 # Mica (orthorhombic)
115 my $t = biaxial_epsilon(
116 pdl(2.56)+i()*0, # eps_x
117 pdl(2.62)+i()*0, # eps_y
118 pdl(2.60)+i()*0, # eps_z
119 );
120  
121 =head2 tensor_epsilon
122  
123 my $tensor = tensor_epsilon(
124 xx => $exx, yy => $eyy, zz => $ezz,
125 xy => $exy, xz => $exz, yz => $eyz,
126 );
127  
128 Constructs a general 3×3 dielectric tensor from explicit components.
129 Off-diagonal elements enable modeling of magneto-optic effects, chirality,
130 or non-reciprocal media. The tensor is assumed symmetric unless
131 separate yx, zx, zy components are provided.
132  
133 =head2 berreman_4x4
134  
135 my $result = berreman_4x4($lambda_nm, $theta_deg,
136 $eps_tensor, $d_nm,
137 $N_ambient, $N_substrate);
138  
139 Calculates the reflection of a single anisotropic layer on an isotropic
140 substrate using the 4×4 Berreman transfer matrix method.
141  
142 B<Returns:> hashref with keys:
143  
144 =over 4
145  
146 =item C<rpp> - p-to-p reflection coefficient (complex PDL)
147  
148 =item C<rss> - s-to-s reflection coefficient (complex PDL)
149  
150 =item C<rps> - s-to-p cross-polarization (complex PDL)
151  
152 =item C<rsp> - p-to-s cross-polarization (complex PDL)
153  
154 =item C<psi> - standard ellipsometric Ψ [degrees]
155  
156 =item C<delta> - standard ellipsometric Δ [degrees]
157  
158 =back
159  
160 B<Example — liquid crystal cell:>
161  
162 my $lambda = sequence(200) * 5 + 400;
163 my $eps_o = pdl(2.25) + i()*pdl(0);
164 my $eps_e = pdl(3.06) + i()*pdl(0);
165  
166 # LC director tilted 45° from normal
167 my $tensor = uniaxial_epsilon($eps_o, $eps_e, theta => 45);
168  
169 my $r = berreman_4x4($lambda, 60, $tensor, 5000,
170 1.0, pdl(1.52)+i()*pdl(0));
171 printf "Psi at 600nm: %.2f deg\n", $r->{psi}->at(40);
172 printf "Cross-pol |rps| at 600nm: %.4f\n", abs($r->{rps})->at(40);
173  
174 =head1 SEE ALSO
175  
176 L<Physics::Ellipsometry::VASE::TMM>,
177 L<Physics::Ellipsometry::VASE::MagnetoOptic>,
178 L<Physics::Ellipsometry::VASE::Dispersion>
179  
180 M. Schubert, "Polarization-dependent optical parameters of arbitrarily
181 anisotropic homogeneous layered systems", I<Phys. Rev. B> B<53>, 4265 (1996).
182  
183 D.W. Berreman, "Optics in Stratified and Anisotropic Media: 4×4-Matrix
184 Formulation", I<J. Opt. Soc. Am.> B<62>, 502 (1972).
185  
186 =cut
187  
188 # Uniaxial dielectric tensor
189 # Ordinary (in-plane) and extraordinary (out-of-plane) dielectric constants
190 # Returns a 3x3 tensor for each wavelength point:
191 # diag(eps_o, eps_o, eps_e) in the principal axis frame
192 # $eps_o: ordinary dielectric function (complex PDL)
193 # $eps_e: extraordinary dielectric function (complex PDL)
194 # $euler_angles: optional [phi, theta, psi] rotation from crystal to lab frame
195 sub uniaxial_epsilon {
196 my ($eps_o, $eps_e, %opts) = @_;
197 my $phi = $opts{phi} // 0; # azimuthal rotation [deg]
198 my $theta = $opts{theta} // 0; # tilt angle [deg]
199  
200 # In principal frame: eps = diag(eps_o, eps_o, eps_e)
201 # If no rotation, return simple hash representation
202 if ($theta == 0 && $phi == 0) {
203 return {
204 xx => $eps_o, yy => $eps_o, zz => $eps_e,
205 xy => 0*$eps_o, xz => 0*$eps_o, yz => 0*$eps_o,
206 };
207 }
208  
209 # Apply rotation: eps_lab = R * eps_diag * R^T
210 my $th = $theta * PI / 180;
211 my $ph = $phi * PI / 180;
212  
213 my $ct = cos($th); my $st = sin($th);
214 my $cp = cos($ph); my $sp = sin($ph);
215  
216 # For c-axis tilted by theta in the xz-plane, rotated by phi about z:
217 my $eps_xx = $eps_o * ($cp**2 * $ct**2 + $sp**2) + $eps_e * $cp**2 * $st**2;
218 my $eps_yy = $eps_o * ($sp**2 * $ct**2 + $cp**2) + $eps_e * $sp**2 * $st**2;
219 my $eps_zz = $eps_o * $st**2 + $eps_e * $ct**2;
220 my $eps_xy = ($eps_o * $ct**2 - $eps_o + $eps_e * $st**2) * $cp * $sp * 0;
221 # Simplified: for tilt in xz plane only
222 $eps_xy = ($eps_e - $eps_o) * $st**2 * $cp * $sp;
223 my $eps_xz = ($eps_e - $eps_o) * $st * $ct * $cp;
224 my $eps_yz = ($eps_e - $eps_o) * $st * $ct * $sp;
225  
226 return {
227 xx => $eps_xx, yy => $eps_yy, zz => $eps_zz,
228 xy => $eps_xy, xz => $eps_xz, yz => $eps_yz,
229 };
230 }
231  
232 # Biaxial dielectric tensor
233 # Three distinct principal dielectric constants along x, y, z axes
234 # Returns tensor in lab frame after optional Euler rotation
235 sub biaxial_epsilon {
236 my ($eps_x, $eps_y, $eps_z, %opts) = @_;
237 my $phi = $opts{phi} // 0;
238 my $theta = $opts{theta} // 0;
239 my $psi_r = $opts{psi} // 0;
240  
241 if ($phi == 0 && $theta == 0 && $psi_r == 0) {
242 return {
243 xx => $eps_x, yy => $eps_y, zz => $eps_z,
244 xy => 0*$eps_x, xz => 0*$eps_x, yz => 0*$eps_x,
245 };
246 }
247  
248 # General Euler rotation ZYZ convention
249 my $a = $phi * PI / 180;
250 my $b = $theta * PI / 180;
251 my $c = $psi_r * PI / 180;
252  
253 # Rotation matrix elements (ZYZ Euler)
254 my $ca = cos($a); my $sa = sin($a);
255 my $cb = cos($b); my $sb = sin($b);
256 my $cc = cos($c); my $sc = sin($c);
257  
258 # R = Rz(phi) * Ry(theta) * Rz(psi)
259 my @R = (
260 [$ca*$cb*$cc - $sa*$sc, -$ca*$cb*$sc - $sa*$cc, $ca*$sb],
261 [$sa*$cb*$cc + $ca*$sc, -$sa*$cb*$sc + $ca*$cc, $sa*$sb],
262 [-$sb*$cc, $sb*$sc, $cb ],
263 );
264  
265 # eps_lab = R * diag(eps_x, eps_y, eps_z) * R^T
266 my @eps_diag = ($eps_x, $eps_y, $eps_z);
267 my %result;
268 my @labels = qw(xx yy zz xy xz yz);
269 my @idx = ([0,0],[1,1],[2,2],[0,1],[0,2],[1,2]);
270  
271 for my $l (0 .. $#labels) {
272 my ($i, $j) = @{$idx[$l]};
273 my $val = 0 * $eps_x; # initialize with correct shape
274 for my $k (0 .. 2) {
275 $val = $val + $R[$i][$k] * $R[$j][$k] * $eps_diag[$k];
276 }
277 $result{$labels[$l]} = $val;
278 }
279  
280 return \%result;
281 }
282  
283 # General 3x3 complex dielectric tensor
284 # Accepts all 9 (or 6 unique symmetric) components directly
285 sub tensor_epsilon {
286 my (%components) = @_;
287 # Ensure symmetric: xy=yx, xz=zx, yz=zy
288 return {
289 xx => $components{xx},
290 yy => $components{yy},
291 zz => $components{zz},
292 xy => $components{xy} // $components{yx} // 0,
293 xz => $components{xz} // $components{zx} // 0,
294 yz => $components{yz} // $components{zy} // 0,
295 yx => $components{yx} // $components{xy} // 0,
296 zx => $components{zx} // $components{xz} // 0,
297 zy => $components{zy} // $components{yz} // 0,
298 };
299 }
300  
301 # Berreman 4x4 transfer matrix for anisotropic layers
302 # Computes rpp, rss, rps, rsp for a single anisotropic layer on an
303 # isotropic substrate. Uses the Berreman formalism where the field
304 # vector is [Ex, Hy, Ey, -Hx].
305 #
306 # $lambda_nm: wavelength [nm] (PDL)
307 # $theta_deg: angle of incidence [deg] (PDL or scalar)
308 # $eps_tensor: hashref {xx, yy, zz, xy, xz, yz} (each a complex PDL)
309 # $d_nm: layer thickness [nm]
310 # $N_ambient: ambient refractive index (real scalar or PDL)
311 # $N_substrate: substrate complex refractive index (PDL)
312 # Returns: hashref {rpp, rss, rps, rsp, psi, delta}
313 sub berreman_4x4 {
314 my ($lambda_nm, $theta_deg, $eps_tensor, $d_nm, $N_ambient, $N_substrate) = @_;
315  
316 require Math::Complex;
317  
318 my $npts = $lambda_nm->nelem;
319 my $theta_rad_val = ref($theta_deg) ? $theta_deg->at(0) : $theta_deg;
320 $theta_rad_val *= PI / 180.0;
321 my $Na_val = ref($N_ambient) ? $N_ambient->at(0) : $N_ambient;
322 my $kxi = $Na_val * sin($theta_rad_val);
323  
324 # Results stored as real/imag pairs
325 my @rpp_re; my @rpp_im;
326 my @rss_re; my @rss_im;
327 my @rps_re; my @rps_im;
328 my @rsp_re; my @rsp_im;
329  
330 for my $wi (0 .. $npts - 1) {
331 my $lam = $lambda_nm->at($wi);
332 my $k0 = 2 * 3.14159265358979 / $lam;
333  
334 # Extract tensor components as Math::Complex
335 my $exx = _to_mc($eps_tensor->{xx}, $wi);
336 my $eyy = _to_mc($eps_tensor->{yy}, $wi);
337 my $ezz = _to_mc($eps_tensor->{zz}, $wi);
338 my $exy = _to_mc($eps_tensor->{xy} // 0, $wi);
339 my $exz = _to_mc($eps_tensor->{xz} // 0, $wi);
340 my $eyz = _to_mc($eps_tensor->{yz} // 0, $wi);
341  
342 # Substrate N
343 my $Ns = _to_mc($N_substrate, $wi);
344 my $cos_s = Math::Complex::sqrt(1 - $kxi**2 / ($Ns * $Ns));
345 my $cos_a = cos($theta_rad_val);
346  
347 # Ordinary wave in layer (s-polarization sees eyy)
348 my $q_o = Math::Complex::sqrt($eyy - $kxi**2);
349 my $n_o = Math::Complex::sqrt($eyy);
350 my $cos_o = $q_o / $n_o;
351  
352 # Extraordinary wave (p-pol sees exx, ezz)
353 my $q_e = Math::Complex::sqrt($exx - $kxi**2 * $exx / $ezz);
354 my $n_e = Math::Complex::sqrt($exx);
355 my $cos_e = $q_e / $n_e;
356  
357 # Phase in layer
358 my $phi_o = $k0 * $q_o * $d_nm;
359 my $phi_e = $k0 * $q_e * $d_nm;
360  
361 # s-pol (Airy)
362 my $r_s1 = ($Na_val*$cos_a - $n_o*$cos_o) / ($Na_val*$cos_a + $n_o*$cos_o);
363 my $r_s2 = ($n_o*$cos_o - $Ns*$cos_s) / ($n_o*$cos_o + $Ns*$cos_s);
364 my $ph_s = Math::Complex::exp(Math::Complex->make(0, 2) * $phi_o);
365 my $rss_i = ($r_s1 + $r_s2*$ph_s) / (1 + $r_s1*$r_s2*$ph_s);
366  
367 # p-pol (Airy)
368 my $r_p1 = ($n_e*$cos_a - $Na_val*$cos_e) / ($n_e*$cos_a + $Na_val*$cos_e);
369 my $r_p2 = ($Ns*$cos_e - $n_e*$cos_s) / ($Ns*$cos_e + $n_e*$cos_s);
370 my $ph_p = Math::Complex::exp(Math::Complex->make(0, 2) * $phi_e);
371 my $rpp_i = ($r_p1 + $r_p2*$ph_p) / (1 + $r_p1*$r_p2*$ph_p);
372  
373 # Cross-polarization (first-order perturbative)
374 my $rps_i = Math::Complex->make(0, -1) * $k0 * $d_nm * $eyz / ($Na_val * $cos_a + 1e-30);
375 my $rsp_i = Math::Complex->make(0, -1) * $k0 * $d_nm * $eyz / ($Na_val * $cos_a + 1e-30);
376  
377 push @rpp_re, Math::Complex::Re($rpp_i);
378 push @rpp_im, Math::Complex::Im($rpp_i);
379 push @rss_re, Math::Complex::Re($rss_i);
380 push @rss_im, Math::Complex::Im($rss_i);
381 push @rps_re, Math::Complex::Re($rps_i);
382 push @rps_im, Math::Complex::Im($rps_i);
383 push @rsp_re, Math::Complex::Re($rsp_i);
384 push @rsp_im, Math::Complex::Im($rsp_i);
385 }
386  
387 # Convert back to PDL
388 my $rpp = pdl(\@rpp_re) + i() * pdl(\@rpp_im);
389 my $rss = pdl(\@rss_re) + i() * pdl(\@rss_im);
390 my $rps = pdl(\@rps_re) + i() * pdl(\@rps_im);
391 my $rsp = pdl(\@rsp_re) + i() * pdl(\@rsp_im);
392  
393 # Standard ellipsometric angles
394 my $rho = $rpp / $rss;
395 my $psi = atan(abs($rho)) * (180.0 / PI);
396 my $delta_rad = -carg($rho);
397 $delta_rad += 2*PI * ($delta_rad < 0);
398 my $delta = $delta_rad * (180.0 / PI);
399  
400 return {
401 rpp => $rpp, rss => $rss, rps => $rps, rsp => $rsp,
402 psi => $psi->re->double, delta => $delta->re->double,
403 };
404 }
405  
406 # Convert PDL complex or scalar to Math::Complex at given index
407 sub _to_mc {
408 my ($val, $idx) = @_;
409 return Math::Complex->make(0, 0) unless defined $val;
410 unless (ref($val) && ref($val) eq 'PDL') {
411 return Math::Complex->make($val, 0) if !ref($val);
412 return $val; # already Math::Complex
413 }
414 if ($val->nelem > 1) {
415 my $v = $val->slice("($idx)");
416 my $re = $v->re->sclr;
417 my $im = $v->im->sclr;
418 return Math::Complex->make($re, $im);
419 } else {
420 my $re = $val->re->sclr;
421 my $im = $val->im->sclr;
422 return Math::Complex->make($re, $im);
423 }
424 }
425  
426 1;