ó
î’“Vc           @   sT  d  d l  Z d  d l m Z m Z m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z d Z d Z e e d <e e d <i d d 6Z e d	 e  d
 „  Z d „  Z d „  Z e d k rPd Z d Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d d d „ Z  d „  Z! d „  Z" e ƒ  e! ƒ  e" ƒ  e ƒ  n  d S(   iÿÿÿÿN(   t   plott   showt   legendt   holdt   rcParamst   rct   figuret   axhlinet   closet   xtickst   xlabelt   ylabelt   savefigt   axist   gridi   i   s   lines.linewidths	   font.sizei   t   sizet   fontc         C   sÐ   t  j t  j | ƒ t  j | ƒ f ƒ } | | d d d … f <xŒ t t | ƒ d ƒ D]t } | | d | | } | | d d … f t  j |  | | d d … f | | ƒ ƒ | | | d d d … f <qT W| S(   sÓ   The Euler scheme for solution of systems of ODEs.
    z0 is a vector for the initial conditions,
    the right hand side of the system is represented by func which returns
    a vector with the same size as z0 .i    Ni   (   t   npt   zerosR   t   ranget   lent   asarray(   t   funct   z0t   timet   zt   it   dt(    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   euler   s    '\c   	         s0  ‡  f d †  } t  j t  j | ƒ t  j | ƒ f ƒ } | | d d d … f <t  j | ƒ } xÎ t | d d !ƒ D]¹ \ } } | | d | | } | | d d … f | | | d d … f | ƒ | } | | d d … f | | | d d … f | ƒ | | | | ƒ | d | | d d d … f <qo W| S(   sÒ   The Heun scheme for solution of systems of ODEs.
    z0 is a vector for the initial conditions,
    the right hand side of the system is represented by func which returns
    a vector with the same size as z0 .c            s   t  j ˆ  |  | ƒ ƒ S(   sŒ   A local function to ensure that the return of func is an np array
        and to avoid lengthy code for implementation of the Heun algorithm(   R   R   (   R   t   t(   R   (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   f_np%   s    i    Niÿÿÿÿi   g       @(   R   R   R   t
   zeros_liket	   enumerate(	   R   R   R   R   R   t   zpR   R   R   (    (   R   s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   heun   s    ' 7dc         C   s¢  t  j t  j | ƒ t  j | ƒ f ƒ } | | d d d … f <t  j | ƒ } xOt | d d !ƒ D]:\ } } | | d | | } | d } t  j |  | | d d … f | ƒ ƒ }	 t  j |  | | d d … f |	 | | | ƒ ƒ }
 t  j |  | | d d … f |
 | | | ƒ ƒ } t  j |  | | d d … f | | | | ƒ ƒ } | | d d … f | d |	 d |
 d | | | | d d d … f <q` W| S(   sÛ   The Runge-Kutta 4 scheme for solution of systems of ODEs.
    z0 is a vector for the initial conditions,
    the right hand side of the system is represented by func which returns
    a vector with the same size as z0 .i    Niÿÿÿÿi   g       @g      @(   R   R   R   R   R    R   (   R   R   R   R   R!   R   R   R   t   dt2t   k1t   k2t   k3t   k4(    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   rk47   s    ' 
(444Nt   __main__gš™™™™™É?g      @c         C   s   t  |  t S(   N(   t   at   b(   R   (    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   <lambda>P   s    c         C   s"   t  j t |  t | ƒ d g ƒ S(   sV   A function which returns an np.array but less easy to read
        than f(z,t) below. i   (   R   R   R*   t   u_exact(   t   uR   (    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   f_localR   s    c         C   s   t  |  t | ƒ d g S(   s'   Simple to read function implementation i   (   R*   R-   (   R   R   (    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   fW   s    c          C   sç   d d l  m }  m } d } d } d } |  d | | d ƒ } t j d ƒ } t d ƒ | d <t t t g } xt | D]l } | t	 | | ƒ }	 t j
 t | ƒ |	 d	 d	 … d f ƒ }
 d
 | j |
 f } |
 | k  ss t | ƒ ‚ qs Wd	 S(   s9   Use knowledge of an exact numerical solution for testing.iÿÿÿÿ(   t   linspaceR   gVçž¯Ò<g       @i   i    i   g        Ns   %s failed with error = %g(   t   numpyR1   R   R   R   R-   R   R"   R(   R0   t   maxt	   func_namet   AssertionError(   R1   R   t   tolt   Tt   NR   R   t   schemest   schemeR   t	   max_errort   msg(    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   test_ODEschemes\   s    )g       @g      ð¿c         C   s   | |  | S(   t    (    (   R   R   R*   R+   (    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   f3r   s    c         C   sZ   d d l  m } d } t | ƒ | k rJ |  | | | | | ƒ | | S|  | | Sd  S(   Niÿÿÿÿ(   t   expg›+¡†›„=(   R2   R@   t   abs(   t   u0R   R*   R+   R@   t   TOL(    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   u_nonlin_analyticalv   s
    "c       
   C   s†  d d l  m }  m } m } m } m } m } t ƒ  d } d } d } d }	 t t	 t
 g }
 g  } i  } d d d	 d
 d d d g } d d d d d d g } d } xQ|
 D]I} d } |  d | | d ƒ } g  } xt | d ƒ D]õ } | t |	 | ƒ } | t |	 | ƒ | d d … d f ƒ } | | d ƒ } t | ƒ } t | d | | | | | d | d ƒ| j | j d t | ƒ ƒ t d ƒ | d k r°| j | | ƒ n  | } | d 9} |  d | | d ƒ } qá W| | | j <| d 7} q¨ Wt | d d ƒt d ƒ t d ƒ t ƒ  | d | } g  t d | d ƒ D] } | d | ^ qB} t j | ƒ } t ƒ  x( | D]  } t | t j | | ƒ ƒ qyWt d d  d d! | d" d d# d ƒt d$ d  d d! | d" d d# d ƒt d% d  d d! | d" d d# d ƒt | d& d' ƒ| j ƒ  } | j d( ƒ t | d d d) t ƒt d* ƒ t d+ ƒ t d t | ƒ d d g ƒ t d, d- t  ƒd S(.   s&    Test convergence rate of the methods iÿÿÿÿ(   R1   R   RA   t   log10t   meant   log2gVçž¯Ò<g       @i   i   t   rt   gR+   t   mt   kt   yt   ct   -s   --s   -.t   :s   v--s   *-.i    i   i   Nt	   markeverys   : N = t   ont   loct   bestt   Times
   log(error)g      ð?t   xmint   xmaxt	   linestylet   colorg       @g      @t   rotationiºÿÿÿt   theoreticalt   frameons   Number of unknownss   Scheme order approximations   ConvergenceODEschemes.pngt   transparent(!   R2   R1   R   RA   RE   RF   RG   R   R   R"   R(   R   R?   RD   R3   R    t   appendR4   t   strR   R   R
   R   R   R   R   R   R	   t   keyst   FalseR   R   t   True(   R1   R   RA   RE   RF   RG   R6   R7   t   NdtsR   R9   t   legendst   schemes_ordert   colorst
   linestylest   iclrR:   R8   R   t   order_approxR   R   t	   abs_errort	   log_errort   max_log_errt   previous_max_log_errt   N_listt   key(    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   convergence_test€   sh    .)*



."""

c    	      C   sá   d d l  m }  t ƒ  d } d } |  d | | d ƒ } d } t t t g } g  } xM | D]E } | t | | ƒ } t | | d d … d f ƒ | j | j	 ƒ q[ Wt | t
 | | ƒ ƒ | j d	 ƒ t | d
 d d t ƒd S(   s2   Plot the solutions for the test schemes in schemesiÿÿÿÿ(   R1   g      ø?i2   i    i   g       @Nt
   analyticalRR   RS   R[   (   R2   R1   R   R   R"   R(   R?   R    R]   R4   RD   R   R`   (	   R1   R7   R8   R   R   R9   Rc   R:   R   (    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   plot_ODEschemes_solutionsÄ   s    (#   R2   R   t   matplotlib.pyplotR    R   R   R   R   R   R   R   R   R	   R
   R   R   R   R   t   LNWDTt   FNTR   R   R"   R(   t   __name__R*   R+   R-   R/   R0   R=   R?   RD   Ro   Rq   (    (    (    s…   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/ODEschemes.pyt   <module>   s2   d 
 
 							
	D	