Exemplo n.º 1
0
def viz(
    I, V, f, c, L, dt, C, T, ue,  # PDE paramteres
    umin, umax,               # Interval for u in plots
    animate=True,             # Simulation with animation?
    tool='matplotlib',        # 'matplotlib' or 'scitools'
    solver_function=solver,   # Function with numerical algorithm
    plot_error=False
    ):
    """Run solver and visualize u at each time level."""
    sleep=0.02
    def plot_u_st(u, x, t, n):
        """user_action function for solver."""
        plt.plot(x, u, 'r-',
                 xlabel='x', ylabel='u',
                 axis=[0, L, umin, umax],
                 title='t=%f' % t[n], show=True)
        # Let the initial condition stay on the screen for 2
        # seconds, else insert a pause of 0.2 s between each plot
        time.sleep(2) if t[n] == 0 else time.sleep(sleep)
        plt.savefig('frame_%04d.png' % n)  # for movie making

    class PlotMatplotlib:
        def __call__(self, u, x, t, n):
            """user_action function for solver."""
            if plot_error==True:
                if n == 0:
                    plt.ion()
                    self.lines = plt.plot(x, u-ue(x,0), 'r-')
                    plt.xlabel('x');  plt.ylabel('error')
                    plt.axis([0, L, umin, umax])
                    plt.legend(['t=%f' % t[n]], loc='lower left')
                else:
                    self.lines[0].set_ydata(u-ue(x,t[n]))
                    plt.legend(['t=%f' % t[n]], loc='lower left')
                    plt.draw()
                time.sleep(2) if t[n] == 0 else time.sleep(sleep)
                plt.savefig('tmp_%04d.png' % n)  # for movie making
            else:
                if n == 0:
                    plt.ion()
                    self.lines = plt.plot(x, u, 'r-')
                    self.lines2 = plt.plot(x,ue(x,0),'b--')
                    plt.xlabel('x');  plt.ylabel('error')
                    plt.axis([0, L, umin, umax])
                    plt.legend(['t=%f' % t[n]], loc='lower left')
                else:
                    self.lines[0].set_ydata(u)
                    self.lines2[0].set_ydata(ue(x,t[n]))
                    plt.legend(['t=%f' % t[n]], loc='lower left')
                    plt.draw()
                    
                time.sleep(2) if t[n] == 0 else time.sleep(sleep)
                plt.savefig('tmp_%04d.png' % n)  # for movie making




    if tool == 'matplotlib':
        import matplotlib.pyplot as plt
        plot_u = PlotMatplotlib()
    elif tool == 'scitools':
        import scitools.std as plt  # scitools.easyviz interface
        plot_u = plot_u_st
    import time, glob, os

    # Clean up old movie frames
    for filename in glob.glob('tmp_*.png'):
        os.remove(filename)

    # Call solver and do the simulaton
    user_action = plot_u if animate else None
    u, x, t, cpu = solver_function(
        I, V, f, c, L, dt, C, T, user_action)

    # Make video files
    fps = 4  # frames per second
    codec2ext = dict(flv='flv', libx264='mp4', libvpx='webm',
                     libtheora='ogg')  # video formats
    filespec = 'tmp_%04d.png'
    movie_program = 'ffmpeg'  # or 'avconv'
    for codec in codec2ext:
        ext = codec2ext[codec]
        cmd = '%(movie_program)s -r %(fps)d -i %(filespec)s '\
              '-vcodec %(codec)s movie.%(ext)s' % vars()
        os.system(cmd)

    if tool == 'scitools':
        # Make an HTML play for showing the animation in a browser
        plt.movie('tmp_*.png', encoder='html', fps=fps,
                  output_file='movie.html')
    return cpu
