Esempio n. 1
0
def run_accuracy_check(nvars_list, problem_params):
    """
    Routine to check the error of the Laplacian vs. its FD discretization

    Args:
        nvars_list: list of nvars to do the testing with
        problem_params: dictionary containing the problem-dependent parameters

    Returns:
        a dictionary containing the errors and a header (with nvars_list)
    """

    results = {}
    # loop over all nvars
    for nvars in nvars_list:

        # setup problem
        problem_params['nvars'] = nvars
        prob = heat2d_periodic(problem_params=problem_params,
                               dtype_u=mesh,
                               dtype_f=mesh)

        # create x values, use only inner points
        xvalues = np.array([i * prob.dx for i in range(prob.params.nvars[0])])

        # create a mesh instance and fill it with a sine wave
        u = prob.u_exact(t=0)

        # create a mesh instance and fill it with the Laplacian of the sine wave
        u_lap = prob.dtype_u(init=prob.init)
        u_lap.values = -2 * (np.pi *
                             prob.params.freq)**2 * prob.params.nu * np.kron(
                                 np.sin(np.pi * prob.params.freq * xvalues),
                                 np.sin(np.pi * prob.params.freq * xvalues))
        u_lap.values = u_lap.values.flatten()

        # compare analytic and computed solution using the eval_f routine of the problem class
        err = abs(prob.eval_f(u, 0) - u_lap)

        # get id for this nvars and put error into dictionary
        id = ID(nvars=nvars)
        results[id] = err

    # add nvars_list to dictionary for easier access later on
    results['nvars_list'] = nvars_list

    return results
Esempio n. 2
0
def test_mesh_to_mesh_2d_periodic():
    """
        A simple test program to test periodic interpolation order in 2d
        """

    # initialize problem parameters
    problem_params = {}
    problem_params['c'] = 0.1  # advection coefficient
    problem_params['freq'] = 4  # frequency for the test value
    problem_params['nu'] = 1.0

    # initialize transfer parameters
    space_transfer_params = {}
    space_transfer_params['rorder'] = 2
    space_transfer_params['periodic'] = True

    iorder_list = [2, 4, 6, 8]
    nvars_fine_list = [(2**p, 2**p) for p in range(5, 9)]

    # set up dictionary to store results (plus lists)
    results = {}
    results['nvars_fine_list'] = nvars_fine_list
    results['iorder_list'] = iorder_list

    # loop over interpolation orders and number of DOFs
    for iorder in iorder_list:

        space_transfer_params['iorder'] = iorder

        for nvars_fine in nvars_fine_list:
            # instantiate fine problem
            problem_params[
                'nvars'] = nvars_fine  # number of degrees of freedom
            Pfine = heat2d_periodic(problem_params=problem_params,
                                    dtype_u=mesh,
                                    dtype_f=mesh)

            # instantiate coarse problem
            problem_params['nvars'] = (int(nvars_fine[0] / 2),
                                       int(nvars_fine[1] / 2))
            Pcoarse = heat2d_periodic(problem_params=problem_params,
                                      dtype_u=mesh,
                                      dtype_f=mesh)

            # instantiate spatial interpolation
            T = mesh_to_mesh(fine_prob=Pfine,
                             coarse_prob=Pcoarse,
                             params=space_transfer_params)

            # set exact fine solution to compare with
            uexact_fine = Pfine.u_exact(t=0)

            # set exact coarse solution as source
            uexact_coarse = Pcoarse.u_exact(t=0)

            # do the interpolation/prolongation
            uinter = T.prolong(uexact_coarse)

            # compute error and store
            err = abs(uinter - uexact_fine)
            id = ID(nvars_fine=nvars_fine, iorder=iorder)
            results[id] = err

    orders = get_accuracy_orders(results)

    print(orders)

    for p in range(len(orders)):
        # print(abs(orders[p][1] - orders[p][2]) / orders[p][1])
        assert abs(orders[p][1] - orders[p][2]) / orders[p][
            1] < 0.115, 'ERROR: did not get expected orders for interpolation, got %s' % str(
                orders[p])