def run_test_o(K,source,u_fabric,n):
    T = random_perturbation(0.5/(n))
    mesh = Mesh(n,5*n,T)

    num_unknowns = mesh.cell_centers.shape[0]*mesh.cell_centers.shape[1]
    matrix = lil_matrix((num_unknowns,num_unknowns))
    flux_matrix = {'x': lil_matrix((num_unknowns,num_unknowns)),'y':lil_matrix((num_unknowns,num_unknowns))}
    A,fx,fy = compute_matrix_o(mesh,K,matrix,flux_matrix)
    A = csr_matrix(A,dtype=float)
    f = compute_vector(mesh,source,sym.lambdify([x,y],u_fabric))
    u = spsolve(A,f)

    fx = csr_matrix(fx,dtype=float)
    fy = csr_matrix(fy,dtype=float)
    l2err_flux,maxerr_flux = compute_flux_error(fx.dot(u),fy.dot(u),u_fabric,mesh)
    l2err,maxerr = compute_error(mesh,u,sym.lambdify([x,y],u_fabric))
    return (l2err,maxerr,l2err_flux,maxerr_flux)
Beispiel #2
0
 def L_scheme(u_j, u_n, TOL, L, K, tau, F):
     A = np.zeros((mesh.num_unknowns, mesh.num_unknowns))
     rhs = L * mass @ u_j + mass @ theta(u_n) - mass @ theta(u_j) + tau * F
     permability = K(
         np.reshape(
             u_j, (mesh.cell_centers.shape[0], mesh.cell_centers.shape[1]),
             order='F'))
     lhs = tau * stiffness(mesh, np.eye(2), A,
                           k_global=permability) + L * mass
     u_j_n = np.linalg.solve(lhs, rhs)
     # print(np.linalg.norm(u_j_n-u_j))
     if np.linalg.norm(u_j_n - u_j) > TOL + TOL * np.linalg.norm(u_j_n):
         return L_scheme(
             u_j_n, u_n, TOL, L, K, tau,
             compute_vector(mesh, lambda x, y: f(x, y, t),
                            lambda x, y: u_exact(x, y, t)))
     else:
         return u_j_n
def run_test_l(K,source,u_fabric,n):
    T = random_perturbation(0.5/(n))
    mesh = Mesh(n,5*n,T)
    start = time.time()
    num_unknowns = mesh.cell_centers.shape[0]*mesh.cell_centers.shape[1]
    matrix = lil_matrix((num_unknowns,num_unknowns))
    flux_matrix = {'x': lil_matrix((num_unknowns,num_unknowns)),'y':lil_matrix((num_unknowns,num_unknowns))}
    A,fx,fy = compute_matrix(mesh,K,matrix,flux_matrix)
    end = time.time()
    print('matrix assembly l-scheme: ',end-start)
    f = compute_vector(mesh,source,sym.lambdify([x,y],u_fabric))
    A  = csr_matrix(A,dtype = float)
    start = time.time()

    u = spsolve(A,f)
    end = time.time()
    print('linear solver l-scheme: ',end-start)

    fx = csr_matrix(fx,dtype=float)
    fy = csr_matrix(fy,dtype=float)
    l2err_flux,maxerr_flux = compute_flux_error(fx.dot(u),fy.dot(u),u_fabric,mesh)
    l2err,maxerr = compute_error(mesh,u,sym.lambdify([x,y],u_fabric))
    return (l2err,maxerr,l2err_flux,maxerr_flux)
