Example #1
0
void free_preconditioner(struct preconditioner *precon, int id)
{
	if (id == 0) {
		free_3d_array(precon->inverse_mass);
		for (int i = 0; i < precon->nlevels; ++i) {
			free_3d_array(precon->poisson_diag[i]);
			free_3d_array(precon->helmholtz_diag[i]);
			free_3d_array(precon->stiffsum_scale[i]);
			free_3d_array(precon->dirichlet_mask[i]);
		}
	}
	for (int i = 1; i < precon->nlevels; ++i) {
		if (precon->geom[i])
			free_geometry(precon->geom[i]);
		if (precon->sendcounts[i])
			free_int_array(precon->sendcounts[i]);
		if (precon->senddispls[i])
			free_int_array(precon->senddispls[i]);
	}
	for (int i = 1; i < (2 * precon->nlevels - 1); ++i)
		if (precon->basis[i])
			free_basis(precon->basis[i]);
	free_ptr_array(precon->basis, 0, NULL);
	free_ptr_array(precon->geom, 0, NULL);
	free_ptr_array(precon->poisson_diag, 0, NULL);
	free_ptr_array(precon->helmholtz_diag, 0, NULL);
	free_ptr_array(precon->stiffsum_scale, 0, NULL);
	free_ptr_array(precon->dirichlet_mask, 0, NULL);
	free_ptr_array(precon->sendcounts, 0, NULL);
	free_ptr_array(precon->senddispls, 0, NULL);
	free_int_array(precon->nmg);
	free_1d_array(precon->poisson_eigen_max);
	free_1d_array(precon->helmholtz_eigen_max);
	free(precon);
}
Example #2
0
static void initialise_stiffness_summation_scaling(struct element **sel)
{
	double ****x = new_ptr_array((*sel)->nel, 0, NULL);
	for (int j = 0; j < (*sel)->precon->nlevels; ++j) {
		switch_multigrid_level(sel, j);
		const int n = (*sel)->basis->n;
		double ****a = new_4d_array(n, n, n, (*sel)->nel);
		double ****b = new_4d_array(n, n, n, (*sel)->nel);
		for (int i = 0; i < (*sel)->nel; ++i)
			x[i] = sel[i]->precon->stiffsum_scale[j];

		ones(***a, (*sel)->basis->ntot * (*sel)->nel);
		ones(***b, (*sel)->basis->ntot * (*sel)->nel);
		direct_stiffness_summation(sel, a);
		divide(***b, ***a, ***x, n * n * n * (*sel)->nel, 1.0, 0.0);
		free_4d_array(a);
		free_4d_array(b);
	}
	free_ptr_array(x, 0, NULL);
	switch_multigrid_level(sel, 0);
}
Example #3
0
static void initialise_diagonal_precon(struct element **sel, int var)
{
	const double lambda = (11.0 / 6.0) * (double) (var != PRESSURE)
			/ ((*sel)->params->dt * (*sel)->params->visc);
	double ****p = new_ptr_array((*sel)->nel, 0, NULL);

	for (int j = 0; j < (*sel)->precon->nlevels; ++j) {
		switch_multigrid_level(sel, j);
		const int n = (*sel)->basis->n;
		double ****a = new_4d_array(n, n, n, (*sel)->nel);
		double ****b = new_4d_array(n, n, n, (*sel)->nel);
		for (int i = 0; i < (*sel)->nel; ++i) {
			p[i] = precon_array(sel[i], var, j);
			set_helmholtz_diagonal(sel[i], a[i], lambda);
		}
		direct_stiffness_summation(sel, a);
		ones(***b, (*sel)->basis->ntot * (*sel)->nel);
		divide(***b, ***a, ***p, n * n * n * (*sel)->nel, 1.0, 0.0);
		free_4d_array(a);
		free_4d_array(b);
	}
	free_ptr_array(p, 0, NULL);
	switch_multigrid_level(sel, 0);
}
Example #4
0
double comet_exact_test(int k, int N, int *ctbl, int *final_num_tbls, double pvalthresh){
  // Computation variables
  int kbar, Tobs, T, margin_size, num_co_cells;
  double numerator;
  double *pval;
  int *ex_cells, *co_cells, *blank_tbl, *margins, *num_tbls, *cells;
  int **mar_stack;
  
  int num_entries, i, res; // Helper variables
  double final_p_value;
  
  num_entries = 1 << k;
  
  // Compute binary representation of each cell
  ex_cells     = get_ex_cells(k);
  co_cells     = get_co_cells(k);
  num_co_cells = pow(2, k)-k-1;
  
  // Allocate memory stack for marginal remainders
  mar_stack = malloc(sizeof(int *) * (num_co_cells + 1));
  for (i=0; i < num_co_cells + 1; i++) mar_stack[i] = malloc(sizeof(int) * k);
  cells = malloc(sizeof(int) * 1 << (k-1));
  
  /* Compute additional fixed values */
  // Margins
  margins = malloc(sizeof(int) * 2 * k);
  margin_size = pow(2, k-1);
  for (i = 0; i < k; i++){
    // Negative margin
    fixed_cells(k, i, 0, cells);
    margins[i]   = sum_cells( ctbl, cells, margin_size );
    
    // Positive margin
    fixed_cells(k, i, 1, cells);
    margins[i+k] = sum_cells( ctbl, cells, margin_size );
    
    // Initialize margin stack
    mar_stack[0][i] = margins[i+k];
  }
  
  // Numerator
  numerator = 0.0;
  for (i = 0; i < 2*k; i++) numerator += lnfacs[margins[i]];
  
  // Observed
  Tobs = sum_cells(ctbl, ex_cells, k);
  kbar = 0;
  for (i = 0; i < k; i++) kbar += margins[i+k];
  
  /* Set up recursion */
  // Set remaining co-occurrences allowed
  T = kbar - Tobs;
  pval = malloc(sizeof(double));
  num_tbls = malloc(sizeof(int));
  pval[0] = 0.0;
  pval[1] = 0.0; // mid-pvalue
  num_tbls[0] = 0;
  
  // Construct a blank table to start with
  blank_tbl = malloc(sizeof(int) * num_entries);
  
  // Run the exact test recursion which will update the results array
  // with the number of extreme tables and the pval
  res = exact_test_helper( pval, num_tbls, k, pvalthresh, num_entries, N, numerator,
                           margins, ex_cells, co_cells, num_co_cells, blank_tbl,
                           mar_stack, 0, T, Tobs);
  
  *final_num_tbls = num_tbls[0];
  final_p_value = (pval[0]+pval[1])/2;
  
  // Free memory
  free(cells);
  free(margins);
  free(pval);
  free(num_tbls);
  free(co_cells);
  free(ex_cells);
  free(blank_tbl);
  free_ptr_array((void **) mar_stack, num_co_cells + 1);
  
  return (res == OVER_THRESH) ? res : final_p_value;

}