ambrosys The Taylor series method for ordinary differential equations Karsten Ahnert 1,2 Mario Mulansky 2 December, 8, 2011

Similar documents
Parallel implicit ordinary differential equation solver for cuda. Tomasz M. Kardaś

Solving Ordinary Differential Equations on GPUs

Precision Spin Tracking for Electric Dipole Moment Searches

4 Visualization and. Approximation

Mathematics for chemical engineers

CODE-GENERATION FOR DIFFERENTIAL EQUATION SOLVERS

Solving systems of nonlinear equations with arbitrary precision

over The idea is to construct an algorithm to solve the IVP ODE (8.1)

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

GiNaC. Symbolic computation with C++ Jens Vollinga. Institut für Physik Universität Mainz. GiNaC p.1/13

Floating-point numbers. Phys 420/580 Lecture 6

Euler s Methods (a family of Runge- Ku9a methods)

FIELA: A Fast Image Encryption with Lorenz Attractor using Hybrid Computing

Scientific Programming in C VI. Common errors

Ordinary Differential Equations

Numerical methods in physics

x n x n stepnumber k order r error constant C r+1 1/2 5/12 3/8 251/720 abs. stab. interval (α,0) /11-3/10

Finite difference methods

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

(Part - 1) P. Sam Johnson. April 14, Numerical Solution of. Ordinary Differential Equations. (Part - 1) Sam Johnson. NIT Karnataka.

GTC 2014 Session 4155

Math 225 Scientific Computing II Outline of Lectures

Homework Set #2-3, Math 475B

tutorial i: Using matcont for numerical integration of ODEs

1 Motivating Questions. 2 An Illustrating Example. 3 Software Quality. 4 An Example of OOP

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

Program Syntax; Operational Semantics

C++ Playground for Numerical Integration Method Developers

Performance Evaluation of Multiple and Mixed Precision Iterative Refinement Method and its Application to High-Order Implicit Runge-Kutta Method

over The idea is to construct an algorithm to solve the IVP ODE (9.1)

Ordinary differential equations solving methods

C++ Programming Lecture 1 Software Engineering Group

2. Numbers In, Numbers Out

Arrays and Functions

The Cantor Handbook. Alexander Rieder

A Transformation Tool for ODE Based Models

Information Science. No. For each question, choose one correct answer and write its symbol (A E) in the box.

Outline. When we last saw our heros. Language Issues. Announcements: Selecting a Language FORTRAN C MATLAB Java

1 date: December 12, 1997 le: walters9. Automatic Dierentiation. form of an algorithm (e.g. computer program) so

Expression Templates (Todd Veldhuizen)

c-xsc R. Klatte U. Kulisch A. Wiethoff C. Lawo M. Rauch A C++ Class Library for Extended Scientific Computing Springer-Verlag Berlin Heidelberg GmbH

Polymath 6. Overview

Zheng-Liang Lu Java Programming 45 / 79

Continuations provide a novel way to suspend and reexecute

Arithmetic Parameterization of General Purpose CFD Code

Module1: Numerical Solution of Ordinary Differential Equations. Lecture 6. Higher order Runge Kutta Methods

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

ODEs occur quite often in physics and astrophysics: Wave Equation in 1-D stellar structure equations hydrostatic equation in atmospheres orbits

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Multiple-Precision Arithmetic Library exflib (C++) FUJIWARA Hiroshi

Lecture Objectives. Structured Programming & an Introduction to Error. Review the basic good habits of programming

STEPHEN WOLFRAM MATHEMATICADO. Fourth Edition WOLFRAM MEDIA CAMBRIDGE UNIVERSITY PRESS

the Simulation of Dynamics Using Simulink

An improved image encryption algorithm based on chaotic maps

Exact real arithmetic. Keith Briggs

Parameter Estimation in Differential Equations: A Numerical Study of Shooting Methods

The SPL Programming Language Reference Manual

VALLIAMMAI ENGINEERING COLLEGE

Review Initial Value Problems Euler s Method Summary

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Osinga, HM., & Krauskopf, B. (Accepted/In press). The Lorenz manifold: Crochet and curvature.

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Introduction to Scientific Computing Lecture 8