Beispiel #4
0
def run_heat(timestep, num_nodes):
    #Construct the data given a solution
    K = np.array([[1, 0], [0, 1]])
    x = sym.Symbol('x')
    y = sym.Symbol('y')
    t = sym.Symbol('t')
    u_exact = -0.1 * sym.sin(math.pi * x) * sym.sin(math.pi * y) * t - 1
    # u_exact = -t*x*y*(1-x)*(1-y)
    f = sym.diff(u_exact, t) - divergence(gradient(u_exact, [x, y]), [x, y])
    f = sym.lambdify([x, y, t], f)
    u_exact = sym.lambdify([x, y, t], u_exact)

    #Define the mesh
    def random_perturbation(h, aspect):
        return lambda p: np.array([
            random.uniform(0, h) * random.choice([-1, 1]) + p[0] - 0.5 * p[1],
            (1 / aspect) * random.uniform(0, h) * random.choice([-1, 1]) + p[1]
        ])

    T = lambda p: np.array([p[0], p[1]])
    nx = num_nodes
    ny = num_nodes
    mesh = Mesh(nx, ny, T, ghostboundary=True)
    h = np.linalg.norm(mesh.nodes[1, 1, :] - mesh.nodes[0, 0, :])
    print('h ', h)

    #make mass and gravitation matrices
    mass = mass_matrix(mesh)

    def compute_error(mesh, u, u_fabric):
        cx = mesh.cell_centers.shape[1]
        cy = mesh.cell_centers.shape[0]
        u_fabric_vec = u.copy()
        volumes = u.copy()

        for i in range(cy):
            for j in range(cx):
                u_fabric_vec[mesh.meshToVec(i, j)] = u_fabric(
                    mesh.cell_centers[i, j, 0], mesh.cell_centers[i, j, 1])
                volumes[mesh.meshToVec(i, j)] = mesh.volumes[i, j]
        L2err = math.sqrt(
            np.square(u - u_fabric_vec).T @ volumes /
            (np.ones(volumes.shape).T @ volumes))
        maxerr = np.max(np.abs(u - u_fabric_vec))
        return (L2err, maxerr)

    #Time discretization
    time_partition = np.linspace(0, 1, math.ceil(1 / timestep))
    tau = time_partition[1] - time_partition[0]

    u = mesh.interpolate(lambda x, y: u_exact(x, y, 0))

    for t in time_partition[1:]:
        A = np.zeros((mesh.num_unknowns, mesh.num_unknowns))
        lhs = tau * stiffness(mesh, K, A) + mass
        rhs = tau * compute_vector(mesh, lambda x, y: f(x, y, t),
                                   lambda x, y: u_exact(x, y, t)) + mass @ u
        u = np.linalg.solve(lhs, rhs)
        # mesh.plot_vector(u,'numerical solution')
        # mesh.plot_funtion(lambda x,y:u_exact(x,y,t),'exact solution')
        # e = u - mesh.interpolate(lambda x,y: u_exact(x,y,t))
        # mesh.plot_vector(e,'error')
    return compute_error(mesh, u, lambda x, y: u_exact(x, y, 1))
