예제 #1
0
def test_quadrature():
    # 4 cases: linear spacing, log spacing, simpson, trapezoidal
    grid_min = 1.
    grid_max = 2.
    n_pts = 101
    
    linear_grid, lin_dh, lin_dhdx = setup_grid(grid_min, grid_max, n_pts, 
                                               type = 'linear')
    quad_coeffs_linear_trap = setup_quadrature(linear_grid, lin_dh, lin_dhdx,
                                               type = 'trapezoidal')
    quad_coeffs_linear_simp = setup_quadrature(linear_grid, lin_dh, lin_dhdx,
                                               type = 'simpson')
    
    log_grid, log_dh, log_dhdx = setup_grid(grid_min, grid_max, n_pts,
                                            type = 'log')
    quad_coeffs_log_trap = setup_quadrature(log_grid, log_dh, log_dhdx,
                                            type = 'trapezoidal')
    quad_coeffs_log_simp = setup_quadrature(log_grid,log_dh, log_dhdx,
                                            type = 'simpson')

    # tolerance where it is b/c of inherent approximations in 
    # setting up quadrature grid w/non-uniform spacing, becomes exact as 
    # n_pts -> infinity.
    assert_allclose(np.array([(quad_coeffs_linear_trap * linear_grid).sum(),
                              (quad_coeffs_log_trap * log_grid).sum(),
                              (quad_coeffs_linear_simp * linear_grid**2).sum(),
                              (quad_coeffs_log_simp * log_grid**2).sum()]),
                    np.array([1.5, 1.5, 7./3., 7./3.]), rtol = 2e-5)
예제 #2
0
def test_regularizer():
    '''
    Check the integral of the second derivative of a function.
    '''
    grid_min = 1.
    grid_max = 1.1
    n_pts = 100

    grid, dh, dhdx = setup_grid(grid_min, grid_max, n_pts, type = 'log')
    s = grid**3

    regularizer = setup_regularizer(grid, n_pts) 
    Rs = np.dot(regularizer, s)
    regularized_integral = Rs.sum()
    gold = (3. * (grid[-1]**2 - grid[1]**2))

    assert_allclose(regularized_integral, gold, rtol = 1e-3)
예제 #3
0
    def setUp(self):
        # Physical parameters
        lambda_0 = 488.  # nm
        n_med = 1.43
        theta_deg = 60.
        theta = theta_deg * pi / 180.
        q = 4. * pi * n_med * sin(theta / 2.) / lambda_0 * 1e7
        # convert to cm^-1
        prop_const = 1.37e-4
        mw_dist_kwargs = {'q': q, 'prop_const': prop_const}

        # Load/preprocess data
        test_data = np.loadtxt('contin_test_data_set_1.txt')
        tbase = test_data[:, 0]
        self.y = problem_setup.nonneg_sqrt(test_data[:, 1])

        # Solution setup
        self.n_grid = 31
        gmnmx = np.array([5e2, 5e6])  # bounds of grid
        self.grid_mw, dh, dhdx = setup_grid(gmnmx[0], gmnmx[1], self.n_grid)
        self.quad_weights = setup_quadrature(self.grid_mw, dh, dhdx)

        # Set up coefficient, constraint, and regularizer matrices
        self.A = problem_setup.setup_coefficient_matrix(
            self.grid_mw, tbase, molecular_wt_distr, mw_dist_kwargs,
            self.quad_weights, True)
        self.big_D, self.little_d = problem_setup.setup_nonneg(self.n_grid + 1)
        self.R = problem_setup.dumb_regularizer(self.grid_mw, self.n_grid + 1,
                                                0)

        # Load matrix stats: table of grid points in CONTIN test output
        self.matrix_stats = np.loadtxt('contin_data_set_1_matrix_stats.txt')

        # solve alpha ~ 0 case:
        self.x0, self.err_x0, \
            self.infodict0, self.int_res = solve_fixed_alpha(self.A, self.y,
                                                             5.91e-10, self.R,
                                                             self.big_D,
                                                             self.little_d,
                                                             True)
        # solve regularized case
        self.xa, self.err_xa, \
            self.infodicta = solve_fixed_alpha(self.A, self.y, 3e-6, self.R,
                                               self.big_D, self.little_d,
                                               False)
