Debian Codegraph @

texmf-dist/dvips/pst-ode/pst-ode.pro

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %
3 % PostScript prologue for pst-ode.tex.
4 % Version 0.19, 2024/01/04
5 %
6 % Alexander Grahn (C) 2012--today
7 %
8 % This program can be redistributed and/or modified under the terms
9 % of the LaTeX Project Public License
10 %
11 % http://www.latex-project.org/lppl.txt
12 %
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 /tx@odeDict 1 dict def
15 tx@odeDict begin
16 /ode@@dict 1 dict def
17 /ode@dict {ode@@dict begin} bind def
18 ode@dict
19 %some constants for step size calculation
20 /sfty 0.9 def /pgrow -0.2 def /pshrink -0.25 def
21 %helper functions
22 /addvect { % [1 2 3] [4 5 6] addvect => [5 7 9]
23 ode@dict
24 aload pop xlength1 -1 roll {xlength1 -1 roll add} forall
25 xlength array astore
26 end
27 } bind def
28 /subvect { % [1 2 3] [4 5 6] subvect => [-3 -3 -3]
29 ode@dict
30 aload pop xlength1 -1 roll {xlength1 -1 roll sub} forall
31 xlength array astore
32 end
33 } bind def
34 /mulvect { % [1 2 3] 4 mulvect => [4 8 12]
35 ode@dict /mul cvx 2 array astore cvx forall xlength array astore end
36 } bind def
37 /edivvect { % [1 2 3] [4 5 6] edivvect => [0.25 0.4 0.5]
38 ode@dict
39 aload pop xlength1 -1 roll {xlength1 -1 roll div} forall
40 xlength array astore
41 end
42 } bind def
43 /eabsvect { % [-1 2 -3] eabsvect => [1 2 3]
44 ode@dict {abs} forall xlength array astore end
45 } bind def
46 %/revstack { % (a) (b) (c) (d) 3 revstack => (a) (d) (c) (b)
47 % -1 2 {dup 1 sub neg roll} for
48 %} bind def
49 /min { 2 copy gt { exch } if pop } bind def
50 /max { 2 copy lt { exch } if pop } bind def
51 %coefficient table (Butcher table) of RKF45
52 /c2 1 4 div def /c3 3 8 div def /c4 12 13 div def /c6 1 2 div def
53 /a21 1 4 div def /a31 3 32 div def /a32 9 32 div def
54 /a41 1932 2197 div def /a42 7200 2197 div neg def /a43 7296 2197 div def
55 /a51 439 216 div def /a52 8 neg def /a53 3680 513 div def
56 /a54 845 4104 div neg def /a61 8 27 div neg def /a62 2 def
57 /a63 3544 2565 div neg def /a64 1859 4104 div def /a65 11 40 div neg def
58 /b1 16 135 div def /b3 6656 12825 div def
59 /b4 28561 56430 div def /b5 9 50 div neg def
60 /b6 2 55 div def
61 /b1* 25 216 div def /b3* 1408 2565 div def
62 /b4* 2197 4104 div def /b5* 1 5 div neg def
63 end
64 %Runge-Kutta-Fehlberg (RKF45) method
65 %performs one integration step over tentative step size ddt
66 %[state vector x(t)] RKF45 => [x(t)] [x(t+ddt) by RKF4] errmax
67 /RKF45 {
68 dup
69 /t ode@dict tcur end def
70 ODESET
71 ode@dict
72 ddt mulvect /k1 exch def
73 dup k1 a21 mulvect addvect
74 tcur ddt c2 mul add
75 end
76 /t exch def
77 ODESET
78 ode@dict
79 ddt mulvect /k2 exch def
80 dup k1 a31 mulvect addvect k2 a32 mulvect addvect
81 tcur ddt c3 mul add
82 end
83 /t exch def
84 ODESET
85 ode@dict
86 ddt mulvect /k3 exch def
87 dup k1 a41 mulvect addvect k2 a42 mulvect addvect k3 a43 mulvect addvect
88 tcur ddt c4 mul add
89 end
90 /t exch def
91 ODESET
92 ode@dict
93 ddt mulvect /k4 exch def
94 dup k1 a51 mulvect addvect k2 a52 mulvect addvect k3 a53 mulvect addvect
95 k4 a54 mulvect addvect
96 tcur ddt add
97 end
98 /t exch def
99 ODESET
100 ode@dict
101 ddt mulvect /k5 exch def
102 dup k1 a61 mulvect addvect k2 a62 mulvect addvect k3 a63 mulvect addvect
103 k4 a64 mulvect addvect k5 a65 mulvect addvect
104 tcur ddt c6 mul add
105 end
106 /t exch def
107 ODESET
108 ode@dict
109 ddt mulvect /k6 exch def % => [x(t)]
110 %fourth order solution (increment dx)
111 dup dup k1 b1* mulvect k3 b3* mulvect addvect k4 b4* mulvect addvect
112 k5 b5* mulvect addvect dup
113 % => [x(t)] [x(t)] [x(t)] [dx by RKF4] [dx by RKF4]
114 %fifth order solution (abs. error)
115 k1 b1 mulvect k3 b3 mulvect addvect k4 b4 mulvect addvect
116 k5 b5 mulvect addvect k6 b6 mulvect addvect subvect
117 % => [x(t)] [x(t)] [x(t)] [dx by RKF4] [err]
118 5 1 roll addvect 4 -2 roll % => [x(t)] [x(t+ddt) by RKF4] [err] [x(t)]
119 %scaling vector for step size adjustment (Numerical Recipies)
120 eabsvect k1 eabsvect addvect {1e-30 add} forall xlength array astore
121 % => [x(t)] [x(t+ddt) by RKF4] [err] [xscale]
122 edivvect eabsvect 1e-30 exch {max} forall %maximum rel. error
123 % => [x(t)] [x(t+ddt) by RKF4] errmax
124 end
125 } bind def
126 %classical Runge-Kutta (RK4) method
127 %one integration step over step size ddt; no error estimate
128 %[state vector x(t)] RK4 => [x(t+ddt) by RK4]
129 /RK4 {
130 dup
131 ode@dict tcur end /t exch def
132 ODESET
133 ode@dict
134 ddt mulvect /k1 exch def
135 dup k1 0.5 mulvect addvect
136 tcur ddt 2 div add
137 end
138 /t exch def
139 ODESET
140 ode@dict
141 ddt mulvect /k2 exch def
142 dup k2 0.5 mulvect addvect
143 end
144 ODESET
145 ode@dict
146 ddt mulvect /k3 exch def
147 dup k3 addvect
148 tcur ddt add
149 end
150 /t exch def
151 ODESET
152 ode@dict
153 ddt mulvect /k4 exch def % => [x(t)]
154 k1 k2 k3 addvect 2 mulvect addvect k4 addvect 1 6 div mulvect addvect
155 % => [x(t+ddt) by RK4]
156 end
157 } bind def
158 /ODEINT { % performs integration over output step [t,t+dt]
159 % [ state vector x(t) ] ODEINT => [ state vector x(t+dt) ]
160 rk4 {
161 RK4 (o) odeprint
162 ode@dict
163 /tcur tout def
164 /outStepCnt outStepCnt 1 add def
165 /tout tStart dt outStepCnt mul add def
166 end
167 }{
168 %decrease overshooting step size
169 ode@dict
170 tout tcur sub dup abs ddt abs lt {/ddt exch def}{pop} ifelse
171 end
172 RKF45
173 ode@tol div dup 1 gt {
174 %failed step -> reduce step size
175 ode@dict
176 exch pop pshrink exp 0.1 max sfty mul ddt mul /ddt exch def
177 tcur ddt add tcur eq {
178 % error: step size underflow in ODEINT
179 (! t=) odeprint tcur odeprint
180 % print & remove previous state vector
181 (, x=[) odeprint /ode@spc () def
182 {ode@spc odeprint /ode@spc ( ) def odeprint} forall (]) odeprint
183 true
184 }{
185 (-) odeprint
186 false
187 } ifelse
188 end
189 % on step size underflow, leave loop over output steps (pst-ode.tex)
190 {exit} if
191 ODEINT %repeat step with new ddt
192 }{
193 %success
194 3 -1 roll pop % remove previous state vector
195 ode@dict
196 /tcur tcur ddt add def
197 dup 0 ne {pgrow exp 5 min sfty mul ddt mul /ddt exch def}{pop} ifelse
198 %output step completed?
199 tcur tout sub
200 end
201 0 eq {
202 (o) odeprint
203 ode@dict
204 /tcur tout def
205 /outStepCnt outStepCnt 1 add def
206 /tout tStart dt outStepCnt mul add def
207 end
208 }{
209 (+) odeprint ODEINT %continue integration
210 } ifelse
211 } ifelse
212 } ifelse
213 } bind def
214 /writeresult { %write output vector to file
215 /loopproc load forall
216 statefile (\n) writestring
217 } bind def
218 /loopproc {
219 0 cvs statefile exch writestring
220 statefile ( ) writestring
221 } bind def
222 /loopproc load 0 256 string put
223 /assembleresult { %assembles state vector for building table of results
224 {
225 dup (t) eq {
226 pop ode@dict tcur end
227 }{
228 ode@laststate exch get
229 } ifelse
230 } forall
231 } bind def
232 end