void hp_amr_smooth_pred_save_predictor ( p4est_t* p4est, hp_amr_smooth_pred_data_t* smooth_pred_data ) { smooth_pred_data->pred_storage_size = p4est->local_num_quadrants; smooth_pred_data->pred_storage = P4EST_REALLOC ( smooth_pred_data->pred_storage, double, smooth_pred_data->pred_storage_size ); p4est_iterate ( p4est, NULL, (void*)smooth_pred_data, hp_amr_smooth_pred_save_predictor_callback, NULL, #if (P4EST_DIM)==3 NULL, #endif NULL ); }
static void hp_amr_apriori_max_l2_err_density_calc ( p4est_t* p4est ) { p4est_iterate(p4est, NULL, NULL, hp_amr_apriori_max_l2_err_density_calc_callback, NULL, #if (P4EST_DIM)==3 NULL, #endif NULL); }
/** Write the state variable to vtk format, one file per process. * * \param [in] p4est the forest, whose quadrant data contains the state * \param [in] timestep the timestep number, used to name the output files */ static void step3_write_solution (p4est_t * p4est, int timestep) { char filename[BUFSIZ] = { '\0' }; double *u_interp; p4est_locidx_t numquads; snprintf (filename, 17, P4EST_STRING "_step3_%04d", timestep); numquads = p4est->local_num_quadrants; /* create a vector with one value for the corner of every local quadrant * (the number of children is always the same as the number of corners) */ u_interp = P4EST_ALLOC (double, numquads * P4EST_CHILDREN); /* Use the iterator to visit every cell and fill in the solution values. * Using the iterator is not absolutely necessary in this case: we could * also loop over every tree (there is only one tree in this case) and loop * over every quadrant within every tree, but we are trying to demonstrate * the usage of p4est_iterate in this example */ p4est_iterate (p4est, NULL, /* we don't need any ghost quadrants for this loop */ (void *) u_interp, /* pass in u_interp so that we can fill it */ step3_interpolate_solution, /* callback function that interpolate from the cell center to the cell corners, defined above */ NULL, /* there is no callback for the faces between quadrants */ #ifdef P4_TO_P8 NULL, /* there is no callback for the edges between quadrants */ #endif NULL); /* there is no callback for the corners between quadrants */ p4est_vtk_write_all (p4est, NULL, /* we do not need to transform from the vertex space into physical space, so we do not need a p4est_geometry_t * pointer */ 0.99, /* draw each quadrant at almost full scale */ 0, /* do not write the tree id's of each quadrant (there is only one tree in this example) */ 1, /* do write the refinement level of each quadrant */ 1, /* do write the mpi process id of each quadrant */ 0, /* do not wrap the mpi rank (if this were > 0, the modulus of the rank relative to this number would be written instead of the rank) */ 1, /* write one scalar field: the solution value */ 0, /* write no vector fields */ filename, "solution", u_interp); P4EST_FREE (u_interp); }
void hp_amr_smooth_pred_load_predictor ( p4est_t* p4est, hp_amr_smooth_pred_data_t* smooth_pred_data ) { /* only load the data if it matches the size of local mesh */ mpi_assert(smooth_pred_data->pred_storage_size == p4est->local_num_quadrants); p4est_iterate ( p4est, NULL, (void*)smooth_pred_data, hp_amr_smooth_pred_load_predictor_callback, NULL, #if (P4EST_DIM)==3 NULL, #endif NULL ); }
void hp_amr_smooth_pred_zero_predictor ( p4est_t* p4est ) { /* zero out prediction if this is the first init call, otherwise we want to keep it around for the next round of refinement. */ p4est_iterate(p4est, NULL, NULL, hp_amr_smooth_pred_init_callback, NULL, #if (P4EST_DIM)==3 NULL, #endif NULL); }
int main(int argc, char *argv[]) { int mpiret; sc_MPI_Comm mpicomm; int proc_size; p4est_connectivity_t *conn; p4est_geometry_t *geom; p4est_t* p4est; int seed = time(NULL); srand(seed); /* MPI init */ mpiret = sc_MPI_Init(&argc, &argv); SC_CHECK_MPI(mpiret); mpicomm = sc_MPI_COMM_WORLD; sc_init (mpicomm, 1, 1, NULL, SC_LP_ESSENTIAL); mpiret = MPI_Comm_size(mpicomm, &proc_size); SC_CHECK_MPI (mpiret); /* pXest init */ p4est_init(NULL, SC_LP_PRODUCTION); conn = p4est_connectivity_new_disk(); /* geom = p4est_geometry_new_connectivity(conn); */ geom = p4est_geometry_new_disk(conn,1.,2.); p4est = p4est_new_ext (mpicomm, conn, -1, 0, 1, sizeof(curved_element_data_t), NULL, NULL); int world_rank,world_size; sc_MPI_Comm_rank(sc_MPI_COMM_WORLD, &world_rank); sc_MPI_Comm_size(sc_MPI_COMM_WORLD, &world_size); /* start just-in-time dg-math */ dgmath_jit_dbase_t* dgmath_jit_dbase = dgmath_jit_dbase_init(); geometric_factors_t* geometric_factors = geometric_factors_init(p4est); curved_element_data_init(p4est, geometric_factors, dgmath_jit_dbase, geom, 4); /* int local_nodes = curved_element_data_get_local_nodes(p4est); */ /* double* u = P4EST_ALLOC(double, local_nodes); */ /* for (int i = 0; i < local_nodes; i++){ */ /* /\* u = x *\/ */ /* u[i] = geometric_factors->xyz[i]; */ /* } */ int num_of_refinements = 2; /* p4est_vtk_write_all */ /* (p4est, */ /* geom, */ /* 0.99, */ /* 1, */ /* 1, */ /* 1, */ /* 0, */ /* 0, */ /* 0, */ /* "disk0" */ /* ); */ for (int i = 0; i < num_of_refinements; i++){ p4est_refine_ext (p4est, 0, -1, random_h_refine, NULL, refine_uniform_replace_callback); p4est_balance_ext(p4est, P4EST_CONNECT_FACE, NULL, refine_uniform_replace_callback); /* p4est_vtk_write_all */ /* (p4est, */ /* geom, */ /* 0.99, */ /* 1, */ /* 1, */ /* 1, */ /* 0, */ /* 0, */ /* 0, */ /* "disk" */ /* ); */ } curved_element_data_init(p4est, geometric_factors, dgmath_jit_dbase, geom, -1); int local_nodes = curved_element_data_get_local_nodes(p4est); double* u = P4EST_ALLOC(double, local_nodes); for (int i = 0; i < local_nodes; i++){ /* u = x */ u[i] = geometric_factors->xyz[i]; } /* curved_element_data_init(p4est, geometric_factors, dgmath_jit_dbase, geom, -1); */ /* store vector */ curved_element_data_copy_from_vec_to_storage ( p4est, u ); test_curved_data_t test_curved_data; test_curved_data.mortar_err = 0.; test_curved_data.hanging_proj_err = 0.; test_curved_data.full_proj_err = 0.; test_curved_data.print_data = 1; test_curved_data.no_reorient = 0; test_curved_data.geom = geom; p4est_ghost_t* ghost = p4est_ghost_new (p4est, P4EST_CONNECT_FACE); /* create space for storing the ghost data */ curved_element_data_t* ghost_data = P4EST_ALLOC (curved_element_data_t, ghost->ghosts.elem_count); p4est_ghost_exchange_data (p4est, ghost, ghost_data); curved_compute_flux_user_data_t curved_compute_flux_user_data; curved_compute_flux_user_data.dgmath_jit_dbase = dgmath_jit_dbase; curved_flux_fcn_ptrs_t flux_fcns = (test_curved_data_fetch_fcns(&test_curved_data)); curved_compute_flux_user_data.flux_fcn_ptrs = &flux_fcns; p4est->user_pointer = &curved_compute_flux_user_data; p4est_iterate (p4est, ghost, (void *) ghost_data, NULL, curved_compute_flux_on_local_elements, #if (P4EST_DIM)==3 NULL, #endif NULL); test_curved_data.mortar_err = 0.; if (world_rank == 0) printf("mortar_err = %.20f\n", test_curved_data.mortar_err ); p4est_ghost_destroy (ghost); P4EST_FREE (ghost_data); ghost = NULL; ghost_data = NULL; /* curved_hp_amr(p4est, */ /* &u, */ /* test_nonconform_random_hp, */ /* NULL, */ /* NULL, */ /* NULL, */ /* dgmath_jit_dbase */ /* ); */ /* curved_element_data_init(p4est, geometric_factors, dgmath_jit_dbase, geom, -1); */ /* double* u_vertex = P4EST_ALLOC(double, p4est->local_num_quadrants*(P4EST_CHILDREN)); */ /* element_data_store_nodal_vec_in_vertex_array */ /* ( */ /* p4est, */ /* u, */ /* u_vertex */ /* ); */ /* char sol_save_as [500]; */ /* sprintf(sol_save_as, "%s_test_nonconform_sym_level_%d_u", P4EST_STRING, i); */ /* curved_hacked_p4est_vtk_write_all */ /* (p4est, */ /* NULL, */ /* 0.99, */ /* 0, */ /* 1, */ /* 1, */ /* 0, */ /* 1, */ /* 0, */ /* sol_save_as, */ /* "u", */ /* u_vertex */ /* ); */ /* P4EST_FREE(u_vertex); */ /* ip_flux_params_t ip_flux_params; */ /* ip_flux_params.ip_flux_penalty_prefactor = atoi(argv[6]); */ /* ip_flux_params.ip_flux_penalty_calculate_fcn = sipg_flux_vector_calc_penalty_maxp2_over_minh; */ /* problem_data_t vecs; */ /* vecs.u = u; */ /* vecs.local_nodes = element_data_get_local_nodes(p4est); */ /* vecs.vector_flux_fcn_data = sipg_flux_vector_dirichlet_fetch_fcns */ /* ( */ /* zero_fcn, */ /* &ip_flux_params */ /* ); */ /* vecs.scalar_flux_fcn_data = sipg_flux_scalar_dirichlet_fetch_fcns(zero_fcn); */ /* weakeqn_ptrs_t fcns; */ /* fcns.apply_lhs = poisson_apply_aij; */ /* matrix_sym_tester */ /* ( */ /* p4est, */ /* &vecs, /\* only needed for # of nodes *\/ */ /* &fcns, */ /* .000000000000001, */ /* dgmath_jit_dbase, */ /* 0, */ /* 0 */ /* ); */ /* } */ P4EST_FREE(u); geometric_factors_destroy(geometric_factors); /* free pointers */ dgmath_jit_dbase_destroy(dgmath_jit_dbase); /* free pXest */ p4est_destroy(p4est); /* p4est_destroy(p4est); */ if (geom != NULL) { p4est_geometry_destroy (geom); } p4est_connectivity_destroy(conn); /* finalize mpi stuff */ sc_finalize(); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI(mpiret); }
/** Timestep the advection problem. * * Update the state, refine, repartition, and write the solution to file. * * \param [in,out] p4est the forest, whose state is updated * \param [in] time the end time */ static void step3_timestep (p4est_t * p4est, double time) { double t = 0.; double dt = 0.; int i; step3_data_t *ghost_data; step3_ctx_t *ctx = (step3_ctx_t *) p4est->user_pointer; int refine_period = ctx->refine_period; int repartition_period = ctx->repartition_period; int write_period = ctx->write_period; int recursive = 0; int allowed_level = P4EST_QMAXLEVEL; int allowcoarsening = 1; int callbackorphans = 0; int mpiret; double orig_max_err = ctx->max_err; double umax, global_umax; p4est_ghost_t *ghost; /* create the ghost quadrants */ ghost = p4est_ghost_new (p4est, P4EST_CONNECT_FULL); /* create space for storing the ghost data */ ghost_data = P4EST_ALLOC (step3_data_t, ghost->ghosts.elem_count); /* synchronize the ghost data */ p4est_ghost_exchange_data (p4est, ghost, ghost_data); /* initialize du/dx estimates */ p4est_iterate (p4est, ghost, (void *) ghost_data, /* pass in ghost data that we just exchanged */ step3_reset_derivatives, /* blank the previously calculated derivatives */ step3_minmod_estimate, /* compute the minmod estimate of each cell's derivative */ #ifdef P4_TO_P8 NULL, /* there is no callback for the edges between quadrants */ #endif NULL); /* there is no callback for the corners between quadrants */ for (t = 0., i = 0; t < time; t += dt, i++) { P4EST_GLOBAL_PRODUCTIONF ("time %f\n", t); /* refine */ if (!(i % refine_period)) { if (i) { /* compute umax */ umax = 0.; /* initialize derivative estimates */ p4est_iterate (p4est, NULL, (void *) &umax, /* pass in ghost data that we just exchanged */ step3_compute_max, /* blank the previously calculated derivatives */ NULL, /* there is no callback for the faces between quadrants */ #ifdef P4_TO_P8 NULL, /* there is no callback for the edges between quadrants */ #endif NULL); /* there is no callback for the corners between quadrants */ mpiret = sc_MPI_Allreduce (&umax, &global_umax, 1, sc_MPI_DOUBLE, sc_MPI_MAX, p4est->mpicomm); SC_CHECK_MPI (mpiret); ctx->max_err = orig_max_err * global_umax; P4EST_GLOBAL_PRODUCTIONF ("u_max %f\n", global_umax); /* adapt */ p4est_refine_ext (p4est, recursive, allowed_level, step3_refine_err_estimate, NULL, step3_replace_quads); p4est_coarsen_ext (p4est, recursive, callbackorphans, step3_coarsen_err_estimate, NULL, step3_replace_quads); p4est_balance_ext (p4est, P4EST_CONNECT_FACE, NULL, step3_replace_quads); p4est_ghost_destroy (ghost); P4EST_FREE (ghost_data); ghost = NULL; ghost_data = NULL; } dt = step3_get_timestep (p4est); } /* repartition */ if (i && !(i % repartition_period)) { p4est_partition (p4est, allowcoarsening, NULL); if (ghost) { p4est_ghost_destroy (ghost); P4EST_FREE (ghost_data); ghost = NULL; ghost_data = NULL; } } /* write out solution */ if (!(i % write_period)) { step3_write_solution (p4est, i); } /* synchronize the ghost data */ if (!ghost) { ghost = p4est_ghost_new (p4est, P4EST_CONNECT_FULL); ghost_data = P4EST_ALLOC (step3_data_t, ghost->ghosts.elem_count); p4est_ghost_exchange_data (p4est, ghost, ghost_data); } /* compute du/dt */ /* *INDENT-OFF* */ p4est_iterate (p4est, /* the forest */ ghost, /* the ghost layer */ (void *) ghost_data, /* the synchronized ghost data */ step3_quad_divergence, /* callback to compute each quad's interior contribution to du/dt */ step3_upwind_flux, /* callback to compute each quads' faces' contributions to du/du */ #ifdef P4_TO_P8 NULL, /* there is no callback for the edges between quadrants */ #endif NULL); /* there is no callback for the corners between quadrants */ /* *INDENT-ON* */ /* update u */ p4est_iterate (p4est, NULL, /* ghosts are not needed for this loop */ (void *) &dt, /* pass in dt */ step3_timestep_update, /* update each cell */ NULL, /* there is no callback for the faces between quadrants */ #ifdef P4_TO_P8 NULL, /* there is no callback for the edges between quadrants */ #endif NULL); /* there is no callback for the corners between quadrants */ /* synchronize the ghost data */ p4est_ghost_exchange_data (p4est, ghost, ghost_data); /* update du/dx estimate */ p4est_iterate (p4est, ghost, (void *) ghost_data, /* pass in ghost data that we just exchanged */ step3_reset_derivatives, /* blank the previously calculated derivatives */ step3_minmod_estimate, /* compute the minmod estimate of each cell's derivative */ #ifdef P4_TO_P8 NULL, /* there is no callback for the edges between quadrants */ #endif NULL); /* there is no callback for the corners between quadrants */ } P4EST_FREE (ghost_data); p4est_ghost_destroy (ghost); }
int main(int argc, char **argv) { int mpiret, i; mpi_context_t mpi_context, *mpi = &mpi_context; p4est_t *p4est; p4est_connectivity_t *connectivity; pchase_world_t *W; /* initialize MPI and p4est internals */ mpiret = MPI_Init(&argc, &argv); SC_CHECK_MPI(mpiret); mpi->mpicomm = MPI_COMM_WORLD; /* your favourite comm here */ mpiret = MPI_Comm_size(mpi->mpicomm, &mpi->mpisize); SC_CHECK_MPI(mpiret); mpiret = MPI_Comm_rank(mpi->mpicomm, &mpi->mpirank); SC_CHECK_MPI(mpiret); /* Sets global program identifiers (e.g. the MPIrank) and some flags */ sc_init(mpi->mpicomm, 1, 1, NULL, SC_LP_SILENT); /* Registers p4est with the SC Library and sets the logging behavior */ p4est_init(NULL, SC_LP_SILENT); /* build up the world */ W = pchase_world_init(); /* store connectivity for a unitsquare */ connectivity = p4est_connectivity_new_unitsquare(); /* build uniform tree and get space for 25 particles each */ p4est = p4est_new_ext(mpi->mpicomm, connectivity, 0, 4, 1, sizeof(pchase_quadrant_data_t), W->init_fn, NULL); /* initialize everything depending on p4est */ pchase_world_init_p4est(W, p4est); /* if (W->p4est->mpirank == 0) { */ /* for (i=0; i<100; i++) */ /* * sc_list_append(W->particle_push_list, * pchase_world_random_particle(W)); */ /* } */ pchase_particle_t * p = P4EST_ALLOC(pchase_particle_t, 1); p->x[0] = 0.5; p->x[1] = 0.1; sc_list_append(W->particle_push_list, p); /* this has to be done for each proc */ pchase_world_insert_particles(W); pchase_world_insert_particles(W); /* let this particle move */ pchase_world_simulate(W); #ifdef DEBUG /* print out all quadrants */ p4est_iterate(W->p4est, NULL, NULL, W->viter_fn, NULL, NULL); #endif /* destroy all particles, p4est and its connectivity structure */ pchase_world_destroy(W); p4est_destroy(p4est); p4est_connectivity_destroy(connectivity); /* clean up and exit */ sc_finalize(); mpiret = MPI_Finalize(); SC_CHECK_MPI(mpiret); return 0; }
p4est_mesh_t * p4est_mesh_new_ext (p4est_t * p4est, p4est_ghost_t * ghost, int compute_tree_index, int compute_level_lists, p4est_connect_type_t btype) { int do_corner = 0; int do_volume = 0; int rank; p4est_locidx_t lq, ng; p4est_locidx_t jl; p4est_mesh_t *mesh; P4EST_ASSERT (p4est_is_balanced (p4est, btype)); mesh = P4EST_ALLOC_ZERO (p4est_mesh_t, 1); lq = mesh->local_num_quadrants = p4est->local_num_quadrants; ng = mesh->ghost_num_quadrants = (p4est_locidx_t) ghost->ghosts.elem_count; if (btype == P4EST_CONNECT_FULL) { do_corner = 1; } do_volume = (compute_tree_index || compute_level_lists ? 1 : 0); /* Optional map of tree index for each quadrant */ if (compute_tree_index) { mesh->quad_to_tree = P4EST_ALLOC (p4est_topidx_t, lq); } mesh->ghost_to_proc = P4EST_ALLOC (int, ng); mesh->quad_to_quad = P4EST_ALLOC (p4est_locidx_t, P4EST_FACES * lq); mesh->quad_to_face = P4EST_ALLOC (int8_t, P4EST_FACES * lq); mesh->quad_to_half = sc_array_new (P4EST_HALF * sizeof (p4est_locidx_t)); /* Optional per-level lists of quadrants */ if (compute_level_lists) { mesh->quad_level = P4EST_ALLOC (sc_array_t, P4EST_QMAXLEVEL + 1); for (jl = 0; jl <= P4EST_QMAXLEVEL; ++jl) { sc_array_init (mesh->quad_level + jl, sizeof (p4est_locidx_t)); } } /* Populate ghost information */ rank = 0; for (jl = 0; jl < ng; ++jl) { while (ghost->proc_offsets[rank + 1] <= jl) { ++rank; P4EST_ASSERT (rank < p4est->mpisize); } mesh->ghost_to_proc[jl] = rank; } /* Fill face arrays with default values */ memset (mesh->quad_to_quad, -1, P4EST_FACES * lq * sizeof (p4est_locidx_t)); memset (mesh->quad_to_face, -25, P4EST_FACES * lq * sizeof (int8_t)); if (do_corner) { /* Initialize corner information to a consistent state */ mesh->quad_to_corner = P4EST_ALLOC (p4est_locidx_t, P4EST_CHILDREN * lq); memset (mesh->quad_to_corner, -1, P4EST_CHILDREN * lq * sizeof (p4est_locidx_t)); mesh->corner_offset = sc_array_new (sizeof (p4est_locidx_t)); *(p4est_locidx_t *) sc_array_push (mesh->corner_offset) = 0; mesh->corner_quad = sc_array_new (sizeof (p4est_locidx_t)); mesh->corner_corner = sc_array_new (sizeof (int8_t)); } /* Call the forest iterator to collect face connectivity */ p4est_iterate (p4est, ghost, mesh, (do_volume ? mesh_iter_volume : NULL), mesh_iter_face, #ifdef P4_TO_P8 NULL, #endif do_corner ? mesh_iter_corner : NULL); return mesh; }