| 1 |
% Copyright 2019 by Christophe Jorssen |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Public License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
\def\pgfmathode@stopadd{\pgfmathode@stopadd} |
| 10 |
|
| 11 |
\def\pgfmathode@addindextofunc[#1]#2{% |
| 12 |
\begingroup |
| 13 |
\gdef\pgfmathode@func{}% |
| 14 |
\pgfmathode@addindextofunc@i[#1]#2\pgfmathode@stopadd} |
| 15 |
|
| 16 |
\def\pgfmathode@addindextofunc@i[#1]#2#3{% |
| 17 |
\ifx#1#2 |
| 18 |
\expandafter\gdef\expandafter\pgfmathode@func\expandafter{% |
| 19 |
\pgfmathode@func #2[\pgfmathode@timeindex]}% |
| 20 |
\else |
| 21 |
\expandafter\gdef\expandafter\pgfmathode@func\expandafter{% |
| 22 |
\pgfmathode@func #2}% |
| 23 |
\fi |
| 24 |
\ifx#3\pgfmathode@stopadd |
| 25 |
\expandafter\gdef\expandafter\pgfmathode@func\expandafter{% |
| 26 |
\expandafter{\pgfmathode@func}}% |
| 27 |
\let\next\endgroup |
| 28 |
\else |
| 29 |
\def\next{\pgfmathode@addindextofunc@i[#1]#3}% |
| 30 |
\fi |
| 31 |
\next} |
| 32 |
|
| 33 |
%********************************************************************** |
| 34 |
% Know limitation: fk cannot be a time function. Should be possible to |
| 35 |
% use \Sol[0] as time in the set of equations. |
| 36 |
%********************************************************************** |
| 37 |
|
| 38 |
% Numerically solve a differential system with 1st order Runge-Kutta |
| 39 |
% (Euler method) |
| 40 |
% dq1/dt = f1(t,q1,q2,...,qN) |
| 41 |
% dq2/dt = f2(t,q1,q2,...,qN) |
| 42 |
% ... |
| 43 |
% dqN/dt = fN(t,q1,q2,...,qN) |
| 44 |
% |
| 45 |
% #1: A pgf array where the solution will be stored. |
| 46 |
% At the end: |
| 47 |
% #1 -> {{t(0),q1(t(0)),q2(t(0)),...,qN(t(0))}, |
| 48 |
% {t(1),q1(t(1)),q2(t(1)),...,qN(t(1))}, |
| 49 |
% ... |
| 50 |
% {t(Nstep),q1(t(Nstep)),q2(t(Nstep)),...,qN(t(Nstep))}} |
| 51 |
% #2: A pgf array with f1,f2,...,fN |
| 52 |
% {f1} |
| 53 |
% #3: A pgf array with initial conditions. |
| 54 |
% {t(0),q1(t(0)),q2(t(0)),...,qN(t(0))} |
| 55 |
% #4: t(Nstep) |
| 56 |
% #5: Nstep (number of steps) |
| 57 |
% |
| 58 |
% * First order example: dq/dt = -q + 5 with IC=(t=0,q(0)=0) |
| 59 |
% \pgfmathodeRKI{\Sol}{{-\Sol[1]+5}}{{0,0}}{3.5}{100} |
| 60 |
% * Second order example: {dq1/dt=q2, dq2/dt=-q2/2-sin(q1)} (damped |
| 61 |
% pendulum) |
| 62 |
% \pgfmathodeRKI{\Sol}{{\Sol[2],-\Sol[2]/2-sin(deg(\Sol[1]))}}% |
| 63 |
% {{0,-1,5}}{15}{50} |
| 64 |
|
| 65 |
\def\pgfmathode@generic@init@RK{% |
| 66 |
\begingroup |
| 67 |
% Sol array (#1) first element is given by the set of initial |
| 68 |
% conditions (#3). We need to enclose it in brace so that is a now |
| 69 |
% a "matrix" array. |
| 70 |
\gdef#1{{#3}}% |
| 71 |
% The set of rhs {f1(t,q1,q2,...,qN),...,fN(t,q1,q2,...,qN)} is |
| 72 |
% given in terms of #1[0],#1[1],...,#1[N]. We need to transform to |
| 73 |
% #1[\pgfmathode@timeindex][0],...,#1[\pgfmathode@timeindex][N], that is |
| 74 |
% add [\pgfmathode@timeindex] after every occurrence of #1 in #2. |
| 75 |
% No need to brace #2 since it is already surrounded by braces. The |
| 76 |
% result is stored in \pgfmathode@func |
| 77 |
\pgfmathode@addindextofunc[#1]#2% |
| 78 |
% Set time step. Actually the number of time steps is #5-1, so we |
| 79 |
% divide by (#5-1)+1 = #5. |
| 80 |
\pgfmathsetmacro\pgfmathode@Sol@t{#1[0][0]}% |
| 81 |
\pgfmathsetmacro\pgfmathode@Deltat{(#4-\pgfmathode@Sol@t)/#5}% |
| 82 |
% Compute the order of the system: it is equal to the number of IC |
| 83 |
% minus one (time). |
| 84 |
\pgfmathsetmacro\pgfmathode@dimfunc{dim(#3)-1}% |
| 85 |
\pgfmathsetmacro\pgfmathode@numstep{#5-1}} |
| 86 |
|
| 87 |
\def\pgfmathodeRKI#1#2#3#4#5{% |
| 88 |
\pgfmathode@generic@init@RK |
| 89 |
% Start the time loop for Runge-Kutta 1st order |
| 90 |
\foreach \pgfmathode@timeindex in {0,...,\pgfmathode@numstep} {% |
| 91 |
\message{Step \pgfmathode@timeindex...}% |
| 92 |
% Start the function loop |
| 93 |
\foreach \pgfmathode@funcindex in {1,...,\pgfmathode@dimfunc} {% |
| 94 |
% The RKI algorithm |
| 95 |
% qk[t(i)] = Deltat*fk(t(i-1),q1(t(i-1)),...qN(t(i-1))) + |
| 96 |
% qk[t(i-1)] |
| 97 |
% Note: this part can be really slow due to array management in |
| 98 |
% pgfmath. |
| 99 |
% TODO: find a better way (at least inside this loop) |
| 100 |
\pgfmathparse{% |
| 101 |
\pgfmathode@Deltat*(\pgfmathode@func[\pgfmathode@funcindex-1])+ |
| 102 |
#1[\pgfmathode@timeindex][\pgfmathode@funcindex]}% |
| 103 |
% Store the result in a helper macro |
| 104 |
\expandafter\xdef\csname pgfmathode@Sol@\pgfmathode@funcindex |
| 105 |
\endcsname{\pgfmathresult}% |
| 106 |
} |
| 107 |
% Store step i |
| 108 |
\pgfmathparse{\pgfmathode@Sol@t+\pgfmathode@Deltat}% |
| 109 |
\xdef\pgfmathode@Sol@t{\pgfmathresult}% |
| 110 |
\xdef\pgfmathode@temp{}% |
| 111 |
\foreach \pgfmathode@funcindex in {1,...,\pgfmathode@dimfunc} {% |
| 112 |
\ifx\pgfmathode@temp\pgfutil@empty |
| 113 |
\xdef\pgfmathode@temp{% |
| 114 |
\csname pgfmathode@Sol@\pgfmathode@funcindex\endcsname}% |
| 115 |
\else |
| 116 |
\xdef\pgfmathode@temp{% |
| 117 |
\pgfmathode@temp, |
| 118 |
\csname pgfmathode@Sol@\pgfmathode@funcindex\endcsname}% |
| 119 |
\fi} |
| 120 |
\xdef#1{% |
| 121 |
{\expandafter\pgfutil@firstofone#1,% |
| 122 |
{\pgfmathode@Sol@t,\pgfmathode@temp}}}% |
| 123 |
} |
| 124 |
\endgroup} |
| 125 |
|
| 126 |
% RKIV: far from working yet! |
| 127 |
\def\pgfmathodeRKIV#1#2#3#4#5{% |
| 128 |
\pgfmathode@generic@init@RK |
| 129 |
% Start the time loop for Runge-Kutta 4th order |
| 130 |
\foreach \pgfmathode@timeindex in {0,...,\pgfmathode@numstep} {% |
| 131 |
\message{Step \pgfmathode@timeindex...}% |
| 132 |
% Start the function loop |
| 133 |
\foreach \pgfmathode@funcindex in {1,...,\pgfmathode@dimfunc} {% |
| 134 |
% The RKIV algorithm |
| 135 |
% qk[t(i)] = qk[t(i-1)] + |
| 136 |
% (1/6) * (j1k + 2j2k + 2j3k + j4k) |
| 137 |
% where j1k = fk(t(i-1),q1(t(i-1)),...qN(t(i-1))) * Deltat |
| 138 |
% j2k = fk(t(i-1)+.5*Deltat, |
| 139 |
% q1(t(i-1))+.5*j11,...qN(t(i-1))+.5*j1N) * Deltat |
| 140 |
% j3k = fk(t(i-1)+.5*Deltat, |
| 141 |
% q1(t(i-1))+.5*j21,...qN(t(i-1))+.5*j2N) * Deltat |
| 142 |
% j4k = fk(t(i-1)+Deltat, |
| 143 |
% q1(t(i-1))+j31,...qN(t(i-1))+j3N) * Deltat |
| 144 |
% |
| 145 |
% Compute j1k |
| 146 |
\pgfmathparse{% |
| 147 |
\pgfmathode@Deltat*(\pgfmathode@func[\pgfmathode@funcindex-1])+ |
| 148 |
#1[\pgfmathode@timeindex][\pgfmathode@funcindex]}% |
| 149 |
\expandafter\xdef\csname pgfmathode@RKIV@1@\pgfmathode@funcindex |
| 150 |
\endcsname{\pgfmathresult}% |
| 151 |
% Compute j2k |
| 152 |
\pgfmathparse{% |
| 153 |
\pgfmathode@Deltat*(\pgfmathode@func[\pgfmathode@funcindex-1])+ |
| 154 |
#1[\pgfmathode@timeindex][\pgfmathode@funcindex]}% |
| 155 |
\expandafter\xdef\csname pgfmathode@RKIV@1@\pgfmathode@funcindex |
| 156 |
\endcsname{\pgfmathresult}% |
| 157 |
} |
| 158 |
% Store step i |
| 159 |
\pgfmathparse{\pgfmathode@Sol@t+\pgfmathode@Deltat}% |
| 160 |
\xdef\pgfmathode@Sol@t{\pgfmathresult}% |
| 161 |
\xdef\pgfmathode@temp{}% |
| 162 |
\foreach \pgfmathode@funcindex in {1,...,\pgfmathode@dimfunc} {% |
| 163 |
\ifx\pgfmathode@temp\pgfutil@empty |
| 164 |
\xdef\pgfmathode@temp{% |
| 165 |
\csname pgfmathode@Sol@\pgfmathode@funcindex\endcsname}% |
| 166 |
\else |
| 167 |
\xdef\pgfmathode@temp{% |
| 168 |
\pgfmathode@temp, |
| 169 |
\csname pgfmathode@Sol@\pgfmathode@funcindex\endcsname}% |
| 170 |
\fi} |
| 171 |
\xdef#1{% |
| 172 |
{\expandafter\pgfutil@firstofone#1,% |
| 173 |
{\pgfmathode@Sol@t,\pgfmathode@temp}}}% |
| 174 |
} |
| 175 |
\endgroup} |
| 176 |
|
| 177 |
|
| 178 |
\endinput |