Technische Universität München Lehrstuhl für Informatik V Dr. Tobias Neckel Ivana Buha, Iryna Burak SS 2024 April 17, 2024 Algorithms for Uncertainty Quantification Tutorial 1: Python overview + ODE numerics In this worksheet, we do a short overview of programming in python. Note: Everything that follows is a lot easier if you use a Unix-based operating system on your machine. If you are using Windows then we recommend you to use Anaconda. We recommend you to create a virtual environment for this course. We assume that you are familiar with the basics concepts of computer programming: • data types • variables • loops • arrays • functions • etc. If you are new to programming and you want to cover the basic concepts, see e.g. tutorialspoint.com/computer programming/computer programming basics.htm. Short overview of programming with python Throughout these tutorials, we use python 3.x. How to use python1 ? • directly in the command line: simply type python 1 extensive python 3.x documentation at https://docs.python.org/3/ 1 • via Jupyter2 notebooks • via an IDE, e.g. Spyder, Eclipse + PyDev, PyCharm, VisualStudio python is • strongly typed: types are enforced • dynamically, implicitly typed: you don’t need to declare variables • case sensitive: a and A are different variables • object-oriented: everything is an object Data structures Build-in data structures • lists: similar to 1D arrays • dictionaries: hash-tables (key-value pairs) • tuples: immutable 1D arrays • sets: since python 2.5 >>> # l i s t example >>> m y l i s t = [ ’ a ’ , 1 , 2 , [ 1 , 2 ] ] >>> m y l i s t [ 1 ] = 22 >>> m y l i s t [ 1 , 22 , ’ a ’ , [ 1 , 2 ] ] >>> # t u p l e example >>> mytuple = ( 1 , 2 ) >>> mytuple [ 1 ] = 2 Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<s t d i n >” , l i n e 1 , in <module> TypeError : ’ t u p l e ’ object does not s u p p o r t item a s s i g n m e n t >>> # d i c t i o n a r y example >>> mydict = { 1 : ’ a ’ , 0 : ’ b ’ } >>> mydict [ 0 ] 2 http://jupyter.org/ 2 ’b ’ >>> mydict . keys ( ) [0 , 1] >>> mydict . v a l u e s ( ) [ ’b ’ , ’a ’ ] >>> mydict [ 5 ] = ’ f o o ’ >>> mydict {0: ’a ’ , 1: ’b ’ , 5: ’ foo ’} >>> # s e t example >>> m y l i s t = [ 1 , 2 , 2 , 3 ] >>> set ( m y l i s t ) set ( [ 1 , 2 , 3 ] ) >>> # l i s t s l i c i n g >>> m y l i s t = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] >>> m y l i s t [ 0 : − 1 ] [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8] >>> m y l i s t [ 2 : 4 ] [3 , 4] >>> m y l i s t [ −1: −4: −1] [9 , 8 , 7] >>> m y l i s t [ : 5 ] [1 , 2 , 3 , 4 , 5] Flow control statements Flow control statements in python: if, for, and while. To obtain a list of numbers, use range<number> >>> for i in range ( 5 ) : ... if i % 2: ... print ( i ) ... else : ... print ( i ) ... −1 1 1 3 3 3 Functions Functions are declared with the def keyword. Optional arguments are set in the function declaration. In python, functions can return a tuple (this way, you can effectively return multiple values) >>> def i n c r e a s e b y o n e ( x ) : ... return x + 1 ... >>> i n c r e a s e b y o n e ( 2 ) 3 >>> i n c r e a s e b y o n e ( 2 . 3 ) 3.3 >>> n e w i n c r e a s e b y o n e = lambda x : ( x + 1 , 0 ) >>> r e s = n e w i n c r e a s e b y o n e ( 1 ) >>> r e s (2 , 0) >>> r e s [ 1 ] = 2 Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<s t d i n >” , l i n e 1 , in <module> TypeError : ’ t u p l e ’ object does not s u p p o r t item a s s i g n m e n t Use of external libraries >>> # import e n t i r e l i b r a r y >>> import [ libname ] >>> # import s p e c i f i c f u n c t i o n s / p a c k a g e s >>> from [ libname ] import [ funcname | packagename ] Object-oriented programming Classes are declared with the class keyword. Private variables and methods can be declared, however, by convention, this is not enforced by the language. >>> c l a s s MyClass : init ( self ) : ... def ... print ( ’ t h i s i s an i n s t a n c e o f MyClass ’ ) 4 ... s e l f . v a r i a b l e = 10 ... def myfunction ( s e l f , x ) : ... return 2∗ x ... >>> i n s t a n c e = MyClass ( ) t h i s i s an i n s t a n c e o f MyClass >>> i n s t a n c e . myfunction ( 1 0 ) 20 >>> i n s t a n c e . v a r i a b l e 10 >>> # i n h e r i t a n c e example >>> c l a s s SubClass ( MyClass ) : init ( self ) : ... def ... super ( ) . i n i t ( ) ... print ( ’ t h i s i s an i n s t a n c e o f t he s u b c l a s s ’ ) ... >>> i n s t a n c e=SubClass ( ) t h i s i s an i n s t a n c e o f MyClass t h i s i s an i n s t a n c e o f th e s u b c l a s s Polymorphism is also possible >>> c l a s s Animal : i n i t ( s e l f , name ) : ... def ... s e l f . name = name ... def t a l k ( s e l f ) : ... r a i s e NotImplementedError ( ” A b s t r a c t method must be implemented ” ) ... >>> c l a s s Cat ( Animal ) : ... def t a l k ( s e l f ) : ... return ’Meow ! ’ ... >>> c l a s s Dog ( Animal ) : ... def t a l k ( s e l f ) : ... return ’ Woof ! Woof ! ’ ... >>> c l a s s Human( Animal ) : ... def bark ( s e l f ) : 5 ... print ( ’UQ i s t he b e s t ’ ) ... >>> a n i m a l s = [ Cat ( ’Mr . P u r r i f y ’ ) , Dog ( ’ L a s s i e ’ ) , Human( ’me ’ ) ] >>> for animal in a n i m a l s : ... print ( animal . name + ’ : ’ + animal . t a l k ( ) ) ... Mr . P u r r i f y : Meow ! L a s s i e : Woof ! Woof ! Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<s t d i n >” , l i n e 2 , in <module> F i l e ”<s t d i n >” , l i n e 5 , in t a l k NotImplementedError : A b s t r a c t method must be implemented Scientific computing with python Since python is an interpreted and dynamically typed programming language, python programs can be slow compared to compiled statically typed programming languages, such as C and Fortran. However, using suitable libraries, python can be used for scientific computing3 . Numpy - multidimensional data arrays package that provides high-performance vector, matrix and higher-dimensional data structures for python. >>> import numpy as np >>> # do c l e v e r t h i n g s SciPy - Library of scientific algorithms It includes • Special functions (scipy.special) • Integration (scipy.integrate) • Optimization (scipy.optimize) 3 some materials on scientific computing https://github.com/jrjohansson/scientific-python-lectures 6 • Interpolation (scipy.interpolate) • Fourier Transforms (scipy.fftpack) • Signal Processing (scipy.signal) • Linear Algebra (scipy.linalg) • Sparse Eigenvalue Problems (scipy.sparse) • Statistics (scipy.stats) • Multi-dimensional image processing (scipy.ndimage) • File IO (scipy.io) matplotlib - 2D and 3D plotting Using Fortran and C code In python it is relatively easy to call out to libraries with compiled C or Fortran code • Fortran: F2PY • C: ctypes, Cython Tools for high-performance computing applications • thread parallelism: the multiprocessing package • IPython parallel • MPI: mpi4py • opencl: pyopencl • etc. 7 Assignment A linear damped oscillator is a modeled by a second order ODE 2 d y dy dt2 (t) + c dt (t) + ky(t) = f cos(ωt) y(0) = y0 dy (0) = y1 , dt (1) where c is the damping coefficient, k the spring constant, f the forcing amplitude, ω the frequency, y0 represents the initial position, whereas y1 is the initial velocity. Assignment 1 Transform the above equation in a first order system of ODEs. Hint: Use helper functions z0 (t) := y(t), dy(t) , z1 (t) := dt .. . zn−1 (t) := dy n−1 (t) dtn−1 and transform into system dz0 := z1 , dt dz1 := z2 , dt .. . f (t, z0 , dz0 dz1 dzn−1 , ,··· , ) := 0. dt dt dt Solution: z0 (t) := y(t), dy(t) dz0 (t) z1 (t) := = , dt dt d2 y(t) dz1 (t) z2 (t) := = . 2 dt dt 8 Write down system: dz0 (t) = z1 (t) dt dz1 (t) ż1 := = −cz1 (t) − kz0 (t) + f cos(ωt) dt ż0 := or in matrix/vector notation: ż0 0 1 z0 (t) 0 = + ż1 −k −c z1 (t) f cos(ωt) Assignment 2 Considering t ∈ [0, 20], c = 0.5, k = 2.0, f = 0.5, ω = 1.05, y0 = 0.5, y1 = 0.0 and the setup as in Assignment 1, discretize the oscillator using explicit Euler time on the system with ∆t = 0.1, 0.05, 0.01. You can use standard python lists or numpy lists. Change ∆t = 0.5, what do you observe? Download ex2 framework.py from Moodle and implement the unimplemented part. Solution: See code Code Solution.py Assignment 3 With the same setup, solve Equation 1 using the odeint4 function from scipy.integrate. Download ex3 framework.py from Moodle and implement the unimplemented part. Solution: See code Code Solution.py 4 see https://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.integrate.odeint.html 9