On computer algebra aided numerical solution of ODE by finite … PCA 2019.pdf · 2019. 4. 19. ·...
Transcript of On computer algebra aided numerical solution of ODE by finite … PCA 2019.pdf · 2019. 4. 19. ·...
On computer algebra aided numerical solution of ODEby finite difference method
Yuri A. Blinkov1 Vladimir P. Gerdt2
1Department of Mathematics and MechanicsSaratov University
410012, Saratov, Russia
2Laboratory of Information TechnologiesJoint Institute for Nuclear Research
141980, Dubna, Russia
Talk at PCA 2019, April, St. Petersburg, Russia
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 1 / 22
Plan
1 Research problem
2 Numerical results
3 References
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 2 / 22
Research problem
Except very special cases, nonlinear ordinary differential equations (ODE) admitnumerical integration only. Historically first and one of the most-used numericalmethods is finite difference method (FDM) [1] based on a finite differenceapproximation (FDA). As this takes place, the quality of numerical solution toPDE is determined by the quality of its FDA and by the method of numericalsolution used to solve the difference equations comprising FDA.
One of the most challenging problems in to construct FDA which mimics basicalgebraic properties of the ODE. Such mimetic FDA are more likely to producehighly accurate and stable numerical results (cf. [2]). In particular, a mimetic FDAis to be totally conservative (see [3], Def.1) what means the inheritance ofalgebraic integrals of the ODE at the discrete level.
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 3 / 22
Research problem
Example. We consider the following autonomous ODE system [3]⎧⎪⎨⎪⎩p =qr ,q = − pr , k = const ,
r = − k2pq
(1)
which has two quadratic integrals
p2 + q2 = const and k2p2 + r2 = const. (2)
We use the denotations x := {p, q, r} and F(x) := {qr ,−pr ,−k2pq} and considerthe implicit midpoint finite difference discretization of system (1)
dxdt
= F(x) =⇒ xtn+1
tn
=
∫ tn+1
tnF(x) dt =⇒ xn+1 − xn
∆t=
F(xn+1) + F(xn)
2(3)
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 4 / 22
Research problem
The construction of a simple iteration is based on the following quadratic formulasfor monomials occurring in F(x)
u2 = u2 − u′2 + u′2 = (u − u′)(u + u′) +
+ u′2 ≈ (u − u′)2u′ + u′2 = 2uu′ − u′2
uv = (u + v)2 − (u − v)2)/4 ≈ ((2(u + v)(u′ + v ′) − (u′ + v ′)2) −− (2(u − v)(u′ − v ′) − (u′ − v ′)2))/4 = uv ′ + u′v − u′v ′
u3 = u3 − u′3 + u′3 = (u − u′)(u2 + uu′ + u′2) +
+ u′3 ≈ (u − u′)3u′2 + u′2 = 3uu′2 − 2u′3
(4)
and the qubic one
u2v = uuv ≈ u′u′v + uu′v ′ + u′uv ′ −− (3− 1)u′2v ′ = u′2v + 2uu′v ′ − 2u′2v ′ (5)
The rule (5) can be easily implemented in a user’s programming language of anymodern computer algebra system and allows obtain the code for numericalcomputations.
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 5 / 22
Research problem
Thus, for the system (1) and its implicit scheme (3) the simple ireration methodyields⎧⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎩
pn+1 − pn
∆t−qn+1r ′n+1 + q′
n+1rn+1 − q′n+1r
′n+1 + qnrn
2= 0,
qn+1 − qn
∆t−−pn+1r ′n+1 − p′
n+1rn+1 + p′n+1r
′n+1 − pnrn
2= 0,
rn+1 − rn∆t
−−k2pn+1q′n+1 − k2p′
n+1qn+1 + k2p′n+1q
′n+1 − k2pnqn
2= 0
(6)
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 6 / 22
Research problem
In the matrix form obtained by using the computer algebrasystem SymPy (https://www.sympy.org) is given by⎛⎜⎜⎝
1/∆t −r ′n+1/2 −q′n+1/2
r ′n+1/2 1/∆t p′n+1/2
k2q′n+1/2 k2p′
n+1/2 1/∆t
⎞⎟⎟⎠⎛⎝pn+1
qn+1rn+1
⎞⎠ =
=
⎛⎜⎝ pn/∆t − q′n+1r
′n+1/2 + qnrn/2
qn/∆t + p′n+1r
′n+1/2− pnrn/2
rn/∆t + k2p′n+1q
′n+1/2− k2pnqn/2
⎞⎟⎠(7)
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 7 / 22
Numerical results
In [3]: p, q, r, k = sympy.symbols(’p, q, r, k’, real=True)
In [4]: f = [q*r, -p*r, -k**2*p*q]f
Out[4]: [qr , −pr , −k2pq
]In [5]: J = sympy.Matrix(f).jacobian(sympy.Matrix([p, q, r]))
J
Out[5]: ⎡⎣ 0 r q−r 0 −p
−k2q −k2p 0
⎤⎦
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 8 / 22
Numerical results
In [6]: num_f = sympy.lambdify((p, q, r, k), f, "numpy")num_J = sympy.lambdify((p, q, r, k), J, "numpy")
def func(t, y, k):return num_f(y[0], y[1], y[2], k)
def jac(t, y, k):return num_J(y[0], y[1], y[2], k)
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 9 / 22
Numerical results
lsoda:Real-valued Variable-coefficient Ordinary Differential Equation solver, withfixed-leading-coefficient implementation. It provides automatic method switchingbetween implicit Adams method (for non-stiff problems) and a method based onbackward differentiation formulas (BDF) (for stiff problems).Source: http://www.netlib.org/odepack
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 10 / 22
Numerical results
In [7]: r = ode(func, jac).set_integrator(’lsoda’, method=’bdf’,with_jacobian=True)
r.set_initial_value(y0).set_f_params(num_k,).set_jac_params(num_k,)
sol = ndarray((len(t), 3), dtype=float64)sol[0, :] = y0clock = time.clock()for i in range(1, len(t)):
assert r.successful()r.integrate(t[i])sol[i, :] = r.y
print("Time: %.3f" % (time.clock() - clock))draw(sol)draw(sol, "./fig/fig01.pdf")
Time: 0.283
0 200 400 600 800 1000
t
0.00000
0.00002
0.00004
0.00006
0.00008
p2
+q2
+1 ‖p2 + q2 − 1‖∞ = 8.1e− 05
numericexact
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 11 / 22
Numerical results
In [7]: r = ode(func, jac).set_integrator(’lsoda’, method=’bdf’,with_jacobian=True)
r.set_initial_value(y0).set_f_params(num_k,).set_jac_params(num_k,)
....Time: 0.283
0 200 400 600 800 1000
t
0.00000
0.00002
0.00004
0.00006
0.00008
p2
+q2
+1 ‖p2 + q2 − 1‖∞ = 8.1e− 05
numericexact
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 12 / 22
Numerical results
In [8]: r = ode(func).set_integrator(’lsoda’)r.set_initial_value(y0).set_f_params(num_k,)....
Time: 0.366
0 200 400 600 800 1000
t
0.000000
0.000025
0.000050
0.000075
0.000100
0.000125
0.000150
0.000175
p2
+q2
+1 ‖p2 + q2 − 1‖∞ = 1.9e− 04
numericexact
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 13 / 22
Numerical results
vode:Real-valued Variable-coefficient Ordinary Differential Equation solver, withfixed-leading-coefficient implementation. It provides implicit Adams method (fornon-stiff problems) and a method based on backward differentiation formulas(BDF) (for stiff problems).Authors:
Peter N. Brown and Alan C. HindmarshCenter for Applied Scientific ComputingLawrence Livermore National LaboratoryLivermore, CA 94551
andGeorge D. Byrne (Prof. Emeritus)Illinois Institute of TechnologyChicago, IL 60616
Source: http://www.netlib.org/ode/vode.f
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 14 / 22
Numerical results
In [9]: r = ode(func, jac).set_integrator(’vode’, method=’bdf’,with_jacobian=True)
r.set_initial_value(y0).set_f_params(num_k,).set_jac_params(num_k,)
....Time: 0.293
0 200 400 600 800 1000
t
1.0000
1.0002
1.0004
1.0006
1.0008
1.0010
1.0012
1.0014
p2
+q2
‖p2 + q2 − 1‖∞ = 1.4e− 03
numericexact
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 15 / 22
Numerical results
In [10]: r = ode(func).set_integrator(’vode’, method=’bdf’)r.set_initial_value(y0).set_f_params(num_k,)....
Time: 0.338
0 200 400 600 800 1000
t
0.0000
0.0001
0.0002
0.0003
0.0004
0.0005
0.0006
0.0007
p2
+q2
+1 ‖p2 + q2 − 1‖∞ = 7.3e− 04
numericexact
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 16 / 22
Numerical results
dopri5, dop853:
This is an explicit runge-kutta method of order (4)5 due to Dormand & Prince(with stepsize control and dense output).
Authors:E. Hairer and G. Wanner Universite de Geneve, Dept. de Mathematiques CH-1211Geneve 24, Switzerland e-mail: [email protected],[email protected]
This code is described in:E. Hairer, S.P. Norsett and G. Wanner, Solving Ordinary Differential Equations i.Nonstiff Problems. 2nd edition. Springer Series in Computational Mathematics,Springer-Verlag (1993)
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 17 / 22
Numerical results
In [12]: r = ode(func).set_integrator(’dopri5’)r.set_initial_value(y0).set_f_params(num_k,)....
Time: 0.740
0 200 400 600 800 1000
t
0.0000065
0.0000070
0.0000075
0.0000080
0.0000085
0.0000090
0.0000095
0.0000100
p2
+q2
+9.9999×10−1 ‖p2 + q2 − 1‖∞ = 3.6e− 06
numericexact
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 18 / 22
Numerical results
In [13]: r = ode(func).set_integrator(’dop853’)r.set_initial_value(y0).set_f_params(num_k,)....
Time: 1.204
0 200 400 600 800 1000
t
−0.8
−0.6
−0.4
−0.2
0.0
p2
+q2
×10−12 + 1 ‖p2 + q2 − 1‖∞ = 9.2e− 13
numericexact
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 19 / 22
Numerical results
In [14]: sol = ndarray((len(t), 3), dtype=float64)sol[0, :] = y0tau, num_k2 = t[1] - t[0], num_k**2A = empty((3, 3), dtype=float64)b = empty(3, dtype=float64)y, jmax = y0, 0clock = time.clock()for i in range(1, len(t)):
u, f, j = y, True, 0while f:
A[0] = 1./tau, -u[2]/2., -u[1]/2.A[1] = u[2]/2., 1./tau, u[0]/2.A[2] = num_k2*u[1]/2., num_k2*u[0]/2., 1./taub[:] = y[0]/tau - u[1]*u[2]/2. + y[1]*y[2]/2.,\
y[1]/tau + u[0]*u[2]/2. - y[0]*y[2]/2.,\y[2]/tau + num_k2*u[0]*u[1]/2.
- num_k2*y[0]*y[1]/2.y1 = linalg.solve(A, b)f = norm(y1-u) > 1.e-10u = y1j += 1....Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 20 / 22
Numerical results
....if j > jmax:
print(j, end= ’ ’)jmax = j
y = y1sol[i, :] = y
print("Time: %.3f" % (time.clock() - clock))draw(sol, start=-100)draw(sol, start=-100, name="./fig/fig08.pdf")
3 4 Time: 0.941
990 992 994 996 998 1000
t
0.0000
0.0001
0.0002
0.0003
0.0004
0.0005
0.0006
p2
+q2
+1 ‖p2 + q2 − 1‖∞ = 6.2e− 04
numericexact
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 21 / 22
References
References
A. A. Samarskii.Theory of Difference Schemes.Marcel Dekker, New York, 2001.
B. Koren, R. Abgral, P. Bochev, J. Frank and B. Perot (eds.)Physics - compatible numerical methods.J. Comput. Phys., 257, Part B, 1039–1526, 2014.
Edik A. Ayryan, Mikhail D. Malykh, Leonid A. Sevastianov, Yu Ying.Finite Difference Schemes and Classical Transcendental FunctionsLecture Notes in Computer Science, Vol. 11189, pp. 235-242, 2018.
J. M. Sanz-Serna.Symplectic Runge-Kutta schemes for adjoint equations, automaticdifferentiation, optimal control, and more.SIAM Rev. 58(1), 3–33., 2016.
Blinkov & Gerd (SSU & JINR) On computer algebra aided numerical solution of ODE PCA 2019 22 / 22