| 1 |
%% |
| 2 |
%% This is file `pst-ode.tex', |
| 3 |
%% |
| 4 |
%% Alexander Grahn, (C) 2012--today |
| 5 |
%% |
| 6 |
%% This program can be redistributed and/or modified under the terms |
| 7 |
%% of the LaTeX Project Public License |
| 8 |
%% |
| 9 |
%% http://www.latex-project.org/lppl.txt |
| 10 |
%% |
| 11 |
%% `pst-ode' defines \pstODEsolve for integrating systems of first order |
| 12 |
%% ODEs using the Runge-Kutta-Fehlberg (RKF45) method with automatic |
| 13 |
%% step size adjustment |
| 14 |
%% |
| 15 |
\def\fileversion{0.19} |
| 16 |
\def\filedate{2024/01/04} |
| 17 |
|
| 18 |
\csname PSTODELoaded\endcsname |
| 19 |
\let\PSTODELoaded\endinput |
| 20 |
% |
| 21 |
% Requires some packages |
| 22 |
\ifx\PSTricksLoaded\endinput\else \input pstricks \fi |
| 23 |
% |
| 24 |
\message{`pst-ode' v\fileversion, \filedate\space (ag)} |
| 25 |
% |
| 26 |
\edef\PstAtCode{\the\catcode`\@} \catcode`\@=11\relax |
| 27 |
\pst@addfams{pst-ode} |
| 28 |
%% prologue for postcript |
| 29 |
\pstheader{pst-ode.pro}% |
| 30 |
|
| 31 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 32 |
% |
| 33 |
% pstODEsolve |
| 34 |
% |
| 35 |
% LaTeX command for integrating systems of first order ODEs using the Runge- |
| 36 |
% Kutta-Fehlberg method with automatic step size adjustment; |
| 37 |
% values of the integration parameter `t' as well as the solution (= state) |
| 38 |
% vectors `x(t)' at output points are stored as a long list in a Postscript |
| 39 |
% object; its content can be plotted using the listplot* functions |
| 40 |
% of pst-plot and pst-3dplot packages. |
| 41 |
% |
| 42 |
% Optionally, the result can be written to a file (ps2pdf -dNOSAFER ...) |
| 43 |
% |
| 44 |
% Usage: |
| 45 |
% |
| 46 |
% \pstODEsolve[Options] |
| 47 |
% {result}{output vector format}{ta}{tb}{number of output points} |
| 48 |
% {initial cond.}{function} |
| 49 |
% |
| 50 |
% options: |
| 51 |
% * append result appended to <result> which must already exist (e. g. |
| 52 |
% from previous use of \pstODEsolve); usually the initial |
| 53 |
% condition vector argument is left empty in order to continue |
| 54 |
% integration from the last state |
| 55 |
% * saveData result is written to file <result>.dat |
| 56 |
% * algebraic (system of) ODE given in infix notation |
| 57 |
% * algebraicIC initial condition vector given in infix notation |
| 58 |
% * algebraicT integration interval limits ta, tb given in infix notation |
| 59 |
% * algebraicN accepting infix expression for number of output points |
| 60 |
% * algebraicOutputFormat output vector format given in infix notation |
| 61 |
% * algebraicAll output vector format, ta, tb, initial cond., function given |
| 62 |
% in infix notation |
| 63 |
% * silent suppress output of stepping information |
| 64 |
% * varsteptol relative tolerance for step size adjustment |
| 65 |
% * dt0 first tentative integration step size, overrides output step |
| 66 |
% size (#4-#3)/(#5-1) used as default value |
| 67 |
% * rk4 instead of RKF45, use 4th-order classical Runge-Kutta between |
| 68 |
% output points (without adaptive step size) |
| 69 |
% |
| 70 |
% arguments: |
| 71 |
% |
| 72 |
% #1: Postscript identifier taking the result as a long list of state vectors |
| 73 |
% calculated at the output points |
| 74 |
% #2: output vector format, e. g. `(t) 0 1'; specifies which data to be written |
| 75 |
% to #1; (t) (parentheses required) puts value of integration parameter `t' |
| 76 |
% into the output list; 0, 1, 2, etc. specify the elements of the state |
| 77 |
% vector to be put into the output list |
| 78 |
% #3: start value of integration parameter (ta) |
| 79 |
% #4: end value of integration parameter (tb) |
| 80 |
% #5: number of output points, including ta and tb; must be >= 2 |
| 81 |
% #6: initial condition vector; if empty, continue integration from the last |
| 82 |
% state vector of the previous \pstODEsolve usage or from state vector set |
| 83 |
% by \pstODErestoreState macro |
| 84 |
% #7: right hand side of ODE system; if given in Postscript notation (algebraic= |
| 85 |
% false), the function provided should pop (in reverse order!) the elements |
| 86 |
% of the current state vector from the operand stack and push the first |
| 87 |
% derivatives (right hand side of ODE system) back to it; inside the |
| 88 |
% function, the integration parameter can be accessed using `t' |
| 89 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 90 |
\define@boolkey[psset]{pst-ode}[PstODE@]{append}[true]{}% |
| 91 |
\define@boolkey[psset]{pst-ode}[PstODE@]{saveData}[true]{}% |
| 92 |
\define@boolkey[psset]{pst-ode}[PstODE@]{rk4}[true]{}% |
| 93 |
\define@boolkey[psset]{pst-ode}[PstODE@]{algebraicT}[true]{}% |
| 94 |
\define@boolkey[psset]{pst-ode}[PstODE@]{algebraicN}[true]{}% |
| 95 |
\define@boolkey[psset]{pst-ode}[PstODE@]{algebraicIC}[true]{}% |
| 96 |
\define@boolkey[psset]{pst-ode}[PstODE@]{algebraicOutputFormat}[true]{}% |
| 97 |
\define@boolkey[psset]{pst-ode}[PstODE@]{algebraicAll}[true]{}% |
| 98 |
\define@boolkey[psset]{pst-ode}[PstODE@]{silent}[true]{}% |
| 99 |
\define@key[psset]{pst-ode}{varsteptol}{\def\ode@varsteptol{#1}}% |
| 100 |
\define@key[psset]{pst-ode}{dt0}{\def\ode@dtInit{#1}}% |
| 101 |
\def\pstODEsolve{\def\pst@par{}\pst@object{pstODEsolve}}% |
| 102 |
\def\pstODEsolve@i#1#2#3#4#5#6#7{% |
| 103 |
\pst@killglue% |
| 104 |
\begingroup% |
| 105 |
\use@par% |
| 106 |
\def\filemode{w}% |
| 107 |
\ifPstODE@append\def\filemode{a}\fi% |
| 108 |
\edef\ode@ta{#3{}}% |
| 109 |
\edef\ode@tb{#4{}}% |
| 110 |
\edef\ode@N{#5{}}% |
| 111 |
\edef\ode@init{#6{}}% |
| 112 |
\edef\ode@init{\expandafter\ode@zapspace\ode@init\@nil}% |
| 113 |
\edef\ode@arg{#7{}}% |
| 114 |
\ifPstODE@algebraicAll% |
| 115 |
\PstODE@algebraicTtrue% |
| 116 |
\PstODE@algebraicNtrue% |
| 117 |
\PstODE@algebraicICtrue% |
| 118 |
\PstODE@algebraicOutputFormattrue% |
| 119 |
\Pst@algebraictrue% |
| 120 |
\fi% |
| 121 |
\ifPstODE@algebraicOutputFormat\edef\ode@fmt{#2{}}\fi% |
| 122 |
\pstVerb{ |
| 123 |
tx@odeDict begin |
| 124 |
\ifPstODE@silent |
| 125 |
/odeprint systemdict /pop get def |
| 126 |
\else |
| 127 |
/odeprint {0 cvs print flush} def |
| 128 |
/odeprint load 0 256 string put |
| 129 |
\fi |
| 130 |
/ode@tol \ode@varsteptol\space def % rel. tolerance for step size adjustment |
| 131 |
%process arguments |
| 132 |
/rk4 |
| 133 |
\expandafter\csname ifPstODE@rk4\endcsname true\else false\fi\space |
| 134 |
def |
| 135 |
\ifPstODE@saveData /statefile (#1.dat) (\filemode) file def \fi |
| 136 |
\ifPstODE@algebraicT |
| 137 |
tx@Dict begin (\expandafter\ode@zapspace\ode@ta\@nil) |
| 138 |
AlgParser cvx exec end ode@dict /tStart exch def end |
| 139 |
tx@Dict begin (\expandafter\ode@zapspace\ode@tb\@nil) |
| 140 |
AlgParser cvx exec end ode@dict /tEnd exch def end |
| 141 |
\else |
| 142 |
tx@Dict begin 1 dict begin #3 end end ode@dict /tStart exch def end |
| 143 |
tx@Dict begin 1 dict begin #4 end end ode@dict /tEnd exch def end |
| 144 |
\fi% |
| 145 |
\ifPstODE@algebraicN |
| 146 |
tx@Dict begin (\expandafter\ode@zapspace\ode@N\@nil) |
| 147 |
AlgParser cvx exec cvi end ode@dict /N exch def end |
| 148 |
\else |
| 149 |
tx@Dict begin 1 dict begin #5 cvi end end ode@dict /N exch def end |
| 150 |
\fi |
| 151 |
ode@dict /dt tEnd tStart sub N 1 sub div def end % output step size |
| 152 |
\ifx\@empty\ode@init\@empty\else |
| 153 |
\ifPstODE@algebraicIC |
| 154 |
true setglobal globaldict /ode@laststate [ |
| 155 |
tx@Dict begin (\ode@init) AlgParser cvx |
| 156 |
exec end |
| 157 |
] put false setglobal |
| 158 |
\else |
| 159 |
true setglobal |
| 160 |
globaldict /ode@laststate [tx@Dict begin 1 dict begin #6 end end] put |
| 161 |
false setglobal |
| 162 |
\fi |
| 163 |
\fi% |
| 164 |
ode@dict /xlength ode@laststate length def end % number of equations |
| 165 |
ode@dict /xlength1 xlength 1 add def end % number of equations plus 1 |
| 166 |
\ifPst@algebraic |
| 167 |
/ODESET [% system of ODEs |
| 168 |
10 array aload pop |
| 169 |
(\expandafter\ode@zapspace\ode@arg\@nil) AlgParser aload pop |
| 170 |
7 array aload pop |
| 171 |
] |
| 172 |
dup 0 {tx@Dict begin 2 begin /x exch def /y x def} putinterval |
| 173 |
% ensure local scope of user defined variables in #7, from BlueBook, p. 133 |
| 174 |
dup 2 1 dict put |
| 175 |
dup dup length 7 sub {end end ode@dict xlength end array astore} putinterval |
| 176 |
cvx bind def |
| 177 |
\else |
| 178 |
/ODESET { |
| 179 |
aload pop tx@Dict begin 4 begin #7 end end ode@dict xlength end array astore |
| 180 |
} bind def |
| 181 |
/ODESET load 4 1 dict put |
| 182 |
\fi |
| 183 |
\ifPstODE@algebraicOutputFormat |
| 184 |
/formatoutput [ |
| 185 |
13 array aload pop |
| 186 |
(\expandafter\ode@zapspace\ode@fmt\@nil) AlgParser aload pop |
| 187 |
/end cvx |
| 188 |
] |
| 189 |
dup 0 { |
| 190 |
/x ode@laststate def /y x def |
| 191 |
/t ode@dict tcur end def |
| 192 |
tx@Dict begin |
| 193 |
} putinterval |
| 194 |
cvx bind def |
| 195 |
\else /formatoutput {[#2] assembleresult} def \fi |
| 196 |
%perform ode integration |
| 197 |
rk4 { |
| 198 |
(\string\n pstODEsolve (RK4),) odeprint ( "o" output step:\string\n) odeprint |
| 199 |
}{ |
| 200 |
(\string\n pstODEsolve (RKF45),\string\n) odeprint |
| 201 |
(-/+ failed/successful step, "o" output step, "!" step size underflow (stop):\string\n) odeprint |
| 202 |
} ifelse |
| 203 |
ode@dict |
| 204 |
/tcur tStart def % current parameter t value |
| 205 |
/outStepCnt 1 def % output step counter |
| 206 |
/tout tStart dt add def % next output t |
| 207 |
%initial integration step size `ddt': either same as first output step |
| 208 |
% size `dt' or last from previous solution (empty initial condition) or |
| 209 |
% user provided (option `dt0') |
| 210 |
\ifx\@empty\ode@init\@empty |
| 211 |
\ode@dtInit\space 0 eq |
| 212 |
{/ddt ode@ddt def}{/ddt \ode@dtInit\space def} ifelse |
| 213 |
\else |
| 214 |
\ode@dtInit\space 0 eq |
| 215 |
{/ddt dt def}{/ddt \ode@dtInit\space def} ifelse |
| 216 |
\fi |
| 217 |
% for RK4, use output step size |
| 218 |
rk4 {/ddt dt def} if |
| 219 |
end |
| 220 |
\ifPstODE@append\else |
| 221 |
[ |
| 222 |
[formatoutput] |
| 223 |
\ifPstODE@saveData dup writeresult \fi |
| 224 |
aload pop |
| 225 |
true setglobal |
| 226 |
] |
| 227 |
globaldict exch /#1 exch cvx put |
| 228 |
false setglobal |
| 229 |
(o) odeprint |
| 230 |
\fi |
| 231 |
ode@dict N end 1 sub { |
| 232 |
ode@laststate ODEINT [ exch aload pop true setglobal ] |
| 233 |
globaldict exch /ode@laststate exch put false setglobal |
| 234 |
[ |
| 235 |
#1 [formatoutput] |
| 236 |
\ifPstODE@saveData dup writeresult \fi |
| 237 |
aload pop |
| 238 |
true setglobal |
| 239 |
] |
| 240 |
globaldict exch /#1 exch cvx put |
| 241 |
globaldict /ode@dt ode@dict dt end put |
| 242 |
globaldict /ode@ddt ode@dict ddt end put |
| 243 |
globaldict /ode@tcur ode@dict tcur end put |
| 244 |
globaldict /ode@tout ode@dict tout end put |
| 245 |
false setglobal |
| 246 |
} repeat (\string\n) odeprint |
| 247 |
\ifPstODE@saveData statefile closefile \fi |
| 248 |
end % tx@odeDict |
| 249 |
}% |
| 250 |
\endgroup% |
| 251 |
\ignorespaces% |
| 252 |
} |
| 253 |
|
| 254 |
\def\ode@zapspace#1#2\@nil{% helper for stripping spaces from argument |
| 255 |
\ifx#1 \relax\else#1\fi% |
| 256 |
\ifx\@empty#2\@empty\else\ode@zapspace#2\@nil\fi% |
| 257 |
} |
| 258 |
|
| 259 |
%macro for saving last state |
| 260 |
\def\pstODEsaveState#1{% #1: identifier |
| 261 |
\pstVerb{ |
| 262 |
true setglobal |
| 263 |
globaldict /#1@ode@laststate [ode@laststate cvx exec] cvx put |
| 264 |
globaldict /#1@ode@dt ode@dt put |
| 265 |
globaldict /#1@ode@ddt ode@ddt put |
| 266 |
globaldict /#1@ode@tcur ode@tcur put |
| 267 |
globaldict /#1@ode@tout ode@tout put |
| 268 |
false setglobal |
| 269 |
}% |
| 270 |
} |
| 271 |
|
| 272 |
%macro for restoring state |
| 273 |
\def\pstODErestoreState#1{% #1: identifier |
| 274 |
\pstVerb{ |
| 275 |
true setglobal |
| 276 |
globaldict /ode@laststate [#1@ode@laststate] put |
| 277 |
globaldict /ode@dt #1@ode@dt put |
| 278 |
globaldict /ode@ddt #1@ode@ddt put |
| 279 |
globaldict /ode@tcur #1@ode@tcur put |
| 280 |
globaldict /ode@tout #1@ode@tout put |
| 281 |
false setglobal |
| 282 |
}% |
| 283 |
} |
| 284 |
|
| 285 |
\psset[pst-ode]{append=false,saveData=false,algebraicIC=false,algebraicT=false, |
| 286 |
algebraicN=false,silent=false,varsteptol=1e-6,algebraicOutputFormat=false, |
| 287 |
algebraicAll=false,dt0=0,rk4=false |
| 288 |
} |
| 289 |
\psset{algebraic=false} |
| 290 |
\catcode`\@=\PstAtCode\relax |
| 291 |
|
| 292 |
%% END: pst-ode.tex |
| 293 |
\endinput |