Exemplo n.º 1
0
def line_plot():
    """Make a line plot of T along the vertical direction."""
    if T.ufl_element().degree() != 1:
        T2 = interpolate(T, FunctionSpace(mesh, 'Lagrange', 1))
    else:
        T2 = T
    T_box = scitools.BoxField.dolfin_function2BoxField(
            T2, mesh, divisions, uniform_mesh=True)
    #T_box = scitools.BoxField.update_from_dolfin_array(
    #        T.vector().array(), T_box)
    coor, Tval, fixed, snapped = \
            T_box.gridline(start_pt, direction=d-1)

    # Use just one ev.plot command, not hold('on') and two ev.plot
    # etc for smooth movie on the screen
    if kappa_0 == kappa_1:  # analytical solution?
        ev.plot(coor, Tval, 'r-',
                coor, T_exact(coor), 'b-',
                axis=[-D, 0, T_R-T_A, T_R+T_A],
                xlabel='depth', ylabel='temperature',
                legend=['numerical', 'exact, const kappa=%g' % kappa_0],
                legend_loc='upper left',
                title='t=%.4f' % t)
    else:
        ev.plot(coor, Tval, 'r-',
                axis=[-D, 0, T_R-T_A, T_R+T_A],
                xlabel='depth', ylabel='temperature',
                title='t=%.4f' % t)

    ev.savefig('tmp_%04d.png' % counter)
    time.sleep(0.1)
Exemplo n.º 2
0
def line_plot():
    """Make a line plot of T along the vertical direction."""
    if T.ufl_element().degree() != 1:
        T2 = interpolate(T, FunctionSpace(mesh, 'Lagrange', 1))
    else:
        T2 = T
    T_box = scitools.BoxField.dolfin_function2BoxField(
            T2, mesh, divisions, uniform_mesh=True)
    #T_box = scitools.BoxField.update_from_dolfin_array(
    #        T.vector().array(), T_box)
    coor, Tval, fixed, snapped = \
            T_box.gridline(start_pt, direction=d-1)

    # Use just one ev.plot command, not hold('on') and two ev.plot
    # etc for smooth movie on the screen
    if kappa_0 == kappa_1:  # analytical solution?
        ev.plot(coor, Tval, 'r-',
                coor, T_exact(coor), 'b-',
                axis=[-D, 0, T_R-T_A, T_R+T_A],
                xlabel='depth', ylabel='temperature',
                legend=['numerical', 'exact, const kappa=%g' % kappa_0],
                legend_loc='upper left',
                title='t=%.4f' % t)
    else:
        ev.plot(coor, Tval, 'r-',
                axis=[-D, 0, T_R-T_A, T_R+T_A],
                xlabel='depth', ylabel='temperature',
                title='t=%.4f' % t)

    ev.savefig('tmp_%04d.png' % counter)
    time.sleep(0.1)
Exemplo n.º 3
0
def plot(x, y, f, title, plt=None):
    if plt is None:
        #import matplotlib.pylab as plt
        import scitools.easyviz as plt
    plt.figure()
    plt.plot(x, y, x, f)
    plt.legend(["approximation", "exact"])
    plt.title(title)
    return plt
Exemplo n.º 4
0
def plot(x, y, f, title, plt=None):
    if plt is None:
        # import matplotlib.pylab as plt
        import scitools.easyviz as plt
    plt.figure()
    plt.plot(x, y, x, f)
    plt.legend(["approximation", "exact"])
    plt.title(title)
    return plt
Exemplo n.º 5
0
 def plotConvergence(cls, scale, error4scale, title, legend, regression = False):
     p.figure()
     p.loglog(scale, error4scale, "k-d", xlabel = "samples", ylabel = "error",)  # semilogy
     p.title(title)
     p.legend(legend)
     if regression and len(error4scale) > 1:
         p.hold('on')
         lineSpace = np.array((scale[0], scale[-1]))
         slope, intercept, r_value, p_value, std_err = stats.linregress(scale, np.log10(error4scale))
         line = np.power(10, slope * lineSpace + intercept)
         p.plot(lineSpace, line, "k:", legend = "{slope:.2}x + c" \
                .format(slope = slope))
     p.hardcopy(cls.folder + title + "  " + t.strftime('%Y-%m-%d_%H-%M') + ".pdf")
Exemplo n.º 6
0
    def plotConvergenceMean(cls, approx, exact, regression = False):
        nApprox = approx.shape[0]
        errorMean = np.empty(nApprox)
        for curSampleN in range(0, nApprox):
            errorMean[curSampleN] = cls.computeError(np.mean(approx[:curSampleN + 1], 0), exact)

        p.figure()
        # lSpace = p.linspace(0, errorMean.size, errorMean.size)
        lSpace = list(i * (len(exact) ** 2) for i in range(nApprox))
        p.loglog(lSpace, errorMean, "k-", xlabel = "samples", ylabel = "error",
                   legend = "E[(Q_M - E[Q])^2]^{1/2}")
        print("CompCost: {compCost:5.2e}\t Error: {error:5.4e}"
              .format(compCost = lSpace[-1], error = errorMean[-1]))
        if regression:
            p.hold('on')
            lineSpace = np.array((lSpace[0], lSpace[-1]))
            slope, intercept, r_value, p_value, std_err = stats.linregress(lSpace, np.log10(errorMean))
            line = np.power(10, slope * lineSpace + intercept)
            p.plot(lineSpace, line, "k:", legend = "{slope:.2}x + c" \
                   .format(slope = slope))
Exemplo n.º 7
0
        title='surf plot of u',
        savefig='tmp3.eps')
