blib/lib/Physics/Springs.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 84.8% 84 / 99 CC 7 SCAR 9.8
CC
7
Cov
88%
add_spring
5.5
new
2.1
iterate_step
2.0
CRAP 2.7
↑ Summary
1 package Physics::Springs;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '1.01';
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Physics::Particles;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Sub::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use base 'Physics::Particles';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
17 1 my $proto = shift;
18 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
19  
20 1 my $self = $class->SUPER::new();
21 1 $self->{_PhSprings_springs} = [];
22 1 return $self;
23 }
24  
25 2 assert
26 pre => '@PARAM == 1',
27 post => '@RETURN == 1',
28 context => 'novoid',
29 action => 'croak',
30 sub => 'new';
31  
32 3 ▶sub add_spring {
Subroutine add_spring: called CC=5 SCAR=17.1
33 3 my $self = shift;
34 3 my %args = @_;
35 3 my $spring = {};
36 3 my $k = $args{k};
37 3 my $p1 = $args{p1};
38 3 my $p2 = $args{p2};
39 3 my $l = $args{l};
40  
41 3 ▶ defined($k) && defined($p1) && defined($p2)
Branch: 1 branch
BranchTrueFalseTotal
defined $k and defined $p1 and defined $p2 or croak("You need to supply several named arguments.") 3 0 3
Condition33%2/6
defined $k and defined $p1 and defined $p2
!ll&&!rl&&r
003
defined $k and defined $p1
!ll&&!rl&&r
003
MC/DC0%0/3
defined $k and defined $p1 and defined $p2
defined $k defined $p1 defined $p2
Truth table25%1/4
A and B and C
A= defined $kB= defined $p1C= defined $p2
ABCresult
0XX0
10X0
1100
1111
42 or croak("You need to supply several named arguments.");
43  
44 # default to being relaxed
45 3 ▶ if (not defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $l) 0 3 3
46 my $dist = sqrt(
47 ( ($self->{p}[$p1]{x} - $self->{p}[$p2]{x}) )**2 +
48 ( ($self->{p}[$p1]{y} - $self->{p}[$p2]{y}) )**2 +
49 0 ( ($self->{p}[$p1]{z} - $self->{p}[$p2]{z}) )**2
50 );
51 0 $l = abs($dist);
52 }
53  
54 3 $spring->{k} = $k;
55 3 $spring->{p1} = $p1;
56 3 $spring->{p2} = $p2;
57 3 $spring->{len} = $l;
58  
59 3 push @{$self->{_PhSprings_springs}}, $spring;
60 3 return $#{$self->{_PhSprings_springs}};
61 }
62  
63 2 assert
64 pre => '@PARAM >= 9',
65 post => '$VOID or @RETURN == 1 && $RETURN >= 0',
66 action => 'croak',
67 sub => 'add_spring';
68  
69 10 ▶sub iterate_step {
Subroutine iterate_step: called CC=2 SCAR=6.9
Pod documented
70 10 my $self = shift;
71 10 my @params = @_;
72 10 my $time_diff = $params[0];
73 10 foreach my $spring (@{$self->{_PhSprings_springs}}) {
74 30 my $p1 = $self->{p}[$spring->{p1}];
75 30 my $p2 = $self->{p}[$spring->{p2}];
76 30 my $l = $spring->{len};
77 30 my $k = $spring->{k};
78  
79 my $dist = sqrt(
80 ( ($p1->{x} - $p2->{x}) )**2 +
81 ( ($p1->{y} - $p2->{y}) )**2 +
82 30 ( ($p1->{z} - $p2->{z}) )**2
83 );
84  
85 30 my $force1 = $k * ($dist-$l);
86 30 my $force2 = -$force1;
87  
88 30 my $dx = ($p2->{x} - $p1->{x}) / $dist;
89 30 my $dy = ($p2->{y} - $p1->{y}) / $dist;
90 30 my $dz = ($p2->{z} - $p1->{z}) / $dist;
91  
92 30 $p1->{_fx} += $force1 * $dx;
93 30 $p1->{_fy} += $force1 * $dy;
94 30 $p1->{_fz} += $force1 * $dz;
95  
96 30 $p2->{_fx} += $force2 * $dx;
97 30 $p2->{_fy} += $force2 * $dy;
98 30 $p2->{_fz} += $force2 * $dz;
99 }
100  
101 10 $self->SUPER::iterate_step(@params);
102 }
103  
104 2 1;
105 __END__
↑ Summary