Exemplo n.º 2
0
def viz(
    I, V, f, c, L, dt, C, T,  # PDE paramteres
    umin, umax,               # Interval for u in plots
    animate=True,             # Simulation with animation?
    tool='matplotlib',        # 'matplotlib' or 'scitools'
    solver_function=solver,   # Function with numerical algorithm
    ):
    """Run solver and visualize u at each time level."""
    sleep=0.001
    def plot_u_st(u, x, t, n):
        """user_action function for solver."""
        plt.plot(x, u, 'r-',
                 xlabel='x', ylabel='u',
                 axis=[0, L, umin, umax],
                 title='t=%f' % t[n], show=True)
        # Let the initial condition stay on the screen for 2
        # seconds, else insert a pause of 0.2 s between each plot
        time.sleep(2) if t[n] == 0 else time.sleep(sleep)
        plt.savefig('frame_%04d.png' % n)  # for movie making

    class PlotMatplotlib:
        def __call__(self, u, x, t, n):
            """user_action function for solver."""
            if n == 0:
                plt.ion()
                self.lines = plt.plot(x, u, 'r-')
                plt.xlabel('x');  plt.ylabel('u')
                plt.axis([0, L, umin, umax])
                plt.legend(['t=%f' % t[n]], loc='lower left')
            else:
                self.lines[0].set_ydata(u)
                plt.legend(['t=%f' % t[n]], loc='lower left')
                plt.draw()
            time.sleep(2) if t[n] == 0 else time.sleep(sleep)
            plt.savefig('tmp_%04d.png' % n)  # for movie making

    if tool == 'matplotlib':
        import matplotlib.pyplot as plt
        plot_u = PlotMatplotlib()
    elif tool == 'scitools':
        import scitools.std as plt  # scitools.easyviz interface
        plot_u = plot_u_st
    import time, glob, os

    # Clean up old movie frames
    for filename in glob.glob('tmp_*.png'):
        os.remove(filename)

    # Call solver and do the simulaton
    user_action = plot_u if animate else None
    u, x, t, cpu = solver_function(
        I, V, f, c, L, dt, C, T, user_action)

    # Make video files
    fps = 4  # frames per second
    codec2ext = dict(flv='flv', libx264='mp4', libvpx='webm',
                     libtheora='ogg')  # video formats
    filespec = 'tmp_%04d.png'
    movie_program = 'ffmpeg'  # or 'avconv'
    for codec in codec2ext:
        ext = codec2ext[codec]
        cmd = '%(movie_program)s -r %(fps)d -i %(filespec)s '\
              '-vcodec %(codec)s movie.%(ext)s' % vars()
        os.system(cmd)

    if tool == 'scitools':
        # Make an HTML play for showing the animation in a browser
        plt.movie('tmp_*.png', encoder='html', fps=fps,
                  output_file='movie.html')
    return cpu