예제 #4
0
def test_noisy_data():
    n_grid = 51
    grid_r, dh, dhdx = setup_grid(1e-8, 1e-6, n_grid)  # log
    quadrature_weights = setup_quadrature(grid_r, dh, dhdx)

    # set up F_k matrix
    matr_Fk = np.zeros((len(tbase), n_grid))

    for i in np.arange(len(tbase)):
        matr_Fk[i] = F_k(grid_r, tbase[i] * 1e-3)

    matrix_A = np.dot(matr_Fk, np.diag(quadrature_weights))

    # set up y
    # see subroutine USERSI
    noise_sigma = 1e-8
    noise = noise_sigma * np.random.randn(len(data))
    noisy_data = data + noise * np.sqrt(data + 1)
    y_problem = nonneg_sqrt(noisy_data)

    # set up nonnegativity constraints
    big_D = np.diag(np.ones(n_grid))
    little_d = np.zeros(n_grid)

    # set up regularizer matrix
    R = problem_setup.setup_regularizer(grid_r, n_grid)

    best_soln, all_solns = computations.solution_series(matrix_A,
                                                        y_problem,
                                                        R,
                                                        big_D,
                                                        little_d,
                                                        converge_radius=0.05)
    #print grid_r
    #print best_soln

    weights = problem_setup.setup_weights(y_problem -
                                          best_soln[2]['residuals'])
    weighted_y = weights * y_problem
    weighted_A = np.dot(np.diag(weights), matrix_A)

    weighted_soln, weighted_list = computations.solution_series(
        weighted_A, weighted_y, R, big_D, little_d, converge_radius=0.05)
예제 #5
0
def test_deltafunction():
    n_grid = 51
    grid_r, dh, dhdx = setup_grid(1e-8, 1e-6, n_grid)  # log
    quadrature_weights = setup_quadrature(grid_r, dh, dhdx)

    # set up F_k matrix
    matr_Fk = np.zeros((len(tbase), n_grid))

    for i in np.arange(len(tbase)):
        matr_Fk[i] = F_k(grid_r, tbase[i] * 1e-3)

    matrix_A = np.dot(matr_Fk, np.diag(quadrature_weights))

    # set up y
    y_problem = nonneg_sqrt(data)

    # set up nonnegativity constraints
    big_D = np.diag(np.ones(n_grid))
    little_d = np.zeros(n_grid)

    # set up regularizer matrix
    R = problem_setup.setup_regularizer(grid_r, n_grid)

    # how small is too small for alpha?
    # 1e-15 works well
    # 1e-18 is as small as we can go, i think
    # 1e-12 is already perturbing the solution.
    x, err_x, infodict, int_results = computations.solve_fixed_alpha(
        matrix_A, y_problem, 1.1e-21, R, big_D, little_d, True)

    x0, err_x0, infodict0, int_results0 = \
        computations.solve_fixed_alpha(matrix_A, y_problem, 1e-22, R,
                                       big_D, little_d, True)

    prob1alpha = computations.prob_1_alpha(infodict['Valpha'],
                                           infodict0['Valpha'],
                                           infodict0['n_dof'], len(y_problem))

    gold_prob1alpha = 0.64637369
    #assert_allclose(prob1alpha, gold_prob1alpha, rtol=1e-6)

    x2, err_x2, infodict2 = \
        computations.re_solve_fixed_alpha(1.1e-21,
                                          int_results['gamma'],
                                          infodict['singular_values'],
                                          int_results['DZH1_invW'],
                                          int_results['ZH1_invW'],
                                          little_d, infodict['xsc'],
                                          infodict['alpha_sc'],
                                          int_results['Rsc'],
                                          int_results['C'],
                                          int_results['Asc'], y_problem,
                                          big_D)
    assert_allclose(x, x2)
    assert_allclose(err_x, err_x2)

    best_soln, all_solns = computations.solution_series(matrix_A,
                                                        y_problem,
                                                        R,
                                                        big_D,
                                                        little_d,
                                                        converge_radius=0.02)
예제 #6
0

def F_k(mw, tau):
    # so i see RUSER(23) = 0. Is this right??
    # it is, but userk has leading factor of mw
    return mw * exp(-prop_const * q**2 * tau / np.sqrt(mw))


test_data = np.loadtxt('contin_test_data_set_1.txt')
tbase = test_data[:, 0]
y = problem_setup.nonneg_sqrt(test_data[:, 1])

# solution grid
n_grid = 31
gmnmx = [5e2, 5e6]
grid_mw, dh, dhdx = setup_grid(gmnmx[0], gmnmx[1], n_grid)
quadrature_weights = setup_quadrature(grid_mw, dh, dhdx)

matrix_A = problem_setup.setup_coefficient_matrix(grid_mw, tbase,
                                                  molecular_wt_distr,
                                                  mw_dist_kwargs,
                                                  quadrature_weights, True)

# nonnegativity constraints on solution and on dust term
big_D, little_d = problem_setup.setup_nonneg(n_grid + 1)

# regularizer, extra column of zeros added
#R = problem_setup.setup_regularizer(grid_mw, n_grid + 1)
R = problem_setup.dumb_regularizer(grid_mw, n_grid + 1, 0)

# preliminary unweighted analysis