/* Prints graph in dot language */ std::ostream& DirectionalGraph::print( std::ostream& out ) const{ out << "digraph DataFlowGraph{" << std::endl; /* Print nodes */ const_node_iterator node = getBeginNode(); const_node_iterator endNode = getEndNode(); for( ; node != endNode; node++ ){ out << *node << std::endl; } /* Print arrows between nodes */ node = getBeginNode(); endNode = getEndNode(); for( ; node != endNode; node++ ){ const node_set outArrows = getOutNodesSet(*node); const_node_iterator nodeOut = outArrows.begin(); const_node_iterator endNodeOut = outArrows.end(); for( ; nodeOut != endNodeOut; nodeOut++ ){ out << *node << "->" << *nodeOut << std::endl; } } out << '}' << std::endl; return out; }
void BranchInfo::_insertVariable(node_type &variable, node_set &variables) { if ((variables.find(variable) != variables.end()) || (_divergGraph.isDivNode(variable))) { return; } #ifdef DEBUG std::cout << "Variable " << variable << " taints: " << std::endl; #endif node_set newVariables; newVariables.insert(variable); while (newVariables.size() > 0) { node_type newVar = *newVariables.begin(); variables.insert(newVar); newVariables.erase(newVar); #ifdef DEBUG std::cout << newVar << ' '; #endif node_set dependences = _divergGraph.getOutNodesSet(newVar); node_set::const_iterator depend = dependences.begin(); node_set::const_iterator endDepend = dependences.end(); for (; depend != endDepend; depend++) { if ((variables.find(*depend) == variables.end()) && (!_divergGraph.isDivNode(*depend))) { newVariables.insert(*depend); } } } #ifdef DEBUG std::cout << std::endl; #endif }
double compute_deformed_length(node_set& nodes, EquationSystems* equation_systems) { System& X_system = equation_systems->get_system<System>(IBFEMethod::COORDS_SYSTEM_NAME); const unsigned int X_sys_num = X_system.number(); NumericVector<double>* X_vec = X_system.solution.get(); AutoPtr<NumericVector<Number> > X_serial_vec = NumericVector<Number>::build(X_vec->comm()); X_serial_vec->init(X_vec->size(), true, SERIAL); X_vec->localize(*X_serial_vec); // Get the current positions of the points. std::vector<IBTK::Point> points; points.reserve(nodes.size()); IBTK::Point p; for (typename node_set::iterator it = nodes.begin(); it != nodes.end(); ++it) { Node* node = *it; for (unsigned int d = 0; d < NDIM; ++d) { p(d) = (*X_serial_vec)(node->dof_number(X_sys_num, d, 0)); } points.push_back(p); } // Compute the length of the center line. IBTK::Point p0, p1; double l = 0.0; std::vector<IBTK::Point>::iterator it = points.begin(); p0 = *it; ++it; for (; it != points.end(); ++it) { p1 = *it; double l_segment_sq = 0.0; for (int d = 0; d < NDIM; ++d) { l_segment_sq += pow(p0(d) - p1(d), 2.0); } l += sqrt(l_segment_sq); p0 = p1; } return l; }
double compute_displaced_area(node_set& nodes, EquationSystems* equation_systems) { System& X_system = equation_systems->get_system<System>(IBFEMethod::COORDS_SYSTEM_NAME); const unsigned int X_sys_num = X_system.number(); NumericVector<double>* X_vec = X_system.solution.get(); AutoPtr<NumericVector<Number> > X_serial_vec = NumericVector<Number>::build(X_vec->comm()); X_serial_vec->init(X_vec->size(), true, SERIAL); X_vec->localize(*X_serial_vec); // Get the current positions of the points. std::vector<IBTK::Point> points; points.reserve(nodes.size()); IBTK::Point p; for (typename node_set::iterator it = nodes.begin(); it != nodes.end(); ++it) { Node* node = *it; for (unsigned int d = 0; d < NDIM; ++d) { p(d) = (*X_serial_vec)(node->dof_number(X_sys_num, d, 0)); } points.push_back(p); } // Compute the area of the polygon. IBTK::Point p0, p1; double A2 = 0.0; p0 = *points.rbegin(); for (std::vector<IBTK::Point>::iterator it = points.begin(); it != points.end(); ++it) { p1 = *it; A2 += p0(0) * p1(1) - p0(1) * p1(0); p0 = p1; } return 0.5 * abs(A2); }
//寻找最短路径,最短路径存储在path中。返回值为路径长度 int Dijkstra(path_vec& path) { //初始化各个变量 path.clear(); dist2start.clear(); open_set.clear(); close_set.clear(); close_set.insert(start_node); for(int i=0; i<node_count; i++) { if(i != start_node) open_set.insert(i); dist2start[i] = std::make_pair(start_node, GetCrtMap(start_node,i) ); } //开始寻路 while(!open_set.empty()) { path_pair min_cost = std::make_pair(start_node, BIG_NUMBER); int min_cost_node = -1; for(note_set_iter nodeToDo = open_set.begin(); nodeToDo != open_set.end(); nodeToDo++) { for(note_set_iter nodeDone = close_set.begin(); nodeDone != close_set.end(); nodeDone++) { int tmp_cost = dist2start[*nodeDone].second + GetCrtMap(*nodeDone, *nodeToDo); if(min_cost.second >= tmp_cost) { min_cost.first = *nodeDone; min_cost.second = tmp_cost; min_cost_node = *nodeToDo; } } } assert(min_cost_node != start_node); if(min_cost_node < 0) return -1; open_set.erase(min_cost_node); close_set.insert(min_cost_node); dist2start[min_cost_node] = (min_cost); // update nodes connected with min_cost_node for(note_set_iter nodeRel = open_set.begin(); nodeRel != open_set.end(); nodeRel++) { int tmp_dist = GetCrtMap(min_cost_node, *nodeRel); if(tmp_dist < BIG_NUMBER && min_cost.second + tmp_dist < dist2start[*nodeRel].second) { dist2start[*nodeRel].first = min_cost_node; dist2start[*nodeRel].second = min_cost.second + tmp_dist < dist2start[*nodeRel].second; } } } //输出路径 int crtNode = end_node; do { path.insert(path.begin(), crtNode); crtNode = dist2start[crtNode].first; }while( crtNode != start_node ); path.insert(path.begin(), start_node); return dist2start[end_node].second; }
/*!\brief Prints the divergence graph in dot language */ std::ostream& DivergenceGraph::print( std::ostream& out ) const{ using ir::PTXOperand; out << "digraph DirtyVariablesGraph{" << endl; /* Print dirt sources */ map<PTXOperand::SpecialRegister, node_set>::const_iterator dirt = _specials.begin(); map<PTXOperand::SpecialRegister, node_set>::const_iterator endDirt = _specials.end(); out << "//Dirt sources:" << endl; for( ; dirt != endDirt; dirt++ ){ if( dirt->second.size() ){ out << getSpecialName(dirt->first) << "[style=filled, fillcolor = \"" << (isDivSource(dirt->first)?"tomato":"lightblue") << "\"]" << endl; } } /* Print nodes */ out << "//Nodes:" << endl; const_node_iterator node = getBeginNode(); const_node_iterator endNode = getEndNode(); for( ; node != endNode; node++ ){ out << *node << " [style=filled, fillcolor = \"" << (isDivNode(*node)?"lightyellow":"white") << "\"]" << endl; } out << endl; /* Print edges coming out of dirt sources */ dirt = _specials.begin(); endDirt = _specials.end(); out << "//Dirt out edges:" << endl; for( ; dirt != endDirt; dirt++ ){ if( dirt->second.size() ){ node = dirt->second.begin(); endNode = dirt->second.end(); for( ; node != endNode; node++ ){ out << getSpecialName(dirt->first) << "->" << *node << "[color = \"" << (isDivSource(dirt->first)?"red":"blue") << "\"]" << endl; } } } /* Print arrows between nodes */ node = getBeginNode(); endNode = getEndNode(); out << "//Nodes edges:" << endl; for( ; node != endNode; node++ ){ const node_set outArrows = getOutNodesSet(*node); const_node_iterator nodeOut = outArrows.begin(); const_node_iterator endNodeOut = outArrows.end(); for( ; nodeOut != endNodeOut; nodeOut++ ){ out << *node << "->" << *nodeOut << endl; } } out << '}'; return out; }