| 1 | package Text::NumericData::App::txdodeint; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Text::NumericData::App; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Text::NumericData::Calc qw(formula_function); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # This is just a placeholder because of a past build system bug. | ||||||||||||||||||||||||||||
| 9 | # The one and only version for Text::NumericData is kept in | ||||||||||||||||||||||||||||
| 10 | # the Text::NumericData module itself. | ||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '1'; | |||||||||||||||||||||||||||
| 12 | 1 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | my $infostring = "integrate a given ordinary differential equation system along a coordinate | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Usage: | ||||||||||||||||||||||||||||
| 17 | pipe | txdodeint [parameters] [<ode> [<initval> [initval ...]]] | pipe | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | By default, the first column in the data is used as time coordinate for which | ||||||||||||||||||||||||||||
| 20 | the expression(s) of the ODE are defined. The derivative can be a | ||||||||||||||||||||||||||||
| 21 | system of ODEs. The time derivative values shall be stored in the array members | ||||||||||||||||||||||||||||
| 22 | D0 .. Dn, with the current corresponding variable values available in V0 .. Vn | ||||||||||||||||||||||||||||
| 23 | and the corresponding values of the (interpolated) auxilliary timeseries | ||||||||||||||||||||||||||||
| 24 | from the piped data in [1] .. [m]. There are also the auxillary array values | ||||||||||||||||||||||||||||
| 25 | A0 .. Ak (to be used at your leisure to store/load values) and the constants | ||||||||||||||||||||||||||||
| 26 | C0 .. Cl (initialised from program parameters). | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | For convenience, the basic setup of time column, ODE, and intial values can | ||||||||||||||||||||||||||||
| 29 | be given directly on the command line without mentioning parameter names. | ||||||||||||||||||||||||||||
| 30 | The integrated values are written out at the points in time given by the input | ||||||||||||||||||||||||||||
| 31 | data. For each variable (size of the array V), a column is appended. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Example for a simple system (constant acceleration): | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | D0=V1; D1=A0; D2=C0*[1] | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Assuming the time in column 1 and the constant acceleration in C0, | ||||||||||||||||||||||||||||
| 38 | this computes the evolution of the covered distance V0 via the numerically | ||||||||||||||||||||||||||||
| 39 | accelerated speed V1 and also directly from the analytically accelerated | ||||||||||||||||||||||||||||
| 40 | speed as variable V2, to give a hint about the accuracy of the numerical | ||||||||||||||||||||||||||||
| 41 | integration. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | The employed integration method is a standard Runge-Kutta scheme with up to | ||||||||||||||||||||||||||||
| 44 | 4 stages, which should be fine for any application where you consider a | ||||||||||||||||||||||||||||
| 45 | humble Perl script for your numerical integration. A comparison of the fully | ||||||||||||||||||||||||||||
| 46 | numerical with the fully analytical solution can be constructed via the | ||||||||||||||||||||||||||||
| 47 | pipeline | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | txdconstruct -n=11 '[1]=C0-1' \\ | ||||||||||||||||||||||||||||
| 50 | | txdodeint --rksteps=1 'D0=V1; D1=3' 0 0 \\ | ||||||||||||||||||||||||||||
| 51 | | txdcalc '[4]=3/2*[1]**2; [5]=[4] ? ([2]-[4])/[4] : 0' \\ | ||||||||||||||||||||||||||||
| 52 | | txdfilter -N=%g 'integration test' 't/s' 's/m' 'v/m' 's_ref/m' 'error' | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | With rksteps>1, you will not see any difference in this example. In general, | ||||||||||||||||||||||||||||
| 55 | the choice of integration stages, time step and interpolation might have | ||||||||||||||||||||||||||||
| 56 | significant influence on your results. A simple test of the quality of the | ||||||||||||||||||||||||||||
| 57 | chosen integration employs trivial polynomials: | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | txdconstruct -n=11 '[1]=C0-1' \\ | ||||||||||||||||||||||||||||
| 60 | | txdodeint --rksteps=2 --timediv=1 \\ | ||||||||||||||||||||||||||||
| 61 | 'D0=1; D1=2*[1]; D2=3*[1]**2; D3=4*[1]**3' \\ | ||||||||||||||||||||||||||||
| 62 | 0 0 0 0 \\ | ||||||||||||||||||||||||||||
| 63 | | txdcalc '[2]-=[1]; [3]-=[1]**2; [4]-=[1]**3; [5]-=[1]**4' \\ | ||||||||||||||||||||||||||||
| 64 | | txdfilter -N=g 'integration order test' x err0 err1 err2 err3 | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | These polynomials can actually be solved exactly to machine precision | ||||||||||||||||||||||||||||
| 67 | (depending on rksteps value) and smaller time steps would introduce | ||||||||||||||||||||||||||||
| 68 | rounding errors here from the summation. Finally, another classic example, | ||||||||||||||||||||||||||||
| 69 | the Lorenz attractor: | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | txdconstruct -n=5001 '[1]=(C0-1)/100' | txdodeint --timediv=1 \\ | ||||||||||||||||||||||||||||
| 72 | 'D0=10*(V1-V0); D1=28*V0-V1-V0*V2; D2=-8/3*V2+V0*V1' \\ | ||||||||||||||||||||||||||||
| 73 | 20 -20 1 | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | More practical applications actually have some more data columns besides | ||||||||||||||||||||||||||||
| 76 | the time in the input data (measurements) and involve derivative expressions | ||||||||||||||||||||||||||||
| 77 | that make use of this data-driven time-dependence."; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | our @ISA = ('Text::NumericData::App'); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub new | ||||||||||||||||||||||||||||
| 82 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 84 | 1 | my @pars = | |||||||||||||||||||||||||||
| 85 | ( | ||||||||||||||||||||||||||||
| 86 | 'timecol', 1, 't' | ||||||||||||||||||||||||||||
| 87 | , 'Column for the (time) coordinate the ODE shall be advanced on.' | ||||||||||||||||||||||||||||
| 88 | . ' In the ODE, you can access it via [1] if the column is 1 (just like' | ||||||||||||||||||||||||||||
| 89 | . ' any other variable of the (interpolated) input data).' | ||||||||||||||||||||||||||||
| 90 | , 'ode', 'D0 = 1', 'e' | ||||||||||||||||||||||||||||
| 91 | , 'The ordinary differential equation system. The return value of the generated' | ||||||||||||||||||||||||||||
| 92 | . ' function does not matter, only that you set the values of the D array.' | ||||||||||||||||||||||||||||
| 93 | , 'varinit', [], 'i' | ||||||||||||||||||||||||||||
| 94 | , 'array of initial values; must match number of derivatives from ODE' | ||||||||||||||||||||||||||||
| 95 | , 'vartitle', [], '' | ||||||||||||||||||||||||||||
| 96 | , 'array of column titles for the integrated variables' | ||||||||||||||||||||||||||||
| 97 | , 'const', [], 'n' | ||||||||||||||||||||||||||||
| 98 | , 'array of constants to use in ODE' | ||||||||||||||||||||||||||||
| 99 | , 'rksteps', '4', 'k' | ||||||||||||||||||||||||||||
| 100 | , 'steps (stages) of the RK integration scheme, only 4 supported right now' | ||||||||||||||||||||||||||||
| 101 | , 'timestep', 0, 's' | ||||||||||||||||||||||||||||
| 102 | , 'desired time step size (see timediv)' | ||||||||||||||||||||||||||||
| 103 | , 'timediv', 10, '' | ||||||||||||||||||||||||||||
| 104 | , 'Divide input time intervals by that to get the integration time step. If' | ||||||||||||||||||||||||||||
| 105 | . ' timestep is set to non-zero, still an integer division is used, but one that' | ||||||||||||||||||||||||||||
| 106 | . ' yields a step close to the desired one (subject to rounding).' | ||||||||||||||||||||||||||||
| 107 | , 'interpolate', 'spline', '' | ||||||||||||||||||||||||||||
| 108 | , 'type of interpolation to use for intermediate points: spline or linear' | ||||||||||||||||||||||||||||
| 109 | , 'debug', 0, 'd' | ||||||||||||||||||||||||||||
| 110 | , 'give some info that may help debugging, >1 increasing verbosity' | ||||||||||||||||||||||||||||
| 111 | , 'plainperl', 0, '' | ||||||||||||||||||||||||||||
| 112 | , 'Use plain Perl syntax for formula for full force without confusing the' | ||||||||||||||||||||||||||||
| 113 | . ' intermediate parser.' | ||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | 1 | return $class->SUPER::new | |||||||||||||||||||||||||||
| 116 | ({ | ||||||||||||||||||||||||||||
| 117 | parconf=>{ info=>$infostring } | ||||||||||||||||||||||||||||
| 118 | , pardef=>\@pars | ||||||||||||||||||||||||||||
| 119 | , filemode=>1 | ||||||||||||||||||||||||||||
| 120 | , pipemode=>1 | ||||||||||||||||||||||||||||
| 121 | , pipe_init=>\&prepare | ||||||||||||||||||||||||||||
| 122 | , pipe_file=>\&process_file | ||||||||||||||||||||||||||||
| 123 | }); | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub prepare | ||||||||||||||||||||||||||||
| 127 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=11 SCAR=31.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | 1 | my $p = $self->{param}; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | $p->{ode} = shift(@{$self->{argv}}) | |||||||||||||||||||||||||||
| 132 | 1 | ▶ | if(@{$self->{argv}}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | $p->{varinit} = $self->{argv} | ||||||||||||||||||||||||||||
| 134 | 1 | ▶ | if(@{$self->{argv}}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | $self->{rk} = {}; | |||||||||||||||||||||||||||
| 137 | 1 | my $rk = $self->{rk}; | |||||||||||||||||||||||||||
| 138 | 1 | $rk->{stages} = 0; | |||||||||||||||||||||||||||
| 139 | 1 | $rk->{a} = []; | |||||||||||||||||||||||||||
| 140 | 1 | $rk->{b} = []; | |||||||||||||||||||||||||||
| 141 | 1 | $rk->{c} = []; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # The generic rksteps might be something funny (like 33, 45) in case | ||||||||||||||||||||||||||||
| 144 | # I intend to introduce methods that differ in stages and order. | ||||||||||||||||||||||||||||
| 145 | 1 | ▶ | if($p->{rksteps} == 1) # Euler | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | { | ||||||||||||||||||||||||||||
| 147 | 0 | $rk->{stages} = 1; | |||||||||||||||||||||||||||
| 148 | 0 | $rk->{a} = [ [0] ]; | |||||||||||||||||||||||||||
| 149 | 0 | $rk->{b} = [ 1 ]; | |||||||||||||||||||||||||||
| 150 | 0 | $rk->{c} = [ 0 ]; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 1 | ▶ | if($p->{rksteps} == 2) # Heun | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||
| 154 | 0 | $rk->{stages} = 2; | |||||||||||||||||||||||||||
| 155 | $rk->{a} = | ||||||||||||||||||||||||||||
| 156 | [ | ||||||||||||||||||||||||||||
| 157 | 0 | [ 0, 0 ] | |||||||||||||||||||||||||||
| 158 | , [ 1, 0 ] | ||||||||||||||||||||||||||||
| 159 | ]; | ||||||||||||||||||||||||||||
| 160 | 0 | $rk->{b} = [ 0.5, 0.5 ]; | |||||||||||||||||||||||||||
| 161 | 0 | $rk->{c} = [ 0, 1 ]; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 1 | ▶ | if($p->{rksteps} == 3) # Simpson | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | { | ||||||||||||||||||||||||||||
| 165 | 0 | $rk->{stages} = 3; | |||||||||||||||||||||||||||
| 166 | $rk->{a} = | ||||||||||||||||||||||||||||
| 167 | [ | ||||||||||||||||||||||||||||
| 168 | 0 | [ 0, 0, 0 ] | |||||||||||||||||||||||||||
| 169 | , [ 0.5, 0, 0 ] | ||||||||||||||||||||||||||||
| 170 | , [ -1, 2, 0 ] | ||||||||||||||||||||||||||||
| 171 | ]; | ||||||||||||||||||||||||||||
| 172 | 0 | $rk->{b} = [ 1/6, 4/6, 1/6 ]; | |||||||||||||||||||||||||||
| 173 | 0 | $rk->{c} = [ 0, 0.5, 1 ]; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 1 | ▶ | if($p->{rksteps} == 4) # RK44 method | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | { | ||||||||||||||||||||||||||||
| 177 | 1 | $rk->{stages} = 4; | |||||||||||||||||||||||||||
| 178 | $rk->{a} = | ||||||||||||||||||||||||||||
| 179 | [ | ||||||||||||||||||||||||||||
| 180 | 1 | [0, 0, 0, 0] | |||||||||||||||||||||||||||
| 181 | , [0.5, 0, 0, 0] | ||||||||||||||||||||||||||||
| 182 | , [0, 0.5, 0, 0] | ||||||||||||||||||||||||||||
| 183 | , [0, 0, 1, 0] | ||||||||||||||||||||||||||||
| 184 | ]; | ||||||||||||||||||||||||||||
| 185 | 1 | $rk->{b} = [1./6, 1./3, 1./3, 1./6]; | |||||||||||||||||||||||||||
| 186 | 1 | $rk->{c} = [0, 0.5, 0.5, 1]; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | return $self->error("Invalid RK setup (nothing for $p->{rksteps} stages).") | ||||||||||||||||||||||||||||
| 190 | 1 | ▶ | unless($rk->{stages}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 1 | ▶ | if($p->{debug}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||
| 193 | 0 | print STDERR "Using RK scheme with $rk->{stages} stages, tableau:\n"; | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | for(my $s=0; $s<$rk->{stages}; ++$s) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | { | ||||||||||||||||||||||||||||
| 196 | print STDERR sprintf( '%5.3f |'.(' %5.3f' x $rk->{stages})."\n" | ||||||||||||||||||||||||||||
| 197 | 0 | , $rk->{c}[$s], @{$rk->{a}[$s]} ); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 0 | print STDERR '------|'.('------' x $rk->{stages})."\n"; | |||||||||||||||||||||||||||
| 200 | print STDERR sprintf( ' |'.(' %5.3f' x $rk->{stages})."\n" | ||||||||||||||||||||||||||||
| 201 | 0 | , @{$rk->{b}} ); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # The ODE stored as sub reference. Work arrays are V and D in addition | ||||||||||||||||||||||||||||
| 205 | # to A and C. | ||||||||||||||||||||||||||||
| 206 | $self->{ode} = formula_function( $p->{ode} | ||||||||||||||||||||||||||||
| 207 | , {plainperl=>$p->{plainperl}, verbose=>$p->{debug}} | ||||||||||||||||||||||||||||
| 208 | 1 | , 'V', 'D' ); | |||||||||||||||||||||||||||
| 209 | return $self->error("Failed to compile your ODE.") | ||||||||||||||||||||||||||||
| 210 | 1 | ▶ | unless defined $self->{ode}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 1 | return 0; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | sub process_file | ||||||||||||||||||||||||||||
| 216 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
process_file: called CC=14 SCAR=31.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 217 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 218 | 1 | my $p = $self->{param}; | |||||||||||||||||||||||||||
| 219 | 1 | my $txd = $self->{txd}; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 1 | $self->{A} = []; | |||||||||||||||||||||||||||
| 222 | 1 | $self->{C} = []; | |||||||||||||||||||||||||||
| 223 | 1 | @{$self->{C}} = @{$p->{const}}; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | my $cols = $txd->columns(); | |||||||||||||||||||||||||||
| 226 | 1 | ▶ | unless($cols > 0 and @{$txd->{data}}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cols > 0 and @{$txd->{"data"};}
MC/DC0%0/2
$cols > 0 and @{$txd->{"data"};}
$cols > 0
@{$txd->{"data"};}
Truth table33%1/3
A and B
A= $cols > 0B= @{$txd->{"data"};}
| |||||||||||||||||||||||||||||
| 227 | { | ||||||||||||||||||||||||||||
| 228 | 0 | print STDERR "No data?\n"; | |||||||||||||||||||||||||||
| 229 | 0 | $txd->write_all($self->{out}); | |||||||||||||||||||||||||||
| 230 | 0 | return; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 1 | my $tc = $p->{timecol}-1; | |||||||||||||||||||||||||||
| 233 | 1 | ▶ | if($tc < 0 or $tc >= $cols) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tc < 0 or $tc >= $cols
MC/DC0%0/2
$tc < 0 or $tc >= $cols
$tc < 0
$tc >= $cols
Truth table33%1/3
A or B
A= $tc < 0B= $tc >= $cols
| |||||||||||||||||||||||||||||
| 234 | { | ||||||||||||||||||||||||||||
| 235 | 0 | $txd->{data} = []; | |||||||||||||||||||||||||||
| 236 | 0 | $txd->{titles} = []; | |||||||||||||||||||||||||||
| 237 | 0 | print STDERR "Bad time index.\n"; | |||||||||||||||||||||||||||
| 238 | 0 | $txd->write_all($self->{out}); | |||||||||||||||||||||||||||
| 239 | 0 | return; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # The initial values tell us how many variables to expect, | ||||||||||||||||||||||||||||
| 243 | # prepare titles for added columns and also set initial | ||||||||||||||||||||||||||||
| 244 | # values. | ||||||||||||||||||||||||||||
| 245 | 1 | my $vars = @{$p->{varinit}}; | |||||||||||||||||||||||||||
| 246 | 1 | my $vi=0; | |||||||||||||||||||||||||||
| 247 | 0 | ▶ | if(@{$txd->{raw_header}}){ $txd->write_header($self->{out}); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 1 | ▶ | if(@{$txd->{titles}}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | { | ||||||||||||||||||||||||||||
| 250 | 0 | ▶ | for(my $vi=0; $vi<$vars; ++$vi) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | { | ||||||||||||||||||||||||||||
| 252 | $txd->{titles}[$cols+$vi] = defined $p->{vartitle}[$vi] | ||||||||||||||||||||||||||||
| 253 | 0 | ▶ | ? $p->{vartitle}[$vi] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | : 'var'.($vi+1); | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | 0 | print {$self->{out}} ${$txd->title_line()}; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | # We'll print data on the fly, not bothering to clog memory with | ||||||||||||||||||||||||||||
| 259 | # storage of the integrated variables. Also might interfere with the | ||||||||||||||||||||||||||||
| 260 | # interpolation otherwise. | ||||||||||||||||||||||||||||
| 261 | 1 | my @val = @{$p->{varinit}}; | |||||||||||||||||||||||||||
| 262 | 1 | print {$self->{out}} ${$txd->data_line([ | |||||||||||||||||||||||||||
| 263 | 1 | @{$txd->{data}[0]}[0..($cols-1)] | |||||||||||||||||||||||||||
| 264 | , @val ])}; | ||||||||||||||||||||||||||||
| 265 | 1 | ▶ | for(my $mi=1; $mi< @{$txd->{data}}; ++$mi) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | { | ||||||||||||||||||||||||||||
| 267 | # Integrate from to to t1, using a fixed step that fits into the interval. | ||||||||||||||||||||||||||||
| 268 | 5 | my $t0 = $txd->{data}[$mi-1][$tc]; | |||||||||||||||||||||||||||
| 269 | 5 | my $t1 = $txd->{data}[$mi][$tc]; | |||||||||||||||||||||||||||
| 270 | my $div = $p->{timestep} | ||||||||||||||||||||||||||||
| 271 | ? int(abs(($t1-$t0)/$p->{timestep})+0.5) | ||||||||||||||||||||||||||||
| 272 | 5 | ▶ | : $p->{timediv}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 5 | ▶ | $div = 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | if $div < 1; | ||||||||||||||||||||||||||||
| 275 | 5 | my $step = ($t1-$t0)/$div; | |||||||||||||||||||||||||||
| 276 | print STDERR "int $t0 to $t1 div $div step $step\n" | ||||||||||||||||||||||||||||
| 277 | 5 | ▶ | if $p->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 5 | ▶ | for(my $si=0; $si<$div; ++$si) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | { | ||||||||||||||||||||||||||||
| 280 | 50 | $self->rk_step($t0+$si*$step, $step, \@val); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | 5 | print {$self->{out}} ${$txd->data_line([ | |||||||||||||||||||||||||||
| 283 | 5 | @{$txd->{data}[$mi]}[0..($cols-1)] | |||||||||||||||||||||||||||
| 284 | , @val ])}; | ||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # Compute one RK step with given time increment. | ||||||||||||||||||||||||||||
| 289 | sub rk_step | ||||||||||||||||||||||||||||
| 290 | 50 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
rk_step: called CC=16 SCAR=27.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 291 | 50 | my $self = shift; | |||||||||||||||||||||||||||
| 292 | 50 | my ($t, $dt, $val) = @_; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 50 | my $rk = $self->{rk}; | |||||||||||||||||||||||||||
| 295 | 50 | my $vars = @{$val}; | |||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 50 | my @work; # Storage for the derivatives in the stages. | |||||||||||||||||||||||||||
| 298 | my @tmp; # Storage for the current variables. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # Initialise them with the correct size. | ||||||||||||||||||||||||||||
| 301 | 50 | ▶ | for(my $s=0; $s<$rk->{stages}; ++$s) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | { | ||||||||||||||||||||||||||||
| 303 | 200 | ▶ | for(my $v=0; $v<$vars; ++$v) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | { | ||||||||||||||||||||||||||||
| 305 | 400 | $work[$s][$v] = 0; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | 50 | ▶ | for(my $v=0; $v<$vars; ++$v) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | { | ||||||||||||||||||||||||||||
| 310 | 100 | $tmp[$v] = 0; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # Collect the stage derivatives. | ||||||||||||||||||||||||||||
| 314 | 50 | $self->eval_ode($t, $val, $work[0]); | |||||||||||||||||||||||||||
| 315 | 0 | print STDERR "deriv 0: @{$work[0]}\n" | |||||||||||||||||||||||||||
| 316 | 50 | ▶ | if $self->{param}{debug} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 50 | ▶ | for(my $stage=1; $stage < $rk->{stages}; ++$stage) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | { | ||||||||||||||||||||||||||||
| 319 | 150 | for(@tmp){ $_ = 0 } | |||||||||||||||||||||||||||
| 320 | 150 | ▶ | for(my $substage = 0; $substage < $stage; ++$substage) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | { | ||||||||||||||||||||||||||||
| 322 | 300 | ▶ | if($rk->{a}[$stage][$substage] != 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | { # Does the condition really save work? | ||||||||||||||||||||||||||||
| 324 | 150 | ▶ | for(my $i=0; $i<$vars; ++$i) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | { | ||||||||||||||||||||||||||||
| 326 | 300 | $tmp[$i] += $rk->{a}[$stage][$substage]*$work[$substage][$i]; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | 150 | ▶ | for(my $i=0; $i<$vars; ++$i){ $tmp[$i] *= $dt; $tmp[$i] += $val->[$i]; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 150 | $self->eval_ode($t+$rk->{c}[$stage]*$dt, \@tmp, $work[$stage]); | |||||||||||||||||||||||||||
| 332 | 0 | print STDERR "deriv $stage: @{$work[$stage]}\n" | |||||||||||||||||||||||||||
| 333 | 150 | ▶ | if $self->{param}{debug} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | # Compute the definite derivative, apply and be done. | ||||||||||||||||||||||||||||
| 337 | 50 | for(@tmp){ $_ = 0 } | |||||||||||||||||||||||||||
| 338 | 50 | ▶ | for(my $stage=0; $stage < $rk->{stages}; ++$stage) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | { | ||||||||||||||||||||||||||||
| 340 | 200 | ▶ | for(my $i=0; $i<$vars; ++$i) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | { | ||||||||||||||||||||||||||||
| 342 | 400 | $tmp[$i] += $rk->{b}[$stage]*$work[$stage][$i]; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | 50 | ▶ | for(my $i=0; $i<$vars; ++$i) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | { | ||||||||||||||||||||||||||||
| 347 | 100 | $val->[$i] += $tmp[$i]*$dt; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # Evaluate the ODE once, interpolating in the input data for time-varying | ||||||||||||||||||||||||||||
| 352 | # parameters. | ||||||||||||||||||||||||||||
| 353 | sub eval_ode | ||||||||||||||||||||||||||||
| 354 | 200 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
eval_ode: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 355 | 200 | my $self = shift; | |||||||||||||||||||||||||||
| 356 | 200 | my ($t, $var, $deriv) = @_; | |||||||||||||||||||||||||||
| 357 | 200 | my @fd; # interpolated data set here | |||||||||||||||||||||||||||
| 358 | 200 | $fd[0] = $self->{txd}->set_of($t, $self->{param}{timecol}-1); | |||||||||||||||||||||||||||
| 359 | 0 | print STDERR "fd: @{$fd[0]}\n" | |||||||||||||||||||||||||||
| 360 | 200 | ▶ | if $self->{param}{debug} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 0 | print STDERR "V: @{$var}\n" | |||||||||||||||||||||||||||
| 362 | 200 | ▶ | if $self->{param}{debug} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | # @{$deriv} == 0 since rk_step intialised it | ||||||||||||||||||||||||||||
| 364 | 200 | $self->{ode}->(\@fd, $self->{A}, $self->{C}, $var, $deriv); | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 1 | 1; | |||||||||||||||||||||||||||