Exemplo n.º 3
0
def viz(
    I, V, f, c, L, dt, C, T,  # PDE parameters
    umin, umax,               # Interval for u in plots
    animate=True,             # Simulation with animation?
    tool='matplotlib',        # 'matplotlib' or 'scitools'
    solver_function=solver,   # Function with numerical algorithm
    ):
    """
    Run solver, store and viz. u at each time level with all C values.
    """
    
    class PlotUst:
        def __init__(self):
            self.all_u = []
            self.all_u_for_all_C = []
            self.x_mesh = []   # need each mesh for final plots
        def __call__(self, u, x, t, n):
            """user_action function for solver."""
            self.all_u.append(u.copy())
            if t[n] == T: # i.e., whole time interv. done for this C
                self.x_mesh.append(x)
                self.all_u_for_all_C.append(self.all_u)
                self.all_u = []     # reset to empty list
                
                if len(self.all_u_for_all_C) == len(C):  # all C done
                    print 'Finished all C. Proceed with plots...'
                    # note: n will here be the last index in t[n]
                    for n_ in range(0, n+1):      # for each tn
                        plt.plot(self.x_mesh[0], 
                                 self.all_u_for_all_C[0][n_],
                                 axis=[0, L, umin, umax],
                                 title='Solutions for all \
                                        C at t=%f' % t[n_])
                        plt.hold('on')
                        
                        for j in range(1, len(C)):
                            # build plot at this tn with each 
                            # sol. from the different C values
                            plt.plot(self.x_mesh[j], 
                                     self.all_u_for_all_C[j][n_],
                                     axis=[0, L, umin, umax])
                        plt.xlabel('x'); plt.ylabel('u')
                        plt.hold('off')
                        plt.show()
                        # Let the init. cond. stay on the screen for
                        # 2 sec, else insert a pause of 0.2 s  
                        # between each plot                        
                        time.sleep(2) if t[n_] == 0 else \
                                                    time.sleep(0.2)                        
                        plt.savefig('tmp_%04d.png' % n_)  # for movie
                

    class PlotMatplotlib:
        def __init__(self):
            self.all_u = []
            self.all_u_for_all_C = []
        def __call__(self, u, x, t, n):
            """user_action function for solver."""
            self.all_u.append(u.copy())
            if t[n] == T: # i.e., whole time interv. done for this C
                self.all_u_for_all_C.append(self.all_u)
                self.all_u = []     # reset to empty list
                
                if len(self.all_u_for_all_C) == len(C):  # all C done
                    print 'Finished all C. Proceed with plots...'
                    plt.ion()
                    # note: n will here be the last index in t[n]
                    for n_ in range(0, n+1):      # for each tn
                        plt.plot(x, self.all_u_for_all_C[0][n_])
                        plt.axis([0, L, umin, umax])
                        plt.hold(True)
                        for j in range(1, len(C)):
                            # build plot at this tn with each 
                            # sol. from the different C values
                            plt.plot(x, self.all_u_for_all_C[j][n_])
                        plt.axis([0, L, umin, umax])
                        plt.xlabel('x'); plt.ylabel('u')
                        plt.title('Solutions for all \
                                   C at t=%f' % t[n_])
                        plt.hold(False)
                        plt.draw()
                        # Let the init. cond. stay on the screen for
                        # 2 sec, else insert a pause of 0.2 s  
                        # between each plot                        
                        time.sleep(2) if t[n_] == 0 else \
                                                    time.sleep(0.2)                        
                        plt.savefig('tmp_%04d.png' % n_)  # for movie

    if tool == 'matplotlib':
        import matplotlib.pyplot as plt
        plot_u = PlotMatplotlib()
    elif tool == 'scitools':
        import scitools.std as plt  # scitools.easyviz interface
        plot_u = PlotUst()
    import time, glob, os

    # Clean up old movie frames
    for filename in glob.glob('tmp_*.png'):
        os.remove(filename)

    # Call solver and do the simulaton
    user_action = plot_u if animate else None
    for C_value in C:
        print 'C_value --------------------------------- ', C_value
        u, x, t, cpu = solver_function(
                     I, V, f, c, L, dt, C_value, T, user_action)

    # Make video files
    fps = 4  # frames per second
    codec2ext = dict(flv='flv', libx264='mp4', libvpx='webm',
                     libtheora='ogg')  # video formats
    filespec = 'tmp_%04d.png'
    movie_program = 'ffmpeg'  # or 'avconv'
    for codec in codec2ext:
        ext = codec2ext[codec]
        cmd = '%(movie_program)s -r %(fps)d -i %(filespec)s '\
              '-vcodec %(codec)s movie.%(ext)s' % vars()
        os.system(cmd)

    if tool == 'scitools':
        # Make an HTML play for showing the animation in a browser
        plt.movie('tmp_*.png', encoder='html', fps=fps,
                  output_file='movie.html')
    return cpu
