blib/lib/Physics/Particles.pm

filter: Statementstmt 87.7% 100 / 114 Branchbran 42.8% 6 / 14 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 82.3% 14 / 17 Podpod 100% 8 / 8 totaltotal 78.5% 132 / 168 CC 17 SCAR 16.8
CC
17
Cov
80%
remove_particle
12.0
iterate_step
10.0
set_particle_default
6.0
CRAP 5.4
↑ Summary
1  
2 # See the POD documentation at the end of this
3 # document for detailed copyright information.
4 # (c) 2002-2003 Steffen Mueller, all rights reserved.
5  
6 package Physics::Particles;
7  
8 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use constant C_VACUUM => 299792458;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use constant C_VACUUM_SQUARED => 299792458 * 299792458;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 $VERSION = '1.02';
21  
22  
23 # constructor new
24 #
25 # Does not require any arguments. All arguments
26 # directly modify the object as key/value pairs.
27 # returns freshly created simulator object.
28  
29 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
30 1 my $proto = shift;
31 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
32  
33 # Make a new object with default values.
34 1 my $self = {
35 forces => [], # All forces (callbacks) will be stored here.
36 p => [], # All particles (hashrefs) will be stored here.
37 p_attr => {
38 x => 0,
39 y => 0,
40 z => 0,
41 vx => 0,
42 vy => 0,
43 vz => 0,
44 m => 1,
45 n => '',
46 },
47 @_
48 };
49  
50 1 bless $self => $class;
51 }
52  
53  
54 # method set_particle_default
55 #
56 # Takes a hash reference whichis then used as is for
57 # particle default attributes such as position, velocity, mass,
58 # unique ID, or whatever properties you fancy.
59  
60 0 ▶sub set_particle_default {
Subroutine set_particle_default: not called CC=2 SCAR=17.9
Pod documented
61 0 my $self = shift;
62 0 my $hashref = shift;
63
64 0 ▶ ref $hashref eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $hashref eq "HASH" or croak("Must pass hash reference to set_particle_default().") 0 0 0
65 or croak "Must pass hash reference to set_particle_default().";
66
67 0 $self->{p_attr} = $hashref;
68 0 return 1;
69 }
70  
71 # method add_particle
72 #
73 # Takes key/value pairs of particle attributes.
74 # Attributes default to whatever has been set using
75 # set_particle_default.
76 # A new particle represented by the attributes is then
77 # created in the simulation and its particle number is
78 # returned. Attributes starting with an underscore are
79 # reserved to internal attributes (so don't use any of them).
80  
81 6 ▶sub add_particle {
Subroutine add_particle: called CC=1 SCAR=0.0
Pod documented
82 6 my $self = shift;
83
84 my $particle = {
85 6 %{$self->{p_attr}},
86 @_,
87 _fx => 0,
88 _fy => 0,
89 _fz => 0,
90 };
91
92 6 my $particle_no = $self->_make_particle();
93
94 6 $self->{p}[$particle_no] = $particle;
95
96 6 return $particle_no;
97 }
98  
99  
100 # private method _make_particle
101 #
102 # Returns a currently unused particle number or
103 # appends an empty particle to the particle list.
104  
105 6 ▶sub _make_particle {
Subroutine _make_particle: called CC=3 SCAR=11.0
106 6 my $self = shift;
107 6 my $count = 0;
108 6 foreach (@{$self->{p}}) {
109 15 ▶ return $count unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 15 15
110 15 $count++;
111 }
112
113 6 push @{ $self->{p} }, undef;
114 6 return $count;
115 }
116  
117  
118 # method clear_particles
119 #
120 # Removes all particles from the simulation.
121  
122 0 ▶sub clear_particles {
Subroutine clear_particles: not called CC=1 SCAR=6.9
Pod documented
123 0 my $self = shift;
124 0 $self->{p} = [];
125 0 return 1;
126 }
127  
128  
129 # method remove_particle
130 #
131 # Removes a specific particle from the simulation.
132 # Takes the particle number as argument.
133 # Returns 1 on success, 0 otherwise.
134  
135 0 ▶sub remove_particle {
Subroutine remove_particle: not called CC=3 SCAR=24.8
Pod documented
136 0 my $self = shift;
137 0 my $particle_no = shift;
138
139 0 ▶ return 0 if $particle_no < 0 || $particle_no > $#{$self->{p}};
Branch: 1 branch
BranchTrueFalseTotal
if $particle_no < 0 or $particle_no > $#{$$self{"p"};} 0 0 0
Condition0%0/3
$particle_no < 0 or $particle_no > $#{$$self{"p"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$particle_no < 0 or $particle_no > $#{$$self{"p"};}
$particle_no < 0 $particle_no > $#{$$self{"p"};}
Truth table0%0/3
A or B
A= $particle_no < 0B= $particle_no > $#{$$self{"p"};}
ABresult
1X1
011
000
140
141 0 $self->{p}[$particle_no] = undef;
142
143 0 return 1;
144 }
145  
146  
147 # method add_force
148 #
149 # Adds a new force to the simulation.
150 # Takes a subroutine reference (the force) as argument,
151 # appends it to the list of forces and returns the force it
152 # just appended. Second argument is optional and defaults to false.
153 # It is a boolean indicating whether or not the force is symmetric.
154 # Symmetric means that the force particle1 excerts on particle2
155 # is exactly zero minus the force particle2 excerts on particle1.
156  
157 1 ▶sub add_force {
Subroutine add_force: called CC=2 SCAR=7.0
Pod documented
158 1 my $self = shift;
159 1 my $force = shift;
160 1 my $symmetric = shift;
161 1 ▶ $symmetric = 0 unless defined $symmetric;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $symmetric 0 1 1
162
163 1 push @{$self->{forces}}, [$force, $symmetric];
164 1 return $force;
165 }
166  
167  
168 # method iterate_step
169 #
170 # Applies all forces (excerted by every particle) to all particles.
171 # That means this is of complexity no_particles*no_particles*forces.
172 # Takes a list of additional parameters as argument that will be
173 # passed to every force subroutine.
174  
175 10 ▶sub iterate_step {
Subroutine iterate_step: called CC=10 SCAR=23.0
Pod documented
176 10 my $self = shift;
177 10 my @params = @_;
178  
179 10 my $forces = [];
180 10 foreach (@{ $self->{p} }) {
181 60 push @$forces, [$_->{_fx}, $_->{_fy}, $_->{_fz}];
182 60 $_->{_fx} = $_->{_fy} = $_->{_fz} = 0;
183 }
184  
185 10 my $new_state = [];
186  
187 10 my $p_no = 0;
188 10 foreach my $p (@{ $self->{p} }) {
189 60 my $new_p = { %$p };
190 60 push @$new_state, $new_p;
191 60 my $f = $forces->[$p_no];
192 60 foreach my $force_def (@{$self->{forces}}) {
193 60 my $symm = $force_def->[1];
194 60 my $force = $force_def->[0];
195 60 my $exc_no = 0;
196 60 foreach my $excerter (@{$self->{p}}) {
197 210 ▶ last if $symm and $exc_no >= $p_no;
Branch: 1 branch
BranchTrueFalseTotal
if $symm and $exc_no >= $p_no 60 150 210
Condition66%2/3
$symm and $exc_no >= $p_no
!ll&&!rl&&r
015060
MC/DC50%1/2
$symm and $exc_no >= $p_no
$symm $exc_no >= $p_no
Truth table66%2/3
A and B
A= $symmB= $exc_no >= $p_no
ABresult
0X0
100
111
198 150 ▶ $exc_no++, next if $exc_no == $p_no;
Branch: 1 branch
BranchTrueFalseTotal
if $exc_no == $p_no 0 150 150
199 150 my @this_f = $force->(
200 $p,
201 $excerter,
202 \@params
203 );
204 150 $f->[0] += $this_f[0];
205 150 $f->[1] += $this_f[1];
206 150 $f->[2] += $this_f[2];
207 150 ▶ if ($symm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symm) 150 0 150
208 150 $forces->[$exc_no][0] -= $this_f[0];
209 150 $forces->[$exc_no][1] -= $this_f[1];
210 150 $forces->[$exc_no][2] -= $this_f[2];
211 }
212 150 $exc_no++;
213 }
214 }
215 60 $p_no++;
216 }
217  
218 10 $p_no = 0;
219 10 foreach my $new_p (@$new_state) {
220 60 my $f = $forces->[$p_no++];
221 # accel_i = force_i / mass (i-th component)
222 my $m = $new_p->{m} /
223 sqrt(1 -
224 ($new_p->{vx}**2 +
225 $new_p->{vy}**2 +
226 60 $new_p->{vz}**2) /
227 C_VACUUM_SQUARED
228 );
229 60 my @a = ($f->[0] / $m, $f->[1] / $m, $f->[2] / $m);
230  
231 60 @a = map $_ * $params[0], @a;
232
233 60 $new_p->{x} += $new_p->{vx} * $params[0] +
234 0.5 * $a[0] * $params[0];
235 60 $new_p->{y} += $new_p->{vy} * $params[0] +
236 0.5 * $a[1] * $params[0];
237 60 $new_p->{z} += $new_p->{vz} * $params[0] +
238 0.5 * $a[2] * $params[0];
239  
240 60 $new_p->{vx} += $a[0];
241 60 $new_p->{vy} += $a[1];
242 60 $new_p->{vz} += $a[2];
243 }
244
245 10 $self->{p} = $new_state;
246  
247 10 return 1;
248 }
249  
250  
251 # method dump_state
252 #
253 # Returns a Data::Dumper dump of the state of all particles.
254  
255 1 ▶sub dump_state {
Subroutine dump_state: called CC=1 SCAR=0.0
Pod documented
256 1 my $self = shift;
257 1 return Dumper($self->{p});
258 }
259  
260 2 1;
261  
262  
263 __END__
↑ Summary