Physics/Ellipsometry/VASE/Materials.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 108 CC 11 SCAR 44.6
CC
11
Cov
0%
load_material
132.0
interpolate_material
2.0
BEGIN
2.0
CRAP 86.1
1 package Physics::Ellipsometry::VASE::Materials;
2 use strict;
3 use warnings;
4 use PDL;
5 use PDL::NiceSlice;
6 use Exporter 'import';
7  
8 our @EXPORT_OK = qw(load_material interpolate_material);
9  
10 our $VERSION = '1.04';
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Physics::Ellipsometry::VASE::Materials - Load and interpolate tabulated
17 optical constants
18  
19 =head1 SYNOPSIS
20  
21 use PDL;
22 use Physics::Ellipsometry::VASE::Materials qw(load_material
23 interpolate_material);
24  
25 # Load a Woollam .mat file (auto-detects eV↔nm)
26 my $si = load_material('Si_jaw.mat');
27  
28 printf "Material : %s\n", $si->{name};
29 printf "Points : %d\n", $si->{npts};
30 printf "Range : %.1f – %.1f nm\n", $si->{wav_min}, $si->{wav_max};
31  
32 # Interpolate onto the measurement wavelength grid
33 my $lambda = sequence(500) * 2 + 300; # 300–1298 nm
34 my ($n_si, $k_si) = interpolate_material($si, $lambda);
35  
36 # Build the complex refractive index for TMM
37 my $N_si = $n_si + i() * $k_si;
38  
39 =head1 DESCRIPTION
40  
41 In spectroscopic ellipsometry the optical model usually contains at
42 least one material whose refractive index is not described by a simple
43 parametric formula but is instead given as a table of measured values —
44 for example, a crystalline silicon substrate or a metal reference layer.
45 These are called B<point-by-point> (PBP) optical constants.
46  
47 Physics::Ellipsometry::VASE::Materials loads such tabulated data from
48 disk and provides linear interpolation to an arbitrary wavelength grid,
49 so the constants can be used directly by L<Physics::Ellipsometry::VASE::TMM>
50 or any model function.
51  
52 When a file stores its spectral axis in electron-volts (eV) the module
53 converts automatically to nanometres via
54  
55 λ (nm) = 1239.842 / E (eV)
56  
57 and reverses the data order if necessary to ensure wavelengths are
58 ascending.
59  
60 =head1 SUPPORTED FORMATS
61  
62 =head2 Woollam .mat format
63  
64 The standard format produced by WVASE® and CompleteEASE. The file has a
65 three-line header followed by data columns:
66  
67 silicon
68 eV
69 469
70 0.6199 3.939 0.0240
71 0.6250 3.941 0.0242
72 ...
73  
74 =over 4
75  
76 =item Line 1 — material name (free text)
77  
78 =item Line 2 — spectral units: C<nm> or C<eV>
79  
80 =item Line 3 — number of data points
81  
82 =item Data — three whitespace-separated columns: wavelength (or energy),
83 I<n>, I<k>
84  
85 =back
86  
87 =head2 Generic 3-column format
88  
89 A plain text file with three whitespace-separated columns (wavelength in
90 nm, I<n>, I<k>). Comment lines beginning with C<#> and blank lines are
91 skipped. No header is required.
92  
93 # wavelength(nm) n k
94 300.0 1.540 0.000
95 310.0 1.538 0.000
96 ...
97  
98 =head1 FUNCTIONS
99  
100 =head2 load_material
101  
102 my $mat = load_material($filepath);
103  
104 Reads a tabulated optical-constants file and returns a hashref with:
105  
106 =over 4
107  
108 =item C<name> — material name (from header, or filename for generic files)
109  
110 =item C<wavelength> — PDL piddle of wavelengths in nm (ascending order)
111  
112 =item C<n> — PDL piddle of refractive index values
113  
114 =item C<k> — PDL piddle of extinction coefficient values
115  
116 =item C<npts> — number of data points
117  
118 =item C<wav_min> — shortest wavelength (nm)
119  
120 =item C<wav_max> — longest wavelength (nm)
121  
122 =back
123  
124 The format (Woollam vs generic) is auto-detected. If the file stores
125 data in eV the conversion to nm is performed automatically.
126  
127 my $ta = load_material('ta_pbp.mat');
128 my $sio2 = load_material('SiO2_Palik.dat');
129  
130 =head2 interpolate_material
131  
132 my ($n, $k) = interpolate_material($material, $lambda_nm);
133  
134 Linearly interpolates the tabulated I<n> and I<k> values in C<$material>
135 (as returned by L</load_material>) onto the wavelength grid
136 C<$lambda_nm>.
137  
138 B<Important:> the target wavelengths should lie within the range
139 C<[wav_min, wav_max]> of the loaded material. Values outside this range
140 are extrapolated linearly, which may be inaccurate.
141  
142 my $lambda = sequence(500) * 2 + 300;
143 my ($n, $k) = interpolate_material($ta, $lambda);
144  
145 # Use in a TMM calculation
146 my $N_sub = $n + i() * $k;
147  
148 =head1 TYPICAL WORKFLOW
149  
150 use Physics::Ellipsometry::VASE::Materials qw(load_material
151 interpolate_material);
152 use Physics::Ellipsometry::VASE::TMM qw(psi_delta);
153 use Physics::Ellipsometry::VASE::Dispersion qw(cauchy_nk);
154  
155 # 1. Load substrate from a .mat file
156 my $sub = load_material('Si_jaw.mat');
157  
158 # 2. Define measurement grid
159 my $lambda = sequence(400) * 2.5 + 300;
160 my $theta = ones($lambda) * 70;
161  
162 # 3. Interpolate substrate onto measurement grid
163 my ($n_sub, $k_sub) = interpolate_material($sub, $lambda);
164 my $N_sub = $n_sub + i() * $k_sub;
165  
166 # 4. Film from a parametric model
167 my ($n_film, $k_film) = cauchy_nk($lambda, 1.46, 0.003, 0.0);
168 my $N_film = $n_film + i() * $k_film;
169  
170 # 5. Ambient
171 my $N_air = ones($lambda) + i() * zeroes($lambda);
172  
173 # 6. Calculate Psi/Delta
174 my ($psi, $delta) = psi_delta($lambda, $theta,
175 [$N_air, $N_film, $N_sub], [100.0]);
176  
177 =head1 SEE ALSO
178  
179 L<Physics::Ellipsometry::VASE>,
180 L<Physics::Ellipsometry::VASE::TMM>,
181 L<Physics::Ellipsometry::VASE::Dispersion>
182  
183 =cut
184  
185 sub load_material {
186 my ($filepath) = @_;
187 open my $fh, '<', $filepath or die "Cannot open material file $filepath: $!";
188 my @lines = <$fh>;
189 close $fh;
190  
191 # Strip Windows CR from all lines
192 s/\r//g for @lines;
193 chomp @lines;
194  
195 my ($name, $units, $npts);
196 my @data;
197  
198 # Detect format by checking header
199 if (@lines >= 3 && $lines[1] =~ /^\s*(nm|eV)\s*$/i) {
200 # Woollam .mat format
201 $name = $lines[0];
202 $units = lc($lines[1]);
203 $units =~ s/\s+//g;
204 $npts = $lines[2] + 0 if $lines[2] =~ /^\d+/;
205  
206 for my $i (3 .. $#lines) {
207 next if $lines[$i] =~ /^\s*$/;
208 my @fields = split /\s+/, $lines[$i];
209 next unless @fields >= 3 && $fields[0] =~ /^[-+]?\d/;
210 push @data, [@fields[0..2]];
211 }
212 } else {
213 # Generic 3-column format
214 $name = $filepath;
215 $units = 'nm';
216 for my $line (@lines) {
217 next if $line =~ /^\s*#/;
218 next if $line =~ /^\s*$/;
219 my @fields = split /\s+/, $line;
220 next unless @fields >= 3 && $fields[0] =~ /^[-+]?\d/;
221 push @data, [@fields[0..2]];
222 }
223 }
224  
225 die "No data found in $filepath" unless @data;
226  
227 my $arr = pdl \@data;
228 my $wav = $arr->(0,:)->flat->sever;
229 my $n = $arr->(1,:)->flat->sever;
230 my $k = $arr->(2,:)->flat->sever;
231  
232 # Convert eV to nm if needed
233 if ($units eq 'ev') {
234 $wav = 1239.842 / $wav;
235 # Reverse if now in descending order
236 if ($wav->at(0) > $wav->at(-1)) {
237 $wav = $wav->(-1:0)->sever;
238 $n = $n->(-1:0)->sever;
239 $k = $k->(-1:0)->sever;
240 }
241 }
242  
243 return {
244 name => $name,
245 wavelength => $wav,
246 n => $n,
247 k => $k,
248 npts => $wav->nelem,
249 wav_min => $wav->min->sclr,
250 wav_max => $wav->max->sclr,
251 };
252 }
253  
254 # Interpolate material optical constants to a given wavelength grid
255 sub interpolate_material {
256 my ($material, $lambda_nm) = @_;
257 my $n = $lambda_nm->interpol($material->{wavelength}, $material->{n});
258 my $k = $lambda_nm->interpol($material->{wavelength}, $material->{k});
259 return ($n, $k);
260 }
261  
262 1;