Comp6024 Pdes

Embed Size (px)

Citation preview

  • 8/12/2019 Comp6024 Pdes

    1/47

    Solving partial differential equations (PDEs)

    Hans Fangohr

    Engineering and the EnvironmentUniversity of Southampton

    United [email protected]

    May 3, 2012

    1 / 4 7

    http://find/
  • 8/12/2019 Comp6024 Pdes

    2/47

    Outline I

    1 Introduction: what are PDEs?

    2 Computing derivatives using finite differences

    3 Diffusion equation

    4 Recipe to solve 1d diffusion equation

    5 Boundary conditions, numerics, performance

    6 Finite elements

    7 Summary

    2 / 4 7

    http://find/
  • 8/12/2019 Comp6024 Pdes

    3/47

    This lecture

    tries to compress several years of material into 45 minutes

    has lecture notes and code available for download athttp://www.soton.ac.uk/fangohr/teaching/comp6024

    3 / 4 7

    http://www.soton.ac.uk/~fangohr/teaching/comp6024http://www.soton.ac.uk/~fangohr/teaching/comp6024http://www.soton.ac.uk/~fangohr/teaching/comp6024http://www.soton.ac.uk/~fangohr/teaching/comp6024http://find/
  • 8/12/2019 Comp6024 Pdes

    4/47

    What are partial differential equations (PDEs)

    Ordinary Differential Equations (ODEs)one independent variable, for example t in

    d2x

    dt2 =

    k

    mx

    often the indepent variable t is the timesolution is function x(t)

    important for dynamical systems, population growth,control, moving particles

    Partial Differential Equations (ODEs)multiple independent variables, for example t, x and y in

    u

    t =D2ux2 +

    2u

    y2

    solution is function u(t,x,y)important for fluid dynamics, chemistry,electromagnetism, . . . , generally problems with spatial

    resolution4 / 4 7

    http://find/http://goback/
  • 8/12/2019 Comp6024 Pdes

    5/47

    2d Diffusion equation

    ut

    =D 2ux2

    + 2

    uy2

    u(t,x,y) is the concentration [mol/m3]

    t is the time [s]

    x is the x-coordinate [m]

    y is the y-coordinate [m]

    D is the diffusion coefficient [m2/s]

    Also known as Ficks second law. The heat equation has thesame structure (and urepresents the temperature).Example:http://www.youtube.com/watch?v=WC6Kj5ySWkQ

    5 / 4 7

    http://www.youtube.com/watch?v=WC6Kj5ySWkQhttp://www.youtube.com/watch?v=WC6Kj5ySWkQhttp://find/
  • 8/12/2019 Comp6024 Pdes

    6/47

    Examples of PDEs

    Cahn Hilliard Equation(phase separation)

    Fluid dynamics (including ocean and atmospheric models,plasma physics, gas turbine and aircraft modelling)Structural mechanics and vibrations, superconductivity,

    micromagnetics, . . .6 / 4 7

    http://en.wikipedia.org/wiki/Cahn%E2%80%93Hilliard_equationhttp://en.wikipedia.org/wiki/Cahn%E2%80%93Hilliard_equationhttp://find/
  • 8/12/2019 Comp6024 Pdes

    7/47

    Computing derivatives

    using finite differences

    7 / 4 7

    O

    http://find/
  • 8/12/2019 Comp6024 Pdes

    8/47

    Overview

    Motivation:

    We need derivatives of functions for example foroptimisation and root finding algorithmsNot always is the function analytically known (but weare usually able to compute the function numerically)The material presented here forms the basis of thefinite-difference technique that is commonly used tosolve ordinary and partial differential equations.

    The following slides show

    the forward difference technique

    the backward difference technique and thecentral difference technique to approximate thederivative of a function.We also derive the accuracy of each of these methods.

    8 / 4 7

    Th 1 d

    http://find/
  • 8/12/2019 Comp6024 Pdes

    9/47

    The 1st derivative

    (Possible) Definition of the derivative (or differentialoperator d

    dx)

    f(x) df

    dx(x) = lim

    h0

    f(x+h) f(x)

    h

    Use differenceoperator to approximate differential

    operator

    f(x) =df

    dx(x) = lim

    h0

    f(x+h) f(x)

    h

    f(x+h) f(x)

    h

    can now compute an approximation off(x) simply byevaluating f (twice).

    This is called the forward differencebecause we use f(x)and f(x+h).

    Important questions: How accurate isthisapproximation?9 / 4 7

    A f h f d diff

    http://find/
  • 8/12/2019 Comp6024 Pdes

    10/47

    Accuracy of the forward difference

    Formal derivation using the Taylor series off around x

    f(x+h) = n=0

    hn f(n)

    (x)n!

    = f(x) +hf(x) +h2f(x)

    2! +h3

    f(x)3!

    +. . .

    Rearranging for f(x)

    hf(x) =f(x+h) f(x) h2f(x)

    2! h3

    f(x)3!

    . . .

    f(x) =

    1

    h f(x+h) f(x) h2 f

    (x)

    2!

    h

    3 f(x)

    3!

    . . .=

    f(x+h) f(x)

    h

    h2f(x)2! h

    3 f(x)3!

    h . . .

    = f(x+h) f(x)

    h

    hf(x)

    2! h2

    f(x)

    3! . . .

    10/47

    A f h f d diff (2)

    http://find/
  • 8/12/2019 Comp6024 Pdes

    11/47

    Accuracy of the forward difference (2)

    f(x) = f(x+h) f(x)

    h hf(x)

    2! h2

    f(x)

    3! . . . Eforw(h)

    f(x) = f(x+h) f(x)

    h +Eforw(h)

    Therefore, the error term Eforw(h) is

    Eforw(

    h) =

    hf(x)

    2! h2

    f(x)

    3! . . .

    Can also be expressed as

    f(x) = f(x+h) f(x)

    h + O(h)

    11/47

    Th 1 t d i ti i th b k d diff

    http://find/http://goback/
  • 8/12/2019 Comp6024 Pdes

    12/47

    The 1st derivative using the backward difference

    Another definition of the derivative (or differential

    operator ddx)

    df

    dx(x) = lim

    h0

    f(x) f(x h)

    h

    Use difference operator to approximate differentialoperator

    df

    dx(x) = lim

    h0

    f(x) f(x h)

    h

    f(x) f(x h)

    h

    This is called the backward differencebecause we usef(x) and f(x h).

    How accurate is the backward difference?

    12/47

    A f th b k d diff

    http://find/
  • 8/12/2019 Comp6024 Pdes

    13/47

    Accuracy of the backward difference

    Formal derivation using the Taylor Series off around x

    f(x h) = f(x) hf(x) +h2 f(x)2!

    h3 f(x)3!

    +. . .

    Rearranging for f(x)

    hf(x) =f(x) f(x h) +h2 f(x)2!

    h3 f(x)3!

    . . .

    f(x) = 1

    h

    f(x) f(x h) +h2

    f(x)2!

    h3f(x)

    3! . . .

    = f(x) f(x h)

    h +

    h2f(x)2! h

    3 f(x)3!

    h . . .

    = f(x) f(x h)

    h +h

    f(x)2!

    h2f(x)

    3! . . .

    13/47

    A f th b k d diff (2)

    http://find/http://goback/
  • 8/12/2019 Comp6024 Pdes

    14/47

    Accuracy of the backward difference (2)

    f(x) = f(x) f(x h)

    h +hf(x)

    2! h2f(x)

    3! . . . Eback(h)

    f(x) = f(x) f(x h)

    h +Eback(h) (1)

    Therefore, the error term Eback(h) is

    Eback(h) =hf(x)

    2!

    h2f(x)

    3!

    . . .

    Can also be expressed as

    f(x) = f(x) f(x h)

    h + O(h)

    14/47

    Combining backward and forward differences (1)

    http://find/
  • 8/12/2019 Comp6024 Pdes

    15/47

    Combining backward and forward differences (1)

    The approximations are

    forward:

    f(x) = f(x+h) f(x)

    h +Eforw(h) (2)

    backward

    f(x) = f

    (x

    ) f

    (x h

    )h +Eback(h) (3)

    Eforw(h) = hf(x)

    2! h2

    f(x)3!

    h3f(x)

    4! h4

    f(x)5!

    . . .

    Eback(h) = hf(x)

    2! h2

    f(x)3!

    +h3f(x)

    4! h4

    f(x)5!

    +. . .

    Add equations (2) and (3) together, then the error cancels

    partly! 15/47

    Combining backward and forward differences (2)

    http://find/
  • 8/12/2019 Comp6024 Pdes

    16/47

    Combining backward and forward differences (2)

    Add these lines together

    f(x) =

    f(x+h) f(x)

    h +Eforw(h)

    f(x) = f(x) f(x h)

    h +Eback(h)

    2f(x) = f(x+h) f(x h)

    h +Eforw(h) +Eback(h)

    Adding the error terms:

    Eforw(h) +Eback(h) = 2h2 f

    (x)3!

    2h4f(x)

    5! . . .

    The combined (central) difference operator is

    f(x) = f(x+h) f(x h)

    2h +Ecent(h)

    with

    Ecent(h) = h2f(x)

    3! h4f(x)

    5! . . . 16/47

    Central difference

    http://find/
  • 8/12/2019 Comp6024 Pdes

    17/47

    Central difference

    Can be derived (as on previous slides) by adding forwardand backward difference

    Can also be interpreted geometrically by defining thedifferential operator as

    df

    dx(x) = lim

    h

    0

    f(x+h) f(x h)

    2h

    and taking the finite difference form

    df

    dx(x)

    f(x+h) f(x h)

    2h

    Error of the central difference is only O

    (h2

    ), i.e. betterthan forward or backward difference

    It is generally the case that symmetric differences

    are more accurate than asymmetricexpressions. 17/47

    Example (1)

    http://find/
  • 8/12/2019 Comp6024 Pdes

    18/47

    Example (1)

    Using forward difference to estimate the derivative off(x) = exp(x)

    f(x) fforw = f(x+h) f(x)

    h

    =exp(x+h) exp(x)

    hNumerical example:

    h= 0.1, x= 1

    f(1) fforw(1.0) = exp(1.1)exp(1)

    0.1 = 2.8588

    Exact answers is f(1.0) = exp(1) = 2.71828

    (Central diff: fcent(1.0) = exp(1+0.1)exp(10.1)

    0.2 = 2.72281)

    18/47

    Example (2)

    http://find/
  • 8/12/2019 Comp6024 Pdes

    19/47

    Example (2)

    Comparison: forward difference, central difference and exact

    derivative off(x) = exp(x)

    01

    2 34 5

    x

    0

    2 0

    4 0

    6 0

    8 0

    1 0 0

    1 2 0

    1 4 0

    d

    f

    /

    d

    x

    (

    x

    )

    A p p r o x i m a t i o n s o f d f / d x f o r f ( x ) = e x p ( x )

    f o r w a r d h = 1

    c e n t r a l h = 1

    f o r w a r d h = 0 . 0 0 0 1

    e x a c t

    19/47

    Summary

    http://find/
  • 8/12/2019 Comp6024 Pdes

    20/47

    Summary

    Can approximate derivatives offnumerically using onlyfunction evaluations off

    size of step hvery important

    central differences has smallest error term

    name formula error

    forward f(x) = f(x+h)f(x)h

    O(h)

    backward f(x) = f(x)f(xh)h

    O(h)

    central f(x) = f(x+h)

    f(x

    h)

    2h O(h2)

    20/47

    Appendix: source to compute figure on page 19 I

    http://find/
  • 8/12/2019 Comp6024 Pdes

    21/47

    Appendix: source to compute figure on page19I

    EPS=1 # v er y l ar ge E PS to p ro vo ke i na c cu ra cy

    def f o r w a r d d i f f ( f , x , h = E P S ) :# df / dx = ( f (x +h )- f( x) )/ h + O (h )

    return ( f (x + h) -f ( x) )/ h

    def b a c k w a r d d i f f ( f , x , h = E P S ) :

    # df / dx = ( f (x )- f(x -h ) )/ h + O (h )

    return ( f (x ) -f (x - h) )/ h

    def c e n t r a l d i f f ( f , x , h = E P S ) :

    # df / dx = ( f( x+ h) - f (x -h ))/ h + O (h ^2)

    return ( f ( x +h ) - f ( x -h ) ) /( 2* h )

    if _ _n a me _ _ = = " _ _ m a in _ _ " :

    # c r ea te e xa mp le p lo t

    import pylab

    import n um py as np

    a =0 # l ef t a nd

    b =5 # r ig ht l im it s f or x

    N=11 # s t e p s

    21/47

    Appendix: source to compute figure on page 19 II

    http://find/
  • 8/12/2019 Comp6024 Pdes

    22/47

    Appendix: source to compute figure on page19II

    def f ( x ) :

    " "" O u r t es t f un ti on w it h

    c o n ve n ie n t p r op e rt y t ha t

    df / dx = f "" " return n p . e x p ( x )

    x s = n p . l i n s p a c e ( a , b , N )

    f or wa rd = []

    f o rw a rd _ sm a ll _ h = [ ]

    c en tr al = []

    for x in xs :

    f o rw a rd . a p pe n d ( f o r wa r d di f f ( f , x ) )

    c e nt r al . a p pe n d ( c e n tr a l di f f ( f , x ) )

    f o r w a r d _ s m a l l _ h . a p p e n d (

    f o r w a r d d i f f ( f , x , h = 1 e - 4 ) )

    p y l a b . f i g u r e ( f i g s i z e = ( 6 , 4 ) )p y l a b . a x i s ( [ a , b , 0 , n p . e x p ( b ) ] )

    p y l ab . p l ot ( x s , f o rw a rd , ^ , l a b e l = f o r w ar d h = % g % E P S )

    p y l ab . p l ot ( x s , c e nt r al , x , l a b e l = c e n t ra l h = % g % E P S )

    pylab.plot(xs,forward_small_h ,o,

    l a be l = f o r wa r d h = % g % 1 e - 4)

    x s fi n e = n p . l i ns p ac e ( a , b , N * 10 0 )

    22/47

    Appendix: source to compute figure on page 19 III

    http://find/
  • 8/12/2019 Comp6024 Pdes

    23/47

    Appendix: source to compute figure on page19III

    p y l a b . p l o t ( x s f i n e , f ( x s f i n e ) , - , l a b e l = e x a c t )

    p y l a b . g r i d ( )

    p y la b . l e g en d ( l o c = u pp e r l ef t )

    p y l a b . x l a b e l ( " x " )

    p y l a b . y l a b e l ( " d f / d x ( x ) " )p y la b . t i t le ( " A p p r o x im a t io n s o f d f / dx f or f ( x ) = e xp ( x ) " )

    p y l a b . p l o t ( )

    pylab. savefig( central -and- forward - difference .pdf )

    p y l a b . s h o w ( )

    23/47

    Note: Eulers (integration) method derivation

    http://find/
  • 8/12/2019 Comp6024 Pdes

    24/47

    Note: Euler s (integration) method derivation

    using finite difference operator

    Use forward difference operator to approximatedifferential operator

    dy

    dx(x) = lim

    h0

    y(x+h) y(x)

    h

    y(x+h) y(x)

    h

    Change differential to difference operator in dydx

    =f(x, y)

    f(x, y) = dy

    dx

    y(x+h) y(x)

    h

    hf(x, y) y(x+h) y(x)= yi+1 = yi+hf(xi, yi)

    Eulers method (for ODEs) can be derived from theforward difference operator.

    24/47

    Note: Newtons (root finding) method

    http://find/
  • 8/12/2019 Comp6024 Pdes

    25/47

    Note: Newton s (root finding) method

    derivation from Taylor series

    We are looking for a root, i.e.

    we are looking for a x so thatf(x) = 0.

    We have an initial guess x0 which we refine in subsequent iterations:

    xi+1=xi hi where hi = f(xi)

    f(xi). (4)

    .

    This equation can be derived from the Taylor series off around x.Suppose we guess the root to be at x and x+h is the actuallocation of the root (so h is unknown and f(x+h) = 0):

    f(x

    +h

    ) = f

    (x

    ) +hf

    (x

    ) +. . .

    0 = f(x) +hf(x) +. . .

    = 0 f(x) +hf(x)

    h f(x)

    f(x). (5)

    25/47

    http://find/
  • 8/12/2019 Comp6024 Pdes

    26/47

    The diffusion equation

    26/47

    Diffusion equation

    http://find/
  • 8/12/2019 Comp6024 Pdes

    27/47

    q

    The 2d operator

    2

    x2

    +

    2

    y2

    is called the Laplaceoperator, so that we can also write

    u

    t =D

    2

    x2+

    2

    y2

    = Du

    The diffusion equation (with constant diffusioncoefficient D) reads u

    t =Duwhere the Laplace

    operator depends on the number dof spatial dimensions

    d= 1: = 2

    x2

    d= 2: = 2x2

    + 2y2

    d= 3: = 2

    x2+

    2

    y2+

    2

    z2

    27/47

    1d Diffusion equation ut = D ux2

    http://find/
  • 8/12/2019 Comp6024 Pdes

    28/47

    q t x2

    In one spatial dimension, the diffusion equation reads

    ut

    =D 2u

    x2

    This is the equation we will use as an example.

    Lets assume an initial concentrationu(x, t0) =

    12

    exp

    (xxmean)2

    2

    with xmean= 0and

    width = 0.5.

    64

    2 0 24

    6

    x

    0 . 0

    0 . 1

    0 . 2

    0 . 3

    0 . 4

    0 . 5

    0 . 6

    0 . 7

    0 . 8

    u

    (

    x

    ,

    t

    _

    0

    )

    ( x , t

    0

    )

    28/47

    1d Diffusion eqn ut = D ux2

    , time integration I

    http://find/
  • 8/12/2019 Comp6024 Pdes

    29/47

    q t x2 , g

    Let us assume that we have some way of computingD 2u

    x2 at time t0 and lets call this g(x, t0), i.e.

    g(x, t0) D2u(x, t0)

    x2

    We like to solve

    u(x, t)

    t =g(x, t0)

    to compute u(x, t1) at some later time t1.

    Use finite difference time integration scheme:

    Introduce a time step size hso that t1 =t0+h.

    29/47

    1d Diffusion eqn ut = D ux2

    , time integration II

    http://find/
  • 8/12/2019 Comp6024 Pdes

    30/47

    q t x2 , g

    Change differential operator to forward difference operator

    g(x, t0) = u(x, t)t

    = limh0

    u(x, t0+h) u(x, t0)h

    (6)

    u(x, t0+h) u(x, t0)

    h (7)

    Rearrange to find u(x, t1) u(x, t0+h) gives

    u(x, t1) u(x, t0) +hg(x, t0)

    We can generalise this using ti=t0+ih to read

    u(x, ti+1) u(x, ti) +hg(x, ti) (8)

    If we can find g(x, ti), we can compute u(x, ti+1)

    30/47

    1d Diffusion eqn ut = D ux2 , spatial part I

    http://find/
  • 8/12/2019 Comp6024 Pdes

    31/47

    q t x p p

    u

    t =D

    2u

    x2 =g(x, t)

    Need to compute g(x, t) =D 2u(x,t)x2

    for a givenu(x, t).

    Can ignore the time dependence here, and obtain

    g(x) =D2u(x)

    x2 .

    Recall that

    2

    ux2 = x ux

    and we that know how to compute ux

    using centraldifferences.

    31/47

    Second order derivatives from finite differences I

    http://find/
  • 8/12/2019 Comp6024 Pdes

    32/47

    Recall central difference equation for first order derivative

    df

    dx

    (x) f(x+h) f(x h)

    2hwill be more convenient to replace hby 1

    2h:

    df

    dx(x)

    f(x+ 12

    h) f(x 12

    h)

    h

    32/47

    Second order derivatives from finite differences II

    http://find/
  • 8/12/2019 Comp6024 Pdes

    33/47

    Apply the central difference equation twice to obtain d2f

    dx2:

    d2f

    dx2(x) =

    d

    dx

    df

    dx(x)

    d

    dx

    f(x+ 1

    2h) f(x 1

    2h)

    h

    = 1

    h

    d

    dxf

    x+

    1

    2h

    d

    dxf

    x

    1

    2h

    1

    h f(x+h) f(x)

    h

    f(x) f(x h)

    h =

    f(x+h) 2f(x) +f(x h)

    h2 (9)

    33/47

    Recipe to solve ut = D ux2

    http://find/
  • 8/12/2019 Comp6024 Pdes

    34/47

    t x

    1 Discretise solution u(x, t) into discrete values2 uij u(xj, ti)where

    xj x0+jx andti t0+it.

    3 Start with time iteration i= 04 Need to know configuration u(x, ti).

    5 Then compute g(x, ti) =D2ux2

    using finite differences(9).

    6 Then compute u(x, ti+1) based on g(x, ti) using (8)7 increase ito i+ 1, then go back to5.

    34/47

    A sample solution ut = D ux2 ,I

    http://find/
  • 8/12/2019 Comp6024 Pdes

    35/47

    x

    import n um py as np

    import m a t pl o t li b . p y p l ot a s p lt

    import m a t p lo t l ib . a n i ma t io n a s a n i ma t io n

    a , b = - 5 , 5 # size of box

    N = 51 # n um be r of s u bd i vi s io n s

    x = n p . l i n s p a c e ( a , b , N ) # p o si t i on s o f s u bd i v is i o ns

    h = x [ 1 ] - x [ 0 ] # d i s c r et i s at i o n s t ep s iz e i n x - d i r e c ti o n

    def t o t a l ( u ) :" "" C o m pu te s t ot al n um be r of m ol es i n u . "" "

    return ( ( b - a ) / f l o a t ( N ) * n p . s u m ( u ) )

    def g a u s s d i s t r ( m e a n , s i g m a , x ) :

    " "" R e tu rn g au ss d i st r ib u ti o n f or g iv en n um py a rr ay x " ""

    return 1 . / ( s i g m a * n p . s q r t ( 2 * n p . p i ) ) * n p . e x p (

    - 0 . 5 * ( x - m e a n ) * * 2 / s i g m a * * 2 )

    # s t a r ti n g c o n fi g u ra t i on f or u ( x , t0 )

    u = g a u ss d is t r ( m e an = 0 . , s i gm a = 0 .5 , x = x )

    35/47

    A sample solution ut = D ux2 ,II

    http://find/
  • 8/12/2019 Comp6024 Pdes

    36/47

    def c om pu te _g ( u , D , h ):

    " "" g i ve n a u (x , t ) in a rr ay , c om pu te g (x , t )= D * d ^2 u / dx ^ 2

    u si ng c en tr al d if f er e nc es w it h s pa ci ng h ,

    a nd r et ur n g (x , t ). " "" d 2 u_ d x2 = n p . z er o s ( u . sh ap e , n p . f lo a t )

    for i in r a n g e ( 1 , l e n ( u ) - 1 ) :

    d 2u _d x2 [ i ] = ( u [ i +1 ] - 2* u [ i ]+ u [ i -1 ]) / h * *2

    # s p ec ia l c as es a t b ou nd ar y : a ss um e N eu ma n b ou nd ar y

    # c o nd it io ns , i . e . no c ha ng e o f u o ve r b ou nd ar y

    # so t ha t u [ 0] - u [ - 1] =0 a nd t hu s u [ - 1] = u [0 ]

    i =0d 2u _d x2 [ i ] = ( u [i + 1] - 2 * u[ i ]+ u [ i ]) / h ** 2

    # s am e at o th er e nd so t ha t u [N -1] - u [ N ]=0

    # a nd t hu s u [ N ]= u [ N -1 ]

    i = l e n ( u ) - 1

    d 2u _d x2 [ i ] = ( u [i ] - 2* u [ i ]+ u [ i - 1] )/ h * *2

    return D * d 2 u _ d x 2

    def a d va n ce _ ti m e ( u , g , dt ) :

    "" " Gi ve n t he a rr ay u , t he r at e of c ha ng e a rr ay g ,

    an d a t im es te p dt , c om pu te th e s ol ut io n fo r u

    a ft er t , u si ng s im pl e E ul er m et ho d . " ""

    u = u + d t * g

    36/47

    A sample solution ut = D ux2 ,III

    http://find/
  • 8/12/2019 Comp6024 Pdes

    37/47

    return u

    # s ho w e xa mp le , q ui ck a nd d ir tl y , l ot s o f g lo ba l v ar i ab le s

    dt = 0.01 # s te p s iz e or t imes t e p sb e f o re u p d at i n g gr a p h = 2 0 # p l ot ti ng is s lo w

    D = 1. # D i f f us i on c o e ff i c ie n t

    s te ps do ne = 0 # k ee p t ra ck of i te r at io ns

    def d o _ s t e p s ( j , n s t e p s = s t e p s b e f o r e u p d a t i n g g r a p h ) :

    " "" F u n ct io n c al le d by F u nc A ni m at io n c la ss . C om pu te s

    n s te p s i te r at i on s , i . e . c a rr i es f o rw a rd s o lu t io n f ro m u ( x , t _i ) t o u ( x , t_ { i + n s t ep s } ) .

    """

    global u , s t e p s d o n e

    for i in r a n g e ( n s t e p s ) :

    g = com pute_g ( u , D , h )

    u = a dv an ce _t im e ( u , g , dt )

    s te ps do ne += 1

    t im e_ pa ss ed = s te ps do ne * dt

    print ( " s t e ps d o ne = %5 d , t im e = % 8 gs , t o ta l ( u ) = %8 g " %

    (stepsdone ,time_passed ,total(u )))

    l . s e t _ y d a t a ( u ) # upd at e data in plot

    f i g 1 . c a n v a s . d r a w ( ) # r ed ra w th e c an va s

    37/47

    A sample solution ut = D ux2 ,IV

    http://find/
  • 8/12/2019 Comp6024 Pdes

    38/47

    return l ,

    f ig 1 = p lt . f i gu re ( ) # s e t up a n im a ti o n

    l , = p lt . p l o t ( x ,u , b - o ) # p lo t in it ia l u( x ,t )

    # t he n c om pu te s ol ut io n a nd a ni ma tel i ne _ an i = a n im a t io n . F u n c A n im a t io n ( f i g1 ,

    d o _s t ep s , r a n ge ( 1 0 0 0 0) )

    p l t . s h o w ( )

    38/47

    Boundary conditions I

    http://find/
  • 8/12/2019 Comp6024 Pdes

    39/47

    For ordinary differential equations (ODEs), we need toknow theinitial value(s)to be able to compute a solution.

    For partial differential equations (PDEs), we need toknow the initial values and extra information about the

    behaviour of the solution u(x, t)at the boundary of thespatial domain (i.e. at x= aand x=b in this example).

    Commonly used boundary conditions are

    Dirichlet boundary conditions: fix u(a) =c to some

    constant.Would correspond here to some mechanism that keepsthe concentration u at position x=a constant.

    39/47

    Boundary conditions II

    http://find/
  • 8/12/2019 Comp6024 Pdes

    40/47

    Neuman boundary conditions: fix the change ofu acrossthe boundary, i.e.

    u

    x(a) =c.

    For positive/negative c this corresponds to an imposedconcentration gradient.

    For c= 0, this corresponds to conservation of the atomsin the solution: as the gradient across the boundarycannot change, no atoms can move out of the box.

    (Used in our program on slide 35)

    40/47

    Numerical issues

    http://find/
  • 8/12/2019 Comp6024 Pdes

    41/47

    The time integration scheme we use isexplicitbecause wehave an explicit equation that tells us how to computeu(x, ti+1) based on u(x, ti) (equation (8) on slide 30)

    An implicit scheme would compute u(x, ti+1) based onu(x, ti) and on u(x, ti+1).

    The implicit scheme is more complicated as it requiressolving an additional equation system just to findu(x, ti+1) but allows larger step sizes t for the time.

    The explicit integration scheme becomes quickly unstableift is too large. t depends on the chose spatialdiscretisation x.

    41/47

    Performance issues

    http://find/
  • 8/12/2019 Comp6024 Pdes

    42/47

    Our sample code is (nearly) as slow as possibleinterpreted languageexplicit for loopsenforced small step size from explicit scheme

    Solutions:Refactor for-loops into matrix operations and use(compiled) matrix library (numpy for small systems, usescipy.sparse for larger systems)Use library function to carry out time integration (will

    use implicit method if required), for examplescipy.integrate.odeint.

    42/47

    Finite Elements

    http://find/
  • 8/12/2019 Comp6024 Pdes

    43/47

    Another widely spread way of solving PDEs is using so-calledfinite elements.

    Mathematically, the solution u(x) for a problem like2ux2

    =f(x) is written as

    u(x) =N

    i=1uii(x) (10)

    where each ui is a number (a coefficient), and each i(x)a known function of space.The i are called basisorshape functions.Each i is normally chosen to be zero for nearly all x, and

    to be non-zero close to a particular node in the finiteelement mesh.By substitution (10) into the PDE, a matrix system canbe obtained, which if solved provides the coefficients

    ui, and thus the solution. 43/47

    Finite Elements vs Finite differences

    http://find/
  • 8/12/2019 Comp6024 Pdes

    44/47

    Finite differencesare mathematically much simpler andfor simple geometries (such as cuboids) easier toprogram

    Finite elementshave greater flexibility in the shape of the domain,the specification and implementation of boundaryconditions is easierbut the basic mathematics and code is morecomplicated.

    44/47

    Practical observation on time integration

    http://find/
  • 8/12/2019 Comp6024 Pdes

    45/47

    Usually, we solve thespatialpart of a PDE using somediscretisation scheme such as finite differences and finiteelements).

    This results in a set of coupled ordinary differential

    equations (where time is the independent variable).Can think of this as one ODE for every cube from ourdiscretisation.

    This temporalpart is then solved using time integration

    schemes for (systems of) ordinary differential equations.

    45/47

    Summary

    http://find/
  • 8/12/2019 Comp6024 Pdes

    46/47

    Partial differential equations important in many contexts

    If no analytical solution known, use numerics.

    Discretise the problem through

    finite differences (replace differential with differenceoperator, corresponds to chopping space and time in

    little cuboids)finite elements (project solution on localised basisfunctions, often used with tetrahedral meshes)related methods (finite volumes, meshless methods).

    Finite elements and finite difference calculations arestandard tools in many areas of engineering, physics,chemistry, but increasingly in other fields.

    46/47

    changeset: 53:a22b7f13329etag: tipuser: Hans Fangohr [MBP13] date: Fri Dec 16 10:57:15 2011 +0000

    http://find/
  • 8/12/2019 Comp6024 Pdes

    47/47

    date: Fri Dec 16 10:57:15 2011 +0000

    47/47

    http://find/