3.1. Chapter 3: The cin Object. Expressions and Interactivity

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Simulation in Computer Graphics. Particles. Matthias Teschner. Computer Science Department University of Freiburg

The CAPD library and its applications

Dynamics and Vibrations Mupad tutorial

ODE IVP. An Ordinary Differential Equation (ODE) is an equation that contains a function having one independent variable:

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Linear Algebra libraries in Debian. DebConf 10 New York 05/08/2010 Sylvestre

Free Software Alternatives to Commercial Math Software

Computational Physics Operating systems

1 Maple Introduction. 1.1 Getting Started. 1.2 Maple Commands

Towards Automatic Partial Evaluation for the C++ Language. Robert Anisko

MIDTERM EXAM (Solutions)

Application 7.6A The Runge-Kutta Method for 2-Dimensional Systems

Type Checking. Chapter 6, Section 6.3, 6.5

Double Pendulum. Freddie Witherden. February 10, 2009

Stream Computing using Brook+

Math Precalculus (12H/4H) Review. CHSN Review Project

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter 2. Outline. Simple C++ Programs

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

5. Syntax-Directed Definitions & Type Analysis

CS 314 Principles of Programming Languages. Lecture 9

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Unit testing with pytest and nose 1

Building Compilers with Phoenix

Numerical Solutions of Differential Equations (1)

Exercises for a Numerical Methods Course

LECTURE 3. Compiler Phases

Silver Oak Engineering College and technology Information Technology Department

A New Vectorization Technique for Expression Templates in C++ arxiv: v1 [cs.ms] 6 Sep 2011

The interface for functions in the dune-functions module

Programmin Languages/Variables and Storage

Advanced Programming in Engineering Saurabh Srivastava, Thomas Weinhart Martin Robinson, and Stefan Luding MSM, CTW, UTwente, NL

Transcription:

1 The Taylor series method for ordinary differential equations Karsten Ahnert 1,2 Mario Mulansky 2 1 Ambrosys GmbH, Potsdam 2 Institut für Physik und Astronomie, Universität Potsdam December, 8, 2011 ambrosys

2 Outline 1 Solving ODEs 2 The method 3 Implementation 4 Conclusion

Solving Ordinary differential equations numerically Find a numerical solution of the initial value problem for an ODE ẋ = f (x, t), x(t = 0) = x 0 Example: Explicit Euler x(t + t) = x(t) + t f (x(t), t) + O( t 2 ) General scheme of order s x(t) x(t + t), or x(t + t) = F t x(t) + O( t s+1 )

4 Numerical methods Overview Methods: Steppers: x(t) x(t + t) Methods with embedded error estimation Adaptive step size control Dense output Examples: Explicit Runge Kutta methods Implicit methods for stiff systems Symplectic methods for Hamiltonian systems Multistep methods Taylor series method

5 Software for ordinary differential equations GNU Scientific library gsl, C Numerical recipes, C and C++ www.odeint.com, C++ odeint, Python apache.common.math, Java

6 Taylor series method ẋ = f (x) Taylor series of the solution x(t + t) = x(t) + tẋ(t) + t2 t3 ẍ(t) + 2! 3! x (3) (t) +... Auto Differentiation to calculate ẋ(t), ẍ(t), x (3) (t),... Applications: Problems with high accuracy astrophyical application, chaotic dynamical systems Arbitrary precision types Interval arithmetics

7 Software for the Taylor method Most software packages use generators ATSMCC, ATOMFT Fortran generator George F. Corliss and Y. F. Chang. ACM Trans. Math. Software, 8(2):114-144, 1982. Y. F. Chang and George F. Corliss, Comput. Math. Appl., 28:209-233, 1994 Taylor C Generator Ángel Jorba and Maorong Zou, Experiment. Math. Volume 14, Issue 1 (2005), 99-117. TIDES arbitrary precision, Mathematica generator Rodríguez, M. et. al, TIDES: A free software based on the Taylor series method, 2011 Operator overloading Adol-C cppad Expression templates Taylor

8 1 Solving ODEs 2 The method 3 Implementation 4 Conclusion

