def test_qmc_sample_generation():
    """Tests that all the anscillary code (apart from actually calculating
the qois) for qmc points happens in an expected way."""
    k = 1.0

    h_spec = (1.0,-1.5)

    J = 100

    nu = 16

    M = 4

    point_generation_method = 'qmc'

    delta = 2.0

    lambda_mult = 1.0

    j_scaling = 1.0

    qois = ['testing']

    num_spatial_cores = 1

    output = gen_samples.generate_samples(k,h_spec,J,nu,M,
                                          point_generation_method,
                                          delta,lambda_mult,j_scaling,qois,
                                          num_spatial_cores,dim=2)

    for shift_no in range(nu):
        assert np.allclose(output[1][shift_no][0],[float(ii) for ii in range(1,2**M + 1)])
def test_mc_sample_generation():
    k = 1.0

    h_spec = (1.0,-1.5)

    J = 100

    nu = 16

    M = 4

    point_generation_method = 'mc'

    delta = 2.0

    lambda_mult = 1.0

    j_scaling = 1.0

    num_spatial_cores = 1

    qois = ['testing']

    output = gen_samples.generate_samples(k,h_spec,J,nu,M,
                                          point_generation_method,
                                          delta,lambda_mult,j_scaling,qois,num_spatial_cores,dim=2)

    N = float(nu*2**M)
    
    assert np.isclose(output[1],(N+1.0)/2.0)
    
    assert np.isclose(output[2],np.sqrt((N+1.0)/12.0))
def test_every_point_a_preconditioner():
    """Tests for expected behaviour when every point is a preconditioner."""

    k = 10.0

    h_spec = (1.0, -1.5)

    J = 4

    nu = 1

    M = 5

    point_generation_method = 'qmc'

    delta = 2.0

    lambda_mult = 1.0

    j_scaling = 1.0

    num_spatial_cores = 1

    # This is just testing that we correctly handle multiple qois
    qois = ['integral']

    dim = 2

    display_progress = True

    physically_realistic = False

    nearby_preconditioning = True

    # This is a hack to ensure that every point is selected - if you put this as 1.0, then because of how we select the preconditioning points, not every point is selected.
    nearby_preconditioning_proportion = 3.0

    output = gen_samples.generate_samples(
        k, h_spec, J, nu, M, point_generation_method, delta, lambda_mult,
        j_scaling, qois, num_spatial_cores, dim, display_progress,
        physically_realistic, nearby_preconditioning,
        nearby_preconditioning_proportion)

    assert (np.array(output[3]) == 1).all()
def test_one_point_a_preconditioner():
    """Tests for expected behaviour when only one point is a preconditioner."""

    k = 10.0

    h_spec = (1.0, -1.5)

    J = 4

    nu = 1

    M = 5

    point_generation_method = 'qmc'

    delta = 2.0

    lambda_mult = 1.0

    j_scaling = 1.0

    num_spatial_cores = 1

    # This is just testing that we correctly handle multiple qois
    qois = ['integral']

    dim = 2

    display_progress = True

    physically_realistic = False

    nearby_preconditioning = True

    nearby_preconditioning_proportion = 1.0 / float(2**M)

    output = gen_samples.generate_samples(
        k, h_spec, J, nu, M, point_generation_method, delta, lambda_mult,
        j_scaling, qois, num_spatial_cores, dim, display_progress,
        physically_realistic, nearby_preconditioning,
        nearby_preconditioning_proportion)

    assert np.array(np.array(output[3]) == 1).sum() == 1
def test_nbpc_qmc():
    """Checks that QMC + nearby preconditioning doesn't crash."""

    k = 1.0

    h_spec = (1.0, -1.5)

    J = 2

    nu = 1

    M = 6

    point_generation_method = 'qmc'

    delta = 2.0

    lambda_mult = 1.0

    j_scaling = 1.0

    num_spatial_cores = 1

    # This is just testing that we correctly handle multiple qois
    qois = ['integral']

    dim = 2

    display_progress = True

    physically_realistic = False

    nearby_preconditioning = True

    nearby_preconditioning_proportion = 0.1

    output = gen_samples.generate_samples(
        k, h_spec, J, nu, M, point_generation_method, delta, lambda_mult,
        j_scaling, qois, num_spatial_cores, dim, display_progress,
        physically_realistic, nearby_preconditioning,
        nearby_preconditioning_proportion)
