blib/lib/Physics/RayTransfer.pm

filter: Statementstmt 84.3% 292 / 346 Branchbran 45.6% 63 / 138 Conditioncond 36.1% 26 / 72 MC/DCmcdc 10.4% 5 / 48 Subroutinesub 94.4% 85 / 90 Podpod n/a totaltotal 67.8% 471 / 694 CC 155 SCAR 21.6
CC
155
Cov
69%
__ANON__
38.8
add_lens
38.8
__ANON__
28.1
CRAP 8.7
↑ Summary
1 =head1 NAME
2  
3 Physics::RayTransfer - Object-oriented ray transfer analysis
4  
5 =head1 SYNOPSIS
6  
7 use Physics::RayTransfer;
8  
9 my $sys = Physics::RayTransfer->new();
10 $sys->add_mirror;
11 $sys->add_space->parameter(sub{shift});
12 $sys->add_mirror(8);
13  
14 my $d = [ map { $_ / 10 } (0..100) ];
15  
16 my @data =
17 map { $_->[1]->w(1063e-7) }
18 $sys->evaluate_parameterized($d);
19  
20 =head1 DESCRIPTION
21  
22 This physics module is a helper for creating a system of ray transfer matrices (RTM) for analyzing optical systems. The most useful functionality is related to laser cavity stability analysis.
23  
24 =head1 CLASSES
25  
26 =head2 Physics::RayTransfer
27  
28 This class provides the main simulation object, which houses the element objects and does the overall calculations.
29  
30 =head3 METHODS
31  
32 =over
33  
34 =cut
35  
36 6 ▶use MooseX::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 6 ▶use Method::Signatures::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 6 ▶class Physics::RayTransfer {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=1.0
40  
41 6 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶ use List::Util qw/reduce/;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 =item new( [ %attributes ] )
45  
46 Constructor. Can accept a hash of attributes. Those attributes also have accessor methods of the same name. They are:
47  
48 =over
49  
50 =item elements
51  
52 An array reference of C<Physics::RayTransfer::Element> objects, ordered left-to-right. The default is an empty array reference.
53  
54 =back
55  
56 =item add_element( Physics::RayTransfer::Element $object )
57  
58 A helper method to push a manually constructed C<Physics::RayTransfer::Element> onto the C<elements> attribute.
59  
60 =cut
61  
62 has 'elements' => (
63 isa => 'ArrayRef[Physics::RayTransfer::Element]',
64 is => 'rw',
65 7 ▶ default => sub{ [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 6 traits => ['Array'],
67 handles => {
68 add_element => 'push',
69 },
70 );
71  
72 =item add_observer()
73  
74 Shortcut method for adding a C<Physics::RayTransfer::Observer> object as the next element in the system (on the right side). Returns the object added.
75  
76 =cut
77  
78 0 ▶ method add_observer () {
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine add_observer: not called CC=2 SCAR=9.6
Subroutine __ANON__: called CC=2 SCAR=9.6
Branch: 2 branches
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
if scalar @_ > 0 0 2 2
79 0 my $obs = Physics::RayTransfer::Observer->new();
80 0 $self->add_element( $obs );
81 0 return $obs;
82 6 }
83  
84 =item add_space( [ Num $length ] )
85  
86 Shortcut method for adding a C<Physics::RayTransfer::Space> object as the next element in the system (on the right side). Optionally takes a number representing the length of the object. Returns the object added.
87  
88 =cut
89  
90 6 ▶ method add_space (Num $length?) {
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine add_space: called CC=6 SCAR=19.4
Branch: 2 branches
BranchTrueFalseTotal
if scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($length) 0 13 13
if scalar @_ > 1 0 13 13
Condition50%3/6
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($length)
!ll&&!rl&&r
0130
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
1120
MC/DC0%0/2
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($length)
scalar @_ > 0 not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($length)
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table33%1/3
A and not(B)
A= scalar @_ > 0B= not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($length)
ABresult
0X0
100
111
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
91 13 ▶ my $space = Physics::RayTransfer::Space->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $length ? : 13 0 13
92 (defined $length) ? ( length => $length ) : ()
93 );
94 13 $self->add_element($space);
95 13 return $space;
96 6 }
97  
98 =item add_mirror( [ Num $radius ] )
99  
100 Shortcut method for adding a C<Physics::RayTransfer::Mirror> object as the next element in the system (on the right side). Optionally takes a number representing the radius of curvature of the object's mirror. Returns the object added.
101  
102 =cut
103  
104 0 ▶ method add_mirror (Num $radius?) {
Subroutine BEGIN: called CC=1 SCAR=2.5
Subroutine add_mirror: called CC=6 SCAR=26.7
Branch: 4 branches
BranchTrueFalseTotal
if scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius) 0 7 7
if scalar @_ > 1 0 7 7
if scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius) 0 0 0
if scalar @_ > 1 0 0 0
Condition25%3/12
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius)
!ll&&!rl&&r
610
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
100
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius)
!ll&&!rl&&r
000
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius)
scalar @_ > 0 not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius)
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
MC/DC0%0/2
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius)
scalar @_ > 0 not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius)
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table66%2/3
A and not(B)
A= scalar @_ > 0B= not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius)
ABresult
0X0
100
111
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
Truth table0%0/3
A and not(B)
A= scalar @_ > 0B= not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($radius)
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
105 0 ▶ my $mirror = Physics::RayTransfer::Mirror->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $radius ? : 1 6 7
defined $radius ? : 0 0 0
106 (defined $radius) ? ( radius => $radius ) : ()
107 );
108 0 $self->add_element($mirror);
109 0 return $mirror;
110 6 }
111  
112 =item add_lens( [ Num $focal_length ] )
113  
114 Shortcut method for adding a C<Physics::RayTransfer::Lens> object as the next element in the system (on the right side). Optionally takes a number representing the focal length of the object's lens. Returns the object added.
115  
116 =cut
117  
118 0 ▶ method add_lens (Num $f?) {
Subroutine BEGIN: called CC=1 SCAR=6.3
Subroutine add_lens: not called CC=6 SCAR=36.6
Branch: 4 branches
BranchTrueFalseTotal
if scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($f) 0 0 0
if scalar @_ > 1 0 0 0
if scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($f) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/6
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($f)
!ll&&!rl&&r
000
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($f)
scalar @_ > 0 not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($f)
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table0%0/3
A and not(B)
A= scalar @_ > 0B= not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($f)
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
119 0 ▶ my $lens = Physics::RayTransfer::Lens->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $f ? : 0 0 0
defined $f ? : 0 0 0
120 (defined $f) ? ( radius => $f ) : ()
121 );
122 0 $self->add_element($lens);
123 0 return $lens;
124 6 }
125  
126 =item evaluate()
127  
128 Returns a C<Physics::RayTransfer::Element> object which is equivalent to the system as a whole. See L</EVALUATION> for more.
129  
130 =cut
131  
132 6 ▶ method evaluate (ArrayRef $vals?) {
Subroutine BEGIN: called CC=1 SCAR=1.0
Subroutine evaluate: called CC=5 SCAR=18.3
Branch: 2 branches
BranchTrueFalseTotal
if scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef"))->check($vals) 0 7 7
if scalar @_ > 1 0 7 7
Condition16%1/6
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef"))->check($vals)
!ll&&!rl&&r
700
$Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef")
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef"))->check($vals)
scalar @_ > 0 not +($Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef"))->check($vals)
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef")
$Method::Signatures::mutc{"cache"}{"ArrayRef"} "Method::Signatures::Modifiers"->_make_constraint("ArrayRef")
Truth table33%1/3
A and not(B)
A= scalar @_ > 0B= not +($Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef"))->check($vals)
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"ArrayRef"}B= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef")
ABresult
1X1
011
000
133 7 my $elements = $self->_construct;
134 7 ▶ return reduce { $a->times($b) } map { $_->get } @$elements;
Subroutine __ANON__: called CC=1 SCAR=0.0
135 6 }
136  
137 =item evaluate_parameterized( ArrayRef $vals )
138  
139 Takes an array reference of values for the parameterization functions. Returns 2D array (i.e. a list of array references). Foreach value an array reference is returned containg that value and a C<Physics::RayTransfer::Element> object which is equivalent to the system as a whole when that parameter is used for evaluation. See L</EVALUATION> for more.
140  
141 =cut
142  
143 0 ▶ method evaluate_parameterized (ArrayRef $vals) {
Subroutine BEGIN: called CC=1 SCAR=6.4
Subroutine evaluate_parameterized: not called CC=5 SCAR=33.4
Branch: 6 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef"))->check($vals) 0 0 0
if scalar @_ > 1 0 0 0
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef"))->check($vals) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"ArrayRef"} ||= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef")
$Method::Signatures::mutc{"cache"}{"ArrayRef"} "Method::Signatures::Modifiers"->_make_constraint("ArrayRef")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"ArrayRef"}B= "Method::Signatures::Modifiers"->_make_constraint("ArrayRef")
ABresult
1X1
011
000
144 0 my $elements = $self->_construct;
145 return map {
146 0 my $val = $_;
147 my $new =
148 0 ▶ reduce { $a->times($b) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 0 map { $_->get($val) }
150 @$elements;
151 0 [ $val, $new ];
152 } @$vals;
153 6 }
154  
155 6 ▶ method _construct () {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _construct: called CC=14 SCAR=26.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 7 7
156 7 my @orig = @{ $self->elements };
157  
158 # see if system begins(left) and/or ends(right) with mirror
159 7 ▶ my $left_mirror = shift @orig if $orig[0]->isa('Physics::RayTransfer::Mirror');
Branch: 1 branch
BranchTrueFalseTotal
if $orig[0]->isa("Physics::RayTransfer::Mirror") 3 4 7
160 7 ▶ my $right_mirror = pop @orig if $orig[-1]->isa('Physics::RayTransfer::Mirror');
Branch: 1 branch
BranchTrueFalseTotal
if $orig[-1]->isa("Physics::RayTransfer::Mirror") 4 3 7
161  
162 # see if an observer object exists
163 7 my $has_observer = grep { $_->isa('Physics::RayTransfer::Observer') } @orig;
164  
165 7 ▶ croak "Cannot have more than one observer" if $has_observer > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $has_observer > 1 0 7 7
166  
167 # create an observer "at the end" for several cases
168 7 ▶ unless ($has_observer) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_observer) 5 2 7
169 5 my $obs = Physics::RayTransfer::Observer->new();
170 5 ▶ if ( $right_mirror and not $left_mirror) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right_mirror and not $left_mirror) { } 1 4 5
Condition100%3/3
$right_mirror and not $left_mirror
!ll&&!rl&&r
221
MC/DC100%2/2
$right_mirror and not $left_mirror
$right_mirror not $left_mirror
Truth table100%3/3
A and not(B)
A= $right_mirrorB= not $left_mirror
ABresult
0X0
100
111
171 1 unshift @orig, $obs;
172 } else {
173 4 push @orig, $obs;
174 }
175 }
176  
177  
178 # determine which elements are left or right of observer
179 7 my (@left, @right);
180 7 my $obs_seen = 0;
181  
182 7 foreach my $elem (@orig) {
183 20 ▶ if ($elem->isa('Physics::RayTransfer::Observer')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("Physics::RayTransfer::Observer")) 7 13 20
184 7 $obs_seen = 1;
185 7 next;
186 }
187
188 13 ▶ if (! $obs_seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $obs_seen) { } 9 4 13
189 9 push @left, $elem;
190 } else {
191 4 push @right, $elem;
192 }
193 }
194  
195 # build elements array for evaluation
196 7 my @elements;
197 7 ▶ if ($left_mirror and $right_mirror) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left_mirror and $right_mirror) { } 2 5 7
Condition100%3/3
$left_mirror and $right_mirror
!ll&&!rl&&r
412
MC/DC100%2/2
$left_mirror and $right_mirror
$left_mirror $right_mirror
Truth table100%3/3
A and B
A= $left_mirrorB= $right_mirror
ABresult
0X0
100
111
198 #cavity begins and ends at observer
199 2 push @elements, @right, $right_mirror, reverse(@right), reverse(@left), $left_mirror, @left;
200 } else {
201 5 push @elements, @left;
202 5 ▶ if ($right_mirror) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right_mirror) 2 3 5
203 2 push @elements, @right, $right_mirror, reverse(@right);
204 }
205 }
206  
207 7 return \@elements;
208 6 }
209  
210 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
211  
212 =pod
213  
214 =back
215  
216 =head3 EVALUATION
217  
218 The evaluation process is perhaps the important part of the simulation. It is at this point that the objects are converted into a series of matricies which are multiplied out to get the equivalent single matrix. This can either be done in a purely numerical way (C<evaluate>) or repeatedly for multiple values of some parameter (C<evaluate_parameterized>).
219  
220 The conversion to a series of matricies depends on the layout of your system. The most basic is just a linear array of optical elements. In such a system there are no end mirror objects. The input ray is assumed to originate on the left. If a C<Physics::RayTranser::Observer> object (henceforth called "an observer") is placed somewhere in the system, this is then seen as the endpoint. If no observer is placed one will be assumed on the right side.
221  
222 A more complicated system having a C<Physics::RayTransfer::Mirror> object (henceforth called "a mirror") as the last element on the right will have its ray start on the left, traverse the system, reflect off the mirror and then return back through the system. An observer will stop the light I<only on the return trip>. If no observer has been placed, one will implicitly exist on the leftmost part of the system, meaning the light will travel from left to right and back to the starting point.
223  
224 Finally if there are mirrors at both the left and right of the system, this triggers I<cavity> mode, most useful for laser cavity simulation. In cavity mode, light both starts and stops at the observer, making a complete round trip inside the cavity (passing the observer once). If no observer is placed, the observer is implicitly positioned just before the right mirror (assumed to be the output coupler).
225  
226 =head2 Physics::RayTransfer::Element
227  
228 This class is the base class of all the individual elements of the system. Further it is the generic class used for when the elements are combined down to a single matrix. Most of the functionality in this class is present in the subclasses, except for some of the constructor defaults.
229  
230 =head3 METHODS
231  
232 =over
233  
234 =cut
235  
236 6 ▶class Physics::RayTransfer::Element {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.2
237  
238 =item new( [ %attributes ] )
239  
240 Constructor. Can accept a hash of attributes. Those attributes also have accessor methods of the same name. They are:
241  
242 =over
243  
244 =item a
245  
246 =item b
247  
248 =item c
249  
250 =item d
251  
252 These are the holders for the C<a> (1,1), C<b> (1,2), C<c> (2,1), and C<d> (2,2) matrix slots. C<a> and C<d> default to C<1>. C<b> and C<c> default to C<0> but this can be overridden by subclasses.
253  
254 =item parameter
255  
256 This is a coderef which maps an arbitrary input parameter to the relevant matrix element (either C<b> or C<c> depending on element). This allows for plotting the behavior of systems in terms of arbitrary parameters in multiple elements. For example one might want to move a lens across a space of constant length C<10>:
257  
258 $sys->add_space->parameter(sub{shift});
259 $sys->add_lens(2);
260 $sys->add_space->parameter(sub{10-shift});
261  
262 my $d = [ map { $_ / 10 } (0..100) ];
263  
264 my @data = $sys->evaluate_parameterized($d);
265  
266 =back
267  
268 =cut
269  
270 6 has 'a' => ( isa => 'Num', is => 'rw', default => 1 );
271 6 has 'b' => ( isa => 'Num', is => 'rw', builder => '_build_b', lazy => 1 );
272 6 has 'c' => ( isa => 'Num', is => 'rw', builder => '_build_c', lazy => 1 );
273 6 has 'd' => ( isa => 'Num', is => 'rw', default => 1 );
274  
275 6 ▶ method _build_b () { 0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_b: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 16 16
276 6 ▶ method _build_c () { 0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_c: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 18 18
277  
278 6 has 'parameter' => (
279 isa => 'CodeRef',
280 is => 'rw',
281 predicate => 'has_parameter',
282 );
283  
284 =item get_parameterized( [ Num $val ] )
285  
286 This method is meant to be subclassed with the specific behavior of the element in question. The default behavior is to return the object itself (the optional parameter value is ignored in this generic class). Most subclasses will return a new object, one in which the parameterization is used to construct the matrix.
287  
288 =cut
289  
290 0 ▶ method get_parameterized (Num $val?) {
Subroutine BEGIN: called CC=1 SCAR=6.0
Subroutine get_parameterized: not called CC=5 SCAR=32.4
Branch: 2 branches
BranchTrueFalseTotal
if scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/6
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val)
!ll&&!rl&&r
000
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val)
scalar @_ > 0 not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val)
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table0%0/3
A and not(B)
A= scalar @_ > 0B= not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val)
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
291 0 return $self;
292 6 }
293  
294 =item get( [ Num $val ] )
295  
296 This is the generic dispatcher called when building the composite system. If an optional (numeric) parameter value is passed AND if the object has a parameterization, then it dispatches the object's C<get_parameterized> method. If no parameterization is available the object itself is returned.
297  
298 =cut
299  
300 6 ▶ method get (Num $val?) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine get: called CC=7 SCAR=20.1
Branch: 2 branches
BranchTrueFalseTotal
if scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val) 0 36 36
if scalar @_ > 1 0 36 36
Condition66%4/6
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val)
!ll&&!rl&&r
3060
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
330
MC/DC0%0/2
scalar @_ > 0 and not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val)
scalar @_ > 0 not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val)
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table66%2/3
A and not(B)
A= scalar @_ > 0B= not +($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($val)
ABresult
0X0
100
111
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
301 36 ▶ if (defined $val and $self->has_parameter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $self->has_parameter) { } 6 30 36
Condition66%2/3
defined $val and $self->has_parameter
!ll&&!rl&&r
3006
MC/DC50%1/2
defined $val and $self->has_parameter
defined $val $self->has_parameter
Truth table66%2/3
A and B
A= defined $valB= $self->has_parameter
ABresult
0X0
100
111
302 6 my $param_val = $self->parameter->($val);
303 6 return $self->get_parameterized($param_val);
304 } else {
305 30 return $self;
306 }
307 6 }
308  
309 =item times( Physics::RayTransfer::Element $right )
310  
311 This method performs matrix multiplication between the instance (the left matrix) and the passed in right matrix. The result is a new C<Physics::RayTransfer::Element> object containing the result of the multiplication.
312  
313 =cut
314  
315 6 ▶ method times (Physics::RayTransfer::Element $right) {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine times: called CC=5 SCAR=16.5
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 17 17
unless ($Method::Signatures::mutc{"cache"}{"Physics::RayTransfer::Element"} ||= "Method::Signatures::Modifiers"->_make_constraint("Physics::RayTransfer::Element"))->check($right) 0 17 17
if scalar @_ > 1 0 17 17
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Physics::RayTransfer::Element"} ||= "Method::Signatures::Modifiers"->_make_constraint("Physics::RayTransfer::Element")
l!l&&r!l&&!r
1520
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Physics::RayTransfer::Element"} ||= "Method::Signatures::Modifiers"->_make_constraint("Physics::RayTransfer::Element")
$Method::Signatures::mutc{"cache"}{"Physics::RayTransfer::Element"} "Method::Signatures::Modifiers"->_make_constraint("Physics::RayTransfer::Element")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Physics::RayTransfer::Element"}B= "Method::Signatures::Modifiers"->_make_constraint("Physics::RayTransfer::Element")
ABresult
1X1
011
000
316 17 my $a = $self->a * $right->a + $self->b * $right->c;
317 17 my $b = $self->a * $right->b + $self->b * $right->d;
318 17 my $c = $self->c * $right->a + $self->d * $right->c;
319 17 my $d = $self->c * $right->b + $self->d * $right->d;
320  
321 17 return Physics::RayTransfer::Element->new(
322 a => $a, b => $b, c => $c, d => $d,
323 );
324 6 }
325  
326 =item as_arrayref()
327  
328 A simple diagnostic method which return an array reference of the C<abcd> elements in order (note: this is in 1D C<[a, b, c, d]>).
329  
330 =cut
331  
332 6 ▶ method as_arrayref () {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine as_arrayref: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 21 21
333 21 return [$self->a, $self->b, $self->c, $self->d];
334 6 }
335  
336 =item stability( Num $lambda )
337  
338 This method returns the value of C<(a+d)/2>, this quantity is useful for judging the stability of a laser cavity. It takes a number representing the wavelength of the light used, this should be in the same units as any distances employed.
339  
340 =cut
341  
342 6 ▶ method stability (Num $lambda) {
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine __ANON__: called CC=5 SCAR=17.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 2 2
unless ($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($lambda) 0 2 2
if scalar @_ > 1 0 2 2
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
200
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
343 2 my $stability = ($self->a + $self->d) / 2;
344 2 return $stability;
345 6 }
346  
347 =item w( Num $lambda )
348  
349 This method returns the (Gaussian) spot size of a beam in the cavity at the observer. This is given by C<sqrt( abs(b) * $lambda / pi * sqrt( 1 / ( 1 - stability()**2 ) ) )>. It takes a number representing the wavelength of the light used, this should be in the same units as any distances employed.
350  
351 =cut
352  
353 6 ▶ method w (Num $lambda) {
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine w: called CC=6 SCAR=19.4
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
unless ($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($lambda) 0 1 1
if scalar @_ > 1 0 1 1
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
100
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
354 1 my $stability = $self->stability($lambda);
355 1 ▶ return undef if abs($stability) >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if abs $stability >= 1 0 1 1
356  
357 return sqrt(
358 1 abs($self->b) * $lambda / (4*atan2(1,1))
359 * sqrt(
360 1 / ( 1 - $stability ** 2 )
361 )
362 );
363 6 }
364  
365 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
366  
367 =pod
368  
369 =back
370  
371 =head3 SUBCLASSES
372  
373 =over
374  
375 =item Physics::RayTransfer::Observer
376  
377 Represents the position at which to evaluate the output (or cavity operation). Has no additional attributes or methods.
378  
379 =cut
380  
381 class Physics::RayTransfer::Observer
382 6 ▶ extends Physics::RayTransfer::Element {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
383  
384 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
385  
386 =item Physics::RayTransfer::Space
387  
388 Represents an amount of free propagation distance. Has the additional attribute C<length>. Its C<get_parameterized> method creates a new object directly setting the C<b> attribute (length).
389  
390 =cut
391  
392 class Physics::RayTransfer::Space
393 6 ▶ extends Physics::RayTransfer::Element {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
394  
395 6 has 'length' => (isa => 'Num', is => 'rw', default => 0);
396  
397 6 ▶ override _build_b () {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _build_b: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 14 14
398 14 return $self->length;
399 6 }
400  
401 6 ▶ override get_parameterized (Num $length) {
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine get_parameterized: called CC=5 SCAR=17.5
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 2 2
unless ($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($length) 0 2 2
if scalar @_ > 1 0 2 2
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
200
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
402 2 return __PACKAGE__->new( b => $length );
403 6 }
404 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
405  
406 =item Physics::RayTransfer::Mirror
407  
408 Represents a (possibly curved) mirror. Has the additional attribute C<radius>. Its C<get_parameterized> method returns a new object directly setting the C<c> attribute (C<c=-2/r>).
409  
410 =cut
411  
412 class Physics::RayTransfer::Mirror
413 6 ▶ extends Physics::RayTransfer::Element {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
414  
415 6 has 'radius' => (isa => 'Num', is => 'rw', predicate => 'has_radius');
416  
417 6 ▶ method _radius_to_c () {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _radius_to_c: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 8 8
418 8 my $c_term;
419  
420 8 ▶ if ($self->has_radius) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_radius) { } 2 6 8
421 2 $c_term = - 2 / $self->radius;
422 } else {
423 6 $c_term = 0;
424 }
425  
426 8 return $c_term;
427 6 }
428  
429 6 ▶ override _build_c () {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 8 8
430 8 my $c = $self->_radius_to_c;
431 8 return $c;
432 6 }
433  
434 6 ▶ override get_parameterized (Num $c) {
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine get_parameterized: called CC=5 SCAR=17.5
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 2 2
unless ($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($c) 0 2 2
if scalar @_ > 1 0 2 2
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
200
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
435 2 return __PACKAGE__->new( c => $c );
436 6 }
437 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
438  
439 =item Physics::RayTransfer::Lens
440  
441 Represents a lens. Has the additional attribute C<f>, indicating focal length. Note that if C<f> is not set, the C<c> matrix element is set to C<0>. Its C<get_parameterized> method returns a new object directly setting the C<c> attribute (C<c=-1/f>).
442  
443 =cut
444  
445 class Physics::RayTransfer::Lens
446 6 ▶ extends Physics::RayTransfer::Element {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
447  
448 6 has 'f' => (isa => 'Num', is => 'rw', predicate => 'has_f');
449  
450 6 ▶ method _f_to_c () {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _f_to_c: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 2 2
451 2 my $c_term;
452  
453 2 ▶ if ($self->has_f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_f) { } 1 1 2
454 1 $c_term = - 1 / $self->f;
455 } else {
456 1 $c_term = 0;
457 }
458  
459 2 return $c_term;
460 6 }
461  
462 6 ▶ override _build_c () {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _build_c: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 2 2
463 2 my $c = $self->_f_to_c;
464 2 return $c;
465 6 }
466  
467 6 ▶ override get_parameterized (Num $c) {
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine __ANON__: called CC=5 SCAR=17.5
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 2 2
unless ($Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num"))->check($c) 0 2 2
if scalar @_ > 1 0 2 2
Condition33%1/3
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
l!l&&r!l&&!r
200
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Num"} ||= "Method::Signatures::Modifiers"->_make_constraint("Num")
$Method::Signatures::mutc{"cache"}{"Num"} "Method::Signatures::Modifiers"->_make_constraint("Num")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Num"}B= "Method::Signatures::Modifiers"->_make_constraint("Num")
ABresult
1X1
011
000
468 2 return __PACKAGE__->new( c => $c );
469 6 }
470 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
471  
472 =pod
473  
474 =back
475  
476 =head1 SEE ALSO
477  
478 =over
479  
480 =item * L<"Encyclopedia of Laser Physics and Technology"|http://www.rp-photonics.com/abcd_matrix.html>
481  
482 =item * L<"Wikipedia"|http://en.wikipedia.org/wiki/Ray_transfer_matrix_analysis>
483  
484 =back
485  
486 =head1 SOURCE REPOSITORY
487  
488 L<http://github.com/jberger/Physics-RayTransfer>
489  
490 =head1 AUTHOR
491  
492 Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
493  
494 =head1 COPYRIGHT AND LICENSE
495  
496 Copyright (C) 2012 by Joel Berger
497  
498 This library is free software; you can redistribute it and/or modify
499 it under the same terms as Perl itself.
500  
501  
502  
↑ Summary