Exemplo n.º 4
0
def viz(
        I,
        V,
        f,
        c,
        L,
        dt,
        C,
        T,  # PDE parameters
        umin,
        umax,  # Interval for u in plots
        animate=True,  # Simulation with animation?
        tool='matplotlib',  # 'matplotlib' or 'scitools'
        solver_function=solver,  # Function with numerical algorithm
):
    """
    Run solver, store and viz. u at each time level with all C values.
    """
    class PlotUst:
        def __init__(self):
            self.all_u = []
            self.all_u_for_all_C = []
            self.x_mesh = []  # need each mesh for final plots

        def __call__(self, u, x, t, n):
            """user_action function for solver."""
            self.all_u.append(u.copy())
            if t[n] == T:  # i.e., whole time interv. done for this C
                self.x_mesh.append(x)
                self.all_u_for_all_C.append(self.all_u)
                self.all_u = []  # reset to empty list

                if len(self.all_u_for_all_C) == len(C):  # all C done
                    print 'Finished all C. Proceed with plots...'
                    # note: n will here be the last index in t[n]
                    for n_ in range(0, n + 1):  # for each tn
                        plt.plot(self.x_mesh[0],
                                 self.all_u_for_all_C[0][n_],
                                 axis=[0, L, umin, umax],
                                 title='Solutions for all \
                                        C at t=%f' % t[n_])
                        plt.hold('on')

                        for j in range(1, len(C)):
                            # build plot at this tn with each
                            # sol. from the different C values
                            plt.plot(self.x_mesh[j],
                                     self.all_u_for_all_C[j][n_],
                                     axis=[0, L, umin, umax])
                        plt.xlabel('x')
                        plt.ylabel('u')
                        plt.hold('off')
                        plt.show()
                        # Let the init. cond. stay on the screen for
                        # 2 sec, else insert a pause of 0.2 s
                        # between each plot
                        time.sleep(2) if t[n_] == 0 else \
                                                    time.sleep(0.2)
                        plt.savefig('tmp_%04d.png' % n_)  # for movie

    class PlotMatplotlib:
        def __init__(self):
            self.all_u = []
            self.all_u_for_all_C = []

        def __call__(self, u, x, t, n):
            """user_action function for solver."""
            self.all_u.append(u.copy())
            if t[n] == T:  # i.e., whole time interv. done for this C
                self.all_u_for_all_C.append(self.all_u)
                self.all_u = []  # reset to empty list

                if len(self.all_u_for_all_C) == len(C):  # all C done
                    print 'Finished all C. Proceed with plots...'
                    plt.ion()
                    # note: n will here be the last index in t[n]
                    for n_ in range(0, n + 1):  # for each tn
                        plt.plot(x, self.all_u_for_all_C[0][n_])
                        plt.axis([0, L, umin, umax])
                        plt.hold(True)
                        for j in range(1, len(C)):
                            # build plot at this tn with each
                            # sol. from the different C values
                            plt.plot(x, self.all_u_for_all_C[j][n_])
                        plt.axis([0, L, umin, umax])
                        plt.xlabel('x')
                        plt.ylabel('u')
                        plt.title('Solutions for all \
                                   C at t=%f' % t[n_])
                        plt.hold(False)
                        plt.draw()
                        # Let the init. cond. stay on the screen for
                        # 2 sec, else insert a pause of 0.2 s
                        # between each plot
                        time.sleep(2) if t[n_] == 0 else \
                                                    time.sleep(0.2)
                        plt.savefig('tmp_%04d.png' % n_)  # for movie

    if tool == 'matplotlib':
        import matplotlib.pyplot as plt
        plot_u = PlotMatplotlib()
    elif tool == 'scitools':
        import scitools.std as plt  # scitools.easyviz interface
        plot_u = PlotUst()
    import time, glob, os

    # Clean up old movie frames
    for filename in glob.glob('tmp_*.png'):
        os.remove(filename)

    # Call solver and do the simulaton
    user_action = plot_u if animate else None
    for C_value in C:
        print 'C_value --------------------------------- ', C_value
        u, x, t, cpu = solver_function(I, V, f, c, L, dt, C_value, T,
                                       user_action)

    # Make video files
    fps = 4  # frames per second
    codec2ext = dict(flv='flv', libx264='mp4', libvpx='webm',
                     libtheora='ogg')  # video formats
    filespec = 'tmp_%04d.png'
    movie_program = 'ffmpeg'  # or 'avconv'
    for codec in codec2ext:
        ext = codec2ext[codec]
        cmd = '%(movie_program)s -r %(fps)d -i %(filespec)s '\
              '-vcodec %(codec)s movie.%(ext)s' % vars()
        os.system(cmd)

    if tool == 'scitools':
        # Make an HTML play for showing the animation in a browser
        plt.movie('tmp_*.png',
                  encoder='html',
                  fps=fps,
                  output_file='movie.html')
    return cpu
