Пример #1
0
def test_radial_waves(plot=1, version='scalar'):
    L = 1
    c = 1

    def I(r):
        return 0

    def f(r, t):
        return 8 * exp(-100 * r) * sin(100 * t)

    def U_L_func(t):
        return 0

    n = 500
    dr = L / float(n)
    dt = 0.5 * dr
    tstop = 2
    if plot:
        g = graph(program='Gnuplot')
        g.configure(ymin=-0.055, ymax=0.055)
    else:
        g = None
    dt, r, v, cpu = solver_r(I,
                             f,
                             c,
                             U_L_func,
                             L,
                             n,
                             dt,
                             tstop,
                             g,
                             user_action=None,
                             version=version)
    print 'CPU time', version, 'version:', cpu
    print 'v[0] final time:', v[-1][0]
Пример #2
0
def test_solver2(N, plot=True, version='scalar'):
    s = StoreSolution()
    s.main(N, version)
    print 'CPU time:', s.cpu
    if len(s.x) < 10: print s.solutions
    if plot:
        from CurveViz import graph
        g = graph(program='Gnuplot', coor=s.x, ymax=1, ymin=-1)
        for s in s.solutions:
            g.plotcurve(s)
Пример #3
0
def test_solver2(N, plot=True, version='scalar'):
    s = StoreSolution()
    s.main(N, version)
    print 'CPU time:', s.cpu
    if len(s.x) < 10: print s.solutions
    if plot:
        from CurveViz import graph
        g = graph(program='Gnuplot', coor=s.x, ymax=1, ymin=-1)
        for s in s.solutions:
            g.plotcurve(s)
Пример #4
0
def test_solver_plug(plot=1, version='scalar', n=50):
    L = 1
    c = 1
    tstop = 2

    def I(x):
        """Plug profile as initial condition."""
        if abs(x - L / 2.0) > 0.1:
            return 0
        else:
            return 1

    def f(x, t):
        return 0

    def U_0(t):
        return 0

    def U_L(t):
        return 0

    def action(u, x, t):
        pass
        #print t, u

    if plot:
        g = graph(program='Gnuplot')
        g.configure(ymin=-1.1, ymax=1.1)
    else:
        g = None

    import time
    t0 = time.clock()
    solutions, x, dt, cpu = visualizer(I,
                                       f,
                                       c,
                                       U_0,
                                       U_L,
                                       L,
                                       n,
                                       0,
                                       tstop,
                                       user_action=None,
                                       version=version,
                                       graphics=g)

    print 'CPU time: %s version =' % version, cpu
    # check that first and last (if tstop=2) are equal:
    if not allclose(solutions[0], solutions[-1], atol=1.0E-10, rtol=1.0E-12):
        print 'error in computations'
    else:
        print 'correct solution'
Пример #5
0
    def __init__(self, solver, plot=0, **graphics_kwargs):
        """Store solver instance. Initialize graphics tool."""
        self.s = solver
        self.solutions = []  # store self.up at each time level

        if not 'program' in graphics_kwargs:
            graphics_kwargs['program'] = 'Gnuplot'
        self._plot = plot
        if self._plot:
            self.g = graph(**graphics_kwargs)
            # could use scitools.easyviz.blt_ instead...
        else:
            self.g = None
Пример #6
0
    def __init__(self, solver, plot=0, **graphics_kwargs):
        """Store solver instance. Initialize graphics tool."""
        self.s = solver
        self.solutions = []  # store self.up at each time level

        if not 'program' in graphics_kwargs:
            graphics_kwargs['program'] = 'Gnuplot'
        self._plot = plot
        if self._plot:
            self.g = graph(**graphics_kwargs)
            # could use scitools.easyviz.blt_ instead...
        else:
            self.g = None
Пример #7
0
def test2(program, parent=None):
    from CurveViz import graph
    t = linspace(-4, 4, 81)
    g = graph(coor=t, ymin=-1.2, ymax=1.2, xlabel='t',
              program=program, parent_frame=parent)
    # make animations:
    for p in linspace(0, 3, 13):
        u = exp(-p*p)*(sin(t) + 0.2*sin(5*t))
        g.plotcurve((t,u), legend='u(t); p=%4.2f' % p)

    # plot several curves in one plot:
    t = linspace(0, 10, 101)
    g.configure(coor=t) # change coordinate vector
    g.configure(ymin=-4, ymax=4, ylabel='u')
    u1 = sin(t)*t; u2 = sin(t)*sqrt(t)
    g.plotcurves([((t,u1),'t ampl.'),((t,u2),'sqrt(t) ampl.')], ps=1)