ev.figure()
ev.mesh(u_box.grid.coorv[X],
        u_box.grid.coorv[Y],
        u_box.values,
        title='mesh plot of u',
        savefig='tmp4.eps')

# Extract and plot u along the line y=0.5
start = (0, 0.5)
x, uval, y_fixed, snapped = u_box.gridline(start, direction=X)
if snapped:
    print 'Line at %s adjusted (snapped) to y=%g' % (start, y_fixed)
ev.figure()
ev.plot(x, uval, 'r-', title='Solution', legend='finite element solution')

# Plot the numerical (projected) and exact flux along this line
ev.figure()
flux2_x = flux_x if flux_x.ufl_element().degree() == 1 else \
          interpolate(flux_x, FunctionSpace(mesh, 'Lagrange', 1))
flux_x_box = scitools.BoxField.dolfin_function2BoxField(flux2_x,
                                                        mesh, (nx, ny),
                                                        uniform_mesh=True)
x, fluxval, y_fixed, snapped = \
        flux_x_box.gridline(start, direction=0)
y = y_fixed
flux_x_exact = -(x + y) * 2 * x
ev.plot(x,
        fluxval,
        'r-',
Exemplo n.º 8
0
import numpy as np
import sys
import scitools.easyviz as plt

n = int(sys.argv[1])

T = 102.2
t = np.linspace(0, T, n + 1)
dt = t[1] - t[0]

I = 2.0
V = 3.0
w = 1
#w = w_val*(1 - (1/24.)*w_val*w_val*dt*dt)
u = np.zeros(n + 1)
wdt2 = dt * w * dt * w

u[0] = I
u[1] = u[0] * (1 - 0.5 * wdt2) + dt * V

for n in range(1, n):
    u[n + 1] = 2 * u[n] - u[n - 1] - wdt2 * u[n]

u_e = np.zeros(n + 2)
u_e[:] = (V / np.sqrt(w)) * np.sin(np.sqrt(w) * t[:]) + I * np.cos(
    np.sqrt(w) * t[:])

plt.plot(t, u, t, u_e, legend=["discrete", "exact"])
raw_input("press enter")
Exemplo n.º 9
0
           clabels='on')
ev.figure()
ev.surf(u_box.grid.coorv[X], u_box.grid.coorv[Y], u_box.values,
        shading='interp', colorbar='on',
        title='surf plot of u', savefig='tmp3.eps')
ev.figure()
ev.mesh(u_box.grid.coorv[X], u_box.grid.coorv[Y], u_box.values,
        title='mesh plot of u', savefig='tmp4.eps')

# Extract and plot u along the line y=0.5
start = (0,0.5)
x, uval, y_fixed, snapped = u_box.gridline(start, direction=X)
if snapped:
    print('Line at %s adjusted (snapped) to y=%g' % (start, y_fixed))
ev.figure()
ev.plot(x, uval, 'r-', title='Solution',
        legend='finite element solution')

# Plot the numerical (projected) and exact flux along this line
ev.figure()
flux2_x = flux_x if flux_x.ufl_element().degree() == 1 else \
          interpolate(flux_x, FunctionSpace(mesh, 'Lagrange', 1))
flux_x_box = scitools.BoxField.dolfin_function2BoxField(
        flux2_x, mesh, (nx,ny), uniform_mesh=True)
x, fluxval, y_fixed, snapped = \
        flux_x_box.gridline(start, direction=0)
y = y_fixed
flux_x_exact = -(x + y)*2*x
ev.plot(x, fluxval, 'r-',
        x, flux_x_exact, 'b-',
        legend=('numerical (projected) flux', 'exact flux'),
        title='Flux in x-direction (at y=%g)' % y_fixed,
Exemplo n.º 10
0
import numpy as np
import sys
import scitools.easyviz as plt

n = int(sys.argv[1])


T = 102.2
t = np.linspace(0,T,n+1)
dt  = t[1] - t[0]

I = 2.0
V = 3.0
w = 1
#w = w_val*(1 - (1/24.)*w_val*w_val*dt*dt)
u = np.zeros(n+1)
wdt2 = dt*w*dt*w

u[0] = I
u[1] = u[0]*(1 - 0.5*wdt2) + dt*V

for n in range(1,n):
    u[n+1] = 2*u[n] - u[n-1] - wdt2*u[n]

u_e    = np.zeros(n+2)
u_e[:] = (V/np.sqrt(w))*np.sin(np.sqrt(w)*t[:])+I*np.cos(np.sqrt(w)*t[:])

plt.plot(t,u,t,u_e,legend=["discrete","exact"])
raw_input("press enter")
Exemplo n.º 11
0
    
    n = 0
    while n<N and T[n]>=temp_after_death-0.0001 :
        T[n+1] = (T[n] - k*dt*((1-theta)*T[n] - T_s))/(1.0 + theta*dt*k)
        n = n + 1

    t = linspace(0,t_end,N+1)
    return T,t
    
    
def exact_sol(T0,k,T_s,t_end,dt):
    N = int(round(t_end/float(dt)))
    t = linspace(0,t_end,N+1)
    exact = (T0-T_s)*exp(-t*k) + T_s
    return exact,t
    

if __name__ == "__main__":
    T,t = cooling(37.5, 0.1, 14.0, 60.0, 5, 0.5)
    plot(t,T,"r--o")
    T_exact,t = exact_sol(37.5, 0.1, 14.0, 60.0, 5)
    #figure()
    hold("on")
    plot(t,T_exact)
    legend(["discrete sol","exact sol"])

    print "maximum error :" ,max(abs(T-T_exact))
    raw_input("press enter")