Beispiel #5
0
def run_richards(num_nodes):

    #Construct the data given a solution
    K = np.array([[1, 0], [0, 1]])
    x = sym.Symbol('x')
    y = sym.Symbol('y')
    t = sym.Symbol('t')
    p = sym.Symbol('p')
    u_exact = -0.1 * sym.sin(math.pi * x) * sym.sin(math.pi * y) * t**2 - 1
    # u_exact = -(t)**2*(x*y*(1-x)*(1-y))**2 - 1
    # u_exact = (t**2*sym.cos(y*math.pi)*sym.cosh(x*math.pi))/12-1
    K = p**2
    theta = 1 / (1 - p)
    # theta = p
    f = sym.diff(theta.subs(p, u_exact), t) - divergence(
        gradient(u_exact, [x, y]), [x, y], K.subs(p, u_exact))
    # f = sym.diff(u_exact,t)-divergence(gradient(u_exact ,[x,y]),[x,y])

    f = sym.lambdify([x, y, t], f)
    u_exact = sym.lambdify([x, y, t], u_exact)

    #Define the mesh
    def random_perturbation(h, aspect):
        return lambda p: np.array([
            random.uniform(0, h) * random.choice([-1, 1]) + p[0] - 0.5 * p[1],
            (1 / aspect) * random.uniform(0, h) * random.choice([-1, 1]) + p[1]
        ])

    T = lambda p: np.array([p[0] + 0.5 * p[1], p[1]])
    nx = num_nodes
    ny = num_nodes

    mesh = Mesh(nx, ny, T, ghostboundary=True)

    h = mesh.max_h()
    timestep = h**2  #cfl condition
    print('h ', h)

    #make mass and gravitation matrices
    mass = mass_matrix(mesh, sparse=True)

    #gravitation = gravitation_matrix(mesh)

    def compute_error(mesh, u, u_fabric):
        cx = mesh.cell_centers.shape[1]
        cy = mesh.cell_centers.shape[0]
        u_fabric_vec = u.copy()
        volumes = u.copy()

        for i in range(cy):
            for j in range(cx):
                u_fabric_vec[mesh.meshToVec(i, j)] = u_fabric(
                    mesh.cell_centers[i, j, 0], mesh.cell_centers[i, j, 1])
                volumes[mesh.meshToVec(i, j)] = mesh.volumes[i, j]
        L2err = math.sqrt(
            np.square(u - u_fabric_vec).T @ volumes /
            (np.ones(volumes.shape).T @ volumes))
        maxerr = np.max(np.abs(u - u_fabric_vec))
        return (L2err, maxerr)

    #Time discretization
    time_partition = np.linspace(0, 1, math.ceil(1 / timestep))
    tau = time_partition[1] - time_partition[0]

    L = 0.5
    TOL = 0.000000005

    K = lambda x: x**2

    theta = lambda x: 1 / (1 - x)
    # theta = lambda x:x

    u = mesh.interpolate(lambda x, y: u_exact(x, y, 0))  #Initial condition

    def L_scheme(u_j, u_n, TOL, L, K, tau, F):
        A = np.zeros((mesh.num_unknowns, mesh.num_unknowns))
        rhs = L * mass @ u_j + mass @ theta(u_n) - mass @ theta(u_j) + tau * F
        permability = K(
            np.reshape(
                u_j, (mesh.cell_centers.shape[0], mesh.cell_centers.shape[1]),
                order='F'))
        lhs = tau * stiffness(mesh, np.eye(2), A,
                              k_global=permability) + L * mass
        u_j_n = np.linalg.solve(lhs, rhs)
        # print(np.linalg.norm(u_j_n-u_j))
        if np.linalg.norm(u_j_n - u_j) > TOL + TOL * np.linalg.norm(u_j_n):
            return L_scheme(
                u_j_n, u_n, TOL, L, K, tau,
                compute_vector(mesh, lambda x, y: f(x, y, t),
                               lambda x, y: u_exact(x, y, t)))
        else:
            return u_j_n

    # for t in time_partition[1:]:
    #     u =  L_scheme(u,u,TOL,L,K,tau,compute_vector(mesh,lambda x,y: f(x,y,t),lambda x,y:u_exact(x,y,t)))
    #     mesh.plot_vector(u,'numerical solution')
    #     mesh.plot_funtion(lambda x,y: u_exact(x,y,t),'exact solution')
    #     e = u - mesh.interpolate(lambda x,y: u_exact(x,y,t))
    #     mesh.plot_vector(e,'error')
    #     print(t)
    #     (l2,m) = compute_error(mesh,u,lambda x,y:u_exact(x,y,t))
    #     print(l2)
    #     print(m)
    for t in time_partition[1:]:
        source = compute_vector(mesh, lambda x, y: f(x, y, t),
                                lambda x, y: u_exact(x, y, t))
        A = lil_matrix((mesh.num_unknowns, mesh.num_unknowns))
        permability = K(
            np.reshape(
                u.copy(),
                (mesh.cell_centers.shape[0], mesh.cell_centers.shape[1]),
                order='F'))
        # permability = np.ones((mesh.cell_centers.shape[0],mesh.cell_centers.shape[1]),order='F')
        A = stiffness(mesh, np.eye(2), A, k_global=permability)
        A = csr_matrix(A, dtype=float)
        #A = np.zeros((mesh.num_unknowns,mesh.num_unknowns))
        rhs = tau * source + L * mass @ u
        lhs = tau * A + L * mass
        u_n_i = spsolve(lhs, rhs)
        # u_n_i = np.reshape(u_n_i,(mesh.cell_centers.shape[0],mesh.cell_centers.shape[1]))
        # u_n_i = np.ravel(u_n_i,order='F')
        u_n = u
        while np.linalg.norm(u_n_i - u_n) > TOL + TOL * np.linalg.norm(u_n):
            u_n = u_n_i
            A = lil_matrix((mesh.num_unknowns, mesh.num_unknowns))
            permability = K(
                np.reshape(
                    u_n.copy(),
                    (mesh.cell_centers.shape[0], mesh.cell_centers.shape[1]),
                    order='F'))
            # permability = np.ones((mesh.cell_centers.shape[0],mesh.cell_centers.shape[1]),order='F')

            A = stiffness(mesh, np.eye(2), A, k_global=permability)
            A = csr_matrix(A, dtype=float)
            lhs = tau * A + L * mass
            rhs = -mass @ theta(u_n) + mass @ theta(
                u) + tau * source + L * mass @ u_n
            u_n_i = spsolve(lhs, rhs)
            # u_n_i = np.reshape(u_n_i,(mesh.cell_centers.shape[0],mesh.cell_centers.shape[1]))
            # u_n_i = np.ravel(u_n_i,order='F')
        u = u_n_i
        # mesh.plot_vector(u,'numerical solution')
        # mesh.plot_funtion(lambda x,y: u_exact(x,y,t),'exact solution')
        # e = u - mesh.interpolate(lambda x,y: u_exact(x,y,t))
        # mesh.plot_vector(e,'error')
        # print(t)
        # (l2,m) = compute_error(mesh,u,lambda x,y:u_exact(x,y,t))
        # print(l2)
        # print(m)

    return ((h, timestep), compute_error(mesh, u,
                                         lambda x, y: u_exact(x, y, 1)))