Пример #8
0
def test2(program, parent=None):
    from CurveViz import graph
    t = linspace(-4, 4, 81)
    g = graph(coor=t, ymin=-1.2, ymax=1.2, xlabel='t',
              program=program, parent_frame=parent)
    # make animations:
    for p in linspace(0, 3, 13):
        u = exp(-p*p)*(sin(t) + 0.2*sin(5*t))
        g.plotcurve((t,u), legend='u(t); p=%4.2f' % p)

    # plot several curves in one plot:
    t = linspace(0, 10, 101)
    g.configure(coor=t) # change coordinate vector
    g.configure(ymin=-4, ymax=4, ylabel='u')
    u1 = sin(t)*t; u2 = sin(t)*sqrt(t)
    g.plotcurves([((t,u1),'t ampl.'),((t,u2),'sqrt(t) ampl.')], ps=1)
Пример #9
0
def test_solver_plug(plot=1, version='scalar', n=50):
    L = 1
    c = 1
    tstop = 2
    def I(x):
        """Plug profile as initial condition."""
        if abs(x-L/2.0) > 0.1:
            return 0
        else:
            return 1

    def f(x,t):
        return 0

    def U_0(t):
        return 0

    def U_L(t):
        return 0

    def action(u, x, t):
        pass
        #print t, u
        
    if plot:
        g = graph(program='Gnuplot')
        g.configure(ymin=-1.1, ymax=1.1)
    else:
        g = None

    import time
    t0 = time.clock()
    solutions, x, dt, cpu = visualizer(I, f, c, U_0, U_L, L,
    n, 0, tstop, user_action=None, version=version, graphics=g)

    print 'CPU time: %s version =' % version, cpu
    # check that first and last (if tstop=2) are equal:
    if not allclose(solutions[0], solutions[-1],
                    atol=1.0E-10, rtol=1.0E-12):
        print 'error in computations'
    else:
        print 'correct solution'
Пример #10
0
def test_radial_waves(plot=1, version='scalar'):
    L = 1
    c = 1
    def I(r): return 0
    def f(r,t):
        return 8*exp(-100*r)*sin(100*t)
    def U_L_func(t):
        return 0

    n = 500
    dr = L/float(n)
    dt = 0.5*dr
    tstop = 2
    if plot:
        g = graph(program='Gnuplot')
        g.configure(ymin=-0.055, ymax=0.055)
    else:
        g = None
    dt, r, v, cpu = solver_r(I, f, c, U_L_func, L, n, dt, tstop,
                             g, user_action=None, version=version)
    print 'CPU time', version, 'version:', cpu
    print 'v[0] final time:', v[-1][0]
Пример #11
0
def solver_r_v1(I, f, c, U_L, L, n, dt, tstop,
                version='scalar', plot=True, umin=0, umax=1):
    """
    1D wave equation as in wave1D_func1.solver, but the present
    function models spherical waves. The physical solution is v=u/r.
    The boundary condition at r=0 is u=0 because of symmetry
    so u(L)=U_L is the only free boundary condition.

    This version winds numerical solution and visualization
    together. An alternative is the solver_r function.
    """
    import time
    t0 = time.clock()
    
    dr = L/float(n)
    r = linspace(0, L, n+1)  # grid points in r dir
    if dt <= 0:  dt = dr/float(c)  # max time step? stability limit
    C2 = (c*dt/dr)**2        # help variable in the scheme
    dt2 = dt*dt

    up = zeros(n+1)   # solution array
    u  = up.copy()    # solution at t-dt
    um = up.copy()    # solution at t-2*dt
    v  = up.copy()    # physical solution (up/r)
    
    # set initial condition (pointwise - allows straight if-tests):
    for i in iseq(0,n):
        u[i] = I(r[i])
    for i in iseq(1,n-1):
        um[i] = u[i] + 0.5*C2*(u[i-1] - 2*u[i] + u[i+1]) + \
                dt2*f(x[i], t)
    um[0] = 0;  um[n] = U_L(t+dt)

    if plot:
        g = graph(program='Gnuplot')
        g.configure(ymin=umin, ymax=umax, coor=r)
        v[1:] = u[1:]/r[1:]
        v[0] = v[1]  # from the b.c. dv/dr=0 at r=0
        g.plotcurve(v, legend='u(r,t=0)')

    solutions = [u.copy()]  # hold all u arrays, start with init cond.
    t = 0.0
    while t <= tstop:
        t_old = t;  t += dt
        # update all inner points:
        if version == 'scalar':
            for i in iseq(start=1, stop=n-1):
                up[i] = - um[i] + 2*u[i] + \
                        C2*(u[i-1] - 2*u[i] + u[i+1]) + \
                        dt2*f(r[i], t_old)
        elif version == 'vectorized':
            up[1:n] = - um[1:n] + 2*u[1:n] + \
                     C2*(u[0:n-1] - 2*u[1:n] + u[2:n+1]) + \
                     dt2*f(r[1:n], t_old)
            
        # insert boundary conditions:
        up[0] = 0;  up[n] = U_L(t)
        # physical solution:
        v[1:] = up[1:]/r[1:]; v[0] = v[1] 
        # update data structures for next step
        um = u.copy(); u = up.copy() 
        # efficiency improvement of the update:
        # tmp = um; um = u; u = up; up = tmp
        if plot: g.plotcurve(v, legend='v(r,t=%9.4E)' % t)
        solutions.append(v.copy())  # save a copy!

    t1 = time.clock()
    return dt, r, solutions, t1-t0
