void Triangulation::calculate_boundaries() { _VERBOSE("Triangulation::calculate_boundaries"); get_neighbors(); // Ensure _neighbors has been created. // Create set of all boundary TriEdges, which are those which do not // have a neighbor triangle. typedef std::set<TriEdge> BoundaryEdges; BoundaryEdges boundary_edges; for (int tri = 0; tri < _ntri; ++tri) { if (!is_masked(tri)) { for (int edge = 0; edge < 3; ++edge) { if (get_neighbor(tri, edge) == -1) { boundary_edges.insert(TriEdge(tri, edge)); } } } } // Take any boundary edge and follow the boundary until return to start // point, removing edges from boundary_edges as they are used. At the same // time, initialise the _tri_edge_to_boundary_map. while (!boundary_edges.empty()) { // Start of new boundary. BoundaryEdges::iterator it = boundary_edges.begin(); int tri = it->tri; int edge = it->edge; _boundaries.push_back(Boundary()); Boundary& boundary = _boundaries.back(); while (true) { boundary.push_back(TriEdge(tri, edge)); boundary_edges.erase(it); _tri_edge_to_boundary_map[TriEdge(tri, edge)] = BoundaryEdge(_boundaries.size()-1, boundary.size()-1); // Move to next edge of current triangle. edge = (edge+1) % 3; // Find start point index of boundary edge. int point = get_triangle_point(tri, edge); // Find next TriEdge by traversing neighbors until find one // without a neighbor. while (get_neighbor(tri, edge) != -1) { tri = get_neighbor(tri, edge); edge = get_edge_in_triangle(tri, point); } if (TriEdge(tri,edge) == boundary.front()) break; // Reached beginning of this boundary, so finished it. else it = boundary_edges.find(TriEdge(tri, edge)); } } }
bool invade_bond(Bond & inv_bond) { Site invSite = inv_bond.second.second; if (invadedSites.count(invSite) > 0){ trapped.push_back(inv_bond); return true; } double newStrength; Site newNeighbor; Bond newBond; growth.push_back(inv_bond); invadedSites.insert(invSite); for (int dir=0; dir<6; dir++) { newNeighbor = get_neighbor(invSite, dir); if (invadedSites.count(newNeighbor) == 0) { newStrength = drand48(); newBond = std::make_pair(newStrength, std::make_pair(invSite, newNeighbor)); accessibleBonds.insert(std::make_pair(newStrength, newBond)); } } return false; }
bool invade_bond(Bond & inv_bond, double strength) { Site invSite = inv_bond.second; if (invadedSites.count(invSite) > 0){ removed.push_back(std::make_pair(strength,inv_bond)); return true; } double newStrength; Site newNeighbor; Bond newBond; growth.push_back(std::make_pair(strength,inv_bond)); invadedSites.insert(invSite); for (int dir=0;dir<4;++dir) { newNeighbor = get_neighbor(invSite, dir); if (invadedSites.count(newNeighbor) == 0) { newStrength = get_new_strength(dir); if(newStrength < 1.0 || accessibleBonds.size() < 10000) { newBond = std::make_pair(invSite, newNeighbor); accessibleBonds.insert(std::make_pair(newStrength, newBond)); } } } return false; }
float Grid_2D :: get_edge(pair<uint32_t, uint32_t> start, cell_adjacent neighbor) { pair<uint32_t, uint32_t> neighbor_node = neighbor_to_pair(start, neighbor); if(!is_valid_node(start) || !is_neighbor(start, neighbor_node)) { return INFINITY; } uint8_t start_weight = get_weight(start); uint8_t neighbor_weight = get_weight(get_neighbor(start, neighbor)); if(start_weight == INF || neighbor_weight == INF) { return INFINITY; } float edge = start_weight + neighbor_weight; edge /= 2; if(neighbor == TOP_LEFT || neighbor == TOP_RIGHT || neighbor == BOTTOM_LEFT || neighbor == BOTTOM_RIGHT) { edge *= sqrt(2); } return edge; }
static nbr_entry_t* update_neighbor(nbr_state_t *s, uint16_t saddr, int16_t seqno, bool *duplicate) { nbr_entry_t *nb = get_neighbor(s, saddr); int16_t sDelta; if(nb == NULL) { *duplicate = true; return NULL; } *duplicate = false; sDelta = (seqno - nb->lastSeqno - 1); if(nb->flags & NBRFLAG_NEW) { nb->received++; nb->lastSeqno = seqno; nb->flags ^= NBRFLAG_NEW; } else if(sDelta >= 0) { nb->missed += sDelta; nb->received++; nb->lastSeqno = seqno; } else if(sDelta < ACCEPTABLE_MISSED) { // Something happend to this node. Reinitialize it's state init_nb(nb, saddr); nb->received++; nb->lastSeqno = seqno; nb->flags ^= NBRFLAG_NEW; } else { *duplicate = true; } return nb; }
/*! ******************************************************************************* \brief La fonction ajoute un voisin dans la liste des voisins du Cluster head. \return retourne la valeur du nouvel element */ neighbor_desc_t *add_neighbor( neighbor_desc_t **neighbor_entry, ///< pointeur sur l'entree de la liste de voisinage L2_ID *L2_id ///< ID du nouveau noeud ) { neighbor_desc_t *pNewItem = get_neighbor( *neighbor_entry, L2_id ); if ( pNewItem == NULL ) { // If not Exist then create a new item neighbor_desc_t *pOldEntry = *neighbor_entry; pNewItem = RRM_MALLOC(neighbor_desc_t , 1 ) ; PNULL(pNewItem) ; if ( pNewItem == NULL ) return NULL ; memcpy( pNewItem->L2_id.L2_id , L2_id->L2_id, sizeof(L2_ID) ) ; //pNewItem->RSSI = 0 ; pNewItem->NB_neighbor = 0 ; pNewItem->Sensing_meas= NULL; *neighbor_entry = pNewItem ; pNewItem->next = pOldEntry; } PRINT_NEIGHBOR_DB( *neighbor_entry ); return pNewItem ; }
static char* get_neighbor(char *hash, int direction) { int hash_length = strlen(hash); char last_char = hash[hash_length - 1]; int is_odd = hash_length % 2; char **border = is_odd ? odd_borders : even_borders; char **neighbor = is_odd ? odd_neighbors : even_neighbors; char *base = emalloc(sizeof(char) * (hash_length+1)); base[0] = '\0'; strncat(base, hash, hash_length - 1); if(index_for_char(last_char, border[direction]) != -1){ char *base_temp = get_neighbor(base, direction); strcpy( base, base_temp ); efree( base_temp ); } int neighbor_index = index_for_char(last_char, neighbor[direction]); last_char = char_map[neighbor_index]; char *last_hash = emalloc(sizeof(char) * 2); last_hash[0] = last_char; last_hash[1] = '\0'; strcat(base, last_hash); efree(last_hash); return base; }
/* * Recursive depth-first search algorithm for solving your maze. * This function should also print out either every visited room as it goes, or * the final pruned solution, depending on whether the FULL macro is set. * * Note that you may need to add extra parameters to this function's signature, * depending on how you choose to store the pruned solution. * * See handout for more details, as well as a pseudocode implementation. * * Parameters: * - row: row of the current room * - col: column of the current room * - goal_row: row of the goal room * - goal_col: col of the goal room * - num_rows: number of rows in the maze * - num_cols: number of columns in the maze * - maze: an array of maze_room structs representing your maze * - file: the file to write the solution to * - full: 1 to print full path, 0 to print prunned path * * Returns: * - 1 if the current branch finds a valid solution, 0 if no branches are valid. */ int dfs(int row, int col, int goal_row, int goal_col, int num_rows, int num_cols, struct maze_room maze[], FILE *file, int full) { if(row == goal_row && col == goal_col){ return 1; } //printf("dfs(%d, %d)\n", row, col); fprintf(file, "(%d, %d)\n", row, col); int index = get_index(row, col, num_cols); maze[index].visited = 1; Direction dirs[4] = {NORTH, SOUTH, WEST, EAST}; int i; for(i=0; i<4; i++){ int neighbor_out = neighbor_out_bound(num_rows, num_cols, &maze[index], i); if(neighbor_out == 0){ struct maze_room *neighbor = get_neighbor(maze,num_rows, num_cols, &maze[index], i); if(maze[index].walls[i] == 0 && neighbor->visited == 0){ //printf(" at (%d, %d), visiting neighbor at dir: %d \n", row, col, i); int neighbor_path = dfs(neighbor->row, neighbor->col, goal_row, goal_col, num_rows, num_cols, maze, file, full); if(neighbor_path == 1){ printf("setting neighboor from index %d", index); maze[index].next = neighbor; return 1; } else { if(full == 1){ printf("setting neighboor from index %d", index); maze[index].next = neighbor; } } } } } return 0; }
TriEdge Triangulation::get_neighbor_edge(int tri, int edge) const { int neighbor_tri = get_neighbor(tri, edge); if (neighbor_tri == -1) return TriEdge(-1,-1); else return TriEdge(neighbor_tri, get_edge_in_triangle(neighbor_tri, get_triangle_point(tri, (edge+1)%3))); }
// Acts as Ruby API wrapper to get_neighbor function, which is recursive and does nasty C things static VALUE calculate_adjacent(VALUE self, VALUE geohash, VALUE dir) { char *str; VALUE ret_val; Check_Type(geohash, T_STRING); Check_Type(dir, T_FIXNUM); str = RSTRING_PTR(geohash); if (!strlen(str)) return Qnil; ret_val = rb_str_new(str,strlen(str)); get_neighbor(RSTRING_PTR(ret_val), NUM2INT(dir), strlen(str)); return ret_val; }
void initialize_sim(void) { Site start = std::make_pair(0, 0); invadedSites.insert(start); for (int neigh=0;neigh<4; neigh++) { Bond newBond; double newStrength =get_new_strength(neigh); newBond = std::make_pair(start, get_neighbor(start, neigh)); accessibleBonds.insert(std::make_pair(newStrength, newBond)); } }
// 3D full weight restriction void restriction( double* R, double* R_new, cuint I, cuint J, cuint K, cuint I_new, cuint J_new, cuint K_new ) { unsigned int nei[3][3][3]; #pragma omp parallel for shared(R, R_new) private(nei) num_threads(nt) for(int i=0; i<I; i+=2){ for(int j=0; j<J; j+=2){ for(int k=0; k<K; k+=2){ get_neighbor( nei, i,j,k, I,J,K); // get new index uint t_new; three_d_to_one_d( i/2, j/2, k/2, I_new, J_new, t_new); coarse_map( R, R_new, nei, i,j,k, t_new); } } } }
void invade_bond(Bond & inv_bond) { Site invSite = inv_bond.second.second; double newStrength; double tempWeight = inv_bond.first; Site newNeighbor; Bond newBond; growth.push_back(inv_bond); std::deque<Bond> tempList; for (int dir=0;dir<4;++dir) { newNeighbor = get_neighbor(invSite, dir); if (invadedSites.count(newNeighbor) == 0) { newStrength = drand48(); tempWeight += newStrength; newBond = std::make_pair(newStrength, std::make_pair(invSite, newNeighbor)); tempList.push_back(newBond); } } invSite.weight = tempWeight; invadedSites.insert(invSite); if(invSite.weight < threshold) { while (!tempList.empty()) { newBond = tempList.front(); tempList.pop_front(); accessibleBonds.insert(std::make_pair(newBond.first, newBond)); } } else { while (!tempList.empty()) { newBond = tempList.front(); tempList.pop_front(); trapped.push_back(newBond); } }; }
TEST(Graph, GetNeighbor) { // Graph G = graphs::RandomGraph(20,0.5); Graph G = graphs::Petersen(); G.add_edges({{1, 0}, {0, 2}, {2, 4}, {3, 5}, {3, 1}, {8, 4}, {1, 9}}); for (auto v : G.vertices()) { for (auto u : G.vertices()) { auto it = G.get_neighbor(u, v); if (G.is_neighbor(u, v)) { ASSERT_EQ(*it, v); } else { ASSERT_EQ(it, G.neighbors(u).end()); } } } auto sortedG = G; sortedG.sort_neighbors(); for (auto v : sortedG.vertices()) { for (auto u : sortedG.vertices()) { auto it = sortedG.get_neighbor(u, v); if (sortedG.is_neighbor(u, v)) { ASSERT_EQ(*it, v); } else { ASSERT_EQ(it, sortedG.neighbors(u).end()); } } } }
// Given a particular geohash string, a direction, and a final length // Compute a neighbor using base32 lookups, recursively when necessary void get_neighbor(char *str, int dir, int hashlen) { /* Right, Left, Top, Bottom */ static char *neighbors[] = { "bc01fg45238967deuvhjyznpkmstqrwx", "238967debc01fg45kmstqrwxuvhjyznp", "p0r21436x8zb9dcf5h7kjnmqesgutwvy", "14365h7k9dcfesgujnmqp0r2twvyx8zb" }; static char *borders[] = { "bcfguvyz", "0145hjnp", "prxz", "028b" }; char last_chr, *border, *neighbor; int index = ( 2 * (hashlen % 2) + dir) % 4; neighbor = neighbors[index]; border = borders[index]; last_chr = str[hashlen-1]; if (strchr(border,last_chr)) get_neighbor(str, dir, hashlen-1); str[hashlen-1] = BASE32[strchr(neighbor, last_chr)-neighbor]; }
void DGFEMContext::neighbor_side_fe_reinit () { // Call this *after* side_fe_reinit // Initialize all the neighbor side FE objects based on inverse mapping // the quadrature points on the current side std::vector<Point> qface_side_points; std::vector<Point> qface_neighbor_points; std::map<FEType, FEAbstract *>::iterator local_fe_end = _neighbor_side_fe.end(); for (std::map<FEType, FEAbstract *>::iterator i = _neighbor_side_fe.begin(); i != local_fe_end; ++i) { FEType neighbor_side_fe_type = i->first; FEAbstract* side_fe = _side_fe[neighbor_side_fe_type]; qface_side_points = side_fe->get_xyz(); FEInterface::inverse_map (dim, neighbor_side_fe_type, &get_neighbor(), qface_side_points, qface_neighbor_points); i->second->reinit(&get_neighbor(), &qface_neighbor_points); } // Set boolean flag to indicate that the DG terms are active on this element _dg_terms_active = true; // Also, initialize data required for DG assembly on the current side, // analogously to FEMContext::pre_fe_reinit // Initialize the per-element data for elem. get_system().get_dof_map().dof_indices (&get_neighbor(), _neighbor_dof_indices); const unsigned int n_dofs = dof_indices.size(); const unsigned int n_neighbor_dofs = libmesh_cast_int<unsigned int> (_neighbor_dof_indices.size()); // These resize calls also zero out the residual and jacobian _neighbor_residual.resize(n_neighbor_dofs); _elem_elem_jacobian.resize(n_dofs, n_dofs); _elem_neighbor_jacobian.resize(n_dofs, n_neighbor_dofs); _neighbor_elem_jacobian.resize(n_neighbor_dofs, n_dofs); _neighbor_neighbor_jacobian.resize(n_neighbor_dofs, n_neighbor_dofs); // Initialize the per-variable data for elem. { unsigned int sub_dofs = 0; for (unsigned int i=0; i != get_system().n_vars(); ++i) { get_system().get_dof_map().dof_indices (&get_neighbor(), _neighbor_dof_indices_var[i], i); const unsigned int n_dofs_var = libmesh_cast_int<unsigned int> (_neighbor_dof_indices_var[i].size()); _neighbor_subresiduals[i]->reposition (sub_dofs, n_dofs_var); for (unsigned int j=0; j != i; ++j) { const unsigned int n_dofs_var_j = libmesh_cast_int<unsigned int> (dof_indices_var[j].size()); _elem_elem_subjacobians[i][j]->reposition (sub_dofs, _neighbor_subresiduals[j]->i_off(), n_dofs_var, n_dofs_var_j); _elem_elem_subjacobians[j][i]->reposition (_neighbor_subresiduals[j]->i_off(), sub_dofs, n_dofs_var_j, n_dofs_var); _elem_neighbor_subjacobians[i][j]->reposition (sub_dofs, _neighbor_subresiduals[j]->i_off(), n_dofs_var, n_dofs_var_j); _elem_neighbor_subjacobians[j][i]->reposition (_neighbor_subresiduals[j]->i_off(), sub_dofs, n_dofs_var_j, n_dofs_var); _neighbor_elem_subjacobians[i][j]->reposition (sub_dofs, _neighbor_subresiduals[j]->i_off(), n_dofs_var, n_dofs_var_j); _neighbor_elem_subjacobians[j][i]->reposition (_neighbor_subresiduals[j]->i_off(), sub_dofs, n_dofs_var_j, n_dofs_var); _neighbor_neighbor_subjacobians[i][j]->reposition (sub_dofs, _neighbor_subresiduals[j]->i_off(), n_dofs_var, n_dofs_var_j); _neighbor_neighbor_subjacobians[j][i]->reposition (_neighbor_subresiduals[j]->i_off(), sub_dofs, n_dofs_var_j, n_dofs_var); } _elem_elem_subjacobians[i][i]->reposition (sub_dofs, sub_dofs, n_dofs_var, n_dofs_var); _elem_neighbor_subjacobians[i][i]->reposition (sub_dofs, sub_dofs, n_dofs_var, n_dofs_var); _neighbor_elem_subjacobians[i][i]->reposition (sub_dofs, sub_dofs, n_dofs_var, n_dofs_var); _neighbor_neighbor_subjacobians[i][i]->reposition (sub_dofs, sub_dofs, n_dofs_var, n_dofs_var); sub_dofs += n_dofs_var; } libmesh_assert_equal_to (sub_dofs, n_dofs); } }
int main(int argc, char **argv) { long int i, N; bool already_invaded; N = atoi(argv[1]); threshold = atof(argv[2]); srand48(atoi(argv[3])); Site start; start.loc = std::make_pair(0, 0); start.weight = 0; invadedSites.insert(start); for (int neigh=0;neigh<4; neigh++) { Bond newBond; double newStrength = drand48(); newBond = std::make_pair(newStrength, std::make_pair(start, get_neighbor(start, neigh))); accessibleBonds.insert(std::make_pair(newStrength, newBond)); } while(!accessibleBonds.empty()) { BondMap::iterator weakest; weakest = accessibleBonds.begin(); accessibleBonds.erase(weakest); invade_bond(weakest->second); i++; if(i == N) { accessibleBonds.clear(); } } std::stringstream fileName; fileName << "fractures" << argv[3] << ".txt"; std::ofstream toFile1(fileName.str().c_str(), std::ios::trunc); // std::ofstream toFile2("trapped.txt", std::ios::trunc); toFile1 << growth.size() << "\n"; // toFile2 << trapped.size() << "\n"; // toFile1 << "Invasion for: temp" << "\n"; // toFile2 << "Trapping for: temp" << "\n"; // toFile1.precision(17); // toFile2.precision(17); // // Bond current_Line; // while (!growth.empty()) // { // current_Line = growth.front(); // growth.pop_front(); // toFile1 << current_Line.first << "\t"; // toFile1 << current_Line.second.first.loc.first << "\t"; // toFile1 << current_Line.second.first.loc.second << "\t"; // toFile1 << current_Line.second.second.loc.first << "\t"; // toFile1 << current_Line.second.second.loc.second << "\n"; // } // // // while (!trapped.empty()) // { // current_Line = trapped.front(); // trapped.pop_front(); // toFile2 << current_Line.first << "\t"; // toFile2 << current_Line.second.first.loc.first << "\t"; // toFile2 << current_Line.second.first.loc.second << "\t"; // toFile2 << current_Line.second.second.loc.first << "\t"; // toFile2 << current_Line.second.second.loc.second << "\n"; // } // // toFile1.close(); // toFile2.close(); return 0; }
int main(int argc, char** argv) { int rank, size; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); int mpi_lattice_size = lround(sqrt(size)); if(size < 4 || ! (mpi_lattice_size*mpi_lattice_size==size) ) { printf("You have to use a square number of MPI processes."); MPI_Abort(MPI_COMM_WORLD,1); } if( find_option( argc, argv, "-h" ) >= 0 ) { printf( "Options:\n" ); printf( "-h to see this help\n" ); printf( "-n <int> to set the grid size\n" ); printf( "-o <filename> to specify the output file name\n" ); MPI_Abort(MPI_COMM_WORLD,1); } int GRIDSIZE = read_int( argc, argv, "-n", DEFAULT_GRIDSIZE ); // Check gridsize for some basic assumptions if(GRIDSIZE%2 || GRIDSIZE%(mpi_lattice_size)) { printf("Only even Gridsize allowed and\nGridsize has to be a multiple of the number of MPI procs!\n"); MPI_Abort(MPI_COMM_WORLD,1); } FILE *f; if(rank==0) { char *savename = read_string( argc, argv, "-o", "sample_conduct.txt" ); f = savename ? fopen( savename, "w" ) : NULL; if( f == NULL ) { printf( "failed to open %s\n", savename ); MPI_Abort(MPI_COMM_WORLD,1); } } int my_mpi_i,my_mpi_j; int my_gridsize= GRIDSIZE/(mpi_lattice_size); int t,i,j; double *T, *Tn,*Tf; my_mpi_j=rank%mpi_lattice_size; my_mpi_i=rank/mpi_lattice_size; // Allocate Grid with a border on every side int padded_grid_size = my_gridsize+2; if(rank == 0) { Tf=(double *) malloc(GRIDSIZE*GRIDSIZE*sizeof(double)); } T=(double *) malloc((padded_grid_size)*(padded_grid_size)*sizeof(double)); Tn=(double *) malloc((padded_grid_size)*(padded_grid_size)*sizeof(double)); // remember -- our grid has a border around it! if(rank==0) init_cells(Tf,GRIDSIZE); else { int i,j; for (i=0;i<padded_grid_size;i++) for(j=0;j<padded_grid_size;j++) T[i*padded_grid_size+j]=0; } if(rank==0) { for(i=0;i<mpi_lattice_size;i++) { for(j=0;j<mpi_lattice_size;j++) { if(i==0 && j==0) { int k,l,m=1,n=1; for(k=0;k<padded_grid_size;k++) for(l=0;l<padded_grid_size;l++) T[k*padded_grid_size+l]=0; for(k=0;k<my_gridsize;k++) { for(l=0;l<my_gridsize;l++) { T[m*padded_grid_size+n]=Tf[k*(GRIDSIZE) + l]; n++; } m++; n=1; } continue; } //re-use Tn for temp arrays int k,l,m=1,n=1; for(k=0;k<padded_grid_size;k++) for(l=0;l<padded_grid_size;l++) Tn[k*padded_grid_size+l]=0; for(k=0;k<my_gridsize;k++) { for(l=0;l<my_gridsize;l++) { Tn[m*padded_grid_size+n]=Tf[(k + my_gridsize*i)*(GRIDSIZE) + l+my_gridsize*j]; n++; } m++; n=1; } int dest; dest=i*mpi_lattice_size+j; // printf("\nDest: %d from %d where %d\n ",dest,size,mpi_lattice_size); MPI_Send(Tn,padded_grid_size*padded_grid_size,MPI_DOUBLE,dest,42,MPI_COMM_WORLD); } } } else { // printf("\nRank: %d %d %d\n ",rank,my_mpi_i,my_mpi_j); MPI_Recv(T,padded_grid_size*padded_grid_size,MPI_DOUBLE,0,42,MPI_COMM_WORLD,MPI_STATUS_IGNORE); } // printf("\nRank: %d %d %d\n ",rank,my_mpi_i,my_mpi_j); // print(T,padded_grid_size,rank); // MPI_Barrier(MPI_COMM_WORLD); // exit(1); for(t=1;t<=TIMESTEPS;t++) { // Loop for the time steps // get the neighbors: put_neighbor(T,0,1,my_mpi_i,my_mpi_j,mpi_lattice_size,my_gridsize); get_neighbor(T,0,-1,my_mpi_i,my_mpi_j,mpi_lattice_size,my_gridsize); put_neighbor(T,1,0,my_mpi_i,my_mpi_j,mpi_lattice_size,my_gridsize); get_neighbor(T,-1,0,my_mpi_i,my_mpi_j,mpi_lattice_size,my_gridsize); put_neighbor(T,0,-1,my_mpi_i,my_mpi_j,mpi_lattice_size,my_gridsize); get_neighbor(T,0,1,my_mpi_i,my_mpi_j,mpi_lattice_size,my_gridsize); put_neighbor(T,-1,0,my_mpi_i,my_mpi_j,mpi_lattice_size,my_gridsize); get_neighbor(T,1,0,my_mpi_i,my_mpi_j,mpi_lattice_size,my_gridsize); MPI_Barrier(MPI_COMM_WORLD); /* printf("\nI am: %d\n",rank); print(T,padded_grid_size,t); printf("\n"); */ for(i=1;i<my_gridsize+1;i++) { for(j=1;j<my_gridsize+1;j++) { Tn[i*padded_grid_size + j] = T[(i-1)*padded_grid_size + (j)] + T[(i+1)*padded_grid_size + (j)] + T[(i)*padded_grid_size + (j-1)] + T[(i)*padded_grid_size + (j+1)]; Tn[i*padded_grid_size + j] /= 4; } } for(i=1;i<my_gridsize+1;i++) { for(j=1;j<my_gridsize+1;j++) { T[i*padded_grid_size + j] = Tn[i*padded_grid_size + j]; } } /* MPI_Barrier(MPI_COMM_WORLD); printf("\nI am: %d\n",rank); print(T,padded_grid_size,t); printf("\n"); */ if(!(t % PRINTSTEP)) { if(rank==0) { for(i=0;i<mpi_lattice_size;i++) { for(j=0;j<mpi_lattice_size;j++) { if(i==0 && j==0) { int k,l,m=1,n=1; for(k=0;k<my_gridsize;k++) { for(l=0;l<my_gridsize;l++) { Tf[k*(GRIDSIZE) + l]=T[m*padded_grid_size+n]; n++; } m++; n=1; } } else { int source=i*mpi_lattice_size+j; MPI_Recv(Tn,padded_grid_size*padded_grid_size,MPI_DOUBLE,source,42,MPI_COMM_WORLD,MPI_STATUS_IGNORE); //re-use Tn for temp arrays int k,l,m=1,n=1; for(k=0;k<my_gridsize;k++) { for(l=0;l<my_gridsize;l++) { Tf[(k + my_gridsize*i)*(GRIDSIZE) + l+my_gridsize*j]=Tn[m*padded_grid_size+n]; n++; } m++; n=1; } } } } } else { MPI_Send(T,padded_grid_size*padded_grid_size,MPI_DOUBLE,0,42,MPI_COMM_WORLD); } if(rank==0) { // print(Tf,GRIDSIZE,t); save(f,Tf,GRIDSIZE,t); printf("Time: %d\n",t); } } } if(rank==0) fclose(f); MPI_Finalize(); return 0; }
int aud_move(struct POINT *cur_p, int dir) { int ret = 0; struct POINT *neighbor = NULL; switch(dir) { case EAST: neighbor = get_neighbor(cur_p, dir); if(neighbor) { GO_EAST(*cur_p); GO_WEST(*neighbor); } #ifdef DEBUG ret = ROBOT_MOVE(cur_p->x + 1, cur_p->y); #else move(cur_p->x + 1, cur_p->y); #endif break; case SOUTH: neighbor = get_neighbor(cur_p, dir); if(neighbor) { GO_SOUTH(*cur_p); GO_NORTH(*neighbor); } #ifdef DEBUG ret = ROBOT_MOVE(cur_p->x, cur_p->y - 1); #else move(cur_p->x, cur_p->y - 1); #endif break; case WEST: neighbor = get_neighbor(cur_p, dir); if(neighbor) { GO_WEST(*cur_p); GO_EAST(*neighbor); } #ifdef DEBUG ret = ROBOT_MOVE(cur_p->x - 1, cur_p->y); #else move(cur_p->x - 1, cur_p->y); #endif break; case NORTH: neighbor = get_neighbor(cur_p, dir); if(neighbor) { GO_NORTH(*cur_p); GO_SOUTH(*neighbor); } #ifdef DEBUG ret = ROBOT_MOVE(cur_p->x, cur_p->y + 1); #else move(cur_p->x, cur_p->y + 1); #endif break; default: ret = 0x00; } return ret; }
ErrorCode ScdInterface::get_shared_vertices(ParallelComm *, ScdBox *, std::vector<int> &, std::vector<int> &, std::vector<int> &) { return MB_FAILURE; #else ErrorCode ScdInterface::get_shared_vertices(ParallelComm *pcomm, ScdBox *box, std::vector<int> &procs, std::vector<int> &offsets, std::vector<int> &shared_indices) { // get index of partitioned dimension const int *ldims = box->box_dims(); ErrorCode rval; int ijkrem[6], ijkface[6], across_bdy[3]; for (int k = -1; k <= 1; k ++) { for (int j = -1; j <= 1; j ++) { for (int i = -1; i <= 1; i ++) { if (!i && !j && !k) continue; int pto; int dijk[] = {i, j, k}; rval = get_neighbor(pcomm->proc_config().proc_size(), pcomm->proc_config().proc_rank(), box->par_data(), dijk, pto, ijkrem, ijkface, across_bdy); if (MB_SUCCESS != rval) return rval; if (-1 != pto) { if (procs.empty() || pto != *procs.rbegin()) { procs.push_back(pto); offsets.push_back(shared_indices.size()); } rval = get_indices(ldims, ijkrem, across_bdy, ijkface, shared_indices); if (MB_SUCCESS != rval) return rval; // check indices against known #verts on local and remote // begin of this block is shared_indices[*offsets.rbegin()], end is shared_indices.end(), halfway // is (shared_indices.size()-*offsets.rbegin())/2 #ifndef NDEBUG int start_idx = *offsets.rbegin(), end_idx = shared_indices.size(), mid_idx = (start_idx+end_idx)/2; int num_local_verts = (ldims[3]-ldims[0]+1)*(ldims[4]-ldims[1]+1)* (-1 == ldims[2] && -1 == ldims[5] ? 1 : (ldims[5]-ldims[2]+1)), num_remote_verts = (ijkrem[3]-ijkrem[0]+1)*(ijkrem[4]-ijkrem[1]+1)* (-1 == ijkrem[2] && -1 == ijkrem[5] ? 1 : (ijkrem[5]-ijkrem[2]+1)); assert(*std::min_element(&shared_indices[start_idx], &shared_indices[mid_idx]) >= 0 && *std::max_element(&shared_indices[start_idx], &shared_indices[mid_idx]) < num_local_verts && *std::min_element(&shared_indices[mid_idx], &shared_indices[end_idx]) >= 0 && *std::max_element(&shared_indices[mid_idx], &shared_indices[end_idx]) < num_remote_verts); #endif } } } } offsets.push_back(shared_indices.size()); return MB_SUCCESS; #endif } } // namespace moab
/** Build the maze by removing walls according to Prim's algorithm. * * @param maze a maze with all walls present. */ static void build_prim(maze_t* maze) { // MST edges and cells. If (a, b) in mst_edges, then (b, a) not in // mst_edges. (a, b) in mst_edges iff a, b both in mst_cells. list356_t* mst_edges = make_list() ; list356_t* mst_cells = make_list() ; // The frontier. This is the collection of edges between cells in the MST // and cells not in the MST. If (a, b) in frontier, then a in mst_cells // and b not in mst_cells. list356_t* frontier = make_list() ; // Choose two adjacent cells at random to put into the MST, then // populate the frontier accordinately. For simplicitly, choose a // cell in the interior of the maze, then randomly choose a direction // for the other cell. cell_t* start = get_cell(maze, random_limit(1, maze->nrows-1), random_limit(1, maze->ncols-1)); unsigned char direction = 1 << random_limit(0, 4) ; cell_t* next = get_neighbor(maze, start, direction) ; /* debug("Removing (%d, %d) - (%d, %d).\n", start->r, start->c, next->r, next->c) ; */ remove_wall(maze, start, direction) ; edge_t init_edge = (edge_t){start, next} ; lst_add(mst_edges, &init_edge) ; lst_add(mst_cells, start) ; lst_add(mst_cells, next) ; for (int d=0; d<4; ++d) { if (directions[d] != direction && is_cell(maze, start, directions[d])) { cell_t* c = get_neighbor(maze, start, directions[d]) ; edge_t* e = malloc(sizeof(edge_t)) ; e->a = start ; e->b = c ; lst_add(frontier, e) ; } } for (int d=0; d<4; ++d) { if (directions[d] != opposite(direction) && is_cell(maze, next, directions[d])) { cell_t* c = get_neighbor(maze, next, directions[d]) ; edge_t* e = malloc(sizeof(edge_t)) ; e->a = next ; e->b = c ; lst_add(frontier, e) ; } } // As long as we don't have all the cells in the MST, choose an // edge in the frontier at random. Put the edge in the MST // and compute the new edges to add to the frontier. while (lst_size(mst_cells) < (maze->nrows)*(maze->ncols)) { int p = random_limit(0, lst_size(frontier)) ; edge_t* edge = lst_get(frontier, p) ; cell_t* old_cell = edge->a ; cell_t* new_cell = edge->b ; /* debug("Removing (%d, %d) - (%d, %d).\n", old_cell->r, old_cell->c, new_cell->r, new_cell->c) ; */ remove_wall(maze, old_cell, get_direction(old_cell, new_cell)) ; lst_add(mst_edges, edge) ; lst_add(mst_cells, new_cell) ; for (int d=0; d<4; ++d) { unsigned char dir = directions[d] ; if (is_cell(maze, new_cell, dir)) { cell_t* adj_cell = get_neighbor(maze, new_cell, dir) ; edge_t* edge2 = malloc(sizeof(edge_t)) ; edge2->a = new_cell ; edge2->b = adj_cell ; if (lst_contains(mst_cells, adj_cell, cell_cmp)) { lst_remove(frontier, edge2, edge_cmp) ; if (adj_cell != old_cell) free(edge2) ; } else { lst_add(frontier, edge2) ; } } } } }
/** Remove a wall from the maze. * * @param m a maze. * @param c a cell in <code>m</code>. * @param d a direction. */ static void remove_wall(maze_t* m, cell_t* c, unsigned char d) { MAZECELL(m, c) |= d ; cell_t* adj = get_neighbor(m, c, d) ; MAZECELL(m, adj) |= opposite(d) ; }
int main(int argc, char **argv) { long int i, N; bool already_invaded; N = atoi(argv[1]); Site start = std::make_tuple(0, 0, 0); invadedSites.insert(start); for (int neigh=0;neigh<4; neigh++) { Bond newBond; double newStrength = drand48(); newBond = std::make_pair(newStrength, std::make_pair(start, get_neighbor(start, neigh))); accessibleBonds.insert(std::make_pair(newStrength, newBond)); } for (i=0; i<N;) { BondMap::iterator weakest; weakest = accessibleBonds.begin(); already_invaded = invade_bond(weakest->second); if(!already_invaded) { i++; } accessibleBonds.erase(weakest); } std::ofstream toFile1("fractures.txt", std::ios::trunc); std::ofstream toFile2("trapped.txt", std::ios::trunc); toFile1 << growth.size() << "\n"; toFile2 << trapped.size() << "\n"; toFile1 << "Invasion for: temp" << "\n"; toFile2 << "Trapping for: temp" << "\n"; toFile1.precision(17); toFile2.precision(17); Bond current_Line; while (!growth.empty()) { current_Line = growth.front(); growth.pop_front(); toFile1 << current_Line.first << "\t"; toFile1 << std::get<0>(current_Line.second.first) << "\t"; toFile1 << std::get<1>(current_Line.second.first) << "\t"; toFile1 << std::get<2>(current_Line.second.first) << "\t"; toFile1 << std::get<0>(current_Line.second.second) << "\t"; toFile1 << std::get<1>(current_Line.second.second) << "\t"; toFile1 << std::get<2>(current_Line.second.second) << "\n"; } while (!trapped.empty()) { current_Line = trapped.front(); trapped.pop_front(); toFile2 << current_Line.first << "\t"; toFile2 << std::get<0>(current_Line.second.first) << "\t"; toFile2 << std::get<1>(current_Line.second.first) << "\t"; toFile2 << std::get<2>(current_Line.second.first) << "\t"; toFile2 << std::get<0>(current_Line.second.second) << "\t"; toFile2 << std::get<1>(current_Line.second.second) << "\t"; toFile2 << std::get<2>(current_Line.second.second) << "\n"; } toFile1.close(); toFile2.close(); return 0; }