#T = lambda x,y: (0.9*y+0.1)*math.sqrt(x) + (0.9-0.9*y)*x**2
#T = lambda p: np.array([p[0]*0.1*p[1]+p[0],p[1]+p[1]*0.1*p[0]])
T = lambda p: np.array([p[0],p[1]])



def random_perturbation(h):
    return lambda p: np.array([random.uniform(0,h)*random.choice([-1,1]) + p[0] - 0.32*p[1],random.uniform(0,h)*random.choice([-1,1]) + p[1]])

mesh = Mesh(6,6,random_perturbation(1/20))
num_unknowns = mesh.cell_centers.shape[0]*mesh.cell_centers.shape[1]
matrix = lil_matrix((num_unknowns,num_unknowns))
flux_matrix = {'x': lil_matrix((num_unknowns,num_unknowns)),'y':lil_matrix((num_unknowns,num_unknowns))}
A,fx,fy = compute_matrix(mesh,K,matrix,flux_matrix)
A = csr_matrix(A,dtype=float)
f = compute_vector(mesh,source,u_lam)
u = spsolve(A,f)
mesh.plot()
mesh.plot_vector(u)

def compute_error(mesh,u,u_fabric):
    cx = mesh.cell_centers.shape[1]
    cy = mesh.cell_centers.shape[0]
    u_fabric_vec = u.copy()
    volumes = u.copy()

    for i in range(cy):
        for j in range(cx):
            u_fabric_vec[mesh.meshToVec(i,j)] = u_fabric(mesh.cell_centers[i,j,0],mesh.cell_centers[i,j,1])
            volumes[mesh.meshToVec(i,j)] = mesh.volumes[i,j]
    #mesh.plot_vector(u-u_fabric_vec,'error')