9 Notation Taylor series of the ODE x(t + t) = x(t) + tẋ(t) + t2 t3 ẍ(t) + 2! 3! x (3) (t) +... Introduce the reduced derivatives X i F i = 1 i! (f ( x(t) )) (i), X i = 1 i! x (i) (t), X i+1 = 1 i + 1 F i Taylor series x(t + t) = X 0 + tx 1 + t 2 X 2 + t 3 X 3 +...

10 Algebraic operations Expression trees Example: Lorenz system ẋ = σ(y x) ẏ = Rx y xz ż = bz + xy Expression trees σ y x y x z R z x y R x

11 The algorithm Evaluation of the expression tree Recursive determination of the Taylor coefficients 1. Initialize X 0 = x(t) 2. Calculate X 1 = F 0 (X 0 ) 3. Calculate X 2 = 1 2 F 1(X 0, X 1 ) 4. Calculate X 3 = 1 3 F 2(X 0, X 1, X 2 )... Calculate X s = 1 s F s 1(X 0, X 1,..., X s 1 ) Finally x(t + t) = X 0 + tx 1 + t 2 X 2 +...

11 The algorithm Evaluation of the expression tree Recursive determination of the Taylor coefficients 1. Initialize X 0 = x(t) 2. Calculate X 1 = F 0 (X 0 ) 3. Calculate X 2 = 1 2 F 1(X 0, X 1 ) 4. Calculate X 3 = 1 3 F 2(X 0, X 1, X 2 )... Calculate X s = 1 s F s 1(X 0, X 1,..., X s 1 ) Finally x(t + t) = X 0 + tx 1 + t 2 X 2 +... In every iteration the expression tree is evaluated!

12 Algebraic operations At every iteration the nodes in the expression tree have to be evaluated Formulas for iteration i: Constants: C i = cδ i,0 Dependend variable x: X i Summation s = l + r: S i = L i + R i Multiplication m = l r: M i = i j=0 L j R i j Division d = l/r: D i = 1/R 0 (L i i 1 j=0 D j R i j ) Formulas for special functions exist: exp, log, cos, sin,...

13 Algebraic operations Formulas At every iteration the nodes in the expression tree have to be evaluated. Formulas for iteration i: C Constants: C i = cδ i,0 x Dependend variable: x X i Summation s = l + r: S i = L i + R i Multiplication m = l r: M i = i L j R i j j=0 Division d = l/r: D i = 1/R 0 (L i i 1 j=0 D j R i j ) f(x) Formulas for special functions exist

14 Nice side effects Step size control Error estimate: err = X s err v = k ɛ rel +ɛ abs x k t = v 1/s No acceptance or rejection step is required Dense output is trivially present x(t + τ) = X 0 + τx 1 + τ 2 X 2 + τ 3 X 3 +..., 0 < τ < t Methods for order estimation exist

15 1 Solving ODEs 2 The method 3 Implementation 4 Conclusion

16 Implementation Taylor Download https://github.com/headmyshoulder/taylor

16 Implementation Taylor Download https://github.com/headmyshoulder/taylor Taylor will be integrated into odeint Implements some odeint - stepper

16 Implementation Taylor Download https://github.com/headmyshoulder/taylor Taylor will be integrated into odeint Implements some odeint - stepper Modern C++ Heavy use of the C++ template systems Expression templates for expression trees Template meta-programming

17 C++ Templates Here, C++ templates will be used to create the expression tree Expression Templates Template Metaprogramming to evaluate the expression templates It basically means using the template engine to generate a program from which the compiler creates then the binary C++ compilers always use the template engine (no additional compile step required)

17 C++ Templates Here, C++ templates will be used to create the expression tree Expression Templates Template Metaprogramming to evaluate the expression templates It basically means using the template engine to generate a program from which the compiler creates then the binary C++ compilers always use the template engine (no additional compile step required) Template engine and templates are a functional programming language Templates form a Turing-complete programming language You can solve any problem with the template engine

18 Expression templates template< class L, class R > struct binary_expression { binary_expression( string name, L l, R r )... L m_l; R m_r; }; struct terminal_expression { terminal_expression( string name )... }; const terminal_expression arg1( "arg1" ), arg2( "arg2" ); template< class L, class R > binary_expression< L, R > operator+( L l, R r ) { return binary_expression< L, R >( "Plus", l, r ); }... template< class Expr > void print( Expr expr ) {... } print( arg1 ); print( arg1 + ( arg2 + arg1 - arg2 ) );

