ó
 ·´Vc           @   sP  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 m Z m Z m Z d Z d Z e e d <e e d <d „  Z d „  Z d	 „  Z e d
 k rLd Z d Z d „  Z d „  Z d „  Z d „  Z  d d d „ Z! d d d „ Z" d „  Z# d „  Z$ d „  Z% d „  Z& e% ƒ  e ƒ  n  d S(   iÿÿÿÿN(   t   plott   showt   legendt   holdt   rcParamst   rct   figuret   axhlinet   closet   xtickst   titlet   xlabelt   ylabelt   savefigt   axist   gridt   subplotst   setpi   i
   s   lines.linewidths	   font.sizec         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   zerost   sizet   ranget   lent   asarray(   t   funct   z0t   timet   zt   it   dt(    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   euler   s    '\c         C   s<  t  j t  j | ƒ t  j | ƒ f ƒ } | | d d d … f <t  j | ƒ } xé t | d d !ƒ D]Ô \ } } | | d | | } | | d d … f t  j |  | | d d … f | ƒ ƒ | } | | d d … f t  j |  | | d d … f | ƒ ƒ t  j |  | | | ƒ ƒ | d | | d d d … f <q` 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 .i    Niÿÿÿÿi   g       @(   R   R   R   t
   zeros_liket	   enumerateR   (   R   R   R   R   t   zpR   t   tR   (    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   heun   s    ' @v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(    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   rk41   s    ' 
(444Nt   __main__gš™™™™™É?g      @c         C   s   t  |  t S(   N(   t   at   b(   R"   (    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   <lambda>J   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"   (    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   f_localL   s    c         C   s   t  |  t | ƒ d g S(   s'   Simple to read function implementation i   (   R+   R.   (   R   R"   (    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   fQ   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   numpyR2   R   R   R   R.   R   R#   R)   R1   t   maxt	   func_namet   AssertionError(   R2   R   t   tolt   Tt   NR   R   t   schemest   schemeR   t	   max_errort   msg(    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   test_ODEschemesV   s    )g       @g      ð¿c         C   s   | |  | S(   t    (    (   R   R"   R+   R,   (    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   f3l   s    c         C   sZ   d d l  m } d } t | ƒ | k rJ |  | | | | | ƒ | | S|  | | Sd  S(   Niÿÿÿÿ(   t   expg›+¡†›„=(   R3   RA   t   abs(   t   u0R"   R+   R,   RA   t   TOL(    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   u_nonlin_analyticalp   s
    "c       
   C   sv  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 ƒ d S(,   s&    Test convergence rate of the methods iÿÿÿÿ(   R2   R   RB   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 approximation(   R3   R2   R   RB   RF   RG   RH   R   R   R#   R)   R   R@   RE   R4   R    t   appendR5   t   strR   R   R   R   R   R   R   R   R	   t   keyst   FalseR   (   R2   R   RB   RF   RG   RH   R7   R8   t   NdtsR   R:   t   legendst   schemes_ordert   colorst
   linestylest   iclrR;   R9   R   t   order_approxR   R   t	   abs_errort	   log_errort   max_log_errt   previous_max_log_errt   N_listt   key(    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   convergence_testz   sf    .)*



."""

c    /   
      s\  d d l  m }  m } m } m } m } m } d d l m } m	 } m
 } m }	 d d l m }
 m } d GHd GH| d ƒ } d } d } d	 d
 g } | d } | d } d | |
 d | ƒ | | | d d | d ƒ } | | | ƒ } | | | ƒ } | | | ƒ } | | | | ‰  |	 | g | ƒ } |	 | g | ƒ } |	 | g | ƒ } |	 | g ˆ  ƒ ‰  ‡  f d †  } t j | | ƒ | | ƒ | | ƒ g ƒ } t ƒ  d } d } t t t g } g  } i  } d d d d d d d g } d d d d d d g } d } xÈ| D]À}  d }! |  | | |! d ƒ }" t j |" ƒ }# d }$ x( |" D]  }% | |% ƒ |# |$ <|$ d }$ qKWg  }& x>t | d ƒ D],}' |  | | |" ƒ }( | |# |( d d … d f ƒ }) | |) d ƒ }* t |* ƒ }+ t |" d |* | |' | | d  |! d ƒ| j |  j d! t |! ƒ ƒ t d" ƒ |' d k rL|& j |, |+ ƒ n  |+ }, |! d 9}! |  | | |! d ƒ }" t j |" ƒ }# d }$ x( |" D]  }% | |% ƒ |# |$ <|$ d }$ qŽWq†W|& | |  j <| d 7} qWt | d# d$ ƒt d% ƒ t d& ƒ t  ƒ  |! d | }! g  t d | d ƒ D] }' |! d |' ^ q}- t j! |- ƒ }- t ƒ  x( | D]  }. t |- t j! | |. ƒ ƒ qU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 d0 ƒ t | d# d$ d1 t% ƒt& d2 ƒ t d3 ƒ t d4 ƒ t' d t |- ƒ d d g ƒ d S(5   sv   Test convergence rate of the methods, by using the Method of Manufactured solutions.
            The coefficient function f is chosen to be the normal distribution
            f = (1/(sigma*sqrt(2*pi)))*exp(-((t-mu)**2)/(2*sigma**2)).
            The ODE to be solved is than chosen to be: f''' + f''*f + f' = RHS,
            leading to to f''' = RHS - f''*f - f
         iÿÿÿÿ(   R2   R   RB   RF   RG   RH   (   RA   t   symbolst   difft   lambdify(   t   sqrtt   pis(   solving equation f''' + f''*f + f' = RHSs$   which lead to f''' = RHS - f''*f - fR"   g      à?g      ø¿g      @i    i   i   c            sI   t  j |  ƒ } |  d |  d ˆ  | ƒ |  d |  d |  d g | (| S(   Ni   i   i    (   R   R   (   RM   R"   t   yout(   t   RHS(    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyR   Ý   s    6gVçž¯Ò<i   RI   RJ   R,   RK   RL   RM   RN   RO   s   --s   -.RP   s   v--s   *-.id   NRQ   s   : N = RR   RS   RT   RU   s
   log(error)g      ð?RV   RW   RX   RY   g       @g      @RZ   iºÿÿÿR[   R\   s   Method of Manufactured Solutions   Number of unknownss   Scheme order approximation((   R3   R2   R   RB   RF   RG   RH   t   sympyRA   Ro   Rp   Rq   t   mathRr   Rs   R   t   arrayR   R   R#   R)   R   R   R4   R    R]   R5   R^   R   R   R   R   R   R   R   R	   R_   R`   R
   R   (/   R2   R   RB   RF   RG   RH   RA   Ro   Rp   Rq   Rr   Rs   R"   t   sigmat   mut   Domaint   t0t   tendR1   t   dfdtt   d2fdtt   d3fdtR   R   R7   Ra   R:   Rb   Rc   Rd   Re   Rf   R;   R9   R   t	   fanalyticRL   t   tauRg   R   R   Rh   Ri   Rj   Rk   Rl   Rm   (    (   Ru   sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   manufactured_solution¾   s¢    ."

7* *



."""


c    6   	      s|  d d l  m }  m } d d l m } m ‰ m ‰ m ‰ d d l m	 } m
 } d d l  m } m } ˆ d ƒ } d } d }	 d | | d	 | ƒ | | |	 d	 d	 | d	 ƒ }
 ˆ |
 | ƒ } ˆ | | ƒ } ˆ | | ƒ } | | | |
 ‰  ˆ | g |
 t ƒ }
 ˆ | g | t ƒ } ˆ | g | ƒ } ˆ | g ˆ  ƒ ‰  ‡  f d
 †  } d3 \ } } t j |
 | ƒ | | ƒ | | ƒ g ƒ } t t t g } i  } g  } d } t d t | ƒ d t d t ƒ\ } } x…t | ƒ D]w\ } } d } g  } g  } xô t | d ƒ D]â } |  | | | d ƒ } | d k rM| j | d | d ƒ n  | | | | ƒ } |
 | ƒ }  | | d d … d f |  ƒ }! | j t |! ƒ ƒ | d | j | | d d … d f ƒ | j d t | | ƒ ƒ | d	 9} q	W| | | j <| d | j | |  d ƒ | j d ƒ | d | j | j ƒ | d | j d ƒ qÚW| d d	 j | d d d t ƒ| d d j  d ƒ t! | d d d d g d d d d d  g ƒ‡ ‡ ‡ f d! †  }" t j" | ƒ }# t j" | d" ƒ }$ t j" | d# ƒ }% t j" | d$ ƒ }& |" |$ |# d d g ƒ \ }' }( |" |% |# d d	 g ƒ \ }) }* |" |& |# d d g ƒ \ }+ }, d d% l m# }- ˆ d& ƒ } |' | |( }. d' |- |. ƒ d' }/ |) | |* }0 d' |- |0 ƒ d' }1 |+ | |, }2 d' |- |2 ƒ d' }3 |/ GH|1 GH|3 GHˆ | |. t ƒ }. ˆ | |0 t ƒ }0 ˆ | |2 t ƒ }2 | d	 | d	 } g  t d | d	 ƒ D] } | d	 | ^ qZ}4 t j" |4 ƒ }4 t |4 ƒ GHt |$ ƒ GHt$ ƒ  t |4 | |$ ƒ d( ƒ t |4 | |. |# ƒ ƒ d) ƒ t |4 | |% ƒ d* ƒ t |4 | |0 |# ƒ ƒ d+ ƒ t |4 | |& ƒ d, ƒ t |4 | |2 |# ƒ ƒ d- ƒ d. |/ d/ |1 d0 |3 g }5 t |5 d d d t ƒt% d1 ƒ t& d2 ƒ d S(4   ss   Test convergence rate of the methods, by using the Method of Manufactured solutions.
            The coefficient function f is chosen to be the normal distribution
            f = (1/(sigma*sqrt(2*pi)))*exp(-((t-mu)**2)/(2*sigma**2)).
            The ODE to be solved is than chosen to be: f''' + f''*f + f' = RHS,
            leading to f''' = RHS - f''*f - f
         iÿÿÿÿ(   R2   RB   (   RA   Ro   Rp   Rq   (   Rr   Rs   (   t   logRH   R"   g      à?i   i   c            sI   t  j |  ƒ } |  d |  d ˆ  | ƒ |  d |  d |  d g | (| S(   s   Function that returns the dfn/dt of the differential equation f + f''*f + f''' = RHS
                as a system of 1st order equations; f = f1 
                        f1' = f2 
                        f2' = f3
                        f3' = RHS - f1 - f2*f3
            
                Args:
                    y(array): solutian array [f1, f2, f3] at time t
                    t(float): current time
        
                Returns:
                    yout(array): differantiation array [f1', f2', f3'] at time t
            i   i   i    (   R   R   (   RM   R"   Rt   (   Ru   (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyR   H  s    6g      ø¿g      @i   t   shareyt   squeezei   i    Ns   $h$ = s   k:s   $u_m$R   RS   RT   R\   R/   R	   t   yticksg        gš™™™™™Ù?gš™™™™™é?g333333ó?c            sZ  d d l  m } ˆ d ƒ \ } } | | d | |  d } | | d | |  d } | | g ‰  | | g ‰ ‡  ‡ ‡ f d †  } | d d | ƒ }	 |	 j ƒ  }
 ˆ ˆ g |
 ƒ } ˆ ˆ g ˆ  ƒ } | ‰ xZ t d ƒ D]L } t j | ˆ ƒ ƒ ‰  t j | ˆ ƒ ƒ } ˆ t j | ˆ  ƒ } | ‰ qØ Wt ˆ d d ƒ ˆ d <t ˆ d	 d
 ƒ ˆ d	 <ˆ S(   sÎ   Function that solves for the nonlinear set of equations
                error1 = C*h1^p --> f1 = C*h1^p - error1 = 0
                error2 = C*h2^p --> f2 = C h2^p - error 2 = 0
                where C is a constant h is the step length and p is the order,
                with use of a newton rhapson solver. In this case C and p are
                the unknowns, whereas h and error are knowns. The newton rhapson 
                method is an iterative solver which take the form:
                xnew = xold - (J^-1)*F, where J is the Jacobi matrix and F is the 
                residual funcion.
                    x = [C, p]^T
                    J = [[df1/dx1  df2/dx2],
                         [df2/dx1  df2/dx2]]
                    F = [f1, f2]
                This is very neatly done with use of the sympy module
                
                Args:
                    error(list): list of calculated errors [error(h1), error(h2)]
                    h(list): list of steplengths corresponding to the list of errors
                    x0(list): list of starting (guessed) values for x
                
                Returns:
                    x(array): iterated solution of x = [C, p]
        
            iÿÿÿÿ(   t   Matrixs   C piþÿÿÿc            s   ˆ ˆ  |  ˆ | ƒ S(   N(    (   R   t   j(   t   FRp   t   x(    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   jacobiElement©  s    i   i   i    i   i   (   Rv   Rˆ   t   invR   R   R   t   dott   round(   t   errort   ht   x0Rˆ   t   Ct   pt   f1t   f2RŒ   t   Jacobit	   JacobiInvt   JacobiInvfunct   Ffunct   nt   Jinvt   xnew(   Rp   Rq   Ro   (   RŠ   R‹   sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   Newton_solver_sympyˆ  s(    
R   R#   R)   (   t   latexR‘   t   $R,   s   b--RJ   s   g--RI   s   r--s   ${\epsilon}_{euler}$s   ${\epsilon}_{heun}$s   ${\epsilon}_{rk4}$s   -log(h)s   -log($\epsilon$)(   g      ø¿g      @('   R3   R2   RB   Rv   RA   Ro   Rp   Rq   Rw   Rr   Rs   R„   RH   R   Rx   R   R#   R)   R   R   t   TrueR`   R    R   R]   R4   R    R^   R5   t	   set_titlet
   set_xlabelR   t
   set_ylabelR   R   RŸ   R   R   R   (6   R2   RB   RA   Rr   Rs   R„   RH   R"   Ry   Rz   R1   R~   R   R€   R   R|   R}   R   R:   t   schemes_errorR‘   t   Ntdst   figt   axRL   R;   R9   R   t
   legendListR   R   R   R   Rh   Rž   t   htt
   eulerErrort	   heunErrort   rk4Errort   C_eulert   p_eulert   C_heunt   p_heunt   C_rk4t   p_rk4RŸ   t   epsilon_eulert   epsilon_euler_latext   epsilon_heunt   epsilon_heun_latext   epsilon_rk4t   epsilon_rk4_latexRl   t
   LegendList(    (   Ru   Rp   Rq   Ro   sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   manufactured_solution_Nonlinear-  sª    "7*' (!+5.
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ÿÿÿÿ(   R2   g      ø?i2   i    i   g       @Nt
   analyticalRS   RT   R\   (   R3   R2   R   R   R#   R)   R@   R    R]   R5   RE   R   R`   (	   R2   R8   R9   R   R   R:   Rb   R;   R   (    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   plot_ODEschemes_solutionsì  s    ('   R3   R   t   matplotlib.pyplotR    R   R   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   R   t   LNWDTt   FNTR   R#   R)   t   __name__R+   R,   R.   R0   R1   R>   R@   RE   Rn   Rƒ   R»   R½   (    (    (    sw   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/ODEschemes.pyt   <module>   s.   v 
 
							
	D	o	¿	