#define PROG_NAME "test_grid" #define PROG_DESC "test of dyadic grids: basics" #define PROG_VERS "1.0" /* Last edited on 2014-05-16 00:14:02 by stolfilocal */ #define test_grid_COPYRIGHT "Copyright © 2014 by the State University of Campinas (UNICAMP)." #define PROG_HELP \ " " PROG_NAME " \\\n" \ " -dim {DIM} \\\n" \ " -outName NAME \\\n" \ " " argparser_help_info_HELP " \\\n" \ " < {INFILE}" #define PROG_INFO \ "NAME\n" \ " " PROG_NAME " - " PROG_DESC "\n" \ "\n" \ "SYNOPSIS\n" \ PROG_HELP "\n" \ "\n" \ "DESCRIPTION\n" \ "\n" \ "INPUT FILE FORMAT\n" \ "\n" \ "OPTIONS\n" \ test_grid_options_INFO "\n" \ "\n" \ "DOCUMENTATION OPTIONS\n" \ argparser_help_info_HELP_INFO "\n" \ "\n" \ "SEE ALSO\n" \ " /There Will Be Blood/.\n" \ "\n" \ "AUTHOR\n" \ " Started 2014-05-15 by Jorge Stolfi, IC-UNICAMP.\n" \ "\n" \ "MODIFICATION HISTORY\n" \ "\n" \ "WARRANTY\n" \ argparser_help_info_NO_WARRANTY "\n" \ "\n" \ "RIGHTS\n" \ " " test_grid_COPYRIGHT ".\n" \ "\n" \ argparser_help_info_STANDARD_RIGHTS /* Datapoint interpolation with 2D dyadic splines. */ #include #include #include #include #include #include #include #include #include #include #include #include #include typedef struct test_grid_options_t { dg_dim_t dim; /* Dimension of domain space. */ /* Output options: */ char *outName; /* Prefix for output file names. */ } test_grid_options_t; \ #define test_grid_options_INFO \ " -dim {DIM}\n" \ " Specifies the domain's dimension as {DIM}.\n" \ "\n" \ " -outName {OUT_NAME}\n" \ " Specifies the common prefix {OUT_NAME} of all output file names." void test_grid_basic_ops(dg_dim_t d, dg_tree_t *G); void test_dg_axis_set_complement(dg_dim_t d, dg_tree_t *G); void test_dg_rank_cmp(dg_dim_t d, dg_tree_t *G); void test_dg_split_axis(dg_dim_t d, dg_tree_t *G); void test_dg_grid_size(dg_dim_t d, dg_tree_t *G); void test_dg_max_grid_pos(dg_dim_t d, dg_tree_t *G); void test_dg_cell_min_id(dg_dim_t d, dg_tree_t *G); void test_dg_cell_max_id(dg_dim_t d, dg_tree_t *G); void test_dg_cell_rank(dg_dim_t d, dg_tree_t *G); void test_dg_check_cell_rank(dg_dim_t d, dg_tree_t *G); void test_dg_cell_position(dg_dim_t d, dg_tree_t *G); void test_dg_cell_from_position(dg_dim_t d, dg_tree_t *G); void test_dg_cell_axis_shift(dg_dim_t d, dg_tree_t *G); void test_dg_cell_shift(dg_dim_t d, dg_tree_t *G); void test_dg_cell_id_add(dg_dim_t d, dg_tree_t *G); void test_dg_cell_neighbor(dg_dim_t d, dg_tree_t *G); void test_dg_cell_box_root_relative(dg_dim_t d, dg_tree_t *G); void test_dg_cell_root_canonical_sizes(dg_dim_t d, dg_tree_t *G); void test_dg_cell_box_canonical(dg_dim_t d, dg_tree_t *G); void test_dg_cell_print(dg_dim_t d, dg_tree_t *G); void test_grid_sort_loci(dg_locus_vec_t *L, int nd); /* Sorts the list of loci {L} by increasing rank. */ int main(int argc, char **argv); test_grid_options_t *test_grid_get_options(int argc, char **argv); /* TYPES */ int main(int argc, char **argv) { test_grid_options_t *o = test_grid_get_options(argc, argv); dg_dim_t d = o->dim; fprintf(stderr, "=== testing dg_grid.h for dim = %d ===\n", d); /* The grid's tree: */ dg_rank_t minRank = 2; dg_rank_t maxRank = 8; int numCells = 100; dg_tree_node_t *G = dg_test_make_adaptive_grid(d, minRank, maxRank, numCells); /* Sort the loci by increasing depth: */ test_grid_basic_ops(d, &G); return 0; } void test_grid_basic_ops(dg_dim_t d, dg_tree_t *G) { test_dg_axis_set_complement(d, G); test_dg_rank_cmp(d, G); test_dg_split_axis(d, G); test_dg_grid_size(d, G); test_dg_max_grid_pos(d, G); test_dg_cell_min_id(d, G); test_dg_cell_max_id(d, G); test_dg_cell_rank(d, G); test_dg_check_cell_rank(d, G); test_dg_cell_position(d, G); test_dg_cell_from_position(d, G); test_dg_cell_axis_shift(d, G); test_dg_cell_shift(d, G); test_dg_cell_id_add(d, G); test_dg_cell_neighbor(d, G); test_dg_cell_box_root_relative(d, G); test_dg_cell_root_canonical_sizes(d, G); test_dg_cell_box_canonical(d, G); test_dg_cell_print(d, G); } void test_dg_axis_set_complement(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "testing %s ...\n", "dg_axis_set_complement"); dg_axis_set_t nil = (dg_axis_set_t)0; dg_axis_set_t all = (((dg_axis_set_t)1) << d) - 1; int A; for (A = (int)nil; A <= (int)all; A++) { dg_axis_set_t B = dg_axis_set_complement(d, (dg_axis_set_t)A); assert((A & B) == nil); assert((A | B) == all); } } void test_dg_rank_cmp(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "testing %s ...\n", "dg_rank_cmp"); int x, y; for (x = 0; x <= dg_rank_MAX; x++) { for (y = 0; y <= dg_rank_MAX; y++) { dg_rank_t xr = (dg_rank_t)x; dg_rank_t yr = (dg_rank_t)y; sign_t res = dg_rank_cmp(&xr, &yr); int rex = (x < y ? -1 : (x > y ? +1 : 0)); assert((int)res == rex); } } } void test_dg_split_axis(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "testing %s ...\n", "dg_split_axis"); int x; for (x = 0; x <= dg_rank_MAX; x++) { dg_rank_t xr = (dg_rank_t)x; dg_axis_t res = dg_split_axis(d, xr); int rex = (x % d); assert((int)res == rex); } } void test_dg_grid_size(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_grid_size"); /* void res = dg_grid_size(dg_dim_t d, dg_rank_t r, dg_grid_size_t gsz[]); */ /* assert(res ); */ } void test_dg_max_grid_pos(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_max_grid_pos"); /* dg_grid_pos_t res = dg_max_grid_pos(dg_dim_t d, dg_rank_t r, dg_axis_t i); */ /* assert(res ); */ } void test_dg_cell_min_id(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_min_id"); /* dg_cell_id_t res = dg_cell_min_id(r) (1LL << (r)); */ /* assert(res ); */ } void test_dg_cell_max_id(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_max_id"); /* dg_cell_id_t res = dg_cell_max_id(r) ((1LL << (r+1))-1LL); */ /* assert(res ); */ } void test_dg_cell_rank(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_rank"); /* dg_rank_t res = dg_cell_rank(dg_cell_id_t id); */ /* assert(res ); */ } void test_dg_check_cell_rank(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_check_cell_rank"); /* bool_t res = dg_check_cell_rank(id,r) (((id)>>(r)) == 1LL); */ /* assert(res ); */ } void test_dg_cell_position(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_position"); /* void res = dg_cell_position(dg_dim_t d, dg_cell_id_t id, dg_grid_pos_t pos[]); */ /* assert(res ); */ } void test_dg_cell_from_position(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_from_position"); /* dg_cell_id_t res = dg_cell_from_position(dg_dim_t d, dg_rank_t r, dg_grid_pos_t pos[]); */ /* assert(res ); */ } void test_dg_cell_axis_shift(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_axis_shift"); /* dg_cell_id_t res = dg_cell_axis_shift(dg_dim_t d, dg_cell_id_t id, dg_axis_t ax, dg_grid_pos_t dp); */ /* assert(res ); */ } void test_dg_cell_shift(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_shift"); /* dg_cell_id_t res = dg_cell_shift(dg_dim_t d, dg_cell_id_t id, dg_grid_pos_t dp[]); */ /* assert(res ); */ } void test_dg_cell_id_add(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_id_add"); /* dg_cell_id_t res = dg_cell_id_add(dg_dim_t d, dg_cell_id_t ida, dg_cell_id_t idb); */ /* assert(res ); */ } void test_dg_cell_neighbor(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_neighbor"); /* dg_cell_id_t res = dg_cell_neighbor(dg_dim_t d, dg_cell_id_t id, box_face_index_t fi); */ /* assert(res ); */ } void test_dg_cell_box_root_relative(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_box_root_relative"); /* void res = dg_cell_box_root_relative(dg_dim_t d, dg_cell_id_t id, interval_t B[]); */ /* assert(res ); */ } void test_dg_cell_root_canonical_sizes(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_root_canonical_sizes"); /* double * res = dg_cell_root_canonical_sizes(dg_dim_t d); */ /* assert(res ); */ } void test_dg_cell_box_canonical(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_box_canonical"); /* void res = dg_cell_box_canonical(dg_dim_t d, dg_cell_id_t id, interval_t B[]); */ /* assert(res ); */ } void test_dg_cell_print(dg_dim_t d, dg_tree_t *G) { fprintf(stderr, "** %s not implemented yet\n", "test_dg_cell_print"); /* void res = dg_cell_print(FILE *wr, dg_dim_t d, dg_cell_id_t id); */ /* assert(res ); */ } void test_grid_sort_loci(dg_locus_vec_t *L, int nd) { int np = L->ne; /* Number of loci. */ /* Allocates the perm vector {ord}: */ int ord[np], i; /* Set {ord[0..np-1]} to indices of elems of {L.e}, in rank order: */ for (i = 0; i < np; i++) { ord[i] = i; } auto sign_t cmp_locus_rank(int ix, int iy); /* Compares ranks of {L.e[ix]} and {L.e[iy]}. */ isrt_heapsort(ord, np, cmp_locus_rank, +1); /* Compute the inverse perm {dro} of {ord}: */ int dro[np]; for (i = 0; i < np; i++) { dro[ord[i]] = i; } /* Swaps the elements in the order given by {ord}: */ for (i = 0; i < np; i++) { int j = ord[i]; assert(dro[j] == i); /* Swap locus {i} with locus {j}: */ { dg_locus_t Lt = L->e[i]; L->e[i] = L->e[j]; L->e[j] = Lt; } /* Remember that elem {L.e[i]} is now at {L.e[j]}: */ dro[j] = dro[i]; ord[dro[i]] = j; ord[i] = i; dro[i] = i; } return; sign_t cmp_locus_rank(int ix, int iy) { dg_rank_t rx = dg_locus_rank(L->e[ix]); dg_rank_t ry = dg_locus_rank(L->e[iy]); return dg_rank_cmp(&rx, &ry); } } test_grid_options_t *test_grid_get_options(int argc, char **argv) { argparser_t *pp = argparser_new(stderr, argc, argv); argparser_set_help(pp, PROG_NAME " version " PROG_VERS ", usage:\n" PROG_HELP); argparser_set_info(pp, PROG_INFO); argparser_process_help_info_options(pp); test_grid_options_t *o = notnull(malloc(sizeof(test_grid_options_t)), "no mem"); argparser_get_keyword(pp, "-dim"); o->dim = (dg_dim_t)argparser_get_next_int(pp, 1, dg_dim_MAX); argparser_get_keyword(pp, "-outName"); o->outName = argparser_get_next(pp); /* Check for spurious args: */ argparser_finish(pp); return o; }