Ejemplo n.º 1
0
def thetaEuler(nx, ntmax, theta, comm):
    '''theta == 0 means forward Euler method, theta == 1 means backward Euler method. 0 <= theta <= 1. '''
    dx = 1.0/nx
    dt = 0.5*dx**2
    x = []
    for i in range(nx + 1):
        x.append(i*dx)
    u = np.zeros(nx + 1)
    dimension = nx - 1
    dimension, rowA, colA, dataA = spmvParallel.sparseMatrix(dimension, theta*dt/dx**2)
    dimension, rowB, colB, dataB = spmvParallel.sparseMatrix(dimension, -(1-theta)*dt/dx**2)

    f = np.zeros(len(x))
    for i in range(len(x)):
        f[i] = F(x[i])

    tol = 0.0001
    iterMax = 100
    step = 0
    while(True):
        V = []
        for element in u[1:-1]:
            V.append(element)
        u[1:-1] = spmvcgParallel.conjugateGradient(dimension, rowA, colA, dataA, spmvParallel.productParallel(dimension, rowB, colB, dataB, u[1:-1], comm) + dt*f[1:-1], tol, iterMax, comm)
        step = step + 1
        residual = 0
        for i in range(len(V)):
            residual = residual + (V[i] - u[1:-1][i])**2
        #print step
        #print residual
        if (step > ntmax):
            break
        if (np.sqrt(residual) < 0.0000001):
            break
    return x, u
Ejemplo n.º 2
0
def main():
    import sys

    if len(sys.argv) != 3:
        print "Matrix dimension = argv[1], l = argv[2]. "
        return -1
    pr = cProfile.Profile()
    pr.enable()
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    nproc = comm.Get_size()
    dimension = int(sys.argv[1])
    l = float(sys.argv[2])
    dimension, row, col, data = spmvParallel.sparseMatrix(dimension, l)
    b = np.zeros(dimension)
    for i in range(dimension):
        b[i] = i + 1

    tol = 0.0001
    iterMax = 100
    x = conjugateGradient(dimension, row, col, data, b, tol, iterMax, comm)
    if False and rank == 0:
        A = spmvParallel.createSparseMatrix(dimension, l)
        print "Matrix A: "
        print A
        print "Vector b: "
        print b
        print "Solution of Ax = b: "
        print x
        print "Ax = "
        print spmvParallel.productParallel(dimension, row, col, data, x, comm)
    pr.disable()
    pr.dump_stats("profile")
    pr.print_stats()
    return 0
Ejemplo n.º 3
0
def main():
    import sys
    if (len(sys.argv) != 3):
        print "Matrix dimension = argv[1], l = argv[2]. "
        return -1
    pr = cProfile.Profile()
    pr.enable()
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    nproc = comm.Get_size()
    dimension = int(sys.argv[1])
    l = float(sys.argv[2])
    dimension, row, col, data = spmvParallel.sparseMatrix(dimension, l)
    b = np.zeros(dimension)
    for i in range(dimension):
        b[i] = i + 1

    tol = 0.0001
    iterMax = 100
    x = conjugateGradient(dimension, row, col, data, b, tol, iterMax, comm)
    if (False and rank == 0):
        A = spmvParallel.createSparseMatrix(dimension, l)
        print "Matrix A: "
        print A
        print "Vector b: "
        print b
        print "Solution of Ax = b: "
        print x
        print "Ax = "
        print spmvParallel.productParallel(dimension, row, col, data, x, comm)
    pr.disable()
    pr.dump_stats("profile")
    pr.print_stats()
    return 0
Ejemplo n.º 4
0
def thetaEuler(nx, ntmax, theta, comm):
    '''theta == 0 means forward Euler method, theta == 1 means backward Euler method. 0 <= theta <= 1. '''
    dx = 1.0 / nx
    dt = 0.5 * dx**2
    x = []
    for i in range(nx + 1):
        x.append(i * dx)
    u = np.zeros(nx + 1)
    dimension = nx - 1
    dimension, rowA, colA, dataA = spmvParallel.sparseMatrix(
        dimension, theta * dt / dx**2)
    dimension, rowB, colB, dataB = spmvParallel.sparseMatrix(
        dimension, -(1 - theta) * dt / dx**2)

    f = np.zeros(len(x))
    for i in range(len(x)):
        f[i] = F(x[i])

    tol = 0.0001
    iterMax = 100
    step = 0
    while (True):
        V = []
        for element in u[1:-1]:
            V.append(element)
        u[1:-1] = spmvcgParallel.conjugateGradient(
            dimension, rowA, colA, dataA,
            spmvParallel.productParallel(dimension, rowB, colB, dataB, u[1:-1],
                                         comm) + dt * f[1:-1], tol, iterMax,
            comm)
        step = step + 1
        residual = 0
        for i in range(len(V)):
            residual = residual + (V[i] - u[1:-1][i])**2
        #print step
        #print residual
        if (step > ntmax):
            break
        if (np.sqrt(residual) < 0.0000001):
            break
    return x, u