19 Expression templates Expression template are constructed during compile-time Strong optimization no performance loss Lazyness Applications: Linear algebra systems, AD, (E)DSL Example MTL4 mtl4::dense_matrix< double > m1( n, n ), m2( n, n ), m3( n, n ) ; / / do s o m e t h i n g u s e f u l w i t h m1, m2, m3 mtl4::dense_matrix< double > result = m1 + 5.0 * m2 + 0.5 * m3 ; Last line creates an expression template which is evaluated to for( int i=0 ; i<n ; ++i ) for( int j=0 ; j<n ; ++j ) result( i, j ) = m1( i, j ) + 5.0 * m2( i, j ) + 0.5 * m3( i, j );

20 First example Lorenz system taylor_direct_fixed_order< 25, 3 > stepper; state_type x = {{ 10.0, 10.0, 10.0 }} ; double t = 0.0; double dt = 1.0; while( t < 50000.0 ) { stepper.try_step( fusion::make_vector( sigma * ( arg2 - arg1 ), R * arg1 - arg2 - arg1 * arg3, arg1 * arg2 - b * arg3 ), x, t, dt ); cout << t << "\t" << x << "\t" << dt << endl; } ODE is a compile time sequence of expression templates The expression template is defined with boost::proto No preprocessing step is necessary!

Expression templates and ODEs Boost.Proto (C++ library) Creation, manipulation and evaluation of the syntax tree Grammar = Allowed expression + (optional) Transformation

Expression templates and ODEs Boost.Proto (C++ library) Creation, manipulation and evaluation of the syntax tree Grammar = Allowed expression + (optional) Transformation Taylor library uses Proto as front end: The ODE is a set of Proto expression templates ODE is transformed into a custom expression template struct tree_generator : proto::or_ < variable_generator< proto::_ >, constant_generator, plus_generator< tree_generator >, minus_generator< tree_generator >, multiplies_generator< tree_generator >, divides_generator< tree_generator >, unary_generator< tree_generator > > { };

22 Expression templates and ODEs Evaluation of the custom template iteration several times of the template The nodes implement the rules for the algebraic expressions Optimzation of the expression tree R R Example: The plus node template< class Left, class Right > struct plus_node : binary_node< Left, Right > { plus_node( const Left &left, const Right &right ) : binary_node< Left, Right >( left, right ) { } }; template< class State, class Derivs > Value operator()( const State &x, const Derivs &derivs, size_t which ) { return m_left( x, derivs, which ) + m_right( x, derivs, which ); }

3 The interface Interface for easy implementation of arbitrary ODEs exist taylor_direct_fixed_order< 25, 3 > stepper; stepper.try_step( sys, x, t, dt ); sys represents the ODE, Example: fusion::make_vector( sigma * ( arg2 - arg1 ), R * arg1 - arg2 - arg1 * arg3, arg1 * arg2 - b * arg3 ) x is the state of the ODE is in-place transformed t,dt are the time and the step size

24 Results Performance comparison against full Fortran code The Lorenz system as test system Benchmarking: a Fortran code with a non-ad implementation Both codes have the same performance, run-time deviation is less 20% Exact result depends strongly on the used compiler (gcc 4.5, gcc 4.6, gfortran,...)

25 Comparison against other mehtods 10 2 10 1 Runge Kutta Cash Karp 54 Taylor s=15 Taylor s=25 t Comp 10 0 10-1 10-2 10-12 10-9 10-6 10-3 ε rel Taylor has good performance for high precision Outperforms the classical Runge-Kutta steppers

26 Conclusion Taylor A C++ library for the Taylor series method of ordinary differential equations Uses expression templates as basis for the automatic differentiation Fast Uses modern C++ methods Template Metaprogramming is the main programming technique

27 Outlook The library is not complete Implementation of special functions Implementation of stencils for lattice equations Implementation of variable order Implementation of dense output functionality Portability layer for arbitrary precision types Integration into odeint

28 Resources Download and development https://www.github.com/headmyshoulder/taylor Odeint odeint.com Contributions and feedback are highly welcome