Пример #12
0
def solver_r_v1(I,
                f,
                c,
                U_L,
                L,
                n,
                dt,
                tstop,
                version='scalar',
                plot=True,
                umin=0,
                umax=1):
    """
    1D wave equation as in wave1D_func1.solver, but the present
    function models spherical waves. The physical solution is v=u/r.
    The boundary condition at r=0 is u=0 because of symmetry
    so u(L)=U_L is the only free boundary condition.

    This version winds numerical solution and visualization
    together. An alternative is the solver_r function.
    """
    import time
    t0 = time.clock()

    dr = L / float(n)
    r = linspace(0, L, n + 1)  # grid points in r dir
    if dt <= 0: dt = dr / float(c)  # max time step? stability limit
    C2 = (c * dt / dr)**2  # help variable in the scheme
    dt2 = dt * dt

    up = zeros(n + 1)  # solution array
    u = up.copy()  # solution at t-dt
    um = up.copy()  # solution at t-2*dt
    v = up.copy()  # physical solution (up/r)

    # set initial condition (pointwise - allows straight if-tests):
    for i in iseq(0, n):
        u[i] = I(r[i])
    for i in iseq(1, n - 1):
        um[i] = u[i] + 0.5*C2*(u[i-1] - 2*u[i] + u[i+1]) + \
                dt2*f(x[i], t)
    um[0] = 0
    um[n] = U_L(t + dt)

    if plot:
        g = graph(program='Gnuplot')
        g.configure(ymin=umin, ymax=umax, coor=r)
        v[1:] = u[1:] / r[1:]
        v[0] = v[1]  # from the b.c. dv/dr=0 at r=0
        g.plotcurve(v, legend='u(r,t=0)')

    solutions = [u.copy()]  # hold all u arrays, start with init cond.
    t = 0.0
    while t <= tstop:
        t_old = t
        t += dt
        # update all inner points:
        if version == 'scalar':
            for i in iseq(start=1, stop=n - 1):
                up[i] = - um[i] + 2*u[i] + \
                        C2*(u[i-1] - 2*u[i] + u[i+1]) + \
                        dt2*f(r[i], t_old)
        elif version == 'vectorized':
            up[1:n] = - um[1:n] + 2*u[1:n] + \
                     C2*(u[0:n-1] - 2*u[1:n] + u[2:n+1]) + \
                     dt2*f(r[1:n], t_old)

        # insert boundary conditions:
        up[0] = 0
        up[n] = U_L(t)
        # physical solution:
        v[1:] = up[1:] / r[1:]
        v[0] = v[1]
        # update data structures for next step
        um = u.copy()
        u = up.copy()
        # efficiency improvement of the update:
        # tmp = um; um = u; u = up; up = tmp
        if plot: g.plotcurve(v, legend='v(r,t=%9.4E)' % t)
        solutions.append(v.copy())  # save a copy!

    t1 = time.clock()
    return dt, r, solutions, t1 - t0