Physics/Ellipsometry/VASE.pm

filter: Statementstmt 74.4% 169 / 227 Branchbran 54.0% 27 / 50 Conditioncond 51.1% 22 / 43 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 93.3% 14 / 15 Podpod 100% 6 / 6 totaltotal 65.7% 240 / 365 CC 48 SCAR 30.4
CC
48
Cov
68%
plot
83.4
new
16.6
load_data
14.5
CRAP 20.9
« Physics/Ellipsometry/VASE/Parameter.pm ↑ Summary Physics/Ellipsometry/VASE/Anisotropy.pm »
1 package Physics::Ellipsometry::VASE;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use PDL::Fit::LM;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use PDL::NiceSlice;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use PDL::Constants qw(PI);
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 4 our $VERSION = '1.04';
10
11 # Sub-modules (loaded on demand)
12 # use Physics::Ellipsometry::VASE::TMM;
13 # use Physics::Ellipsometry::VASE::Dispersion;
14 # use Physics::Ellipsometry::VASE::EMA;
15 # use Physics::Ellipsometry::VASE::Materials;
16 # use Physics::Ellipsometry::VASE::Parameter;
17 # use Physics::Ellipsometry::VASE::Optimizer;
18
19 # Constructor
20 4 ▶sub new {
Subroutine new: called CC=9 SCAR=28.1
Pod documented
21 4 my ($class, %args) = @_;
22 my $self = {
23 layers => $args{layers} || 1,
24 0 ▶ model => $args{model} || sub { die "Model not implemented" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 data => undef,
26 # Fitting options
27 deriv_step => $args{deriv_step} || 1e-4,
28 min_deriv_step => $args{min_deriv_step} || 0.001,
29 maxiter => $args{maxiter} || 500,
30 eps => $args{eps} || 1e-7,
31 # Delta handling
32 circular_delta => $args{circular_delta} // 1, # use circular residuals
33 # LM regularization
34 4 ▶ lm_reg_floor => $args{lm_reg_floor} || 1e-10,
Condition47%8/17
$args{'layers'} || 1
l!l
40
$args{'model'} || sub { die 'Model not implemented'; }
l!l&&r!l&&!r
040
$args{'deriv_step'} || 0.0001
l!l
04
$args{'min_deriv_step'} || 0.001
l!l
04
$args{'maxiter'} || 500
l!l
04
$args{'eps'} || 1e-07
l!l
04
$args{"circular_delta"} // 1
l!l
04
$args{'lm_reg_floor'} || 1e-10
l!l
04
MC/DC0%0/1
$args{'layers'} || 1
$args{'layers'}
MC/DC0%0/2
$args{'model'} || sub { die 'Model not implemented'; }
$args{'model'} sub { die 'Model not implemented'; }
MC/DC0%0/1
$args{'deriv_step'} || 0.0001
$args{'deriv_step'}
MC/DC0%0/1
$args{'min_deriv_step'} || 0.001
$args{'min_deriv_step'}
MC/DC0%0/1
$args{'maxiter'} || 500
$args{'maxiter'}
MC/DC0%0/1
$args{'eps'} || 1e-07
$args{'eps'}
MC/DC0%0/1
$args{"circular_delta"} // 1
$args{"circular_delta"}
MC/DC0%0/1
$args{'lm_reg_floor'} || 1e-10
$args{'lm_reg_floor'}
Truth table50%1/2
A
A= $args{'layers'}
Aresult
11
00
Truth table33%1/3
A || B
A= $args{'model'}B= sub { die 'Model not implemented'; }
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{'deriv_step'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'min_deriv_step'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'maxiter'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'eps'}
Aresult
11
00
Truth table50%1/2
A
A= $args{"circular_delta"}
Aresult
11
00
Truth table50%1/2
A
A= $args{'lm_reg_floor'}
Aresult
11
00
35 };
36 4 bless $self, $class;
37 4 return $self;
38 }
39
40 # Load data from file (auto-detects Woollam VASE format)
41 4 ▶sub load_data {
Subroutine load_data: called CC=14 SCAR=26.7
Pod documented
42 4 my ($self, $filename) = @_;
43 4 ▶ open my $fh, '<', $filename or die "Cannot open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "Cannot open ${filename}: $!" 4 0 4
44 4 my @lines = <$fh>;
45 4 close $fh;
46
47 # Detect Woollam VASE format: line 2 starts with VASEmethod[
48 4 ▶ my $is_woollam = (@lines >= 4 && $lines[1] =~ /^VASEmethod\[/);
Condition66%2/3
@lines >= 4 && $lines[1] =~ /^VASEmethod\[/
!ll&&!rl&&r
031
MC/DC50%1/2
@lines >= 4 && $lines[1] =~ /^VASEmethod\[/
@lines >= 4 $lines[1] =~ /^VASEmethod\[/
Truth table66%2/3
A && B
A= @lines >= 4B= $lines[1] =~ /^VASEmethod\[/
ABresult
0X0
100
111
49
50 4 my @data;
51 4 ▶ if ($is_woollam) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_woollam) { } 1 3 4
52 # Parse Woollam header (strip Windows \r)
53 1 chomp(my $sample_name = $lines[0]);
54 1 $sample_name =~ s/\r//g;
55 1 $self->{sample_name} = $sample_name;
56
57 1 ▶ if ($lines[1] =~ /^VASEmethod\[(.+)\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[1] =~ /^VASEmethod\[(.+)\]/) 1 0 1
58 1 $self->{vase_method} = $1;
59 }
60
61 1 ▶ if ($lines[2] =~ /^Original\[(.+)\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[2] =~ /^Original\[(.+)\]/) 1 0 1
62 1 $self->{original_file} = $1;
63 }
64
65 1 chomp(my $units = $lines[3]);
66 1 $units =~ s/\r//g;
67 1 $self->{units} = $units;
68
69 # Data starts at line 5 (index 4)
70 1 for my $i (4 .. $#lines) {
71 5 my $line = $lines[$i];
72 5 chomp $line;
73 5 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 5 5
74 5 my @fields = split ' ', $line;
75 # Skip lines whose first field is not numeric (e.g. Mueller labels)
76 5 ▶ next unless $fields[0] =~ /^[-+]?\d*\.?\d+([eE][-+]?\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields[0] =~ /^[-+]?\d*\.?\d+([eE][-+]?\d+)?$/ 0 5 5
77 5 push @data, \@fields;
78 }
79 } else {
80 # Original format: skip #comments and blank lines
81 3 for my $line (@lines) {
82 18 ▶ next if $line =~ /^\s*#/; # skip comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ 3 15 18
83 15 ▶ next if $line =~ /^\s*$/; # skip blank lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 15 15
84 15 my @fields = split ' ', $line;
85 15 push @data, \@fields;
86 }
87 }
88
89 4 my $data = pdl \@data;
90
91 # Woollam files have 6 columns; extract sigma and keep standard 4
92 4 ▶ if ($is_woollam && $data->getdim(0) >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_woollam and $data->getdim(0) >= 6) 1 3 4
Condition66%2/3
$is_woollam and $data->getdim(0) >= 6
!ll&&!rl&&r
301
MC/DC50%1/2
$is_woollam and $data->getdim(0) >= 6
$is_woollam $data->getdim(0) >= 6
Truth table66%2/3
A and B
A= $is_woollamB= $data->getdim(0) >= 6
ABresult
0X0
100
111
93 1 $self->{sigma} = $data->(4:5,:);
94 1 $data = $data->(0:3,:)->sever;
95 }
96
97 4 $self->{data} = $data;
98 4 return $data;
99 }
100
101 # Set model function
102 3 ▶sub set_model {
Subroutine set_model: called CC=1 SCAR=0.0
Pod documented
103 3 my ($self, $model) = @_;
104 3 $self->{model} = $model;
105 }
106
107 # Fit data to model
108 3 ▶sub fit {
Subroutine fit: called CC=2 SCAR=6.9
Pod documented
109 3 my ($self, $initial_params, %opts) = @_;
110 3 my $data = $self->{data};
111 3 my $model = $self->{model};
112
113 # data shape is (nfields, npts); dim0=fields, dim1=data points
114 # Extract x as (npts, 2) so $x->(:,0)=wavelength, $x->(:,1)=angle
115 3 my $x_data = $data->(0:1,:)->xchg(0,1);
116
117 # Build y to match model output order: [psi_all, delta_all]
118 3 my $psi_data = $data->((2),:)->flat;
119 3 my $delta_data = $data->((3),:)->flat;
120 3 my $y_data = $psi_data->append($delta_data);
121 3 my $npts = $psi_data->nelem;
122
123 # Use measured uncertainties from Woollam data when available
124 3 my $sigma;
125 3 ▶ if (defined $self->{sigma}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'sigma'}) { } 0 3 3
126 0 $sigma = $self->{sigma}->((0),:)->flat->append($self->{sigma}->((1),:)->flat);
127 } else {
128 3 $sigma = ones($y_data->nelem);
129 }
130
131 # lmfit sizes $dyda from $x->getdim(0), so pass a dummy x whose
132 # first dimension equals the number of y values (2*npts)
133 3 my $x_fit = sequence($y_data->nelem);
134
135 # Wrapper: adapts user model to lmfit interface ($x, $par, $ym, $dyda)
136 3 my $circular = $self->{circular_delta};
137 13 ▶ my $fit_func = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
138 13 my ($x, $par, $ym, $dyda) = @_;
139
140 13 my $y_model = &$model($par, $x_data);
141
142 # Apply circular delta residual alignment
143 13 ▶ if ($circular) {
Branch: 1 branch
BranchTrueFalseTotal
if ($circular) 13 0 13
144 13 my $delta_model = $y_model->slice("$npts:" . (2*$npts-1));
145 13 my $diff = $delta_model - $delta_data;
146 13 my $correction = 360.0 * rint($diff / 360.0);
147 13 $delta_model -= $correction;
148 }
149
150 13 $ym .= $y_model;
151
152 # Numerical partial derivatives via finite differences
153 13 my $np = $par->nelem;
154 13 for my $i (0 .. $np - 1) {
155 61 my $par_h = $par->copy;
156 61 my $p_i = $par->slice("($i)")->sclr;
157 61 my $eps = abs($p_i) * ($self->{deriv_step}) + 1e-10;
158 61 ▶ $eps = $self->{min_deriv_step} if $eps < $self->{min_deriv_step};
Branch: 1 branch
BranchTrueFalseTotal
if $eps < $self->{"min_deriv_step"} 35 26 61
159 61 $par_h->slice("($i)") += $eps;
160 61 my $y_pert = &$model($par_h, $x_data);
161 61 ▶ if ($circular) {
Branch: 1 branch
BranchTrueFalseTotal
if ($circular) 61 0 61
162 61 my $dm = $y_pert->slice("$npts:" . (2*$npts-1));
163 61 my $diff2 = $dm - $delta_data;
164 61 $dm -= 360.0 * rint($diff2 / 360.0);
165 }
166 61 $dyda->slice(",($i)") .= ($y_pert - $ym) / $eps;
167 }
168 3 };
169
170 my ($ym, $finalp, $covar, $iters) = _lmfit(
171 $x_fit, $y_data, $sigma, $fit_func, $initial_params,
172 {Maxiter => $self->{maxiter}, Eps => $self->{eps},
173 RegFloor => $self->{lm_reg_floor}}
174 3 );
175
176 3 $self->{covar} = $covar;
177 3 $self->{iters} = $iters;
178 3 $self->{ym} = $ym;
179
180 3 return $finalp;
181 }
182
183 # Levenberg-Marquardt, identical to PDL::Fit::LM::lmfit except that the
184 # damped diagonal gets an additive floor (lambda * RegFloor). Without it, a
185 # parameter with an all-zero Jacobian column (e.g. one pinned at a clamp in
186 # the model) makes the normal matrix singular and lu_backsub dies. With it,
187 # that parameter simply receives a zero step.
188 3 ▶sub _lmfit {
Subroutine _lmfit: called CC=12 SCAR=24.9
189 3 my ($x, $y, $sig, $func, $c, $opt) = @_;
190 3 ▶ my $maxiter = $opt->{Maxiter} // 200;
Condition50%1/2
$opt->{"Maxiter"} // 200
l!l
30
MC/DC0%0/1
$opt->{"Maxiter"} // 200
$opt->{"Maxiter"}
Truth table50%1/2
A
A= $opt->{"Maxiter"}
Aresult
11
00
191 3 ▶ my $eps = $opt->{Eps} // 1e-4;
Condition50%1/2
$opt->{"Eps"} // "0.0001"
l!l
30
MC/DC0%0/1
$opt->{"Eps"} // "0.0001"
$opt->{"Eps"}
Truth table50%1/2
A
A= $opt->{"Eps"}
Aresult
11
00
192 3 ▶ my $floor = $opt->{RegFloor} // 1e-10;
Condition50%1/2
$opt->{"RegFloor"} // "1e-10"
l!l
30
MC/DC0%0/1
$opt->{"RegFloor"} // "1e-10"
$opt->{"RegFloor"}
Truth table50%1/2
A
A= $opt->{"RegFloor"}
Aresult
11
00
193
194 3 my ($isig2, $chisq) = (1 / ($sig * $sig), 0);
195 my ($ym, $al, $cov, $bet, $oldbet, $olda, $oldal, $ochisq, $di) =
196 3 map { null } (0 .. 8);
197 3 my ($aldiag, $codiag);
198 3 my $dyda = zeroes($x->type, $x->getdim(0), $c->getdim(0));
199 3 my $alv = zeroes($x->type, $x->getdim(0), $c->getdim(0), $c->getdim(0));
200 3 my ($iter, $lambda) = (0, 0.001);
201
202 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$iter++ == 0 || $iter < $maxiter && $di / $chisq > $eps and do { do { if ($iter > 0) { $cov .= $al; $codiag .= ($aldiag * (1 + $lambda) + $lambda * $floor); () = lu_decomp($cov); $bet .= lu_backsub($lu, $perm, $par, $bet); $c += $bet; } ; &$func($x, $c, $ym, $dyda); $chisq = ($y - $ym) * ($y - $ym); $chisq *= $isig2; $chisq = $chisq->sumover; $dyda->transpose->outer($dyda->transpose, $alv->mv(0, 2)); $alv *= $isig2; $alv->sumover($al); (($y - $ym) * $isig2 * $dyda)->sumover($bet); if ($iter == 0) { $olda .= $c; $ochisq .= $chisq; $oldbet .= $bet; $oldal .= $al; $aldiag = $al->diagonal(0, 1); $cov .= $al; $codiag = $cov->diagonal(0, 1); } ; $di .= abs($chisq - $ochisq); if ($chisq < $ochisq) { $lambda *= "0.1"; $ochisq .= $chisq; $olda .= $c; $oldbet .= $bet; $oldal .= $al; } else { $lambda *= 10; $chisq .= $ochisq; $c .= $olda; $bet .= $oldbet; $al .= $oldal; } } } 10 3 13
Condition83%5/6
$iter++ == 0 or $iter < $maxiter and $di / $chisq > $eps
l!l&&r!l&&!r
319
$iter < $maxiter and $di / $chisq > $eps
!ll&&!rl&&r
091
MC/DC0%0/3
$iter++ == 0 or $iter < $maxiter and $di / $chisq > $eps
$iter++ == 0 $iter < $maxiter $di / $chisq > $eps
Truth table25%1/4
A or B and C
A= $iter++ == 0B= $iter < $maxiterC= $di / $chisq > $eps
ABCresult
1XX1
0111
00X0
0100
203 13 ▶ if ($iter > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter > 0) 10 3 13
204 10 $cov .= $al;
205 10 $codiag .= $aldiag * (1 + $lambda) + $lambda * $floor;
206 10 my ($lu, $perm, $par) = lu_decomp($cov);
207 10 $bet .= lu_backsub($lu, $perm, $par, $bet);
208 10 $c += $bet;
209 }
210 13 &$func($x, $c, $ym, $dyda);
211 13 $chisq = ($y - $ym) * ($y - $ym);
212 13 $chisq *= $isig2;
213 13 $chisq = $chisq->sumover;
214 13 $dyda->transpose->outer($dyda->transpose, $alv->mv(0, 2));
215 13 $alv *= $isig2;
216 13 $alv->sumover($al);
217 13 (($y - $ym) * $isig2 * $dyda)->sumover($bet);
218 13 ▶ if ($iter == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter == 0) 3 10 13
219 3 $olda .= $c; $ochisq .= $chisq; $oldbet .= $bet;
220 3 $oldal .= $al; $aldiag = $al->diagonal(0, 1);
221 3 $cov .= $al; $codiag = $cov->diagonal(0, 1);
222 }
223 13 $di .= abs($chisq - $ochisq);
224 13 ▶ if ($chisq < $ochisq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chisq < $ochisq) { } 7 6 13
225 7 $lambda *= 0.1;
226 7 $ochisq .= $chisq; $olda .= $c; $oldbet .= $bet; $oldal .= $al;
227 } else {
228 6 $lambda *= 10;
229 6 $chisq .= $ochisq; $c .= $olda; $bet .= $oldbet; $al .= $oldal;
230 }
231 } while ($iter++ == 0 || $iter < $maxiter && $di / $chisq > $eps);
232 3 ▶ barf "iteration did not converge" if $iter >= $maxiter && $di / $chisq > $eps;
Branch: 1 branch
BranchTrueFalseTotal
if $iter >= $maxiter and $di / $chisq > $eps 0 3 3
Condition33%1/3
$iter >= $maxiter and $di / $chisq > $eps
!ll&&!rl&&r
300
MC/DC0%0/2
$iter >= $maxiter and $di / $chisq > $eps
$iter >= $maxiter $di / $chisq > $eps
Truth table33%1/3
A and B
A= $iter >= $maxiterB= $di / $chisq > $eps
ABresult
0X0
100
111
233
234 # Covariance estimate; floor the diagonal so inv() survives dead parameters
235 3 my $alr = $al->copy;
236 3 my $d = $alr->diagonal(0, 1);
237 3 $d->where($d < $floor) .= $floor;
238 3 return ($ym, $c, inv($alr), $iter);
239 }
240
241 # Calculate MSE (WVASE convention: sqrt(χ²/(2N-M)))
242 1 ▶sub mse {
Subroutine mse: called CC=3 SCAR=11.1
Pod documented
243 1 my ($self, $fit_params, %opts) = @_;
244 1 my $data = $self->{data};
245 1 my $model = $self->{model};
246 1 ▶ my $nparams = $opts{nparams} // $fit_params->nelem;
Condition33%1/3
$opts{"nparams"} // $fit_params->nelem
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{"nparams"} // $fit_params->nelem
$opts{"nparams"} $fit_params->nelem
Truth table33%1/3
A // B
A= $opts{"nparams"}B= $fit_params->nelem
ABresult
1X1
011
000
247
248 1 my $x_data = $data->(0:1,:)->xchg(0,1);
249 1 my $npts = $data->getdim(1);
250 1 my $y_data = $data->((2),:)->flat->append($data->((3),:)->flat);
251
252 1 my $y_fit = &$model($fit_params, $x_data);
253
254 # Apply circular delta alignment for MSE calculation
255 1 ▶ if ($self->{circular_delta}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"circular_delta"}) 1 0 1
256 1 my $delta_data = $data->((3),:)->flat;
257 1 my $delta_model = $y_fit->slice("$npts:" . (2*$npts-1));
258 1 my $diff = $delta_model - $delta_data;
259 1 $delta_model -= 360.0 * rint($diff / 360.0);
260 }
261
262 1 my $chi2 = sum(($y_data - $y_fit)**2)->sclr;
263 1 return sqrt($chi2 / (2*$npts - $nparams));
264 }
265
266 # Plot raw data with model fit overlay
267 1 ▶sub plot {
Subroutine plot: called CC=9 SCAR=44.2
Pod documented
268 1 my ($self, $fit_params, %opts) = @_;
269 1 require PDL::Graphics::Gnuplot;
270
271 0 my $data = $self->{data};
272 0 my $model = $self->{model};
273
274 0 my $wavelength = $data->((0),:)->flat;
275 0 my $angles = $data->((1),:)->flat;
276 0 my $psi_data = $data->((2),:)->flat;
277 0 my $delta_data = $data->((3),:)->flat;
278
279 # Evaluate model at fitted parameters
280 0 my $x_data = $data->(0:1,:)->xchg(0,1);
281 0 my $y_model = &$model($fit_params, $x_data);
282 0 my $npts = $wavelength->nelem;
283 0 my $psi_fit = $y_model->slice("0:" . ($npts - 1));
284 0 my $delta_fit = $y_model->slice("$npts:" . (2 * $npts - 1));
285
286 0 my $output = $opts{output};
287 0 ▶ my $title = $opts{title} // 'VASE Fit Results';
Condition0%0/2
$opts{"title"} // "VASE Fit Results"
l!l
00
MC/DC0%0/1
$opts{"title"} // "VASE Fit Results"
$opts{"title"}
Truth table0%0/2
A
A= $opts{"title"}
Aresult
11
00
288
289 # Find unique angles for grouping
290 0 my @unique_angles = sort { $a <=> $b }
291 0 do { my %s; grep { !$s{$_}++ } list $angles };
292
293 # Color palette for multiple angles
294 0 my @colors = ('#0072B2', '#D55E00', '#009E73', '#CC79A7', '#F0E442',
295 '#56B4E9', '#E69F00', '#000000');
296
297 # Select terminal and construct gpwin
298 0 my $gp;
299 0 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) { } 0 0 0
300 0 my ($term, @topts);
301 0 ▶ if ($output =~ /\.png$/i) { $term = "pngcairo"; @topts = (size => [900,700,"px"]) }
Branch: 4 branches
BranchTrueFalseTotal
if ($output =~ /\.png$/i) { } 0 0 0
elsif ($output =~ /\.pdf$/i) { } 0 0 0
elsif ($output =~ /\.svg$/i) { } 0 0 0
elsif ($output =~ /\.eps$/i) { } 0 0 0
302 0 elsif ($output =~ /\.pdf$/i) { $term = "pdfcairo"; @topts = (size => [7,5.5,"in"]) }
303 0 elsif ($output =~ /\.svg$/i) { $term = "svg"; @topts = (size => [900,700,"px"]) }
304 0 elsif ($output =~ /\.eps$/i) { $term = "epscairo" }
305 0 else { $term = "pngcairo"; @topts = (size => [900,700,"px"]) }
306 0 $gp = PDL::Graphics::Gnuplot::gpwin($term, output => $output, enhanced => 1, @topts);
307 } else {
308 0 $gp = PDL::Graphics::Gnuplot::gpwin(enhanced => 1);
309 }
310
311 # Multiplot: Psi on top, Delta on bottom
312 0 $gp->multiplot(layout => [1, 2], title => $title);
313
314 # Build plot curves grouped by angle
315 0 my (@psi_curves, @delta_curves);
316 0 for my $ai (0 .. $#unique_angles) {
317 0 my $ang = $unique_angles[$ai];
318 0 my $mask = ($angles == $ang);
319 0 my $idx = which($mask);
320 0 my $wl = $wavelength->index($idx);
321 0 my $psid = $psi_data->index($idx);
322 0 my $deld = $delta_data->index($idx);
323 0 my $psif = $psi_fit->index($idx);
324 0 my $delf = $delta_fit->index($idx);
325 0 my $col = $colors[$ai % scalar @colors];
326 0 my $label = sprintf("%.1f{/Symbol \260}", $ang);
327
328 0 push @psi_curves,
329 (with => 'points', legend => "$label data",
330 pt => 7, ps => 0.6, lc => "rgb \"$col\"",
331 $wl, $psid),
332 (with => 'lines', legend => "$label fit",
333 lw => 2, lc => "rgb \"$col\"",
334 $wl, $psif);
335
336 0 push @delta_curves,
337 (with => 'points', legend => "$label data",
338 pt => 7, ps => 0.6, lc => "rgb \"$col\"",
339 $wl, $deld),
340 (with => 'lines', legend => "$label fit",
341 lw => 2, lc => "rgb \"$col\"",
342 $wl, $delf);
343 }
344
345 # --- Psi panel ---
346 $gp->plot(
347 0 { title => '{/Symbol Y} (Psi)',
348 xlabel => '',
349 ylabel => '{/Symbol Y} (deg)',
350 key => 'outside right' },
351 @psi_curves,
352 );
353
354 # --- Delta panel ---
355 0 $gp->plot(
356 { title => '{/Symbol D} (Delta)',
357 xlabel => 'Wavelength (nm)',
358 ylabel => '{/Symbol D} (deg)',
359 key => 'outside right' },
360 @delta_curves,
361 );
362
363 0 $gp->end_multi;
364
365 0 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 0 0 0
366 0 $gp->close;
367 0 print "Plot saved to $output\n";
368 }
369
370 0 return $gp;
371 }
372
373 4 1;
374
375 __END__
« Physics/Ellipsometry/VASE/Parameter.pm ↑ Summary Physics/Ellipsometry/VASE/Anisotropy.pm »