ó
îVc           @   sç   d  d l  m Z m Z m Z m Z d  d l m Z m Z m Z d  d l	 m
 Z
 m Z d  d l j Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l  Z d  d l  m Z m Z d  d l  m Z d d d d	 d
 d d d  Z d S(   i˙˙˙˙(   t   expt   cost   linspacet
   zeros_like(   t   eulert   heunt   rk4(   t   sqrtt   piN(   t   sinR   (   R    s5   (1/(sigma*sqrt(2*pi)))*exp(-((t-mu)**2)/(2*sigma**2))g      à?s
   [-1.5,2.5]R   id   t   Noc   %         s  d d l  m } m } m }	 m }
 m } m } m } m } | d  } t	 |   } t	 |  \ } } t	 |  } | | |  } | | |  } | | |  } | | | |   | | g |  } | | g |  } | | g |  } | | g      t
 j | | | d  }   f d   } t
 j | |  | |  | |  g  } | | | |  } | d d  d f } t
 j |  } d } x( | D]  } | |  | | <| d } qW| d k rÏt j   d	 GHn  t j   g  } t j | | d
  t j | | d  d } d } | j |  | j |  t j d  t j d  t j d  t j | d d d t d d l m }  |    }! t j |! d d |! j d  d d l }" |" j |! j    }# |    }! t j |! d d |! j d  d |! j   j d  d }$ t |$ d  }$ |# S(   s  Function that solves the equation f + f''*f + f''' = RHS numerically with different ODEsolvers with 
        use of the method of manufactured solution. The necesary Right hand side (RHS) is calculated from
        the differential equation by choosing/manufacturing a wanted solution. The differentiations are
        performed with use of the sympolic python module sympy. 
        In this case the chosen solution f=Expression is set default to be a normal distribution.
        In order to be able to handle various functions f/Ekspression on various domains and with different
        solvers, filled in on a form on a webpage  the arguments "Expression", "Domain" and "solver" are of 
        type str. and are evaluated into python expressions with use of the eval() function.
        The default values of the arguments are automatically filled into the form of "view.html" file.
        
        Args:
            Expression(str): the manufactured solution
            sigma(float): standard deviation
            mu(float): mean value
            Domain(str): the computational domain 
            solver(str): the chosen ODEsolver
            Nsteps(int): the number of timesteps
            erase(str): erase previous plot (Yes/No)
            
        Returns:
            figdata_png(string): the plot of (t, fnumeric) and (t, fanalytic) encoded with BytesIo to the fed into html file
        i˙˙˙˙(   t   symbolst   difft	   integratet   Rationalt   lambdifyR    R	   R   t   ti   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 equation; f = f1 
                    f1' = f2 
                    f2' = f3
                    f3' = RHS - f1 - f2*f3
        
        Args:
            y(array): solutian array [f1, f2, f3] at time t
            t(time): current time

        Returns:
            yout(array): differantiation array [f1', f2', f3'] at time t
        i   i   i    (   t   npR   (   t   yR   t   yout(   t   RHS(    s   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/compute.pyt   func;   s    6Ni    t   Yest   hellot   gs   r--t	   fanalytict   fnumerics   Normal distributions   f(t)t   loct   bestt   frameon(   t   BytesIOt   formatt   pngt   svgs   <svgs   utf-8(    t   sympyR   R   R   R   R   R    R	   R   t   evalR   R   t   arrayR   t   pltt   figuret   plott   appendt   titlet   xlabelt   ylabelt   legendt   Falset   ioR   t   savefigt   seekt   base64t	   b64encodet   getvaluet   splitt   unicode(%   t
   Expressiont   sigmat   mut   Domaint   solvert   Nstepst   eraseR   R   R   R   R   R    R	   R   R   t   ft   t0t   tendt   dfdtt   d2fdtt   d3fdtt   timeR   t   y0t   uR   R   t   it   legendst   analyticlegendt   numericlegendR   t   figfileR1   t   figdata_pngt   figdata_svg(    (   R   s   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/compute.pyt   compute_Manufactured_solution   sd    :*

		(   t   numpyR    R   R   R   t
   ODEschemesR   R   R   t   mathR   R   t   matplotlib.pyplott   pyplotR%   t   osRC   t   globR   R	   t   eRM   (    (    (    s   /Users/fredrikeikelandfossan/Documents/git/tkt4140_digital_compendium/digital_compendium/chapter1/src-ch1/Flask_app_MMS/compute.pyt   <module>   s   "0	