Ejemplo n.º 1
0
def main():
    """Calls argsparse, selects right mode, performs heat equation 
	and plotting. Also handles in- and output files."""
    parser = argparse.ArgumentParser(description='Heat equation')
    n, m, t0, t1, dt, nu, hs, verbose, timeit_mode, img, argin, argout, \
    argtype, argtest = arg_parser(parser)
    computation_time = 0

    # If input file was choosen, read and set heatsource.
    if argin:
        data_list_init, n, m = read_input_file(argin, hs, n, m)
    else:
        data_list_init = [[hs for i in range(n)] for j in range(m)]

    # Sets heatsource and selecs right heat equation implementation.
    f = [[hs for i in range(n)] for j in range(m)]
    print "Running calculations. This might take a while..."
    if (argtype == "numpy"):
        if verbose: print "Numpy mode selected"
        data_list = heat_equation_numpy(t0, t1, dt, n, m, nu, data_list_init,
                                        f, verbose)
        if timeit_mode:
            timer = timeit.Timer(lambda: heat_equation_numpy(
                t0, t1, dt, n, m, nu, data_list_init, f, verbose))
    elif (argtype == "c"):
        if verbose: print "C(Cython) mode selected ."
        data_list = heat_equation_cython(t0, t1, dt, n, m, nu, data_list_init,
                                         f, verbose)
        if timeit_mode:
            timer = timeit.Timer(lambda: heat_equation_cython(
                t0, t1, dt, n, m, nu, data_list_init, f, verbose))
    elif (argtype == "python"):
        if verbose: print "python mode selected(default)"
        data_list = heat_equation(t0, t1, dt, n, m, nu, data_list_init, f,
                                  verbose)
        if timeit_mode:
            timer = timeit.Timer(lambda: heat_equation(
                t0, t1, dt, n, m, nu, data_list_init, f, verbose))
    else:
        print "Error occured! This should not happen."

    # plotting and testing
    plot_data(data_list_init, data_list, img)

    # Check if testing is turned on
    if not argtest:
        runtest = testing(t0, t1, dt, m, n, data_list_init, f, verbose, False)

    # Check if timeit is turned on
    if timeit_mode:
        print "Running timer ..."
        times = timer.repeat(repeat=5, number=1)
        print "Computation times: ", times
        print "Average: ", reduce(lambda x, y: x + y, times) / len(times)

    # If output file was choosen
    if (argout):
        write_output(argout, data_list)
Ejemplo n.º 2
0
def main():
    """Calls argsparse, selects right mode, performs heat equation 
	and plotting. Also handles in- and output files."""
    parser = argparse.ArgumentParser(description="Heat equation")
    n, m, t0, t1, dt, nu, hs, verbose, timeit_mode, img, argin, argout, argtype, argtest = arg_parser(parser)
    computation_time = 0

    # If input file was choosen, read and set heatsource.
    if argin:
        data_list_init, n, m = read_input_file(argin, hs, n, m)
    else:
        data_list_init = [[hs for i in range(n)] for j in range(m)]

        # Sets heatsource and selecs right heat equation implementation.
    f = [[hs for i in range(n)] for j in range(m)]
    print "Running calculations. This might take a while..."
    if argtype == "numpy":
        if verbose:
            print "Numpy mode selected"
        data_list = heat_equation_numpy(t0, t1, dt, n, m, nu, data_list_init, f, verbose)
        if timeit_mode:
            timer = timeit.Timer(lambda: heat_equation_numpy(t0, t1, dt, n, m, nu, data_list_init, f, verbose))
    elif argtype == "c":
        if verbose:
            print "C(Cython) mode selected ."
        data_list = heat_equation_cython(t0, t1, dt, n, m, nu, data_list_init, f, verbose)
        if timeit_mode:
            timer = timeit.Timer(lambda: heat_equation_cython(t0, t1, dt, n, m, nu, data_list_init, f, verbose))
    elif argtype == "python":
        if verbose:
            print "python mode selected(default)"
        data_list = heat_equation(t0, t1, dt, n, m, nu, data_list_init, f, verbose)
        if timeit_mode:
            timer = timeit.Timer(lambda: heat_equation(t0, t1, dt, n, m, nu, data_list_init, f, verbose))
    else:
        print "Error occured! This should not happen."

        # plotting and testing
    plot_data(data_list_init, data_list, img)

    # Check if testing is turned on
    if not argtest:
        runtest = testing(t0, t1, dt, m, n, data_list_init, f, verbose, False)

        # Check if timeit is turned on
    if timeit_mode:
        print "Running timer ..."
        times = timer.repeat(repeat=5, number=1)
        print "Computation times: ", times
        print "Average: ", reduce(lambda x, y: x + y, times) / len(times)

        # If output file was choosen
    if argout:
        write_output(argout, data_list)
Ejemplo n.º 3
0
def testing(t0, t1, dt, m, n, u, f, verbose, img):
    """ Performs test to check if formula is mathematically correct,
	and prints an error value based on u - analytic_u."""
    nu = 1
    f_new = get_heat(f, m, n, nu, verbose)
    analytic_u = get_analytic(m, n, verbose)
    null = zeros((m, n))

    print "Running test. This might take a while..."
    u = heat_equation_numpy(t0, t1, dt, n, m, nu, null, f_new, verbose)

    err = (abs(u - analytic_u)).max()
    print "Error val: {:.6f}".format(err)

    if verbose: print "Plotting data..."
    plot_data(u, analytic_u, img)