| 1 | package Math::ODE; | ||||||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 7 | my $VERSION = '0.07'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 7 | $Data::Dumper::Varname = "y"; | |||||||||||||||||||||||||||||||
| 10 | 7 | $Data::Dumper::Indent = 1; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | sub evolve | ||||||||||||||||||||||||||||||||
| 13 | 41 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
evolve: called CC=14 SCAR=27.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 14 | 41 | my $self = shift; | |||||||||||||||||||||||||||||||
| 15 | 41 | my ($F,$h,$t,$initial,$file) = map{ $self->{$_} } qw(ODE step t0 initial file); | |||||||||||||||||||||||||||||||
| 16 | 41 | ▶ | my $delim = $self->{csv} ? ',' : ($self->{delim} || $self->{delimeter} || " "); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
$self->{"delim"} or $self->{"delimeter"} or " "
$self->{"delim"} or $self->{"delimeter"}
MC/DC0%0/2
$self->{"delim"} or $self->{"delimeter"} or " "
$self->{"delim"}
$self->{"delimeter"}
Truth table33%1/3
A or B
A= $self->{"delim"}B= $self->{"delimeter"}
| |||||||||||||||||||||||||||||||||
| 17 | 41 | my $y; | |||||||||||||||||||||||||||||||
| 18 | my $fh; | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | # don't clobber the initial condition in case we want to do multiple runs | ||||||||||||||||||||||||||||||||
| 21 | # with the same object | ||||||||||||||||||||||||||||||||
| 22 | 41 | @$y = @$initial; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 41 | ▶ | if( defined $file ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | open($fh,'>', $file) or croak "$file: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 41 | $self->_clear_values; | |||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 41 | ▶ | while ( $t < $self->{tf} ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | # use Runge Kutta 4th order to step from $t to $t + $h | ||||||||||||||||||||||||||||||||
| 32 | 12283 | $y = _RK4($self,$t,$y); | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 12283 | ▶ | warn "Exiting RK4 with t=$t ," . Dumper($y) . "\n" if( $self->{verbose} > 1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 12283 | for my $i ( 0 .. $self->{N}-1 ){ | |||||||||||||||||||||||||||||||
| 37 | # check for under/over flow | ||||||||||||||||||||||||||||||||
| 38 | 22900 | ▶ | next unless $y->[$i] =~ qr/nan|infinity/i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | warn "Bailing out, over/under flow at t=$t,y->[$i] = $y->[$i]" if $self->{verbose}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 0 | return undef; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | 12283 | $t += $h; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 12283 | my $str = join $delim, map { sprintf $self->{format}, $_ } ($t, @$y); | |||||||||||||||||||||||||||||||
| 45 | 12283 | chop $str; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 12283 | ▶ | if( defined $file ){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 48 | 44 | print $fh "$str\n"; | |||||||||||||||||||||||||||||||
| 49 | } elsif ( ! $self->{keep_values} ) { | ||||||||||||||||||||||||||||||||
| 50 | 11 | print "$str\n"; | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | 41 | ▶ | close $fh if defined $file; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 41 | return $self; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | sub _RK4 | ||||||||||||||||||||||||||||||||
| 58 | 12283 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_RK4: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 59 | # $t = dependent variable | ||||||||||||||||||||||||||||||||
| 60 | # $y = $N - vector of independent variables | ||||||||||||||||||||||||||||||||
| 61 | # $h = step size | ||||||||||||||||||||||||||||||||
| 62 | # $F = arrayref of coderefs of the equations to solve | ||||||||||||||||||||||||||||||||
| 63 | 12283 | my ($self,$t,$y) = @_; | |||||||||||||||||||||||||||||||
| 64 | 12283 | my $F = $self->{ODE}; | |||||||||||||||||||||||||||||||
| 65 | 12283 | my $h = $self->{step}; | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | ## w vectors hold constants for equations | ||||||||||||||||||||||||||||||||
| 68 | ## each $q holds a modified $y vector to feed to the next | ||||||||||||||||||||||||||||||||
| 69 | ## for loop ( ie $y + $w1/2 , etc ... ) | ||||||||||||||||||||||||||||||||
| 70 | 12283 | my (@w1,@w2,@w3,@w4,$q); | |||||||||||||||||||||||||||||||
| 71 | 12283 | my @indices = ( 0 .. $self->{N} - 1 ); | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 12283 | map { $w1[$_] = $h * &{ $F->[$_] }($t,$y) } @indices; | |||||||||||||||||||||||||||||||
| 74 | 12283 | map { $q->[$_] = $y->[$_] + 0.5*$w1[$_] } @indices; | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 12283 | map { $w2[$_] = $h * &{ $F->[$_] }($t + 0.5*$h,$q) } @indices; | |||||||||||||||||||||||||||||||
| 77 | 12283 | map { $q->[$_] = $y->[$_] + 0.5*$w2[$_] } @indices; | |||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 12283 | map { $w3[$_] = $h * &{ $F->[$_] }($t + 0.5*$h,$q) } @indices; | |||||||||||||||||||||||||||||||
| 80 | 12283 | map { $q->[$_] = $y->[$_] + $w3[$_] } @indices; | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 12283 | map { $w4[$_] = $h * &{ $F->[$_] }($t + $h,$q) } @indices; | |||||||||||||||||||||||||||||||
| 83 | 12283 | map { $y->[$_]+= ( $w1[$_] + 2 * $w2[$_] + 2 * $w3[$_] + $w4[$_])/6 } @indices; | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 12283 | $self->_store_values( $t + $h, $y ); | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 12283 | return $y; | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | sub _store_values | ||||||||||||||||||||||||||||||||
| 91 | 12283 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_store_values: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 92 | 12283 | my ($self,$t, $y) = @_; | |||||||||||||||||||||||||||||||
| 93 | 12283 | ▶ | return unless $self->{keep_values}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 12261 | my $s = sprintf $self->{format}, $t ; | |||||||||||||||||||||||||||||||
| 95 | 12261 | push @{ $self->{values}{$s} }, @$y; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | sub values_at | ||||||||||||||||||||||||||||||||
| 99 | 12230 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
values_at: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 100 | 12230 | my ($self,$t, %args) = @_; | |||||||||||||||||||||||||||||||
| 101 | 12230 | ▶ | if ($self->{keep_values}){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | 12229 | return @{ $self->{values}{sprintf($self->{format},$t)} }; | |||||||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||||||
| 104 | 1 | warn "Values were not kept because keep_values was set to 0"; | |||||||||||||||||||||||||||||||
| 105 | 1 | return; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | # because Math::ODE implements a 4th order Runge-Kutta method | ||||||||||||||||||||||||||||||||
| 109 | 73 | ▶ | sub error { $_[0]->{step} ** 4 } | ||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | sub file | ||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
file: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 113 | 1 | my ($self,$target) = @_; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | if ($target) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | 0 | $self->{file} = $target; | |||||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||||||
| 118 | 1 | return $self->{file}; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | sub format | ||||||||||||||||||||||||||||||||
| 123 | 4 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 124 | 4 | my ($self,$format) = @_; | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 4 | ▶ | $format ? $self->{format} = $format : return $self->{format}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | sub step | ||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
step: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 131 | 0 | my ($self,$step) = @_; | |||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if (defined $step){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | croak "Stepsize must be strictly between zero and one" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$step <= 0 or $step >= 1
MC/DC0%0/2
$step <= 0 or $step >= 1
$step <= 0
$step >= 1
Truth table0%0/3
A or B
A= $step <= 0B= $step >= 1
| |||||||||||||||||||||||||||||||||
| 134 | if ($step <= 0 || $step >= 1); | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 0 | $self->{step} = $step; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | 0 | return $self->{step}; | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | sub initial | ||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
initial: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 143 | 0 | my ($self, $initial) = @_; | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | croak "not an array ref" unless ref $initial eq "ARRAY"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 0 | $self->{initial} = $initial; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | sub _clear_values | ||||||||||||||||||||||||||||||||
| 152 | 82 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_clear_values: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 153 | 82 | my $self = shift; | |||||||||||||||||||||||||||||||
| 154 | $self->{values} = {} | ||||||||||||||||||||||||||||||||
| 155 | 82 | } | |||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | sub _init | ||||||||||||||||||||||||||||||||
| 158 | 41 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||||||
| 159 | 41 | my ($self,%args) = @_; | |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | # defaults | ||||||||||||||||||||||||||||||||
| 162 | 41 | $self->{keep_values} = 1; | |||||||||||||||||||||||||||||||
| 163 | 41 | $self->{verbose} = 1; | |||||||||||||||||||||||||||||||
| 164 | 41 | $self->{step} = 0.1; | |||||||||||||||||||||||||||||||
| 165 | 41 | $self->{csv} = 0; | |||||||||||||||||||||||||||||||
| 166 | 41 | ▶ | $self->{N} = scalar( @{ $args{ODE} } ) || 1; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
scalar @{$args{'ODE'};} || 1
MC/DC0%0/1
scalar @{$args{'ODE'};} || 1
scalar @{$args{'ODE'};}
Truth table50%1/2
A
A= scalar @{$args{'ODE'};}
| |||||||||||||||||||||||||||||||||
| 167 | 41 | $self->{format} = '%.12f'; | |||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 41 | @$self{keys %args} = values %args; | |||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 41 | $self->_clear_values; | |||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 41 | ▶ | if( $self->{N} != scalar( @{ $args{initial } }) ){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 174 | 0 | croak "Must have same number of initial conditions as equations!"; | |||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 41 | ▶ | croak "Stepsize must be positive!" if $self->{step} <= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 41 | ▶ | croak "\$self->t0 must be less than \$self->tf!" if $self->{t0} >= $self->{tf}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 41 | return $self; | |||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | sub max_error | ||||||||||||||||||||||||||||||||
| 184 | 36 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
max_error: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 185 | 36 | my ($self, $sols) = @_; | |||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 36 | my $max_error = 0; | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 36 | for my $pt ( sort keys %{$self->{values}} ){ | |||||||||||||||||||||||||||||||
| 190 | 12228 | my $k = 0; | |||||||||||||||||||||||||||||||
| 191 | 12228 | my @vals = $self->values_at($pt); | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 12228 | for my $sol ( @$sols ) { | |||||||||||||||||||||||||||||||
| 194 | 15767 | my $res = abs( $vals[$k] - &$sol($pt) ); | |||||||||||||||||||||||||||||||
| 195 | 15767 | ▶ | $max_error = $res if ($res > $max_error); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | 15767 | $k++; | |||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | 36 | $max_error; | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | 41 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 203 | 41 | my $class = shift; | |||||||||||||||||||||||||||||||
| 204 | 41 | my $self = {}; | |||||||||||||||||||||||||||||||
| 205 | 41 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 206 | 41 | $self->_init(@_); | |||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | 7 | 42; | |||||||||||||||||||||||||||||||
| 210 | __END__ | ||||||||||||||||||||||||||||||||