def qmc_test(num_spatial_cores,
             nearby_preconditioning=False,
             nearby_preconditioning_proportion=None):
    """The test used to check parallelism is working correctly."""

    k = 5.0

    h_spec = (1.0, -1.5)

    J = 10

    nu = 20

    M = 5

    delta = 1.0

    lambda_mult = 1.0

    j_scaling = 1.0

    qois = ['integral', 'origin']

    return gen_samples.generate_samples(
        k,
        h_spec,
        J,
        nu,
        M,
        'qmc',
        delta,
        lambda_mult,
        j_scaling,
        qois,
        num_spatial_cores=num_spatial_cores,
        dim=2,
        display_progress=True,
        physically_realistic=False,
        nearby_preconditioning=nearby_preconditioning,
        nearby_preconditioning_proportion=nearby_preconditioning_proportion)
def test_multiple_qois_mc():
    """Checks that multiple qois are calculated correctly for MC."""

    k = 1.0

    h_spec = (1.0,-1.5)

    J = 100

    nu = 16

    M = 4

    point_generation_method = 'mc'

    delta = 2.0

    lambda_mult = 1.0

    j_scaling = 1.0

    # This is just testing that we correctly handle multiple qois
    qois = ['testing','testing']
    

    output = gen_samples.generate_samples(k,h_spec,J,nu,M,
                                          point_generation_method,
                                          delta,lambda_mult,j_scaling,qois,dim=2)

    N = nu*(2**M)
    
    # First qoi
    assert np.isclose(output[1][0],(float(N)+1.0)/2.0)
    
    assert np.isclose(output[2][0],np.sqrt((float(N)+1.0)/12.0))
    
    # Second qoi
    assert np.isclose(output[1][1],(float(N)+1.0)/2.0)
    
    assert np.isclose(output[2][1],np.sqrt((float(N)+1.0)/12.0))
def test_multiple_qois_qmc():
    """Checks that anscillary code with multiple qois works for QMC."""

    k = 1.0

    h_spec = (1.0,-1.5)

    J = 100

    #    nu = 16
    nu=2

    M = 4

    point_generation_method = 'qmc'

    delta = 2.0

    lambda_mult = 1.0

    j_scaling = 1.0
    
    num_spatial_cores = 1
    
    # This is just testing that we correctly handle multiple qois
    qois = ['testing','testing']
    

    output = gen_samples.generate_samples(k,h_spec,J,nu,M,
                                          point_generation_method,
                                          delta,lambda_mult,j_scaling,qois,
                                          num_spatial_cores,dim=2)

    # First qoi
    for shift_no in range(nu):
        assert np.allclose(output[1][shift_no][0],[float(ii) for ii in range(1,2**M + 1)])

    # Second qoi
    for shift_no in range(nu):
        assert np.allclose(output[1][shift_no][1],[float(ii) for ii in range(1,2**M + 1)])
    # Assumes number of cores is a power of 2

    num_spatial_cores = int(
        2**np.floor(np.log2(np.max((1, dofs // 50000))))
    )  # 50,000 is Firedrake's recommendend minimum number of DoFs per node to get good parallel scalability

    qmc_out = gen.generate_samples(
        k=k,
        h_spec=h_spec,
        J=quants['J'],
        nu=quants['nu'],
        M=quants['M_high'],
        point_generation_method='qmc',
        delta=quants['delta'],
        lambda_mult=quants['lambda_mult'],
        j_scaling=quants['j_scal'],
        qois=quants['qois'],
        num_spatial_cores=num_spatial_cores,
        dim=dim,
        display_progress=True,
        physically_realistic=False,
        nearby_preconditioning=quants['nbpc'],
        nearby_preconditioning_proportion=quants['nbpc_prop'])

    if fd.COMM_WORLD.rank == 0:
        if h_refinement == 0:
            mkdir(folder_name)

        with open(
                './' + folder_name + 'output-h_magnitude-' + str(h_spec[0]) +