Exemplo n.º 5
0
def viz(
    I,
    V,
    f,
    c,
    L,
    dt,
    C,
    T,  # PDE parameters
    umin,
    umax,  # Interval for u in plots
    animate=True,  # Simulation with animation?
    tool="matplotlib",  # 'matplotlib' or 'scitools'
    solver_function=solver,  # Function with numerical algorithm
):
    """Run solver and visualize u at each time level."""

    def plot_u_st(u, x, t, n):
        """user_action function for solver."""
        plt.plot(x, u, "r-", xlabel="x", ylabel="u", axis=[0, L, umin, umax], title="t=%f" % t[n], show=True)
        # Let the initial condition stay on the screen for 2
        # seconds, else insert a pause of 0.2 s between each plot
        time.sleep(2) if t[n] == 0 else time.sleep(0.2)
        plt.savefig("frame_%04d.png" % n)  # for movie making

    class PlotMatplotlib:
        def __call__(self, u, x, t, n):
            """user_action function for solver."""
            if n == 0:
                plt.ion()
                self.lines = plt.plot(x, u, "r-")
                plt.xlabel("x")
                plt.ylabel("u")
                plt.axis([0, L, umin, umax])
                plt.legend(["t=%f" % t[n]], loc="lower left")
            else:
                self.lines[0].set_ydata(u)
                plt.legend(["t=%f" % t[n]], loc="lower left")
                plt.draw()
            time.sleep(2) if t[n] == 0 else time.sleep(0.2)
            plt.savefig("tmp_%04d.png" % n)  # for movie making

    if tool == "matplotlib":
        import matplotlib.pyplot as plt

        plot_u = PlotMatplotlib()
    elif tool == "scitools":
        import scitools.std as plt  # scitools.easyviz interface

        plot_u = plot_u_st
    import time, glob, os

    # Clean up old movie frames
    for filename in glob.glob("tmp_*.png"):
        os.remove(filename)

    # Call solver and do the simulaton
    user_action = plot_u if animate else None
    u, x, t, cpu = solver_function(I, V, f, c, L, dt, C, T, user_action)

    # Make video files
    fps = 4  # frames per second
    codec2ext = dict(flv="flv", libx264="mp4", libvpx="webm", libtheora="ogg")  # video formats
    filespec = "tmp_%04d.png"
    movie_program = "ffmpeg"  # or 'avconv'
    for codec in codec2ext:
        ext = codec2ext[codec]
        cmd = "%(movie_program)s -r %(fps)d -i %(filespec)s " "-vcodec %(codec)s movie.%(ext)s" % vars()
        os.system(cmd)

    if tool == "scitools":
        # Make an HTML play for showing the animation in a browser
        plt.movie("tmp_*.png", encoder="html", fps=fps, output_file="movie.html")
    return cpu
Exemplo n.º 6
0
def viz(
    I, V, f, c, l, tau, gamma, T,
    umin, umax,
    animate=True,
    tool='matplotlib',
    solver_function=solver,
    ):

    all_u = []

    def plot_u_st(u, x, t, n):
        plt.plot(x, u, 'r-',
                 xlabel='x', ylabel='u',
                 axis=[0, l, umin, umax],
                 title='t=%f' % t[n], show=True)
        all_u.append(u)
        time.sleep(0.01) if t[n] == 0 else time.sleep(0.01)
        plt.savefig('tmp_%04d.png' % n)

    class PlotMatplotlib:
        def __call__(self, u, x, t, n):
            if n == 0:
                plt.ion()
                self.lines = plt.plot(x, u, 'r-')
                plt.xlabel('x');  plt.ylabel('u')
                plt.axis([0, l, umin, umax])
                plt.legend(['t=%f' % t[n]], loc='lower left')
            else:
                self.lines[0].set_ydata(u)
                plt.legend(['t=%f' % t[n]], loc='lower left')
                plt.draw()
            time.sleep(2) if t[n] == 0 else time.sleep(0.2)
            plt.savefig('tmp_%04d.png' % n)

    if tool == 'matplotlib':
        import matplotlib.pyplot as plt
        plot_u = PlotMatplotlib()
    elif tool == 'scitools':
        import scitools.std as plt
        plot_u = plot_u_st
    import time, glob, os


    for filename in glob.glob('tmp_*.png'):
        os.remove(filename)

    user_action = plot_u if animate else None
    u, x, t, cpu = solver_function(
        I, V, f, c, l, tau, gamma, T, user_action)

    fps = 4
    codec2ext = dict(flv='flv', libx264='mp4', libvpx='webm',
                     libtheora='ogg')
    filespec = 'tmp_%04d.png'
    movie_program = 'ffmpeg'
    for codec in codec2ext:
        ext = codec2ext[codec]
        cmd = '%(movie_program)s -r %(fps)d -i %(filespec)s '\
              '-vcodec %(codec)s movie.%(ext)s' % vars()
        os.system(cmd)

    if tool == 'scitools':
        plt.movie('tmp_*.png', encoder='html', fps=fps,
                  output_file='movie.html')
    return cpu, all_u